@defisaver/automation-sdk 3.3.11 → 3.3.12

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/cjs/automation/private/StrategiesAutomation.d.ts +2 -0
  2. package/cjs/automation/private/StrategiesAutomation.js +10 -1
  3. package/cjs/automation/private/StrategiesAutomation.test.js +25 -0
  4. package/cjs/constants/index.d.ts +1 -0
  5. package/cjs/constants/index.js +62 -1
  6. package/cjs/services/strategiesService.js +104 -0
  7. package/cjs/services/strategySubService.d.ts +6 -0
  8. package/cjs/services/strategySubService.js +30 -1
  9. package/cjs/services/strategySubService.test.js +96 -0
  10. package/cjs/services/subDataService.d.ts +295 -137
  11. package/cjs/services/subDataService.js +639 -393
  12. package/cjs/services/subDataService.test.js +157 -0
  13. package/cjs/services/triggerService.d.ts +29 -0
  14. package/cjs/services/triggerService.js +53 -1
  15. package/cjs/services/triggerService.test.js +84 -0
  16. package/cjs/services/utils.d.ts +1 -1
  17. package/cjs/services/utils.js +10 -2
  18. package/cjs/types/enums.d.ts +17 -3
  19. package/cjs/types/enums.js +14 -0
  20. package/cjs/types/index.d.ts +22 -1
  21. package/esm/automation/private/StrategiesAutomation.d.ts +2 -0
  22. package/esm/automation/private/StrategiesAutomation.js +10 -1
  23. package/esm/automation/private/StrategiesAutomation.test.js +25 -0
  24. package/esm/constants/index.d.ts +1 -0
  25. package/esm/constants/index.js +61 -0
  26. package/esm/services/strategiesService.js +105 -1
  27. package/esm/services/strategySubService.d.ts +6 -0
  28. package/esm/services/strategySubService.js +29 -0
  29. package/esm/services/strategySubService.test.js +97 -1
  30. package/esm/services/subDataService.d.ts +295 -137
  31. package/esm/services/subDataService.js +639 -393
  32. package/esm/services/subDataService.test.js +157 -0
  33. package/esm/services/triggerService.d.ts +29 -0
  34. package/esm/services/triggerService.js +52 -0
  35. package/esm/services/triggerService.test.js +85 -1
  36. package/esm/services/utils.d.ts +1 -1
  37. package/esm/services/utils.js +10 -2
  38. package/esm/types/enums.d.ts +17 -3
  39. package/esm/types/enums.js +14 -0
  40. package/esm/types/index.d.ts +22 -1
  41. package/package.json +2 -2
  42. package/src/automation/private/StrategiesAutomation.test.ts +40 -0
  43. package/src/automation/private/StrategiesAutomation.ts +11 -0
  44. package/src/constants/index.ts +62 -0
  45. package/src/services/strategiesService.ts +119 -1
  46. package/src/services/strategySubService.test.ts +124 -0
  47. package/src/services/strategySubService.ts +118 -0
  48. package/src/services/subDataService.test.ts +172 -0
  49. package/src/services/subDataService.ts +1031 -782
  50. package/src/services/triggerService.test.ts +97 -0
  51. package/src/services/triggerService.ts +74 -1
  52. package/src/services/utils.ts +15 -4
  53. package/src/types/enums.ts +14 -0
  54. package/src/types/index.ts +26 -0
@@ -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 = {
@@ -506,6 +518,56 @@ export const MAINNET_BUNDLES_INFO: MainnetBundleInfo = {
506
518
  strategyId: Strategies.Identifiers.BoostOnPrice,
507
519
  protocol: PROTOCOLS.Spark,
508
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
+ },
509
571
  };
510
572
 
