@defisaver/automation-sdk 3.3.22-coll-switch-generic-dev → 3.3.22

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 (52) hide show
  1. package/cjs/automation/private/StrategiesAutomation.d.ts +5 -1
  2. package/cjs/automation/private/StrategiesAutomation.js +12 -0
  3. package/cjs/automation/private/StrategiesAutomation.test.js +51 -0
  4. package/cjs/constants/index.js +0 -60
  5. package/cjs/services/apiSubscriptionsService.d.ts +7 -0
  6. package/cjs/services/apiSubscriptionsService.js +33 -0
  7. package/cjs/services/apiSubscriptionsService.test.d.ts +1 -0
  8. package/cjs/services/apiSubscriptionsService.test.js +67 -0
  9. package/cjs/services/strategiesService.js +1 -21
  10. package/cjs/services/strategiesService.test.js +0 -93
  11. package/cjs/services/strategySubService.d.ts +0 -2
  12. package/cjs/services/strategySubService.js +0 -12
  13. package/cjs/services/strategySubService.test.js +0 -107
  14. package/cjs/services/subDataService.d.ts +0 -24
  15. package/cjs/services/subDataService.js +2 -66
  16. package/cjs/services/subDataService.test.js +0 -139
  17. package/cjs/types/enums.d.ts +4 -18
  18. package/cjs/types/enums.js +0 -14
  19. package/cjs/types/index.d.ts +24 -2
  20. package/esm/automation/private/StrategiesAutomation.d.ts +5 -1
  21. package/esm/automation/private/StrategiesAutomation.js +12 -0
  22. package/esm/automation/private/StrategiesAutomation.test.js +51 -0
  23. package/esm/constants/index.js +0 -60
  24. package/esm/services/apiSubscriptionsService.d.ts +7 -0
  25. package/esm/services/apiSubscriptionsService.js +29 -0
  26. package/esm/services/apiSubscriptionsService.test.d.ts +1 -0
  27. package/esm/services/apiSubscriptionsService.test.js +65 -0
  28. package/esm/services/strategiesService.js +1 -21
  29. package/esm/services/strategiesService.test.js +1 -94
  30. package/esm/services/strategySubService.d.ts +0 -2
  31. package/esm/services/strategySubService.js +0 -12
  32. package/esm/services/strategySubService.test.js +0 -107
  33. package/esm/services/subDataService.d.ts +0 -24
  34. package/esm/services/subDataService.js +0 -64
  35. package/esm/services/subDataService.test.js +0 -139
  36. package/esm/types/enums.d.ts +4 -18
  37. package/esm/types/enums.js +0 -14
  38. package/esm/types/index.d.ts +24 -2
  39. package/package.json +1 -1
  40. package/src/automation/private/StrategiesAutomation.test.ts +65 -1
  41. package/src/automation/private/StrategiesAutomation.ts +17 -0
  42. package/src/constants/index.ts +0 -60
  43. package/src/services/apiSubscriptionsService.test.ts +73 -0
  44. package/src/services/apiSubscriptionsService.ts +33 -0
  45. package/src/services/strategiesService.test.ts +1 -103
  46. package/src/services/strategiesService.ts +1 -23
  47. package/src/services/strategySubService.test.ts +0 -131
  48. package/src/services/strategySubService.ts +0 -46
  49. package/src/services/subDataService.test.ts +0 -149
  50. package/src/services/subDataService.ts +0 -101
  51. package/src/types/enums.ts +0 -14
  52. package/src/types/index.ts +26 -2
@@ -1,6 +1,6 @@
1
1
  import type Web3 from 'web3';
2
2
  import type { PastEventOptions } from 'web3-eth-contract';
3
- import type { Position, Interfaces, EthereumAddress, SubscriptionOptions, Contract, ParseData, BlockNumber } from '../../types';
3
+ import type { Position, Interfaces, EthereumAddress, SubscriptionOptions, Contract, ParseData, BlockNumber, ApiSubscriptionRecord } from '../../types';
4
4
  import type { StrategyModel, Subscribe, SubStorage, UpdateData } from '../../types/contracts/generated/SubStorage';
5
5
  import type { ChainId } from '../../types/enums';
6
6
  import Automation from './Automation';
@@ -30,6 +30,10 @@ export default class StrategiesAutomation extends Automation {
30
30
  protected mergeSubs(_subscriptions: (Position.Automated | null)[]): Position.Automated[];
31
31
  protected _getSubscriptions(addresses?: EthereumAddress[], options?: SubscriptionOptions): Promise<(Position.Automated | null)[]>;
32
32
  getSubscriptions(options?: SubscriptionOptions): Promise<(Position.Automated | null)[]>;
33
+ /**
34
+ * @description Parses subscriptions returned by the automation API (GET /v1/subscriptions)
35
+ */
36
+ parseSubscriptionsFromApi(records: ApiSubscriptionRecord[], options?: SubscriptionOptions): (Position.Automated | null)[];
33
37
  getSubscriptionsFor(addresses: EthereumAddress[], options?: SubscriptionOptions): Promise<(Position.Automated | null)[]>;
34
38
  }
35
39
  export {};
@@ -19,6 +19,7 @@ const utils_1 = require("../../services/utils");
19
19
  const contractService_1 = require("../../services/contractService");
20
20
  const ethereumService_1 = require("../../services/ethereumService");
21
21
  const strategiesService_1 = require("../../services/strategiesService");
22
+ const apiSubscriptionsService_1 = require("../../services/apiSubscriptionsService");
22
23
  const Automation_1 = __importDefault(require("./Automation"));
23
24
  class StrategiesAutomation extends Automation_1.default {
24
25
  constructor(args) {
@@ -189,6 +190,17 @@ class StrategiesAutomation extends Automation_1.default {
189
190
  return this._getSubscriptions(undefined, options);
190
191
  });
191
192
  }
