@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
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import Dec from 'decimal.js';
|
|
2
|
+
import { MAXUINT } from '@defisaver/tokens';
|
|
2
3
|
import AbiCoder from 'web3-eth-abi';
|
|
3
4
|
import * as web3Utils from 'web3-utils';
|
|
4
5
|
|
|
@@ -100,6 +101,46 @@ export const aaveV3QuotePriceTrigger = {
|
|
|
100
101
|
},
|
|
101
102
|
};
|
|
102
103
|
|
|
104
|
+
export const aaveV3QuotePriceWithMaximumGasPriceTrigger = {
|
|
105
|
+
encode(
|
|
106
|
+
baseTokenAddress: EthereumAddress,
|
|
107
|
+
quoteTokenAddress: EthereumAddress,
|
|
108
|
+
price: number,
|
|
109
|
+
ratioState: RatioState,
|
|
110
|
+
maximumGasPriceInGwei?: number,
|
|
111
|
+
) {
|
|
112
|
+
// Price is always in 8 decimals
|
|
113
|
+
const _price = new Dec(price.toString()).mul(10 ** 8).floor().toString();
|
|
114
|
+
|
|
115
|
+
const _maximumGasPrice = maximumGasPriceInGwei
|
|
116
|
+
? new Dec(maximumGasPriceInGwei.toString()).mul(10 ** 9).floor().toString() // We convert it to WEI
|
|
117
|
+
: MAXUINT; // If undefined than set to MAXUINT
|
|
118
|
+
|
|
119
|
+
return [
|
|
120
|
+
AbiCoder.encodeParameters(['address', 'address', 'uint256', 'uint8'], [baseTokenAddress, quoteTokenAddress, _price, ratioState]),
|
|
121
|
+
AbiCoder.encodeParameters(['uint256'], [_maximumGasPrice]),
|
|
122
|
+
];
|
|
123
|
+
},
|
|
124
|
+
decode(
|
|
125
|
+
triggerData: TriggerData,
|
|
126
|
+
): { baseTokenAddress: EthereumAddress, quoteTokenAddress: EthereumAddress, price: string, ratioState: RatioState, maximumGasPrice: string } {
|
|
127
|
+
const decodedPriceTrigger = AbiCoder.decodeParameters(['address', 'address', 'uint256', 'uint8'], triggerData[0]) as Array<string>;
|
|
128
|
+
const decodedMaximumGasPriceTrigger = AbiCoder.decodeParameters(['uint256'], triggerData[1]) as Array<string>;
|
|
129
|
+
// Price is always in 8 decimals
|
|
130
|
+
const price = new Dec(decodedPriceTrigger[2]).div(10 ** 8).toDP(8).toString();
|
|
131
|
+
|
|
132
|
+
const maximumGasPrice = new Dec(decodedMaximumGasPriceTrigger[0]).div(10 ** 9).toDP(9).toString();
|
|
133
|
+
|
|
134
|
+
return {
|
|
135
|
+
baseTokenAddress: decodedPriceTrigger[0],
|
|
136
|
+
quoteTokenAddress: decodedPriceTrigger[1],
|
|
137
|
+
price,
|
|
138
|
+
ratioState: +decodedPriceTrigger[3],
|
|
139
|
+
maximumGasPrice,
|
|
140
|
+
};
|
|
141
|
+
},
|
|
142
|
+
};
|
|
143
|
+
|
|
103
144
|
export const compoundV2RatioTrigger = {
|
|
104
145
|
encode(owner: EthereumAddress, ratioPercentage: number, ratioState: RatioState) {
|
|
105
146
|
const ratioWei = ratioPercentageToWei(ratioPercentage);
|
|
@@ -206,21 +247,23 @@ export const exchangeTimestampTrigger = {
|
|
|
206
247
|
): { timestamp: number, interval: number } {
|
|
207
248
|
const decodedData = AbiCoder.decodeParameters(['uint256', 'uint256'], triggerData[0]);
|
|
208
249
|
return {
|
|
209
|
-
timestamp: decodedData[0] as number,
|
|
210
|
-
interval: decodedData[1] as number,
|
|
250
|
+
timestamp: Number(decodedData[0]) as number,
|
|
251
|
+
interval: Number(decodedData[1]) as number,
|
|
211
252
|
};
|
|
212
253
|
},
|
|
213
254
|
};
|
|
255
|
+
|
|
214
256
|
export const exchangeOffchainPriceTrigger = {
|
|
215
257
|
encode(
|
|
216
258
|
targetPrice: string,
|
|
217
259
|
goodUntil: number,
|
|
218
260
|
orderType: OrderType,
|
|
219
261
|
fromTokenDecimals: number,
|
|
262
|
+
toTokenDecimals: number,
|
|
220
263
|
) {
|
|
221
|
-
const
|
|
222
|
-
const
|
|
223
|
-
return [AbiCoder.encodeParameters(['uint256', 'uint256'], [price,
|
|
264
|
+
const decimals = new Dec(toTokenDecimals).plus(18).minus(fromTokenDecimals).toNumber();
|
|
265
|
+
const price = new Dec(targetPrice.toString()).mul(10 ** decimals).floor().toString();
|
|
266
|
+
return [AbiCoder.encodeParameters(['uint256', 'uint256', 'uint8'], [price, goodUntil, orderType])];
|
|
224
267
|
},
|
|
225
268
|
decode(
|
|
226
269
|
triggerData: TriggerData,
|
|
@@ -232,7 +275,7 @@ export const exchangeOffchainPriceTrigger = {
|
|
|
232
275
|
const price = new Dec(decodedData[0] as string).div(new Dec(10).pow(decimals)).toDP(fromTokenDecimals).toString();
|
|
233
276
|
return {
|
|
234
277
|
targetPrice: price,
|
|
235
|
-
goodUntil: decodedData[1],
|
|
278
|
+
goodUntil: +decodedData[1],
|
|
236
279
|
orderType: +decodedData[2]!,
|
|
237
280
|
};
|
|
238
281
|
},
|
|
@@ -298,7 +341,7 @@ export const curveUsdBorrowRateTrigger = {
|
|
|
298
341
|
triggerData: TriggerData,
|
|
299
342
|
): { market: EthereumAddress, targetRate: string, rateState: RatioState } {
|
|
300
343
|
const decodedData = AbiCoder.decodeParameters(['address', 'uint256', 'uint8'], triggerData[0]);
|
|
301
|
-
const rateEth =
|
|
344
|
+
const rateEth = web3Utils.fromWei(decodedData[1] as string, 'ether');
|
|
302
345
|
|
|
303
346
|
// the form is x = (e**(rate*365*86400))-1 where x*100 is number in %
|
|
304
347
|
const exponentRate = new Dec(rateEth).mul(365).mul(86400);
|
package/src/types/enums.ts
CHANGED
|
@@ -83,7 +83,9 @@ export namespace Strategies {
|
|
|
83
83
|
Boost = 'boost',
|
|
84
84
|
EoaBoost = 'eoa-boost',
|
|
85
85
|
CloseToDebt = 'close-to-debt',
|
|
86
|
+
CloseToDebtWithGasPrice = 'close-to-debt-with-gas-price',
|
|
86
87
|
CloseToCollateral = 'close-to-collateral',
|
|
88
|
+
CloseToCollateralWithGasPrice = 'close-to-collateral-with-gas-price',
|
|
87
89
|
CloseOnPriceToDebt = 'close-on-price-to-debt',
|
|
88
90
|
CloseOnPriceToColl = 'close-on-price-to-collateral',
|
|
89
91
|
TrailingStopToColl = 'trailing-stop-to-collateral',
|
|
@@ -96,6 +98,8 @@ export namespace Strategies {
|
|
|
96
98
|
export enum IdOverrides {
|
|
97
99
|
TakeProfit = 'take-profit',
|
|
98
100
|
StopLoss = 'stop-loss',
|
|
101
|
+
TakeProfitWithGasPrice = 'take-profit-with-gas-price',
|
|
102
|
+
StopLossWithGasPrice = 'stop-loss-with-gas-price',
|
|
99
103
|
TrailingStop = 'trailing-stop',
|
|
100
104
|
LeverageManagement = 'leverage-management',
|
|
101
105
|
EoaLeverageManagement = 'leverage-management-eoa',
|
|
@@ -117,7 +121,9 @@ export namespace Bundles {
|
|
|
117
121
|
MAKER_REPAY = 10,
|
|
118
122
|
MAKER_BOOST = 11,
|
|
119
123
|
AAVE_V3_CLOSE_TO_DEBT = 12,
|
|
124
|
+
AAVE_V3_CLOSE_TO_DEBT_WITH_GAS_PRICE = 24,
|
|
120
125
|
AAVE_V3_CLOSE_TO_COLLATERAL = 13,
|
|
126
|
+
AAVE_V3_CLOSE_TO_COLLATERAL_WITH_GAS_PRICE = 25,
|
|
121
127
|
MORPHO_AAVE_V2_REPAY = 14,
|
|
122
128
|
MORPHO_AAVE_V2_BOOST = 15,
|
|
123
129
|
LIQUITY_REPAY = 16,
|
package/src/types/index.ts
CHANGED
|
@@ -140,6 +140,19 @@ export declare namespace Position {
|
|
|
140
140
|
price: string,
|
|
141
141
|
ratioState: RatioState,
|
|
142
142
|
}
|
|
143
|
+
|
|
144
|
+
interface CloseOnPriceWithMaximumGasPriceAave extends Base {
|
|
145
|
+
collAsset: EthereumAddress,
|
|
146
|
+
collAssetId: number,
|
|
147
|
+
debtAsset: EthereumAddress,
|
|
148
|
+
debtAssetId: number,
|
|
149
|
+
baseToken: EthereumAddress,
|
|
150
|
+
quoteToken: EthereumAddress,
|
|
151
|
+
price: string,
|
|
152
|
+
maximumGasPrice: string,
|
|
153
|
+
ratioState: RatioState,
|
|
154
|
+
}
|
|
155
|
+
|
|
143
156
|
interface TrailingStop extends Base {
|
|
144
157
|
roundId: number,
|
|
145
158
|
triggerPercentage: number,
|
|
@@ -147,7 +160,7 @@ export declare namespace Position {
|
|
|
147
160
|
}
|
|
148
161
|
}
|
|
149
162
|
|
|
150
|
-
type SpecificAny = Specific.CloseOnPrice | Specific.TrailingStop | Specific.RatioProtection | Specific.CloseOnPriceAave;
|
|
163
|
+
type SpecificAny = Specific.CloseOnPrice | Specific.TrailingStop | Specific.RatioProtection | Specific.CloseOnPriceAave | Specific.CloseOnPriceWithMaximumGasPriceAave;
|
|
151
164
|
|
|
152
165
|
export interface Automated {
|
|
153
166
|
chainId: ChainId,
|