@defisaver/automation-sdk 2.0.6 → 2.0.9

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.
Files changed (54) hide show
  1. package/.env +4 -0
  2. package/.env.dev +5 -0
  3. package/.tests.sh +3 -0
  4. package/README.md +3 -1
  5. package/esm/automation/private/LegacyAutomation.js +9 -3
  6. package/esm/automation/private/LegacyProtocol.test.d.ts +1 -0
  7. package/esm/automation/private/LegacyProtocol.test.js +25 -0
  8. package/esm/automation/private/Protocol.test.d.ts +1 -0
  9. package/esm/automation/private/Protocol.test.js +25 -0
  10. package/esm/automation/private/StrategiesAutomation.d.ts +4 -0
  11. package/esm/automation/private/StrategiesAutomation.js +29 -4
  12. package/esm/constants/index.js +5 -0
  13. package/esm/services/contractService.d.ts +3 -2
  14. package/esm/services/contractService.js +7 -1
  15. package/esm/services/ethereumService.test.d.ts +1 -0
  16. package/esm/services/ethereumService.test.js +241 -0
  17. package/esm/services/strategiesService.js +14 -0
  18. package/esm/services/strategiesService.test.d.ts +1 -0
  19. package/esm/services/strategiesService.test.js +108 -0
  20. package/esm/services/strategySubService.d.ts +5 -3
  21. package/esm/services/strategySubService.js +10 -6
  22. package/esm/services/strategySubService.test.d.ts +1 -0
  23. package/esm/services/strategySubService.test.js +692 -0
  24. package/esm/services/subDataService.d.ts +8 -1
  25. package/esm/services/subDataService.js +23 -14
  26. package/esm/services/subDataService.test.d.ts +1 -0
  27. package/esm/services/subDataService.test.js +993 -0
  28. package/esm/services/triggerService.d.ts +7 -0
  29. package/esm/services/triggerService.js +14 -1
  30. package/esm/services/triggerService.test.js +119 -17
  31. package/esm/types/enums.d.ts +4 -2
  32. package/esm/types/enums.js +2 -0
  33. package/esm/types/index.d.ts +13 -4
  34. package/package.json +5 -5
  35. package/src/automation/private/LegacyAutomation.ts +14 -4
  36. package/src/automation/private/LegacyProtocol.test.ts +24 -0
  37. package/src/automation/private/Protocol.test.ts +24 -0
  38. package/src/automation/private/StrategiesAutomation.ts +40 -6
  39. package/src/constants/index.ts +5 -0
  40. package/src/services/contractService.ts +17 -4
  41. package/src/services/ethereumService.test.ts +256 -0
  42. package/src/services/ethereumService.ts +2 -1
  43. package/src/services/strategiesService.test.ts +103 -0
  44. package/src/services/strategiesService.ts +27 -3
  45. package/src/services/strategySubService.test.ts +835 -0
  46. package/src/services/strategySubService.ts +28 -15
  47. package/src/services/subDataService.test.ts +1063 -0
  48. package/src/services/subDataService.ts +29 -14
  49. package/src/services/triggerService.test.ts +131 -19
  50. package/src/services/triggerService.ts +14 -0
  51. package/src/services/utils.test.ts +1 -1
  52. package/src/types/enums.ts +2 -0
  53. package/src/types/index.ts +20 -5
  54. package/umd/index.js +919 -523
@@ -83,6 +83,13 @@ export declare const liquityDebtInFrontTrigger: {
83
83
  debtInFrontMin: string;
84
84
  };
85
85
  };
86
+ export declare const liquityDebtInFrontWithLimitTrigger: {
87
+ encode(owner: EthereumAddress, debtInFrontMin: string): string[];
88
+ decode(triggerData: string[]): {
89
+ owner: EthereumAddress;
90
+ debtInFrontMin: string;
91
+ };
92
+ };
86
93
  export declare const aaveV2RatioTrigger: {
87
94
  encode(owner: EthereumAddress, market: EthereumAddress, ratioPercentage: number, ratioState: RatioState): string[];
88
95
  decode(triggerData: string[]): {
@@ -26,7 +26,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
26
26
  return (mod && mod.__esModule) ? mod : { "default": mod };
27
27
  };
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.curveUsdSoftLiquidationTrigger = exports.curveUsdBorrowRateTrigger = exports.sparkQuotePriceTrigger = exports.sparkRatioTrigger = exports.exchangeOffchainPriceTrigger = exports.exchangeTimestampTrigger = exports.compoundV3RatioTrigger = exports.cBondsRebondTrigger = exports.aaveV2RatioTrigger = exports.liquityDebtInFrontTrigger = exports.liquityRatioTrigger = exports.compoundV2RatioTrigger = exports.aaveV3QuotePriceWithMaximumGasPriceTrigger = exports.aaveV3QuotePriceTrigger = exports.morphoAaveV2RatioTrigger = exports.aaveV3RatioTrigger = exports.makerRatioTrigger = exports.trailingStopTrigger = exports.chainlinkPriceTrigger = void 0;
29
+ exports.curveUsdSoftLiquidationTrigger = exports.curveUsdBorrowRateTrigger = exports.sparkQuotePriceTrigger = exports.sparkRatioTrigger = exports.exchangeOffchainPriceTrigger = exports.exchangeTimestampTrigger = exports.compoundV3RatioTrigger = exports.cBondsRebondTrigger = exports.aaveV2RatioTrigger = exports.liquityDebtInFrontWithLimitTrigger = exports.liquityDebtInFrontTrigger = exports.liquityRatioTrigger = exports.compoundV2RatioTrigger = exports.aaveV3QuotePriceWithMaximumGasPriceTrigger = exports.aaveV3QuotePriceTrigger = exports.morphoAaveV2RatioTrigger = exports.aaveV3RatioTrigger = exports.makerRatioTrigger = exports.trailingStopTrigger = exports.chainlinkPriceTrigger = void 0;
30
30
  const decimal_js_1 = __importDefault(require("decimal.js"));
31
31
  const tokens_1 = require("@defisaver/tokens");
32
32
  const web3_eth_abi_1 = __importDefault(require("web3-eth-abi"));
@@ -177,6 +177,19 @@ exports.liquityDebtInFrontTrigger = {
177
177
  };
178
178
  },