511
573
  export const OPTIMISM_BUNDLES_INFO: OptimismBundleInfo = {
@@ -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
 
@@ -1235,6 +1339,20 @@ const parsingMethodsMapping: StrategiesToProtocolVersionMapping = {
1235
1339
  [Strategies.Identifiers.EoaCloseOnPrice]: parseAaveV3CloseOnPrice,
1236
1340
  [Strategies.Identifiers.CollateralSwitch]: parseAaveV3CollateralSwitch,
1237
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
+ },
1238
1356
  [ProtocolIdentifiers.StrategiesAutomation.CompoundV2]: {
1239
1357
  [Strategies.Identifiers.Repay]: parseCompoundV2LeverageManagement,
1240
1358
  [Strategies.Identifiers.Boost]: parseCompoundV2LeverageManagement,
@@ -22,6 +22,7 @@ import {
22
22
  compoundV3L2Encode,
23
23
  morphoBlueEncode,
24
24
  sparkEncode,
25
+ aaveV4Encode,
25
26
  } from './strategySubService';
26
27
 
27
28
  describe('Feature: strategySubService.ts', () => {
@@ -1892,4 +1893,127 @@ describe('Feature: strategySubService.ts', () => {
1892
1893
  });
1893
1894
  });
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
+ });
1895
2019
  });
@@ -846,3 +846,121 @@ export const fluidEncode = {
846
846
  return [strategyOrBundleId, isBundle, triggerData, subData];
847
847
  },
848
848
  };
849
+
850
+ export const aaveV4Encode = {
851
+ leverageManagement(
852
+ strategyOrBundleId: number,
853
+ owner: EthereumAddress,
854
+ spoke: EthereumAddress,
855
+ ratioState: RatioState,
856
+ targetRatio: number,
857
+ triggerRatio: number,
858
+ ) {
859
+ const isBundle = true;
860
+ const subData = subDataService.aaveV4LeverageManagementSubData.encode(spoke, owner, ratioState, targetRatio);
861
+ const triggerData = triggerService.aaveV4RatioTrigger.encode(owner, spoke, triggerRatio, ratioState);
862
+
863
+ return [strategyOrBundleId, isBundle, triggerData, subData];
864
+ },
865
+ leverageManagementOnPrice(
866
+ strategyOrBundleId: number,
867
+ owner: EthereumAddress,
868
+ spoke: EthereumAddress,
869
+ collAsset: EthereumAddress,
870
+ collAssetId: number,
871
+ debtAsset: EthereumAddress,
872
+ debtAssetId: number,
873
+ targetRatio: number,
874
+ price: string,
875
+ priceState: RatioState,
876
+ ratioState: RatioState, // UNDER for repay, OVER for boost
877
+ ) {
878
+ const isBundle = true;
879
+ const subData = subDataService.aaveV4LeverageManagementOnPriceSubData.encode(
880
+ spoke,
881
+ owner,
882
+ collAsset,
883
+ collAssetId,
884
+ debtAsset,
885
+ debtAssetId,
886
+ ratioState,
887
+ targetRatio,
888
+ );
889
+ const triggerData = triggerService.aaveV4QuotePriceTrigger.encode(
890
+ spoke,
891
+ collAssetId,
892
+ debtAssetId,
893
+ price,
894
+ priceState,
895
+ );
896
+
897
+ return [strategyOrBundleId, isBundle, triggerData, subData];
898
+ },
899
+ closeOnPrice(
900
+ strategyOrBundleId: number,
901
+ owner: EthereumAddress,
902
+ spoke: EthereumAddress,
903
+ collAsset: EthereumAddress,
904
+ collAssetId: number,
905
+ debtAsset: EthereumAddress,
906
+ debtAssetId: number,
907
+ stopLossPrice: string = '0',
908
+ stopLossType: CloseToAssetType = CloseToAssetType.DEBT,
909
+ takeProfitPrice: string = '0',
910
+ takeProfitType: CloseToAssetType = CloseToAssetType.COLLATERAL,
911
+ ) {
912
+ const isBundle = true;
913
+ const closeType = getCloseStrategyType(stopLossPrice, stopLossType, takeProfitPrice, takeProfitType);
914
+
915
+ const subData = subDataService.aaveV4CloseSubData.encode(
916
+ spoke,
917
+ owner,
918
+ collAsset,
919
+ collAssetId,
920
+ debtAsset,
921
+ debtAssetId,
922
+ closeType,
923
+ );
924
+ const triggerData = triggerService.aaveV4QuotePriceRangeTrigger.encode(
925
+ spoke,
926
+ collAssetId,
927
+ debtAssetId,
928
+ stopLossPrice,
929
+ takeProfitPrice,
930
+ );
931
+
932
+ return [strategyOrBundleId, isBundle, triggerData, subData];
933
+ },
934
+ collateralSwitch(
935
+ strategyOrBundleId: number,
936
+ owner: EthereumAddress,
937
+ spoke: EthereumAddress,
938
+ fromAsset: EthereumAddress,
939
+ fromAssetId: number,
940
+ toAsset: EthereumAddress,
941
+ toAssetId: number,
942
+ amountToSwitch: string,
943
+ price: string,
944
+ ratioState: RatioState,
945
+ ) {
946
+ const isBundle = false;
947
+ const subData = subDataService.aaveV4CollateralSwitchSubData.encode(
948
+ spoke,
949
+ owner,
950
+ fromAsset,
951
+ fromAssetId,
952
+ toAsset,
953
+ toAssetId,
954
+ amountToSwitch,
955
+ );
956
+ const triggerData = triggerService.aaveV4QuotePriceTrigger.encode(
957
+ spoke,
958
+ fromAssetId, // baseTokenId
959
+ toAssetId, // quoteTokenId
960
+ price,
961
+ ratioState,
962
+ );
963
+
964
+ return [strategyOrBundleId, isBundle, triggerData, subData];
965
+ },
966
+ };
@@ -2330,4 +2330,176 @@ describe('Feature: subDataService.ts', () => {
2330
2330
  });
