This repository was archived by the owner on Oct 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathtransfer.ts
More file actions
101 lines (91 loc) · 2.24 KB
/
Copy pathtransfer.ts
File metadata and controls
101 lines (91 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import {
INSUFFICIENT_FUNDS_MESSAGE,
INVALID_TARGET_MESSAGE,
INVALID_VAULT_LOCK_LENGTH_MESSAGE,
MAX_TOKEN_LOCK_BLOCK_LENGTH,
MIN_TOKEN_LOCK_BLOCK_LENGTH,
} from './constants';
import {
Balances,
BlockHeight,
RegistryVaults,
TransactionId,
VaultData,
WalletAddress,
mIOToken,
} from './types';
import {
incrementBalance,
unsafeDecrementBalance,
walletHasSufficientBalance,
} from './utilities';
export function safeTransfer({
balances,
fromAddress,
toAddress,
qty,
}: {
balances: Balances;
fromAddress: WalletAddress;
toAddress: WalletAddress;
qty: mIOToken;
}): void {
// do not do anything if the transfer quantity is less than 1
if (qty.valueOf() < 1) {
return;
}
if (fromAddress === toAddress) {
throw new ContractError(INVALID_TARGET_MESSAGE);
}
if (balances[fromAddress] === null || isNaN(balances[fromAddress])) {
throw new ContractError(`Caller balance is not defined!`);
}
if (!walletHasSufficientBalance(balances, fromAddress, qty)) {
throw new ContractError(INSUFFICIENT_FUNDS_MESSAGE);
}
incrementBalance(balances, toAddress, qty);
unsafeDecrementBalance(balances, fromAddress, qty);
}
export function safeVaultedTransfer({
balances,
vaults,
fromAddress,
toAddress,
startHeight,
id,
qty,
lockLength,
}: {
balances: Balances;
vaults: RegistryVaults;
fromAddress: WalletAddress;
toAddress: WalletAddress;
id: TransactionId;
qty: mIOToken;
startHeight: BlockHeight;
lockLength: BlockHeight;
}): void {
if (!walletHasSufficientBalance(balances, fromAddress, qty)) {
throw new ContractError(INSUFFICIENT_FUNDS_MESSAGE);
}
if (vaults[toAddress] && id in vaults[toAddress]) {
throw new ContractError(`Vault with id '${id}' already exists`);
}
if (
lockLength.valueOf() < MIN_TOKEN_LOCK_BLOCK_LENGTH ||
lockLength.valueOf() > MAX_TOKEN_LOCK_BLOCK_LENGTH
) {
throw new ContractError(INVALID_VAULT_LOCK_LENGTH_MESSAGE);
}
const newVault: VaultData = {
balance: qty.valueOf(),
start: startHeight.valueOf(),
end: startHeight.valueOf() + lockLength.valueOf(),
};
// create the new vault
vaults[toAddress] = {
...vaults[toAddress],
[id]: newVault,
};
unsafeDecrementBalance(balances, fromAddress, qty);
}