@defisaver/automation-sdk 3.3.5 → 3.3.7-dev.0

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 (39) hide show
  1. package/cjs/constants/index.js +5 -10
  2. package/cjs/services/strategiesService.js +11 -14
  3. package/cjs/services/strategySubService.d.ts +1 -11
  4. package/cjs/services/strategySubService.js +5 -5
  5. package/cjs/services/strategySubService.test.js +0 -72
  6. package/cjs/services/subDataService.d.ts +12 -9
  7. package/cjs/services/subDataService.js +33 -20
  8. package/cjs/services/subDataService.test.js +0 -72
  9. package/cjs/services/triggerService.d.ts +9 -9
  10. package/cjs/services/triggerService.js +20 -19
  11. package/cjs/services/triggerService.test.js +0 -46
  12. package/cjs/services/utils.test.js +0 -77
  13. package/cjs/types/enums.d.ts +2 -3
  14. package/cjs/types/enums.js +1 -2
  15. package/esm/constants/index.js +5 -10
  16. package/esm/services/strategiesService.js +11 -14
  17. package/esm/services/strategySubService.d.ts +1 -11
  18. package/esm/services/strategySubService.js +5 -5
  19. package/esm/services/strategySubService.test.js +1 -73
  20. package/esm/services/subDataService.d.ts +12 -9
  21. package/esm/services/subDataService.js +32 -19
  22. package/esm/services/subDataService.test.js +0 -72
  23. package/esm/services/triggerService.d.ts +9 -9
  24. package/esm/services/triggerService.js +19 -18
  25. package/esm/services/triggerService.test.js +1 -47
  26. package/esm/services/utils.test.js +1 -52
  27. package/esm/types/enums.d.ts +2 -3
  28. package/esm/types/enums.js +1 -2
  29. package/package.json +1 -1
  30. package/src/constants/index.ts +5 -12
  31. package/src/services/strategiesService.ts +12 -22
  32. package/src/services/strategySubService.test.ts +4 -94
  33. package/src/services/strategySubService.ts +15 -16
  34. package/src/services/subDataService.test.ts +1 -78
  35. package/src/services/subDataService.ts +52 -31
  36. package/src/services/triggerService.test.ts +0 -51
  37. package/src/services/triggerService.ts +29 -26
  38. package/src/services/utils.test.ts +0 -59
  39. package/src/types/enums.ts +1 -2
@@ -194,8 +194,6 @@ var Bundles;
194
194
  MainnetIds[MainnetIds["LIQUITY_BOOST"] = 17] = "LIQUITY_BOOST";
195
195
  MainnetIds[MainnetIds["SPARK_REPAY"] = 18] = "SPARK_REPAY";
196
196
  MainnetIds[MainnetIds["SPARK_BOOST"] = 19] = "SPARK_BOOST";
197
- MainnetIds[MainnetIds["SPARK_CLOSE_TO_DEBT"] = -21231230] = "SPARK_CLOSE_TO_DEBT";
198
- MainnetIds[MainnetIds["SPARK_CLOSE_TO_COLLATERAL"] = -21231231] = "SPARK_CLOSE_TO_COLLATERAL";
199
197
  MainnetIds[MainnetIds["AAVE_V2_REPAY"] = 22] = "AAVE_V2_REPAY";
200
198
  MainnetIds[MainnetIds["AAVE_V2_BOOST"] = 23] = "AAVE_V2_BOOST";
201
199
  MainnetIds[MainnetIds["COMP_V2_REPAY"] = 20] = "COMP_V2_REPAY";
@@ -231,6 +229,7 @@ var Bundles;
231
229
  MainnetIds[MainnetIds["AAVE_V3_EOA_REPAY_ON_PRICE"] = 54] = "AAVE_V3_EOA_REPAY_ON_PRICE";
232
230
  MainnetIds[MainnetIds["AAVE_V3_EOA_BOOST_ON_PRICE"] = 55] = "AAVE_V3_EOA_BOOST_ON_PRICE";
233
231
  MainnetIds[MainnetIds["AAVE_V3_EOA_CLOSE"] = 56] = "AAVE_V3_EOA_CLOSE";
232
+ MainnetIds[MainnetIds["SPARK_CLOSE"] = 57] = "SPARK_CLOSE";
234
233
  })(MainnetIds = Bundles.MainnetIds || (Bundles.MainnetIds = {}));
235
234
  let OptimismIds;
