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
/
distributions.test.ts
211 lines (202 loc) · 5.9 KB
/
distributions.test.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import {
INSUFFICIENT_FUNDS_MESSAGE,
INVALID_GATEWAY_REGISTERED_MESSAGE,
MIN_DELEGATED_STAKE,
} from './constants';
import {
safeDelegateDistribution,
safeGatewayStakeDistribution,
} from './distributions';
import {
stubbedArweaveTxId,
stubbedDelegateData,
stubbedDelegatedGatewayData,
stubbedGateways,
} from './tests/stubs';
import { DelegateData, mIOToken } from './types';
describe('safeDelegateDistribution function', () => {
it.each([
[{ foo: 1, bar: 2 }, 'baz'],
[{ foo: Number.NaN, bar: 2 }, 'foo'],
[{ foo: Math.sqrt(-1), bar: 2 }, 'foo'],
])(
"should throw an error if balances %p can't be used to retrieve address %s",
(balances, protocolAddress) => {
expect(() => {
safeDelegateDistribution({
balances,
gateways: {
...stubbedGateways,
['a-gateway']: {
...stubbedDelegatedGatewayData,
},
},
qty: new mIOToken(1),
protocolAddress,
gatewayAddress: 'a-gateway',
delegateAddress: 'delegate-1',
});
}).toThrowError('Caller balance is not defined!');
},
);
it('should throw an error if address does not have enough balance', () => {
expect(() => {
safeDelegateDistribution({
balances: { foo: 1, bar: 2 },
gateways: {
...stubbedGateways,
['a-gateway']: {
...stubbedDelegatedGatewayData,
},
},
qty: new mIOToken(2),
protocolAddress: 'foo',
gatewayAddress: 'a-gateway',
delegateAddress: 'delegate-1',
});
}).toThrowError(INSUFFICIENT_FUNDS_MESSAGE);
});
it('should throw an error if gateway does not exist', () => {
expect(() => {
safeDelegateDistribution({
balances: { foo: 1, bar: 2 },
gateways: {
...stubbedGateways,
['a-gateway']: {
...stubbedDelegatedGatewayData,
},
},
qty: new mIOToken(1),
protocolAddress: 'foo',
gatewayAddress: 'doesnt-exist',
delegateAddress: 'delegate-1',
});
}).toThrowError(INVALID_GATEWAY_REGISTERED_MESSAGE);
});
it('should throw an error if delegate does not exist in gateway', () => {
expect(() => {
safeDelegateDistribution({
balances: { foo: 1, bar: 2 },
gateways: {
...stubbedGateways,
['a-gateway']: {
...stubbedDelegatedGatewayData,
},
},
qty: new mIOToken(1),
protocolAddress: 'foo',
gatewayAddress: 'a-gateway',
delegateAddress: 'doesnt-exist',
});
}).toThrowError('Delegate not staked on this gateway.');
});
it('should distribute stake to delegate', () => {
const balances = { foo: MIN_DELEGATED_STAKE.valueOf(), bar: 2 };
const gateways = {
[stubbedArweaveTxId]: {
...stubbedDelegatedGatewayData,
},
};
const qty = new mIOToken(1);
const protocolAddress = 'foo';
const gatewayAddress = stubbedArweaveTxId;
const delegateAddress = 'delegate-1';
const expectedNewDelegateData: DelegateData = {
delegatedStake: stubbedDelegateData.delegatedStake + qty.valueOf(),
start: 0,
vaults: {},
};
safeDelegateDistribution({
balances,
gateways,
qty,
protocolAddress,
gatewayAddress,
delegateAddress,
});
expect(gateways[gatewayAddress].delegates[delegateAddress]).toEqual({
...expectedNewDelegateData,
});
expect(gateways[gatewayAddress].totalDelegatedStake).toEqual(
stubbedDelegatedGatewayData.totalDelegatedStake + qty.valueOf(),
);
});
});
describe('safeGatewayStakeDistribution function', () => {
it.each([
[{ foo: 1, bar: 2 }, 'baz'],
[{ foo: Number.NaN, bar: 2 }, 'foo'],
[{ foo: Math.sqrt(-1), bar: 2 }, 'foo'],
])(
"should throw an error if balances %p can't be used to retrieve address %s",
(balances, protocolAddress) => {
expect(() => {
safeGatewayStakeDistribution({
balances,
gateways: {
...stubbedGateways,
['a-gateway']: {
...stubbedDelegatedGatewayData,
},
},
qty: new mIOToken(1),
protocolAddress,
gatewayAddress: 'a-gateway',
});
}).toThrowError('Caller balance is not defined!');
},
);
it('should throw an error if address does not have enough balance', () => {
expect(() => {
safeGatewayStakeDistribution({
balances: { foo: 1, bar: 2 },
gateways: {
...stubbedGateways,
['a-gateway']: {
...stubbedDelegatedGatewayData,
},
},
qty: new mIOToken(2),
protocolAddress: 'foo',
gatewayAddress: 'a-gateway',
});
}).toThrowError(INSUFFICIENT_FUNDS_MESSAGE);
});
it('should throw an error if gateway does not exist', () => {
expect(() => {
safeGatewayStakeDistribution({
balances: { foo: 1, bar: 2 },
gateways: {
...stubbedGateways,
['a-gateway']: {
...stubbedDelegatedGatewayData,
},
},
qty: new mIOToken(1),
protocolAddress: 'foo',
gatewayAddress: 'doesnt-exist',
});
}).toThrowError(INVALID_GATEWAY_REGISTERED_MESSAGE);
});
it('should distribute stake to gateway', () => {
const balances = { foo: MIN_DELEGATED_STAKE.valueOf(), bar: 2 };
const gateways = {
[stubbedArweaveTxId]: {
...stubbedDelegatedGatewayData,
},
};
const qty = new mIOToken(1);
const protocolAddress = 'foo';
const gatewayAddress = stubbedArweaveTxId;
const expectedNewQty =
qty.valueOf() + stubbedDelegatedGatewayData.operatorStake;
safeGatewayStakeDistribution({
balances,
gateways,
qty,
protocolAddress,
gatewayAddress,
});
expect(gateways[gatewayAddress].operatorStake).toEqual(expectedNewQty);
});
});