@defisaver/automation-sdk 3.1.1-dev-2 → 3.1.1-dev-4

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.
@@ -652,13 +652,22 @@ function parseLiquityV2CloseOnPrice(position, parseData) {
652
652
  _position.strategyData.decoded.triggerData = triggerData;
653
653
  _position.strategyData.decoded.subData = subData;
654
654
  _position.positionId = (0, utils_1.getPositionId)(_position.chainId, _position.protocol.id, _position.owner, subData.troveId, subData.market);
655
+ const { takeProfitType, stopLossType } = (0, utils_1.getStopLossAndTakeProfitTypeByCloseStrategyType)(subData.closeType);
655
656
  // User can have:
656
657
  // - Only TakeProfit
657
658
  // - Only StopLoss
658
659
  // - Both
659
660
  // TODO: see on frontend what specific data we need here because stop-loss and take-profit is one bundle now
660
661
  _position.strategy.strategyId = enums_1.Strategies.Identifiers.CloseOnPrice;
661
- _position.specific = {};
662
+ _position.specific = {
663
+ market: subData.market,
664
+ troveId: subData.troveId,
665
+ stopLossPrice: triggerData.lowerPrice,
666
+ takeProfitPrice: triggerData.upperPrice,
667
+ closeToAssetAddr: triggerData.tokenAddr,
668
+ takeProfitType,
669
+ stopLossType,
670
+ };
662
671
  return _position;
663
672
  }
