@defisaver/automation-sdk 3.1.4 → 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.
Files changed (44) hide show
  1. package/cjs/automation/private/StrategiesAutomation.d.ts +2 -2
  2. package/cjs/automation/public/Strategies.test.d.ts +1 -0
  3. package/cjs/automation/public/Strategies.test.js +61 -0
  4. package/cjs/constants/index.js +45 -0
  5. package/cjs/services/strategiesService.js +155 -0
  6. package/cjs/services/strategySubService.d.ts +10 -1
  7. package/cjs/services/strategySubService.js +35 -1
  8. package/cjs/services/subDataService.d.ts +55 -1
  9. package/cjs/services/subDataService.js +174 -1
  10. package/cjs/services/triggerService.d.ts +43 -0
  11. package/cjs/services/triggerService.js +81 -1
  12. package/cjs/services/utils.d.ts +6 -1
  13. package/cjs/services/utils.js +52 -1
  14. package/cjs/types/enums.d.ts +38 -3
  15. package/cjs/types/enums.js +40 -1
  16. package/cjs/types/index.d.ts +16 -3
  17. package/esm/automation/private/StrategiesAutomation.d.ts +2 -2
  18. package/esm/automation/public/Strategies.test.d.ts +1 -0
  19. package/esm/automation/public/Strategies.test.js +56 -0
  20. package/esm/constants/index.js +45 -0
  21. package/esm/services/strategiesService.js +153 -1
  22. package/esm/services/strategySubService.d.ts +10 -1
  23. package/esm/services/strategySubService.js +36 -2
  24. package/esm/services/subDataService.d.ts +55 -1
  25. package/esm/services/subDataService.js +174 -1
  26. package/esm/services/triggerService.d.ts +43 -0
  27. package/esm/services/triggerService.js +80 -0
  28. package/esm/services/utils.d.ts +6 -1
  29. package/esm/services/utils.js +50 -1
  30. package/esm/types/enums.d.ts +38 -3
  31. package/esm/types/enums.js +39 -0
  32. package/esm/types/index.d.ts +16 -3
  33. package/package.json +3 -3
  34. package/src/automation/private/StrategiesAutomation.ts +2 -2
  35. package/src/automation/public/Strategies.test.ts +49 -0
  36. package/src/constants/index.ts +45 -0
  37. package/src/services/strategiesService.ts +199 -1
  38. package/src/services/strategySubService.ts +97 -1
  39. package/src/services/subDataService.ts +243 -2
  40. package/src/services/triggerService.ts +125 -0
  41. package/src/services/utils.ts +60 -1
  42. package/src/types/enums.ts +39 -0
  43. package/src/types/index.ts +20 -2
  44. package/umd/index.js +34219 -0
@@ -447,3 +447,128 @@ export const morphoBlueRatioTrigger = {
447
447
  };
448
448
  },
449
449
  };
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
+ };
526
+ export const morphoBluePriceTrigger = {
527
+ encode(
528
+ oracle: EthereumAddress,
529
+ collateralToken: EthereumAddress,
530
+ loanToken: EthereumAddress,
531
+ price: number,
532
+ priceState: RatioState,
533
+ ) {
534
+ const _price = new Dec(price.toString()).mul(1e8).floor().toString();
535
+ return [
536
+ AbiCoder.encodeParameters(
537
+ ['address', 'address', 'address', 'uint256', 'uint8'],
538
+ [oracle, collateralToken, loanToken, _price, priceState]),
539
+ ];
540
+ },
541
+ decode(
542
+ triggerData: TriggerData,
543
+ ) {
544
+ const decodedData = AbiCoder.decodeParameters(['address', 'address', 'address', 'uint256', 'uint8'], triggerData[0]);
545
+ return {
546
+ oracle: decodedData[0] as EthereumAddress,
547
+ collateralToken: decodedData[1] as EthereumAddress,
548
+ loanToken: decodedData[2] as EthereumAddress,
549
+ price: new Dec(decodedData[3] as string).div(1e8).toString(),
550
+ priceState: Number(decodedData[4]),
551
+ };
552
+ },
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
+ };
@@ -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 { ChainId, RatioState } from '../types/enums';
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
  }
@@ -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',
@@ -108,6 +137,7 @@ export namespace Strategies {
108
137
  DebtInFrontRepay = 'debt-in-front-repay',
109
138
  OpenOrderFromCollateral = 'open-order-from-collateral',
110
139
  OpenOrderFromDebt = 'open-order-from-debt',
140
+ BoostOnPrice = 'boost-on-price',
111
141
  RepayOnPrice = 'repay-on-price',
112
142
  }
113
143
  export enum IdOverrides {
@@ -163,6 +193,14 @@ export namespace Bundles {
163
193
  MORPHO_BLUE_EOA_BOOST = 35,
164
194
  AAVE_V3_OPEN_ORDER_FROM_COLLATERAL = 36,
165
195
  AAVE_V3_REPAY_ON_PRICE = 37,
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,
166
204
  }
167
205
 
168
206
  export enum OptimismIds {
@@ -185,6 +223,7 @@ export namespace Bundles {
185
223
  MORPHO_BLUE_BOOST = 9,
186
224
  AAVE_V3_OPEN_ORDER_FROM_COLLATERAL = 10,
187
225
  AAVE_V3_REPAY_ON_PRICE = 11,
226
+ MORPHO_BLUE_BOOST_ON_PRICE = 12,
188
227
  }
189
228
 
190
229
  export enum ArbitrumIds {
@@ -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: Web3,
94
+ providerFork?: Web3,
94
95
  }
95
96
  interface LegacyAutomation<T extends BaseContract> {
96
97
  provider: Web3,
@@ -149,6 +150,11 @@ export declare namespace Position {
149
150
  ratio: number,
150
151
  }
151
152
 
153
+ interface BoostOnPriceMorpho extends Base {
154
+ marketId: string;
155
+ subHash: string;
156
+ }
157
+
152
158
  interface CloseOnPriceWithMaximumGasPriceAave extends Base {
153
159
  collAsset: EthereumAddress,
154
160
  collAssetId: number,
@@ -161,6 +167,16 @@ export declare namespace Position {
161
167
  ratioState: RatioState,
162
168
  }
163
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
+
164
180
  interface TrailingStop extends Base {
165
181
  roundId: number,
166
182
  triggerPercentage: number,
@@ -186,7 +202,9 @@ export declare namespace Position {
186
202
  | Specific.BoostOnPriceAave
187
203
  | Specific.CloseOnPriceWithMaximumGasPriceAave
188
204
  | Specific.DebtInFrontRepay
189
- | Specific.LeverageManagementCrvUSD;
205
+ | Specific.LeverageManagementCrvUSD
206
+ | Specific.CloseOnPriceLiquityV2
207
+ | Specific.BoostOnPriceMorpho;
190
208
 
191
209
  export interface Automated {
192
210
  chainId: ChainId,