@defisaver/automation-sdk 3.1.5 → 3.1.6-fluid-dev
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 +2 -2
- package/cjs/automation/public/Strategies.test.d.ts +1 -0
- package/cjs/automation/public/Strategies.test.js +61 -0
- package/cjs/constants/index.js +35 -0
- package/cjs/services/strategiesService.js +123 -0
- package/cjs/services/strategySubService.d.ts +9 -1
- package/cjs/services/strategySubService.js +30 -1
- package/cjs/services/subDataService.d.ts +43 -1
- package/cjs/services/subDataService.js +143 -1
- package/cjs/services/triggerService.d.ts +33 -0
- package/cjs/services/triggerService.js +63 -1
- package/cjs/services/utils.d.ts +6 -1
- package/cjs/services/utils.js +52 -1
- package/cjs/types/enums.d.ts +34 -2
- package/cjs/types/enums.js +37 -1
- package/cjs/types/index.d.ts +12 -3
- package/esm/automation/private/StrategiesAutomation.d.ts +2 -2
- package/esm/automation/public/Strategies.test.d.ts +1 -0
- package/esm/automation/public/Strategies.test.js +56 -0
- package/esm/constants/index.js +35 -0
- package/esm/services/strategiesService.js +124 -1
- package/esm/services/strategySubService.d.ts +9 -1
- package/esm/services/strategySubService.js +31 -2
- package/esm/services/subDataService.d.ts +43 -1
- package/esm/services/subDataService.js +143 -1
- package/esm/services/triggerService.d.ts +33 -0
- package/esm/services/triggerService.js +62 -0
- package/esm/services/utils.d.ts +6 -1
- package/esm/services/utils.js +50 -1
- package/esm/types/enums.d.ts +34 -2
- package/esm/types/enums.js +36 -0
- package/esm/types/index.d.ts +12 -3
- package/package.json +3 -3
- package/src/automation/private/StrategiesAutomation.ts +2 -2
- package/src/automation/public/Strategies.test.ts +49 -0
- package/src/constants/index.ts +35 -0
- package/src/services/strategiesService.ts +159 -1
- package/src/services/strategySubService.ts +80 -1
- package/src/services/subDataService.ts +202 -2
- package/src/services/triggerService.ts +96 -0
- package/src/services/utils.ts +60 -1
- package/src/types/enums.ts +36 -0
- package/src/types/index.ts +13 -1
- package/umd/index.js +34219 -0
|
@@ -448,6 +448,81 @@ export const morphoBlueRatioTrigger = {
|
|
|
448
448
|
},
|
|
449
449
|
};
|
|
450
450
|
|
|
451
|
+
export const liquityV2RatioTrigger = {
|
|
452
|
+
encode(
|
|
453
|
+
market: EthereumAddress,
|
|
454
|
+
troveId: string,
|
|
455
|
+
ratioPercentage: number,
|
|
456
|
+
ratioState: RatioState,
|
|
457
|
+
) {
|
|
458
|
+
const ratioWei = ratioPercentageToWei(ratioPercentage);
|
|
459
|
+
return [AbiCoder.encodeParameters(['address', 'uint256', 'uint256', 'uint8'], [market, troveId, ratioWei, ratioState])];
|
|
460
|
+
},
|
|
461
|
+
decode(
|
|
462
|
+
triggerData: TriggerData,
|
|
463
|
+
) {
|
|
464
|
+
const decodedData = AbiCoder.decodeParameters(['address', 'uint256', 'uint256', 'uint8'], triggerData[0]);
|
|
465
|
+
return {
|
|
466
|
+
market: decodedData[0] as EthereumAddress,
|
|
467
|
+
troveId: decodedData[1] as string,
|
|
468
|
+
ratio: weiToRatioPercentage(decodedData[2] as string),
|
|
469
|
+
ratioState: Number(decodedData[3]),
|
|
470
|
+
};
|
|
471
|
+
},
|
|
472
|
+
};
|
|
473
|
+
|
|
474
|
+
export const liquityV2QuotePriceTrigger = {
|
|
475
|
+
encode(
|
|
476
|
+
market: EthereumAddress,
|
|
477
|
+
price: number,
|
|
478
|
+
ratioState: RatioState,
|
|
479
|
+
) {
|
|
480
|
+
// Price is always in 18 decimals
|
|
481
|
+
const _price = new Dec(price.toString()).mul(10 ** 18).floor().toString();
|
|
482
|
+
return [AbiCoder.encodeParameters(['address', 'uint256', 'uint8'], [market, _price, ratioState])];
|
|
483
|
+
},
|
|
484
|
+
decode(
|
|
485
|
+
triggerData: TriggerData,
|
|
486
|
+
) {
|
|
487
|
+
const decodedData = AbiCoder.decodeParameters(['address', 'uint256', 'uint8'], triggerData[0]);
|
|
488
|
+
const price = new Dec(decodedData[1] as string).div(10 ** 18).toDP(18).toString();
|
|
489
|
+
return {
|
|
490
|
+
market: decodedData[0] as EthereumAddress,
|
|
491
|
+
price,
|
|
492
|
+
ratioState: Number(decodedData[2]),
|
|
493
|
+
};
|
|
494
|
+
},
|
|
495
|
+
};
|
|
496
|
+
|
|
497
|
+
export const closePriceTrigger = {
|
|
498
|
+
encode(
|
|
499
|
+
tokenAddr: EthereumAddress,
|
|
500
|
+
lowerPrice: number,
|
|
501
|
+
upperPrice: number,
|
|
502
|
+
) {
|
|
503
|
+
const lowerPriceFormatted = new Dec(lowerPrice).mul(1e8).floor().toString();
|
|
504
|
+
const upperPriceFormatted = new Dec(upperPrice).mul(1e8).floor().toString();
|
|
505
|
+
|
|
506
|
+
return [
|
|
507
|
+
AbiCoder.encodeParameters(
|
|
508
|
+
['address', 'uint256', 'uint256'],
|
|
509
|
+
[tokenAddr, lowerPriceFormatted, upperPriceFormatted],
|
|
510
|
+
),
|
|
511
|
+
];
|
|
512
|
+
},
|
|
513
|
+
decode(triggerData: TriggerData): {
|
|
514
|
+
tokenAddr: EthereumAddress,
|
|
515
|
+
lowerPrice: string,
|
|
516
|
+
upperPrice: string,
|
|
517
|
+
} {
|
|
518
|
+
const decodedData = AbiCoder.decodeParameters(['address', 'uint256', 'uint256'], triggerData[0]);
|
|
519
|
+
return {
|
|
520
|
+
tokenAddr: decodedData[0] as EthereumAddress,
|
|
521
|
+
lowerPrice: new Dec(decodedData[1] as string).div(1e8).toString(),
|
|
522
|
+
upperPrice: new Dec(decodedData[2] as string).div(1e8).toString(),
|
|
523
|
+
};
|
|
524
|
+
},
|
|
525
|
+
};
|
|
451
526
|
export const morphoBluePriceTrigger = {
|
|
452
527
|
encode(
|
|
453
528
|
oracle: EthereumAddress,
|
|
@@ -476,3 +551,24 @@ export const morphoBluePriceTrigger = {
|
|
|
476
551
|
};
|
|
477
552
|
},
|
|
478
553
|
};
|
|
554
|
+
|
|
555
|
+
export const fluidRatioTrigger = {
|
|
556
|
+
encode(
|
|
557
|
+
nftId: string,
|
|
558
|
+
ratioPercentage: number,
|
|
559
|
+
ratioState: RatioState,
|
|
560
|
+
) {
|
|
561
|
+
const ratioWei = ratioPercentageToWei(ratioPercentage);
|
|
562
|
+
return [AbiCoder.encodeParameters(['uint256', 'uint256', 'uint8'], [nftId, ratioWei, ratioState])];
|
|
563
|
+
},
|
|
564
|
+
decode(
|
|
565
|
+
triggerData: TriggerData,
|
|
566
|
+
) {
|
|
567
|
+
const decodedData = AbiCoder.decodeParameters(['uint256', 'uint256', 'uint8'], triggerData[0]);
|
|
568
|
+
return {
|
|
569
|
+
nftId: decodedData[0] as string,
|
|
570
|
+
ratio: weiToRatioPercentage(decodedData[1] as string),
|
|
571
|
+
ratioState: Number(decodedData[2]),
|
|
572
|
+
};
|
|
573
|
+
},
|
|
574
|
+
};
|
package/src/services/utils.ts
CHANGED
|
@@ -4,7 +4,9 @@ import AbiCoder from 'web3-eth-abi';
|
|
|
4
4
|
import { getAssetInfo, getAssetInfoByAddress } from '@defisaver/tokens';
|
|
5
5
|
|
|
6
6
|
import type { EthereumAddress } from '../types';
|
|
7
|
-
import {
|
|
7
|
+
import {
|
|
8
|
+
ChainId, CloseStrategyType, CloseToAssetType, RatioState,
|
|
9
|
+
} from '../types/enums';
|
|
8
10
|
|
|
9
11
|
export function isDefined<T>(value: T): value is NonNullable<T> {
|
|
10
12
|
return value !== undefined && value !== null;
|
|
@@ -101,4 +103,61 @@ export function getRatioStateInfoForAaveCloseStrategy(
|
|
|
101
103
|
|
|
102
104
|
export function getPositionId(...args: (number | string)[]) {
|
|
103
105
|
return args.map(arg => arg.toString().toLowerCase().split(' ').join('_')).join('-');
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export function getCloseStrategyType(
|
|
109
|
+
stopLossPrice: number,
|
|
110
|
+
stopLossType: CloseToAssetType,
|
|
111
|
+
takeProfitPrice: number,
|
|
112
|
+
takeProfitType: CloseToAssetType,
|
|
113
|
+
): CloseStrategyType {
|
|
114
|
+
const isStopLoss = stopLossPrice > 0;
|
|
115
|
+
const isTakeProfit = takeProfitPrice > 0;
|
|
116
|
+
|
|
117
|
+
if (!isStopLoss && !isTakeProfit) {
|
|
118
|
+
throw new Error('CloseOnPrice: At least one price must be defined');
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if (isStopLoss && isTakeProfit) {
|
|
122
|
+
if (stopLossType === CloseToAssetType.COLLATERAL && takeProfitType === CloseToAssetType.COLLATERAL) {
|
|
123
|
+
return CloseStrategyType.TAKE_PROFIT_AND_STOP_LOSS_IN_COLLATERAL;
|
|
124
|
+
} if (stopLossType === CloseToAssetType.COLLATERAL) {
|
|
125
|
+
return CloseStrategyType.TAKE_PROFIT_IN_DEBT_AND_STOP_LOSS_IN_COLLATERAL;
|
|
126
|
+
} if (takeProfitType === CloseToAssetType.COLLATERAL) {
|
|
127
|
+
return CloseStrategyType.TAKE_PROFIT_IN_COLLATERAL_AND_STOP_LOSS_IN_DEBT;
|
|
128
|
+
}
|
|
129
|
+
return CloseStrategyType.TAKE_PROFIT_AND_STOP_LOSS_IN_DEBT;
|
|
130
|
+
} if (isStopLoss) {
|
|
131
|
+
return stopLossType === CloseToAssetType.COLLATERAL
|
|
132
|
+
? CloseStrategyType.STOP_LOSS_IN_COLLATERAL
|
|
133
|
+
: CloseStrategyType.STOP_LOSS_IN_DEBT;
|
|
134
|
+
}
|
|
135
|
+
return takeProfitType === CloseToAssetType.COLLATERAL
|
|
136
|
+
? CloseStrategyType.TAKE_PROFIT_IN_COLLATERAL
|
|
137
|
+
: CloseStrategyType.TAKE_PROFIT_IN_DEBT;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export function getStopLossAndTakeProfitTypeByCloseStrategyType(
|
|
141
|
+
closeStrategyType: CloseStrategyType,
|
|
142
|
+
): { stopLossType: CloseToAssetType | undefined, takeProfitType: CloseToAssetType | undefined } {
|
|
143
|
+
switch (closeStrategyType) {
|
|
144
|
+
case CloseStrategyType.STOP_LOSS_IN_COLLATERAL:
|
|
145
|
+
return { stopLossType: CloseToAssetType.COLLATERAL, takeProfitType: undefined };
|
|
146
|
+
case CloseStrategyType.STOP_LOSS_IN_DEBT:
|
|
147
|
+
return { stopLossType: CloseToAssetType.DEBT, takeProfitType: undefined };
|
|
148
|
+
case CloseStrategyType.TAKE_PROFIT_IN_COLLATERAL:
|
|
149
|
+
return { stopLossType: undefined, takeProfitType: CloseToAssetType.COLLATERAL };
|
|
150
|
+
case CloseStrategyType.TAKE_PROFIT_IN_DEBT:
|
|
151
|
+
return { stopLossType: undefined, takeProfitType: CloseToAssetType.DEBT };
|
|
152
|
+
case CloseStrategyType.TAKE_PROFIT_IN_COLLATERAL_AND_STOP_LOSS_IN_DEBT:
|
|
153
|
+
return { stopLossType: CloseToAssetType.DEBT, takeProfitType: CloseToAssetType.COLLATERAL };
|
|
154
|
+
case CloseStrategyType.TAKE_PROFIT_IN_DEBT_AND_STOP_LOSS_IN_COLLATERAL:
|
|
155
|
+
return { stopLossType: CloseToAssetType.COLLATERAL, takeProfitType: CloseToAssetType.DEBT };
|
|
156
|
+
case CloseStrategyType.TAKE_PROFIT_AND_STOP_LOSS_IN_DEBT:
|
|
157
|
+
return { stopLossType: CloseToAssetType.DEBT, takeProfitType: CloseToAssetType.DEBT };
|
|
158
|
+
case CloseStrategyType.TAKE_PROFIT_AND_STOP_LOSS_IN_COLLATERAL:
|
|
159
|
+
return { stopLossType: CloseToAssetType.COLLATERAL, takeProfitType: CloseToAssetType.COLLATERAL };
|
|
160
|
+
default:
|
|
161
|
+
throw new Error('CloseStrategyType not supported');
|
|
162
|
+
}
|
|
104
163
|
}
|
package/src/types/enums.ts
CHANGED
|
@@ -21,6 +21,32 @@ export enum BundleProtocols {
|
|
|
21
21
|
Rari = 'rari',
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
+
export enum CollActionType {
|
|
25
|
+
SUPPLY = 0,
|
|
26
|
+
WITHDRAW = 1,
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export enum DebtActionType {
|
|
30
|
+
PAYBACK = 0,
|
|
31
|
+
BORROW = 1,
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export enum CloseStrategyType {
|
|
35
|
+
TAKE_PROFIT_IN_COLLATERAL = 0,
|
|
36
|
+
STOP_LOSS_IN_COLLATERAL = 1,
|
|
37
|
+
TAKE_PROFIT_IN_DEBT = 2,
|
|
38
|
+
STOP_LOSS_IN_DEBT = 3,
|
|
39
|
+
TAKE_PROFIT_AND_STOP_LOSS_IN_COLLATERAL = 4,
|
|
40
|
+
TAKE_PROFIT_IN_COLLATERAL_AND_STOP_LOSS_IN_DEBT = 5,
|
|
41
|
+
TAKE_PROFIT_AND_STOP_LOSS_IN_DEBT = 6,
|
|
42
|
+
TAKE_PROFIT_IN_DEBT_AND_STOP_LOSS_IN_COLLATERAL = 7,
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export enum CloseToAssetType {
|
|
46
|
+
COLLATERAL = 0,
|
|
47
|
+
DEBT = 1,
|
|
48
|
+
}
|
|
49
|
+
|
|
24
50
|
/**
|
|
25
51
|
* @dev Follow the naming convention:
|
|
26
52
|
* - Enum name consists of two parts, name and version
|
|
@@ -32,6 +58,7 @@ export namespace ProtocolIdentifiers {
|
|
|
32
58
|
export enum StrategiesAutomation {
|
|
33
59
|
MakerDAO = 'MakerDAO',
|
|
34
60
|
Liquity = 'Liquity',
|
|
61
|
+
LiquityV2 = 'Liquity__V2',
|
|
35
62
|
ChickenBonds = 'Chicken Bonds',
|
|
36
63
|
CompoundV2 = 'Compound__V2',
|
|
37
64
|
CompoundV3 = 'Compound__V3',
|
|
@@ -42,6 +69,7 @@ export namespace ProtocolIdentifiers {
|
|
|
42
69
|
Spark = 'Spark',
|
|
43
70
|
CrvUSD = 'CurveUSD',
|
|
44
71
|
MorphoBlue = 'MorphoBlue',
|
|
72
|
+
FluidT1 = 'FluidT1',
|
|
45
73
|
}
|
|
46
74
|
|
|
47
75
|
export enum LegacyAutomation {
|
|
@@ -98,6 +126,7 @@ export namespace Strategies {
|
|
|
98
126
|
CloseToCollateralWithGasPrice = 'close-to-collateral-with-gas-price',
|
|
99
127
|
CloseOnPriceToDebt = 'close-on-price-to-debt',
|
|
100
128
|
CloseOnPriceToColl = 'close-on-price-to-collateral',
|
|
129
|
+
CloseOnPrice = 'close-on-price',
|
|
101
130
|
TrailingStopToColl = 'trailing-stop-to-collateral',
|
|
102
131
|
TrailingStopToDebt = 'trailing-stop-to-debt',
|
|
103
132
|
Rebond = 'rebond',
|
|
@@ -165,6 +194,13 @@ export namespace Bundles {
|
|
|
165
194
|
AAVE_V3_OPEN_ORDER_FROM_COLLATERAL = 36,
|
|
166
195
|
AAVE_V3_REPAY_ON_PRICE = 37,
|
|
167
196
|
MORPHO_BLUE_BOOST_ON_PRICE = 38,
|
|
197
|
+
LIQUITY_V2_REPAY = 39,
|
|
198
|
+
LIQUITY_V2_BOOST = 40,
|
|
199
|
+
LIQUITY_V2_CLOSE = 41,
|
|
200
|
+
LIQUITY_V2_REPAY_ON_PRICE = 42,
|
|
201
|
+
LIQUITY_V2_BOOST_ON_PRICE = 43,
|
|
202
|
+
FLUID_T1_REPAY = 44,
|
|
203
|
+
FLUID_T2_BOOST = 45,
|
|
168
204
|
}
|
|
169
205
|
|
|
170
206
|
export enum OptimismIds {
|
package/src/types/index.ts
CHANGED
|
@@ -5,6 +5,7 @@ import type { Subscribe, StrategyModel } from './contracts/generated/SubStorage'
|
|
|
5
5
|
import type {
|
|
6
6
|
ChainId, Strategies, Bundles, ProtocolIdentifiers,
|
|
7
7
|
RatioState,
|
|
8
|
+
CloseToAssetType,
|
|
8
9
|
} from './enums';
|
|
9
10
|
|
|
10
11
|
export type PlaceholderType = any; // TODO - fix any types
|
|
@@ -90,7 +91,7 @@ export declare namespace Interfaces {
|
|
|
90
91
|
|
|
91
92
|
interface Automation {
|
|
92
93
|
provider: Web3,
|
|
93
|
-
providerFork
|
|
94
|
+
providerFork?: Web3,
|
|
94
95
|
}
|
|
95
96
|
interface LegacyAutomation<T extends BaseContract> {
|
|
96
97
|
provider: Web3,
|
|
@@ -166,6 +167,16 @@ export declare namespace Position {
|
|
|
166
167
|
ratioState: RatioState,
|
|
167
168
|
}
|
|
168
169
|
|
|
170
|
+
interface CloseOnPriceLiquityV2 extends Base {
|
|
171
|
+
market: EthereumAddress,
|
|
172
|
+
troveId: string,
|
|
173
|
+
stopLossPrice: string,
|
|
174
|
+
takeProfitPrice: string,
|
|
175
|
+
closeToAssetAddr: EthereumAddress,
|
|
176
|
+
stopLossType: CloseToAssetType | undefined,
|
|
177
|
+
takeProfitType: CloseToAssetType | undefined,
|
|
178
|
+
}
|
|
179
|
+
|
|
169
180
|
interface TrailingStop extends Base {
|
|
170
181
|
roundId: number,
|
|
171
182
|
triggerPercentage: number,
|
|
@@ -192,6 +203,7 @@ export declare namespace Position {
|
|
|
192
203
|
| Specific.CloseOnPriceWithMaximumGasPriceAave
|
|
193
204
|
| Specific.DebtInFrontRepay
|
|
194
205
|
| Specific.LeverageManagementCrvUSD
|
|
206
|
+
| Specific.CloseOnPriceLiquityV2
|
|
195
207
|
| Specific.BoostOnPriceMorpho;
|
|
196
208
|
|
|
197
209
|
export interface Automated {
|