193
+ /**
194
+ * @description Parses subscriptions returned by the automation API (GET /v1/subscriptions)
195
+ */
196
+ parseSubscriptionsFromApi(records, options) {
197
+ const filtered = (options === null || options === void 0 ? void 0 : options.enabledOnly) ? records.filter((record) => record.is_enabled) : records;
198
+ let subscriptions = filtered.map((record) => this.getParsedSubscriptions((0, apiSubscriptionsService_1.parseDataFromApiSubscription)(record, this.chainId)));
199
+ if (options === null || options === void 0 ? void 0 : options.mergeSubs) {
200
+ subscriptions = this.mergeSubs(subscriptions);
201
+ }
202
+ return (options === null || options === void 0 ? void 0 : options.unexpiredOnly) ? this.removeExpiredSubscriptions(subscriptions) : subscriptions;
203
+ }
192
204
  getSubscriptionsFor(addresses, options) {
193
205
  return __awaiter(this, void 0, void 0, function* () {
194
206
  return this._getSubscriptions(addresses, options);
@@ -693,4 +693,55 @@ describe('Feature: StrategiesAutomation.ts', () => {
693
693
  (0, chai_1.expect)(loneBoost === null || loneBoost === void 0 ? void 0 : loneBoost.subId).to.equal(1);
694
694
  });
695
695
  }));
696
+ describe('When testing StrategiesAutomation.parseSubscriptionsFromApi', () => {
697
+ const strategiesAutomation = new StrategiesAutomation_1.default({
698
+ chainId: enums_1.ChainId.Ethereum,
699
+ provider: Web3_1,
700
+ providerFork: null,
701
+ });
702
+ const owner = '0x9cB7E19861665366011899d74E75d4F2A419aEeD';
703
+ const subData = [
704
+ '0x0000000000000000000000000000000000000000000000001bc16d674ec80000',
705
+ '0x0000000000000000000000000000000000000000000000000000000000000001',
706
+ '0x0000000000000000000000000000000000000000000000000000000000000001',
707
+ '0x0000000000000000000000000000000000000000000000000000000000000000',
708
+ ];
709
+ // Aave V3 leverage management: repay (bundle 8, ratio under) and boost (bundle 9, ratio over) for the same owner and market
710
+ const repay = {
711
+ id: 379,
712
+ wallet: owner,
713
+ wallet_type: 'safe',
714
+ is_enabled: true,
715
+ is_bundle: true,
716
+ strategy_or_bundle_id: 8,
717
+ strategy_ids: [34, 35],
718
+ sub_data_hash: '0xafa4d200be62f171b57b1ae0f4e8348d1ac3f6d0812ad6da74a2adae8037dde1',
719
+ trigger_data: ['0000000000000000000000009cb7e19861665366011899d74e75d4f2a419aeed0000000000000000000000002f39d218133afab8f2b819b1066c7e434ad94e9e00000000000000000000000000000000000000000000000019ac8532c27900000000000000000000000000000000000000000000000000000000000000000001'],
720
+ sub_data: subData,
721
+ block_number: 18015756,
722
+ additional_triggers: null,
723
+ };
724
+ const boost = Object.assign(Object.assign({}, repay), { id: 380, is_enabled: false, strategy_or_bundle_id: 9, sub_data_hash: '0x1111111111111111111111111111111111111111111111111111111111111111', trigger_data: ['0000000000000000000000009cb7e19861665366011899d74e75d4f2a419aeed0000000000000000000000002f39d218133afab8f2b819b1066c7e434ad94e9e0000000000000000000000000000000000000000000000002386f26fc10000000000000000000000000000000000000000000000000000000000000000000000'] });
725
+ it('Given API records should return parsed positions', () => {
726
+ const positions = strategiesAutomation.parseSubscriptionsFromApi([repay, boost]);
727
+ (0, chai_1.expect)(positions).to.have.length(2);
728
+ (0, chai_1.expect)(positions.map((p) => p === null || p === void 0 ? void 0 : p.subId)).to.eql([379, 380]);
729
+ (0, chai_1.expect)(positions.map((p) => p === null || p === void 0 ? void 0 : p.isEnabled)).to.eql([true, false]);
730
+ });
731
+ it('Given mergeSubs option should merge the repay and boost pair', () => {
732
+ var _a, _b;
733
+ const positions = strategiesAutomation.parseSubscriptionsFromApi([repay, boost], { mergeSubs: true });
734
+ (0, chai_1.expect)(positions).to.have.length(1);
735
+ (0, chai_1.expect)((_a = positions[0]) === null || _a === void 0 ? void 0 : _a.subIds).to.eql([379, 380]);
736
+ (0, chai_1.expect)((_b = positions[0]) === null || _b === void 0 ? void 0 : _b.isEnabled).to.equal(true);
737
+ });
738
+ it('Given enabledOnly option should drop disabled records before parsing', () => {
739
+ const positions = strategiesAutomation.parseSubscriptionsFromApi([repay, boost], { enabledOnly: true });
740
+ (0, chai_1.expect)(positions.map((p) => p === null || p === void 0 ? void 0 : p.subId)).to.eql([379]);
741
+ });
742
+ it('Given a record of an unknown strategy should return null for it', () => {
743
+ const positions = strategiesAutomation.parseSubscriptionsFromApi([Object.assign(Object.assign({}, repay), { strategy_or_bundle_id: 9999 })]);
744
+ (0, chai_1.expect)(positions).to.eql([null]);
745
+ });
746
+ });
696
747
  });
@@ -107,11 +107,6 @@ exports.MAINNET_STRATEGIES_INFO = {
107
107
  strategyId: enums_1.Strategies.Identifiers.CollateralSwitch,
108
108
  protocol: exports.PROTOCOLS.AaveV3,
109
109
  },