179
179
  };
180
+ exports.liquityDebtInFrontWithLimitTrigger = {
181
+ encode(owner, debtInFrontMin) {
182
+ const debtInFrontMinWei = web3Utils.toWei(new decimal_js_1.default(debtInFrontMin).toString(), 'ether');
183
+ return [web3_eth_abi_1.default.encodeParameters(['address', 'uint256'], [owner, debtInFrontMinWei])];
184
+ },
185
+ decode(triggerData) {
186
+ const decodedData = web3_eth_abi_1.default.decodeParameters(['address', 'uint256'], triggerData[0]);
187
+ return {
188
+ owner: decodedData[0],
189
+ debtInFrontMin: new decimal_js_1.default(decodedData[1]).div(Math.pow(10, 18)).toString(),
190
+ };
191
+ },
192
+ };
180
193
  exports.aaveV2RatioTrigger = {
181
194
  encode(owner, market, ratioPercentage, ratioState) {
182
195
  const ratioWei = (0, utils_1.ratioPercentageToWei)(ratioPercentage);
@@ -59,7 +59,7 @@ describe('Feature: triggerService.ts', () => {
59
59
  ],
60
60
  ];
61
61
  examples.forEach(([expected, actual]) => {
62
- it(`Given ${actual} should return expected value: ${expected}`, () => {
62
+ it(`Given ${actual} should return expected value: ${JSON.stringify(expected)}`, () => {
63
63
  (0, chai_1.expect)(triggerService_1.chainlinkPriceTrigger.decode(actual)).to.eql(expected);
64
64
  });
65
65
  });
@@ -95,7 +95,7 @@ describe('Feature: triggerService.ts', () => {
95
95
  ],
96
96
  ];
97
97
  examples.forEach(([expected, actual]) => {
98
- it(`Given ${actual} should return expected value: ${expected}`, () => {
98
+ it(`Given ${actual} should return expected value: ${JSON.stringify(expected)}`, () => {
99
99
  (0, chai_1.expect)(triggerService_1.trailingStopTrigger.decode(actual)).to.eql(expected);
100
100
  });
101
101
  });
@@ -131,7 +131,7 @@ describe('Feature: triggerService.ts', () => {
131
131
  ],
132
132
  ];
133
133
  examples.forEach(([expected, actual]) => {
134
- it(`Given ${actual} should return expected value: ${expected}`, () => {
134
+ it(`Given ${actual} should return expected value: ${JSON.stringify(expected)}`, () => {
135
135
  (0, chai_1.expect)(triggerService_1.makerRatioTrigger.decode(actual)).to.eql(expected);
136
136
  });
137
137
  });
@@ -167,7 +167,7 @@ describe('Feature: triggerService.ts', () => {
167
167
  ],
168
168
  ];
169
169
  examples.forEach(([expected, actual]) => {
170
- it(`Given ${actual} should return expected value: ${expected}`, () => {
170
+ it(`Given ${actual} should return expected value: ${JSON.stringify(expected)}`, () => {
171
171
  (0, chai_1.expect)(triggerService_1.aaveV3RatioTrigger.decode(actual)).to.eql(expected);
172
172
  });
173
173
  });
@@ -203,7 +203,7 @@ describe('Feature: triggerService.ts', () => {
203
203
  ],
204
204
  ];
205
205
  examples.forEach(([expected, actual]) => {
206
- it(`Given ${actual} should return expected value: ${expected}`, () => {
206
+ it(`Given ${actual} should return expected value: ${JSON.stringify(expected)}`, () => {
207
207
  (0, chai_1.expect)(triggerService_1.morphoAaveV2RatioTrigger.decode(actual)).to.eql(expected);
208
208
  });
209
209
  });
@@ -249,12 +249,72 @@ describe('Feature: triggerService.ts', () => {
249
249
  ]
250
250
  ];
251
251
  examples.forEach(([expected, actual]) => {
252
- it(`Given ${actual} should return expected value: ${expected}`, () => {
252
+ it(`Given ${actual} should return expected value: ${JSON.stringify(expected)}`, () => {
253
253
  (0, chai_1.expect)(triggerService_1.aaveV3QuotePriceTrigger.decode(actual)).to.eql(expected);
254
254
  });
255
255
  });
256
256
  });
257
257
  });
258
+ describe('When testing triggerService.aaveV3QuotePriceWithMaximumGasPriceTrigger', () => {
259
+ describe('encode()', () => {
260
+ const examples = [
261
+ [
262
+ [
263
+ '0x0000000000000000000000006b175474e89094c44da98b954eedeac495271d0f000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20000000000000000000000000000000000000000000000000000000000009c400000000000000000000000000000000000000000000000000000000000000001',
264
+ '0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff',
265
+ ],
266
+ [(0, tokens_1.getAssetInfo)('DAI', enums_1.ChainId.Ethereum).address, (0, tokens_1.getAssetInfo)('WETH', enums_1.ChainId.Ethereum).address, 0.0004, enums_1.RatioState.UNDER]
267
+ ],
268
+ [
269
+ [
270
+ '0x0000000000000000000000002260fac5e5542a773aa44fbcfedf7c193bc2c599000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec700000000000000000000000000000000000000000000000000000000000000170000000000000000000000000000000000000000000000000000000000000000',
271
+ '0x00000000000000000000000000000000000000000000000000000002cb417800',
272
+ ],
273
+ [(0, tokens_1.getAssetInfo)('WBTC', enums_1.ChainId.Ethereum).address, (0, tokens_1.getAssetInfo)('USDT', enums_1.ChainId.Ethereum).address, 0.00000023, enums_1.RatioState.OVER, 12]
274
+ ],
275
+ ];
276
+ examples.forEach(([expected, actual]) => {
277
+ it(`Given ${actual} should return expected value: ${JSON.stringify(expected)}`, () => {
278
+ (0, chai_1.expect)(triggerService_1.aaveV3QuotePriceWithMaximumGasPriceTrigger.encode(...actual)).to.eql(expected);
279
+ });
280
+ });
281
+ });
282
+ describe('decode()', () => {
283
+ const examples = [
284
+ [
285
+ {
286
+ baseTokenAddress: web3Utils.toChecksumAddress((0, tokens_1.getAssetInfo)('DAI', enums_1.ChainId.Ethereum).address),
287
+ quoteTokenAddress: web3Utils.toChecksumAddress((0, tokens_1.getAssetInfo)('WETH', enums_1.ChainId.Ethereum).address),
288
+ price: '0.0004',
289
+ ratioState: enums_1.RatioState.UNDER,
290
+ maximumGasPrice: web3Utils.fromWei(tokens_1.MAXUINT, 'gwei')
291
+ },
292
+ [
293
+ '0x0000000000000000000000006b175474e89094c44da98b954eedeac495271d0f000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20000000000000000000000000000000000000000000000000000000000009c400000000000000000000000000000000000000000000000000000000000000001',
294
+ '0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff',
295
+ ],
296
+ ],
297
+ [
298
+ {
299
+ baseTokenAddress: web3Utils.toChecksumAddress((0, tokens_1.getAssetInfo)('WBTC', enums_1.ChainId.Ethereum).address),
300
+ quoteTokenAddress: web3Utils.toChecksumAddress((0, tokens_1.getAssetInfo)('USDT', enums_1.ChainId.Ethereum).address),
301
+ price: '0.00000023',
302
+ ratioState: enums_1.RatioState.OVER,
303
+ maximumGasPrice: '12',
304
+ },
305
+ [
306
+ '0x0000000000000000000000002260fac5e5542a773aa44fbcfedf7c193bc2c599000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec700000000000000000000000000000000000000000000000000000000000000170000000000000000000000000000000000000000000000000000000000000000',
307
+ '0x00000000000000000000000000000000000000000000000000000002cb417800',
308
+ ],
309
+ ]
310
+ ];
311
+ examples.forEach(([expected, actual]) => {
312
+ it(`Given ${actual} should return expected value: ${JSON.stringify(expected)}`, () => {
313
+ (0, chai_1.expect)(triggerService_1.aaveV3QuotePriceWithMaximumGasPriceTrigger.decode(actual)).to.eql(expected);
314
+ });
315
+ });
316
+ });
317
+ });
258
318
  describe('When testing triggerService.liquityRatioTrigger', () => {
259
319
  describe('encode()', () => {
260
320
  const examples = [
@@ -293,7 +353,7 @@ describe('Feature: triggerService.ts', () => {
293
353
  ],
294
354
  ];
295
355
  examples.forEach(([expected, actual]) => {
296
- it(`Given ${actual} should return expected value: ${expected}`, () => {
356
+ it(`Given ${actual} should return expected value: ${JSON.stringify(expected)}`, () => {
297
357
  (0, chai_1.expect)(triggerService_1.compoundV2RatioTrigger.decode(actual)).to.eql(expected);
298
358
  });
299
359
  });
@@ -335,7 +395,7 @@ describe('Feature: triggerService.ts', () => {
335
395
  ],
336
396
  ];
337
397
  examples.forEach(([expected, actual]) => {
338
- it(`Given ${actual} should return expected value: ${expected}`, () => {
398
+ it(`Given ${actual} should return expected value: ${JSON.stringify(expected)}`, () => {
339
399
  (0, chai_1.expect)(triggerService_1.liquityDebtInFrontTrigger.decode(actual)).to.eql(expected);
340
400
  });
341
401
  });
@@ -368,7 +428,7 @@ describe('Feature: triggerService.ts', () => {
368
428
  ],
369
429
  ];
370
430
  examples.forEach(([expected, actual]) => {
371
- it(`Given ${actual} should return expected value: ${expected}`, () => {
431
+ it(`Given ${actual} should return expected value: ${JSON.stringify(expected)}`, () => {
372
432
  (0, chai_1.expect)(triggerService_1.aaveV2RatioTrigger.decode(actual)).to.eql(expected);
373
433
  });
374
434
  });
@@ -404,7 +464,7 @@ describe('Feature: triggerService.ts', () => {
404
464
  ],
405
465
  ];
406
466
  examples.forEach(([expected, actual]) => {
407
- it(`Given ${actual} should return expected value: ${expected}`, () => {
467
+ it(`Given ${actual} should return expected value: ${JSON.stringify(expected)}`, () => {
408
468
  (0, chai_1.expect)(triggerService_1.cBondsRebondTrigger.decode(actual)).to.eql(expected);
409
469
  });
410
470
  });
@@ -450,7 +510,7 @@ describe('Feature: triggerService.ts', () => {
450
510
  ],
451
511
  ];
452
512
  examples.forEach(([expected, actual]) => {
453
- it(`Given ${actual} should return expected value: ${expected}`, () => {
513
+ it(`Given ${actual} should return expected value: ${JSON.stringify(expected)}`, () => {
454
514
  (0, chai_1.expect)(triggerService_1.compoundV3RatioTrigger.decode(actual)).to.eql(expected);
455
515
  });
456
516
  });
@@ -490,7 +550,7 @@ describe('Feature: triggerService.ts', () => {
490
550
  ],
491
551
  ];
492
552
  examples.forEach(([expected, actual]) => {
493
- it(`Given ${actual} should return expected value: ${expected}`, () => {
553
+ it(`Given ${actual} should return expected value: ${JSON.stringify(expected)}`, () => {
494
554
  (0, chai_1.expect)(triggerService_1.exchangeTimestampTrigger.decode(actual)).to.eql(expected);
495
555
  });
496
556
  });
@@ -538,7 +598,7 @@ describe('Feature: triggerService.ts', () => {
538
598
  ],
539
599
  ];
540
600
  examples.forEach(([expected, actual]) => {
541
- it(`Given ${actual} should return expected value: ${expected}`, () => {
601
+ it(`Given ${actual} should return expected value: ${JSON.stringify(expected)}`, () => {
542
602
  (0, chai_1.expect)(triggerService_1.exchangeOffchainPriceTrigger.decode(...actual)).to.eql(expected);
543
603
  });
544
604
  });
@@ -574,7 +634,7 @@ describe('Feature: triggerService.ts', () => {
574
634
  ],
575
635
  ];
576
636
  examples.forEach(([expected, actual]) => {
577
- it(`Given ${actual} should return expected value: ${expected}`, () => {
637
+ it(`Given ${actual} should return expected value: ${JSON.stringify(expected)}`, () => {
578
638
  (0, chai_1.expect)(triggerService_1.sparkRatioTrigger.decode(actual)).to.eql(expected);
579
639
  });
580
640
  });
@@ -620,7 +680,7 @@ describe('Feature: triggerService.ts', () => {
620
680
  ]
621
681
  ];
622
682
  examples.forEach(([expected, actual]) => {
623
- it(`Given ${actual} should return expected value: ${expected}`, () => {
683
+ it(`Given ${actual} should return expected value: ${JSON.stringify(expected)}`, () => {
624
684
  (0, chai_1.expect)(triggerService_1.sparkQuotePriceTrigger.decode(actual)).to.eql(expected);
625
685
  });
626
686
  });
@@ -666,7 +726,7 @@ describe('Feature: triggerService.ts', () => {
666
726
  ],
667
727
  ];
668
728
  examples.forEach(([expected, actual]) => {
669
- it(`Given ${actual} should return expected value: ${expected}`, () => {
729
+ it(`Given ${actual} should return expected value: ${JSON.stringify(expected)}`, () => {
670
730
  (0, chai_1.expect)(triggerService_1.curveUsdBorrowRateTrigger.decode(actual)).to.eql(expected);
671
731
  });
672
732
  });
@@ -710,10 +770,52 @@ describe('Feature: triggerService.ts', () => {
710
770
  ],
711
771
  ];
712
772
  examples.forEach(([expected, actual]) => {
713
- it(`Given ${actual} should return expected value: ${expected}`, () => {
773
+ it(`Given ${actual} should return expected value: ${JSON.stringify(expected)}`, () => {
714
774
  (0, chai_1.expect)(triggerService_1.curveUsdSoftLiquidationTrigger.decode(actual)).to.eql(expected);
715
775
  });
716
776
  });
717
777
  });
718
778
  });
779
+ describe('When testing triggerService.liquityDebtInFrontWithLimitTrigger', () => {
780
+ describe('encode()', () => {
781
+ const examples = [
782
+ [
783
+ ['0x000000000000000000000000235d6a8db3c57c3f7b4eba749e1738db6093732a0000000000000000000000000000000000000000019d971e4fe8401e74000000'],
784
+ [web3Utils.toChecksumAddress('0x235d6A8DB3C57c3f7b4ebA749E1738Db6093732a'), '500000000']
785
+ ],
786
+ [
787
+ ['0x000000000000000000000000235d6a8db3c57c3f7b4eba749e1738db6093732a00000000000000000000000000000000000000000001a784379d99db42000000'],
788
+ [web3Utils.toChecksumAddress('0x235d6A8DB3C57c3f7b4ebA749E1738Db6093732a'), '2000000']
789
+ ],
790
+ ];
791
+ examples.forEach(([expected, actual]) => {
792
+ it(`Given ${actual} should return expected value: ${expected}`, () => {
793
+ (0, chai_1.expect)(triggerService_1.liquityDebtInFrontWithLimitTrigger.encode(...actual)).to.eql(expected);
794
+ });
795
+ });
796
+ });
797
+ describe('decode()', () => {
798
+ const examples = [
799
+ [
800
+ {
801
+ owner: web3Utils.toChecksumAddress('0x235d6A8DB3C57c3f7b4ebA749E1738Db6093732a'),
802
+ debtInFrontMin: '500000000',
803
+ },
804
+ ['0x000000000000000000000000235d6a8db3c57c3f7b4eba749e1738db6093732a0000000000000000000000000000000000000000019d971e4fe8401e74000000'],
805
+ ],
806
+ [
807
+ {
808
+ owner: web3Utils.toChecksumAddress('0x235d6A8DB3C57c3f7b4ebA749E1738Db6093732a'),
809
+ debtInFrontMin: '2000000',
810
+ },
811
+ ['0x000000000000000000000000235d6a8db3c57c3f7b4eba749e1738db6093732a00000000000000000000000000000000000000000001a784379d99db42000000'],
812
+ ],
813
+ ];
814
+ examples.forEach(([expected, actual]) => {
815
+ it(`Given ${actual} should return expected value: ${JSON.stringify(expected)}`, () => {
816
+ (0, chai_1.expect)(triggerService_1.liquityDebtInFrontWithLimitTrigger.decode(actual)).to.eql(expected);
817
+ });
818
+ });
819
+ });
820
+ });
719
821
  });
@@ -55,7 +55,8 @@ export declare namespace Strategies {
55
55
  EXCHANGE_DCA = 46,
56
56
  EXCHANGE_LIMIT_ORDER = 51,
57
57
  LIQUITY_DSR_PAYBACK = 69,
58
- LIQUITY_DSR_SUPPLY = 70
58
+ LIQUITY_DSR_SUPPLY = 70,
59
+ LIQUITY_DEBT_IN_FRONT_REPAY = 75
59
60
  }
60
61
  enum OptimismIds {
61
62
  EXCHANGE_DCA = 8,
@@ -84,7 +85,8 @@ export declare namespace Strategies {
84
85
  Rebond = "rebond",
85
86
  BondProtection = "bond-protection",
86
87
  Dca = "dca",
87
- LimitOrder = "limit-order"
88
+ LimitOrder = "limit-order",
89
+ DebtInFrontRepay = "debt-in-front-repay"
88
90
  }
89
91
  enum IdOverrides {
90
92
  TakeProfit = "take-profit",
@@ -68,6 +68,7 @@ var Strategies;
68
68
  MainnetIds[MainnetIds["EXCHANGE_LIMIT_ORDER"] = 51] = "EXCHANGE_LIMIT_ORDER";
69
69
  MainnetIds[MainnetIds["LIQUITY_DSR_PAYBACK"] = 69] = "LIQUITY_DSR_PAYBACK";
70
70
  MainnetIds[MainnetIds["LIQUITY_DSR_SUPPLY"] = 70] = "LIQUITY_DSR_SUPPLY";
71
+ MainnetIds[MainnetIds["LIQUITY_DEBT_IN_FRONT_REPAY"] = 75] = "LIQUITY_DEBT_IN_FRONT_REPAY";
71
72
  })(MainnetIds = Strategies.MainnetIds || (Strategies.MainnetIds = {}));
72
73
  let OptimismIds;
73
74
  (function (OptimismIds) {
@@ -100,6 +101,7 @@ var Strategies;
100
101
  Identifiers["BondProtection"] = "bond-protection";
101
102
  Identifiers["Dca"] = "dca";
102
103
  Identifiers["LimitOrder"] = "limit-order";
104
+ Identifiers["DebtInFrontRepay"] = "debt-in-front-repay";
103
105
  })(Identifiers = Strategies.Identifiers || (Strategies.Identifiers = {}));
104
106
  let IdOverrides;
105
107
  (function (IdOverrides) {
@@ -45,11 +45,14 @@ export declare namespace Multicall {
45
45
  }
46
46
  type Payload = any[];
47
47
  }
48
- export interface SubscriptionOptions {
48
+ interface _SubscriptionOptions {
49
49
  toBlock: BlockNumber;
50
50
  fromBlock: BlockNumber;
51
- mergeWithSameId?: boolean;
51
+ mergeWithSameId: boolean;
52
+ enabledOnly: boolean;
53
+ unexpiredOnly: boolean;
52
54
  }
55
+ export type SubscriptionOptions = Partial<_SubscriptionOptions>;
53
56
  export declare namespace Interfaces {
54
57
  interface ProtocolBase {
55
58
  id: ProtocolIdentifiers.StrategiesAutomation | ProtocolIdentifiers.LegacyAutomation;
@@ -132,8 +135,12 @@ export declare namespace Position {
132
135
  triggerPercentage: number;
133
136
  closeToAssetAddr: EthereumAddress;
134
137
  }
138
+ interface DebtInFrontRepay extends Base {
139
+ debtInFrontMin: string;
140
+ targetRepayRatioIncrease: number;
141
+ }
135
142
  }
136
- type SpecificAny = Specific.CloseOnPrice | Specific.TrailingStop | Specific.RatioProtection | Specific.CloseOnPriceAave | Specific.CloseOnPriceWithMaximumGasPriceAave;
143
+ type SpecificAny = Specific.CloseOnPrice | Specific.TrailingStop | Specific.RatioProtection | Specific.CloseOnPriceAave | Specific.CloseOnPriceWithMaximumGasPriceAave | Specific.DebtInFrontRepay;
137
144
  interface Automated {
138
145
  chainId: ChainId;
139
146
  owner: EthereumAddress;
@@ -172,10 +179,12 @@ type StrategyInfo<T extends number> = Record<T, Strategy<T>>;
172
179
  export type MainnetStrategiesInfo = StrategyInfo<Strategies.MainnetIds>;
173
180
  export type OptimismStrategiesInfo = StrategyInfo<Strategies.OptimismIds>;
174
181
  export type ArbitrumStrategiesInfo = StrategyInfo<Strategies.ArbitrumIds>;
182
+ export type StrategyInfoUnion = MainnetStrategiesInfo | OptimismStrategiesInfo | ArbitrumStrategiesInfo;
175
183
  type BundleInfo<T extends number> = Record<T, BundleOrStrategy<T>>;
176
184
  export type MainnetBundleInfo = BundleInfo<Bundles.MainnetIds>;
177
185
  export type OptimismBundleInfo = BundleInfo<Bundles.OptimismIds>;
178
186
  export type ArbitrumBundleInfo = BundleInfo<Bundles.ArbitrumIds>;
187
+ export type BundleInfoUnion = MainnetBundleInfo | OptimismBundleInfo | ArbitrumBundleInfo;
179
188
  export interface StrategiesInfo {
180
189
  [ChainId.Ethereum]: MainnetStrategiesInfo;
181
190
  [ChainId.Optimism]: OptimismStrategiesInfo;
@@ -186,7 +195,7 @@ export interface BundlesInfo {
186
195
  [ChainId.Optimism]: OptimismBundleInfo;
187
196
  [ChainId.Arbitrum]: ArbitrumBundleInfo;
188
197
  }
189
- export type StrategyOrBundleIds = Strategies.MainnetIds & Strategies.OptimismIds & Strategies.ArbitrumIds & Bundles.MainnetIds & Bundles.OptimismIds & Bundles.ArbitrumIds;
198
+ export type StrategyOrBundleIds = typeof Strategies.MainnetIds[keyof typeof Strategies.MainnetIds] | typeof Strategies.OptimismIds[keyof typeof Strategies.OptimismIds] | typeof Strategies.ArbitrumIds[keyof typeof Strategies.ArbitrumIds] | typeof Bundles.MainnetIds[keyof typeof Bundles.MainnetIds] | typeof Bundles.OptimismIds[keyof typeof Bundles.OptimismIds] | typeof Bundles.ArbitrumIds[keyof typeof Bundles.ArbitrumIds];
190
199
  export interface ParseData {
191
200
  chainId: ChainId;
192
201
  blockNumber: BlockNumber;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@defisaver/automation-sdk",
3
- "version": "2.0.6",
3
+ "version": "2.0.9",
4
4
  "description": "",
5
5
  "main": "./umd/index.js",
6
6
  "module": "./esm/index.js",
@@ -15,15 +15,15 @@
15
15
  "lint": "node_modules/.bin/eslint ./src",
16
16
  "gtc": "node scripts/generateContractTypes.js",
17
17
  "prepare": "husky install && npm run gtc",
18
- "test": "mocha \"./src/**/*.test.ts\" --require ts-node/register --extension ts"
18
+ "test": "./.tests.sh"
19
19
  },
20
20
  "author": "DeFi Saver",
21
21
  "repository": "https://github.com/defisaver/automation-sdk",
22
22
  "license": "ISC",
23
23
  "dependencies": {
24
24
  "@defisaver/eslint-config": "^1.0.0",
25
- "@defisaver/sdk": "^1.0.43",
26
- "@defisaver/tokens": "^1.4.55",
25
+ "@defisaver/sdk": "^1.0.53",
26
+ "@defisaver/tokens": "^1.5.1",
27
27
  "@ethersproject/address": "^5.0.10",
28
28
  "@ethersproject/solidity": "^5.0.9",
29
29
  "decimal.js": "^10.4.3",
@@ -45,7 +45,7 @@
45
45
  "@typescript-eslint/parser": "^5.42.0",
46
46
  "babel-loader": "^8.2.2",
47
47
  "chai": "^4.3.8",
48
- "dotenv": "^10.0.0",
48
+ "dotenv": "^16.3.1",
49
49
  "eslint": "8.22.0",
50
50
  "eslint-config-airbnb": "^18.0.2",
51
51
  "eslint-config-airbnb-typescript": "^17.0.0",
@@ -1,3 +1,5 @@
1
+ import Dec from 'decimal.js';
2
+
1
3
  import type Web3 from 'web3';
2
4
  import type {
3
5
  EthereumAddress, Position, Interfaces, Contract, PlaceholderType, SubscriptionOptions,
@@ -99,14 +101,22 @@ export default class LegacyAutomation extends Automation {
99
101
  }
100
102
 
101
103
  protected async getParsedSubscriptions(addresses?: EthereumAddress[], options?: SubscriptionOptions): Promise<Position.LegacyAutomated[]> {
104
+ // Legacy automation was disabled on block 18213086
105
+ if (
106
+ !options?.fromBlock
107
+ || options?.fromBlock === 'latest'
108
+ || options?.fromBlock === 'pending'
109
+ || (options?.fromBlock && new Dec(options?.fromBlock.toString()).lt(18213086))
110
+ ) {
111
+ return [];
112
+ }
113
+
102
114
  const subscriptions = await this._getSubscriptions(addresses, options);
103
115
 
104
- // @ts-ignore
105
- return subscriptions.map((sub) => ({
116
+ return subscriptions.map((sub: any) => ({
106
117
  chainId: this.chainId,
107
118
  owner: sub[this.getOwnerPropName()],
108
- isEnabled: (options?.fromBlock && options?.fromBlock !== 'latest')
109
- ? options?.fromBlock > 18213086 : false, // Legacy automation was disabled on block 18213086
119
+ isEnabled: true,
110
120
  protocol: this.protocol,
111
121
  specific: { ...sub },
112
122
  strategy: {
@@ -0,0 +1,24 @@
1
+ import { expect } from 'chai';
2
+
3
+ import { ProtocolIdentifiers } from '../../types/enums';
4
+
5
+ import LegacyProtocol from './LegacyProtocol';
6
+
7
+ describe('Feature: LegacyProtocol.ts', () => {
8
+ describe('When testing class LegacyProtocol', () => {
9
+ const exampleProtocol = new LegacyProtocol({ id: ProtocolIdentifiers.LegacyAutomation.MakerDAO });
10
+ const examples: Array<[string, string]> = [
11
+ [exampleProtocol.id, 'MakerDAO'],
12
+ [exampleProtocol.name, 'MakerDAO'],
13
+ [exampleProtocol.slug, 'makerdao'],
14
+ [exampleProtocol.version, ''],
15
+ [exampleProtocol.fullName, 'MakerDAO'],
16
+ ];
17
+
18
+ examples.forEach(([expected, actual]) => {
19
+ it(`Given ${actual} should return expected value: ${expected}`, () => {
20
+ expect(actual).to.equal(expected);
21
+ });
22
+ });
23
+ });
24
+ });
@@ -0,0 +1,24 @@
1
+ import { expect } from 'chai';
2
+
3
+ import { ProtocolIdentifiers } from '../../types/enums';
4
+
5
+ import Protocol from './Protocol';
6
+
7
+ describe('Feature: Protocol.ts', () => {
8
+ describe('When testing class Protocol', () => {
9
+ const exampleProtocol = new Protocol({ id: ProtocolIdentifiers.StrategiesAutomation.AaveV3 });
10
+ const examples: Array<[string, string]> = [
11
+ [exampleProtocol.id, 'Aave__V3'],
12
+ [exampleProtocol.name, 'Aave'],
13
+ [exampleProtocol.slug, 'aave'],
14
+ [exampleProtocol.version, 'V3'],
15
+ [exampleProtocol.fullName, 'Aave V3'],
16
+ ];
17
+
18
+ examples.forEach(([expected, actual]) => {
19
+ it(`Given ${actual} should return expected value: ${expected}`, () => {
20
+ expect(actual).to.equal(expected);
21
+ });
22
+ });
23
+ });
24
+ });
@@ -8,6 +8,7 @@ import type {
8
8
  StrategyModel, SubStorage,
9
9
  } from '../../types/contracts/generated/SubStorage';
10
10
  import type { ChainId } from '../../types/enums';
11
+ import { Strategies, ProtocolIdentifiers } from '../../types/enums';
11
12
 
12
13
  import { addToObjectIf, isDefined } from '../../services/utils';
13
14
  import { getAbiItem, makeSubStorageContract } from '../../services/contractService';
@@ -84,20 +85,53 @@ export default class StrategiesAutomation extends Automation {
84
85
  return parseStrategiesAutomatedPosition(parseData);
85
86
  }
86
87
 
88
+ /**
89
+ * @description Removes expired Limit Order subscriptions
90
+ */
91
+ protected removeExpiredSubscriptions(subscriptions: (Position.Automated | null)[]) {
92
+ return subscriptions.filter((subscription: Position.Automated | null) => {
93
+ if (!subscription) {
94
+ return true;
95
+ }
96
+
97
+ const { protocol, strategy, strategyData } = subscription;
98
+
99
+ if (
100
+ protocol.id === ProtocolIdentifiers.StrategiesAutomation.Exchange
101
+ && strategy.strategyId === Strategies.Identifiers.LimitOrder
102
+ ) {
103
+ return new Dec(strategyData.decoded.triggerData.goodUntil).gt(new Dec(Date.now()).div(1000));
104
+ }
105
+
106
+ return true;
107
+ });
108
+ }
109
+
87
110
  protected async _getSubscriptions(addresses?: EthereumAddress[], options?: SubscriptionOptions): Promise<(Position.Automated | null)[]> {
88
111
  const _options = {
89
112
  ...addToObjectIf(isDefined(options), options),
90
113
  ...addToObjectIf(isDefined(addresses), { filter: { proxy: addresses } }),
91
- };
114
+ } as SubscriptionOptions & PastEventOptions;
92
115
 
93
-
94
- const subscriptionEvents = (await this.getSubscriptionEventsFromSubStorage(_options)) as PlaceholderType; // TODO PlaceholderType
116
+ let subscriptionEvents = (await this.getSubscriptionEventsFromSubStorage(_options)) as PlaceholderType; // TODO PlaceholderType
95
117
 
96
118
  let subscriptions: (Position.Automated | null)[] = [];
97
119
 
98
120
  if (subscriptionEvents) {
99
- // @ts-ignore
100
- const strategiesSubs = await this.getStrategiesSubs(subscriptionEvents.map((e) => e.returnValues.subId), _options.toBlock);
121
+ let strategiesSubs = await this.getStrategiesSubs(
122
+ subscriptionEvents.map((e: { returnValues: { subId: number } }) => e.returnValues.subId), _options.toBlock,
123
+ );
124
+
125
+ if (_options.enabledOnly) {
126
+ const filteredSubscriptionEvents = [] as PlaceholderType;
127
+
128
+ strategiesSubs = strategiesSubs.filter((sub, index) => {
129
+ if (sub?.isEnabled) filteredSubscriptionEvents.push(subscriptionEvents[index]);
130
+ return sub?.isEnabled;
131
+ });
132
+
133
+ subscriptionEvents = filteredSubscriptionEvents;
134
+ }
101
135
 
102
136
  subscriptions = await Promise.all(strategiesSubs.map(async (sub, index: number) => {
103
137
  let latestUpdate = subscriptionEvents[index].returnValues;
@@ -168,7 +202,7 @@ export default class StrategiesAutomation extends Automation {
168
202
  }
169
203
  }
170
204
 
171
- return subscriptions;
205
+ return _options.unexpiredOnly ? this.removeExpiredSubscriptions(subscriptions) : subscriptions;
172
206
  }
173
207
 
174
208
  public async getSubscriptions(options?: SubscriptionOptions): Promise<(Position.Automated | null)[]> {
@@ -90,6 +90,11 @@ export const MAINNET_STRATEGIES_INFO: MainnetStrategiesInfo = {
90
90
  strategyId: Strategies.Identifiers.SavingsDsrSupply,
91
91
  protocol: PROTOCOLS.Liquity,
92
92
  },
93
+ [Strategies.MainnetIds.LIQUITY_DEBT_IN_FRONT_REPAY]: {
94
+ strategyOrBundleId: Strategies.MainnetIds.LIQUITY_DEBT_IN_FRONT_REPAY,
95
+ strategyId: Strategies.Identifiers.DebtInFrontRepay,
96
+ protocol: PROTOCOLS.Liquity,
97
+ },
93
98
  };
94
99
 
95
100
  export const OPTIMISM_STRATEGIES_INFO = {
@@ -1,13 +1,17 @@
1
1
  import type Web3 from 'web3';
2
2
  import type { AbiItem } from 'web3-utils';
3
+
3
4
  import type {
4
- BlockNumber, Contract,
5
+ BlockNumber, Contract, EthereumAddress,
5
6
  } from '../types';
6
-
7
7
  import type { BaseContract } from '../types/contracts/generated/types';
8
- import type { Legacy_AuthCheck, SubStorage, UniMulticall } from '../types/contracts/generated';
8
+ import type {
9
+ Legacy_AuthCheck, SubStorage, UniMulticall, Erc20,
10
+ } from '../types/contracts/generated';
9
11
 
10
- import { UniMulticallJson, SubStorageJson, AuthCheckJson } from '../abis';
12
+ import {
13
+ UniMulticallJson, SubStorageJson, AuthCheckJson, Erc20Json,
14
+ } from '../abis';
11
15
 
12
16
  import { isDefined } from './utils';
13
17
  import { ChainId } from '../types/enums';
@@ -62,4 +66,13 @@ export function makeAuthCheckerContract(web3: Web3, chainId: ChainId) {
62
66
 
63
67
  export function makeLegacySubscriptionContract<T extends BaseContract>(web3: Web3, contractJson: Contract.Json) {
64
68
  return makeContract<T>(web3, contractJson, ChainId.Ethereum);
69
+ }
70
+
71
+ export function makeErc20Contract(web3: Web3, tokenAddress: EthereumAddress, chainId: ChainId) {
72
+ return makeContract<Erc20>(web3, {
73
+ ...Erc20Json,
74
+ networks: {
75
+ [chainId]: { address: tokenAddress },
76
+ },
77
+ }, chainId);
65
78
  }