@defisaver/automation-sdk 3.0.2-dev → 3.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.
@@ -32,7 +32,9 @@ class StrategiesAutomation extends Automation_1.default {
32
32
  getEventFromSubStorage(event, options) {
33
33
  var _a;
34
34
  return __awaiter(this, void 0, void 0, function* () {
35
- if (new decimal_js_1.default(this.subStorageContract.createdBlock.toString()).gt(((_a = options === null || options === void 0 ? void 0 : options.fromBlock) === null || _a === void 0 ? void 0 : _a.toString()) || 0)) {
35
+ // only used for backfilling, so in case options?.fromBlock in undefined
36
+ // (just like we omit fromBlock when we call from app when not on fork), we still want to fetch events
37
+ if (new decimal_js_1.default(this.subStorageContract.createdBlock.toString()).gt(((_a = options === null || options === void 0 ? void 0 : options.fromBlock) === null || _a === void 0 ? void 0 : _a.toString()) || this.subStorageContract.createdBlock.toString())) {
36
38
  return [];
37
39
  }
38
40
  return (0, ethereumService_1.getEventsFromContract)(this.subStorageContract, this.subStorageContractFork, event, options);
@@ -90,6 +90,11 @@ exports.MAINNET_STRATEGIES_INFO = {
90
90
  strategyId: enums_1.Strategies.Identifiers.DebtInFrontRepay,
91
91
  protocol: exports.PROTOCOLS.Liquity,
92
92
  },
93
+ [enums_1.Strategies.MainnetIds.CURVEUSD_PAYBACK]: {
94
+ strategyOrBundleId: enums_1.Strategies.MainnetIds.CURVEUSD_PAYBACK,
95
+ strategyId: enums_1.Strategies.Identifiers.Payback,
96
+ protocol: exports.PROTOCOLS.CrvUSD,
97
+ },
93
98
  };
94
99
  exports.OPTIMISM_STRATEGIES_INFO = {
95
100
  [enums_1.Strategies.OptimismIds.EXCHANGE_DCA]: {
@@ -520,6 +520,7 @@ function parseCrvUSDLeverageManagement(position, parseData) {
520
520
  const subData = subDataService.crvUSDLeverageManagementSubData.decode(subStruct.subData);
521
521
  _position.strategyData.decoded.triggerData = triggerData;
522
522
  _position.strategyData.decoded.subData = subData;
523
+ _position.positionId = (0, utils_1.getPositionId)(_position.chainId, _position.protocol.id, _position.owner, triggerData.controller);
523
524
  const isRepay = _position.strategy.strategyId === enums_1.Strategies.Identifiers.Repay;
524
525
  if (isRepay) {
525
526
  _position.specific = {
@@ -541,10 +542,20 @@ function parseCrvUSDLeverageManagement(position, parseData) {
541
542
  mergeId: enums_1.Strategies.Identifiers.Boost,
542
543
  };
543
544
  }
544
- _position.positionId = (0, utils_1.getPositionId)(_position.chainId, _position.protocol.id, _position.owner, triggerData.controller);
545
545
  _position.strategy.strategyId = enums_1.Strategies.IdOverrides.LeverageManagement;
546
546
  return _position;
547
547
  }
548
+ function parseCrvUSDPayback(position, parseData) {
549
+ const _position = (0, lodash_1.cloneDeep)(position);
550
+ const { subStruct } = parseData.subscriptionEventData;
551
+ const triggerData = triggerService.crvUsdHealthRatioTrigger.decode(subStruct.triggerData);
552
+ const subData = subDataService.crvUSDPaybackSubData.decode(subStruct.subData);
553
+ _position.strategyData.decoded.triggerData = triggerData;
554
+ _position.strategyData.decoded.subData = subData;
555
+ _position.positionId = (0, utils_1.getPositionId)(_position.chainId, _position.protocol.id, _position.owner, triggerData.controller, Math.random());
556
+ _position.strategy.strategyId = enums_1.Strategies.Identifiers.Payback;
557
+ return _position;
558
+ }
548
559
  function parseMorphoBlueLeverageManagement(position, parseData) {
549
560
  const _position = (0, lodash_1.cloneDeep)(position);
550
561
  const { subStruct, subId, subHash } = parseData.subscriptionEventData;
@@ -553,6 +564,7 @@ function parseMorphoBlueLeverageManagement(position, parseData) {
553
564
  const subData = subDataService.morphoBlueLeverageManagementSubData.decode(subStruct.subData);
554
565
  _position.strategyData.decoded.triggerData = triggerData;
555
566
  _position.strategyData.decoded.subData = subData;
567
+ _position.positionId = (0, utils_1.getPositionId)(_position.chainId, _position.protocol.id, _position.owner, triggerData.marketId);
556
568
  const isRepay = _position.strategy.strategyId === enums_1.Strategies.Identifiers.Repay;
557
569
  if (isRepay) {
558
570
  _position.specific = {
@@ -574,7 +586,6 @@ function parseMorphoBlueLeverageManagement(position, parseData) {
574
586
  mergeId: enums_1.Strategies.Identifiers.Boost,
575
587
  };
576
588
  }
577
- _position.positionId = (0, utils_1.getPositionId)(_position.chainId, _position.protocol.id, _position.owner, triggerData.marketId);
578
589
  _position.strategy.strategyId = enums_1.Strategies.IdOverrides.LeverageManagement;
579
590
  return _position;
580
591
  }
@@ -640,6 +651,7 @@ const parsingMethodsMapping = {
640
651
  [enums_1.ProtocolIdentifiers.StrategiesAutomation.CrvUSD]: {
641
652
  [enums_1.Strategies.Identifiers.Repay]: parseCrvUSDLeverageManagement,
642
653
  [enums_1.Strategies.Identifiers.Boost]: parseCrvUSDLeverageManagement,
654
+ [enums_1.Strategies.Identifiers.Payback]: parseCrvUSDPayback,
643
655
  },
644
656
  [enums_1.ProtocolIdentifiers.StrategiesAutomation.MorphoBlue]: {
645
657
  [enums_1.Strategies.Identifiers.Repay]: parseMorphoBlueLeverageManagement,
@@ -80,6 +80,7 @@ export declare const sparkEncode: {
80
80
  };
81
81
  export declare const crvUSDEncode: {
82
82
  leverageManagement(owner: EthereumAddress, controllerAddr: EthereumAddress, ratioState: RatioState, targetRatio: number, triggerRatio: number, collTokenAddr: EthereumAddress, crvUSDAddr: EthereumAddress): (boolean | string[] | Bundles.MainnetIds)[];
83
+ payback(proxyAddress: EthereumAddress, addressToPullTokensFrom: EthereumAddress, positionOwner: EthereumAddress, paybackAmount: string, crvUSDAddr: EthereumAddress, controllerAddr: EthereumAddress, minHealthRatio: number): (boolean | string[] | Strategies.MainnetIds)[];
83
84
  };
84
85
  export declare const morphoBlueEncode: {
85
86
  leverageManagement(marketId: string, loanToken: EthereumAddress, collToken: EthereumAddress, oracle: EthereumAddress, irm: EthereumAddress, lltv: string, ratioState: RatioState, targetRatio: number, triggerRatio: number, user: EthereumAddress): (boolean | string[] | Bundles.MainnetIds)[];
@@ -232,6 +232,13 @@ exports.crvUSDEncode = {
232
232
  const isBundle = true;
233
233
  return [strategyOrBundleId, isBundle, triggerData, subData];
234
234
  },
235
+ payback(proxyAddress, addressToPullTokensFrom, positionOwner, paybackAmount, crvUSDAddr, controllerAddr, minHealthRatio) {
236
+ const subData = subDataService.crvUSDPaybackSubData.encode(controllerAddr, addressToPullTokensFrom, positionOwner, paybackAmount, crvUSDAddr);
237
+ const triggerData = triggerService.crvUsdHealthRatioTrigger.encode(proxyAddress, controllerAddr, minHealthRatio);
238
+ const strategyId = enums_1.Strategies.MainnetIds.CURVEUSD_PAYBACK;
239
+ const isBundle = false;
240
+ return [strategyId, isBundle, triggerData, subData];
241
+ },
235
242
  };
236
243
  exports.morphoBlueEncode = {
237
244
  leverageManagement(marketId, loanToken, collToken, oracle, irm, lltv, ratioState, targetRatio, triggerRatio, user) {
@@ -750,6 +750,40 @@ describe('Feature: strategySubService.ts', () => {
750
750
  });
751
751
  });
752
752
  });
753
+ describe('payback()', () => {
754
+ const examples = [
755
+ [
756
+ [
757
+ enums_1.Strategies.MainnetIds.CURVEUSD_PAYBACK,
758
+ false,
759
+ [
760
+ '0x0000000000000000000000007a2af22ba3276108cd331c8985ef9528e10a871a000000000000000000000000a920de414ea4ab66b97da1bfe9e6eca7d421963500000000000000000000000000000000000000000000000002c68af0bb140000',
761
+ ],
762
+ [
763
+ '0x000000000000000000000000a920de414ea4ab66b97da1bfe9e6eca7d4219635',
764
+ '0x000000000000000000000000dc0ad7a48088f1aa55d26f8b36f7c1e827ddd280',
765
+ '0x000000000000000000000000dc0ad7a48088f1aa55d26f8b36f7c1e827ddd280',
766
+ '0x00000000000000000000000000000000000000000000043c33c1937564800000',
767
+ '0x000000000000000000000000f939e0a03fb07f59a73314e73794be0e57ac1b4e'
768
+ ],
769
+ ],
770
+ [
771
+ web3Utils.toChecksumAddress('0x7a2af22ba3276108cd331c8985ef9528e10a871a'),
772
+ web3Utils.toChecksumAddress('0xDc0Ad7a48088f1AA55d26f8b36F7C1E827DdD280'),
773
+ web3Utils.toChecksumAddress('0xDc0Ad7a48088f1AA55d26f8b36F7C1E827DdD280'),
774
+ '20000',
775
+ '0xf939E0A03FB07F59A73314E73794Be0E57ac1b4E',
776
+ web3Utils.toChecksumAddress('0xA920De414eA4Ab66b97dA1bFE9e6EcA7d4219635'),
777
+ 20,
778
+ ]
779
+ ]
780
+ ];
781
+ examples.forEach(([expected, actual]) => {
782
+ it(`Given ${actual} should return expected value: ${JSON.stringify(expected)}`, () => {
783
+ (0, chai_1.expect)(strategySubService_1.crvUSDEncode.payback(...actual)).to.eql(expected);
784
+ });
785
+ });
786
+ });
753
787
  });
754
788
  describe('When testing strategySubService.morphoBlueEncode', () => {
755
789
  describe('leverageManagement()', () => {
@@ -158,6 +158,15 @@ export declare const crvUSDLeverageManagementSubData: {
158
158
  targetRatio: number;
159
159
  };
160
160
  };
161
+ export declare const crvUSDPaybackSubData: {
162
+ encode: (controllerAddr: EthereumAddress, addressToPullTokensFrom: EthereumAddress, positionOwner: EthereumAddress, paybackAmount: string, crvUSDAddr: EthereumAddress) => string[];
163
+ decode: (subData: string[]) => {
164
+ controller: string;
165
+ addressToPullTokensFrom: string;
166
+ positionOwner: string;
167
+ paybackAmount: string;
168
+ };
169
+ };
161
170
  export declare const morphoBlueLeverageManagementSubData: {
162
171
  encode: (loanToken: EthereumAddress, collToken: EthereumAddress, oracle: EthereumAddress, irm: EthereumAddress, lltv: string, ratioState: RatioState, targetRatio: number, user: EthereumAddress) => string[];
163
172
  decode: (subData: string[]) => {
@@ -3,9 +3,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.morphoBlueLeverageManagementSubData = exports.crvUSDLeverageManagementSubData = exports.liquityDebtInFrontRepaySubData = exports.liquityDsrSupplySubData = exports.liquityDsrPaybackSubData = exports.sparkQuotePriceSubData = exports.sparkLeverageManagementSubData = exports.exchangeLimitOrderSubData = exports.exchangeDcaSubData = exports.liquityPaybackUsingChickenBondSubData = exports.cBondsRebondSubData = exports.morphoAaveV2LeverageManagementSubData = exports.compoundV3L2LeverageManagementSubData = exports.compoundV3LeverageManagementSubData = exports.compoundV2LeverageManagementSubData = exports.aaveV3QuotePriceSubData = exports.aaveV3LeverageManagementSubData = exports.aaveV2LeverageManagementSubData = exports.liquityCloseSubData = exports.liquityLeverageManagementSubData = exports.makerLeverageManagementSubData = exports.makerCloseSubData = exports.liquityRepayFromSavingsSubData = exports.makerRepayFromSavingsSubData = void 0;
6
+ exports.morphoBlueLeverageManagementSubData = exports.crvUSDPaybackSubData = exports.crvUSDLeverageManagementSubData = exports.liquityDebtInFrontRepaySubData = exports.liquityDsrSupplySubData = exports.liquityDsrPaybackSubData = exports.sparkQuotePriceSubData = exports.sparkLeverageManagementSubData = exports.exchangeLimitOrderSubData = exports.exchangeDcaSubData = exports.liquityPaybackUsingChickenBondSubData = exports.cBondsRebondSubData = exports.morphoAaveV2LeverageManagementSubData = exports.compoundV3L2LeverageManagementSubData = exports.compoundV3LeverageManagementSubData = exports.compoundV2LeverageManagementSubData = exports.aaveV3QuotePriceSubData = exports.aaveV3LeverageManagementSubData = exports.aaveV2LeverageManagementSubData = exports.liquityCloseSubData = exports.liquityLeverageManagementSubData = exports.makerLeverageManagementSubData = exports.makerCloseSubData = exports.liquityRepayFromSavingsSubData = exports.makerRepayFromSavingsSubData = void 0;
7
7
  const decimal_js_1 = __importDefault(require("decimal.js"));
8
8
  const web3_eth_abi_1 = __importDefault(require("web3-eth-abi"));
9
+ const web3_utils_1 = require("web3-utils");
9
10
  const tokens_1 = require("@defisaver/tokens");
10
11
  const sdk_1 = require("@defisaver/sdk");
11
12
  const enums_1 = require("../types/enums");
@@ -378,6 +379,35 @@ exports.crvUSDLeverageManagementSubData = {
378
379
  return { controller, targetRatio };
379
380
  },
380
381
  };
382
+ exports.crvUSDPaybackSubData = {
383
+ encode: (controllerAddr, addressToPullTokensFrom, positionOwner, paybackAmount, crvUSDAddr) => {
384
+ const controllerAddrEncoded = web3_eth_abi_1.default.encodeParameter('address', controllerAddr);
385
+ const addressToPullTokensFromEncoded = web3_eth_abi_1.default.encodeParameter('address', addressToPullTokensFrom);
386
+ const positionOwnerEncoded = web3_eth_abi_1.default.encodeParameter('address', positionOwner);
387
+ const paybackAmountEncoded = web3_eth_abi_1.default.encodeParameter('uint256', (0, web3_utils_1.toWei)(paybackAmount, 'ether'));
388
+ const crvUSDAddrEncoded = web3_eth_abi_1.default.encodeParameter('address', crvUSDAddr);
389
+ return [
390
+ controllerAddrEncoded,
391
+ addressToPullTokensFromEncoded,
392
+ positionOwnerEncoded,
393
+ paybackAmountEncoded,
394
+ crvUSDAddrEncoded,
395
+ ];
396
+ },
397
+ decode: (subData) => {
398
+ const controller = web3_eth_abi_1.default.decodeParameter('address', subData[0]);
399
+ const addressToPullTokensFrom = web3_eth_abi_1.default.decodeParameter('address', subData[1]);
400
+ const positionOwner = web3_eth_abi_1.default.decodeParameter('address', subData[2]);
401
+ const weiPaybackAmount = web3_eth_abi_1.default.decodeParameter('uint256', subData[3]);
402
+ const paybackAmount = (0, web3_utils_1.fromWei)(weiPaybackAmount, 'ether');
403
+ return {
404
+ controller,
405
+ addressToPullTokensFrom,
406
+ positionOwner,
407
+ paybackAmount,
408
+ };
409
+ },
410
+ };
381
411
  exports.morphoBlueLeverageManagementSubData = {
382
412
  encode: (loanToken, collToken, oracle, irm, lltv, ratioState, targetRatio, user) => {
383
413
  const loanTokenEncoded = web3_eth_abi_1.default.encodeParameter('address', loanToken);
@@ -1045,6 +1045,51 @@ describe('Feature: subDataService.ts', () => {
1045
1045
  });
1046
1046
  });
1047
1047
  });
1048
+ describe('When testing subDataService.crvUSDPaybackSubData', () => {
1049
+ describe('encode()', () => {
1050
+ const examples = [
1051
+ [
1052
+ [
1053
+ '0x000000000000000000000000a920de414ea4ab66b97da1bfe9e6eca7d4219635',
1054
+ '0x000000000000000000000000dc0ad7a48088f1aa55d26f8b36f7c1e827ddd280',
1055
+ '0x000000000000000000000000dc0ad7a48088f1aa55d26f8b36f7c1e827ddd280',
1056
+ '0x00000000000000000000000000000000000000000000043c33c1937564800000',
1057
+ '0x000000000000000000000000f939e0a03fb07f59a73314e73794be0e57ac1b4e',
1058
+ ],
1059
+ ['0xa920de414ea4ab66b97da1bfe9e6eca7d4219635', '0xDc0Ad7a48088f1AA55d26f8b36F7C1E827DdD280', '0xDc0Ad7a48088f1AA55d26f8b36F7C1E827DdD280', '20000', '0xf939E0A03FB07F59A73314E73794Be0E57ac1b4E'],
1060
+ ]
1061
+ ];
1062
+ examples.forEach(([expected, actual]) => {
1063
+ it(`Given ${actual} should return expected value: ${expected}`, () => {
1064
+ (0, chai_1.expect)(subDataService_1.crvUSDPaybackSubData.encode(...actual)).to.eql(expected);
1065
+ });
1066
+ });
1067
+ });
1068
+ describe('decode()', () => {
1069
+ const examples = [
1070
+ [
1071
+ {
1072
+ controller: '0xA920De414eA4Ab66b97dA1bFE9e6EcA7d4219635',
1073
+ addressToPullTokensFrom: '0xDc0Ad7a48088f1AA55d26f8b36F7C1E827DdD280',
1074
+ positionOwner: '0xDc0Ad7a48088f1AA55d26f8b36F7C1E827DdD280',
1075
+ paybackAmount: '20000'
1076
+ },
1077
+ [
1078
+ '0x000000000000000000000000a920de414ea4ab66b97da1bfe9e6eca7d4219635',
1079
+ '0x000000000000000000000000dc0ad7a48088f1aa55d26f8b36f7c1e827ddd280',
1080
+ '0x000000000000000000000000dc0ad7a48088f1aa55d26f8b36f7c1e827ddd280',
1081
+ '0x00000000000000000000000000000000000000000000043c33c1937564800000',
1082
+ '0x000000000000000000000000f939e0a03fb07f59a73314e73794be0e57ac1b4e',
1083
+ ]
1084
+ ]
1085
+ ];
1086
+ examples.forEach(([expected, actual]) => {
1087
+ it(`Given ${actual} should return expected value: ${JSON.stringify(expected)}`, () => {
1088
+ (0, chai_1.expect)(subDataService_1.crvUSDPaybackSubData.decode(actual)).to.eql(expected);
1089
+ });
1090
+ });
1091
+ });
1092
+ });
1048
1093
  describe('When testing subDataService.compoundV3L2LeverageManagementSubData', () => {
1049
1094
  describe('encode()', () => {
1050
1095
  const examples = [
@@ -172,6 +172,14 @@ export declare const crvUSDRatioTrigger: {
172
172
  ratioState: number;
173
173
  };
174
174
  };
175
+ export declare const crvUsdHealthRatioTrigger: {
176
+ encode(owner: EthereumAddress, controller: EthereumAddress, ratioPercentage: number): string[];
177
+ decode(triggerData: string[]): {
178
+ owner: string;
179
+ controller: string;
180
+ ratio: number;
181
+ };
182
+ };
175
183
  export declare const morphoBlueRatioTrigger: {
176
184
  encode(marketId: string, owner: EthereumAddress, ratioPercentage: number, ratioState: RatioState): string[];
177
185
  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.morphoBlueRatioTrigger = exports.crvUSDRatioTrigger = 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;
29
+ exports.morphoBlueRatioTrigger = exports.crvUsdHealthRatioTrigger = exports.crvUSDRatioTrigger = 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"));
@@ -335,6 +335,20 @@ exports.crvUSDRatioTrigger = {
335
335
  };
336
336
  },
337
337
  };
338
+ exports.crvUsdHealthRatioTrigger = {
339
+ encode(owner, controller, ratioPercentage) {
340
+ const ratioWei = (0, utils_1.ratioPercentageToWei)(ratioPercentage);
341
+ return [web3_eth_abi_1.default.encodeParameters(['address', 'address', 'uint256'], [owner, controller, ratioWei])];
342
+ },
343
+ decode(triggerData) {
344
+ const decodedData = web3_eth_abi_1.default.decodeParameters(['address', 'address', 'uint256'], triggerData[0]);
345
+ return {
346
+ owner: decodedData[0],
347
+ controller: decodedData[1],
348
+ ratio: (0, utils_1.weiToRatioPercentage)(decodedData[2]),
349
+ };
350
+ },
351
+ };
338
352
  exports.morphoBlueRatioTrigger = {
339
353
  encode(marketId, // bytes32
340
354
  owner, ratioPercentage, ratioState) {
@@ -859,6 +859,34 @@ describe('Feature: triggerService.ts', () => {
859
859
  });
860
860
  });
861
861
  });
862
+ describe('When testing triggerService.crvUsdHealthRatioTrigger', () => {
863
+ describe('encode()', () => {
864
+ const examples = [
865
+ [
866
+ ['0x0000000000000000000000007a2af22ba3276108cd331c8985ef9528e10a871a000000000000000000000000a920de414ea4ab66b97da1bfe9e6eca7d421963500000000000000000000000000000000000000000000000002c68af0bb140000'],
867
+ [web3Utils.toChecksumAddress('0x7a2af22ba3276108cd331c8985ef9528e10a871a'), web3Utils.toChecksumAddress('0xa920de414ea4ab66b97da1bfe9e6eca7d4219635'), 20]
868
+ ]
869
+ ];
870
+ examples.forEach(([expected, actual]) => {
871
+ it(`Given ${actual} should return expected value: ${JSON.stringify(expected)}`, () => {
872
+ (0, chai_1.expect)(triggerService_1.crvUsdHealthRatioTrigger.encode(...actual)).to.eql(expected);
873
+ });
874
+ });
875
+ });
876
+ describe('decode()', () => {
877
+ const examples = [
878
+ [
879
+ { owner: web3Utils.toChecksumAddress('0x7a2af22ba3276108cd331c8985ef9528e10a871a'), controller: web3Utils.toChecksumAddress('0xa920de414ea4ab66b97da1bfe9e6eca7d4219635'), ratio: 20 },
880
+ ['0x0000000000000000000000007a2af22ba3276108cd331c8985ef9528e10a871a000000000000000000000000a920de414ea4ab66b97da1bfe9e6eca7d421963500000000000000000000000000000000000000000000000002c68af0bb140000'],
881
+ ],
882
+ ];
883
+ examples.forEach(([expected, actual]) => {
884
+ it(`Given ${actual} should return expected value: ${JSON.stringify(expected)}`, () => {
885
+ (0, chai_1.expect)(triggerService_1.crvUsdHealthRatioTrigger.decode(actual)).to.eql(expected);
886
+ });
887
+ });
888
+ });
889
+ });
862
890
  describe('When testing triggerService.morphoBlueRatioTrigger', () => {
863
891
  describe('encode()', () => {
864
892
  const examples = [
@@ -58,7 +58,8 @@ export declare namespace Strategies {
58
58
  EXCHANGE_LIMIT_ORDER = 51,
59
59
  LIQUITY_DSR_PAYBACK = 69,
60
60
  LIQUITY_DSR_SUPPLY = 70,
61
- LIQUITY_DEBT_IN_FRONT_REPAY = 75
61
+ LIQUITY_DEBT_IN_FRONT_REPAY = 75,
62
+ CURVEUSD_PAYBACK = 92
62
63
  }
63
64
  enum OptimismIds {
64
65
  EXCHANGE_DCA = 8,
@@ -85,6 +86,7 @@ export declare namespace Strategies {
85
86
  TrailingStopToColl = "trailing-stop-to-collateral",
86
87
  TrailingStopToDebt = "trailing-stop-to-debt",
87
88
  Rebond = "rebond",
89
+ Payback = "payback",
88
90
  BondProtection = "bond-protection",
89
91
  Dca = "dca",
90
92
  LimitOrder = "limit-order",
@@ -71,6 +71,7 @@ var Strategies;
71
71
  MainnetIds[MainnetIds["LIQUITY_DSR_PAYBACK"] = 69] = "LIQUITY_DSR_PAYBACK";
72
72
  MainnetIds[MainnetIds["LIQUITY_DSR_SUPPLY"] = 70] = "LIQUITY_DSR_SUPPLY";
73
73
  MainnetIds[MainnetIds["LIQUITY_DEBT_IN_FRONT_REPAY"] = 75] = "LIQUITY_DEBT_IN_FRONT_REPAY";
74
+ MainnetIds[MainnetIds["CURVEUSD_PAYBACK"] = 92] = "CURVEUSD_PAYBACK";
74
75
  })(MainnetIds = Strategies.MainnetIds || (Strategies.MainnetIds = {}));
75
76
  let OptimismIds;
76
77
  (function (OptimismIds) {
@@ -100,6 +101,7 @@ var Strategies;
100
101
  Identifiers["TrailingStopToColl"] = "trailing-stop-to-collateral";
101
102
  Identifiers["TrailingStopToDebt"] = "trailing-stop-to-debt";
102
103
  Identifiers["Rebond"] = "rebond";
104
+ Identifiers["Payback"] = "payback";
103
105
  Identifiers["BondProtection"] = "bond-protection";
104
106
  Identifiers["Dca"] = "dca";
105
107
  Identifiers["LimitOrder"] = "limit-order";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@defisaver/automation-sdk",
3
- "version": "3.0.2-dev",
3
+ "version": "3.0.3",
4
4
  "description": "",
5
5
  "main": "./umd/index.js",
6
6
  "module": "./esm/index.js",
@@ -46,7 +46,9 @@ export default class StrategiesAutomation extends Automation {
46
46
  }
47
47
 
48
48
  protected async getEventFromSubStorage(event: string, options?: PastEventOptions) {
49
- if (new Dec(this.subStorageContract.createdBlock.toString()).gt(options?.fromBlock?.toString() || 0)) {
49
+ // only used for backfilling, so in case options?.fromBlock in undefined
50
+ // (just like we omit fromBlock when we call from app when not on fork), we still want to fetch events
51
+ if (new Dec(this.subStorageContract.createdBlock.toString()).gt(options?.fromBlock?.toString() || this.subStorageContract.createdBlock.toString())) {
50
52
  return [];
51
53
  }
52
54
  return getEventsFromContract<SubStorage>(this.subStorageContract, this.subStorageContractFork, event, options);
@@ -95,6 +95,11 @@ export const MAINNET_STRATEGIES_INFO: MainnetStrategiesInfo = {
95
95
  strategyId: Strategies.Identifiers.DebtInFrontRepay,
96
96
  protocol: PROTOCOLS.Liquity,
97
97
  },
98
+ [Strategies.MainnetIds.CURVEUSD_PAYBACK]: {
99
+ strategyOrBundleId: Strategies.MainnetIds.CURVEUSD_PAYBACK,
100
+ strategyId: Strategies.Identifiers.Payback,
101
+ protocol: PROTOCOLS.CrvUSD,
102
+ },
98
103
  };
99
104
 
100
105
  export const OPTIMISM_STRATEGIES_INFO = {
@@ -66,4 +66,4 @@ export async function getEventsFromContract<T extends BaseContract>(
66
66
  }
67
67
 
68
68
  return [...events, ...eventsFork];
69
- }
69
+ }
@@ -692,7 +692,10 @@ function parseCrvUSDLeverageManagement(position: Position.Automated, parseData:
692
692
  _position.strategyData.decoded.triggerData = triggerData;
693
693
  _position.strategyData.decoded.subData = subData;
694
694
 
695
+ _position.positionId = getPositionId(_position.chainId, _position.protocol.id, _position.owner, triggerData.controller);
696
+
695
697
  const isRepay = _position.strategy.strategyId === Strategies.Identifiers.Repay;
698
+
696
699
  if (isRepay) {
697
700
  _position.specific = {
698
701
  triggerRepayRatio: triggerData.ratio,
@@ -713,12 +716,26 @@ function parseCrvUSDLeverageManagement(position: Position.Automated, parseData:
713
716
  };
714
717
  }
715
718
 
716
- _position.positionId = getPositionId(_position.chainId, _position.protocol.id, _position.owner, triggerData.controller);
717
719
  _position.strategy.strategyId = Strategies.IdOverrides.LeverageManagement;
718
720
 
719
721
  return _position;
720
722
  }
721
723
 
724
+ function parseCrvUSDPayback(position: Position.Automated, parseData: ParseData): Position.Automated {
725
+ const _position = cloneDeep(position);
726
+
727
+ const { subStruct } = parseData.subscriptionEventData;
728
+ const triggerData = triggerService.crvUsdHealthRatioTrigger.decode(subStruct.triggerData);
729
+ const subData = subDataService.crvUSDPaybackSubData.decode(subStruct.subData);
730
+
731
+ _position.strategyData.decoded.triggerData = triggerData;
732
+ _position.strategyData.decoded.subData = subData;
733
+ _position.positionId = getPositionId(_position.chainId, _position.protocol.id, _position.owner, triggerData.controller, Math.random());
734
+ _position.strategy.strategyId = Strategies.Identifiers.Payback;
735
+
736
+ return _position;
737
+ }
738
+
722
739
  function parseMorphoBlueLeverageManagement(position: Position.Automated, parseData: ParseData): Position.Automated {
723
740
  const _position = cloneDeep(position);
724
741
 
@@ -729,6 +746,9 @@ function parseMorphoBlueLeverageManagement(position: Position.Automated, parseDa
729
746
 
730
747
  _position.strategyData.decoded.triggerData = triggerData;
731
748
  _position.strategyData.decoded.subData = subData;
749
+
750
+ _position.positionId = getPositionId(_position.chainId, _position.protocol.id, _position.owner, triggerData.marketId);
751
+
732
752
  const isRepay = _position.strategy.strategyId === Strategies.Identifiers.Repay;
733
753
 
734
754
  if (isRepay) {
@@ -751,7 +771,6 @@ function parseMorphoBlueLeverageManagement(position: Position.Automated, parseDa
751
771
  };
752
772
  }
753
773
 
754
- _position.positionId = getPositionId(_position.chainId, _position.protocol.id, _position.owner, triggerData.marketId);
755
774
  _position.strategy.strategyId = Strategies.IdOverrides.LeverageManagement;
756
775
 
757
776
  return _position;
@@ -819,6 +838,7 @@ const parsingMethodsMapping: StrategiesToProtocolVersionMapping = {
819
838
  [ProtocolIdentifiers.StrategiesAutomation.CrvUSD]: {
820
839
  [Strategies.Identifiers.Repay]: parseCrvUSDLeverageManagement,
821
840
  [Strategies.Identifiers.Boost]: parseCrvUSDLeverageManagement,
841
+ [Strategies.Identifiers.Payback]: parseCrvUSDPayback,
822
842
  },
823
843
  [ProtocolIdentifiers.StrategiesAutomation.MorphoBlue]: {
824
844
  [Strategies.Identifiers.Repay]: parseMorphoBlueLeverageManagement,
@@ -899,6 +899,44 @@ describe('Feature: strategySubService.ts', () => {
899
899
  });
900
900
  });
901
901
  });
902
+ describe('payback()', () => {
903
+ const examples: Array<[
904
+ [StrategyOrBundleIds, boolean, TriggerData, SubData],
905
+ [owner: EthereumAddress, addressToPullTokensFrom: EthereumAddress, positionOwner: EthereumAddress, paybackAmount: string, crvUSDAddr: EthereumAddress, controllerAddr: EthereumAddress, minHealthRatio: number],
906
+ ]> = [
907
+ [
908
+ [
909
+ Strategies.MainnetIds.CURVEUSD_PAYBACK,
910
+ false,
911
+ [
912
+ '0x0000000000000000000000007a2af22ba3276108cd331c8985ef9528e10a871a000000000000000000000000a920de414ea4ab66b97da1bfe9e6eca7d421963500000000000000000000000000000000000000000000000002c68af0bb140000',
913
+ ],
914
+ [
915
+ '0x000000000000000000000000a920de414ea4ab66b97da1bfe9e6eca7d4219635',
916
+ '0x000000000000000000000000dc0ad7a48088f1aa55d26f8b36f7c1e827ddd280',
917
+ '0x000000000000000000000000dc0ad7a48088f1aa55d26f8b36f7c1e827ddd280',
918
+ '0x00000000000000000000000000000000000000000000043c33c1937564800000',
919
+ '0x000000000000000000000000f939e0a03fb07f59a73314e73794be0e57ac1b4e'
920
+ ],
921
+ ],
922
+ [
923
+ web3Utils.toChecksumAddress('0x7a2af22ba3276108cd331c8985ef9528e10a871a'),
924
+ web3Utils.toChecksumAddress('0xDc0Ad7a48088f1AA55d26f8b36F7C1E827DdD280'),
925
+ web3Utils.toChecksumAddress('0xDc0Ad7a48088f1AA55d26f8b36F7C1E827DdD280'),
926
+ '20000',
927
+ '0xf939E0A03FB07F59A73314E73794Be0E57ac1b4E',
928
+ web3Utils.toChecksumAddress('0xA920De414eA4Ab66b97dA1bFE9e6EcA7d4219635'),
929
+ 20,
930
+ ]
931
+ ]
932
+ ];
933
+
934
+ examples.forEach(([expected, actual]) => {
935
+ it(`Given ${actual} should return expected value: ${JSON.stringify(expected)}`, () => {
936
+ expect(crvUSDEncode.payback(...actual)).to.eql(expected);
937
+ });
938
+ });
939
+ });
902
940
  });
903
941
  describe('When testing strategySubService.morphoBlueEncode', () => {
904
942
  describe('leverageManagement()', () => {
@@ -447,6 +447,23 @@ export const crvUSDEncode = {
447
447
 
448
448
  return [strategyOrBundleId, isBundle, triggerData, subData];
449
449
  },
450
+ payback(
451
+ proxyAddress: EthereumAddress,
452
+ addressToPullTokensFrom: EthereumAddress,
453
+ positionOwner: EthereumAddress,
454
+ paybackAmount: string,
455
+ crvUSDAddr: EthereumAddress,
456
+ controllerAddr: EthereumAddress,
457
+ minHealthRatio: number,
458
+ ) {
459
+ const subData = subDataService.crvUSDPaybackSubData.encode(controllerAddr, addressToPullTokensFrom, positionOwner, paybackAmount, crvUSDAddr);
460
+ const triggerData = triggerService.crvUsdHealthRatioTrigger.encode(proxyAddress, controllerAddr, minHealthRatio);
461
+
462
+ const strategyId = Strategies.MainnetIds.CURVEUSD_PAYBACK;
463
+ const isBundle = false;
464
+
465
+ return [strategyId, isBundle, triggerData, subData];
466
+ },
450
467
  };
451
468
 
452
469
  export const morphoBlueEncode = {
@@ -30,7 +30,7 @@ import {
30
30
  sparkLeverageManagementSubData,
31
31
  sparkQuotePriceSubData,
32
32
  crvUSDLeverageManagementSubData,
33
- compoundV3L2LeverageManagementSubData, morphoBlueLeverageManagementSubData,
33
+ compoundV3L2LeverageManagementSubData, morphoBlueLeverageManagementSubData, crvUSDPaybackSubData,
34
34
  } from './subDataService';
35
35
 
36
36
  describe('Feature: subDataService.ts', () => {
@@ -1125,6 +1125,58 @@ describe('Feature: subDataService.ts', () => {
1125
1125
  });
1126
1126
  });
1127
1127
 
1128
+ describe('When testing subDataService.crvUSDPaybackSubData', () => {
1129
+ describe('encode()', () => {
1130
+ const examples: Array<[SubData, [controller: EthereumAddress, addressToPullTokensFrom: EthereumAddress, positionOwner: EthereumAddress, paybackAmount: string, crvUSDAddr: EthereumAddress]]> = [
1131
+ [
1132
+ [
1133
+ '0x000000000000000000000000a920de414ea4ab66b97da1bfe9e6eca7d4219635',
1134
+ '0x000000000000000000000000dc0ad7a48088f1aa55d26f8b36f7c1e827ddd280',
1135
+ '0x000000000000000000000000dc0ad7a48088f1aa55d26f8b36f7c1e827ddd280',
1136
+ '0x00000000000000000000000000000000000000000000043c33c1937564800000',
1137
+ '0x000000000000000000000000f939e0a03fb07f59a73314e73794be0e57ac1b4e',
1138
+ ],
1139
+ ['0xa920de414ea4ab66b97da1bfe9e6eca7d4219635', '0xDc0Ad7a48088f1AA55d26f8b36F7C1E827DdD280', '0xDc0Ad7a48088f1AA55d26f8b36F7C1E827DdD280', '20000', '0xf939E0A03FB07F59A73314E73794Be0E57ac1b4E'],
1140
+ ]
1141
+ ];
1142
+ examples.forEach(([expected, actual]) => {
1143
+ it(`Given ${actual} should return expected value: ${expected}`, () => {
1144
+ expect(crvUSDPaybackSubData.encode(...actual)).to.eql(expected);
1145
+ });
1146
+ });
1147
+ });
1148
+ describe('decode()', () => {
1149
+ const examples: Array<[{
1150
+ controller: EthereumAddress,
1151
+ addressToPullTokensFrom: EthereumAddress,
1152
+ positionOwner: EthereumAddress,
1153
+ paybackAmount: string,
1154
+ }, SubData]> = [
1155
+ [
1156
+ {
1157
+ controller: '0xA920De414eA4Ab66b97dA1bFE9e6EcA7d4219635',
1158
+ addressToPullTokensFrom: '0xDc0Ad7a48088f1AA55d26f8b36F7C1E827DdD280',
1159
+ positionOwner: '0xDc0Ad7a48088f1AA55d26f8b36F7C1E827DdD280',
1160
+ paybackAmount: '20000'
1161
+ },
1162
+ [
1163
+ '0x000000000000000000000000a920de414ea4ab66b97da1bfe9e6eca7d4219635',
1164
+ '0x000000000000000000000000dc0ad7a48088f1aa55d26f8b36f7c1e827ddd280',
1165
+ '0x000000000000000000000000dc0ad7a48088f1aa55d26f8b36f7c1e827ddd280',
1166
+ '0x00000000000000000000000000000000000000000000043c33c1937564800000',
1167
+ '0x000000000000000000000000f939e0a03fb07f59a73314e73794be0e57ac1b4e',
1168
+ ]
1169
+ ]
1170
+ ];
1171
+
1172
+ examples.forEach(([expected, actual]) => {
1173
+ it(`Given ${actual} should return expected value: ${JSON.stringify(expected)}`, () => {
1174
+ expect(crvUSDPaybackSubData.decode(actual)).to.eql(expected);
1175
+ });
1176
+ });
1177
+ });
1178
+ });
1179
+
1128
1180
  describe('When testing subDataService.compoundV3L2LeverageManagementSubData', () => {
1129
1181
  describe('encode()', () => {
1130
1182
  const examples: Array<[