110
- [enums_1.Strategies.MainnetIds.AAVE_V3_COLLATERAL_SWITCH_EOA]: {
111
- strategyOrBundleId: enums_1.Strategies.MainnetIds.AAVE_V3_COLLATERAL_SWITCH_EOA,
112
- strategyId: enums_1.Strategies.Identifiers.EoaCollateralSwitch,
113
- protocol: exports.PROTOCOLS.AaveV3,
114
- },
115
110
  [enums_1.Strategies.MainnetIds.AAVE_V4_COLLATERAL_SWITCH]: {
116
111
  strategyOrBundleId: enums_1.Strategies.MainnetIds.AAVE_V4_COLLATERAL_SWITCH,
117
112
  strategyId: enums_1.Strategies.Identifiers.CollateralSwitch,
@@ -132,16 +127,6 @@ exports.MAINNET_STRATEGIES_INFO = {
132
127
  strategyId: enums_1.Strategies.Identifiers.EoaCollateralSwitch,
133
128
  protocol: exports.PROTOCOLS.Spark,
134
129
  },
135
- [enums_1.Strategies.MainnetIds.AAVE_V3_DEBT_SWITCH]: {
136
- strategyOrBundleId: enums_1.Strategies.MainnetIds.AAVE_V3_DEBT_SWITCH,
137
- strategyId: enums_1.Strategies.Identifiers.DebtSwitch,
138
- protocol: exports.PROTOCOLS.AaveV3,
139
- },
140
- [enums_1.Strategies.MainnetIds.AAVE_V3_DEBT_SWITCH_EOA]: {
141
- strategyOrBundleId: enums_1.Strategies.MainnetIds.AAVE_V3_DEBT_SWITCH_EOA,
142
- strategyId: enums_1.Strategies.Identifiers.EoaDebtSwitch,
143
- protocol: exports.PROTOCOLS.AaveV3,
144
- },
145
130
  };
