@defisaver/automation-sdk 3.3.10 → 3.3.11-aave-v4-dev

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 (49) hide show
  1. package/cjs/constants/index.d.ts +1 -0
  2. package/cjs/constants/index.js +72 -1
  3. package/cjs/services/ethereumService.test.js +2 -0
  4. package/cjs/services/strategiesService.js +129 -0
  5. package/cjs/services/strategySubService.d.ts +19 -0
  6. package/cjs/services/strategySubService.js +37 -1
  7. package/cjs/services/strategySubService.test.js +229 -0
  8. package/cjs/services/subDataService.d.ts +298 -129
  9. package/cjs/services/subDataService.js +639 -369
  10. package/cjs/services/subDataService.test.js +214 -0
  11. package/cjs/services/triggerService.d.ts +38 -0
  12. package/cjs/services/triggerService.js +68 -1
  13. package/cjs/services/triggerService.test.js +130 -0
  14. package/cjs/services/utils.d.ts +1 -1
  15. package/cjs/services/utils.js +10 -2
  16. package/cjs/types/enums.d.ts +19 -3
  17. package/cjs/types/enums.js +16 -0
  18. package/cjs/types/index.d.ts +33 -1
  19. package/esm/constants/index.d.ts +1 -0
  20. package/esm/constants/index.js +71 -0
  21. package/esm/services/ethereumService.test.js +2 -0
  22. package/esm/services/strategiesService.js +130 -1
  23. package/esm/services/strategySubService.d.ts +19 -0
  24. package/esm/services/strategySubService.js +36 -0
  25. package/esm/services/strategySubService.test.js +230 -1
  26. package/esm/services/subDataService.d.ts +298 -129
  27. package/esm/services/subDataService.js +639 -369
  28. package/esm/services/subDataService.test.js +214 -0
  29. package/esm/services/triggerService.d.ts +38 -0
  30. package/esm/services/triggerService.js +67 -0
  31. package/esm/services/triggerService.test.js +131 -1
  32. package/esm/services/utils.d.ts +1 -1
  33. package/esm/services/utils.js +10 -2
  34. package/esm/types/enums.d.ts +19 -3
  35. package/esm/types/enums.js +16 -0
  36. package/esm/types/index.d.ts +33 -1
  37. package/package.json +2 -2
  38. package/src/constants/index.ts +72 -0
  39. package/src/services/ethereumService.test.ts +2 -0
  40. package/src/services/strategiesService.ts +150 -2
  41. package/src/services/strategySubService.test.ts +286 -0
  42. package/src/services/strategySubService.ts +140 -0
  43. package/src/services/subDataService.test.ts +239 -0
  44. package/src/services/subDataService.ts +1072 -784
  45. package/src/services/triggerService.test.ts +148 -0
  46. package/src/services/triggerService.ts +97 -1
  47. package/src/services/utils.ts +15 -4
  48. package/src/types/enums.ts +16 -0
  49. package/src/types/index.ts +40 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@defisaver/automation-sdk",
3
- "version": "3.3.10",
3
+ "version": "3.3.11-aave-v4-dev",
4
4
  "description": "",
5
5
  "main": "./cjs/index.js",
6
6
  "module": "./esm/index.js",
@@ -23,7 +23,7 @@
23
23
  "repository": "https://github.com/defisaver/automation-sdk",
24
24
  "license": "ISC",
