@defisaver/automation-sdk 3.1.1-dev-3 → 3.1.1-dev-5
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/services/strategiesService.js +3 -0
- package/cjs/services/utils.d.ts +4 -0
- package/cjs/services/utils.js +24 -1
- package/cjs/types/index.d.ts +3 -1
- package/esm/services/strategiesService.js +4 -1
- package/esm/services/utils.d.ts +4 -0
- package/esm/services/utils.js +22 -0
- package/esm/types/index.d.ts +3 -1
- package/package.json +1 -1
- package/src/services/strategiesService.ts +5 -1
- package/src/services/utils.ts +25 -0
- package/src/types/index.ts +3 -0
|
@@ -652,6 +652,7 @@ 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
|
|
@@ -664,6 +665,8 @@ function parseLiquityV2CloseOnPrice(position, parseData) {
|
|
|
664
665
|
stopLossPrice: triggerData.lowerPrice,
|
|
665
666
|
takeProfitPrice: triggerData.upperPrice,
|
|
666
667
|
closeToAssetAddr: triggerData.tokenAddr,
|
|
668
|
+
takeProfitType,
|
|
669
|
+
stopLossType,
|
|
667
670
|
};
|
|
668
671
|
return _position;
|
|
669
672
|
}
|
package/cjs/services/utils.d.ts
CHANGED
|
@@ -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
|
+
};
|
package/cjs/services/utils.js
CHANGED
|
@@ -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;
|
package/cjs/types/index.d.ts
CHANGED
|
@@ -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;
|
|
@@ -140,6 +140,8 @@ export declare namespace Position {
|
|
|
140
140
|
stopLossPrice: string;
|
|
141
141
|
takeProfitPrice: string;
|
|
142
142
|
closeToAssetAddr: EthereumAddress;
|
|
143
|
+
stopLossType: CloseToAssetType | undefined;
|
|
144
|
+
takeProfitType: CloseToAssetType | undefined;
|
|
143
145
|
}
|
|
144
146
|
interface TrailingStop extends Base {
|
|
145
147
|
roundId: number;
|
|
@@ -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,6 +626,7 @@ 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
|
|
@@ -638,6 +639,8 @@ function parseLiquityV2CloseOnPrice(position, parseData) {
|
|
|
638
639
|
stopLossPrice: triggerData.lowerPrice,
|
|
639
640
|
takeProfitPrice: triggerData.upperPrice,
|
|
640
641
|
closeToAssetAddr: triggerData.tokenAddr,
|
|
642
|
+
takeProfitType,
|
|
643
|
+
stopLossType,
|
|
641
644
|
};
|
|
642
645
|
return _position;
|
|
643
646
|
}
|
package/esm/services/utils.d.ts
CHANGED
|
@@ -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
|
+
};
|
package/esm/services/utils.js
CHANGED
|
@@ -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
|
+
}
|
package/esm/types/index.d.ts
CHANGED
|
@@ -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;
|
|
@@ -140,6 +140,8 @@ export declare namespace Position {
|
|
|
140
140
|
stopLossPrice: string;
|
|
141
141
|
takeProfitPrice: string;
|
|
142
142
|
closeToAssetAddr: EthereumAddress;
|
|
143
|
+
stopLossType: CloseToAssetType | undefined;
|
|
144
|
+
takeProfitType: CloseToAssetType | undefined;
|
|
143
145
|
}
|
|
144
146
|
interface TrailingStop extends Base {
|
|
145
147
|
roundId: number;
|
package/package.json
CHANGED
|
@@ -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,6 +864,8 @@ 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
|
|
@@ -876,6 +878,8 @@ function parseLiquityV2CloseOnPrice(position: Position.Automated, parseData: Par
|
|
|
876
878
|
stopLossPrice: triggerData.lowerPrice,
|
|
877
879
|
takeProfitPrice: triggerData.upperPrice,
|
|
878
880
|
closeToAssetAddr: triggerData.tokenAddr,
|
|
881
|
+
takeProfitType,
|
|
882
|
+
stopLossType,
|
|
879
883
|
};
|
|
880
884
|
|
|
881
885
|
return _position;
|
package/src/services/utils.ts
CHANGED
|
@@ -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
|
}
|
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
|
|
@@ -167,6 +168,8 @@ export declare namespace Position {
|
|
|
167
168
|
stopLossPrice: string,
|
|
168
169
|
takeProfitPrice: string,
|
|
169
170
|
closeToAssetAddr: EthereumAddress,
|
|
171
|
+
stopLossType: CloseToAssetType | undefined,
|
|
172
|
+
takeProfitType: CloseToAssetType | undefined,
|
|
170
173
|
}
|
|
171
174
|
|
|
172
175
|
interface TrailingStop extends Base {
|