664
673
  const parsingMethodsMapping = {
@@ -24,3 +24,7 @@ export declare function getRatioStateInfoForAaveCloseStrategy(currentRatioState:
24
24
  };
25
25
  export declare function getPositionId(...args: (number | string)[]): string;
26
26
  export declare function getCloseStrategyType(stopLossPrice: number, stopLossType: CloseToAssetType, takeProfitPrice: number, takeProfitType: CloseToAssetType): CloseStrategyType;
27
+ export declare function getStopLossAndTakeProfitTypeByCloseStrategyType(closeStrategyType: CloseStrategyType): {
28
+ stopLossType: CloseToAssetType | undefined;
29
+ takeProfitType: CloseToAssetType | undefined;
30
+ };
@@ -26,7 +26,7 @@ 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.getCloseStrategyType = exports.getPositionId = exports.getRatioStateInfoForAaveCloseStrategy = exports.requireAddresses = exports.requireAddress = exports.isEmptyBytes = exports.isRatioStateUnder = exports.isRatioStateOver = exports.weiToRatioPercentage = exports.ratioPercentageToWei = exports.encodeSubId = exports.compareSubHashes = exports.wethToEthByAddress = exports.wethToEth = exports.ethToWeth = exports.addToObjectIf = exports.addToArrayIf = exports.isAddress = exports.compareAddresses = exports.isUndefined = exports.isDefined = void 0;
29
+ exports.getStopLossAndTakeProfitTypeByCloseStrategyType = exports.getCloseStrategyType = exports.getPositionId = exports.getRatioStateInfoForAaveCloseStrategy = exports.requireAddresses = exports.requireAddress = exports.isEmptyBytes = exports.isRatioStateUnder = exports.isRatioStateOver = exports.weiToRatioPercentage = exports.ratioPercentageToWei = exports.encodeSubId = exports.compareSubHashes = exports.wethToEthByAddress = exports.wethToEth = exports.ethToWeth = exports.addToObjectIf = exports.addToArrayIf = exports.isAddress = exports.compareAddresses = exports.isUndefined = exports.isDefined = void 0;
30
30
  const decimal_js_1 = __importDefault(require("decimal.js"));
31
31
  const web3Utils = __importStar(require("web3-utils"));
32
32
  const web3_eth_abi_1 = __importDefault(require("web3-eth-abi"));
@@ -157,3 +157,26 @@ function getCloseStrategyType(stopLossPrice, stopLossType, takeProfitPrice, take
157
157
  : enums_1.CloseStrategyType.TAKE_PROFIT_IN_DEBT;
158
158
  }
159
159
  exports.getCloseStrategyType = getCloseStrategyType;
160
+ function getStopLossAndTakeProfitTypeByCloseStrategyType(closeStrategyType) {
161
+ switch (closeStrategyType) {
162
+ case enums_1.CloseStrategyType.STOP_LOSS_IN_COLLATERAL:
163
+ return { stopLossType: enums_1.CloseToAssetType.COLLATERAL, takeProfitType: undefined };
164
+ case enums_1.CloseStrategyType.STOP_LOSS_IN_DEBT:
165
+ return { stopLossType: enums_1.CloseToAssetType.DEBT, takeProfitType: undefined };
166
+ case enums_1.CloseStrategyType.TAKE_PROFIT_IN_COLLATERAL:
167
+ return { stopLossType: undefined, takeProfitType: enums_1.CloseToAssetType.COLLATERAL };
168
+ case enums_1.CloseStrategyType.TAKE_PROFIT_IN_DEBT:
169
+ return { stopLossType: undefined, takeProfitType: enums_1.CloseToAssetType.DEBT };
170
+ case enums_1.CloseStrategyType.TAKE_PROFIT_IN_COLLATERAL_AND_STOP_LOSS_IN_DEBT:
171
+ return { stopLossType: enums_1.CloseToAssetType.DEBT, takeProfitType: enums_1.CloseToAssetType.COLLATERAL };
172
+ case enums_1.CloseStrategyType.TAKE_PROFIT_IN_DEBT_AND_STOP_LOSS_IN_COLLATERAL:
173
+ return { stopLossType: enums_1.CloseToAssetType.COLLATERAL, takeProfitType: enums_1.CloseToAssetType.DEBT };
174
+ case enums_1.CloseStrategyType.TAKE_PROFIT_AND_STOP_LOSS_IN_DEBT:
175
+ return { stopLossType: enums_1.CloseToAssetType.DEBT, takeProfitType: enums_1.CloseToAssetType.DEBT };
176
+ case enums_1.CloseStrategyType.TAKE_PROFIT_AND_STOP_LOSS_IN_COLLATERAL:
177
+ return { stopLossType: enums_1.CloseToAssetType.COLLATERAL, takeProfitType: enums_1.CloseToAssetType.COLLATERAL };
178
+ default:
179
+ throw new Error('CloseStrategyType not supported');
180
+ }
181
+ }
182
+ exports.getStopLossAndTakeProfitTypeByCloseStrategyType = getStopLossAndTakeProfitTypeByCloseStrategyType;
@@ -2,7 +2,7 @@ import type Web3 from 'web3';
2
2
  import type { AbiItem } from 'web3-utils';
3
3
  import type { BaseContract, BlockType } from './contracts/generated/types';
4
4
  import type { Subscribe, StrategyModel } from './contracts/generated/SubStorage';
5
- import type { ChainId, Strategies, Bundles, ProtocolIdentifiers, RatioState } from './enums';
5
+ import type { ChainId, Strategies, Bundles, ProtocolIdentifiers, RatioState, CloseToAssetType } from './enums';
6
6
  export type PlaceholderType = any;
7
7
  export type EthereumAddress = string;
8
8
  export type BlockNumber = BlockType;
@@ -134,6 +134,15 @@ export declare namespace Position {
134
134
  maximumGasPrice: string;
135
135
  ratioState: RatioState;
136
136
  }
137
+ interface CloseOnPriceLiquityV2 extends Base {
138
+ market: EthereumAddress;
139
+ troveId: string;
140
+ stopLossPrice: string;
141
+ takeProfitPrice: string;
142
+ closeToAssetAddr: EthereumAddress;
143
+ stopLossType: CloseToAssetType | undefined;
144
+ takeProfitType: CloseToAssetType | undefined;
145
+ }
137
146
  interface TrailingStop extends Base {
138
147
  roundId: number;
139
148
  triggerPercentage: number;
@@ -148,7 +157,7 @@ export declare namespace Position {
148
157
  subHashRepay?: string;
149
158
  }
150
159
  }
151
- type SpecificAny = Specific.CloseOnPrice | Specific.TrailingStop | Specific.RatioProtection | Specific.CloseOnPriceAave | Specific.BoostOnPriceAave | Specific.CloseOnPriceWithMaximumGasPriceAave | Specific.DebtInFrontRepay | Specific.LeverageManagementCrvUSD;
160
+ type SpecificAny = Specific.CloseOnPrice | Specific.TrailingStop | Specific.RatioProtection | Specific.CloseOnPriceAave | Specific.BoostOnPriceAave | Specific.CloseOnPriceWithMaximumGasPriceAave | Specific.DebtInFrontRepay | Specific.LeverageManagementCrvUSD | Specific.CloseOnPriceLiquityV2;
152
161
  interface Automated {
153
162
  chainId: ChainId;
154
163
  positionId: string;
@@ -2,7 +2,7 @@ import { getAssetInfoByAddress } from '@defisaver/tokens';
2
2
  import { cloneDeep } from 'lodash';
3
3
  import { BUNDLES_INFO, STRATEGIES_INFO } from '../constants';
4
4
  import { ChainId, ProtocolIdentifiers, Strategies } from '../types/enums';
5
- import { getPositionId, getRatioStateInfoForAaveCloseStrategy, isRatioStateOver, wethToEthByAddress, } from './utils';
5
+ import { getPositionId, getRatioStateInfoForAaveCloseStrategy, getStopLossAndTakeProfitTypeByCloseStrategyType, isRatioStateOver, wethToEthByAddress, } from './utils';
6
6
  import * as subDataService from './subDataService';
7
7
  import * as triggerService from './triggerService';
8
8
  const SPARK_MARKET_ADDRESSES = {
@@ -626,13 +626,22 @@ function parseLiquityV2CloseOnPrice(position, parseData) {
626
626
  _position.strategyData.decoded.triggerData = triggerData;
627
627
  _position.strategyData.decoded.subData = subData;
628
628
  _position.positionId = getPositionId(_position.chainId, _position.protocol.id, _position.owner, subData.troveId, subData.market);
629
+ const { takeProfitType, stopLossType } = getStopLossAndTakeProfitTypeByCloseStrategyType(subData.closeType);
629
630
  // User can have:
630
631
  // - Only TakeProfit
631
632
  // - Only StopLoss
632
633
  // - Both
633
634
  // TODO: see on frontend what specific data we need here because stop-loss and take-profit is one bundle now
634
635
  _position.strategy.strategyId = Strategies.Identifiers.CloseOnPrice;
635
- _position.specific = {};
636
+ _position.specific = {
637
+ market: subData.market,
638
+ troveId: subData.troveId,
639
+ stopLossPrice: triggerData.lowerPrice,
640
+ takeProfitPrice: triggerData.upperPrice,
641
+ closeToAssetAddr: triggerData.tokenAddr,
642
+ takeProfitType,
643
+ stopLossType,
644
+ };
636
645
  return _position;
637
646
  }
638
647
  const parsingMethodsMapping = {
@@ -24,3 +24,7 @@ export declare function getRatioStateInfoForAaveCloseStrategy(currentRatioState:
24
24
  };
25
25
  export declare function getPositionId(...args: (number | string)[]): string;
26
26
  export declare function getCloseStrategyType(stopLossPrice: number, stopLossType: CloseToAssetType, takeProfitPrice: number, takeProfitType: CloseToAssetType): CloseStrategyType;
27
+ export declare function getStopLossAndTakeProfitTypeByCloseStrategyType(closeStrategyType: CloseStrategyType): {
28
+ stopLossType: CloseToAssetType | undefined;
29
+ takeProfitType: CloseToAssetType | undefined;
30
+ };
@@ -107,3 +107,25 @@ export function getCloseStrategyType(stopLossPrice, stopLossType, takeProfitPric
107
107
  ? CloseStrategyType.TAKE_PROFIT_IN_COLLATERAL
108
108
  : CloseStrategyType.TAKE_PROFIT_IN_DEBT;
109
109
  }
110
+ export function getStopLossAndTakeProfitTypeByCloseStrategyType(closeStrategyType) {
111
+ switch (closeStrategyType) {
112
+ case CloseStrategyType.STOP_LOSS_IN_COLLATERAL:
113
+ return { stopLossType: CloseToAssetType.COLLATERAL, takeProfitType: undefined };
114
+ case CloseStrategyType.STOP_LOSS_IN_DEBT:
115
+ return { stopLossType: CloseToAssetType.DEBT, takeProfitType: undefined };
116
+ case CloseStrategyType.TAKE_PROFIT_IN_COLLATERAL:
117
+ return { stopLossType: undefined, takeProfitType: CloseToAssetType.COLLATERAL };
118
+ case CloseStrategyType.TAKE_PROFIT_IN_DEBT:
119
+ return { stopLossType: undefined, takeProfitType: CloseToAssetType.DEBT };
120
+ case CloseStrategyType.TAKE_PROFIT_IN_COLLATERAL_AND_STOP_LOSS_IN_DEBT:
121
+ return { stopLossType: CloseToAssetType.DEBT, takeProfitType: CloseToAssetType.COLLATERAL };
122
+ case CloseStrategyType.TAKE_PROFIT_IN_DEBT_AND_STOP_LOSS_IN_COLLATERAL:
123
+ return { stopLossType: CloseToAssetType.COLLATERAL, takeProfitType: CloseToAssetType.DEBT };
124
+ case CloseStrategyType.TAKE_PROFIT_AND_STOP_LOSS_IN_DEBT:
125
+ return { stopLossType: CloseToAssetType.DEBT, takeProfitType: CloseToAssetType.DEBT };
126
+ case CloseStrategyType.TAKE_PROFIT_AND_STOP_LOSS_IN_COLLATERAL:
127
+ return { stopLossType: CloseToAssetType.COLLATERAL, takeProfitType: CloseToAssetType.COLLATERAL };
128
+ default:
129
+ throw new Error('CloseStrategyType not supported');
130
+ }
131
+ }
@@ -2,7 +2,7 @@ import type Web3 from 'web3';
2
2
  import type { AbiItem } from 'web3-utils';
3
3
  import type { BaseContract, BlockType } from './contracts/generated/types';
4
4
  import type { Subscribe, StrategyModel } from './contracts/generated/SubStorage';
5
- import type { ChainId, Strategies, Bundles, ProtocolIdentifiers, RatioState } from './enums';
5
+ import type { ChainId, Strategies, Bundles, ProtocolIdentifiers, RatioState, CloseToAssetType } from './enums';
6
6
  export type PlaceholderType = any;
7
7
  export type EthereumAddress = string;
8
8
  export type BlockNumber = BlockType;
@@ -134,6 +134,15 @@ export declare namespace Position {
134
134
  maximumGasPrice: string;
135
135
  ratioState: RatioState;
136
136
  }
137
+ interface CloseOnPriceLiquityV2 extends Base {
138
+ market: EthereumAddress;
139
+ troveId: string;
140
+ stopLossPrice: string;
141
+ takeProfitPrice: string;
142
+ closeToAssetAddr: EthereumAddress;
143
+ stopLossType: CloseToAssetType | undefined;
144
+ takeProfitType: CloseToAssetType | undefined;
145
+ }
137
146
  interface TrailingStop extends Base {
138
147
  roundId: number;
139
148
  triggerPercentage: number;
@@ -148,7 +157,7 @@ export declare namespace Position {
148
157
  subHashRepay?: string;
149
158
  }
150
159
  }
151
- type SpecificAny = Specific.CloseOnPrice | Specific.TrailingStop | Specific.RatioProtection | Specific.CloseOnPriceAave | Specific.BoostOnPriceAave | Specific.CloseOnPriceWithMaximumGasPriceAave | Specific.DebtInFrontRepay | Specific.LeverageManagementCrvUSD;
160
+ type SpecificAny = Specific.CloseOnPrice | Specific.TrailingStop | Specific.RatioProtection | Specific.CloseOnPriceAave | Specific.BoostOnPriceAave | Specific.CloseOnPriceWithMaximumGasPriceAave | Specific.DebtInFrontRepay | Specific.LeverageManagementCrvUSD | Specific.CloseOnPriceLiquityV2;
152
161
  interface Automated {
153
162
  chainId: ChainId;
154
163
  positionId: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@defisaver/automation-sdk",
3
- "version": "3.1.1-dev-2",
3
+ "version": "3.1.1-dev-4",
4
4
  "description": "",
5
5
  "main": "./cjs/index.js",
6
6
  "module": "./esm/index.js",
@@ -9,7 +9,7 @@ import type {
9
9
  import { ChainId, ProtocolIdentifiers, Strategies } from '../types/enums';
10
10
 
11
11
  import {
12
- getPositionId, getRatioStateInfoForAaveCloseStrategy, isRatioStateOver, wethToEthByAddress,
12
+ getPositionId, getRatioStateInfoForAaveCloseStrategy, getStopLossAndTakeProfitTypeByCloseStrategyType, isRatioStateOver, wethToEthByAddress,
13
13
  } from './utils';
14
14
  import * as subDataService from './subDataService';
15
15
  import * as triggerService from './triggerService';
@@ -864,13 +864,23 @@ function parseLiquityV2CloseOnPrice(position: Position.Automated, parseData: Par
864
864
  _position.chainId, _position.protocol.id, _position.owner, subData.troveId, subData.market,
865
865
  );
866
866
 
867
+ const { takeProfitType, stopLossType } = getStopLossAndTakeProfitTypeByCloseStrategyType(subData.closeType);
868
+
867
869
  // User can have:
868
870
  // - Only TakeProfit
869
871
  // - Only StopLoss
870
872
  // - Both
871
873
  // TODO: see on frontend what specific data we need here because stop-loss and take-profit is one bundle now
872
874
  _position.strategy.strategyId = Strategies.Identifiers.CloseOnPrice;
873
- _position.specific = {};
875
+ _position.specific = {
876
+ market: subData.market,
877
+ troveId: subData.troveId,
878
+ stopLossPrice: triggerData.lowerPrice,
879
+ takeProfitPrice: triggerData.upperPrice,
880
+ closeToAssetAddr: triggerData.tokenAddr,
881
+ takeProfitType,
882
+ stopLossType,
883
+ };
874
884
 
875
885
  return _position;
876
886
  }
@@ -135,4 +135,29 @@ export function getCloseStrategyType(
135
135
  return takeProfitType === CloseToAssetType.COLLATERAL
136
136
  ? CloseStrategyType.TAKE_PROFIT_IN_COLLATERAL
137
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
+ }
138
163
  }
@@ -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
@@ -161,6 +162,16 @@ export declare namespace Position {
161
162
  ratioState: RatioState,
162
163
  }
163
164
 
165
+ interface CloseOnPriceLiquityV2 extends Base {
166
+ market: EthereumAddress,
167
+ troveId: string,
168
+ stopLossPrice: string,
169
+ takeProfitPrice: string,
170
+ closeToAssetAddr: EthereumAddress,
171
+ stopLossType: CloseToAssetType | undefined,
172
+ takeProfitType: CloseToAssetType | undefined,
173
+ }
174
+
164
175
  interface TrailingStop extends Base {
165
176
  roundId: number,
166
177
  triggerPercentage: number,
@@ -186,7 +197,8 @@ export declare namespace Position {
186
197
  | Specific.BoostOnPriceAave
187
198
  | Specific.CloseOnPriceWithMaximumGasPriceAave
188
199
  | Specific.DebtInFrontRepay
189
- | Specific.LeverageManagementCrvUSD;
200
+ | Specific.LeverageManagementCrvUSD
201
+ | Specific.CloseOnPriceLiquityV2;
190
202
 
191
203
  export interface Automated {
192
204
  chainId: ChainId,