2331
2331
  });
2332
2332
  });
2333
+ describe('When testing subDataService.aaveV4LeverageManagementSubData', () => {
2334
+ describe('encode()', () => {
2335
+ const examples: Array<[SubData, [spoke: EthereumAddress, owner: EthereumAddress, ratioState: RatioState, targetRatio: number]]> = [
2336
+ [
2337
+ ["0x0000000000000000000000002f39d218133afab8f2b819b1066c7e434ad94e9e","0x0000000000000000000000001031d218133afab8c2b819b1366c7e434ad91e9c","0x0000000000000000000000000000000000000000000000000000000000000001","0x00000000000000000000000000000000000000000000000014d1120d7b160000","0x0000000000000000000000000000000000000000000000000000000000000000","0x0000000000000000000000000000000000000000000000000000000000000000"],
2338
+ [web3Utils.toChecksumAddress('0x2f39d218133AFaB8F2B819B1066c7E434Ad94E9e'), web3Utils.toChecksumAddress('0x1031d218133AFaB8c2B819B1366c7E434Ad91E9c'), RatioState.UNDER, 150]
2339
+ ],
2340
+ ];
2341
+
2342
+ examples.forEach(([expected, actual]) => {
2343
+ it(`Given ${actual} should return expected value: ${expected}`, () => {
2344
+ expect(subDataService.aaveV4LeverageManagementSubData.encode(...actual)).to.eql(expected);
2345
+ });
2346
+ });
2347
+ });
2348
+
2349
+ describe('decode()', () => {
2350
+ const examples: Array<[{ spoke: EthereumAddress, owner: EthereumAddress, ratioState: RatioState, targetRatio: number }, SubData]> = [
2351
+ [
2352
+ {
2353
+ spoke: web3Utils.toChecksumAddress('0x2f39d218133AFaB8F2B819B1066c7E434Ad94E9e'),
2354
+ owner: web3Utils.toChecksumAddress('0x1031d218133AFaB8c2B819B1366c7E434Ad91E9c'),
2355
+ ratioState: RatioState.UNDER,
2356
+ targetRatio: 150,
2357
+ },
2358
+ ["0x0000000000000000000000002f39d218133afab8f2b819b1066c7e434ad94e9e","0x0000000000000000000000001031d218133afab8c2b819b1366c7e434ad91e9c","0x0000000000000000000000000000000000000000000000000000000000000001","0x00000000000000000000000000000000000000000000000014d1120d7b160000","0x0000000000000000000000000000000000000000000000000000000000000000","0x0000000000000000000000000000000000000000000000000000000000000000"],
2359
+ ],
2360
+ ];
2361
+
2362
+ examples.forEach(([expected, actual]) => {
2363
+ it(`Given ${actual} should return expected value: ${JSON.stringify(expected)}`, () => {
2364
+ expect(subDataService.aaveV4LeverageManagementSubData.decode(actual)).to.eql(expected);
2365
+ });
2366
+ });
2367
+ });
2368
+ });
2369
+
2370
+ describe('When testing subDataService.aaveV4LeverageManagementOnPriceSubData', () => {
2371
+ describe('encode()', () => {
2372
+ const examples: Array<[SubData, [spoke: EthereumAddress, owner: EthereumAddress, collAsset: EthereumAddress, collAssetId: number, debtAsset: EthereumAddress, debtAssetId: number, ratioState: RatioState, targetRatio: number]]> = [
2373
+ [
2374
+ ["0x0000000000000000000000002f39d218133afab8f2b819b1066c7e434ad94e9e","0x0000000000000000000000001031d218133afab8c2b819b1366c7e434ad91e9c","0x000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2","0x000000000000000000000000000000000000000000000000000000000000000a","0x000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48","0x0000000000000000000000000000000000000000000000000000000000000014","0x0000000000000000000000000000000000000000000000000000000000000000","0x00000000000000000000000000000000000000000000000016345785d8a00000","0x0000000000000000000000000000000000000000000000000000000000000000","0x0000000000000000000000000000000000000000000000000000000000000000"],
2375
+ [
2376
+ web3Utils.toChecksumAddress('0x2f39d218133AFaB8F2B819B1066c7E434Ad94E9e'), web3Utils.toChecksumAddress('0x1031d218133AFaB8c2B819B1366c7E434Ad91E9c'),
2377
+ web3Utils.toChecksumAddress('0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2'), 10,
2378
+ web3Utils.toChecksumAddress('0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48'), 20,
2379
+ RatioState.OVER, 160
2380
+ ]
2381
+ ],
2382
+ ];
2383
+
2384
+ examples.forEach(([expected, actual]) => {
2385
+ it(`Given ${actual} should return expected value: ${expected}`, () => {
2386
+ expect(subDataService.aaveV4LeverageManagementOnPriceSubData.encode(...actual)).to.eql(expected);
2387
+ });
2388
+ });
2389
+ });
2390
+
2391
+ describe('decode()', () => {
2392
+ const examples: Array<[{ spoke: EthereumAddress, owner: EthereumAddress, collAsset: EthereumAddress, collAssetId: number, debtAsset: EthereumAddress, debtAssetId: number, ratioState: RatioState, targetRatio: number }, SubData]> = [
2393
+ [
2394
+ {
2395
+ spoke: web3Utils.toChecksumAddress('0x2f39d218133AFaB8F2B819B1066c7E434Ad94E9e'),
2396
+ owner: web3Utils.toChecksumAddress('0x1031d218133AFaB8c2B819B1366c7E434Ad91E9c'),
2397
+ collAsset: web3Utils.toChecksumAddress('0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2'),
2398
+ collAssetId: 10,
2399
+ debtAsset: web3Utils.toChecksumAddress('0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48'),
2400
+ debtAssetId: 20,
2401
+ ratioState: RatioState.OVER,
2402
+ targetRatio: 160,
2403
+ },
2404
+ ["0x0000000000000000000000002f39d218133afab8f2b819b1066c7e434ad94e9e","0x0000000000000000000000001031d218133afab8c2b819b1366c7e434ad91e9c","0x000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2","0x000000000000000000000000000000000000000000000000000000000000000a","0x000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48","0x0000000000000000000000000000000000000000000000000000000000000014","0x0000000000000000000000000000000000000000000000000000000000000000","0x00000000000000000000000000000000000000000000000016345785d8a00000","0x0000000000000000000000000000000000000000000000000000000000000000","0x0000000000000000000000000000000000000000000000000000000000000000"],
2405
+ ],
2406
+ ];
2407
+
2408
+ examples.forEach(([expected, actual]) => {
2409
+ it(`Given ${actual} should return expected value: ${JSON.stringify(expected)}`, () => {
2410
+ expect(subDataService.aaveV4LeverageManagementOnPriceSubData.decode(actual)).to.eql(expected);
2411
+ });
2412
+ });
2413
+ });
2414
+ });
2415
+
2416
+ describe('When testing subDataService.aaveV4CloseSubData', () => {
2417
+ describe('encode()', () => {
2418
+ const examples: Array<[SubData, [spoke: EthereumAddress, owner: EthereumAddress, collAsset: EthereumAddress, collAssetId: number, debtAsset: EthereumAddress, debtAssetId: number, closeType: CloseStrategyType]]> = [
2419
+ [
2420
+ ["0x0000000000000000000000002f39d218133afab8f2b819b1066c7e434ad94e9e","0x0000000000000000000000001031d218133afab8c2b819b1366c7e434ad91e9c","0x000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2","0x000000000000000000000000000000000000000000000000000000000000000a","0x000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48","0x0000000000000000000000000000000000000000000000000000000000000014","0x0000000000000000000000000000000000000000000000000000000000000001","0x0000000000000000000000000000000000000000000000000000000000000000","0x0000000000000000000000000000000000000000000000000000000000000000"],
2421
+ [
2422
+ web3Utils.toChecksumAddress('0x2f39d218133AFaB8F2B819B1066c7E434Ad94E9e'), web3Utils.toChecksumAddress('0x1031d218133AFaB8c2B819B1366c7E434Ad91E9c'),
2423
+ web3Utils.toChecksumAddress('0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2'), 10,
2424
+ web3Utils.toChecksumAddress('0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48'), 20,
2425
+ CloseStrategyType.STOP_LOSS_IN_COLLATERAL
2426
+ ]
2427
+ ],
2428
+ ];
2429
+
2430
+ examples.forEach(([expected, actual]) => {
2431
+ it(`Given ${actual} should return expected value: ${expected}`, () => {
2432
+ expect(subDataService.aaveV4CloseSubData.encode(...actual)).to.eql(expected);
2433
+ });
2434
+ });
2435
+ });
2436
+
2437
+ describe('decode()', () => {
2438
+ const examples: Array<[{ spoke: EthereumAddress, owner: EthereumAddress, collAsset: EthereumAddress, collAssetId: number, debtAsset: EthereumAddress, debtAssetId: number, closeType: CloseStrategyType }, SubData]> = [
2439
+ [
2440
+ {
2441
+ spoke: web3Utils.toChecksumAddress('0x2f39d218133AFaB8F2B819B1066c7E434Ad94E9e'),
2442
+ owner: web3Utils.toChecksumAddress('0x1031d218133AFaB8c2B819B1366c7E434Ad91E9c'),
2443
+ collAsset: web3Utils.toChecksumAddress('0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2'),
2444
+ collAssetId: 10,
2445
+ debtAsset: web3Utils.toChecksumAddress('0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48'),
2446
+ debtAssetId: 20,
2447
+ closeType: CloseStrategyType.STOP_LOSS_IN_COLLATERAL,
2448
+ },
2449
+ ["0x0000000000000000000000002f39d218133afab8f2b819b1066c7e434ad94e9e","0x0000000000000000000000001031d218133afab8c2b819b1366c7e434ad91e9c","0x000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2","0x000000000000000000000000000000000000000000000000000000000000000a","0x000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48","0x0000000000000000000000000000000000000000000000000000000000000014","0x0000000000000000000000000000000000000000000000000000000000000001","0x0000000000000000000000000000000000000000000000000000000000000000","0x0000000000000000000000000000000000000000000000000000000000000000"],
2450
+ ],
2451
+ ];
2452
+
2453
+ examples.forEach(([expected, actual]) => {
2454
+ it(`Given ${actual} should return expected value: ${JSON.stringify(expected)}`, () => {
2455
+ expect(subDataService.aaveV4CloseSubData.decode(actual)).to.eql(expected);
2456
+ });
2457
+ });
2458
+ });
2459
+ });
2460
+
2461
+ describe('When testing subDataService.aaveV4CollateralSwitchSubData', () => {
2462
+ describe('encode()', () => {
2463
+ const examples: Array<[SubData, [spoke: EthereumAddress, owner: EthereumAddress, fromAsset: EthereumAddress, fromAssetId: number, toAsset: EthereumAddress, toAssetId: number, amountToSwitch: string]]> = [
2464
+ [
2465
+ ["0x0000000000000000000000002f39d218133afab8f2b819b1066c7e434ad94e9e","0x0000000000000000000000001031d218133afab8c2b819b1366c7e434ad91e9c","0x000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2","0x000000000000000000000000000000000000000000000000000000000000000a","0x000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48","0x0000000000000000000000000000000000000000000000000000000000000014","0x0000000000000000000000000000000000000000000000000de0b6b3a7640000","0x0000000000000000000000000000000000000000000000000000000000000000","0x0000000000000000000000000000000000000000000000000000000000000000"],
2466
+ [
2467
+ web3Utils.toChecksumAddress('0x2f39d218133AFaB8F2B819B1066c7E434Ad94E9e'), web3Utils.toChecksumAddress('0x1031d218133AFaB8c2B819B1366c7E434Ad91E9c'),
2468
+ web3Utils.toChecksumAddress('0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2'), 10,
2469
+ web3Utils.toChecksumAddress('0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48'), 20,
2470
+ '1000000000000000000'
2471
+ ]
2472
+ ],
2473
+ ];
2474
+
2475
+ examples.forEach(([expected, actual]) => {
2476
+ it(`Given ${actual} should return expected value: ${expected}`, () => {
2477
+ expect(subDataService.aaveV4CollateralSwitchSubData.encode(...actual)).to.eql(expected);
2478
+ });
2479
+ });
2480
+ });
2481
+
2482
+ describe('decode()', () => {
2483
+ const examples: Array<[{ spoke: EthereumAddress, owner: EthereumAddress, fromAsset: EthereumAddress, fromAssetId: number, toAsset: EthereumAddress, toAssetId: number, amountToSwitch: string }, SubData]> = [
2484
+ [
2485
+ {
2486
+ spoke: web3Utils.toChecksumAddress('0x2f39d218133AFaB8F2B819B1066c7E434Ad94E9e'),
2487
+ owner: web3Utils.toChecksumAddress('0x1031d218133AFaB8c2B819B1366c7E434Ad91E9c'),
2488
+ fromAsset: web3Utils.toChecksumAddress('0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2'),
2489
+ fromAssetId: 10,
2490
+ toAsset: web3Utils.toChecksumAddress('0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48'),
2491
+ toAssetId: 20,
2492
+ amountToSwitch: '1000000000000000000'
2493
+ },
2494
+ ["0x0000000000000000000000002f39d218133afab8f2b819b1066c7e434ad94e9e","0x0000000000000000000000001031d218133afab8c2b819b1366c7e434ad91e9c","0x000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2","0x000000000000000000000000000000000000000000000000000000000000000a","0x000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48","0x0000000000000000000000000000000000000000000000000000000000000014","0x0000000000000000000000000000000000000000000000000de0b6b3a7640000","0x0000000000000000000000000000000000000000000000000000000000000000","0x0000000000000000000000000000000000000000000000000000000000000000"],
2495
+ ],
2496
+ ];
2497
+
2498
+ examples.forEach(([expected, actual]) => {
2499
+ it(`Given ${actual} should return expected value: ${JSON.stringify(expected)}`, () => {
2500
+ expect(subDataService.aaveV4CollateralSwitchSubData.decode(actual)).to.eql(expected);
2501
+ });
2502
+ });
2503
+ });
2504
+ });
2333
2505
  });