@defisaver/automation-sdk 2.0.4 → 2.0.6
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/esm/automation/private/StrategiesAutomation.js +1 -1
- package/esm/constants/index.js +10 -0
- package/esm/services/strategiesService.js +26 -0
- package/esm/services/strategySubService.d.ts +12 -0
- package/esm/services/strategySubService.js +7 -0
- package/esm/services/triggerService.d.ts +11 -1
- package/esm/services/triggerService.js +37 -9
- package/esm/services/triggerService.test.d.ts +1 -0
- package/esm/services/triggerService.test.js +719 -0
- package/esm/types/enums.d.ts +6 -0
- package/esm/types/enums.js +6 -0
- package/esm/types/index.d.ts +12 -1
- package/package.json +2 -2
- package/src/automation/private/StrategiesAutomation.ts +1 -1
- package/src/constants/index.ts +10 -0
- package/src/services/strategiesService.ts +39 -0
- package/src/services/strategySubService.ts +22 -0
- package/src/services/triggerService.test.ts +785 -0
- package/src/services/triggerService.ts +50 -7
- package/src/types/enums.ts +6 -0
- package/src/types/index.ts +14 -1
- package/umd/index.js +172 -79
|
@@ -77,7 +77,7 @@ class StrategiesAutomation extends Automation_1.default {
|
|
|
77
77
|
let subscriptions = [];
|
|
78
78
|
if (subscriptionEvents) {
|
|
79
79
|
// @ts-ignore
|
|
80
|
-
const strategiesSubs = yield this.getStrategiesSubs(subscriptionEvents.map((e) => e.returnValues.subId), _options.
|
|
80
|
+
const strategiesSubs = yield this.getStrategiesSubs(subscriptionEvents.map((e) => e.returnValues.subId), _options.toBlock);
|
|
81
81
|
subscriptions = yield Promise.all(strategiesSubs.map((sub, index) => __awaiter(this, void 0, void 0, function* () {
|
|
82
82
|
var _a;
|
|
83
83
|
let latestUpdate = subscriptionEvents[index].returnValues;
|
package/esm/constants/index.js
CHANGED
|
@@ -193,11 +193,21 @@ exports.MAINNET_BUNDLES_INFO = {
|
|
|
193
193
|
strategyId: enums_1.Strategies.Identifiers.CloseToDebt,
|
|
194
194
|
protocol: exports.PROTOCOLS.AaveV3,
|
|
195
195
|
},
|
|
196
|
+
[enums_1.Bundles.MainnetIds.AAVE_V3_CLOSE_TO_DEBT_WITH_GAS_PRICE]: {
|
|
197
|
+
strategyOrBundleId: enums_1.Bundles.MainnetIds.AAVE_V3_CLOSE_TO_DEBT_WITH_GAS_PRICE,
|
|
198
|
+
strategyId: enums_1.Strategies.Identifiers.CloseToDebtWithGasPrice,
|
|
199
|
+
protocol: exports.PROTOCOLS.AaveV3,
|
|
200
|
+
},
|
|
196
201
|
[enums_1.Bundles.MainnetIds.AAVE_V3_CLOSE_TO_COLLATERAL]: {
|
|
197
202
|
strategyOrBundleId: enums_1.Bundles.MainnetIds.AAVE_V3_CLOSE_TO_COLLATERAL,
|
|
198
203
|
strategyId: enums_1.Strategies.Identifiers.CloseToCollateral,
|
|
199
204
|
protocol: exports.PROTOCOLS.AaveV3,
|
|
200
205
|
},
|
|
206
|
+
[enums_1.Bundles.MainnetIds.AAVE_V3_CLOSE_TO_COLLATERAL_WITH_GAS_PRICE]: {
|
|
207
|
+
strategyOrBundleId: enums_1.Bundles.MainnetIds.AAVE_V3_CLOSE_TO_COLLATERAL_WITH_GAS_PRICE,
|
|
208
|
+
strategyId: enums_1.Strategies.Identifiers.CloseToCollateralWithGasPrice,
|
|
209
|
+
protocol: exports.PROTOCOLS.AaveV3,
|
|
210
|
+
},
|
|
201
211
|
[enums_1.Bundles.MainnetIds.MORPHO_AAVE_V2_REPAY]: {
|
|
202
212
|
strategyOrBundleId: enums_1.Bundles.MainnetIds.MORPHO_AAVE_V2_REPAY,
|
|
203
213
|
strategyId: enums_1.Strategies.Identifiers.Repay,
|
|
@@ -243,6 +243,30 @@ function parseAaveV3CloseOnPrice(position, parseData) {
|
|
|
243
243
|
_position.strategy.strategyId = (0, utils_1.isRatioStateOver)(ratioState) ? enums_1.Strategies.IdOverrides.TakeProfit : enums_1.Strategies.IdOverrides.StopLoss;
|
|
244
244
|
return _position;
|
|
245
245
|
}
|
|
246
|
+
function parseAaveV3CloseOnPriceWithMaximumGasPrice(position, parseData) {
|
|
247
|
+
const _position = (0, lodash_1.cloneDeep)(position);
|
|
248
|
+
const { subStruct } = parseData.subscriptionEventData;
|
|
249
|
+
const triggerData = triggerService.aaveV3QuotePriceWithMaximumGasPriceTrigger.decode(subStruct.triggerData);
|
|
250
|
+
const subData = subDataService.aaveV3QuotePriceSubData.decode(subStruct.subData);
|
|
251
|
+
_position.strategyData.decoded.triggerData = triggerData;
|
|
252
|
+
_position.strategyData.decoded.subData = subData;
|
|
253
|
+
_position.specific = {
|
|
254
|
+
collAsset: subData.collAsset,
|
|
255
|
+
collAssetId: subData.collAssetId,
|
|
256
|
+
debtAsset: subData.debtAsset,
|
|
257
|
+
debtAssetId: subData.debtAssetId,
|
|
258
|
+
baseToken: triggerData.baseTokenAddress,
|
|
259
|
+
quoteToken: triggerData.quoteTokenAddress,
|
|
260
|
+
price: triggerData.price,
|
|
261
|
+
maximumGasPrice: triggerData.maximumGasPrice,
|
|
262
|
+
ratioState: triggerData.ratioState,
|
|
263
|
+
};
|
|
264
|
+
const { ratioState } = (0, utils_1.getRatioStateInfoForAaveCloseStrategy)(_position.specific.ratioState, (0, utils_1.wethToEthByAddress)(_position.specific.collAsset, parseData.chainId), (0, utils_1.wethToEthByAddress)(_position.specific.debtAsset, parseData.chainId), parseData.chainId);
|
|
265
|
+
_position.strategy.strategyId = (0, utils_1.isRatioStateOver)(ratioState)
|
|
266
|
+
? enums_1.Strategies.IdOverrides.TakeProfitWithGasPrice
|
|
267
|
+
: enums_1.Strategies.IdOverrides.StopLossWithGasPrice;
|
|
268
|
+
return _position;
|
|
269
|
+
}
|
|
246
270
|
function parseCompoundV2LeverageManagement(position, parseData) {
|
|
247
271
|
const _position = (0, lodash_1.cloneDeep)(position);
|
|
248
272
|
const { subStruct, subId } = parseData.subscriptionEventData;
|
|
@@ -461,7 +485,9 @@ const parsingMethodsMapping = {
|
|
|
461
485
|
[enums_1.Strategies.Identifiers.Repay]: parseAaveV3LeverageManagement,
|
|
462
486
|
[enums_1.Strategies.Identifiers.Boost]: parseAaveV3LeverageManagement,
|
|
463
487
|
[enums_1.Strategies.Identifiers.CloseToDebt]: parseAaveV3CloseOnPrice,
|
|
488
|
+
[enums_1.Strategies.Identifiers.CloseToDebtWithGasPrice]: parseAaveV3CloseOnPriceWithMaximumGasPrice,
|
|
464
489
|
[enums_1.Strategies.Identifiers.CloseToCollateral]: parseAaveV3CloseOnPrice,
|
|
490
|
+
[enums_1.Strategies.Identifiers.CloseToCollateralWithGasPrice]: parseAaveV3CloseOnPriceWithMaximumGasPrice,
|
|
465
491
|
},
|
|
466
492
|
[enums_1.ProtocolIdentifiers.StrategiesAutomation.CompoundV2]: {
|
|
467
493
|
[enums_1.Strategies.Identifiers.Repay]: parseCompoundV2LeverageManagement,
|
|
@@ -33,6 +33,18 @@ export declare const aaveV3Encode: {
|
|
|
33
33
|
debtAsset: EthereumAddress;
|
|
34
34
|
debtAssetId: number;
|
|
35
35
|
}): (number | boolean | string[])[];
|
|
36
|
+
closeToAssetWithMaximumGasPrice(strategyOrBundleId: number, isBundle: boolean | undefined, triggerData: {
|
|
37
|
+
baseTokenAddress: EthereumAddress;
|
|
38
|
+
quoteTokenAddress: EthereumAddress;
|
|
39
|
+
price: number;
|
|
40
|
+
ratioState: RatioState;
|
|
41
|
+
maximumGasPrice: number;
|
|
42
|
+
}, subData: {
|
|
43
|
+
collAsset: EthereumAddress;
|
|
44
|
+
collAssetId: number;
|
|
45
|
+
debtAsset: EthereumAddress;
|
|
46
|
+
debtAssetId: number;
|
|
47
|
+
}): (number | boolean | string[])[];
|
|
36
48
|
};
|
|
37
49
|
export declare const compoundV2Encode: {
|
|
38
50
|
leverageManagement(triggerRepayRatio: number, triggerBoostRatio: number, targetBoostRatio: number, targetRepayRatio: number, boostEnabled: boolean): string[];
|
|
@@ -152,6 +152,13 @@ exports.aaveV3Encode = {
|
|
|
152
152
|
const triggerDataEncoded = triggerService.aaveV3QuotePriceTrigger.encode(baseTokenAddress, quoteTokenAddress, price, ratioState);
|
|
153
153
|
return [strategyOrBundleId, isBundle, triggerDataEncoded, subDataEncoded];
|
|
154
154
|
},
|
|
155
|
+
closeToAssetWithMaximumGasPrice(strategyOrBundleId, isBundle = true, triggerData, subData) {
|
|
156
|
+
const { collAsset, collAssetId, debtAsset, debtAssetId, } = subData;
|
|
157
|
+
const subDataEncoded = subDataService.aaveV3QuotePriceSubData.encode(collAsset, collAssetId, debtAsset, debtAssetId);
|
|
158
|
+
const { baseTokenAddress, quoteTokenAddress, price, maximumGasPrice, ratioState, } = triggerData;
|
|
159
|
+
const triggerDataEncoded = triggerService.aaveV3QuotePriceWithMaximumGasPriceTrigger.encode(baseTokenAddress, quoteTokenAddress, price, ratioState, maximumGasPrice);
|
|
160
|
+
return [strategyOrBundleId, isBundle, triggerDataEncoded, subDataEncoded];
|
|
161
|
+
},
|
|
155
162
|
};
|
|
156
163
|
exports.compoundV2Encode = {
|
|
157
164
|
leverageManagement(triggerRepayRatio, triggerBoostRatio, targetBoostRatio, targetRepayRatio, boostEnabled) {
|
|
@@ -50,6 +50,16 @@ export declare const aaveV3QuotePriceTrigger: {
|
|
|
50
50
|
ratioState: RatioState;
|
|
51
51
|
};
|
|
52
52
|
};
|
|
53
|
+
export declare const aaveV3QuotePriceWithMaximumGasPriceTrigger: {
|
|
54
|
+
encode(baseTokenAddress: EthereumAddress, quoteTokenAddress: EthereumAddress, price: number, ratioState: RatioState, maximumGasPriceInGwei?: number): string[];
|
|
55
|
+
decode(triggerData: string[]): {
|
|
56
|
+
baseTokenAddress: EthereumAddress;
|
|
57
|
+
quoteTokenAddress: EthereumAddress;
|
|
58
|
+
price: string;
|
|
59
|
+
ratioState: RatioState;
|
|
60
|
+
maximumGasPrice: string;
|
|
61
|
+
};
|
|
62
|
+
};
|
|
53
63
|
export declare const compoundV2RatioTrigger: {
|
|
54
64
|
encode(owner: EthereumAddress, ratioPercentage: number, ratioState: RatioState): string[];
|
|
55
65
|
decode(triggerData: string[]): {
|
|
@@ -105,7 +115,7 @@ export declare const exchangeTimestampTrigger: {
|
|
|
105
115
|
};
|
|
106
116
|
};
|
|
107
117
|
export declare const exchangeOffchainPriceTrigger: {
|
|
108
|
-
encode(targetPrice: string, goodUntil: number, orderType: OrderType, fromTokenDecimals: number): string[];
|
|
118
|
+
encode(targetPrice: string, goodUntil: number, orderType: OrderType, fromTokenDecimals: number, toTokenDecimals: number): string[];
|
|
109
119
|
decode(triggerData: string[], fromTokenDecimals: number, toTokenDecimals: number): {
|
|
110
120
|
orderType: OrderType;
|
|
111
121
|
targetPrice: string;
|
|
@@ -26,8 +26,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
26
26
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
27
|
};
|
|
28
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
-
exports.curveUsdSoftLiquidationTrigger = exports.curveUsdBorrowRateTrigger = exports.sparkQuotePriceTrigger = exports.sparkRatioTrigger = exports.exchangeOffchainPriceTrigger = exports.exchangeTimestampTrigger = exports.compoundV3RatioTrigger = exports.cBondsRebondTrigger = exports.aaveV2RatioTrigger = exports.liquityDebtInFrontTrigger = exports.liquityRatioTrigger = exports.compoundV2RatioTrigger = exports.aaveV3QuotePriceTrigger = exports.morphoAaveV2RatioTrigger = exports.aaveV3RatioTrigger = exports.makerRatioTrigger = exports.trailingStopTrigger = exports.chainlinkPriceTrigger = void 0;
|
|
29
|
+
exports.curveUsdSoftLiquidationTrigger = exports.curveUsdBorrowRateTrigger = exports.sparkQuotePriceTrigger = exports.sparkRatioTrigger = exports.exchangeOffchainPriceTrigger = exports.exchangeTimestampTrigger = exports.compoundV3RatioTrigger = exports.cBondsRebondTrigger = exports.aaveV2RatioTrigger = exports.liquityDebtInFrontTrigger = exports.liquityRatioTrigger = exports.compoundV2RatioTrigger = exports.aaveV3QuotePriceWithMaximumGasPriceTrigger = exports.aaveV3QuotePriceTrigger = exports.morphoAaveV2RatioTrigger = exports.aaveV3RatioTrigger = exports.makerRatioTrigger = exports.trailingStopTrigger = exports.chainlinkPriceTrigger = void 0;
|
|
30
30
|
const decimal_js_1 = __importDefault(require("decimal.js"));
|
|
31
|
+
const tokens_1 = require("@defisaver/tokens");
|
|
31
32
|
const web3_eth_abi_1 = __importDefault(require("web3-eth-abi"));
|
|
32
33
|
const web3Utils = __importStar(require("web3-utils"));
|
|
33
34
|
const utils_1 = require("./utils");
|
|
@@ -109,6 +110,33 @@ exports.aaveV3QuotePriceTrigger = {
|
|
|
109
110
|
};
|
|
110
111
|
},
|
|
111
112
|
};
|
|
113
|
+
exports.aaveV3QuotePriceWithMaximumGasPriceTrigger = {
|
|
114
|
+
encode(baseTokenAddress, quoteTokenAddress, price, ratioState, maximumGasPriceInGwei) {
|
|
115
|
+
// Price is always in 8 decimals
|
|
116
|
+
const _price = new decimal_js_1.default(price.toString()).mul(Math.pow(10, 8)).floor().toString();
|
|
117
|
+
const _maximumGasPrice = maximumGasPriceInGwei
|
|
118
|
+
? new decimal_js_1.default(maximumGasPriceInGwei.toString()).mul(Math.pow(10, 9)).floor().toString() // We convert it to WEI
|
|
119
|
+
: tokens_1.MAXUINT; // If undefined than set to MAXUINT
|
|
120
|
+
return [
|
|
121
|
+
web3_eth_abi_1.default.encodeParameters(['address', 'address', 'uint256', 'uint8'], [baseTokenAddress, quoteTokenAddress, _price, ratioState]),
|
|
122
|
+
web3_eth_abi_1.default.encodeParameters(['uint256'], [_maximumGasPrice]),
|
|
123
|
+
];
|
|
124
|
+
},
|
|
125
|
+
decode(triggerData) {
|
|
126
|
+
const decodedPriceTrigger = web3_eth_abi_1.default.decodeParameters(['address', 'address', 'uint256', 'uint8'], triggerData[0]);
|
|
127
|
+
const decodedMaximumGasPriceTrigger = web3_eth_abi_1.default.decodeParameters(['uint256'], triggerData[1]);
|
|
128
|
+
// Price is always in 8 decimals
|
|
129
|
+
const price = new decimal_js_1.default(decodedPriceTrigger[2]).div(Math.pow(10, 8)).toDP(8).toString();
|
|
130
|
+
const maximumGasPrice = new decimal_js_1.default(decodedMaximumGasPriceTrigger[0]).div(Math.pow(10, 9)).toDP(9).toString();
|
|
131
|
+
return {
|
|
132
|
+
baseTokenAddress: decodedPriceTrigger[0],
|
|
133
|
+
quoteTokenAddress: decodedPriceTrigger[1],
|
|
134
|
+
price,
|
|
135
|
+
ratioState: +decodedPriceTrigger[3],
|
|
136
|
+
maximumGasPrice,
|
|
137
|
+
};
|
|
138
|
+
},
|
|
139
|
+
};
|
|
112
140
|
exports.compoundV2RatioTrigger = {
|
|
113
141
|
encode(owner, ratioPercentage, ratioState) {
|
|
114
142
|
const ratioWei = (0, utils_1.ratioPercentageToWei)(ratioPercentage);
|
|
@@ -197,16 +225,16 @@ exports.exchangeTimestampTrigger = {
|
|
|
197
225
|
decode(triggerData) {
|
|
198
226
|
const decodedData = web3_eth_abi_1.default.decodeParameters(['uint256', 'uint256'], triggerData[0]);
|
|
199
227
|
return {
|
|
200
|
-
timestamp: decodedData[0],
|
|
201
|
-
interval: decodedData[1],
|
|
228
|
+
timestamp: Number(decodedData[0]),
|
|
229
|
+
interval: Number(decodedData[1]),
|
|
202
230
|
};
|
|
203
231
|
},
|
|
204
232
|
};
|
|
205
233
|
exports.exchangeOffchainPriceTrigger = {
|
|
206
|
-
encode(targetPrice, goodUntil, orderType, fromTokenDecimals) {
|
|
207
|
-
const
|
|
208
|
-
const
|
|
209
|
-
return [web3_eth_abi_1.default.encodeParameters(['uint256', 'uint256'], [price,
|
|
234
|
+
encode(targetPrice, goodUntil, orderType, fromTokenDecimals, toTokenDecimals) {
|
|
235
|
+
const decimals = new decimal_js_1.default(toTokenDecimals).plus(18).minus(fromTokenDecimals).toNumber();
|
|
236
|
+
const price = new decimal_js_1.default(targetPrice.toString()).mul(Math.pow(10, decimals)).floor().toString();
|
|
237
|
+
return [web3_eth_abi_1.default.encodeParameters(['uint256', 'uint256', 'uint8'], [price, goodUntil, orderType])];
|
|
210
238
|
},
|
|
211
239
|
decode(triggerData, fromTokenDecimals, toTokenDecimals) {
|
|
212
240
|
const decodedData = web3_eth_abi_1.default.decodeParameters(['uint256', 'uint256', 'uint8'], triggerData[0]);
|
|
@@ -214,7 +242,7 @@ exports.exchangeOffchainPriceTrigger = {
|
|
|
214
242
|
const price = new decimal_js_1.default(decodedData[0]).div(new decimal_js_1.default(10).pow(decimals)).toDP(fromTokenDecimals).toString();
|
|
215
243
|
return {
|
|
216
244
|
targetPrice: price,
|
|
217
|
-
goodUntil: decodedData[1],
|
|
245
|
+
goodUntil: +decodedData[1],
|
|
218
246
|
orderType: +decodedData[2],
|
|
219
247
|
};
|
|
220
248
|
},
|
|
@@ -263,7 +291,7 @@ exports.curveUsdBorrowRateTrigger = {
|
|
|
263
291
|
},
|
|
264
292
|
decode(triggerData) {
|
|
265
293
|
const decodedData = web3_eth_abi_1.default.decodeParameters(['address', 'uint256', 'uint8'], triggerData[0]);
|
|
266
|
-
const rateEth =
|
|
294
|
+
const rateEth = web3Utils.fromWei(decodedData[1], 'ether');
|
|
267
295
|
// the form is x = (e**(rate*365*86400))-1 where x*100 is number in %
|
|
268
296
|
const exponentRate = new decimal_js_1.default(rateEth).mul(365).mul(86400);
|
|
269
297
|
const targetRate = new decimal_js_1.default(new decimal_js_1.default(2.718281828459).pow(exponentRate).minus(1)).mul(100)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|