@defisaver/automation-sdk 2.0.2 → 2.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.js +1 -0
- package/esm/abis/index.js +27 -15
- package/esm/automation/private/Automation.js +9 -6
- package/esm/automation/private/LegacyAutomation.d.ts +5 -5
- package/esm/automation/private/LegacyAutomation.js +30 -23
- package/esm/automation/private/LegacyProtocol.js +4 -1
- package/esm/automation/private/Protocol.js +4 -1
- package/esm/automation/private/StrategiesAutomation.d.ts +2 -2
- package/esm/automation/private/StrategiesAutomation.js +27 -21
- package/esm/automation/public/ArbitrumStrategies.js +10 -4
- package/esm/automation/public/EthereumStrategies.js +10 -4
- package/esm/automation/public/OptimismStrategies.js +10 -4
- package/esm/automation/public/legacy/LegacyAaveAutomation.js +13 -7
- package/esm/automation/public/legacy/LegacyCompoundAutomation.js +13 -7
- package/esm/automation/public/legacy/LegacyMakerAutomation.js +13 -7
- package/esm/configuration.js +8 -5
- package/esm/constants/index.js +281 -275
- package/esm/index.js +57 -17
- package/esm/services/contractService.js +22 -14
- package/esm/services/ethereumService.js +18 -10
- package/esm/services/strategiesService.js +130 -103
- package/esm/services/strategySubService.js +90 -61
- package/esm/services/subDataService.js +151 -145
- package/esm/services/triggerService.d.ts +5 -5
- package/esm/services/triggerService.js +124 -94
- package/esm/services/utils.js +82 -33
- package/esm/services/utils.test.d.ts +1 -0
- package/esm/services/utils.test.js +362 -0
- package/esm/types/contracts/generated/Erc20.js +2 -1
- package/esm/types/contracts/generated/Legacy_AaveV2Subscriptions.js +2 -1
- package/esm/types/contracts/generated/Legacy_AuthCheck.js +2 -1
- package/esm/types/contracts/generated/Legacy_CompoundV2Subscriptions.js +2 -1
- package/esm/types/contracts/generated/Legacy_MakerSubscriptions.js +2 -1
- package/esm/types/contracts/generated/SubStorage.js +2 -1
- package/esm/types/contracts/generated/UniMulticall.js +2 -1
- package/esm/types/contracts/generated/index.js +2 -1
- package/esm/types/contracts/generated/types.js +2 -1
- package/esm/types/enums.js +17 -14
- package/esm/types/index.js +2 -1
- package/package.json +11 -7
- package/src/automation/private/LegacyAutomation.ts +11 -10
- package/src/automation/private/StrategiesAutomation.ts +5 -6
- package/src/configuration.ts +0 -3
- package/src/index.ts +0 -1
- package/src/services/ethereumService.ts +6 -6
- package/src/services/subDataService.ts +75 -76
- package/src/services/triggerService.ts +83 -83
- package/src/services/utils.test.ts +414 -0
- package/src/services/utils.ts +6 -7
- package/tsconfig.json +1 -1
- package/umd/index.js +7016 -4105
- package/src/types/typings/process.d.ts +0 -9
- package/yarn-error.log +0 -7233
|
@@ -0,0 +1,414 @@
|
|
|
1
|
+
import { expect } from 'chai';
|
|
2
|
+
import * as web3Utils from 'web3-utils';
|
|
3
|
+
import AbiCoder from 'web3-eth-abi';
|
|
4
|
+
import { getAssetInfo } from '@defisaver/tokens';
|
|
5
|
+
|
|
6
|
+
import { EthereumAddress } from '../types';
|
|
7
|
+
import { ChainId, RatioState } from '../types/enums';
|
|
8
|
+
|
|
9
|
+
import { sparkEncode } from './strategySubService';
|
|
10
|
+
|
|
11
|
+
import {
|
|
12
|
+
addToArrayIf,
|
|
13
|
+
addToObjectIf,
|
|
14
|
+
compareAddresses,
|
|
15
|
+
compareSubHashes,
|
|
16
|
+
encodeSubId,
|
|
17
|
+
ethToWeth,
|
|
18
|
+
getRatioStateInfoForAaveCloseStrategy,
|
|
19
|
+
isAddress,
|
|
20
|
+
isDefined,
|
|
21
|
+
isEmptyBytes,
|
|
22
|
+
isRatioStateOver,
|
|
23
|
+
isRatioStateUnder,
|
|
24
|
+
isUndefined,
|
|
25
|
+
ratioPercentageToWei,
|
|
26
|
+
requireAddress,
|
|
27
|
+
requireAddresses,
|
|
28
|
+
weiToRatioPercentage,
|
|
29
|
+
wethToEth,
|
|
30
|
+
wethToEthByAddress,
|
|
31
|
+
} from './utils';
|
|
32
|
+
|
|
33
|
+
describe('Feature: utils.ts', () => {
|
|
34
|
+
describe('When testing utils.isDefined()', () => {
|
|
35
|
+
const examples: Array<[boolean, any]> = [
|
|
36
|
+
[true, 'something'],
|
|
37
|
+
[true, NaN],
|
|
38
|
+
[true, 0],
|
|
39
|
+
[false, undefined],
|
|
40
|
+
[false, null],
|
|
41
|
+
];
|
|
42
|
+
|
|
43
|
+
examples.forEach(([expected, actual]) => {
|
|
44
|
+
it(`Given ${actual} should return expected value: ${expected}`, () => {
|
|
45
|
+
expect(isDefined(actual)).to.equal(expected);
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
describe('When testing utils.isUndefined()', () => {
|
|
51
|
+
const examples: Array<[boolean, any]> = [
|
|
52
|
+
[false, 'something'],
|
|
53
|
+
[false, NaN],
|
|
54
|
+
[false, 0],
|
|
55
|
+
[true, undefined],
|
|
56
|
+
[true, null],
|
|
57
|
+
];
|
|
58
|
+
|
|
59
|
+
examples.forEach(([expected, actual]) => {
|
|
60
|
+
it(`Given ${actual} should return expected value: ${expected}`, () => {
|
|
61
|
+
expect(isUndefined(actual)).to.equal(expected);
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
describe('When testing utils.compareAddresses()', () => {
|
|
67
|
+
const examples: Array<[boolean, [string, string]]> = [
|
|
68
|
+
[true, ['nesto', 'nesto']],
|
|
69
|
+
[true, ['', '']],
|
|
70
|
+
[false, ['something', 'nesto']],
|
|
71
|
+
[false, ['', 'nesto']],
|
|
72
|
+
];
|
|
73
|
+
|
|
74
|
+
examples.forEach(([expected, actual]) => {
|
|
75
|
+
it(`Given ${actual} should return expected value: ${expected}`, () => {
|
|
76
|
+
expect(compareAddresses(...actual)).to.equal(expected);
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
describe('When testing utils.isAddress()', () => {
|
|
82
|
+
const examples: Array<[boolean, string]> = [
|
|
83
|
+
[true, '0x996bc83fa1b947cca00e5dcf776c438096549062'],
|
|
84
|
+
[true, '0x0000000000000000000000000000000000000000'],
|
|
85
|
+
[false, '0x996bc83fa147cca00e5dcf776c438096549062'],
|
|
86
|
+
[false, '0x996bc83fa147cca00e5dcf776c438096549062xx'],
|
|
87
|
+
[false, ''],
|
|
88
|
+
[false, 'majmune.eth'],
|
|
89
|
+
];
|
|
90
|
+
|
|
91
|
+
examples.forEach(([expected, actual]) => {
|
|
92
|
+
it(`Given ${actual} should return expected value: ${expected}`, () => {
|
|
93
|
+
expect(isAddress(actual)).to.equal(expected);
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
describe('When testing utils.addToArrayIf()', () => {
|
|
99
|
+
|
|
100
|
+
const exampleOne = [1, ...addToArrayIf(true, 2)];
|
|
101
|
+
const actualOne = [1, 2];
|
|
102
|
+
|
|
103
|
+
it(`Given ${actualOne.join(', ')} should return expected value: [${exampleOne.join(', ')}]`, () => {
|
|
104
|
+
expect(actualOne).to.eql(exampleOne)
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
const exampleTwo = [1, ...addToArrayIf(false, 2)];
|
|
108
|
+
const actualTwo = [1];
|
|
109
|
+
|
|
110
|
+
it(`Given ${actualTwo.join(', ')} should return expected value: [${exampleTwo.join(', ')}]`, () => {
|
|
111
|
+
expect(actualTwo).to.eql(exampleTwo)
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
describe('When testing utils.addToObjectIf()', () => {
|
|
116
|
+
|
|
117
|
+
const exampleOne = {
|
|
118
|
+
a: 1, ...addToObjectIf(true, { b: 2 })
|
|
119
|
+
};
|
|
120
|
+
const actualOne = { a: 1, b: 2 };
|
|
121
|
+
|
|
122
|
+
it(`Given ${JSON.stringify(actualOne)} should return expected value: ${JSON.stringify(exampleOne)}`, () => {
|
|
123
|
+
expect(actualOne).to.eql(exampleOne)
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
const exampleTwo = {
|
|
127
|
+
a: 1, ...addToObjectIf(false, { b: 2 })
|
|
128
|
+
};
|
|
129
|
+
const actualTwo = { a: 1 };
|
|
130
|
+
|
|
131
|
+
it(`Given ${JSON.stringify(actualTwo)} should return expected value: ${JSON.stringify(exampleTwo)}`, () => {
|
|
132
|
+
expect(actualTwo).to.eql(exampleTwo)
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
describe('When testing utils.ethToWeth()', () => {
|
|
137
|
+
|
|
138
|
+
const examples: Array<[string, string]> = [
|
|
139
|
+
['WETH', 'ETH'],
|
|
140
|
+
['rETH', 'rETH'],
|
|
141
|
+
['ETH.c', 'ETH.c'],
|
|
142
|
+
['cETHx2', 'cETHx2'],
|
|
143
|
+
['WETH', 'WETH'],
|
|
144
|
+
['DAI', 'DAI'],
|
|
145
|
+
];
|
|
146
|
+
|
|
147
|
+
examples.forEach(([expected, actual]) => {
|
|
148
|
+
it(`Given ${actual} should return expected value: ${expected}`, () => {
|
|
149
|
+
expect(ethToWeth(actual)).to.equal(expected);
|
|
150
|
+
});
|
|
151
|
+
});
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
describe('When testing utils.wethToEth()', () => {
|
|
155
|
+
|
|
156
|
+
const examples: Array<[string, string]> = [
|
|
157
|
+
['ETH', 'ETH'],
|
|
158
|
+
['rETH', 'rETH'],
|
|
159
|
+
['ETH.c', 'ETH.c'],
|
|
160
|
+
['cETHx2', 'cETHx2'],
|
|
161
|
+
['ETH', 'WETH'],
|
|
162
|
+
['DAI', 'DAI'],
|
|
163
|
+
];
|
|
164
|
+
|
|
165
|
+
examples.forEach(([expected, actual]) => {
|
|
166
|
+
it(`Given ${actual} should return expected value: ${expected}`, () => {
|
|
167
|
+
expect(wethToEth(actual)).to.equal(expected);
|
|
168
|
+
});
|
|
169
|
+
});
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
describe('When testing utils.wethToEthByAddress()', () => {
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* @dev wethToEthByAddress will return empty string when asset doesn't exist in assets array of @defisaver/tokens
|
|
176
|
+
* This is @defisaver/tokens problem, but still seems like a wierd behaviour
|
|
177
|
+
*/
|
|
178
|
+
const examples: Array<[string, [string, number]]> = [
|
|
179
|
+
['0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee', ['0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2', 1]],
|
|
180
|
+
['0xb3319f5d18bc0d84dd1b4825dcde5d5f7266d407', ['0xb3319f5d18bc0d84dd1b4825dcde5d5f7266d407', 1]],
|
|
181
|
+
['0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee', ['0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee', 1]],
|
|
182
|
+
|
|
183
|
+
['0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee', ['0x4200000000000000000000000000000000000006', 10]],
|
|
184
|
+
['0xab7badef82e9fe11f6f33f87bc9bc2aa27f2fcb5', ['0xab7badef82e9fe11f6f33f87bc9bc2aa27f2fcb5', 10]],
|
|
185
|
+
['0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee', ['0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee', 10]],
|
|
186
|
+
|
|
187
|
+
['0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee', ['0x82aF49447D8a07e3bd95BD0d56f35241523fBab1', 42161]],
|
|
188
|
+
['0xaf88d065e77c8cC2239327C5EDb3A432268e5831', ['0xaf88d065e77c8cC2239327C5EDb3A432268e5831', 42161]],
|
|
189
|
+
['0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee', ['0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee', 42161]],
|
|
190
|
+
|
|
191
|
+
];
|
|
192
|
+
|
|
193
|
+
examples.forEach(([expected, actual]) => {
|
|
194
|
+
it(`Given ${actual} should return expected value: ${expected}`, () => {
|
|
195
|
+
expect(wethToEthByAddress(...actual)).to.equal(expected);
|
|
196
|
+
});
|
|
197
|
+
});
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
describe('When testing utils.compareSubHashes()', () => {
|
|
201
|
+
|
|
202
|
+
const subDataToEncodeOne = [
|
|
203
|
+
12,
|
|
204
|
+
false,
|
|
205
|
+
{
|
|
206
|
+
baseTokenAddress: '0xaf88d065e77c8cC2239327C5EDb3A432268e5831',
|
|
207
|
+
quoteTokenAddress: '0xaf88d065e77c8cC2239327C5EDb3A432268e5831',
|
|
208
|
+
price: 100,
|
|
209
|
+
ratioState: 1,
|
|
210
|
+
},
|
|
211
|
+
{
|
|
212
|
+
collAsset: '0xaf88d065e77c8cC2239327C5EDb3A432268e5831',
|
|
213
|
+
collAssetId: 2,
|
|
214
|
+
debtAsset: '0xaf88d065e77c8cC2239327C5EDb3A432268e5831',
|
|
215
|
+
debtAssetId: 3,
|
|
216
|
+
},
|
|
217
|
+
]
|
|
218
|
+
|
|
219
|
+
const subDataToEncodeTwo = [
|
|
220
|
+
13,
|
|
221
|
+
true,
|
|
222
|
+
{
|
|
223
|
+
baseTokenAddress: '0xaf88d065e77c8cC2239327C5EDb3A432268e5831',
|
|
224
|
+
quoteTokenAddress: '0xaf88d065e77c8cC2239327C5EDb3A432268e5831',
|
|
225
|
+
price: 100,
|
|
226
|
+
ratioState: 2,
|
|
227
|
+
},
|
|
228
|
+
{
|
|
229
|
+
collAsset: '0xaf88d065e77c8cC2239327C5EDb3A432268e5831',
|
|
230
|
+
collAssetId: 2,
|
|
231
|
+
debtAsset: '0xaf88d065e77c8cC2239327C5EDb3A432268e5831',
|
|
232
|
+
debtAssetId: 3,
|
|
233
|
+
},
|
|
234
|
+
]
|
|
235
|
+
|
|
236
|
+
// @ts-ignore
|
|
237
|
+
const encodedSubDataOne = sparkEncode.closeToAsset(...subDataToEncodeOne);
|
|
238
|
+
// @ts-ignore
|
|
239
|
+
const encodedSubDataTwo = sparkEncode.closeToAsset(...subDataToEncodeTwo);
|
|
240
|
+
const encodedParams = web3Utils.keccak256(AbiCoder.encodeParameter('(uint64,bool,bytes[],bytes32[])', encodedSubDataOne));
|
|
241
|
+
|
|
242
|
+
const examples: Array<[boolean, [string, any[]]]> = [
|
|
243
|
+
[true, [encodedParams, encodedSubDataOne]],
|
|
244
|
+
[false, [encodedParams, encodedSubDataTwo]],
|
|
245
|
+
];
|
|
246
|
+
|
|
247
|
+
examples.forEach(([expected, actual]) => {
|
|
248
|
+
it(`Given ${actual} should return expected value: ${expected}`, () => {
|
|
249
|
+
expect(compareSubHashes(...actual)).to.equal(expected);
|
|
250
|
+
});
|
|
251
|
+
});
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
describe('When testing utils.encodeSubId()', () => {
|
|
255
|
+
const examples: Array<[string, string]> = [
|
|
256
|
+
['00000001', '1'],
|
|
257
|
+
['00000002', '2'],
|
|
258
|
+
['0021c503', '2213123']
|
|
259
|
+
];
|
|
260
|
+
|
|
261
|
+
examples.forEach(([expected, actual]) => {
|
|
262
|
+
it(`Given ${actual} should return expected value: ${expected}`, () => {
|
|
263
|
+
expect(encodeSubId(actual)).to.equal(expected);
|
|
264
|
+
});
|
|
265
|
+
});
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
describe('When testing utils.ratioPercentageToWei()', () => {
|
|
269
|
+
const examples: Array<[string, number]> = [
|
|
270
|
+
['1200000000000000000', 120],
|
|
271
|
+
['2400000000000000000', 240],
|
|
272
|
+
['870000000000000000', 87],
|
|
273
|
+
];
|
|
274
|
+
|
|
275
|
+
examples.forEach(([expected, actual]) => {
|
|
276
|
+
it(`Given ${actual} should return expected value: ${expected}`, () => {
|
|
277
|
+
expect(ratioPercentageToWei(actual)).to.equal(expected);
|
|
278
|
+
});
|
|
279
|
+
});
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
describe('When testing utils.weiToRatioPercentage()', () => {
|
|
283
|
+
const examples: Array<[number, string]> = [
|
|
284
|
+
[120, '1200000000000000000'],
|
|
285
|
+
[240, '2400000000000000000'],
|
|
286
|
+
[87, '870000000000000000'],
|
|
287
|
+
];
|
|
288
|
+
|
|
289
|
+
examples.forEach(([expected, actual]) => {
|
|
290
|
+
it(`Given ${actual} should return expected value: ${expected}`, () => {
|
|
291
|
+
expect(weiToRatioPercentage(actual)).to.equal(expected);
|
|
292
|
+
});
|
|
293
|
+
});
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
describe('When testing utils.isRatioStateOver()', () => {
|
|
297
|
+
const examples: Array<[boolean, RatioState]> = [
|
|
298
|
+
[true, RatioState.OVER],
|
|
299
|
+
[false, RatioState.UNDER],
|
|
300
|
+
[false, NaN],
|
|
301
|
+
[false, 3],
|
|
302
|
+
[false, Infinity],
|
|
303
|
+
];
|
|
304
|
+
|
|
305
|
+
examples.forEach(([expected, actual]) => {
|
|
306
|
+
it(`Given ${actual} should return expected value: ${expected}`, () => {
|
|
307
|
+
expect(isRatioStateOver(actual)).to.equal(expected);
|
|
308
|
+
});
|
|
309
|
+
});
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
describe('When testing utils.isRatioStateUnder()', () => {
|
|
313
|
+
const examples: Array<[boolean, RatioState]> = [
|
|
314
|
+
[true, RatioState.UNDER],
|
|
315
|
+
[false, RatioState.OVER],
|
|
316
|
+
[false, NaN],
|
|
317
|
+
[false, 3],
|
|
318
|
+
[false, Infinity],
|
|
319
|
+
];
|
|
320
|
+
|
|
321
|
+
examples.forEach(([expected, actual]) => {
|
|
322
|
+
it(`Given ${actual} should return expected value: ${expected}`, () => {
|
|
323
|
+
expect(isRatioStateUnder(actual)).to.equal(expected);
|
|
324
|
+
});
|
|
325
|
+
});
|
|
326
|
+
});
|
|
327
|
+
|
|
328
|
+
describe('When testing utils.isEmptyBytes()', () => {
|
|
329
|
+
const examples: Array<[boolean, string]> = [
|
|
330
|
+
[true, '0x0000000000000000000000000000000000000000'],
|
|
331
|
+
[false, '0x0000000000000000000000000000000000000001'],
|
|
332
|
+
[false, '0x00000000000000000000000000000000000000000000'],
|
|
333
|
+
[false, '0x000000000000000000000000000000000000000'],
|
|
334
|
+
[false, ''],
|
|
335
|
+
];
|
|
336
|
+
|
|
337
|
+
examples.forEach(([expected, actual]) => {
|
|
338
|
+
it(`Given ${actual} should return expected value: ${expected}`, () => {
|
|
339
|
+
expect(isEmptyBytes(actual)).to.equal(expected);
|
|
340
|
+
});
|
|
341
|
+
});
|
|
342
|
+
});
|
|
343
|
+
|
|
344
|
+
describe('When testing utils.requireAddress()', () => {
|
|
345
|
+
const examples: Array<[boolean, any]> = [
|
|
346
|
+
[true, '0x1e5698681E03326d783215adfB3173acF3Cf2B6'],
|
|
347
|
+
[true, 1312],
|
|
348
|
+
[true, ''],
|
|
349
|
+
[true, '0x0000000000000000000000000000000000000000'],
|
|
350
|
+
[true, '0xZe5698681E03326d783215adfB3173acF3Cf2B6'],
|
|
351
|
+
[true, 'Ze5698681E03326d783215adfB3173acF3Cf2B6'],
|
|
352
|
+
[false, '0x1e5698681E03326d783215adfB3173acF3Cf2B61'],
|
|
353
|
+
];
|
|
354
|
+
|
|
355
|
+
examples.forEach(([expected, actual]) => {
|
|
356
|
+
if (expected) {
|
|
357
|
+
it(`Given ${actual} should throw an error`, () => {
|
|
358
|
+
expect(() => requireAddress(actual)).to.throw(Error);
|
|
359
|
+
});
|
|
360
|
+
} else {
|
|
361
|
+
it(`Given ${actual} should not throw an error`, () => {
|
|
362
|
+
expect(() => requireAddress(actual)).not.to.throw(Error);
|
|
363
|
+
});
|
|
364
|
+
}
|
|
365
|
+
});
|
|
366
|
+
});
|
|
367
|
+
|
|
368
|
+
describe('When testing utils.requireAddresses()', () => {
|
|
369
|
+
const examples: Array<[boolean, any]> = [
|
|
370
|
+
[true, '0x1e5698681E03326d783215adfB3173acF3Cf2B6'],
|
|
371
|
+
[true, [1312, '0x1e5698681E03326d783215adfB3173acF3Cf2B61']],
|
|
372
|
+
[true, ['', '0x1e5698681E03326d783215adfB3173acF3Cf2B61']],
|
|
373
|
+
[true, ['0x0000000000000000000000000000000000000000', '0x1e5698481E03326d783215adfB3173acF3Cf2B61']],
|
|
374
|
+
[true, ['0xZe5698681E03326d783215adfB3173acF3Cf2B6', '0x1e5698681E03326d783215adfB3173acF3Cc2B61']],
|
|
375
|
+
[true, ['Ze5698681E03326d783215adfB3173acF3Cf2B6', '', '0x1e5698681E03326d783215adfB3173acF3Cf2B61']],
|
|
376
|
+
[false, ['0x1e5698681E03326d783215adfB3173acF3Cf2B61', '0x2e5698681E03326d783215adfB3173acF3Cf2B61']],
|
|
377
|
+
];
|
|
378
|
+
|
|
379
|
+
examples.forEach(([expected, actual]) => {
|
|
380
|
+
if (expected) {
|
|
381
|
+
it(`Given ${actual} should throw an error`, () => {
|
|
382
|
+
expect(() => requireAddresses(actual)).to.throw(Error);
|
|
383
|
+
});
|
|
384
|
+
} else {
|
|
385
|
+
it(`Given ${actual} should not throw an error`, () => {
|
|
386
|
+
expect(() => requireAddresses(actual)).not.to.throw(Error);
|
|
387
|
+
});
|
|
388
|
+
}
|
|
389
|
+
});
|
|
390
|
+
});
|
|
391
|
+
|
|
392
|
+
describe('When testing utils.getRatioStateInfoForAaveCloseStrategy()', () => {
|
|
393
|
+
const examples: Array<[{ shouldFlip: boolean, ratioState: RatioState }, [RatioState, EthereumAddress, EthereumAddress, ChainId]]> = [
|
|
394
|
+
[
|
|
395
|
+
{ shouldFlip: true, ratioState: RatioState.UNDER },
|
|
396
|
+
[RatioState.OVER, getAssetInfo('DAI').address, getAssetInfo('ETH').address, ChainId.Ethereum]
|
|
397
|
+
],
|
|
398
|
+
[
|
|
399
|
+
{ shouldFlip: false, ratioState: RatioState.UNDER },
|
|
400
|
+
[RatioState.UNDER, getAssetInfo('DAI').address, getAssetInfo('ETH').address, ChainId.Arbitrum]
|
|
401
|
+
],
|
|
402
|
+
[
|
|
403
|
+
{ shouldFlip: false, ratioState: RatioState.OVER },
|
|
404
|
+
[RatioState.OVER, getAssetInfo('ETH').address, getAssetInfo('ETH').address, ChainId.Optimism]
|
|
405
|
+
],
|
|
406
|
+
];
|
|
407
|
+
|
|
408
|
+
examples.forEach(([expected, actual]) => {
|
|
409
|
+
it(`Given ${actual} should return expected value: ${JSON.stringify(expected)}`, () => {
|
|
410
|
+
expect(getRatioStateInfoForAaveCloseStrategy(...actual)).to.eql(expected);
|
|
411
|
+
});
|
|
412
|
+
});
|
|
413
|
+
});
|
|
414
|
+
});
|
package/src/services/utils.ts
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
|
-
import { getAssetInfo, getAssetInfoByAddress } from '@defisaver/tokens';
|
|
2
1
|
import Dec from 'decimal.js';
|
|
2
|
+
import * as web3Utils from 'web3-utils';
|
|
3
|
+
import AbiCoder from 'web3-eth-abi';
|
|
4
|
+
import { getAssetInfo, getAssetInfoByAddress } from '@defisaver/tokens';
|
|
3
5
|
|
|
4
6
|
import type { EthereumAddress } from '../types';
|
|
5
|
-
|
|
6
7
|
import { ChainId, RatioState } from '../types/enums';
|
|
7
8
|
|
|
8
|
-
const { mockedWeb3 } = process;
|
|
9
|
-
|
|
10
9
|
export function isDefined<T>(value: T): value is NonNullable<T> {
|
|
11
10
|
return value !== undefined && value !== null;
|
|
12
11
|
}
|
|
@@ -44,7 +43,7 @@ export function wethToEthByAddress(maybeWethAddr: EthereumAddress, chainId: Chai
|
|
|
44
43
|
}
|
|
45
44
|
|
|
46
45
|
export function compareSubHashes(currentSubHash: string, newSubStructDecoded: object): boolean {
|
|
47
|
-
return currentSubHash ===
|
|
46
|
+
return currentSubHash === web3Utils.keccak256(AbiCoder.encodeParameter('(uint64,bool,bytes[],bytes32[])', newSubStructDecoded));
|
|
48
47
|
}
|
|
49
48
|
|
|
50
49
|
export function encodeSubId(subIdDec: string = '0') {
|
|
@@ -52,11 +51,11 @@ export function encodeSubId(subIdDec: string = '0') {
|
|
|
52
51
|
}
|
|
53
52
|
|
|
54
53
|
export function ratioPercentageToWei(ratioPercentage: number) {
|
|
55
|
-
return
|
|
54
|
+
return web3Utils.toWei(new Dec(ratioPercentage).div(100).toString(), 'ether');
|
|
56
55
|
}
|
|
57
56
|
|
|
58
57
|
export function weiToRatioPercentage(ratioWei: string) {
|
|
59
|
-
return new Dec(
|
|
58
|
+
return new Dec(web3Utils.fromWei(new Dec(ratioWei).mul(100).toString(), 'ether')).toNumber();
|
|
60
59
|
}
|
|
61
60
|
|
|
62
61
|
export function isRatioStateOver(ratioState: RatioState): boolean {
|
package/tsconfig.json
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
/* Basic Options */
|
|
6
6
|
// "incremental": true, /* Enable incremental compilation */
|
|
7
7
|
"target": "ES2015", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
|
|
8
|
-
"module": "
|
|
8
|
+
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
|
|
9
9
|
// "lib": [], /* Specify library files to be included in the compilation. */
|
|
10
10
|
"allowJs": true, /* Allow javascript files to be compiled. */
|
|
11
11
|
// "checkJs": true, /* Report errors in .js files. */
|