146
131
  exports.OPTIMISM_STRATEGIES_INFO = {
147
132
  [enums_1.Strategies.OptimismIds.EXCHANGE_DCA]: {
@@ -159,21 +144,6 @@ exports.OPTIMISM_STRATEGIES_INFO = {
159
144
  strategyId: enums_1.Strategies.Identifiers.CollateralSwitch,
160
145
  protocol: exports.PROTOCOLS.AaveV3,
161
146
  },
162
- [enums_1.Strategies.OptimismIds.AAVE_V3_COLLATERAL_SWITCH_EOA]: {
163
- strategyOrBundleId: enums_1.Strategies.OptimismIds.AAVE_V3_COLLATERAL_SWITCH_EOA,
164
- strategyId: enums_1.Strategies.Identifiers.EoaCollateralSwitch,
165
- protocol: exports.PROTOCOLS.AaveV3,
166
- },
167
- [enums_1.Strategies.OptimismIds.AAVE_V3_DEBT_SWITCH]: {
168
- strategyOrBundleId: enums_1.Strategies.OptimismIds.AAVE_V3_DEBT_SWITCH,
169
- strategyId: enums_1.Strategies.Identifiers.DebtSwitch,
170
- protocol: exports.PROTOCOLS.AaveV3,
171
- },
172
- [enums_1.Strategies.OptimismIds.AAVE_V3_DEBT_SWITCH_EOA]: {
173
- strategyOrBundleId: enums_1.Strategies.OptimismIds.AAVE_V3_DEBT_SWITCH_EOA,
174
- strategyId: enums_1.Strategies.Identifiers.EoaDebtSwitch,
175
- protocol: exports.PROTOCOLS.AaveV3,
176
- },
177
147
  };
178
148
  exports.BASE_STRATEGIES_INFO = {
179
149
  [enums_1.Strategies.BaseIds.EXCHANGE_DCA]: {
@@ -191,21 +161,6 @@ exports.BASE_STRATEGIES_INFO = {
191
161
  strategyId: enums_1.Strategies.Identifiers.CollateralSwitch,
192
162
  protocol: exports.PROTOCOLS.AaveV3,
193
163
  },
194
- [enums_1.Strategies.BaseIds.AAVE_V3_COLLATERAL_SWITCH_EOA]: {
195
- strategyOrBundleId: enums_1.Strategies.BaseIds.AAVE_V3_COLLATERAL_SWITCH_EOA,
196
- strategyId: enums_1.Strategies.Identifiers.EoaCollateralSwitch,
197
- protocol: exports.PROTOCOLS.AaveV3,
198
- },
199
- [enums_1.Strategies.BaseIds.AAVE_V3_DEBT_SWITCH]: {
200
- strategyOrBundleId: enums_1.Strategies.BaseIds.AAVE_V3_DEBT_SWITCH,
201
- strategyId: enums_1.Strategies.Identifiers.DebtSwitch,
202
- protocol: exports.PROTOCOLS.AaveV3,
203
- },
204
- [enums_1.Strategies.BaseIds.AAVE_V3_DEBT_SWITCH_EOA]: {
205
- strategyOrBundleId: enums_1.Strategies.BaseIds.AAVE_V3_DEBT_SWITCH_EOA,
206
- strategyId: enums_1.Strategies.Identifiers.EoaDebtSwitch,
207
- protocol: exports.PROTOCOLS.AaveV3,
208
- },
209
164
  };
210
165
  exports.ARBITRUM_STRATEGIES_INFO = {
211
166
  [enums_1.Strategies.ArbitrumIds.EXCHANGE_DCA]: {
@@ -223,21 +178,6 @@ exports.ARBITRUM_STRATEGIES_INFO = {
223
178
  strategyId: enums_1.Strategies.Identifiers.CollateralSwitch,
224
179
  protocol: exports.PROTOCOLS.AaveV3,
225
180
  },
226
- [enums_1.Strategies.ArbitrumIds.AAVE_V3_COLLATERAL_SWITCH_EOA]: {
227
- strategyOrBundleId: enums_1.Strategies.ArbitrumIds.AAVE_V3_COLLATERAL_SWITCH_EOA,
228
- strategyId: enums_1.Strategies.Identifiers.EoaCollateralSwitch,
229
- protocol: exports.PROTOCOLS.AaveV3,
230
- },
231
- [enums_1.Strategies.ArbitrumIds.AAVE_V3_DEBT_SWITCH]: {
232
- strategyOrBundleId: enums_1.Strategies.ArbitrumIds.AAVE_V3_DEBT_SWITCH,
233
- strategyId: enums_1.Strategies.Identifiers.DebtSwitch,
234
- protocol: exports.PROTOCOLS.AaveV3,
235
- },
236
- [enums_1.Strategies.ArbitrumIds.AAVE_V3_DEBT_SWITCH_EOA]: {
237
- strategyOrBundleId: enums_1.Strategies.ArbitrumIds.AAVE_V3_DEBT_SWITCH_EOA,
238
- strategyId: enums_1.Strategies.Identifiers.EoaDebtSwitch,
239
- protocol: exports.PROTOCOLS.AaveV3,
240
- },
241
181
  };
242
182
  exports.STRATEGIES_INFO = {
243
183
  [enums_1.ChainId.Ethereum]: exports.MAINNET_STRATEGIES_INFO,
@@ -0,0 +1,7 @@
1
+ import type { ApiSubscriptionRecord, ParseData } from '../types';
2
+ import type { ChainId } from '../types/enums';
3
+ /**
4
+ * @description Maps a subscription record returned by the automation API to the data
5
+ * the parser expects from Subscribe events and SubStorage (trigger data is stored without 0x prefix)
6
+ */
7
+ export declare function parseDataFromApiSubscription(record: ApiSubscriptionRecord, chainId: ChainId): ParseData;
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseDataFromApiSubscription = void 0;
4
+ function addHexPrefix(value) {
5
+ return value.startsWith('0x') ? value : `0x${value}`;
6
+ }
7
+ /**
8
+ * @description Maps a subscription record returned by the automation API to the data
9
+ * the parser expects from Subscribe events and SubStorage (trigger data is stored without 0x prefix)
10
+ */
11
+ function parseDataFromApiSubscription(record, chainId) {
12
+ return {
13
+ chainId,
14
+ blockNumber: record.block_number,
15
+ subscriptionEventData: {
16
+ subId: record.id.toString(),
17
+ proxy: record.wallet,
18
+ subHash: record.sub_data_hash,
19
+ subStruct: {
20
+ strategyOrBundleId: record.strategy_or_bundle_id.toString(),
21
+ isBundle: record.is_bundle,
22
+ triggerData: record.trigger_data.map(addHexPrefix),
23
+ subData: record.sub_data.map(addHexPrefix),
24
+ },
25
+ },
26
+ strategiesSubsData: {
27
+ userProxy: record.wallet,
28
+ isEnabled: record.is_enabled,
29
+ strategySubHash: record.sub_data_hash,
30
+ },
31
+ };
32
+ }
33
+ exports.parseDataFromApiSubscription = parseDataFromApiSubscription;
@@ -0,0 +1 @@
1
+ import '../configuration';
@@ -0,0 +1,67 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const chai_1 = require("chai");
4
+ const enums_1 = require("../types/enums");
5
+ require("../configuration");
6
+ const apiSubscriptionsService_1 = require("./apiSubscriptionsService");
7
+ const strategiesService_1 = require("./strategiesService");
8
+ describe('Feature: apiSubscriptionsService.ts', () => {
9
+ describe('When testing apiSubscriptionsService.parseDataFromApiSubscription', () => {
10
+ // same subscription as in strategiesService.test.ts, in the shape the automation API returns it
11
+ const record = {
12
+ id: 379,
13
+ wallet: '0x9cB7E19861665366011899d74E75d4F2A419aEeD',
14
+ wallet_type: 'safe',
15
+ is_enabled: true,
16
+ is_bundle: true,
17
+ strategy_or_bundle_id: 8,
18
+ strategy_ids: [34, 35],
19
+ sub_data_hash: '0xafa4d200be62f171b57b1ae0f4e8348d1ac3f6d0812ad6da74a2adae8037dde1',
20
+ trigger_data: [
21
+ '0000000000000000000000009cb7e19861665366011899d74e75d4f2a419aeed0000000000000000000000002f39d218133afab8f2b819b1066c7e434ad94e9e00000000000000000000000000000000000000000000000019ac8532c27900000000000000000000000000000000000000000000000000000000000000000001',
22
+ ],
23
+ sub_data: [
24
+ '0x0000000000000000000000000000000000000000000000001bc16d674ec80000',
25
+ '0x0000000000000000000000000000000000000000000000000000000000000001',
26
+ '0x0000000000000000000000000000000000000000000000000000000000000001',
27
+ '0x0000000000000000000000000000000000000000000000000000000000000000',
28
+ ],
29
+ block_number: 18015756,
30
+ additional_triggers: null,
31
+ };
32
+ const fromEvents = {
33
+ chainId: enums_1.ChainId.Ethereum,
34
+ blockNumber: 18015756,
35
+ subscriptionEventData: {
36
+ subId: '379',
37
+ proxy: '0x9cB7E19861665366011899d74E75d4F2A419aEeD',
38
+ subHash: '0xafa4d200be62f171b57b1ae0f4e8348d1ac3f6d0812ad6da74a2adae8037dde1',
39
+ subStruct: {
40
+ strategyOrBundleId: '8',
41
+ isBundle: true,
42
+ triggerData: [
43
+ '0x0000000000000000000000009cb7e19861665366011899d74e75d4f2a419aeed0000000000000000000000002f39d218133afab8f2b819b1066c7e434ad94e9e00000000000000000000000000000000000000000000000019ac8532c27900000000000000000000000000000000000000000000000000000000000000000001',
44
+ ],
45
+ subData: record.sub_data,
46
+ },
47
+ },
48
+ strategiesSubsData: {
49
+ userProxy: '0x9cB7E19861665366011899d74E75d4F2A419aEeD',
50
+ isEnabled: true,
51
+ strategySubHash: '0xafa4d200be62f171b57b1ae0f4e8348d1ac3f6d0812ad6da74a2adae8037dde1',
52
+ },
53
+ };
54
+ it('Given an API record should return the same parse data as Subscribe event and SubStorage would', () => {
55
+ (0, chai_1.expect)((0, apiSubscriptionsService_1.parseDataFromApiSubscription)(record, enums_1.ChainId.Ethereum)).to.eql(fromEvents);
56
+ });
57
+ it('Given an API record the parsed position should match the one parsed from events', () => {
58
+ const fromApi = (0, strategiesService_1.parseStrategiesAutomatedPosition)((0, apiSubscriptionsService_1.parseDataFromApiSubscription)(record, enums_1.ChainId.Ethereum));
59
+ (0, chai_1.expect)(fromApi).to.not.equal(null);
60
+ (0, chai_1.expect)(fromApi).to.eql((0, strategiesService_1.parseStrategiesAutomatedPosition)(fromEvents));
61
+ });
62
+ it('Given a disabled record should keep it disabled', () => {
63
+ const parseData = (0, apiSubscriptionsService_1.parseDataFromApiSubscription)(Object.assign(Object.assign({}, record), { is_enabled: false }), enums_1.ChainId.Ethereum);
64
+ (0, chai_1.expect)(parseData.strategiesSubsData.isEnabled).to.equal(false);
65
+ });
66
+ });
67
+ });
@@ -347,29 +347,12 @@ function parseAaveV3CloseOnPrice(position, parseData) {
347
347
  return _position;
348
348
  }
349
349
  function parseAaveV3CollateralSwitch(position, parseData) {
350
- const _position = (0, lodash_1.cloneDeep)(position);
351
- const { subStruct } = parseData.subscriptionEventData;
352
- const isEOA = _position.strategy.strategyId.includes('eoa');
353
- const triggerData = triggerService.aaveV3QuotePriceTrigger.decode(subStruct.triggerData);
354
- const subData = isEOA
355
- ? subDataService.aaveV3GenericFLCollateralSwitchSubData.decode(subStruct.subData)
356
- : subDataService.aaveV3CollateralSwitchSubData.decode(subStruct.subData);
357
- _position.strategyData.decoded.triggerData = triggerData;
358
- _position.strategyData.decoded.subData = subData;
359
- _position.positionId = (0, utils_1.getPositionId)(_position.chainId, _position.protocol.id, _position.owner, subData.marketAddr);
360
- _position.strategy.strategyId = isEOA
361
- ? enums_1.Strategies.Identifiers.EoaCollateralSwitch
362
- : enums_1.Strategies.Identifiers.CollateralSwitch;
363
- return _position;
364
- }
365
- function parseAaveV3DebtSwitch(position, parseData) {
366
350
  const _position = (0, lodash_1.cloneDeep)(position);
367
351
  const { subStruct } = parseData.subscriptionEventData;
368
352
  const triggerData = triggerService.aaveV3QuotePriceTrigger.decode(subStruct.triggerData);
369
- const subData = subDataService.aaveV3DebtSwitchSubData.decode(subStruct.subData);
353
+ const subData = subDataService.aaveV3CollateralSwitchSubData.decode(subStruct.subData);
370
354
  _position.strategyData.decoded.triggerData = triggerData;
371
355
  _position.strategyData.decoded.subData = subData;
372
- // TODO -> This isn't enough for ID
373
356
  _position.positionId = (0, utils_1.getPositionId)(_position.chainId, _position.protocol.id, _position.owner, subData.marketAddr);
374
357
  return _position;
375
358
  }
@@ -1263,9 +1246,6 @@ const parsingMethodsMapping = {
1263
1246
  [enums_1.Strategies.Identifiers.EoaBoostOnPrice]: parseAaveV3LeverageManagementOnPrice,
1264
1247
  [enums_1.Strategies.Identifiers.EoaCloseOnPrice]: parseAaveV3CloseOnPrice,
1265
1248
  [enums_1.Strategies.Identifiers.CollateralSwitch]: parseAaveV3CollateralSwitch,
1266
- [enums_1.Strategies.Identifiers.EoaCollateralSwitch]: parseAaveV3CollateralSwitch,
1267
- [enums_1.Strategies.Identifiers.DebtSwitch]: parseAaveV3DebtSwitch,
1268
- [enums_1.Strategies.Identifiers.EoaDebtSwitch]: parseAaveV3DebtSwitch,
1269
1249
  [enums_1.Strategies.Identifiers.LiquidationProtection]: parseAaveV3LiquidationProtection,
1270
1250
  [enums_1.Strategies.Identifiers.EoaLiquidationProtection]: parseAaveV3LiquidationProtection,
1271
1251
  },
@@ -14,98 +14,6 @@ const enums_1 = require("../types/enums");
14
14
  require("../configuration");
15
15
  const strategiesService_1 = require("./strategiesService");
16
16
  describe('Feature: strategiesService.ts', () => {
17
- describe('When parsing Aave V3 collateral switch subscriptions', () => {
18
- const proxy = '0x9cB7E19861665366011899d74E75d4F2A419aEeD';
19
- const user = '0x1234567890123456789012345678901234567890';
20
- const fromAsset = '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2';
21
- const toAsset = '0xcbB7C0000aB88B473b1f5aFd9ef808440eed33Bf';
22
- const marketAddr = '0x2f39d218133AFaB8F2B819B1066c7E434Ad94E9e';
23
- const amountToSwitch = '115792089237316195423570985008687907853269984665640564039457584007913129639935';
24
- const subHash = `0x${'ab'.repeat(32)}`;
25
- const triggerData = [
26
- '0x000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2'
27
- + '000000000000000000000000cbb7c0000ab88b473b1f5afd9ef808440eed33bf'
28
- + '0000000000000000000000000000000000000000000000000000000000989680'
29
- + '0000000000000000000000000000000000000000000000000000000000000001',
30
- ];
31
- function getParseData(strategyId, isGeneric, chainId = enums_1.ChainId.Ethereum) {
32
- const subData = [
33
- '0x000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2',
34
- isGeneric
35
- ? '0x0000000000000000000000000000000000000000000000000000000000000100'
36
- : '0x0000000000000000000000000000000000000000000000000000000000000000',
37
- '0x000000000000000000000000cbb7c0000ab88b473b1f5afd9ef808440eed33bf',
38
- isGeneric
39
- ? '0x000000000000000000000000000000000000000000000000000000000000ffff'
40
- : '0x0000000000000000000000000000000000000000000000000000000000000007',
41
- '0x0000000000000000000000002f39d218133afab8f2b819b1066c7e434ad94e9e',
42
- '0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff',
43
- isGeneric
44
- ? '0x0000000000000000000000001234567890123456789012345678901234567890'
45
- : '0x0000000000000000000000000000000000000000000000000000000000000000',
46
- ];
47
- const subStruct = Object.assign([String(strategyId), false, triggerData, subData], { strategyOrBundleId: String(strategyId), isBundle: false, triggerData, subData });
48
- return {
49
- chainId,
50
- blockNumber: 18015756,
51
- subscriptionEventData: {
52
- subId: '379', proxy, subHash, subStruct,
53
- 0: '379', 1: proxy, 2: subHash, 3: subStruct,
54
- },
55
- strategiesSubsData: {
56
- userProxy: proxy, isEnabled: true, strategySubHash: subHash,
57
- },
58
- };
59
- }
60
- const genericStrategies = [
61
- [enums_1.ChainId.Ethereum, enums_1.Strategies.MainnetIds.AAVE_V3_COLLATERAL_SWITCH_EOA],
62
- [enums_1.ChainId.Optimism, enums_1.Strategies.OptimismIds.AAVE_V3_COLLATERAL_SWITCH_EOA],
63
- [enums_1.ChainId.Arbitrum, enums_1.Strategies.ArbitrumIds.AAVE_V3_COLLATERAL_SWITCH_EOA],
64
- [enums_1.ChainId.Base, enums_1.Strategies.BaseIds.AAVE_V3_COLLATERAL_SWITCH_EOA],
65
- ];
66
- genericStrategies.forEach(([chainId, strategyId]) => {
67
- it(`decodes generic collateral switches with the user and uint16 reserve IDs on chain ${chainId}`, () => {
68
- const parseData = getParseData(strategyId, true, chainId);
69
- const position = (0, strategiesService_1.parseStrategiesAutomatedPosition)(parseData);
70
- (0, chai_1.expect)(position).not.to.equal(null);
71
- (0, chai_1.expect)(position.protocol.id).to.equal(enums_1.ProtocolIdentifiers.StrategiesAutomation.AaveV3);
72
- (0, chai_1.expect)(position.strategy.strategyId).to.equal(enums_1.Strategies.Identifiers.EoaCollateralSwitch);
73
- (0, chai_1.expect)(position.strategy.isBundle).to.equal(false);
74
- (0, chai_1.expect)(position.strategyData.encoded).to.eql({
75
- triggerData,
76
- subData: parseData.subscriptionEventData.subStruct.subData,
77
- });
78
- (0, chai_1.expect)(position.strategyData.decoded.triggerData).to.eql({
79
- baseTokenAddress: fromAsset,
80
- quoteTokenAddress: toAsset,
81
- price: '0.1',
82
- ratioState: enums_1.RatioState.UNDER,
83
- });
84
- (0, chai_1.expect)(position.strategyData.decoded.subData).to.eql({
85
- fromAsset, fromAssetId: 256, toAsset, toAssetId: 65535, marketAddr, amountToSwitch, user,
86
- });
87
- (0, chai_1.expect)(position.owner).to.equal(proxy.toLowerCase());
88
- (0, chai_1.expect)(position.positionId).to.equal(`${chainId}-aave__v3-${proxy.toLowerCase()}-${marketAddr.toLowerCase()}`);
89
- });
90
- });
91
- const legacyStrategies = [
92
- [enums_1.ChainId.Ethereum, enums_1.Strategies.MainnetIds.AAVE_V3_COLLATERAL_SWITCH],
93
- [enums_1.ChainId.Optimism, enums_1.Strategies.OptimismIds.AAVE_V3_COLLATERAL_SWITCH],
94
- [enums_1.ChainId.Arbitrum, enums_1.Strategies.ArbitrumIds.AAVE_V3_COLLATERAL_SWITCH],
95
- [enums_1.ChainId.Base, enums_1.Strategies.BaseIds.AAVE_V3_COLLATERAL_SWITCH],
96
- ];
97
- legacyStrategies.forEach(([chainId, strategyId]) => {
98
- it(`continues to decode legacy collateral switches on chain ${chainId}`, () => {
99
- const position = (0, strategiesService_1.parseStrategiesAutomatedPosition)(getParseData(strategyId, false, chainId));
100
- (0, chai_1.expect)(position).not.to.equal(null);
101
- (0, chai_1.expect)(position.strategy.strategyId).to.equal(enums_1.Strategies.Identifiers.CollateralSwitch);
102
- (0, chai_1.expect)(position.strategyData.decoded.subData).to.eql({
103
- fromAsset, fromAssetId: 0, toAsset, toAssetId: 7, marketAddr, amountToSwitch,
104
- });
105
- (0, chai_1.expect)(position.positionId).to.equal(`${chainId}-aave__v3-${proxy.toLowerCase()}-${marketAddr.toLowerCase()}`);
106
- });
107
- });
108
- });
109
17
  describe('When testing strategiesService.parseStrategiesAutomatedPosition', () => __awaiter(void 0, void 0, void 0, function* () {
110
18
  // TODO: we should probably write this for every strategy?
111
19
  const examples = [
@@ -175,7 +83,6 @@ describe('Feature: strategiesService.ts', () => {
175
83
  subId: '379',
176
84
  proxy: '0x9cb7e19861665366011899d74e75d4f2a419aeed',
177
85
  subHash: '0xafa4d200be62f171b57b1ae0f4e8348d1ac3f6d0812ad6da74a2adae8037dde1',
178
- // @ts-ignore
179
86
  subStruct: {
180
87
  strategyOrBundleId: '8',
181
88
  isBundle: true,
@@ -62,8 +62,6 @@ export declare const aaveV3Encode: {
62
62
  leverageManagementOnPriceGeneric(strategyOrBundleId: number, price: number, ratioState: RatioState, collAsset: EthereumAddress, collAssetId: number, debtAsset: EthereumAddress, debtAssetId: number, marketAddr: EthereumAddress, targetRatio: number, user: EthereumAddress): (number | boolean | string[])[];
63
63
  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[])[];
64
64
  collateralSwitch(strategyOrBundleId: number, fromAsset: EthereumAddress, fromAssetId: number, toAsset: EthereumAddress, toAssetId: number, marketAddr: EthereumAddress, amountToSwitch: string, baseTokenAddress: EthereumAddress, quoteTokenAddress: EthereumAddress, price: number, state: RatioState): (number | boolean | string[])[];
65
- collateralSwitchGeneric(strategyOrBundleId: number, fromAsset: EthereumAddress, fromAssetId: number, toAsset: EthereumAddress, toAssetId: number, marketAddr: EthereumAddress, amountToSwitch: string, user: EthereumAddress, baseTokenAddress: EthereumAddress, quoteTokenAddress: EthereumAddress, price: number, state: RatioState): (number | boolean | string[])[];
66
- debtSwitch(strategyOrBundleId: number, fromAsset: EthereumAddress, fromAssetId: number, toAsset: EthereumAddress, toAssetId: number, marketAddr: EthereumAddress, amountToSwitch: string, user: EthereumAddress, baseTokenAddress: EthereumAddress, quoteTokenAddress: EthereumAddress, price: number, state: RatioState): (number | boolean | string[])[];
67
65
  };
68
66
  export declare const compoundV2Encode: {
69
67
  leverageManagement(strategyOrBundleId: number, user: EthereumAddress, ratioState: RatioState, targetRatio: number, triggerRatio: number): (number | boolean | string[])[];
@@ -196,18 +196,6 @@ exports.aaveV3Encode = {
196
196
  const triggerDataEncoded = triggerService.aaveV3QuotePriceTrigger.encode(baseTokenAddress, quoteTokenAddress, price, state);
197
197
  return [strategyOrBundleId, isBundle, triggerDataEncoded, subDataEncoded];
198
198
  },
199
- collateralSwitchGeneric(strategyOrBundleId, fromAsset, fromAssetId, toAsset, toAssetId, marketAddr, amountToSwitch, user, baseTokenAddress, quoteTokenAddress, price, state) {
200
- const isBundle = false;
201
- const subDataEncoded = subDataService.aaveV3GenericFLCollateralSwitchSubData.encode(fromAsset, fromAssetId, toAsset, toAssetId, marketAddr, amountToSwitch, user);
202
- const triggerDataEncoded = triggerService.aaveV3QuotePriceTrigger.encode(baseTokenAddress, quoteTokenAddress, price, state);
203
- return [strategyOrBundleId, isBundle, triggerDataEncoded, subDataEncoded];
204
- },
205
- debtSwitch(strategyOrBundleId, fromAsset, fromAssetId, toAsset, toAssetId, marketAddr, amountToSwitch, user, baseTokenAddress, quoteTokenAddress, price, state) {
206
- const isBundle = false;
207
- const subDataEncoded = subDataService.aaveV3DebtSwitchSubData.encode(fromAsset, fromAssetId, toAsset, toAssetId, marketAddr, amountToSwitch, user);
208
- const triggerDataEncoded = triggerService.aaveV3QuotePriceTrigger.encode(baseTokenAddress, quoteTokenAddress, price, state);
209
- return [strategyOrBundleId, isBundle, triggerDataEncoded, subDataEncoded];
210
- },
211
199
  };
212
200
  exports.compoundV2Encode = {
213
201
  leverageManagement(strategyOrBundleId, user, ratioState, targetRatio, triggerRatio) {
@@ -856,113 +856,6 @@ describe('Feature: strategySubService.ts', () => {
856
856
  });
857
857
  });
858
858
  });
859
- describe('collateralSwitchGeneric()', () => {
860
- const strategyId = 12345;
861
- const fromAsset = web3Utils.toChecksumAddress((0, tokens_1.getAssetInfo)('WETH', enums_1.ChainId.Ethereum).address);
862
- const toAsset = web3Utils.toChecksumAddress((0, tokens_1.getAssetInfo)('USDC', enums_1.ChainId.Ethereum).address);
863
- const marketAddr = web3Utils.toChecksumAddress('0x87870Bca3F3fD6335C3F4ce8392D69d0B4161d39');
864
- const user = web3Utils.toChecksumAddress('0x1234567890abcdef1234567890abcdef12345678');
865
- const examples = [
866
- [
867
- enums_1.RatioState.UNDER,
868
- '10000000000000000000',
869
- '0x0000000000000000000000000000000000000000000000008ac7230489e80000',
870
- '0000000000000000000000000000000000000000000000000000000000000001',
871
- ],
872
- [
873
- enums_1.RatioState.OVER,
874
- tokens_1.MAXUINT,
875
- '0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff',
876
- '0000000000000000000000000000000000000000000000000000000000000000',
877
- ],
878
- ];
879
- examples.forEach(([state, amountToSwitch, encodedAmount, encodedState]) => {
880
- it(`encodes a generic collateral switch with the supplied quote pair and state ${state}`, () => {
881
- const expectedTrigger = `0x000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc200000000000000000000000000000000000000000000000000000000000003e8${encodedState}`;
882
- const expectedSubData = [
883
- '0x000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2',
884
- '0x0000000000000000000000000000000000000000000000000000000000000100',
885
- '0x000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48',
886
- '0x000000000000000000000000000000000000000000000000000000000000ffff',
887
- '0x00000000000000000000000087870bca3f3fd6335c3f4ce8392d69d0b4161d39',
888
- encodedAmount,
889
- '0x0000000000000000000000001234567890abcdef1234567890abcdef12345678',
890
- ];
891
- (0, chai_1.expect)(strategySubService_1.aaveV3Encode.collateralSwitchGeneric(strategyId, fromAsset, 256, toAsset, 65535, marketAddr, amountToSwitch, user, toAsset, fromAsset, 0.00001, state)).to.eql([strategyId, false, [expectedTrigger], expectedSubData]);
892
- });
893
- });
894
- });
895
- describe('debtSwitch()', () => {
896
- const examples = [
897
- // USDC -> DAI, price 100000, state UNDER
898
- [
899
- [
900
- enums_1.Strategies.MainnetIds.AAVE_V3_DEBT_SWITCH,
901
- false,
902
- ['0x000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb480000000000000000000000006b175474e89094c44da98b954eedeac495271d0f000000000000000000000000000000000000000000000000000009184e72a0000000000000000000000000000000000000000000000000000000000000000001'],
903
- [
904
- '0x000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48',
905
- '0x0000000000000000000000000000000000000000000000000000000000000001',
906
- '0x0000000000000000000000006b175474e89094c44da98b954eedeac495271d0f',
907
- '0x0000000000000000000000000000000000000000000000000000000000000003',
908
- '0x00000000000000000000000087870bca3f3fd6335c3f4ce8392d69d0b4161d39',
909
- '0x0000000000000000000000000000000000000000000000008ac7230489e80000',
910
- '0x000000000000000000000000dd6ecd6c9b9f7e29e0f14bb9d5f0d0a3c6cb6c6a',
911
- ],
912
- ],
913
- [
914
- enums_1.Strategies.MainnetIds.AAVE_V3_DEBT_SWITCH,
915
- web3Utils.toChecksumAddress((0, tokens_1.getAssetInfo)('USDC', enums_1.ChainId.Ethereum).address),
916
- 1,
917
- web3Utils.toChecksumAddress((0, tokens_1.getAssetInfo)('DAI', enums_1.ChainId.Ethereum).address),
918
- 3,
919
- web3Utils.toChecksumAddress('0x87870Bca3F3fD6335C3F4ce8392D69d0B4161d39'),
920
- '10000000000000000000',
921
- web3Utils.toChecksumAddress('0xDd6ecD6c9b9F7e29E0f14bb9D5F0D0A3C6cB6c6A'),
922
- web3Utils.toChecksumAddress((0, tokens_1.getAssetInfo)('USDC', enums_1.ChainId.Ethereum).address),
923
- web3Utils.toChecksumAddress((0, tokens_1.getAssetInfo)('DAI', enums_1.ChainId.Ethereum).address),
924
- 100000,
925
- enums_1.RatioState.UNDER,
926
- ]
927
- ],
928
- // DAI -> USDC (MaxUint256), price 1, state OVER
929
- [
930
- [
931
- enums_1.Strategies.MainnetIds.AAVE_V3_DEBT_SWITCH,
932
- false,
933
- ['0x0000000000000000000000006b175474e89094c44da98b954eedeac495271d0f000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb480000000000000000000000000000000000000000000000000000000005f5e1000000000000000000000000000000000000000000000000000000000000000000'],
934
- [
935
- '0x0000000000000000000000006b175474e89094c44da98b954eedeac495271d0f',
936
- '0x0000000000000000000000000000000000000000000000000000000000000003',
937
- '0x000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48',
938
- '0x0000000000000000000000000000000000000000000000000000000000000001',
939
- '0x00000000000000000000000087870bca3f3fd6335c3f4ce8392d69d0b4161d39',
940
- '0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff',
941
- '0x000000000000000000000000dd6ecd6c9b9f7e29e0f14bb9d5f0d0a3c6cb6c6a',
942
- ],
943
- ],
944
- [
945
- enums_1.Strategies.MainnetIds.AAVE_V3_DEBT_SWITCH,
946
- web3Utils.toChecksumAddress((0, tokens_1.getAssetInfo)('DAI', enums_1.ChainId.Ethereum).address),
947
- 3,
948
- web3Utils.toChecksumAddress((0, tokens_1.getAssetInfo)('USDC', enums_1.ChainId.Ethereum).address),
949
- 1,
950
- web3Utils.toChecksumAddress('0x87870Bca3F3fD6335C3F4ce8392D69d0B4161d39'),
951
- tokens_1.MAXUINT,
952
- web3Utils.toChecksumAddress('0xDd6ecD6c9b9F7e29E0f14bb9D5F0D0A3C6cB6c6A'),
953
- web3Utils.toChecksumAddress((0, tokens_1.getAssetInfo)('DAI', enums_1.ChainId.Ethereum).address),
954
- web3Utils.toChecksumAddress((0, tokens_1.getAssetInfo)('USDC', enums_1.ChainId.Ethereum).address),
955
- 1,
956
- enums_1.RatioState.OVER,
957
- ]
958
- ],
959
- ];
960
- examples.forEach(([expected, actual]) => {
961
- it(`Given ${JSON.stringify(actual)} should return expected value: ${JSON.stringify(expected)}`, () => {
962
- (0, chai_1.expect)(strategySubService_1.aaveV3Encode.debtSwitch(...actual)).to.eql(expected);
963
- });
964
- });
965
- });
966
859
  });
967
860
  describe('When testing strategySubService.compoundV3Encode', () => {
968
861
  describe('leverageManagementOnPrice()', () => {
@@ -259,30 +259,6 @@ export declare const aaveV3CollateralSwitchSubData: {
259
259
  amountToSwitch: string;
260
260
  };
261
261
  };
262
- export declare const aaveV3GenericFLCollateralSwitchSubData: {
263
- encode(fromAsset: EthereumAddress, fromAssetId: number, toAsset: EthereumAddress, toAssetId: number, marketAddr: EthereumAddress, amountToSwitch: string, user: EthereumAddress): string[];
264
- decode(subData: string[]): {
265
- fromAsset: EthereumAddress;
266
- fromAssetId: number;
267
- toAsset: EthereumAddress;
268
- toAssetId: number;
269
- marketAddr: EthereumAddress;
270
- amountToSwitch: string;
271
- user: EthereumAddress;
272
- };
273
- };
274
- export declare const aaveV3DebtSwitchSubData: {
275
- encode(fromAsset: EthereumAddress, fromAssetId: number, toAsset: EthereumAddress, toAssetId: number, marketAddr: EthereumAddress, amountToSwitch: string, user: EthereumAddress): string[];
276
- decode(subData: string[]): {
277
- fromAsset: EthereumAddress;
278
- fromAssetId: number;
279
- toAsset: EthereumAddress;
280
- toAssetId: number;
281
- marketAddr: EthereumAddress;
282
- amountToSwitch: string;
283
- user: EthereumAddress;
284
- };
285
- };
286
262
  export declare const aaveV3QuotePriceSubData: {
287
263
  encode(collAsset: EthereumAddress, collAssetId: number, debtAsset: EthereumAddress, debtAssetId: number, nullAddress?: EthereumAddress): string[];
288
264
  decode(subData: string[]): {