236
235
  (function (OptimismIds) {
@@ -302,16 +302,6 @@ export const MAINNET_BUNDLES_INFO = {
302
302
  strategyId: Strategies.Identifiers.Boost,
303
303
  protocol: PROTOCOLS.Spark,
304
304
  },
305
- [Bundles.MainnetIds.SPARK_CLOSE_TO_DEBT]: {
306
- strategyOrBundleId: Bundles.MainnetIds.SPARK_CLOSE_TO_DEBT,
307
- strategyId: Strategies.Identifiers.CloseToDebt,
308
- protocol: PROTOCOLS.Spark,
309
- },
310
- [Bundles.MainnetIds.SPARK_CLOSE_TO_COLLATERAL]: {
311
- strategyOrBundleId: Bundles.MainnetIds.SPARK_CLOSE_TO_COLLATERAL,
312
- strategyId: Strategies.Identifiers.CloseToCollateral,
313
- protocol: PROTOCOLS.Spark,
314
- },
315
305
  [Bundles.MainnetIds.AAVE_V2_REPAY]: {
316
306
  strategyOrBundleId: Bundles.MainnetIds.AAVE_V2_REPAY,
317
307
  strategyId: Strategies.Identifiers.Repay,
@@ -467,6 +457,11 @@ export const MAINNET_BUNDLES_INFO = {
467
457
  strategyId: Strategies.Identifiers.EoaCloseOnPrice,
468
458
  protocol: PROTOCOLS.AaveV3,
469
459
  },
460
+ [Bundles.MainnetIds.SPARK_CLOSE]: {
461
+ strategyOrBundleId: Bundles.MainnetIds.SPARK_CLOSE,
462
+ strategyId: Strategies.Identifiers.CloseOnPrice,
463
+ protocol: PROTOCOLS.Spark,
464
+ },
470
465
  };
471
466
  export const OPTIMISM_BUNDLES_INFO = {
472
467
  [Bundles.OptimismIds.AAVE_V3_REPAY]: {
@@ -7,9 +7,6 @@ import { getPositionId, getRatioStateInfoForAaveCloseStrategy, getStopLossAndTak
7
7
  import * as subDataService from './subDataService';
8
8
  import * as triggerService from './triggerService';
9
9
  const web3 = new Web3();
10
- const SPARK_MARKET_ADDRESSES = {
11
- [ChainId.Ethereum]: '0x02C3eA4e34C0cBd694D2adFa2c690EECbC1793eE',
12
- };
13
10
  const AAVE_V3_MARKET_ADDRESSES = {
14
11
  [ChainId.Ethereum]: '0x2f39d218133AFaB8F2B819B1066c7E434Ad94E9e',
15
12
  [ChainId.Optimism]: '0xa97684ead0e402dC232d5A977953DF7ECBaB3CDb',
@@ -590,23 +587,24 @@ function parseSparkLeverageManagement(position, parseData) {
590
587
  function parseSparkCloseOnPrice(position, parseData) {
591
588
  const _position = cloneDeep(position);
592
589
  const { subStruct } = parseData.subscriptionEventData;
593
- const triggerData = triggerService.sparkQuotePriceTrigger.decode(subStruct.triggerData);
594
- const subData = subDataService.sparkQuotePriceSubData.decode(subStruct.subData);
590
+ const triggerData = triggerService.sparkQuotePriceRangeTrigger.decode(subStruct.triggerData);
591
+ const subData = subDataService.sparkCloseGenericSubData.decode(subStruct.subData);
595
592
  _position.strategyData.decoded.triggerData = triggerData;
596
593
  _position.strategyData.decoded.subData = subData;
597
- _position.positionId = getPositionId(_position.chainId, _position.protocol.id, _position.owner, SPARK_MARKET_ADDRESSES[_position.chainId]);
594
+ _position.positionId = getPositionId(_position.chainId, _position.protocol.id, _position.owner, subData.marketAddr);
595
+ const { takeProfitType, stopLossType } = getStopLossAndTakeProfitTypeByCloseStrategyType(+subData.closeType);
598
596
  _position.specific = {
599
597
  collAsset: subData.collAsset,
600
598
  collAssetId: subData.collAssetId,
601
599
  debtAsset: subData.debtAsset,
602
600
  debtAssetId: subData.debtAssetId,
603
- baseToken: triggerData.baseTokenAddress,
604
- quoteToken: triggerData.quoteTokenAddress,
605
- price: triggerData.price,
606
- ratioState: triggerData.ratioState,
601
+ baseToken: triggerData.collToken,
602
+ quoteToken: triggerData.debtToken,
603
+ stopLossPrice: triggerData.lowerPrice,
604
+ takeProfitPrice: triggerData.upperPrice,
605
+ stopLossType,
606
+ takeProfitType,
607
607
  };
608
- const { ratioState } = getRatioStateInfoForAaveCloseStrategy(_position.specific.ratioState, wethToEthByAddress(_position.specific.collAsset, parseData.chainId), wethToEthByAddress(_position.specific.debtAsset, parseData.chainId), parseData.chainId);
609
- _position.strategy.strategyId = isRatioStateOver(ratioState) ? Strategies.IdOverrides.TakeProfit : Strategies.IdOverrides.StopLoss;
610
608
  return _position;
611
609
  }
612
610
  function parseLiquitySavingsLiqProtection(position, parseData) {
@@ -917,8 +915,7 @@ const parsingMethodsMapping = {
917
915
  [ProtocolIdentifiers.StrategiesAutomation.Spark]: {
918
916
  [Strategies.Identifiers.Repay]: parseSparkLeverageManagement,
919
917
  [Strategies.Identifiers.Boost]: parseSparkLeverageManagement,
920
- [Strategies.Identifiers.CloseToDebt]: parseSparkCloseOnPrice,
921
- [Strategies.Identifiers.CloseToCollateral]: parseSparkCloseOnPrice,
918
+ [Strategies.Identifiers.CloseOnPrice]: parseSparkCloseOnPrice,
922
919
  },
923
920
  [ProtocolIdentifiers.StrategiesAutomation.CrvUSD]: {
924
921
  [Strategies.Identifiers.Repay]: parseCrvUSDLeverageManagement,
@@ -85,17 +85,7 @@ export declare const exchangeEncode: {
85
85
  };
86
86
  export declare const sparkEncode: {
87
87
  leverageManagement(triggerRepayRatio: number, triggerBoostRatio: number, targetBoostRatio: number, targetRepayRatio: number, boostEnabled: boolean): string;
88
- closeToAsset(strategyOrBundleId: number, isBundle: boolean | undefined, triggerData: {
89
- baseTokenAddress: EthereumAddress;
90
- quoteTokenAddress: EthereumAddress;
91
- price: number;
92
- ratioState: RatioState;
93
- }, subData: {
94
- collAsset: EthereumAddress;
95
- collAssetId: number;
96
- debtAsset: EthereumAddress;
97
- debtAssetId: number;
98
- }): (number | boolean | string[])[];
88
+ closeOnPriceGeneric(strategyOrBundleId: number, collAsset: EthereumAddress, collAssetId: number, debtAsset: EthereumAddress, debtAssetId: number, marketAddr: EthereumAddress, user: EthereumAddress, stopLossPrice?: number, stopLossType?: CloseToAssetType, takeProfitPrice?: number, takeProfitType?: CloseToAssetType): (number | boolean | string[])[];
99
89
  };
100
90
  export declare const crvUSDEncode: {
101
91
  leverageManagement(owner: EthereumAddress, controllerAddr: EthereumAddress, ratioState: RatioState, targetRatio: number, triggerRatio: number, collTokenAddr: EthereumAddress, crvUSDAddr: EthereumAddress): (boolean | string[] | Bundles.MainnetIds)[];
@@ -232,11 +232,11 @@ export const sparkEncode = {
232
232
  subInput = subInput.concat(boostEnabled ? '01' : '00');
233
233
  return subInput;
234
234
  },
235
- closeToAsset(strategyOrBundleId, isBundle = true, triggerData, subData) {
236
- const { collAsset, collAssetId, debtAsset, debtAssetId, } = subData;
237
- const subDataEncoded = subDataService.sparkQuotePriceSubData.encode(collAsset, collAssetId, debtAsset, debtAssetId);
238
- const { baseTokenAddress, quoteTokenAddress, price, ratioState, } = triggerData;
239
- const triggerDataEncoded = triggerService.sparkQuotePriceTrigger.encode(baseTokenAddress, quoteTokenAddress, price, ratioState);
235
+ closeOnPriceGeneric(strategyOrBundleId, collAsset, collAssetId, debtAsset, debtAssetId, marketAddr, user, stopLossPrice = 0, stopLossType = CloseToAssetType.DEBT, takeProfitPrice = 0, takeProfitType = CloseToAssetType.COLLATERAL) {
236
+ const isBundle = true;
237
+ const closeType = getCloseStrategyType(stopLossPrice, stopLossType, takeProfitPrice, takeProfitType);
238
+ const subDataEncoded = subDataService.sparkCloseGenericSubData.encode(collAsset, collAssetId, debtAsset, debtAssetId, closeType, marketAddr, user);
239
+ const triggerDataEncoded = triggerService.sparkQuotePriceRangeTrigger.encode(collAsset, debtAsset, stopLossPrice, takeProfitPrice);
240
240
  return [strategyOrBundleId, isBundle, triggerDataEncoded, subDataEncoded];
241
241
  },
242
242
  };
@@ -5,7 +5,7 @@ import { getAssetInfo, MAXUINT } from '@defisaver/tokens';
5
5
  import * as web3Utils from 'web3-utils';
6
6
  import { Bundles, ChainId, CloseToAssetType, OrderType, RatioState, Strategies } from '../types/enums';
7
7
  import '../configuration';
8
- import { aaveV2Encode, chickenBondsEncode, liquityEncode, makerEncode, aaveV3Encode, compoundV2Encode, compoundV3Encode, morphoAaveV2Encode, exchangeEncode, sparkEncode, crvUSDEncode, compoundV3L2Encode, morphoBlueEncode, } from './strategySubService';
8
+ import { aaveV2Encode, chickenBondsEncode, liquityEncode, makerEncode, aaveV3Encode, compoundV2Encode, compoundV3Encode, morphoAaveV2Encode, exchangeEncode, crvUSDEncode, compoundV3L2Encode, morphoBlueEncode, } from './strategySubService';
9
9
  describe('Feature: strategySubService.ts', () => {
10
10
  describe('When testing strategySubService.makerEncode', () => {
11
11
  // @ts-ignore // TODO - this requires change in @defisaver/tokens
@@ -1163,78 +1163,6 @@ describe('Feature: strategySubService.ts', () => {
1163
1163
  });
1164
1164
  });
1165
1165
  });
1166
- describe('When testing strategySubService.sparkEncode', () => {
1167
- describe('leverageManagement()', () => {
1168
- const examples = [
1169
- [
1170
- '0x000000000000000016345785d8a0000000000000000000001e87f85809dc0000000000000000000018fae27693b4000000000000000000001a5e27eef13e000001',
1171
- [160, 220, 180, 190, true]
1172
- ],
1173
- [
1174
- '0x000000000000000016345785d8a0000000000000000000001bc16d674ec80000000000000000000018fae27693b4000000000000000000001a5e27eef13e000000',
1175
- [160, 200, 180, 190, false]
1176
- ],
1177
- ];
1178
- examples.forEach(([expected, actual]) => {
1179
- it(`Given ${actual} should return expected value: ${JSON.stringify(expected)}`, () => {
1180
- expect(sparkEncode.leverageManagement(...actual)).to.eql(expected);
1181
- });
1182
- });
1183
- });
1184
- describe('closeToAsset()', () => {
1185
- const examples = [
1186
- [
1187
- [
1188
- Bundles.MainnetIds.SPARK_CLOSE_TO_COLLATERAL,
1189
- true,
1190
- ['0x000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20000000000000000000000006b175474e89094c44da98b954eedeac495271d0f00000000000000000000000000000000000000000000000000000026e1f9c6000000000000000000000000000000000000000000000000000000000000000000'],
1191
- [
1192
- '0x000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2', '0x0000000000000000000000000000000000000000000000000000000000000015',
1193
- '0x0000000000000000000000006b175474e89094c44da98b954eedeac495271d0f', '0x0000000000000000000000000000000000000000000000000000000000000020',
1194
- '0x0000000000000000000000000000000000000000000000000000000000000000',
1195
- ],
1196
- ],
1197
- [
1198
- Bundles.MainnetIds.SPARK_CLOSE_TO_COLLATERAL,
1199
- true,
1200
- {
1201
- baseTokenAddress: getAssetInfo('WETH').address, quoteTokenAddress: getAssetInfo('DAI').address, price: 1670, ratioState: RatioState.OVER
1202
- },
1203
- {
1204
- collAsset: getAssetInfo('WETH').address, collAssetId: 21, debtAsset: getAssetInfo('DAI').address, debtAssetId: 32,
1205
- },
1206
- ]
1207
- ],
1208
- [
1209
- [
1210
- Bundles.MainnetIds.SPARK_CLOSE_TO_DEBT,
1211
- true,
1212
- ['0x000000000000000000000000514910771af9ca656af840dff83e8264ecf986ca0000000000000000000000006b175474e89094c44da98b954eedeac495271d0f0000000000000000000000000000000000000000000000000000000000084d000000000000000000000000000000000000000000000000000000000000000001'],
1213
- [
1214
- '0x0000000000000000000000006b175474e89094c44da98b954eedeac495271d0f', '0x0000000000000000000000000000000000000000000000000000000000000015',
1215
- '0x000000000000000000000000514910771af9ca656af840dff83e8264ecf986ca', '0x0000000000000000000000000000000000000000000000000000000000000020',
1216
- '0x0000000000000000000000000000000000000000000000000000000000000000',
1217
- ],
1218
- ],
1219
- [
1220
- Bundles.MainnetIds.SPARK_CLOSE_TO_DEBT,
1221
- true,
1222
- {
1223
- baseTokenAddress: getAssetInfo('LINK').address, quoteTokenAddress: getAssetInfo('DAI').address, price: 0.00544, ratioState: RatioState.UNDER
1224
- },
1225
- {
1226
- collAsset: getAssetInfo('DAI').address, collAssetId: 21, debtAsset: getAssetInfo('LINK').address, debtAssetId: 32,
1227
- },
1228
- ]
1229
- ]
1230
- ];
1231
- examples.forEach(([expected, actual]) => {
1232
- it(`Given ${actual} should return expected value: ${JSON.stringify(expected)}`, () => {
1233
- expect(sparkEncode.closeToAsset(...actual)).to.eql(expected);
1234
- });
1235
- });
1236
- });
1237
- });
1238
1166
  describe('When testing strategySubService.crvUSDEncode', () => {
1239
1167
  describe('leverageManagement()', () => {
1240
1168
  const examples = [
@@ -166,15 +166,6 @@ export declare const sparkLeverageManagementSubData: {
166
166
  targetRatio: number;
167
167
  };
168
168
  };
169
- export declare const sparkQuotePriceSubData: {
170
- encode(collAsset: EthereumAddress, collAssetId: number, debtAsset: EthereumAddress, debtAssetId: number, nullAddress?: EthereumAddress): string[];
171
- decode(subData: string[]): {
172
- collAsset: EthereumAddress;
173
- collAssetId: number;
174
- debtAsset: EthereumAddress;
175
- debtAssetId: number;
176
- };
177
- };
178
169
  export declare const liquityDsrPaybackSubData: {
179
170
  encode: (targetRatio: number) => string[];
180
171
  decode: (subData: string[]) => {
@@ -315,3 +306,15 @@ export declare const compoundV3CloseSubData: {
315
306
  owner: EthereumAddress;
316
307
  };
317
308
  };
309
+ export declare const sparkCloseGenericSubData: {
310
+ encode(collAsset: EthereumAddress, collAssetId: number, debtAsset: EthereumAddress, debtAssetId: number, closeType: CloseStrategyType, marketAddr: EthereumAddress, user: EthereumAddress): string[];
311
+ decode(subData: string[]): {
312
+ collAsset: EthereumAddress;
313
+ collAssetId: number;
314
+ debtAsset: EthereumAddress;
315
+ debtAssetId: number;
316
+ closeType: CloseStrategyType;
317
+ marketAddr: EthereumAddress;
318
+ owner: EthereumAddress;
319
+ };
320
+ };
@@ -411,25 +411,6 @@ export const sparkLeverageManagementSubData = {
411
411
  return { targetRatio };
412
412
  },
413
413
  };
414
- export const sparkQuotePriceSubData = {
415
- encode(collAsset, collAssetId, debtAsset, debtAssetId, nullAddress = ZERO_ADDRESS) {
416
- const encodedColl = AbiCoder.encodeParameter('address', collAsset);
417
- const encodedCollId = AbiCoder.encodeParameter('uint8', collAssetId);
418
- const encodedDebt = AbiCoder.encodeParameter('address', debtAsset);
419
- const encodedDebtId = AbiCoder.encodeParameter('uint8', debtAssetId);
420
- const encodedNullAddress = AbiCoder.encodeParameter('address', nullAddress);
421
- return [encodedColl, encodedCollId, encodedDebt, encodedDebtId, encodedNullAddress];
422
- },
423
- decode(subData) {
424
- const collAsset = AbiCoder.decodeParameter('address', subData[0]);
425
- const collAssetId = Number(AbiCoder.decodeParameter('uint8', subData[1]));
426
- const debtAsset = AbiCoder.decodeParameter('address', subData[2]);
427
- const debtAssetId = Number(AbiCoder.decodeParameter('uint8', subData[3]));
428
- return {
429
- collAsset, collAssetId, debtAsset, debtAssetId,
430
- };
431
- },
432
- };
433
414
  export const liquityDsrPaybackSubData = {
434
415
  encode: (targetRatio) => {
435
416
  const daiAddress = getAssetInfo('DAI').address;
@@ -839,3 +820,35 @@ export const compoundV3CloseSubData = {
839
820
  };
840
821
  },
841
822
  };
823
+ export const sparkCloseGenericSubData = {
824
+ encode(collAsset, collAssetId, debtAsset, debtAssetId, closeType, marketAddr, user) {
825
+ const encodedColl = AbiCoder.encodeParameter('address', collAsset);
826
+ const encodedCollId = AbiCoder.encodeParameter('uint8', collAssetId);
827
+ const encodedDebt = AbiCoder.encodeParameter('address', debtAsset);
828
+ const encodedDebtId = AbiCoder.encodeParameter('uint8', debtAssetId);
829
+ const encodedCloseType = AbiCoder.encodeParameter('uint8', closeType);
830
+ const encodedMarket = AbiCoder.encodeParameter('address', marketAddr);
831
+ const userEncoded = AbiCoder.encodeParameter('address', user);
832
+ return [
833
+ encodedColl,
834
+ encodedCollId,
835
+ encodedDebt,
836
+ encodedDebtId,
837
+ encodedCloseType,
838
+ encodedMarket,
839
+ userEncoded,
840
+ ];
841
+ },
842
+ decode(subData) {
843
+ const collAsset = AbiCoder.decodeParameter('address', subData[0]);
844
+ const collAssetId = Number(AbiCoder.decodeParameter('uint8', subData[1]));
845
+ const debtAsset = AbiCoder.decodeParameter('address', subData[2]);
846
+ const debtAssetId = Number(AbiCoder.decodeParameter('uint8', subData[3]));
847
+ const closeType = Number(AbiCoder.decodeParameter('uint8', subData[4]));
848
+ const marketAddr = AbiCoder.decodeParameter('address', subData[5]);
849
+ const owner = AbiCoder.decodeParameter('address', subData[6]);
850
+ return {
851
+ collAsset, collAssetId, debtAsset, debtAssetId, closeType, marketAddr, owner,
852
+ };
853
+ },
854
+ };
@@ -873,78 +873,6 @@ describe('Feature: subDataService.ts', () => {
873
873
  });
874
874
  });
875
875
  });
876
- describe('When testing subDataService.sparkQuotePriceSubData', () => {
877
- describe('encode()', () => {
878
- const examples = [
879
- [
880
- [
881
- '0x000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2', '0x0000000000000000000000000000000000000000000000000000000000000002',
882
- '0x0000000000000000000000006b175474e89094c44da98b954eedeac495271d0f', '0x0000000000000000000000000000000000000000000000000000000000000004',
883
- '0x0000000000000000000000000000000000000000000000000000000000000000',
884
- ],
885
- [
886
- web3Utils.toChecksumAddress(getAssetInfo('WETH', ChainId.Ethereum).address),
887
- 2,
888
- web3Utils.toChecksumAddress(getAssetInfo('DAI', ChainId.Ethereum).address),
889
- 4,
890
- ]
891
- ],
892
- [
893
- [
894
- '0x0000000000000000000000005979d7b546e38e414f7e9822514be443a4800529', '0x0000000000000000000000000000000000000000000000000000000000000006',
895
- '0x000000000000000000000000af88d065e77c8cc2239327c5edb3a432268e5831', '0x0000000000000000000000000000000000000000000000000000000000000002',
896
- '0x0000000000000000000000000000000000000000000000000000000000000000',
897
- ],
898
- [
899
- web3Utils.toChecksumAddress(getAssetInfo('wstETH', ChainId.Arbitrum).address),
900
- 6,
901
- web3Utils.toChecksumAddress(getAssetInfo('USDC', ChainId.Arbitrum).address),
902
- 2,
903
- ]
904
- ],
905
- ];
906
- examples.forEach(([expected, actual]) => {
907
- it(`Given ${actual} should return expected value: ${expected}`, () => {
908
- expect(subDataService.sparkQuotePriceSubData.encode(...actual)).to.eql(expected);
909
- });
910
- });
911
- });
912
- describe('decode()', () => {
913
- const examples = [
914
- [
915
- {
916
- collAsset: web3Utils.toChecksumAddress(getAssetInfo('WETH', ChainId.Ethereum).address),
917
- collAssetId: 2,
918
- debtAsset: web3Utils.toChecksumAddress(getAssetInfo('DAI', ChainId.Ethereum).address),
919
- debtAssetId: 4,
920
- },
921
- [
922
- '0x000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2', '0x0000000000000000000000000000000000000000000000000000000000000002',
923
- '0x0000000000000000000000006b175474e89094c44da98b954eedeac495271d0f', '0x0000000000000000000000000000000000000000000000000000000000000004',
924
- '0x0000000000000000000000000000000000000000000000000000000000000000',
925
- ],
926
- ],
927
- [
928
- {
929
- collAsset: web3Utils.toChecksumAddress(getAssetInfo('wstETH', ChainId.Arbitrum).address),
930
- collAssetId: 6,
931
- debtAsset: web3Utils.toChecksumAddress(getAssetInfo('USDC', ChainId.Arbitrum).address),
932
- debtAssetId: 2,
933
- },
934
- [
935
- '0x0000000000000000000000005979d7b546e38e414f7e9822514be443a4800529', '0x0000000000000000000000000000000000000000000000000000000000000006',
936
- '0x000000000000000000000000af88d065e77c8cc2239327c5edb3a432268e5831', '0x0000000000000000000000000000000000000000000000000000000000000002',
937
- '0x0000000000000000000000000000000000000000000000000000000000000000',
938
- ],
939
- ],
940
- ];
941
- examples.forEach(([expected, actual]) => {
942
- it(`Given ${actual} should return expected value: ${JSON.stringify(expected)}`, () => {
943
- expect(subDataService.sparkQuotePriceSubData.decode(actual)).to.eql(expected);
944
- });
945
- });
946
- });
947
- });
948
876
  describe('When testing subDataService.liquityDsrPaybackSubData', () => {
949
877
  describe('encode()', () => {
950
878
  const examples = [
@@ -153,15 +153,6 @@ export declare const sparkRatioTrigger: {
153
153
  ratioState: number;
154
154
  };
155
155
  };
156
- export declare const sparkQuotePriceTrigger: {
157
- encode(baseTokenAddress: EthereumAddress, quoteTokenAddress: EthereumAddress, price: number, ratioState: RatioState): string[];
158
- decode(triggerData: string[]): {
159
- baseTokenAddress: EthereumAddress;
160
- quoteTokenAddress: EthereumAddress;
161
- price: string;
162
- ratioState: RatioState;
163
- };
164
- };
165
156
  export declare const curveUsdBorrowRateTrigger: {
166
157
  encode(market: EthereumAddress, targetRate: string, rateState: RatioState): string[];
167
158
  decode(triggerData: string[]): {
@@ -275,3 +266,12 @@ export declare const aaveV3QuotePriceRangeTrigger: {
275
266
  upperPrice: string;
276
267
  };
277
268
  };
269
+ export declare const sparkQuotePriceRangeTrigger: {
270
+ encode(collToken: EthereumAddress, debtToken: EthereumAddress, lowerPrice: number, upperPrice: number): string[];
271
+ decode(triggerData: string[]): {
272
+ collToken: string;
273
+ debtToken: string;
274
+ lowerPrice: string;
275
+ upperPrice: string;
276
+ };
277
+ };
@@ -261,24 +261,6 @@ export const sparkRatioTrigger = {
261
261
  };
262
262
  },
263
263
  };
264
- export const sparkQuotePriceTrigger = {
265
- encode(baseTokenAddress, quoteTokenAddress, price, ratioState) {
266
- // Price is always in 8 decimals
267
- const _price = new Dec(price.toString()).mul(Math.pow(10, 8)).floor().toString();
268
- return [AbiCoder.encodeParameters(['address', 'address', 'uint256', 'uint8'], [baseTokenAddress, quoteTokenAddress, _price, ratioState])];
269
- },
270
- decode(triggerData) {
271
- const decodedData = AbiCoder.decodeParameters(['address', 'address', 'uint256', 'uint8'], triggerData[0]);
272
- // Price is always in 8 decimals
273
- const price = new Dec(decodedData[2]).div(Math.pow(10, 8)).toDP(8).toString();
274
- return {
275
- price,
276
- baseTokenAddress: decodedData[0],
277
- quoteTokenAddress: decodedData[1],
278
- ratioState: +decodedData[3],
279
- };
280
- },
281
- };
282
264
  export const curveUsdBorrowRateTrigger = {
283
265
  encode(market, targetRate, rateState) {
284
266
  // the form is x = (e**(rate*365*86400))-1 where x*100 is number in %
@@ -497,3 +479,22 @@ export const aaveV3QuotePriceRangeTrigger = {
497
479
  };
498
480
  },
499
481
  };
482
+ export const sparkQuotePriceRangeTrigger = {
483
+ encode(collToken, debtToken, lowerPrice, upperPrice) {
484
+ // Price is scaled to 1e8
485
+ const lowerPriceFormatted = new Dec(lowerPrice).mul(1e8).floor().toString();
486
+ const upperPriceFormatted = new Dec(upperPrice).mul(1e8).floor().toString();
487
+ return [
488
+ AbiCoder.encodeParameters(['address', 'address', 'uint256', 'uint256'], [collToken, debtToken, lowerPriceFormatted, upperPriceFormatted]),
489
+ ];
490
+ },
491
+ decode(triggerData) {
492
+ const decodedData = AbiCoder.decodeParameters(['address', 'address', 'uint256', 'uint256'], triggerData[0]);
493
+ return {
494
+ collToken: decodedData[0],
495
+ debtToken: decodedData[1],
496
+ lowerPrice: new Dec(decodedData[2]).div(1e8).toString(),
497
+ upperPrice: new Dec(decodedData[3]).div(1e8).toString(),
498
+ };
499
+ },
500
+ };
@@ -3,7 +3,7 @@ import { getAssetInfo, MAXUINT } from '@defisaver/tokens';
3
3
  import * as web3Utils from 'web3-utils';
4
4
  import { ChainId, OrderType, RatioState } from '../types/enums';
5
5
  import '../configuration';
6
- import { aaveV2RatioTrigger, aaveV3QuotePriceTrigger, aaveV3QuotePriceWithMaximumGasPriceTrigger, aaveV3RatioTrigger, cBondsRebondTrigger, chainlinkPriceTrigger, compoundV2RatioTrigger, compoundV3RatioTrigger, compoundV3PriceTrigger, curveUsdBorrowRateTrigger, curveUsdSoftLiquidationTrigger, exchangeOffchainPriceTrigger, exchangeTimestampTrigger, liquityDebtInFrontTrigger, makerRatioTrigger, morphoAaveV2RatioTrigger, sparkQuotePriceTrigger, sparkRatioTrigger, trailingStopTrigger, liquityDebtInFrontWithLimitTrigger, crvUSDRatioTrigger, morphoBlueRatioTrigger, crvUsdHealthRatioTrigger, liquityV2DebtInFrontTrigger, liquityV2AdjustTimeTrigger, compoundV3PriceRangeTrigger, aaveV3QuotePriceRangeTrigger, } from './triggerService';
6
+ import { aaveV2RatioTrigger, aaveV3QuotePriceTrigger, aaveV3QuotePriceWithMaximumGasPriceTrigger, aaveV3RatioTrigger, cBondsRebondTrigger, chainlinkPriceTrigger, compoundV2RatioTrigger, compoundV3RatioTrigger, compoundV3PriceTrigger, curveUsdBorrowRateTrigger, curveUsdSoftLiquidationTrigger, exchangeOffchainPriceTrigger, exchangeTimestampTrigger, liquityDebtInFrontTrigger, makerRatioTrigger, morphoAaveV2RatioTrigger, sparkRatioTrigger, trailingStopTrigger, liquityDebtInFrontWithLimitTrigger, crvUSDRatioTrigger, morphoBlueRatioTrigger, crvUsdHealthRatioTrigger, liquityV2DebtInFrontTrigger, liquityV2AdjustTimeTrigger, compoundV3PriceRangeTrigger, aaveV3QuotePriceRangeTrigger, } from './triggerService';
7
7
  describe('Feature: triggerService.ts', () => {
8
8
  describe('When testing triggerService.chainlinkPriceTrigger', () => {
9
9
  describe('encode()', () => {
@@ -781,52 +781,6 @@ describe('Feature: triggerService.ts', () => {
781
781
  });
782
782
  });
783
783
  });
784
- describe('When testing triggerService.sparkQuotePriceTrigger', () => {
785
- describe('encode()', () => {
786
- const examples = [
787
- [
788
- ['0x0000000000000000000000006b175474e89094c44da98b954eedeac495271d0f0000000000000000000000007f39c581f595b53c5cb19bd0b3f8da6c935e2ca0000000000000000000000000000000000000000000000000000000000000a8ca0000000000000000000000000000000000000000000000000000000000000001'],
789
- [getAssetInfo('DAI', ChainId.Ethereum).address, getAssetInfo('wstETH', ChainId.Ethereum).address, 0.0004321, RatioState.UNDER]
790
- ],
791
- [
792
- ['0x0000000000000000000000002f2a2543b76a4166549f7aab2e75bef0aefc5b0f000000000000000000000000af88d065e77c8cc2239327c5edb3a432268e583100000000000000000000000000000000000000000000000000000000000186b70000000000000000000000000000000000000000000000000000000000000000'],
793
- [getAssetInfo('WBTC', ChainId.Arbitrum).address, getAssetInfo('USDC', ChainId.Arbitrum).address, 0.00100023, RatioState.OVER]
794
- ],
795
- ];
796
- examples.forEach(([expected, actual]) => {
797
- it(`Given ${actual} should return expected value: ${expected}`, () => {
798
- expect(sparkQuotePriceTrigger.encode(...actual)).to.eql(expected);
799
- });
800
- });
801
- });
802
- describe('decode()', () => {
803
- const examples = [
804
- [
805
- {
806
- baseTokenAddress: web3Utils.toChecksumAddress(getAssetInfo('DAI', ChainId.Ethereum).address),
807
- quoteTokenAddress: web3Utils.toChecksumAddress(getAssetInfo('wstETH', ChainId.Ethereum).address),
808
- price: '0.0004321',
809
- ratioState: RatioState.UNDER,
810
- },
811
- ['0x0000000000000000000000006b175474e89094c44da98b954eedeac495271d0f0000000000000000000000007f39c581f595b53c5cb19bd0b3f8da6c935e2ca0000000000000000000000000000000000000000000000000000000000000a8ca0000000000000000000000000000000000000000000000000000000000000001'],
812
- ],
813
- [
814
- {
815
- baseTokenAddress: web3Utils.toChecksumAddress(getAssetInfo('WBTC', ChainId.Arbitrum).address),
816
- quoteTokenAddress: web3Utils.toChecksumAddress(getAssetInfo('USDC', ChainId.Arbitrum).address),
817
- price: '0.00100023',
818
- ratioState: RatioState.OVER,
819
- },
820
- ['0x0000000000000000000000002f2a2543b76a4166549f7aab2e75bef0aefc5b0f000000000000000000000000af88d065e77c8cc2239327c5edb3a432268e583100000000000000000000000000000000000000000000000000000000000186b70000000000000000000000000000000000000000000000000000000000000000'],
821
- ]
822
- ];
823
- examples.forEach(([expected, actual]) => {
824
- it(`Given ${actual} should return expected value: ${JSON.stringify(expected)}`, () => {
825
- expect(sparkQuotePriceTrigger.decode(actual)).to.eql(expected);
826
- });
827
- });
828
- });
829
- });
830
784
  describe('When testing triggerService.curveUsdBorrowRateTrigger', () => {
831
785
  describe('encode()', () => {
832
786
  const examples = [
@@ -1,11 +1,8 @@
1
1
  import { expect } from 'chai';
2
- import * as web3Utils from 'web3-utils';
3
- import AbiCoder from 'web3-eth-abi';
4
2
  import { getAssetInfo } from '@defisaver/tokens';
5
3
  import { ChainId, ProtocolIdentifiers, RatioState } from '../types/enums';
6
- import { sparkEncode } from './strategySubService';
7
4
  import '../configuration';
8
- import { addToArrayIf, addToObjectIf, compareAddresses, compareSubHashes, encodeSubId, ethToWeth, getRatioStateInfoForAaveCloseStrategy, isAddress, isDefined, isEmptyBytes, isRatioStateOver, isRatioStateUnder, isUndefined, ratioPercentageToWei, requireAddress, requireAddresses, weiToRatioPercentage, wethToEth, wethToEthByAddress, getPositionId, } from './utils';
5
+ import { addToArrayIf, addToObjectIf, compareAddresses, encodeSubId, ethToWeth, getRatioStateInfoForAaveCloseStrategy, isAddress, isDefined, isEmptyBytes, isRatioStateOver, isRatioStateUnder, isUndefined, ratioPercentageToWei, requireAddress, requireAddresses, weiToRatioPercentage, wethToEth, wethToEthByAddress, getPositionId, } from './utils';
9
6
  describe('Feature: utils.ts', () => {
10
7
  describe('When testing utils.isDefined()', () => {
11
8
  const examples = [
@@ -139,54 +136,6 @@ describe('Feature: utils.ts', () => {
139
136
  });
140
137
  });
141
138
  });
142
- describe('When testing utils.compareSubHashes()', () => {
143
- const subDataToEncodeOne = [
144
- 12,
145
- false,
146
- {
147
- baseTokenAddress: '0xaf88d065e77c8cC2239327C5EDb3A432268e5831',
148
- quoteTokenAddress: '0xaf88d065e77c8cC2239327C5EDb3A432268e5831',
149
- price: 100,
150
- ratioState: 1,
151
- },
152
- {
153
- collAsset: '0xaf88d065e77c8cC2239327C5EDb3A432268e5831',
154
- collAssetId: 2,
155
- debtAsset: '0xaf88d065e77c8cC2239327C5EDb3A432268e5831',
156
- debtAssetId: 3,
157
- },
158
- ];
159
- const subDataToEncodeTwo = [
160
- 13,
161
- true,
162
- {
163
- baseTokenAddress: '0xaf88d065e77c8cC2239327C5EDb3A432268e5831',
164
- quoteTokenAddress: '0xaf88d065e77c8cC2239327C5EDb3A432268e5831',
165
- price: 100,
166
- ratioState: 2,
167
- },
168
- {
169
- collAsset: '0xaf88d065e77c8cC2239327C5EDb3A432268e5831',
170
- collAssetId: 2,
171
- debtAsset: '0xaf88d065e77c8cC2239327C5EDb3A432268e5831',
172
- debtAssetId: 3,
173
- },
174
- ];
175
- // @ts-ignore
176
- const encodedSubDataOne = sparkEncode.closeToAsset(...subDataToEncodeOne);
177
- // @ts-ignore
178
- const encodedSubDataTwo = sparkEncode.closeToAsset(...subDataToEncodeTwo);
179
- const encodedParams = web3Utils.keccak256(AbiCoder.encodeParameter('(uint64,bool,bytes[],bytes32[])', encodedSubDataOne));
180
- const examples = [
181
- [true, [encodedParams, encodedSubDataOne]],
182
- [false, [encodedParams, encodedSubDataTwo]],
183
- ];
184
- examples.forEach(([expected, actual]) => {
185
- it(`Given ${actual} should return expected value: ${expected}`, () => {
186
- expect(compareSubHashes(...actual)).to.equal(expected);
187
- });
188
- });
189
- });
190
139
  describe('When testing utils.encodeSubId()', () => {
191
140
  const examples = [
192
141
  ['00000001', '1'],
@@ -171,8 +171,6 @@ export declare namespace Bundles {
171
171
  LIQUITY_BOOST = 17,
172
172
  SPARK_REPAY = 18,
173
173
  SPARK_BOOST = 19,
174
- SPARK_CLOSE_TO_DEBT = -21231230,
175
- SPARK_CLOSE_TO_COLLATERAL = -21231231,
176
174
  AAVE_V2_REPAY = 22,
177
175
  AAVE_V2_BOOST = 23,
178
176
  COMP_V2_REPAY = 20,
@@ -207,7 +205,8 @@ export declare namespace Bundles {
207
205
  AAVE_V3_EOA_BOOST = 53,
208
206
  AAVE_V3_EOA_REPAY_ON_PRICE = 54,
209
207
  AAVE_V3_EOA_BOOST_ON_PRICE = 55,
210
- AAVE_V3_EOA_CLOSE = 56
208
+ AAVE_V3_EOA_CLOSE = 56,
209
+ SPARK_CLOSE = 57
211
210
  }
212
211
  enum OptimismIds {
213
212
  AAVE_V3_REPAY = 0,
@@ -191,8 +191,6 @@ export var Bundles;
191
191
  MainnetIds[MainnetIds["LIQUITY_BOOST"] = 17] = "LIQUITY_BOOST";
192
192
  MainnetIds[MainnetIds["SPARK_REPAY"] = 18] = "SPARK_REPAY";
193
193
  MainnetIds[MainnetIds["SPARK_BOOST"] = 19] = "SPARK_BOOST";
194
- MainnetIds[MainnetIds["SPARK_CLOSE_TO_DEBT"] = -21231230] = "SPARK_CLOSE_TO_DEBT";
195
- MainnetIds[MainnetIds["SPARK_CLOSE_TO_COLLATERAL"] = -21231231] = "SPARK_CLOSE_TO_COLLATERAL";
196
194
  MainnetIds[MainnetIds["AAVE_V2_REPAY"] = 22] = "AAVE_V2_REPAY";
197
195
  MainnetIds[MainnetIds["AAVE_V2_BOOST"] = 23] = "AAVE_V2_BOOST";
198
196
  MainnetIds[MainnetIds["COMP_V2_REPAY"] = 20] = "COMP_V2_REPAY";
@@ -228,6 +226,7 @@ export var Bundles;
228
226
  MainnetIds[MainnetIds["AAVE_V3_EOA_REPAY_ON_PRICE"] = 54] = "AAVE_V3_EOA_REPAY_ON_PRICE";
229
227
  MainnetIds[MainnetIds["AAVE_V3_EOA_BOOST_ON_PRICE"] = 55] = "AAVE_V3_EOA_BOOST_ON_PRICE";
230
228
  MainnetIds[MainnetIds["AAVE_V3_EOA_CLOSE"] = 56] = "AAVE_V3_EOA_CLOSE";
229
+ MainnetIds[MainnetIds["SPARK_CLOSE"] = 57] = "SPARK_CLOSE";
231
230
  })(MainnetIds = Bundles.MainnetIds || (Bundles.MainnetIds = {}));
232
231
  let OptimismIds;
233
232
  (function (OptimismIds) {