25
25
  "dependencies": {
26
- "@defisaver/sdk": "^1.3.15",
26
+ "@defisaver/sdk": "1.3.21-aave-v4-dev",
27
27
  "@defisaver/tokens": "^1.6.19",
28
28
  "@ethersproject/address": "^5.0.10",
29
29
  "@ethersproject/solidity": "^5.0.9",
@@ -23,6 +23,8 @@ import LegacyProtocol from '../automation/private/LegacyProtocol';
23
23
  // General
24
24
  export const ZERO_ADDRESS: EthereumAddress = '0x0000000000000000000000000000000000000000';
25
25
 
26
+ export const EMPTY_SLOT: string = '0x0000000000000000000000000000000000000000000000000000000000000000';
27
+
26
28
  export const AAVE_V3_VARIABLE_BORROW_RATE = 2;
27
29
 
28
30
  export const PROTOCOLS: Record<keyof typeof ProtocolIdentifiers.StrategiesAutomation, Interfaces.Protocol> = (() => {
@@ -123,6 +125,16 @@ export const MAINNET_STRATEGIES_INFO: MainnetStrategiesInfo = {
123
125
  strategyId: Strategies.Identifiers.CollateralSwitch,
124
126
  protocol: PROTOCOLS.AaveV3,
125
127
  },
128
+ [Strategies.MainnetIds.AAVE_V4_COLLATERAL_SWITCH]: {
129
+ strategyOrBundleId: Strategies.MainnetIds.AAVE_V4_COLLATERAL_SWITCH,
130
+ strategyId: Strategies.Identifiers.CollateralSwitch,
131
+ protocol: PROTOCOLS.AaveV4,
132
+ },
133
+ [Strategies.MainnetIds.AAVE_V4_COLLATERAL_SWITCH_EOA]: {
134
+ strategyOrBundleId: Strategies.MainnetIds.AAVE_V4_COLLATERAL_SWITCH_EOA,
135
+ strategyId: Strategies.Identifiers.EoaCollateralSwitch,
136
+ protocol: PROTOCOLS.AaveV4,
137
+ },
126
138
  };
127
139
 
128
140
  export const OPTIMISM_STRATEGIES_INFO: OptimismStrategiesInfo = {
@@ -496,6 +508,66 @@ export const MAINNET_BUNDLES_INFO: MainnetBundleInfo = {
496
508
  strategyId: Strategies.Identifiers.CloseOnPrice,
497
509
  protocol: PROTOCOLS.MorphoBlue,
498
510
  },
511
+ [Bundles.MainnetIds.SPARK_REPAY_ON_PRICE]: {
512
+ strategyOrBundleId: Bundles.MainnetIds.SPARK_REPAY_ON_PRICE,
513
+ strategyId: Strategies.Identifiers.RepayOnPrice,
514
+ protocol: PROTOCOLS.Spark,
515
+ },
516
+ [Bundles.MainnetIds.SPARK_BOOST_ON_PRICE]: {
517
+ strategyOrBundleId: Bundles.MainnetIds.SPARK_BOOST_ON_PRICE,
518
+ strategyId: Strategies.Identifiers.BoostOnPrice,
519
+ protocol: PROTOCOLS.Spark,
520
+ },
521
+ [Bundles.MainnetIds.AAVE_V4_REPAY]: {
522
+ strategyOrBundleId: Bundles.MainnetIds.AAVE_V4_REPAY,
523
+ strategyId: Strategies.Identifiers.Repay,
524
+ protocol: PROTOCOLS.AaveV4,
525
+ },
526
+ [Bundles.MainnetIds.AAVE_V4_BOOST]: {
527
+ strategyOrBundleId: Bundles.MainnetIds.AAVE_V4_BOOST,
528
+ strategyId: Strategies.Identifiers.Boost,
529
+ protocol: PROTOCOLS.AaveV4,
530
+ },
531
+ [Bundles.MainnetIds.AAVE_V4_REPAY_ON_PRICE]: {
532
+ strategyOrBundleId: Bundles.MainnetIds.AAVE_V4_REPAY_ON_PRICE,
533
+ strategyId: Strategies.Identifiers.RepayOnPrice,
534
+ protocol: PROTOCOLS.AaveV4,
535
+ },
536
+ [Bundles.MainnetIds.AAVE_V4_BOOST_ON_PRICE]: {
537
+ strategyOrBundleId: Bundles.MainnetIds.AAVE_V4_BOOST_ON_PRICE,
538
+ strategyId: Strategies.Identifiers.BoostOnPrice,
539
+ protocol: PROTOCOLS.AaveV4,
540
+ },
541
+ [Bundles.MainnetIds.AAVE_V4_CLOSE]: {
542
+ strategyOrBundleId: Bundles.MainnetIds.AAVE_V4_CLOSE,
543
+ strategyId: Strategies.Identifiers.CloseOnPrice,
544
+ protocol: PROTOCOLS.AaveV4,
545
+ },
546
+ [Bundles.MainnetIds.AAVE_V4_EOA_REPAY]: {
547
+ strategyOrBundleId: Bundles.MainnetIds.AAVE_V4_EOA_REPAY,
548
+ strategyId: Strategies.Identifiers.EoaRepay,
549
+ protocol: PROTOCOLS.AaveV4,
550
+ },
551
+ [Bundles.MainnetIds.AAVE_V4_EOA_BOOST]: {
552
+ strategyOrBundleId: Bundles.MainnetIds.AAVE_V4_EOA_BOOST,
553
+ strategyId: Strategies.Identifiers.EoaBoost,
554
+ protocol: PROTOCOLS.AaveV4,
555
+ },
556
+ [Bundles.MainnetIds.AAVE_V4_EOA_REPAY_ON_PRICE]: {
557
+ strategyOrBundleId: Bundles.MainnetIds.AAVE_V4_EOA_REPAY_ON_PRICE,
558
+ strategyId: Strategies.Identifiers.EoaRepayOnPrice,
559
+ protocol: PROTOCOLS.AaveV4,
560
+ },
561
+ [Bundles.MainnetIds.AAVE_V4_EOA_BOOST_ON_PRICE]: {
562
+ strategyOrBundleId: Bundles.MainnetIds.AAVE_V4_EOA_BOOST_ON_PRICE,
563
+ strategyId: Strategies.Identifiers.EoaBoostOnPrice,
564
+ protocol: PROTOCOLS.AaveV4,
565
+ },
566
+ [Bundles.MainnetIds.AAVE_V4_EOA_CLOSE]: {
567
+ strategyOrBundleId: Bundles.MainnetIds.AAVE_V4_EOA_CLOSE,
568
+ strategyId: Strategies.Identifiers.EoaCloseOnPrice,
569
+ protocol: PROTOCOLS.AaveV4,
570
+ },
499
571
  };
500
572
 
501
573
  export const OPTIMISM_BUNDLES_INFO: OptimismBundleInfo = {
@@ -161,6 +161,7 @@ describe('Feature: ethereumService.ts', () => {
161
161
  'address': '0xDA10009cBd5D07dd0CeCc66161FC93D7c9000da1',
162
162
  'blockHash': '0xacb0213af63b4c17c436f084a96d1ac385641a59a9a4cf014ae3337cbe545aa7',
163
163
  'blockNumber': 5353002,
164
+ 'blockTimestamp': '0x624c1a5b',
164
165
  'event': 'Transfer',
165
166
  'id': 'log_f49645b8',
166
167
  'logIndex': 1,
@@ -206,6 +207,7 @@ describe('Feature: ethereumService.ts', () => {
206
207
  'address': '0xDA10009cBd5D07dd0CeCc66161FC93D7c9000da1',
207
208
  'blockHash': '0xacb0213af63b4c17c436f084a96d1ac385641a59a9a4cf014ae3337cbe545aa7',
208
209
  'blockNumber': 5353002,
210
+ 'blockTimestamp': '0x624c1a5b',
209
211
  'event': 'Transfer',
210
212
  'id': 'log_f49645b8',
211
213
  'logIndex': 1,
@@ -7,7 +7,9 @@ import type {
7
7
  Position, ParseData, StrategiesToProtocolVersionMapping, BundleOrStrategy, StrategyOrBundleIds,
8
8
  BundleInfoUnion, StrategyInfoUnion,
9
9
  } from '../types';
10
- import { ChainId, ProtocolIdentifiers, Strategies } from '../types/enums';
10
+ import {
11
+ ChainId, ProtocolIdentifiers, RatioState, Strategies,
12
+ } from '../types/enums';
11
13
 
12
14
  import {
13
15
  getPositionId, getRatioStateInfoForAaveCloseStrategy, getStopLossAndTakeProfitTypeByCloseStrategyType, isRatioStateOver, wethToEthByAddress,
@@ -383,6 +385,108 @@ function parseAaveV3CollateralSwitch(position: Position.Automated, parseData: Pa
383
385
  return _position;
384
386
  }
385
387
 
388
+ function parseAaveV4LeverageManagement(position: Position.Automated, parseData: ParseData): Position.Automated {
389
+ const _position = cloneDeep(position);
390
+ const { subStruct, subId, subHash } = parseData.subscriptionEventData;
391
+ const { isEnabled } = parseData.strategiesSubsData;
392
+ const triggerData = triggerService.aaveV4RatioTrigger.decode(subStruct.triggerData);
393
+ const subData = subDataService.aaveV4LeverageManagementSubData.decode(subStruct.subData);
394
+ const isEOA = _position.strategy.strategyId.includes('eoa');
395
+ const isRepay = [Strategies.Identifiers.Repay, Strategies.Identifiers.EoaRepay].includes(_position.strategy.strategyId as Strategies.Identifiers);
396
+
397
+ _position.strategyData.decoded.triggerData = triggerData;
398
+ _position.strategyData.decoded.subData = subData;
399
+ _position.positionId = getPositionId(_position.chainId, _position.protocol.id, _position.owner, triggerData.spoke);
400
+ _position.strategy.strategyId = isEOA ? Strategies.IdOverrides.EoaLeverageManagement : Strategies.IdOverrides.LeverageManagement;
401
+
402
+ if (isRepay) {
403
+ _position.specific = {
404
+ triggerRepayRatio: triggerData.ratio,
405
+ targetRepayRatio: subData.targetRatio,
406
+ repayEnabled: isEnabled,
407
+ subId1: Number(subId),
408
+ mergeWithId: isEOA ? Strategies.Identifiers.EoaBoost : Strategies.Identifiers.Boost,
409
+ subHashRepay: subHash,
410
+ };
411
+ } else {
412
+ _position.specific = {
413
+ triggerBoostRatio: triggerData.ratio,
414
+ targetBoostRatio: subData.targetRatio,
415
+ boostEnabled: isEnabled,
416
+ subId2: Number(subId),
417
+ mergeId: isEOA ? Strategies.Identifiers.EoaBoost : Strategies.Identifiers.Boost,
418
+ subHashBoost: subHash,
419
+ };
420
+ }
421
+
422
+ return _position;
423
+ }
424
+
425
+ function parseAaveV4LeverageManagementOnPrice(position: Position.Automated, parseData: ParseData): Position.Automated {
426
+ const _position = cloneDeep(position);
427
+ const { subStruct } = parseData.subscriptionEventData;
428
+ const triggerData = triggerService.aaveV4QuotePriceTrigger.decode(subStruct.triggerData);
429
+ const subData = subDataService.aaveV4LeverageManagementOnPriceSubData.decode(subStruct.subData);
430
+ const isEOA = _position.strategy.strategyId.includes('eoa');
431
+
432
+ _position.strategyData.decoded.triggerData = triggerData;
433
+ _position.strategyData.decoded.subData = subData;
434
+ _position.positionId = getPositionId(_position.chainId, _position.protocol.id, _position.owner, triggerData.spoke);
435
+ _position.strategy.strategyId = isEOA ? Strategies.IdOverrides.EoaLeverageManagementOnPrice : Strategies.IdOverrides.LeverageManagementOnPrice;
436
+
437
+ _position.specific = {
438
+ collAsset: subData.collAsset,
439
+ collAssetId: subData.collAssetId,
440
+ debtAsset: subData.debtAsset,
441
+ debtAssetId: subData.debtAssetId,
442
+ price: triggerData.price,
443
+ ratioState: triggerData.ratioState,
444
+ ratio: subData.targetRatio,
445
+ };
446
+
447
+ return _position;
448
+ }
449
+
450
+ function parseAaveV4CloseOnPrice(position: Position.Automated, parseData: ParseData): Position.Automated {
451
+ const _position = cloneDeep(position);
452
+ const { subStruct } = parseData.subscriptionEventData;
453
+ const triggerData = triggerService.aaveV4QuotePriceRangeTrigger.decode(subStruct.triggerData);
454
+ const subData = subDataService.aaveV4CloseSubData.decode(subStruct.subData);
455
+ const { takeProfitType, stopLossType } = getStopLossAndTakeProfitTypeByCloseStrategyType(+subData.closeType);
456
+ const isEOA = _position.strategy.strategyId.includes('eoa');
457
+
458
+ _position.strategyData.decoded.triggerData = triggerData;
459
+ _position.strategyData.decoded.subData = subData;
460
+ _position.positionId = getPositionId(_position.chainId, _position.protocol.id, _position.owner, triggerData.spoke);
461
+ _position.strategy.strategyId = isEOA ? Strategies.Identifiers.EoaCloseOnPrice : Strategies.Identifiers.CloseOnPrice;
462
+
463
+ _position.specific = {
464
+ collAsset: subData.collAsset,
465
+ collAssetId: subData.collAssetId,
466
+ debtAsset: subData.debtAsset,
467
+ debtAssetId: subData.debtAssetId,
468
+ stopLossPrice: triggerData.lowerPrice,
469
+ takeProfitPrice: triggerData.upperPrice,
470
+ stopLossType,
471
+ takeProfitType,
472
+ };
473
+
474
+ return _position;
475
+ }
476
+
477
+ function parseAaveV4CollateralSwitch(position: Position.Automated, parseData: ParseData): Position.Automated {
478
+ const _position = cloneDeep(position);
479
+ const { subStruct } = parseData.subscriptionEventData;
480
+ const triggerData = triggerService.aaveV4QuotePriceTrigger.decode(subStruct.triggerData);
481
+ const subData = subDataService.aaveV4CollateralSwitchSubData.decode(subStruct.subData);
482
+ const isEOA = _position.strategy.strategyId.includes('eoa');
483
+ _position.strategyData.decoded.triggerData = triggerData;
484
+ _position.strategyData.decoded.subData = subData;
485
+ _position.positionId = getPositionId(_position.chainId, _position.protocol.id, _position.owner, triggerData.spoke);
486
+ _position.strategy.strategyId = isEOA ? Strategies.Identifiers.EoaCollateralSwitch : Strategies.Identifiers.CollateralSwitch;
487
+ return _position;
488
+ }
489
+
386
490
  function parseMorphoAaveV2LeverageManagement(position: Position.Automated, parseData: ParseData): Position.Automated {
387
491
  const _position = cloneDeep(position);
388
492
 
@@ -782,9 +886,37 @@ function parseSparkLeverageManagement(position: Position.Automated, parseData: P
782
886
  return _position;
783
887
  }
784
888
 
785
- function parseSparkCloseOnPrice(position: Position.Automated, parseData: ParseData): Position.Automated {
889
+ function parseSparkLeverageManagementOnPrice(position: Position.Automated, parseData: ParseData): Position.Automated {
786
890
  const _position = cloneDeep(position);
891
+ const { subStruct } = parseData.subscriptionEventData;
892
+
893
+ const triggerData = triggerService.sparkQuotePriceTrigger.decode(subStruct.triggerData);
894
+ const subData = subDataService.sparkLeverageManagementOnPriceSubData.decode(subStruct.subData);
895
+
896
+ _position.strategyData.decoded.triggerData = triggerData;
897
+ _position.strategyData.decoded.subData = subData;
898
+
899
+ _position.positionId = getPositionId(_position.chainId, _position.protocol.id, _position.owner, subData.marketAddr);
787
900
 
901
+ _position.specific = {
902
+ // subData
903
+ collAsset: subData.collAsset,
904
+ collAssetId: subData.collAssetId,
905
+ debtAsset: subData.debtAsset,
906
+ debtAssetId: subData.debtAssetId,
907
+ ratio: subData.targetRatio,
908
+ // triggerData
909
+ baseToken: triggerData.baseTokenAddr,
910
+ quoteToken: triggerData.quoteTokenAddr,
911
+ price: triggerData.price,
912
+ ratioState: triggerData.ratioState,
913
+ };
914
+
915
+ return _position;
916
+ }
917
+
918
+ function parseSparkCloseOnPrice(position: Position.Automated, parseData: ParseData): Position.Automated {
919
+ const _position = cloneDeep(position);
788
920
  const { subStruct } = parseData.subscriptionEventData;
789
921
 
790
922
  const triggerData = triggerService.sparkQuotePriceRangeTrigger.decode(subStruct.triggerData);
@@ -1207,6 +1339,20 @@ const parsingMethodsMapping: StrategiesToProtocolVersionMapping = {
1207
1339
  [Strategies.Identifiers.EoaCloseOnPrice]: parseAaveV3CloseOnPrice,
1208
1340
  [Strategies.Identifiers.CollateralSwitch]: parseAaveV3CollateralSwitch,
1209
1341
  },
1342
+ [ProtocolIdentifiers.StrategiesAutomation.AaveV4]: {
1343
+ [Strategies.Identifiers.Repay]: parseAaveV4LeverageManagement,
1344
+ [Strategies.Identifiers.Boost]: parseAaveV4LeverageManagement,
1345
+ [Strategies.Identifiers.RepayOnPrice]: parseAaveV4LeverageManagementOnPrice,
1346
+ [Strategies.Identifiers.BoostOnPrice]: parseAaveV4LeverageManagementOnPrice,
1347
+ [Strategies.Identifiers.CloseOnPrice]: parseAaveV4CloseOnPrice,
1348
+ [Strategies.Identifiers.EoaRepay]: parseAaveV4LeverageManagement,
1349
+ [Strategies.Identifiers.EoaBoost]: parseAaveV4LeverageManagement,
1350
+ [Strategies.Identifiers.EoaRepayOnPrice]: parseAaveV4LeverageManagementOnPrice,
1351
+ [Strategies.Identifiers.EoaBoostOnPrice]: parseAaveV4LeverageManagementOnPrice,
1352
+ [Strategies.Identifiers.EoaCloseOnPrice]: parseAaveV4CloseOnPrice,
1353
+ [Strategies.Identifiers.CollateralSwitch]: parseAaveV4CollateralSwitch,
1354
+ [Strategies.Identifiers.EoaCollateralSwitch]: parseAaveV4CollateralSwitch,
1355
+ },
1210
1356
  [ProtocolIdentifiers.StrategiesAutomation.CompoundV2]: {
1211
1357
  [Strategies.Identifiers.Repay]: parseCompoundV2LeverageManagement,
1212
1358
  [Strategies.Identifiers.Boost]: parseCompoundV2LeverageManagement,
@@ -1237,6 +1383,8 @@ const parsingMethodsMapping: StrategiesToProtocolVersionMapping = {
1237
1383
  [ProtocolIdentifiers.StrategiesAutomation.Spark]: {
1238
1384
  [Strategies.Identifiers.Repay]: parseSparkLeverageManagement,
1239
1385
  [Strategies.Identifiers.Boost]: parseSparkLeverageManagement,
1386
+ [Strategies.Identifiers.RepayOnPrice]: parseSparkLeverageManagementOnPrice,
1387
+ [Strategies.Identifiers.BoostOnPrice]: parseSparkLeverageManagementOnPrice,
1240
1388
  [Strategies.Identifiers.CloseOnPrice]: parseSparkCloseOnPrice,
1241
1389
  },
1242
1390
  [ProtocolIdentifiers.StrategiesAutomation.CrvUSD]: {
@@ -21,6 +21,8 @@ import {
21
21
  crvUSDEncode,
22
22
  compoundV3L2Encode,
23
23
  morphoBlueEncode,
24
+ sparkEncode,
25
+ aaveV4Encode,
24
26
  } from './strategySubService';
25
27
 
26
28
  describe('Feature: strategySubService.ts', () => {
@@ -1730,4 +1732,288 @@ describe('Feature: strategySubService.ts', () => {
1730
1732
  });
1731
1733
  });
1732
1734
  });
1735
+
1736
+ describe('When testing strategySubService.sparkEncode', () => {
1737
+ describe('leverageManagement()', () => {
1738
+ const examples: Array<[string, [number, number, number, number, boolean]]> = [
1739
+ [
1740
+ '0x0000000000000000136dcc951d8c00000000000000000000214e8348c4f0000000000000000000001d24b2dfac52000000000000000000001a5e27eef13e000001',
1741
+ [140,240,210,190,true]
1742
+ ],
1743
+ [
1744
+ '0x0000000000000000130337bdce49000000000000000000001988fe4052b800000000000000000000281b57b028e1000000000000000000002223acf76376000000',
1745
+ [137, 184, 289, 246, false]
1746
+ ]
1747
+ ];
1748
+
1749
+ examples.forEach(([expected, actual]) => {
1750
+ it(`Given ${actual} should return expected value: ${JSON.stringify(expected)}`, () => {
1751
+ expect(sparkEncode.leverageManagement(...actual)).to.eql(expected);
1752
+ });
1753
+ });
1754
+ });
1755
+
1756
+ describe('leverageManagementOnPrice()', () => {
1757
+ const examples: Array<[[StrategyOrBundleIds, boolean, TriggerData, SubData],
1758
+ [
1759
+ strategyOrBundleId: number,
1760
+ isBundle: boolean,
1761
+ triggerData: { baseTokenAddr: EthereumAddress, quoteTokenAddr: EthereumAddress, price: number, ratioState: RatioState },
1762
+ subData: { collAsset: EthereumAddress, collAssetId: number, debtAsset: EthereumAddress, debtAssetId: number, marketAddr: EthereumAddress, targetRatio: number }
1763
+ ]]> = [
1764
+ [
1765
+ [
1766
+ Bundles.MainnetIds.SPARK_REPAY_ON_PRICE,
1767
+ true,
1768
+ ['0x000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb480000000000000000000000000000000000000000000000000000003a352944000000000000000000000000000000000000000000000000000000000000000001'],
1769
+ ['0x000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2',
1770
+ '0x0000000000000000000000000000000000000000000000000000000000000000',
1771
+ '0x000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48',
1772
+ '0x0000000000000000000000000000000000000000000000000000000000000001',
1773
+ '0x00000000000000000000000002c3ea4e34c0cbd694d2adfa2c690eecbc1793ee',
1774
+ '0x0000000000000000000000000000000000000000000000001bc16d674ec80000',
1775
+ '0x0000000000000000000000000000000000000000000000000000000000000000',
1776
+ ],
1777
+ ],
1778
+ [
1779
+ 59,
1780
+ true,
1781
+ {
1782
+ baseTokenAddr: web3Utils.toChecksumAddress(getAssetInfo('WETH', ChainId.Ethereum).address),
1783
+ quoteTokenAddr: web3Utils.toChecksumAddress(getAssetInfo('USDC', ChainId.Ethereum).address),
1784
+ price: 2500,
1785
+ ratioState: RatioState.UNDER,
1786
+ },
1787
+ {
1788
+ collAsset: web3Utils.toChecksumAddress(getAssetInfo('WETH', ChainId.Ethereum).address),
1789
+ collAssetId: 0,
1790
+ debtAsset: web3Utils.toChecksumAddress(getAssetInfo('USDC', ChainId.Ethereum).address),
1791
+ debtAssetId: 1,
1792
+ marketAddr: web3Utils.toChecksumAddress('0x02C3eA4e34C0cBd694D2adFa2c690EECbC1793eE'),
1793
+ targetRatio: 200,
1794
+ },
1795
+ ]
1796
+ ],
1797
+ [
1798
+ [
1799
+ Bundles.MainnetIds.SPARK_BOOST_ON_PRICE,
1800
+ true,
1801
+ ['0x0000000000000000000000002260fac5e5542a773aa44fbcfedf7c193bc2c5990000000000000000000000006b175474e89094c44da98b954eedeac495271d0f00000000000000000000000000000000000000000000000000000417bce6c8000000000000000000000000000000000000000000000000000000000000000000'],
1802
+ [
1803
+ '0x0000000000000000000000002260fac5e5542a773aa44fbcfedf7c193bc2c599',
1804
+ '0x0000000000000000000000000000000000000000000000000000000000000002',
1805
+ '0x0000000000000000000000006b175474e89094c44da98b954eedeac495271d0f',
1806
+ '0x0000000000000000000000000000000000000000000000000000000000000004',
1807
+ '0x00000000000000000000000002c3ea4e34c0cbd694d2adfa2c690eecbc1793ee',
1808
+ '0x00000000000000000000000000000000000000000000000022b1c8c1227a0000',
1809
+ '0x0000000000000000000000000000000000000000000000000000000000000000',
1810
+ ]
1811
+ ],
1812
+ [
1813
+ 60,
1814
+ true,
1815
+ {
1816
+ baseTokenAddr: web3Utils.toChecksumAddress(getAssetInfo('WBTC', ChainId.Ethereum).address),
1817
+ quoteTokenAddr: web3Utils.toChecksumAddress(getAssetInfo('DAI', ChainId.Ethereum).address),
1818
+ price: 45000,
1819
+ ratioState: RatioState.OVER,
1820
+ },
1821
+ {
1822
+ collAsset: web3Utils.toChecksumAddress(getAssetInfo('WBTC', ChainId.Ethereum).address),
1823
+ collAssetId: 2,
1824
+ debtAsset: web3Utils.toChecksumAddress(getAssetInfo('DAI', ChainId.Ethereum).address),
1825
+ debtAssetId: 4,
1826
+ marketAddr: web3Utils.toChecksumAddress('0x02C3eA4e34C0cBd694D2adFa2c690EECbC1793eE'),
1827
+ targetRatio: 250,
1828
+ },
1829
+ ]
1830
+ ]
1831
+ ];
1832
+
1833
+ examples.forEach(([expected, actual]) => {
1834
+ it(`Given ${actual} should return expected value: ${JSON.stringify(expected)}`, () => {
1835
+ expect(sparkEncode.leverageManagementOnPrice(...actual)).to.eql(expected);
1836
+ });
1837
+ });
1838
+ });
1839
+
1840
+ describe('closeOnPrice()', () => {
1841
+ const examples: Array<[
1842
+ [StrategyOrBundleIds, boolean, TriggerData, SubData],
1843
+ [
1844
+ strategyOrBundleId: number,
1845
+ collAsset: EthereumAddress,
1846
+ collAssetId: number,
1847
+ debtAsset: EthereumAddress,
1848
+ debtAssetId: number,
1849
+ marketAddr: EthereumAddress,
1850
+ user: EthereumAddress,
1851
+ stopLossPrice: number,
1852
+ stopLossType: CloseToAssetType,
1853
+ takeProfitPrice: number,
1854
+ takeProfitType: CloseToAssetType,
1855
+ ]
1856
+ ] > =
1857
+ [
1858
+ [
1859
+ [
1860
+ Bundles.MainnetIds.SPARK_CLOSE,
1861
+ true,
1862
+ ['0x0000000000000000000000002260fac5e5542a773aa44fbcfedf7c193bc2c5990000000000000000000000006b175474e89094c44da98b954eedeac495271d0f000000000000000000000000000000000000000000000000000003a35294400000000000000000000000000000000000000000000000000000000574fbde6000'],
1863
+ [
1864
+ '0x0000000000000000000000002260fac5e5542a773aa44fbcfedf7c193bc2c599',
1865
+ '0x0000000000000000000000000000000000000000000000000000000000000002',
1866
+ '0x0000000000000000000000006b175474e89094c44da98b954eedeac495271d0f',
1867
+ '0x0000000000000000000000000000000000000000000000000000000000000004',
1868
+ '0x0000000000000000000000000000000000000000000000000000000000000005',
1869
+ '0x00000000000000000000000002c3ea4e34c0cbd694d2adfa2c690eecbc1793ee',
1870
+ '0x0000000000000000000000001234567890123456789012345678901234567890',
1871
+ ]
1872
+ ],
1873
+ [
1874
+ 57,
1875
+ web3Utils.toChecksumAddress(getAssetInfo('WBTC', ChainId.Ethereum).address),
1876
+ 2,
1877
+ web3Utils.toChecksumAddress(getAssetInfo('DAI', ChainId.Ethereum).address),
1878
+ 4,
1879
+ web3Utils.toChecksumAddress('0x02C3eA4e34C0cBd694D2adFa2c690EECbC1793eE'),
1880
+ web3Utils.toChecksumAddress('0x1234567890123456789012345678901234567890'),
1881
+ 40000,
1882
+ CloseToAssetType.DEBT,
1883
+ 60000,
1884
+ CloseToAssetType.COLLATERAL,
1885
+ ]
1886
+ ],
1887
+ ]
1888
+
1889
+ examples.forEach(([expected, actual]) => {
1890
+ it(`Given ${actual} should return expected value: ${JSON.stringify(expected)}`, () => {
1891
+ expect(sparkEncode.closeOnPriceGeneric(...actual)).to.eql(expected);
1892
+ });
1893
+ });
1894
+ });
1895
+ });
1896
+ describe('When testing strategySubService.aaveV4Encode', () => {
1897
+ describe('leverageManagement()', () => {
1898
+ const examples: Array<[
1899
+ [StrategyOrBundleIds, boolean, TriggerData, SubData],
1900
+ [
1901
+ strategyOrBundleId: number, owner: EthereumAddress, spoke: EthereumAddress, ratioState: RatioState, targetRatio: number, triggerRatio: number
1902
+ ]
1903
+ ]> = [
1904
+ [
1905
+ [1,true,["0x0000000000000000000000001031d218133afab8c2b819b1366c7e434ad91e9c0000000000000000000000002f39d218133afab8f2b819b1066c7e434ad94e9e00000000000000000000000000000000000000000000000010a741a4627800000000000000000000000000000000000000000000000000000000000000000001"],["0x0000000000000000000000002f39d218133afab8f2b819b1066c7e434ad94e9e","0x0000000000000000000000001031d218133afab8c2b819b1366c7e434ad91e9c","0x0000000000000000000000000000000000000000000000000000000000000001","0x00000000000000000000000000000000000000000000000014d1120d7b160000","0x0000000000000000000000000000000000000000000000000000000000000000","0x0000000000000000000000000000000000000000000000000000000000000000"]],
1906
+ [
1907
+ 1,
1908
+ web3Utils.toChecksumAddress('0x1031d218133AFaB8c2B819B1366c7E434Ad91E9c'),
1909
+ web3Utils.toChecksumAddress('0x2f39d218133AFaB8F2B819B1066c7E434Ad94E9e'),
1910
+ RatioState.UNDER,
1911
+ 150,
1912
+ 120,
1913
+ ]
1914
+ ]
1915
+ ];
1916
+
1917
+ examples.forEach(([expected, actual]) => {
1918
+ it(`Given ${JSON.stringify(actual)} should return expected value: ${JSON.stringify(expected)}`, () => {
1919
+ expect(aaveV4Encode.leverageManagement(...actual)).to.eql(expected);
1920
+ });
1921
+ });
1922
+ });
1923
+
1924
+ describe('leverageManagementOnPrice()', () => {
1925
+ const examples: Array<[
1926
+ [StrategyOrBundleIds, boolean, TriggerData, SubData],
1927
+ [
1928
+ strategyOrBundleId: number, owner: EthereumAddress, spoke: EthereumAddress, collAsset: EthereumAddress, collAssetId: number, debtAsset: EthereumAddress, debtAssetId: number, targetRatio: number, price: string, priceState: RatioState, ratioState: RatioState
1929
+ ]
1930
+ ]> = [
1931
+ [
1932
+ [2,true,["0x0000000000000000000000002f39d218133afab8f2b819b1066c7e434ad94e9e000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000001400000000000000000000000000000000000000000000005150ae84a8cdf000000000000000000000000000000000000000000000000000000000000000000000"],["0x0000000000000000000000002f39d218133afab8f2b819b1066c7e434ad94e9e","0x0000000000000000000000001031d218133afab8c2b819b1366c7e434ad91e9c","0x000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2","0x000000000000000000000000000000000000000000000000000000000000000a","0x000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48","0x0000000000000000000000000000000000000000000000000000000000000014","0x0000000000000000000000000000000000000000000000000000000000000000","0x00000000000000000000000000000000000000000000000016345785d8a00000","0x0000000000000000000000000000000000000000000000000000000000000000","0x0000000000000000000000000000000000000000000000000000000000000000"]],
1933
+ [
1934
+ 2,
1935
+ web3Utils.toChecksumAddress('0x1031d218133AFaB8c2B819B1366c7E434Ad91E9c'),
1936
+ web3Utils.toChecksumAddress('0x2f39d218133AFaB8F2B819B1066c7E434Ad94E9e'),
1937
+ web3Utils.toChecksumAddress('0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2'),
1938
+ 10,
1939
+ web3Utils.toChecksumAddress('0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48'),
1940
+ 20,
1941
+ 160,
1942
+ '1500',
1943
+ RatioState.OVER, // priceState - goes to trigger
1944
+ RatioState.OVER, // ratioState - UNDER for repay, OVER for boost
1945
+ ]
1946
+ ]
1947
+ ];
1948
+
1949
+ examples.forEach(([expected, actual]) => {
1950
+ it(`Given ${JSON.stringify(actual)} should return expected value: ${JSON.stringify(expected)}`, () => {
1951
+ expect(aaveV4Encode.leverageManagementOnPrice(...actual)).to.eql(expected);
1952
+ });
1953
+ });
1954
+ });
1955
+
1956
+ describe('closeOnPrice()', () => {
1957
+ const examples: Array<[
1958
+ [StrategyOrBundleIds, boolean, TriggerData, SubData],
1959
+ [
1960
+ strategyOrBundleId: number, owner: EthereumAddress, spoke: EthereumAddress, collAsset: EthereumAddress, collAssetId: number, debtAsset: EthereumAddress, debtAssetId: number, stopLossPrice: string, stopLossType: CloseToAssetType, takeProfitPrice: string, takeProfitType: CloseToAssetType
1961
+ ]
1962
+ ]> = [
1963
+ [
1964
+ [3,true,["0x0000000000000000000000002f39d218133afab8f2b819b1066c7e434ad94e9e000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000001400000000000000000000000000000000000000000000004be4e7267b6ae000000000000000000000000000000000000000000000000000000000000000000000"],["0x0000000000000000000000002f39d218133afab8f2b819b1066c7e434ad94e9e","0x0000000000000000000000001031d218133afab8c2b819b1366c7e434ad91e9c","0x000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2","0x000000000000000000000000000000000000000000000000000000000000000a","0x000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48","0x0000000000000000000000000000000000000000000000000000000000000014","0x0000000000000000000000000000000000000000000000000000000000000003","0x0000000000000000000000000000000000000000000000000000000000000000","0x0000000000000000000000000000000000000000000000000000000000000000"]],
1965
+ [
1966
+ 3,
1967
+ web3Utils.toChecksumAddress('0x1031d218133AFaB8c2B819B1366c7E434Ad91E9c'),
1968
+ web3Utils.toChecksumAddress('0x2f39d218133AFaB8F2B819B1066c7E434Ad94E9e'),
1969
+ web3Utils.toChecksumAddress('0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2'),
1970
+ 10,
1971
+ web3Utils.toChecksumAddress('0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48'),
1972
+ 20,
1973
+ '1400',
1974
+ CloseToAssetType.DEBT,
1975
+ '0',
1976
+ CloseToAssetType.COLLATERAL
1977
+ ]
1978
+ ]
1979
+ ];
1980
+
1981
+ examples.forEach(([expected, actual]) => {
1982
+ it(`Given ${JSON.stringify(actual)} should return expected value: ${JSON.stringify(expected)}`, () => {
1983
+ expect(aaveV4Encode.closeOnPrice(...actual)).to.eql(expected);
1984
+ });
1985
+ });
1986
+ });
1987
+
1988
+ describe('collateralSwitch()', () => {
1989
+ const examples: Array<[
1990
+ [StrategyOrBundleIds, boolean, TriggerData, SubData],
1991
+ [
1992
+ strategyOrBundleId: number, owner: EthereumAddress, spoke: EthereumAddress, fromAsset: EthereumAddress, fromAssetId: number, toAsset: EthereumAddress, toAssetId: number, amountToSwitch: string, price: string, ratioState: RatioState
1993
+ ]
1994
+ ]> = [
1995
+ [
1996
+ [4,false,["0x0000000000000000000000002f39d218133afab8f2b819b1066c7e434ad94e9e000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000001400000000000000000000000000000000000000000000005150ae84a8cdf000000000000000000000000000000000000000000000000000000000000000000001"],["0x0000000000000000000000002f39d218133afab8f2b819b1066c7e434ad94e9e","0x0000000000000000000000001031d218133afab8c2b819b1366c7e434ad91e9c","0x000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2","0x000000000000000000000000000000000000000000000000000000000000000a","0x000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48","0x0000000000000000000000000000000000000000000000000000000000000014","0x0000000000000000000000000000000000000000000000000de0b6b3a7640000","0x0000000000000000000000000000000000000000000000000000000000000000","0x0000000000000000000000000000000000000000000000000000000000000000"]],
1997
+ [
1998
+ 4,
1999
+ web3Utils.toChecksumAddress('0x1031d218133AFaB8c2B819B1366c7E434Ad91E9c'),
2000
+ web3Utils.toChecksumAddress('0x2f39d218133AFaB8F2B819B1066c7E434Ad94E9e'),
2001
+ web3Utils.toChecksumAddress('0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2'),
2002
+ 10,
2003
+ web3Utils.toChecksumAddress('0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48'),
2004
+ 20,
2005
+ '1000000000000000000',
2006
+ '1500',
2007
+ RatioState.UNDER,
2008
+ ]
2009
+ ]
2010
+ ];
2011
+
2012
+ examples.forEach(([expected, actual]) => {
2013
+ it(`Given ${JSON.stringify(actual)} should return expected value: ${JSON.stringify(expected)}`, () => {
2014
+ expect(aaveV4Encode.collateralSwitch(...actual)).to.eql(expected);
2015
+ });
2016
+ });
2017
+ });
2018
+ });
1733
2019
  });