This repository has been 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
/
delegates.ts
149 lines (134 loc) · 4.86 KB
/
delegates.ts
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import {
DELEGATED_STAKE_UNLOCK_LENGTH,
INSUFFICIENT_FUNDS_MESSAGE,
INVALID_GATEWAY_REGISTERED_MESSAGE,
MAX_DELEGATES,
NETWORK_LEAVING_STATUS,
} from './constants';
import {
Balances,
BlockHeight,
Gateways,
TransactionId,
WalletAddress,
mIOToken,
} from './types';
import {
unsafeDecrementBalance,
walletHasSufficientBalance,
} from './utilities';
export function safeDelegateStake({
balances,
gateways,
fromAddress,
gatewayAddress,
qty,
startHeight,
}: {
balances: Balances;
gateways: Gateways;
fromAddress: WalletAddress;
gatewayAddress: WalletAddress;
qty: mIOToken;
startHeight: BlockHeight;
}): void {
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);
}
const gateway = gateways[gatewayAddress];
if (!gateway) {
throw new ContractError(INVALID_GATEWAY_REGISTERED_MESSAGE);
}
if (gateway.status === NETWORK_LEAVING_STATUS) {
throw new ContractError(
'This Gateway is in the process of leaving the network and cannot have more stake delegated to it.',
);
}
// TODO: when allowedDelegates is supported, check if it's in the array of allowed delegates
if (!gateway.settings.allowDelegatedStaking) {
throw new ContractError(
`This Gateway does not allow delegated staking. Only allowed delegates can delegate stake to this Gateway.`,
);
}
if (Object.keys(gateway.delegates).length > MAX_DELEGATES) {
throw new ContractError(
`This Gateway has reached its maximum amount of delegated stakers.`,
);
}
// TODO: some behaviors we could consider - do we require a delegate to only be able to increase stake if they are above the current minimum stake of the operator
// Additionally, if you're a delegate and not up to the current minimum, do you get any rewards?
const existingDelegate = gateway.delegates[fromAddress];
const minimumStakeForGatewayAndDelegate =
// it already has a stake that is not zero
existingDelegate && existingDelegate.delegatedStake !== 0
? 1 // delegate must provide at least one additional IO - we may want to change this to a higher amount. also need to consider if the operator increases the minmimum amount after you've already staked
: gateway.settings.minDelegatedStake;
if (qty.valueOf() < minimumStakeForGatewayAndDelegate) {
throw new ContractError(
`Qty must be greater than the minimum delegated stake amount.`,
);
}
// If this delegate has staked before, update its amount, if not, create a new delegated staker
// The quantity must also be greater than the minimum delegated stake set by the gateway
if (!existingDelegate) {
// create the new delegate stake
gateways[gatewayAddress].delegates[fromAddress] = {
delegatedStake: qty.valueOf(),
start: startHeight.valueOf(),
vaults: {},
};
} else {
// increment the existing delegate's stake
existingDelegate.delegatedStake += qty.valueOf();
}
// increase the total delegated stake for the gateway - TODO: this could be computed, as opposed to set in state
gateways[gatewayAddress].totalDelegatedStake += qty.valueOf();
// decrement the caller's balance
unsafeDecrementBalance(balances, fromAddress, qty);
}
export function safeDecreaseDelegateStake({
gateways,
fromAddress,
gatewayAddress,
qty,
id,
startHeight,
}: {
gateways: Gateways;
fromAddress: WalletAddress;
gatewayAddress: WalletAddress;
qty: mIOToken;
id: TransactionId;
startHeight: BlockHeight;
}): void {
if (!gateways[gatewayAddress]) {
throw new ContractError(INVALID_GATEWAY_REGISTERED_MESSAGE);
}
const gateway = gateways[gatewayAddress];
const existingDelegate = gateway.delegates[fromAddress];
if (!existingDelegate) {
throw new ContractError('This delegate is not staked at this gateway.');
}
const existingStake = new mIOToken(existingDelegate.delegatedStake);
const requiredMinimumStake = new mIOToken(gateway.settings.minDelegatedStake);
const maxAllowedToWithdraw = existingStake.minus(requiredMinimumStake);
if (maxAllowedToWithdraw.isLessThan(qty) && !qty.equals(existingStake)) {
throw new ContractError(
`Remaining delegated stake must be greater than the minimum delegated stake amount.`,
);
}
// Withdraw the qty delegate's stake
gateways[gatewayAddress].delegates[fromAddress].delegatedStake -=
qty.valueOf();
// Lock the qty in a vault to be unlocked after withdrawal period
gateways[gatewayAddress].delegates[fromAddress].vaults[id] = {
balance: qty.valueOf(),
start: startHeight.valueOf(),
end: startHeight.plus(DELEGATED_STAKE_UNLOCK_LENGTH).valueOf(),
};
// Decrease the gateway's total delegated stake.
gateways[gatewayAddress].totalDelegatedStake -= qty.valueOf();
}