@defisaver/automation-sdk 3.3.22-instant-twap-close-dev → 3.3.23
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.
- package/cjs/automation/private/StrategiesAutomation.d.ts +5 -1
- package/cjs/automation/private/StrategiesAutomation.js +12 -0
- package/cjs/automation/private/StrategiesAutomation.test.js +51 -0
- package/cjs/constants/index.js +40 -40
- package/cjs/services/apiSubscriptionsService.d.ts +7 -0
- package/cjs/services/apiSubscriptionsService.js +33 -0
- package/cjs/services/apiSubscriptionsService.test.d.ts +1 -0
- package/cjs/services/apiSubscriptionsService.test.js +67 -0
- package/cjs/services/strategiesService.js +8 -21
- package/cjs/services/strategiesService.test.js +0 -63
- package/cjs/services/strategySubService.d.ts +1 -1
- package/cjs/services/strategySubService.js +6 -7
- package/cjs/services/strategySubService.test.js +71 -111
- package/cjs/services/subDataService.d.ts +10 -12
- package/cjs/services/subDataService.js +30 -36
- package/cjs/services/subDataService.test.js +104 -70
- package/cjs/types/enums.d.ts +18 -18
- package/cjs/types/enums.js +10 -11
- package/cjs/types/index.d.ts +24 -2
- package/esm/automation/private/StrategiesAutomation.d.ts +5 -1
- package/esm/automation/private/StrategiesAutomation.js +12 -0
- package/esm/automation/private/StrategiesAutomation.test.js +51 -0
- package/esm/constants/index.js +40 -40
- package/esm/services/apiSubscriptionsService.d.ts +7 -0
- package/esm/services/apiSubscriptionsService.js +29 -0
- package/esm/services/apiSubscriptionsService.test.d.ts +1 -0
- package/esm/services/apiSubscriptionsService.test.js +65 -0
- package/esm/services/strategiesService.js +8 -21
- package/esm/services/strategiesService.test.js +1 -64
- package/esm/services/strategySubService.d.ts +1 -1
- package/esm/services/strategySubService.js +6 -7
- package/esm/services/strategySubService.test.js +71 -111
- package/esm/services/subDataService.d.ts +10 -12
- package/esm/services/subDataService.js +29 -35
- package/esm/services/subDataService.test.js +104 -70
- package/esm/types/enums.d.ts +18 -18
- package/esm/types/enums.js +10 -11
- package/esm/types/index.d.ts +24 -2
- package/package.json +1 -1
- package/src/automation/private/StrategiesAutomation.test.ts +65 -1
- package/src/automation/private/StrategiesAutomation.ts +17 -0
- package/src/constants/index.ts +40 -40
- package/src/services/apiSubscriptionsService.test.ts +73 -0
- package/src/services/apiSubscriptionsService.ts +33 -0
- package/src/services/strategiesService.test.ts +1 -79
- package/src/services/strategiesService.ts +8 -27
- package/src/services/strategySubService.test.ts +84 -125
- package/src/services/strategySubService.ts +16 -20
- package/src/services/subDataService.test.ts +108 -98
- package/src/services/subDataService.ts +40 -51
- package/src/types/enums.ts +10 -11
- 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
|
});
|
package/cjs/constants/index.js
CHANGED
|
@@ -127,6 +127,16 @@ exports.MAINNET_STRATEGIES_INFO = {
|
|
|
127
127
|
strategyId: enums_1.Strategies.Identifiers.EoaCollateralSwitch,
|
|
128
128
|
protocol: exports.PROTOCOLS.Spark,
|
|
129
129
|
},
|
|
130
|
+
[enums_1.Strategies.MainnetIds.AAVE_V3_DEBT_SWITCH]: {
|
|
131
|
+
strategyOrBundleId: enums_1.Strategies.MainnetIds.AAVE_V3_DEBT_SWITCH,
|
|
132
|
+
strategyId: enums_1.Strategies.Identifiers.DebtSwitch,
|
|
133
|
+
protocol: exports.PROTOCOLS.AaveV3,
|
|
134
|
+
},
|
|
135
|
+
[enums_1.Strategies.MainnetIds.AAVE_V3_DEBT_SWITCH_EOA]: {
|
|
136
|
+
strategyOrBundleId: enums_1.Strategies.MainnetIds.AAVE_V3_DEBT_SWITCH_EOA,
|
|
137
|
+
strategyId: enums_1.Strategies.Identifiers.EoaDebtSwitch,
|
|
138
|
+
protocol: exports.PROTOCOLS.AaveV3,
|
|
139
|
+
},
|
|
130
140
|
};
|
|
131
141
|
exports.OPTIMISM_STRATEGIES_INFO = {
|
|
132
142
|
[enums_1.Strategies.OptimismIds.EXCHANGE_DCA]: {
|
|
@@ -144,6 +154,16 @@ exports.OPTIMISM_STRATEGIES_INFO = {
|
|
|
144
154
|
strategyId: enums_1.Strategies.Identifiers.CollateralSwitch,
|
|
145
155
|
protocol: exports.PROTOCOLS.AaveV3,
|
|
146
156
|
},
|
|
157
|
+
[enums_1.Strategies.OptimismIds.AAVE_V3_DEBT_SWITCH]: {
|
|
158
|
+
strategyOrBundleId: enums_1.Strategies.OptimismIds.AAVE_V3_DEBT_SWITCH,
|
|
159
|
+
strategyId: enums_1.Strategies.Identifiers.DebtSwitch,
|
|
160
|
+
protocol: exports.PROTOCOLS.AaveV3,
|
|
161
|
+
},
|
|
162
|
+
[enums_1.Strategies.OptimismIds.AAVE_V3_DEBT_SWITCH_EOA]: {
|
|
163
|
+
strategyOrBundleId: enums_1.Strategies.OptimismIds.AAVE_V3_DEBT_SWITCH_EOA,
|
|
164
|
+
strategyId: enums_1.Strategies.Identifiers.EoaDebtSwitch,
|
|
165
|
+
protocol: exports.PROTOCOLS.AaveV3,
|
|
166
|
+
},
|
|
147
167
|
};
|
|
148
168
|
exports.BASE_STRATEGIES_INFO = {
|
|
149
169
|
[enums_1.Strategies.BaseIds.EXCHANGE_DCA]: {
|
|
@@ -161,6 +181,16 @@ exports.BASE_STRATEGIES_INFO = {
|
|
|
161
181
|
strategyId: enums_1.Strategies.Identifiers.CollateralSwitch,
|
|
162
182
|
protocol: exports.PROTOCOLS.AaveV3,
|
|
163
183
|
},
|
|
184
|
+
[enums_1.Strategies.BaseIds.AAVE_V3_DEBT_SWITCH]: {
|
|
185
|
+
strategyOrBundleId: enums_1.Strategies.BaseIds.AAVE_V3_DEBT_SWITCH,
|
|
186
|
+
strategyId: enums_1.Strategies.Identifiers.DebtSwitch,
|
|
187
|
+
protocol: exports.PROTOCOLS.AaveV3,
|
|
188
|
+
},
|
|
189
|
+
[enums_1.Strategies.BaseIds.AAVE_V3_DEBT_SWITCH_EOA]: {
|
|
190
|
+
strategyOrBundleId: enums_1.Strategies.BaseIds.AAVE_V3_DEBT_SWITCH_EOA,
|
|
191
|
+
strategyId: enums_1.Strategies.Identifiers.EoaDebtSwitch,
|
|
192
|
+
protocol: exports.PROTOCOLS.AaveV3,
|
|
193
|
+
},
|
|
164
194
|
};
|
|
165
195
|
exports.ARBITRUM_STRATEGIES_INFO = {
|
|
166
196
|
[enums_1.Strategies.ArbitrumIds.EXCHANGE_DCA]: {
|
|
@@ -178,6 +208,16 @@ exports.ARBITRUM_STRATEGIES_INFO = {
|
|
|
178
208
|
strategyId: enums_1.Strategies.Identifiers.CollateralSwitch,
|
|
179
209
|
protocol: exports.PROTOCOLS.AaveV3,
|
|
180
210
|
},
|
|
211
|
+
[enums_1.Strategies.ArbitrumIds.AAVE_V3_DEBT_SWITCH]: {
|
|
212
|
+
strategyOrBundleId: enums_1.Strategies.ArbitrumIds.AAVE_V3_DEBT_SWITCH,
|
|
213
|
+
strategyId: enums_1.Strategies.Identifiers.DebtSwitch,
|
|
214
|
+
protocol: exports.PROTOCOLS.AaveV3,
|
|
215
|
+
},
|
|
216
|
+
[enums_1.Strategies.ArbitrumIds.AAVE_V3_DEBT_SWITCH_EOA]: {
|
|
217
|
+
strategyOrBundleId: enums_1.Strategies.ArbitrumIds.AAVE_V3_DEBT_SWITCH_EOA,
|
|
218
|
+
strategyId: enums_1.Strategies.Identifiers.EoaDebtSwitch,
|
|
219
|
+
protocol: exports.PROTOCOLS.AaveV3,
|
|
220
|
+
},
|
|
181
221
|
};
|
|
182
222
|
exports.STRATEGIES_INFO = {
|
|
183
223
|
[enums_1.ChainId.Ethereum]: exports.MAINNET_STRATEGIES_INFO,
|
|
@@ -659,16 +699,6 @@ exports.MAINNET_BUNDLES_INFO = {
|
|
|
659
699
|
strategyId: enums_1.Strategies.Identifiers.EoaLiquidationProtection,
|
|
660
700
|
protocol: exports.PROTOCOLS.Spark,
|
|
661
701
|
},
|
|
662
|
-
[enums_1.Bundles.MainnetIds.AAVE_V3_SW_INSTANT_CLOSE]: {
|
|
663
|
-
strategyOrBundleId: enums_1.Bundles.MainnetIds.AAVE_V3_SW_INSTANT_CLOSE,
|
|
664
|
-
strategyId: enums_1.Strategies.Identifiers.InstantCloseOnPrice,
|
|
665
|
-
protocol: exports.PROTOCOLS.AaveV3,
|
|
666
|
-
},
|
|
667
|
-
[enums_1.Bundles.MainnetIds.AAVE_V3_EOA_INSTANT_CLOSE]: {
|
|
668
|
-
strategyOrBundleId: enums_1.Bundles.MainnetIds.AAVE_V3_EOA_INSTANT_CLOSE,
|
|
669
|
-
strategyId: enums_1.Strategies.Identifiers.EoaInstantCloseOnPrice,
|
|
670
|
-
protocol: exports.PROTOCOLS.AaveV3,
|
|
671
|
-
},
|
|
672
702
|
};
|
|
673
703
|
exports.OPTIMISM_BUNDLES_INFO = {
|
|
674
704
|
[enums_1.Bundles.OptimismIds.AAVE_V3_REPAY]: {
|
|
@@ -736,16 +766,6 @@ exports.OPTIMISM_BUNDLES_INFO = {
|
|
|
736
766
|
strategyId: enums_1.Strategies.Identifiers.EoaLiquidationProtection,
|
|
737
767
|
protocol: exports.PROTOCOLS.AaveV3,
|
|
738
768
|
},
|
|
739
|
-
[enums_1.Bundles.OptimismIds.AAVE_V3_SW_INSTANT_CLOSE]: {
|
|
740
|
-
strategyOrBundleId: enums_1.Bundles.OptimismIds.AAVE_V3_SW_INSTANT_CLOSE,
|
|
741
|
-
strategyId: enums_1.Strategies.Identifiers.InstantCloseOnPrice,
|
|
742
|
-
protocol: exports.PROTOCOLS.AaveV3,
|
|
743
|
-
},
|
|
744
|
-
[enums_1.Bundles.OptimismIds.AAVE_V3_EOA_INSTANT_CLOSE]: {
|
|
745
|
-
strategyOrBundleId: enums_1.Bundles.OptimismIds.AAVE_V3_EOA_INSTANT_CLOSE,
|
|
746
|
-
strategyId: enums_1.Strategies.Identifiers.EoaInstantCloseOnPrice,
|
|
747
|
-
protocol: exports.PROTOCOLS.AaveV3,
|
|
748
|
-
},
|
|
749
769
|
};
|
|
750
770
|
exports.BASE_BUNDLES_INFO = {
|
|
751
771
|
[enums_1.Bundles.BaseIds.AAVE_V3_REPAY]: {
|
|
@@ -948,16 +968,6 @@ exports.BASE_BUNDLES_INFO = {
|
|
|
948
968
|
strategyId: enums_1.Strategies.Identifiers.EoaCloseOnPrice,
|
|
949
969
|
protocol: exports.PROTOCOLS.MorphoBlue,
|
|
950
970
|
},
|
|
951
|
-
[enums_1.Bundles.BaseIds.AAVE_V3_SW_INSTANT_CLOSE]: {
|
|
952
|
-
strategyOrBundleId: enums_1.Bundles.BaseIds.AAVE_V3_SW_INSTANT_CLOSE,
|
|
953
|
-
strategyId: enums_1.Strategies.Identifiers.InstantCloseOnPrice,
|
|
954
|
-
protocol: exports.PROTOCOLS.AaveV3,
|
|
955
|
-
},
|
|
956
|
-
[enums_1.Bundles.BaseIds.AAVE_V3_EOA_INSTANT_CLOSE]: {
|
|
957
|
-
strategyOrBundleId: enums_1.Bundles.BaseIds.AAVE_V3_EOA_INSTANT_CLOSE,
|
|
958
|
-
strategyId: enums_1.Strategies.Identifiers.EoaInstantCloseOnPrice,
|
|
959
|
-
protocol: exports.PROTOCOLS.AaveV3,
|
|
960
|
-
},
|
|
961
971
|
};
|
|
962
972
|
exports.ARBITRUM_BUNDLES_INFO = {
|
|
963
973
|
[enums_1.Bundles.ArbitrumIds.AAVE_V3_REPAY]: {
|
|
@@ -1160,16 +1170,6 @@ exports.ARBITRUM_BUNDLES_INFO = {
|
|
|
1160
1170
|
strategyId: enums_1.Strategies.Identifiers.EoaCloseOnPrice,
|
|
1161
1171
|
protocol: exports.PROTOCOLS.MorphoBlue,
|
|
1162
1172
|
},
|
|
1163
|
-
[enums_1.Bundles.ArbitrumIds.AAVE_V3_SW_INSTANT_CLOSE]: {
|
|
1164
|
-
strategyOrBundleId: enums_1.Bundles.ArbitrumIds.AAVE_V3_SW_INSTANT_CLOSE,
|
|
1165
|
-
strategyId: enums_1.Strategies.Identifiers.InstantCloseOnPrice,
|
|
1166
|
-
protocol: exports.PROTOCOLS.AaveV3,
|
|
1167
|
-
},
|
|
1168
|
-
[enums_1.Bundles.ArbitrumIds.AAVE_V3_EOA_INSTANT_CLOSE]: {
|
|
1169
|
-
strategyOrBundleId: enums_1.Bundles.ArbitrumIds.AAVE_V3_EOA_INSTANT_CLOSE,
|
|
1170
|
-
strategyId: enums_1.Strategies.Identifiers.EoaInstantCloseOnPrice,
|
|
1171
|
-
protocol: exports.PROTOCOLS.AaveV3,
|
|
1172
|
-
},
|
|
1173
1173
|
};
|
|
1174
1174
|
exports.BUNDLES_INFO = {
|
|
1175
1175
|
[enums_1.ChainId.Ethereum]: exports.MAINNET_BUNDLES_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
|
+
});
|
|
@@ -346,34 +346,21 @@ function parseAaveV3CloseOnPrice(position, parseData) {
|
|
|
346
346
|
}
|
|
347
347
|
return _position;
|
|
348
348
|
}
|
|
349
|
-
function
|
|
349
|
+
function parseAaveV3CollateralSwitch(position, parseData) {
|
|
350
350
|
const _position = (0, lodash_1.cloneDeep)(position);
|
|
351
351
|
const { subStruct } = parseData.subscriptionEventData;
|
|
352
|
-
const triggerData = triggerService.
|
|
353
|
-
const subData = subDataService.
|
|
354
|
-
const { takeProfitType, stopLossType } = (0, utils_1.getStopLossAndTakeProfitTypeByCloseStrategyType)(+subData.closeType);
|
|
355
|
-
_position.positionId = (0, utils_1.getPositionId)(_position.chainId, _position.protocol.id, _position.owner, AAVE_V3_MARKET_ADDRESSES[_position.chainId]);
|
|
352
|
+
const triggerData = triggerService.aaveV3QuotePriceTrigger.decode(subStruct.triggerData);
|
|
353
|
+
const subData = subDataService.aaveV3CollateralSwitchSubData.decode(subStruct.subData);
|
|
356
354
|
_position.strategyData.decoded.triggerData = triggerData;
|
|
357
355
|
_position.strategyData.decoded.subData = subData;
|
|
358
|
-
_position.
|
|
359
|
-
collAsset: subData.collAsset,
|
|
360
|
-
collAssetId: subData.collAssetId,
|
|
361
|
-
debtAsset: subData.debtAsset,
|
|
362
|
-
debtAssetId: subData.debtAssetId,
|
|
363
|
-
baseToken: triggerData.collToken,
|
|
364
|
-
quoteToken: triggerData.debtToken,
|
|
365
|
-
stopLossPrice: triggerData.lowerPrice,
|
|
366
|
-
takeProfitPrice: triggerData.upperPrice,
|
|
367
|
-
stopLossType,
|
|
368
|
-
takeProfitType,
|
|
369
|
-
};
|
|
356
|
+
_position.positionId = (0, utils_1.getPositionId)(_position.chainId, _position.protocol.id, _position.owner, subData.marketAddr);
|
|
370
357
|
return _position;
|
|
371
358
|
}
|
|
372
|
-
function
|
|
359
|
+
function parseAaveV3DebtSwitch(position, parseData) {
|
|
373
360
|
const _position = (0, lodash_1.cloneDeep)(position);
|
|
374
361
|
const { subStruct } = parseData.subscriptionEventData;
|
|
375
362
|
const triggerData = triggerService.aaveV3QuotePriceTrigger.decode(subStruct.triggerData);
|
|
376
|
-
const subData = subDataService.
|
|
363
|
+
const subData = subDataService.aaveV3DebtSwitchSubData.decode(subStruct.subData);
|
|
377
364
|
_position.strategyData.decoded.triggerData = triggerData;
|
|
378
365
|
_position.strategyData.decoded.subData = subData;
|
|
379
366
|
_position.positionId = (0, utils_1.getPositionId)(_position.chainId, _position.protocol.id, _position.owner, subData.marketAddr);
|
|
@@ -1268,9 +1255,9 @@ const parsingMethodsMapping = {
|
|
|
1268
1255
|
[enums_1.Strategies.Identifiers.EoaRepayOnPrice]: parseAaveV3LeverageManagementOnPrice,
|
|
1269
1256
|
[enums_1.Strategies.Identifiers.EoaBoostOnPrice]: parseAaveV3LeverageManagementOnPrice,
|
|
1270
1257
|
[enums_1.Strategies.Identifiers.EoaCloseOnPrice]: parseAaveV3CloseOnPrice,
|
|
1271
|
-
[enums_1.Strategies.Identifiers.InstantCloseOnPrice]: parseAaveV3InstantCloseOnPrice,
|
|
1272
|
-
[enums_1.Strategies.Identifiers.EoaInstantCloseOnPrice]: parseAaveV3InstantCloseOnPrice,
|
|
1273
1258
|
[enums_1.Strategies.Identifiers.CollateralSwitch]: parseAaveV3CollateralSwitch,
|
|
1259
|
+
[enums_1.Strategies.Identifiers.DebtSwitch]: parseAaveV3DebtSwitch,
|
|
1260
|
+
[enums_1.Strategies.Identifiers.EoaDebtSwitch]: parseAaveV3DebtSwitch,
|
|
1274
1261
|
[enums_1.Strategies.Identifiers.LiquidationProtection]: parseAaveV3LiquidationProtection,
|
|
1275
1262
|
[enums_1.Strategies.Identifiers.EoaLiquidationProtection]: parseAaveV3LiquidationProtection,
|
|
1276
1263
|
},
|
|
@@ -13,7 +13,6 @@ const chai_1 = require("chai");
|
|
|
13
13
|
const enums_1 = require("../types/enums");
|
|
14
14
|
require("../configuration");
|
|
15
15
|
const strategiesService_1 = require("./strategiesService");
|
|
16
|
-
const strategySubService_1 = require("./strategySubService");
|
|
17
16
|
describe('Feature: strategiesService.ts', () => {
|
|
18
17
|
describe('When testing strategiesService.parseStrategiesAutomatedPosition', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
19
18
|
// TODO: we should probably write this for every strategy?
|
|
@@ -84,7 +83,6 @@ describe('Feature: strategiesService.ts', () => {
|
|
|
84
83
|
subId: '379',
|
|
85
84
|
proxy: '0x9cb7e19861665366011899d74e75d4f2a419aeed',
|
|
86
85
|
subHash: '0xafa4d200be62f171b57b1ae0f4e8348d1ac3f6d0812ad6da74a2adae8037dde1',
|
|
87
|
-
// @ts-ignore
|
|
88
86
|
subStruct: {
|
|
89
87
|
strategyOrBundleId: '8',
|
|
90
88
|
isBundle: true,
|
|
@@ -109,65 +107,4 @@ describe('Feature: strategiesService.ts', () => {
|
|
|
109
107
|
}));
|
|
110
108
|
});
|
|
111
109
|
}));
|
|
112
|
-
describe('When parsing Aave V3 instant close bundles', () => {
|
|
113
|
-
const owner = '0x1234567890123456789012345678901234567890';
|
|
114
|
-
const market = '0x2f39d218133AFaB8F2B819B1066c7E434Ad94E9e';
|
|
115
|
-
const weth = '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2';
|
|
116
|
-
const usdc = '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48';
|
|
117
|
-
const bundles = [
|
|
118
|
-
['SW', enums_1.Bundles.MainnetIds.AAVE_V3_SW_INSTANT_CLOSE, enums_1.Strategies.Identifiers.InstantCloseOnPrice],
|
|
119
|
-
['EOA', enums_1.Bundles.MainnetIds.AAVE_V3_EOA_INSTANT_CLOSE, enums_1.Strategies.Identifiers.EoaInstantCloseOnPrice],
|
|
120
|
-
];
|
|
121
|
-
bundles.forEach(([walletType, expectedBundleId, expectedStrategyId]) => {
|
|
122
|
-
describe(`${walletType} bundle`, () => {
|
|
123
|
-
const [bundleId, isBundle, triggerData, subData] = strategySubService_1.aaveV3Encode.instantCloseOnPriceGeneric(expectedBundleId, weth, 0, usdc, 1, market, owner, '1000000000000000000', '10000000000000000', 1500, enums_1.CloseToAssetType.DEBT, 5000, enums_1.CloseToAssetType.COLLATERAL);
|
|
124
|
-
const parseData = {
|
|
125
|
-
chainId: 1,
|
|
126
|
-
blockNumber: 1,
|
|
127
|
-
subscriptionEventData: {
|
|
128
|
-
subId: '1',
|
|
129
|
-
proxy: owner,
|
|
130
|
-
subHash: '0xhash',
|
|
131
|
-
subStruct: {
|
|
132
|
-
strategyOrBundleId: String(bundleId), isBundle, triggerData, subData,
|
|
133
|
-
},
|
|
134
|
-
},
|
|
135
|
-
strategiesSubsData: { userProxy: owner, isEnabled: true, strategySubHash: '0xhash' },
|
|
136
|
-
};
|
|
137
|
-
const result = (0, strategiesService_1.parseStrategiesAutomatedPosition)(parseData);
|
|
138
|
-
it('should identify the strategy as instant close on price', () => {
|
|
139
|
-
(0, chai_1.expect)(result.strategy.strategyOrBundleId).to.equal(expectedBundleId);
|
|
140
|
-
(0, chai_1.expect)(result.strategy.strategyId).to.equal(expectedStrategyId);
|
|
141
|
-
(0, chai_1.expect)(result.protocol.id).to.equal(enums_1.ProtocolIdentifiers.StrategiesAutomation.AaveV3);
|
|
142
|
-
});
|
|
143
|
-
it('should decode sub data including tsi and slippage', () => {
|
|
144
|
-
(0, chai_1.expect)(result.strategyData.decoded.subData).to.eql({
|
|
145
|
-
collAsset: weth,
|
|
146
|
-
collAssetId: 0,
|
|
147
|
-
debtAsset: usdc,
|
|
148
|
-
debtAssetId: 1,
|
|
149
|
-
closeType: enums_1.CloseStrategyType.TAKE_PROFIT_IN_COLLATERAL_AND_STOP_LOSS_IN_DEBT,
|
|
150
|
-
marketAddr: market,
|
|
151
|
-
owner,
|
|
152
|
-
tsi: '1000000000000000000',
|
|
153
|
-
slippage: '10000000000000000',
|
|
154
|
-
});
|
|
155
|
-
});
|
|
156
|
-
it('should fill specific with prices and close types', () => {
|
|
157
|
-
(0, chai_1.expect)(result.specific).to.eql({
|
|
158
|
-
collAsset: weth,
|
|
159
|
-
collAssetId: 0,
|
|
160
|
-
debtAsset: usdc,
|
|
161
|
-
debtAssetId: 1,
|
|
162
|
-
baseToken: weth,
|
|
163
|
-
quoteToken: usdc,
|
|
164
|
-
stopLossPrice: '1500',
|
|
165
|
-
takeProfitPrice: '5000',
|
|
166
|
-
stopLossType: enums_1.CloseToAssetType.DEBT,
|
|
167
|
-
takeProfitType: enums_1.CloseToAssetType.COLLATERAL,
|
|
168
|
-
});
|
|
169
|
-
});
|
|
170
|
-
});
|
|
171
|
-
});
|
|
172
|
-
});
|
|
173
110
|
});
|
|
@@ -61,8 +61,8 @@ export declare const aaveV3Encode: {
|
|
|
61
61
|
liquidationProtection(strategyOrBundleId: number, market: EthereumAddress, user: EthereumAddress, ratioState: RatioState, targetRatio: number, triggerRatio: number): (number | boolean | string[])[];
|
|
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
|
-
instantCloseOnPriceGeneric(strategyOrBundleId: number, collAsset: EthereumAddress, collAssetId: number, debtAsset: EthereumAddress, debtAssetId: number, marketAddr: EthereumAddress, user: EthereumAddress, tsi: string, slippage: string, stopLossPrice?: number, stopLossType?: CloseToAssetType, takeProfitPrice?: number, takeProfitType?: CloseToAssetType): (number | boolean | string[])[];
|
|
65
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
|
+
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[])[];
|
|
66
66
|
};
|
|
67
67
|
export declare const compoundV2Encode: {
|
|
68
68
|
leverageManagement(strategyOrBundleId: number, user: EthereumAddress, ratioState: RatioState, targetRatio: number, triggerRatio: number): (number | boolean | string[])[];
|
|
@@ -190,19 +190,18 @@ exports.aaveV3Encode = {
|
|
|
190
190
|
const triggerDataEncoded = triggerService.aaveV3QuotePriceRangeTrigger.encode(collAsset, debtAsset, stopLossPrice, takeProfitPrice);
|
|
191
191
|
return [strategyOrBundleId, isBundle, triggerDataEncoded, subDataEncoded];
|
|
192
192
|
},
|
|
193
|
-
instantCloseOnPriceGeneric(strategyOrBundleId, collAsset, collAssetId, debtAsset, debtAssetId, marketAddr, user, tsi, slippage, stopLossPrice = 0, stopLossType = enums_1.CloseToAssetType.DEBT, takeProfitPrice = 0, takeProfitType = enums_1.CloseToAssetType.COLLATERAL) {
|
|
194
|
-
const isBundle = true;
|
|
195
|
-
const closeType = (0, utils_1.getCloseStrategyType)(stopLossPrice, stopLossType, takeProfitPrice, takeProfitType);
|
|
196
|
-
const subDataEncoded = subDataService.aaveV3InstantCloseGenericSubData.encode(collAsset, collAssetId, debtAsset, debtAssetId, closeType, marketAddr, user, tsi, slippage);
|
|
197
|
-
const triggerDataEncoded = triggerService.aaveV3QuotePriceRangeTrigger.encode(collAsset, debtAsset, stopLossPrice, takeProfitPrice);
|
|
198
|
-
return [strategyOrBundleId, isBundle, triggerDataEncoded, subDataEncoded];
|
|
199
|
-
},
|
|
200
193
|
collateralSwitch(strategyOrBundleId, fromAsset, fromAssetId, toAsset, toAssetId, marketAddr, amountToSwitch, baseTokenAddress, quoteTokenAddress, price, state) {
|
|
201
194
|
const isBundle = false;
|
|
202
195
|
const subDataEncoded = subDataService.aaveV3CollateralSwitchSubData.encode(fromAsset, fromAssetId, toAsset, toAssetId, marketAddr, amountToSwitch);
|
|
203
196
|
const triggerDataEncoded = triggerService.aaveV3QuotePriceTrigger.encode(baseTokenAddress, quoteTokenAddress, price, state);
|
|
204
197
|
return [strategyOrBundleId, isBundle, triggerDataEncoded, subDataEncoded];
|
|
205
198
|
},
|
|
199
|
+
debtSwitch(strategyOrBundleId, fromAsset, fromAssetId, toAsset, toAssetId, marketAddr, amountToSwitch, user, baseTokenAddress, quoteTokenAddress, price, state) {
|
|
200
|
+
const isBundle = false;
|
|
201
|
+
const subDataEncoded = subDataService.aaveV3DebtSwitchSubData.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
|
+
},
|
|
206
205
|
};
|
|
207
206
|
exports.compoundV2Encode = {
|
|
208
207
|
leverageManagement(strategyOrBundleId, user, ratioState, targetRatio, triggerRatio) {
|