@gmx-io/sdk 1.2.0 → 1.3.0
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/build/cjs/src/abis/ClaimHandler.json +424 -0
- package/build/cjs/src/abis/CustomErrors.json +642 -61
- package/build/cjs/src/abis/DataStore.json +25 -6
- package/build/cjs/src/abis/EventEmitter.json +1 -6
- package/build/cjs/src/abis/ExchangeRouter.json +195 -290
- package/build/cjs/src/abis/GelatoRelayRouter.json +251 -98
- package/build/cjs/src/abis/GlvReader.json +60 -3
- package/build/cjs/src/abis/GlvRouter.json +144 -86
- package/build/cjs/src/abis/GovToken.json +1 -354
- package/build/cjs/src/abis/LayerZeroProvider.json +372 -0
- package/build/cjs/src/abis/Multicall.json +134 -38
- package/build/cjs/src/abis/MultichainClaimsRouter.json +1266 -0
- package/build/cjs/src/abis/MultichainGlvRouter.json +1150 -0
- package/build/cjs/src/abis/MultichainGmRouter.json +1449 -0
- package/build/cjs/src/abis/MultichainOrderRouter.json +1820 -0
- package/build/cjs/src/abis/MultichainSubaccountRouter.json +2069 -0
- package/build/cjs/src/abis/MultichainTransferRouter.json +915 -0
- package/build/cjs/src/abis/MultichainUtils.json +133 -0
- package/build/cjs/src/abis/MultichainVault.json +275 -0
- package/build/cjs/src/abis/ReferralStorage.json +69 -19
- package/build/cjs/src/abis/RelayParams.json +85 -48
- package/build/cjs/src/abis/SmartAccount.json +28 -0
- package/build/cjs/src/abis/SubaccountGelatoRelayRouter.json +282 -148
- package/build/cjs/src/abis/SubaccountRouter.json +62 -118
- package/build/cjs/src/abis/SyntheticsReader.json +342 -474
- package/build/cjs/src/abis/SyntheticsRouter.json +1 -6
- package/build/cjs/src/abis/index.js +53 -5
- package/build/cjs/src/configs/__tests__/markets.spec.js +2 -2
- package/build/cjs/src/configs/__tests__/permits.spec.js +1 -1
- package/build/cjs/src/configs/__tests__/tokens.spec.js +2 -2
- package/build/cjs/src/configs/batch.js +60 -0
- package/build/cjs/src/configs/chains.js +41 -8
- package/build/cjs/src/configs/contracts.js +151 -39
- package/build/cjs/src/configs/dataStore.js +49 -5
- package/build/cjs/src/configs/express.js +6 -1
- package/build/cjs/src/configs/factors.js +1 -1
- package/build/cjs/src/configs/markets.js +163 -0
- package/build/cjs/src/configs/oracleKeeper.js +21 -35
- package/build/cjs/src/configs/tokens.js +272 -2
- package/build/cjs/src/index.js +3 -3
- package/build/cjs/src/modules/markets/index.js +9 -4
- package/build/cjs/src/modules/markets/query-builders.js +28 -8
- package/build/cjs/src/modules/orders/helpers.js +11 -2
- package/build/cjs/src/modules/orders/helpers.spec.js +2 -2
- package/build/cjs/src/modules/orders/orders.js +3 -3
- package/build/cjs/src/modules/orders/utils.js +4 -5
- package/build/cjs/src/modules/positions/positions.js +38 -5
- package/build/cjs/src/modules/utils/utils.js +4 -3
- package/build/cjs/src/prebuilt/hashedKinkModelMarketRatesKeys.json +186 -0
- package/build/cjs/src/prebuilt/hashedMarketConfigKeys.json +2442 -512
- package/build/cjs/src/prebuilt/hashedMarketValuesKeys.json +347 -0
- package/build/cjs/src/test/mock.js +9 -4
- package/build/cjs/src/types/subsquid.js +597 -1
- package/build/cjs/src/types/swapStrategy.js +2 -0
- package/build/cjs/src/types/trade.js +1 -0
- package/build/cjs/src/utils/LruCache.js +4 -0
- package/build/cjs/src/utils/__tests__/LruCache.spec.js +18 -0
- package/build/cjs/src/utils/__tests__/numbers.spec.js +18 -16
- package/build/cjs/src/utils/__tests__/orderTransactions/batchUtils.spec.js +0 -1
- package/build/cjs/src/utils/__tests__/orderTransactions/decreaseOrders.spec.js +8 -0
- package/build/cjs/src/utils/__tests__/orderTransactions/exchangeRouterMulticall.spec.js +40 -30
- package/build/cjs/src/utils/__tests__/orderTransactions/increaseOrders.spec.js +12 -0
- package/build/cjs/src/utils/__tests__/orderTransactions/swapOrders.spec.js +6 -0
- package/build/cjs/src/utils/__tests__/positions.spec.js +23 -6
- package/build/cjs/src/utils/__tests__/uiFeeReceiver.spec.js +10 -0
- package/build/cjs/src/utils/bigmath.js +9 -2
- package/build/cjs/src/utils/errors/parseError.js +35 -1
- package/build/cjs/src/utils/errors/transactionsErrors.js +7 -1
- package/build/cjs/src/utils/fees/executionFee.js +5 -3
- package/build/cjs/src/utils/fees/index.js +8 -6
- package/build/cjs/src/utils/fees/priceImpact.js +94 -32
- package/build/cjs/src/utils/gelatoRelay.js +5 -0
- package/build/cjs/src/utils/marketKeysAndConfigs.js +24 -4
- package/build/cjs/src/utils/markets.js +10 -3
- package/build/cjs/src/utils/numbers.js +18 -17
- package/build/cjs/src/utils/orderTransactions.js +5 -2
- package/build/cjs/src/utils/positions.js +77 -9
- package/build/cjs/src/utils/prices.js +19 -13
- package/build/cjs/src/utils/swap/__tests__/mockRouting.spec.js +1 -0
- package/build/cjs/src/utils/swap/botanixStaking.js +90 -0
- package/build/cjs/src/utils/swap/buildSwapStrategy.js +224 -0
- package/build/cjs/src/utils/swap/externalSwapPath.js +41 -0
- package/build/cjs/src/utils/swap/externalSwapQuoteByPath.js +27 -0
- package/build/cjs/src/utils/swap/swapStats.js +6 -3
- package/build/cjs/src/utils/swap/swapValues.js +250 -11
- package/build/cjs/src/utils/trade/decrease.js +470 -0
- package/build/cjs/src/utils/trade/{amounts.js → increase.js} +175 -72
- package/build/cjs/src/utils/trade/trade.js +2 -2
- package/build/cjs/src/utils/tradeHistory.js +3 -0
- package/build/cjs/src/utils/twap/uiFeeReceiver.js +9 -9
- package/build/esm/src/abis/ClaimHandler.json +424 -0
- package/build/esm/src/abis/CustomErrors.json +642 -61
- package/build/esm/src/abis/DataStore.json +25 -6
- package/build/esm/src/abis/EventEmitter.json +1 -6
- package/build/esm/src/abis/ExchangeRouter.json +195 -290
- package/build/esm/src/abis/GelatoRelayRouter.json +251 -98
- package/build/esm/src/abis/GlvReader.json +60 -3
- package/build/esm/src/abis/GlvRouter.json +144 -86
- package/build/esm/src/abis/GovToken.json +1 -354
- package/build/esm/src/abis/LayerZeroProvider.json +372 -0
- package/build/esm/src/abis/Multicall.json +134 -38
- package/build/esm/src/abis/MultichainClaimsRouter.json +1266 -0
- package/build/esm/src/abis/MultichainGlvRouter.json +1150 -0
- package/build/esm/src/abis/MultichainGmRouter.json +1449 -0
- package/build/esm/src/abis/MultichainOrderRouter.json +1820 -0
- package/build/esm/src/abis/MultichainSubaccountRouter.json +2069 -0
- package/build/esm/src/abis/MultichainTransferRouter.json +915 -0
- package/build/esm/src/abis/MultichainUtils.json +133 -0
- package/build/esm/src/abis/MultichainVault.json +275 -0
- package/build/esm/src/abis/ReferralStorage.json +69 -19
- package/build/esm/src/abis/RelayParams.json +85 -48
- package/build/esm/src/abis/SmartAccount.json +28 -0
- package/build/esm/src/abis/SubaccountGelatoRelayRouter.json +282 -148
- package/build/esm/src/abis/SubaccountRouter.json +62 -118
- package/build/esm/src/abis/SyntheticsReader.json +342 -474
- package/build/esm/src/abis/SyntheticsRouter.json +1 -6
- package/build/esm/src/abis/index.js +53 -5
- package/build/esm/src/configs/__tests__/markets.spec.js +4 -4
- package/build/esm/src/configs/__tests__/permits.spec.js +2 -2
- package/build/esm/src/configs/__tests__/tokens.spec.js +3 -3
- package/build/esm/src/configs/batch.js +61 -1
- package/build/esm/src/configs/chains.js +40 -7
- package/build/esm/src/configs/contracts.js +152 -40
- package/build/esm/src/configs/dataStore.js +37 -1
- package/build/esm/src/configs/express.js +6 -1
- package/build/esm/src/configs/factors.js +1 -1
- package/build/esm/src/configs/markets.js +164 -1
- package/build/esm/src/configs/oracleKeeper.js +20 -30
- package/build/esm/src/configs/tokens.js +271 -2
- package/build/esm/src/index.js +3 -3
- package/build/esm/src/modules/markets/index.js +9 -4
- package/build/esm/src/modules/markets/query-builders.js +28 -8
- package/build/esm/src/modules/orders/helpers.js +10 -1
- package/build/esm/src/modules/orders/helpers.spec.js +2 -2
- package/build/esm/src/modules/orders/orders.js +3 -3
- package/build/esm/src/modules/orders/utils.js +4 -5
- package/build/esm/src/modules/positions/positions.js +40 -7
- package/build/esm/src/modules/utils/utils.js +4 -3
- package/build/esm/src/prebuilt/hashedKinkModelMarketRatesKeys.json +186 -0
- package/build/esm/src/prebuilt/hashedMarketConfigKeys.json +2442 -512
- package/build/esm/src/prebuilt/hashedMarketValuesKeys.json +347 -0
- package/build/esm/src/test/mock.js +9 -4
- package/build/esm/src/types/subsquid.js +596 -0
- package/build/esm/src/types/swapStrategy.js +1 -0
- package/build/esm/src/types/trade.js +1 -0
- package/build/esm/src/utils/LruCache.js +4 -0
- package/build/esm/src/utils/__tests__/LruCache.spec.js +18 -0
- package/build/esm/src/utils/__tests__/numbers.spec.js +18 -16
- package/build/esm/src/utils/__tests__/orderTransactions/batchUtils.spec.js +0 -1
- package/build/esm/src/utils/__tests__/orderTransactions/decreaseOrders.spec.js +8 -0
- package/build/esm/src/utils/__tests__/orderTransactions/exchangeRouterMulticall.spec.js +40 -30
- package/build/esm/src/utils/__tests__/orderTransactions/increaseOrders.spec.js +12 -0
- package/build/esm/src/utils/__tests__/orderTransactions/swapOrders.spec.js +7 -1
- package/build/esm/src/utils/__tests__/positions.spec.js +23 -6
- package/build/esm/src/utils/__tests__/uiFeeReceiver.spec.js +11 -1
- package/build/esm/src/utils/bigmath.js +9 -2
- package/build/esm/src/utils/errors/parseError.js +31 -0
- package/build/esm/src/utils/errors/transactionsErrors.js +7 -1
- package/build/esm/src/utils/fees/executionFee.js +5 -3
- package/build/esm/src/utils/fees/index.js +8 -6
- package/build/esm/src/utils/fees/priceImpact.js +88 -30
- package/build/esm/src/utils/gelatoRelay.js +2 -0
- package/build/esm/src/utils/marketKeysAndConfigs.js +25 -5
- package/build/esm/src/utils/markets.js +7 -1
- package/build/esm/src/utils/numbers.js +18 -17
- package/build/esm/src/utils/orderTransactions.js +5 -2
- package/build/esm/src/utils/positions.js +75 -11
- package/build/esm/src/utils/prices.js +20 -14
- package/build/esm/src/utils/swap/__tests__/mockRouting.spec.js +1 -0
- package/build/esm/src/utils/swap/botanixStaking.js +83 -0
- package/build/esm/src/utils/swap/buildSwapStrategy.js +219 -0
- package/build/esm/src/utils/swap/externalSwapPath.js +37 -0
- package/build/esm/src/utils/swap/externalSwapQuoteByPath.js +23 -0
- package/build/esm/src/utils/swap/swapStats.js +6 -3
- package/build/esm/src/utils/swap/swapValues.js +248 -9
- package/build/esm/src/utils/trade/decrease.js +460 -0
- package/build/esm/src/utils/trade/{amounts.js → increase.js} +176 -74
- package/build/esm/src/utils/trade/trade.js +2 -2
- package/build/esm/src/utils/tradeHistory.js +3 -0
- package/build/esm/src/utils/twap/uiFeeReceiver.js +9 -9
- package/build/types/src/abis/index.d.ts +1 -1
- package/build/types/src/configs/batch.d.ts +2 -1
- package/build/types/src/configs/chains.d.ts +25 -20
- package/build/types/src/configs/contracts.d.ts +118 -8
- package/build/types/src/configs/dataStore.d.ts +20 -0
- package/build/types/src/configs/express.d.ts +1 -0
- package/build/types/src/configs/factors.d.ts +1 -1
- package/build/types/src/configs/markets.d.ts +2 -2
- package/build/types/src/configs/oracleKeeper.d.ts +2 -3
- package/build/types/src/configs/tokens.d.ts +1 -0
- package/build/types/src/index.d.ts +1 -1
- package/build/types/src/modules/base.d.ts +1 -1
- package/build/types/src/modules/markets/query-builders.d.ts +3 -2
- package/build/types/src/modules/markets/types.d.ts +2 -2
- package/build/types/src/modules/orders/utils.d.ts +6 -5
- package/build/types/src/types/fees.d.ts +1 -0
- package/build/types/src/types/markets.d.ts +10 -5
- package/build/types/src/types/orders.d.ts +1 -1
- package/build/types/src/types/positions.d.ts +26 -2
- package/build/types/src/types/sdk.d.ts +2 -2
- package/build/types/src/types/subsquid.d.ts +1562 -9
- package/build/types/src/types/swapStrategy.d.ts +33 -0
- package/build/types/src/types/tokens.d.ts +12 -0
- package/build/types/src/types/trade.d.ts +32 -7
- package/build/types/src/types/tradeHistory.d.ts +3 -0
- package/build/types/src/utils/LruCache.d.ts +1 -0
- package/build/types/src/utils/bigmath.d.ts +2 -1
- package/build/types/src/utils/errors/parseError.d.ts +11 -0
- package/build/types/src/utils/fees/executionFee.d.ts +6 -3
- package/build/types/src/utils/fees/index.d.ts +2 -2
- package/build/types/src/utils/fees/priceImpact.d.ts +33 -5
- package/build/types/src/utils/marketKeysAndConfigs.d.ts +9 -4
- package/build/types/src/utils/markets.d.ts +7 -0
- package/build/types/src/utils/multicall.d.ts +1 -1
- package/build/types/src/utils/numbers.d.ts +6 -3
- package/build/types/src/utils/orderTransactions.d.ts +6 -4
- package/build/types/src/utils/positions.d.ts +30 -0
- package/build/types/src/utils/prices.d.ts +4 -0
- package/build/types/src/utils/swap/botanixStaking.d.ts +11 -0
- package/build/types/src/utils/swap/buildSwapStrategy.d.ts +22 -0
- package/build/types/src/utils/swap/externalSwapPath.d.ts +13 -0
- package/build/types/src/utils/swap/externalSwapQuoteByPath.d.ts +6 -0
- package/build/types/src/utils/swap/swapValues.d.ts +11 -4
- package/build/types/src/utils/trade/decrease.d.ts +77 -0
- package/build/types/src/utils/trade/{amounts.d.ts → increase.d.ts} +20 -2
- package/build/types/src/utils/twap/uiFeeReceiver.d.ts +7 -7
- package/package.json +2 -2
|
@@ -7,6 +7,7 @@ exports.getIsInvalidBatchReceiver = exports.getBatchIsNativePayment = exports.ge
|
|
|
7
7
|
const uniq_1 = __importDefault(require("lodash/uniq"));
|
|
8
8
|
const viem_1 = require("viem");
|
|
9
9
|
const ExchangeRouter_json_1 = __importDefault(require("../abis/ExchangeRouter.json"));
|
|
10
|
+
const index_1 = require("../abis/index");
|
|
10
11
|
const Token_json_1 = __importDefault(require("../abis/Token.json"));
|
|
11
12
|
const chains_1 = require("../configs/chains");
|
|
12
13
|
const contracts_1 = require("../configs/contracts");
|
|
@@ -48,6 +49,7 @@ function buildSwapOrderPayload(p) {
|
|
|
48
49
|
shouldUnwrapNativeToken: tokenTransfersParams.isNativePayment || tokenTransfersParams.isNativeReceive,
|
|
49
50
|
autoCancel: p.autoCancel,
|
|
50
51
|
referralCode: p.referralCode ?? viem_1.zeroHash,
|
|
52
|
+
dataList: [],
|
|
51
53
|
};
|
|
52
54
|
return {
|
|
53
55
|
params: p,
|
|
@@ -103,6 +105,7 @@ function buildIncreaseOrderPayload(p) {
|
|
|
103
105
|
shouldUnwrapNativeToken: tokenTransfersParams.isNativePayment,
|
|
104
106
|
autoCancel: p.autoCancel,
|
|
105
107
|
referralCode: p.referralCode ?? viem_1.zeroHash,
|
|
108
|
+
dataList: [],
|
|
106
109
|
};
|
|
107
110
|
return {
|
|
108
111
|
params: p,
|
|
@@ -154,6 +157,7 @@ function buildDecreaseOrderPayload(p) {
|
|
|
154
157
|
shouldUnwrapNativeToken: p.receiveTokenAddress === tokens_1.NATIVE_TOKEN_ADDRESS,
|
|
155
158
|
autoCancel: p.autoCancel,
|
|
156
159
|
referralCode: p.referralCode ?? viem_1.zeroHash,
|
|
160
|
+
dataList: [],
|
|
157
161
|
};
|
|
158
162
|
return {
|
|
159
163
|
params: p,
|
|
@@ -380,7 +384,6 @@ function buildTokenTransfersParamsForIncreaseOrSwap({ chainId, receiver, payToke
|
|
|
380
384
|
* */
|
|
381
385
|
initialCollateralTokenAddress = (0, tokens_1.convertTokenAddress)(chainId, externalSwapQuote.outTokenAddress, "wrapped");
|
|
382
386
|
initialCollateralDeltaAmount = 0n;
|
|
383
|
-
swapPath = [];
|
|
384
387
|
externalCalls = getExternalCallsPayload({
|
|
385
388
|
chainId,
|
|
386
389
|
account: receiver,
|
|
@@ -589,7 +592,7 @@ function buildCancelOrderMulticall({ params }) {
|
|
|
589
592
|
exports.buildCancelOrderMulticall = buildCancelOrderMulticall;
|
|
590
593
|
function encodeExchangeRouterMulticall(multicall) {
|
|
591
594
|
const encodedMulticall = multicall.map((call) => (0, viem_1.encodeFunctionData)({
|
|
592
|
-
abi:
|
|
595
|
+
abi: index_1.abis.ExchangeRouter,
|
|
593
596
|
functionName: call.method,
|
|
594
597
|
args: call.params,
|
|
595
598
|
}));
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getLiquidationPrice = exports.getLeverage = exports.getPositionNetValue = exports.getPositionPendingFeesUsd = exports.getPositionValueUsd = exports.getPositionPnlUsd = exports.getEntryPrice = exports.parsePositionKey = exports.getPositionKey = void 0;
|
|
3
|
+
exports.getMinCollateralFactorForPosition = exports.getPriceImpactDiffUsd = exports.getNetPriceImpactDeltaUsdForDecrease = exports.getLiquidationPrice = exports.getLeverage = exports.getPositionPnlAfterFees = exports.getPositionNetValue = exports.getPositionPendingFeesUsd = exports.getPositionValueUsd = exports.getPositionPnlUsd = exports.getEntryPrice = exports.parsePositionKey = exports.getPositionKey = void 0;
|
|
4
4
|
const factors_1 = require("../configs/factors");
|
|
5
5
|
const bigmath_1 = require("./bigmath");
|
|
6
6
|
const fees_1 = require("./fees");
|
|
@@ -57,11 +57,22 @@ function getPositionPendingFeesUsd(p) {
|
|
|
57
57
|
}
|
|
58
58
|
exports.getPositionPendingFeesUsd = getPositionPendingFeesUsd;
|
|
59
59
|
function getPositionNetValue(p) {
|
|
60
|
-
const { pnl, closingFeeUsd, collateralUsd, uiFeeUsd } = p;
|
|
60
|
+
const { pnl, closingFeeUsd, collateralUsd, uiFeeUsd, totalPendingImpactDeltaUsd, priceImpactDiffUsd } = p;
|
|
61
61
|
const pendingFeesUsd = getPositionPendingFeesUsd(p);
|
|
62
|
-
return collateralUsd - pendingFeesUsd - closingFeeUsd - uiFeeUsd + pnl;
|
|
62
|
+
return (collateralUsd - pendingFeesUsd - closingFeeUsd - uiFeeUsd + pnl + totalPendingImpactDeltaUsd + priceImpactDiffUsd);
|
|
63
63
|
}
|
|
64
64
|
exports.getPositionNetValue = getPositionNetValue;
|
|
65
|
+
function getPositionPnlAfterFees({ pnl, pendingBorrowingFeesUsd, pendingFundingFeesUsd, closingFeeUsd, uiFeeUsd, totalPendingImpactDeltaUsd, priceImpactDiffUsd, }) {
|
|
66
|
+
const pnlAfterFees = pnl -
|
|
67
|
+
pendingBorrowingFeesUsd -
|
|
68
|
+
pendingFundingFeesUsd -
|
|
69
|
+
closingFeeUsd -
|
|
70
|
+
uiFeeUsd +
|
|
71
|
+
totalPendingImpactDeltaUsd +
|
|
72
|
+
priceImpactDiffUsd;
|
|
73
|
+
return pnlAfterFees;
|
|
74
|
+
}
|
|
75
|
+
exports.getPositionPnlAfterFees = getPositionPnlAfterFees;
|
|
65
76
|
function getLeverage(p) {
|
|
66
77
|
const { pnl, sizeInUsd, collateralUsd, pendingBorrowingFeesUsd, pendingFundingFeesUsd } = p;
|
|
67
78
|
const totalPendingFeesUsd = getPositionPendingFeesUsd({ pendingFundingFeesUsd, pendingBorrowingFeesUsd });
|
|
@@ -73,7 +84,7 @@ function getLeverage(p) {
|
|
|
73
84
|
}
|
|
74
85
|
exports.getLeverage = getLeverage;
|
|
75
86
|
function getLiquidationPrice(p) {
|
|
76
|
-
const { sizeInUsd, sizeInTokens, collateralUsd, collateralAmount, marketInfo, collateralToken, pendingFundingFeesUsd, pendingBorrowingFeesUsd, minCollateralUsd, isLong, userReferralInfo, useMaxPriceImpact, } = p;
|
|
87
|
+
const { sizeInUsd, sizeInTokens, collateralUsd, collateralAmount, marketInfo, collateralToken, pendingFundingFeesUsd, pendingBorrowingFeesUsd, pendingImpactAmount, minCollateralUsd, isLong, userReferralInfo, useMaxPriceImpact, } = p;
|
|
77
88
|
if (sizeInUsd <= 0 || sizeInTokens <= 0) {
|
|
78
89
|
return undefined;
|
|
79
90
|
}
|
|
@@ -87,16 +98,21 @@ function getLiquidationPrice(p) {
|
|
|
87
98
|
priceImpactDeltaUsd = maxNegativePriceImpactUsd;
|
|
88
99
|
}
|
|
89
100
|
else {
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
101
|
+
const priceImpactForPosition = (0, fees_1.getPriceImpactForPosition)(marketInfo, -sizeInUsd, isLong, { fallbackToZero: true });
|
|
102
|
+
priceImpactDeltaUsd = priceImpactForPosition.priceImpactDeltaUsd;
|
|
103
|
+
if (priceImpactDeltaUsd > 0) {
|
|
104
|
+
priceImpactDeltaUsd = (0, fees_1.capPositionImpactUsdByMaxPriceImpactFactor)(marketInfo, sizeInUsd, priceImpactDeltaUsd);
|
|
93
105
|
}
|
|
94
|
-
|
|
106
|
+
const pendingImpactUsd = (0, tokens_1.convertToUsd)(pendingImpactAmount, marketInfo.indexToken.decimals, pendingImpactAmount > 0 ? marketInfo.indexToken.prices.minPrice : marketInfo.indexToken.prices.maxPrice);
|
|
107
|
+
priceImpactDeltaUsd = priceImpactDeltaUsd + pendingImpactUsd;
|
|
95
108
|
if (priceImpactDeltaUsd > 0) {
|
|
96
109
|
priceImpactDeltaUsd = 0n;
|
|
97
110
|
}
|
|
111
|
+
else if (priceImpactDeltaUsd < maxNegativePriceImpactUsd) {
|
|
112
|
+
priceImpactDeltaUsd = maxNegativePriceImpactUsd;
|
|
113
|
+
}
|
|
98
114
|
}
|
|
99
|
-
let liquidationCollateralUsd = (0, numbers_1.applyFactor)(sizeInUsd, marketInfo.
|
|
115
|
+
let liquidationCollateralUsd = (0, numbers_1.applyFactor)(sizeInUsd, marketInfo.minCollateralFactorForLiquidation);
|
|
100
116
|
if (liquidationCollateralUsd < minCollateralUsd) {
|
|
101
117
|
liquidationCollateralUsd = minCollateralUsd;
|
|
102
118
|
}
|
|
@@ -143,3 +159,55 @@ function getLiquidationPrice(p) {
|
|
|
143
159
|
return liquidationPrice;
|
|
144
160
|
}
|
|
145
161
|
exports.getLiquidationPrice = getLiquidationPrice;
|
|
162
|
+
function getNetPriceImpactDeltaUsdForDecrease({ marketInfo, sizeInUsd, pendingImpactAmount, priceImpactDeltaUsd, sizeDeltaUsd, }) {
|
|
163
|
+
const { proportionalPendingImpactDeltaUsd } = (0, fees_1.getProportionalPendingImpactValues)({
|
|
164
|
+
sizeInUsd,
|
|
165
|
+
sizeDeltaUsd,
|
|
166
|
+
pendingImpactAmount,
|
|
167
|
+
indexToken: marketInfo.indexToken,
|
|
168
|
+
});
|
|
169
|
+
let totalImpactDeltaUsd = priceImpactDeltaUsd + proportionalPendingImpactDeltaUsd;
|
|
170
|
+
const priceImpactDiffUsd = getPriceImpactDiffUsd({
|
|
171
|
+
totalImpactDeltaUsd,
|
|
172
|
+
marketInfo,
|
|
173
|
+
sizeDeltaUsd,
|
|
174
|
+
});
|
|
175
|
+
if (totalImpactDeltaUsd > 0) {
|
|
176
|
+
totalImpactDeltaUsd = (0, fees_1.capPositionImpactUsdByMaxPriceImpactFactor)(marketInfo, sizeDeltaUsd, totalImpactDeltaUsd);
|
|
177
|
+
}
|
|
178
|
+
totalImpactDeltaUsd = (0, fees_1.capPositionImpactUsdByMaxImpactPool)(marketInfo, totalImpactDeltaUsd);
|
|
179
|
+
return {
|
|
180
|
+
totalImpactDeltaUsd,
|
|
181
|
+
proportionalPendingImpactDeltaUsd,
|
|
182
|
+
priceImpactDiffUsd,
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
exports.getNetPriceImpactDeltaUsdForDecrease = getNetPriceImpactDeltaUsdForDecrease;
|
|
186
|
+
function getPriceImpactDiffUsd({ totalImpactDeltaUsd, marketInfo, sizeDeltaUsd, }) {
|
|
187
|
+
if (totalImpactDeltaUsd > 0) {
|
|
188
|
+
return 0n;
|
|
189
|
+
}
|
|
190
|
+
const { maxNegativeImpactFactor } = (0, fees_1.getMaxPositionImpactFactors)(marketInfo);
|
|
191
|
+
const maxNegativeImpactUsd = -(0, numbers_1.applyFactor)(sizeDeltaUsd, maxNegativeImpactFactor);
|
|
192
|
+
let priceImpactDiffUsd = 0n;
|
|
193
|
+
if (totalImpactDeltaUsd < maxNegativeImpactUsd) {
|
|
194
|
+
priceImpactDiffUsd = maxNegativeImpactUsd - totalImpactDeltaUsd;
|
|
195
|
+
}
|
|
196
|
+
return priceImpactDiffUsd;
|
|
197
|
+
}
|
|
198
|
+
exports.getPriceImpactDiffUsd = getPriceImpactDiffUsd;
|
|
199
|
+
function getMinCollateralFactorForPosition(position, openInterestDelta) {
|
|
200
|
+
const marketInfo = position.marketInfo;
|
|
201
|
+
const isLong = position.isLong;
|
|
202
|
+
const openInterest = (0, markets_1.getOpenInterestUsd)(marketInfo, isLong) + openInterestDelta;
|
|
203
|
+
const minCollateralFactorMultiplier = isLong
|
|
204
|
+
? marketInfo.minCollateralFactorForOpenInterestLong
|
|
205
|
+
: marketInfo.minCollateralFactorForOpenInterestShort;
|
|
206
|
+
let minCollateralFactor = bigmath_1.bigMath.mulDiv(openInterest, minCollateralFactorMultiplier, numbers_1.PRECISION);
|
|
207
|
+
const minCollateralFactorForMarket = marketInfo.minCollateralFactor;
|
|
208
|
+
if (minCollateralFactorForMarket > minCollateralFactor) {
|
|
209
|
+
minCollateralFactor = minCollateralFactorForMarket;
|
|
210
|
+
}
|
|
211
|
+
return minCollateralFactor;
|
|
212
|
+
}
|
|
213
|
+
exports.getMinCollateralFactorForPosition = getMinCollateralFactorForPosition;
|
|
@@ -6,10 +6,7 @@ const orders_1 = require("../types/orders");
|
|
|
6
6
|
const trade_1 = require("../types/trade");
|
|
7
7
|
const bigmath_1 = require("./bigmath");
|
|
8
8
|
const fees_1 = require("./fees");
|
|
9
|
-
const fees_2 = require("./fees");
|
|
10
9
|
const numbers_1 = require("./numbers");
|
|
11
|
-
const numbers_2 = require("./numbers");
|
|
12
|
-
const numbers_3 = require("./numbers");
|
|
13
10
|
const tokens_1 = require("./tokens");
|
|
14
11
|
function getMarkPrice(p) {
|
|
15
12
|
const { prices, isIncrease, isLong } = p;
|
|
@@ -47,9 +44,12 @@ function getAcceptablePriceInfo(p) {
|
|
|
47
44
|
const values = {
|
|
48
45
|
acceptablePrice: 0n,
|
|
49
46
|
acceptablePriceDeltaBps: 0n,
|
|
47
|
+
cappedPriceImpactDeltaUsd: 0n,
|
|
48
|
+
cappedPriceImpactDeltaAmount: 0n,
|
|
50
49
|
priceImpactDeltaAmount: 0n,
|
|
51
50
|
priceImpactDeltaUsd: 0n,
|
|
52
51
|
priceImpactDiffUsd: 0n,
|
|
52
|
+
balanceWasImproved: false,
|
|
53
53
|
};
|
|
54
54
|
if (sizeDeltaUsd <= 0 || indexPrice == 0n) {
|
|
55
55
|
return values;
|
|
@@ -72,28 +72,34 @@ function getAcceptablePriceInfo(p) {
|
|
|
72
72
|
values.priceImpactDeltaAmount = priceImpact.priceImpactDeltaAmount;
|
|
73
73
|
return values;
|
|
74
74
|
}
|
|
75
|
-
|
|
75
|
+
const { priceImpactDeltaUsd, balanceWasImproved } = (0, fees_1.getCappedPositionImpactUsd)(marketInfo, sizeDeltaUsd, isLong, isIncrease, {
|
|
76
76
|
fallbackToZero: !isIncrease,
|
|
77
|
+
shouldCapNegativeImpact: false,
|
|
77
78
|
});
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
79
|
+
/**
|
|
80
|
+
* We display this value as price impact on action (increase or decrease)
|
|
81
|
+
* But for acceptable price calculation uncapped price impact is used
|
|
82
|
+
* Also on decrease action we calculate totalImpactUsd which will be deducted from the collateral
|
|
83
|
+
*/
|
|
84
|
+
values.priceImpactDeltaUsd = priceImpactDeltaUsd;
|
|
85
|
+
values.balanceWasImproved = balanceWasImproved;
|
|
85
86
|
if (values.priceImpactDeltaUsd > 0) {
|
|
86
87
|
values.priceImpactDeltaAmount = (0, tokens_1.convertToTokenAmount)(values.priceImpactDeltaUsd, indexToken.decimals, indexToken.prices.maxPrice);
|
|
87
88
|
}
|
|
88
89
|
else {
|
|
89
|
-
values.priceImpactDeltaAmount = (0,
|
|
90
|
+
values.priceImpactDeltaAmount = (0, numbers_1.roundUpMagnitudeDivision)(values.priceImpactDeltaUsd * (0, numbers_1.expandDecimals)(1, indexToken.decimals), indexToken.prices.minPrice);
|
|
90
91
|
}
|
|
92
|
+
// Use uncapped price impact for the acceptable price calculation
|
|
93
|
+
const { priceImpactDeltaUsd: priceImpactDeltaUsdForAcceptablePrice } = (0, fees_1.getCappedPositionImpactUsd)(marketInfo, sizeDeltaUsd, isLong, isIncrease, {
|
|
94
|
+
fallbackToZero: !isIncrease,
|
|
95
|
+
shouldCapNegativeImpact: false,
|
|
96
|
+
});
|
|
91
97
|
const acceptablePriceValues = getAcceptablePriceByPriceImpact({
|
|
92
98
|
isIncrease,
|
|
93
99
|
isLong,
|
|
94
100
|
indexPrice,
|
|
95
101
|
sizeDeltaUsd,
|
|
96
|
-
priceImpactDeltaUsd:
|
|
102
|
+
priceImpactDeltaUsd: priceImpactDeltaUsdForAcceptablePrice,
|
|
97
103
|
});
|
|
98
104
|
values.acceptablePrice = acceptablePriceValues.acceptablePrice;
|
|
99
105
|
values.acceptablePriceDeltaBps = acceptablePriceValues.acceptablePriceDeltaBps;
|
|
@@ -41,6 +41,7 @@ const baseGasLimits = {
|
|
|
41
41
|
cancelOrderGasLimit: 1000000n,
|
|
42
42
|
gelatoRelayFeeMultiplierFactor: 1000000000000000000000000000000n,
|
|
43
43
|
tokenPermitGasLimit: 1000000n,
|
|
44
|
+
gmxAccountCollateralGasLimit: 0n,
|
|
44
45
|
};
|
|
45
46
|
const baseGasPrice = 1650000002n;
|
|
46
47
|
(0, vitest_1.describe)("mockRouting", () => {
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.getBotanixStakingExternalSwapQuote = void 0;
|
|
7
|
+
const viem_1 = require("viem");
|
|
8
|
+
const StBTC_json_1 = __importDefault(require("../../abis/StBTC.json"));
|
|
9
|
+
const chains_1 = require("../../configs/chains");
|
|
10
|
+
const contracts_1 = require("../../configs/contracts");
|
|
11
|
+
const trade_1 = require("../../types/trade");
|
|
12
|
+
const bigmath_1 = require("../bigmath");
|
|
13
|
+
const numbers_1 = require("../numbers");
|
|
14
|
+
const externalSwapPath_1 = require("./externalSwapPath");
|
|
15
|
+
const tokens_1 = require("../tokens");
|
|
16
|
+
const COEF_REDUCER = (0, numbers_1.getBasisPoints)(1n, 10000n);
|
|
17
|
+
const getBotanixStakingExternalSwapQuote = ({ tokenInAddress, tokenOutAddress, amountIn, gasPrice, receiverAddress, tokensData, assetsPerShare, }) => {
|
|
18
|
+
const inTokenData = (0, tokens_1.getTokenData)(tokensData, tokenInAddress);
|
|
19
|
+
const outTokenData = (0, tokens_1.getTokenData)(tokensData, tokenOutAddress);
|
|
20
|
+
const assetsPerShareRate = (0, numbers_1.getBasisPoints)(assetsPerShare, 10n ** 18n) - COEF_REDUCER;
|
|
21
|
+
const sharesPerAssetRate = (0, numbers_1.getBasisPoints)(10n ** 18n, assetsPerShare) - COEF_REDUCER;
|
|
22
|
+
if (!inTokenData || !outTokenData) {
|
|
23
|
+
return undefined;
|
|
24
|
+
}
|
|
25
|
+
if (externalSwapPath_1.AVAILABLE_BOTANIX_DEPOSIT_PAIRS.some((pair) => pair.from === tokenInAddress && pair.to === tokenOutAddress)) {
|
|
26
|
+
const priceIn = (0, tokens_1.getMidPrice)(inTokenData.prices);
|
|
27
|
+
const priceOut = bigmath_1.bigMath.mulDiv(priceIn, sharesPerAssetRate, numbers_1.BASIS_POINTS_DIVISOR_BIGINT);
|
|
28
|
+
const usdIn = (0, tokens_1.convertToUsd)(amountIn, inTokenData.decimals, priceIn);
|
|
29
|
+
const amountOut = amountIn > 0n ? bigmath_1.bigMath.mulDiv(amountIn, sharesPerAssetRate, numbers_1.BASIS_POINTS_DIVISOR_BIGINT) - gasPrice : 0n;
|
|
30
|
+
const usdOut = amountOut > 0n ? (0, tokens_1.convertToUsd)(amountOut, outTokenData.decimals, priceOut) : 0n;
|
|
31
|
+
return {
|
|
32
|
+
aggregator: trade_1.ExternalSwapAggregator.BotanixStaking,
|
|
33
|
+
inTokenAddress: tokenInAddress,
|
|
34
|
+
outTokenAddress: tokenOutAddress,
|
|
35
|
+
receiver: receiverAddress,
|
|
36
|
+
amountIn,
|
|
37
|
+
amountOut,
|
|
38
|
+
usdIn: usdIn,
|
|
39
|
+
usdOut: usdOut,
|
|
40
|
+
priceIn,
|
|
41
|
+
priceOut,
|
|
42
|
+
feesUsd: gasPrice,
|
|
43
|
+
needSpenderApproval: true,
|
|
44
|
+
txnData: {
|
|
45
|
+
to: (0, contracts_1.getContract)(chains_1.BOTANIX, "StBTC"),
|
|
46
|
+
data: (0, viem_1.encodeFunctionData)({
|
|
47
|
+
abi: StBTC_json_1.default.abi,
|
|
48
|
+
functionName: "deposit",
|
|
49
|
+
args: [amountIn, receiverAddress],
|
|
50
|
+
}),
|
|
51
|
+
value: 0n,
|
|
52
|
+
estimatedGas: gasPrice,
|
|
53
|
+
estimatedExecutionFee: gasPrice,
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
if (externalSwapPath_1.AVAILABLE_BOTANIX_WITHDRAW_PAIRS.some((pair) => pair.from === tokenInAddress && pair.to === tokenOutAddress)) {
|
|
58
|
+
const priceIn = (0, tokens_1.getMidPrice)(inTokenData.prices);
|
|
59
|
+
const priceOut = bigmath_1.bigMath.mulDiv(priceIn, assetsPerShareRate, numbers_1.BASIS_POINTS_DIVISOR_BIGINT);
|
|
60
|
+
const usdIn = (0, tokens_1.convertToUsd)(amountIn, inTokenData.decimals, priceIn);
|
|
61
|
+
const amountOut = amountIn > 0n ? bigmath_1.bigMath.mulDiv(amountIn, assetsPerShareRate, numbers_1.BASIS_POINTS_DIVISOR_BIGINT) - gasPrice : 0n;
|
|
62
|
+
const usdOut = amountOut > 0n ? (0, tokens_1.convertToUsd)(amountOut, outTokenData.decimals, priceOut) : 0n;
|
|
63
|
+
return {
|
|
64
|
+
aggregator: trade_1.ExternalSwapAggregator.BotanixStaking,
|
|
65
|
+
inTokenAddress: tokenInAddress,
|
|
66
|
+
outTokenAddress: tokenOutAddress,
|
|
67
|
+
receiver: receiverAddress,
|
|
68
|
+
amountIn,
|
|
69
|
+
amountOut,
|
|
70
|
+
usdIn: usdIn,
|
|
71
|
+
usdOut: usdOut,
|
|
72
|
+
priceIn,
|
|
73
|
+
priceOut,
|
|
74
|
+
feesUsd: gasPrice,
|
|
75
|
+
needSpenderApproval: true,
|
|
76
|
+
txnData: {
|
|
77
|
+
to: (0, contracts_1.getContract)(chains_1.BOTANIX, "StBTC"),
|
|
78
|
+
data: (0, viem_1.encodeFunctionData)({
|
|
79
|
+
abi: StBTC_json_1.default.abi,
|
|
80
|
+
functionName: "withdraw",
|
|
81
|
+
args: [amountIn, receiverAddress, (0, contracts_1.getContract)(chains_1.BOTANIX, "ExternalHandler")],
|
|
82
|
+
}),
|
|
83
|
+
value: 0n,
|
|
84
|
+
estimatedGas: gasPrice,
|
|
85
|
+
estimatedExecutionFee: gasPrice,
|
|
86
|
+
},
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
exports.getBotanixStakingExternalSwapQuote = getBotanixStakingExternalSwapQuote;
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.buildReverseSwapStrategy = exports.buildSwapStrategy = void 0;
|
|
4
|
+
const bigmath_1 = require("../bigmath");
|
|
5
|
+
const tokens_1 = require("../tokens");
|
|
6
|
+
const externalSwapPath_1 = require("./externalSwapPath");
|
|
7
|
+
const externalSwapQuoteByPath_1 = require("./externalSwapQuoteByPath");
|
|
8
|
+
const swapPath_1 = require("./swapPath");
|
|
9
|
+
/*
|
|
10
|
+
Order/Priority of getting swap strategy:
|
|
11
|
+
1. Check if it needs a swap and return noSwap if tokens are equivalent, stake or unstake [noSwap]
|
|
12
|
+
2. Check if there is a swap path stats for the internal swap quote and return internalSwap if there is [internalSwap]
|
|
13
|
+
3. Check if there is a combined swap strategy and return combinedSwap if there is [combinedSwap]
|
|
14
|
+
4. Return defaultSwapStrategy (noSwap) if there is no other swap strategy [noSwap]
|
|
15
|
+
*/
|
|
16
|
+
function buildSwapStrategy({ amountIn, tokenIn, tokenOut, marketsInfoData, chainId, swapOptimizationOrder, externalSwapQuoteParams, }) {
|
|
17
|
+
const priceIn = tokenIn.prices.minPrice;
|
|
18
|
+
const usdIn = (0, tokens_1.convertToUsd)(amountIn, tokenIn.decimals, priceIn);
|
|
19
|
+
if (amountIn < 0n) {
|
|
20
|
+
amountIn = 0n;
|
|
21
|
+
}
|
|
22
|
+
const defaultSwapStrategy = {
|
|
23
|
+
type: "noSwap",
|
|
24
|
+
externalSwapQuote: undefined,
|
|
25
|
+
swapPathStats: undefined,
|
|
26
|
+
amountIn,
|
|
27
|
+
amountOut: (0, tokens_1.convertToTokenAmount)(usdIn, tokenOut.decimals, tokenOut.prices.maxPrice),
|
|
28
|
+
usdIn,
|
|
29
|
+
usdOut: usdIn,
|
|
30
|
+
priceIn,
|
|
31
|
+
priceOut: tokenOut.prices.maxPrice,
|
|
32
|
+
feesUsd: 0n,
|
|
33
|
+
};
|
|
34
|
+
if ((0, tokens_1.getIsEquivalentTokens)(tokenIn, tokenOut) || (0, tokens_1.getIsStake)(tokenIn, tokenOut) || (0, tokens_1.getIsUnstake)(tokenIn, tokenOut)) {
|
|
35
|
+
return defaultSwapStrategy;
|
|
36
|
+
}
|
|
37
|
+
const findSwapPath = (0, swapPath_1.createFindSwapPath)({
|
|
38
|
+
chainId,
|
|
39
|
+
fromTokenAddress: tokenIn.address,
|
|
40
|
+
toTokenAddress: tokenOut.address,
|
|
41
|
+
marketsInfoData,
|
|
42
|
+
isExpressFeeSwap: false,
|
|
43
|
+
});
|
|
44
|
+
const swapPathStats = findSwapPath(usdIn, { order: swapOptimizationOrder });
|
|
45
|
+
if (swapPathStats) {
|
|
46
|
+
return {
|
|
47
|
+
type: "internalSwap",
|
|
48
|
+
swapPathStats,
|
|
49
|
+
externalSwapQuote: undefined,
|
|
50
|
+
amountIn,
|
|
51
|
+
amountOut: swapPathStats.amountOut,
|
|
52
|
+
usdIn: usdIn,
|
|
53
|
+
usdOut: swapPathStats.usdOut,
|
|
54
|
+
priceIn: priceIn,
|
|
55
|
+
priceOut: tokenOut.prices.maxPrice,
|
|
56
|
+
feesUsd: usdIn - swapPathStats.usdOut,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
const availableExternalSwapPaths = (0, externalSwapPath_1.getAvailableExternalSwapPaths)({ chainId, fromTokenAddress: tokenIn.address });
|
|
60
|
+
const suitableSwapPath = availableExternalSwapPaths.find((path) => {
|
|
61
|
+
const findSwapPath = (0, swapPath_1.createFindSwapPath)({
|
|
62
|
+
chainId,
|
|
63
|
+
fromTokenAddress: path.outTokenAddress,
|
|
64
|
+
toTokenAddress: tokenOut.address,
|
|
65
|
+
marketsInfoData,
|
|
66
|
+
isExpressFeeSwap: false,
|
|
67
|
+
});
|
|
68
|
+
const swapPathStats = findSwapPath(usdIn);
|
|
69
|
+
return Boolean(swapPathStats);
|
|
70
|
+
});
|
|
71
|
+
if (suitableSwapPath && suitableSwapPath.outTokenAddress !== tokenOut.address) {
|
|
72
|
+
const externalSwapQuoteForCombinedSwap = (0, externalSwapQuoteByPath_1.getExternalSwapQuoteByPath)({
|
|
73
|
+
amountIn,
|
|
74
|
+
externalSwapPath: suitableSwapPath,
|
|
75
|
+
externalSwapQuoteParams,
|
|
76
|
+
});
|
|
77
|
+
const findSwapPathForSuitableSwapPath = (0, swapPath_1.createFindSwapPath)({
|
|
78
|
+
chainId,
|
|
79
|
+
fromTokenAddress: suitableSwapPath.outTokenAddress,
|
|
80
|
+
toTokenAddress: tokenOut.address,
|
|
81
|
+
marketsInfoData,
|
|
82
|
+
isExpressFeeSwap: false,
|
|
83
|
+
});
|
|
84
|
+
const swapPathStatsForCombinedSwap = externalSwapQuoteForCombinedSwap
|
|
85
|
+
? findSwapPathForSuitableSwapPath(externalSwapQuoteForCombinedSwap.usdOut)
|
|
86
|
+
: undefined;
|
|
87
|
+
return externalSwapQuoteForCombinedSwap && swapPathStatsForCombinedSwap
|
|
88
|
+
? {
|
|
89
|
+
type: "combinedSwap",
|
|
90
|
+
externalSwapQuote: externalSwapQuoteForCombinedSwap,
|
|
91
|
+
swapPathStats: swapPathStatsForCombinedSwap,
|
|
92
|
+
amountIn,
|
|
93
|
+
amountOut: swapPathStatsForCombinedSwap.amountOut,
|
|
94
|
+
usdIn: externalSwapQuoteForCombinedSwap.usdIn,
|
|
95
|
+
usdOut: swapPathStatsForCombinedSwap.usdOut,
|
|
96
|
+
priceIn: externalSwapQuoteForCombinedSwap.priceIn,
|
|
97
|
+
priceOut: tokenOut.prices.maxPrice,
|
|
98
|
+
feesUsd: externalSwapQuoteForCombinedSwap.usdIn - swapPathStatsForCombinedSwap.usdOut,
|
|
99
|
+
}
|
|
100
|
+
: defaultSwapStrategy;
|
|
101
|
+
}
|
|
102
|
+
return defaultSwapStrategy;
|
|
103
|
+
}
|
|
104
|
+
exports.buildSwapStrategy = buildSwapStrategy;
|
|
105
|
+
// Used for getting swap amounts by to value
|
|
106
|
+
function buildReverseSwapStrategy({ amountOut, tokenIn, tokenOut, marketsInfoData, chainId, externalSwapQuoteParams, swapOptimizationOrder, }) {
|
|
107
|
+
const priceIn = (0, tokens_1.getMidPrice)(tokenIn.prices);
|
|
108
|
+
const priceOut = (0, tokens_1.getMidPrice)(tokenOut.prices);
|
|
109
|
+
const preferredUsdOut = (0, tokens_1.convertToUsd)(amountOut, tokenOut.decimals, (0, tokens_1.getMidPrice)(tokenOut.prices));
|
|
110
|
+
const approximateAmountIn = (0, tokens_1.convertToTokenAmount)(preferredUsdOut, tokenIn.decimals, (0, tokens_1.getMidPrice)(tokenIn.prices));
|
|
111
|
+
const approximateUsdIn = preferredUsdOut;
|
|
112
|
+
const defaultSwapStrategy = {
|
|
113
|
+
type: "noSwap",
|
|
114
|
+
externalSwapQuote: undefined,
|
|
115
|
+
swapPathStats: undefined,
|
|
116
|
+
amountIn: approximateAmountIn,
|
|
117
|
+
amountOut: amountOut,
|
|
118
|
+
usdIn: approximateUsdIn,
|
|
119
|
+
usdOut: preferredUsdOut,
|
|
120
|
+
priceIn,
|
|
121
|
+
priceOut,
|
|
122
|
+
feesUsd: 0n,
|
|
123
|
+
};
|
|
124
|
+
if ((0, tokens_1.getIsEquivalentTokens)(tokenIn, tokenOut) || (0, tokens_1.getIsStake)(tokenIn, tokenOut) || (0, tokens_1.getIsUnstake)(tokenIn, tokenOut)) {
|
|
125
|
+
return defaultSwapStrategy;
|
|
126
|
+
}
|
|
127
|
+
const findSwapPath = (0, swapPath_1.createFindSwapPath)({
|
|
128
|
+
chainId,
|
|
129
|
+
fromTokenAddress: tokenIn.address,
|
|
130
|
+
toTokenAddress: tokenOut.address,
|
|
131
|
+
marketsInfoData,
|
|
132
|
+
isExpressFeeSwap: false,
|
|
133
|
+
});
|
|
134
|
+
const approximateSwapPathStats = findSwapPath(approximateUsdIn, { order: swapOptimizationOrder });
|
|
135
|
+
if (approximateSwapPathStats) {
|
|
136
|
+
// Increase or decrease usdIn the same way preferred usdOut is different from swapStrategy.usdOut
|
|
137
|
+
// preferred_in / approximate_in = preferred_out / approximate_out
|
|
138
|
+
// preferred_in = approximate_in * preferred_out / approximate_out
|
|
139
|
+
const adjustedUsdIn = approximateSwapPathStats.usdOut > 0
|
|
140
|
+
? bigmath_1.bigMath.mulDiv(approximateUsdIn, preferredUsdOut, approximateSwapPathStats.usdOut)
|
|
141
|
+
: 0n;
|
|
142
|
+
const adjustedAmountIn = (0, tokens_1.convertToTokenAmount)(adjustedUsdIn, tokenIn.decimals, (0, tokens_1.getMidPrice)(tokenIn.prices));
|
|
143
|
+
const adjustedSwapPathStats = findSwapPath(adjustedUsdIn, { order: swapOptimizationOrder });
|
|
144
|
+
if (adjustedSwapPathStats) {
|
|
145
|
+
return {
|
|
146
|
+
type: "internalSwap",
|
|
147
|
+
swapPathStats: adjustedSwapPathStats,
|
|
148
|
+
externalSwapQuote: undefined,
|
|
149
|
+
amountIn: adjustedAmountIn,
|
|
150
|
+
amountOut: adjustedSwapPathStats.amountOut,
|
|
151
|
+
usdIn: adjustedUsdIn,
|
|
152
|
+
usdOut: adjustedSwapPathStats.usdOut,
|
|
153
|
+
priceIn: priceIn,
|
|
154
|
+
priceOut: priceOut,
|
|
155
|
+
feesUsd: adjustedUsdIn - adjustedSwapPathStats.usdOut,
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
const availableExternalSwapPaths = (0, externalSwapPath_1.getAvailableExternalSwapPaths)({ chainId, fromTokenAddress: tokenIn.address });
|
|
160
|
+
const suitableSwapPath = availableExternalSwapPaths.find((path) => {
|
|
161
|
+
if (path.outTokenAddress !== tokenOut.address)
|
|
162
|
+
return false;
|
|
163
|
+
const findSwapPath = (0, swapPath_1.createFindSwapPath)({
|
|
164
|
+
chainId,
|
|
165
|
+
fromTokenAddress: tokenIn.address,
|
|
166
|
+
toTokenAddress: path.inTokenAddress,
|
|
167
|
+
marketsInfoData,
|
|
168
|
+
isExpressFeeSwap: false,
|
|
169
|
+
});
|
|
170
|
+
const swapPathStats = findSwapPath(approximateUsdIn);
|
|
171
|
+
return Boolean(swapPathStats);
|
|
172
|
+
});
|
|
173
|
+
if (suitableSwapPath) {
|
|
174
|
+
const approximateExternalSwapQuoteForCombinedSwap = (0, externalSwapQuoteByPath_1.getExternalSwapQuoteByPath)({
|
|
175
|
+
amountIn: approximateAmountIn,
|
|
176
|
+
externalSwapPath: suitableSwapPath,
|
|
177
|
+
externalSwapQuoteParams,
|
|
178
|
+
});
|
|
179
|
+
if (!approximateExternalSwapQuoteForCombinedSwap) {
|
|
180
|
+
return defaultSwapStrategy;
|
|
181
|
+
}
|
|
182
|
+
const findSwapPathForSuitableSwapPath = (0, swapPath_1.createFindSwapPath)({
|
|
183
|
+
chainId,
|
|
184
|
+
fromTokenAddress: tokenIn.address,
|
|
185
|
+
toTokenAddress: suitableSwapPath.inTokenAddress,
|
|
186
|
+
marketsInfoData,
|
|
187
|
+
isExpressFeeSwap: false,
|
|
188
|
+
});
|
|
189
|
+
const approximateSwapPathStatsForCombinedSwap = findSwapPathForSuitableSwapPath(approximateExternalSwapQuoteForCombinedSwap.usdOut);
|
|
190
|
+
if (!approximateSwapPathStatsForCombinedSwap) {
|
|
191
|
+
return defaultSwapStrategy;
|
|
192
|
+
}
|
|
193
|
+
const adjustedUsdIn = approximateSwapPathStatsForCombinedSwap.usdOut > 0
|
|
194
|
+
? bigmath_1.bigMath.mulDiv(approximateUsdIn, preferredUsdOut, approximateSwapPathStatsForCombinedSwap.usdOut)
|
|
195
|
+
: 0n;
|
|
196
|
+
const adjustedAmountIn = (0, tokens_1.convertToTokenAmount)(adjustedUsdIn, tokenIn.decimals, (0, tokens_1.getMidPrice)(tokenIn.prices));
|
|
197
|
+
const adjustedExternalSwapQuoteForCombinedSwap = (0, externalSwapQuoteByPath_1.getExternalSwapQuoteByPath)({
|
|
198
|
+
amountIn: adjustedAmountIn,
|
|
199
|
+
externalSwapPath: suitableSwapPath,
|
|
200
|
+
externalSwapQuoteParams,
|
|
201
|
+
});
|
|
202
|
+
if (!adjustedExternalSwapQuoteForCombinedSwap) {
|
|
203
|
+
return defaultSwapStrategy;
|
|
204
|
+
}
|
|
205
|
+
const adjustedSwapPathStatsForCombinedSwap = findSwapPathForSuitableSwapPath(adjustedExternalSwapQuoteForCombinedSwap.usdOut);
|
|
206
|
+
if (!adjustedSwapPathStatsForCombinedSwap) {
|
|
207
|
+
return defaultSwapStrategy;
|
|
208
|
+
}
|
|
209
|
+
return {
|
|
210
|
+
type: "combinedSwap",
|
|
211
|
+
externalSwapQuote: adjustedExternalSwapQuoteForCombinedSwap,
|
|
212
|
+
swapPathStats: adjustedSwapPathStatsForCombinedSwap,
|
|
213
|
+
amountIn: adjustedAmountIn,
|
|
214
|
+
amountOut: adjustedSwapPathStatsForCombinedSwap.amountOut,
|
|
215
|
+
usdIn: adjustedExternalSwapQuoteForCombinedSwap.usdIn,
|
|
216
|
+
usdOut: adjustedSwapPathStatsForCombinedSwap.usdOut,
|
|
217
|
+
priceIn: adjustedExternalSwapQuoteForCombinedSwap.priceIn,
|
|
218
|
+
priceOut: priceOut,
|
|
219
|
+
feesUsd: adjustedExternalSwapQuoteForCombinedSwap.usdIn - adjustedSwapPathStatsForCombinedSwap.usdOut,
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
return defaultSwapStrategy;
|
|
223
|
+
}
|
|
224
|
+
exports.buildReverseSwapStrategy = buildReverseSwapStrategy;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getAvailableExternalSwapPaths = exports.AVAILABLE_BOTANIX_WITHDRAW_PAIRS = exports.AVAILABLE_BOTANIX_DEPOSIT_PAIRS = void 0;
|
|
4
|
+
const chains_1 = require("../../configs/chains");
|
|
5
|
+
const tokens_1 = require("../../configs/tokens");
|
|
6
|
+
const trade_1 = require("../../types/trade");
|
|
7
|
+
const BBTC_ADDRESS = tokens_1.NATIVE_TOKEN_ADDRESS;
|
|
8
|
+
const PBTC_ADDRESS = (0, tokens_1.getTokenBySymbol)(chains_1.BOTANIX, "PBTC").address;
|
|
9
|
+
const STBTC_ADDRESS = (0, tokens_1.getTokenBySymbol)(chains_1.BOTANIX, "STBTC").address;
|
|
10
|
+
exports.AVAILABLE_BOTANIX_DEPOSIT_PAIRS = [
|
|
11
|
+
{
|
|
12
|
+
from: BBTC_ADDRESS,
|
|
13
|
+
to: STBTC_ADDRESS,
|
|
14
|
+
},
|
|
15
|
+
{
|
|
16
|
+
from: PBTC_ADDRESS,
|
|
17
|
+
to: STBTC_ADDRESS,
|
|
18
|
+
},
|
|
19
|
+
];
|
|
20
|
+
exports.AVAILABLE_BOTANIX_WITHDRAW_PAIRS = [
|
|
21
|
+
{
|
|
22
|
+
from: STBTC_ADDRESS,
|
|
23
|
+
to: PBTC_ADDRESS,
|
|
24
|
+
},
|
|
25
|
+
];
|
|
26
|
+
const getBotanixStakingExternalSwapPaths = ({ fromTokenAddress }) => {
|
|
27
|
+
return [...exports.AVAILABLE_BOTANIX_DEPOSIT_PAIRS, ...exports.AVAILABLE_BOTANIX_WITHDRAW_PAIRS]
|
|
28
|
+
.filter((pair) => pair.from === fromTokenAddress)
|
|
29
|
+
.map((pair) => ({
|
|
30
|
+
aggregator: trade_1.ExternalSwapAggregator.BotanixStaking,
|
|
31
|
+
inTokenAddress: pair.from,
|
|
32
|
+
outTokenAddress: pair.to,
|
|
33
|
+
}));
|
|
34
|
+
};
|
|
35
|
+
const getAvailableExternalSwapPaths = ({ chainId, fromTokenAddress, }) => {
|
|
36
|
+
if (chainId === chains_1.BOTANIX) {
|
|
37
|
+
return getBotanixStakingExternalSwapPaths({ fromTokenAddress });
|
|
38
|
+
}
|
|
39
|
+
return [];
|
|
40
|
+
};
|
|
41
|
+
exports.getAvailableExternalSwapPaths = getAvailableExternalSwapPaths;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getExternalSwapQuoteByPath = void 0;
|
|
4
|
+
const trade_1 = require("../../types/trade");
|
|
5
|
+
const botanixStaking_1 = require("./botanixStaking");
|
|
6
|
+
const getExternalSwapQuoteByPath = ({ amountIn, externalSwapPath, externalSwapQuoteParams, }) => {
|
|
7
|
+
if (amountIn === undefined ||
|
|
8
|
+
externalSwapQuoteParams.gasPrice === undefined ||
|
|
9
|
+
externalSwapQuoteParams.tokensData === undefined ||
|
|
10
|
+
externalSwapQuoteParams.botanixStakingAssetsPerShare === undefined ||
|
|
11
|
+
externalSwapQuoteParams.receiverAddress === undefined) {
|
|
12
|
+
return undefined;
|
|
13
|
+
}
|
|
14
|
+
if (externalSwapPath.aggregator === trade_1.ExternalSwapAggregator.BotanixStaking) {
|
|
15
|
+
return (0, botanixStaking_1.getBotanixStakingExternalSwapQuote)({
|
|
16
|
+
tokenInAddress: externalSwapPath.inTokenAddress,
|
|
17
|
+
tokenOutAddress: externalSwapPath.outTokenAddress,
|
|
18
|
+
amountIn,
|
|
19
|
+
gasPrice: externalSwapQuoteParams.gasPrice,
|
|
20
|
+
receiverAddress: externalSwapQuoteParams.receiverAddress,
|
|
21
|
+
tokensData: externalSwapQuoteParams.tokensData,
|
|
22
|
+
assetsPerShare: externalSwapQuoteParams.botanixStakingAssetsPerShare,
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
return undefined;
|
|
26
|
+
};
|
|
27
|
+
exports.getExternalSwapQuoteByPath = getExternalSwapQuoteByPath;
|
|
@@ -149,8 +149,11 @@ function getSwapStats(p) {
|
|
|
149
149
|
const priceOut = tokenOut.prices.maxPrice;
|
|
150
150
|
const amountIn = (0, tokens_2.convertToTokenAmount)(usdIn, tokenIn.decimals, priceIn);
|
|
151
151
|
let priceImpactDeltaUsd;
|
|
152
|
+
let balanceWasImproved;
|
|
152
153
|
try {
|
|
153
|
-
|
|
154
|
+
const priceImpactValues = (0, fees_1.getPriceImpactForSwap)(marketInfo, tokenIn, tokenOut, usdIn, usdIn * -1n);
|
|
155
|
+
priceImpactDeltaUsd = priceImpactValues.priceImpactDeltaUsd;
|
|
156
|
+
balanceWasImproved = priceImpactValues.balanceWasImproved;
|
|
154
157
|
}
|
|
155
158
|
catch (e) {
|
|
156
159
|
// Approximate if the market would be out of capacity
|
|
@@ -176,8 +179,8 @@ function getSwapStats(p) {
|
|
|
176
179
|
isOutCapacity,
|
|
177
180
|
};
|
|
178
181
|
}
|
|
179
|
-
const swapFeeAmount = (0, fees_1.getSwapFee)(marketInfo, amountIn,
|
|
180
|
-
const swapFeeUsd = (0, fees_1.getSwapFee)(marketInfo, usdIn,
|
|
182
|
+
const swapFeeAmount = (0, fees_1.getSwapFee)(marketInfo, amountIn, balanceWasImproved, isAtomicSwap);
|
|
183
|
+
const swapFeeUsd = (0, fees_1.getSwapFee)(marketInfo, usdIn, balanceWasImproved, isAtomicSwap);
|
|
181
184
|
const amountInAfterFees = amountIn - swapFeeAmount;
|
|
182
185
|
const usdInAfterFees = usdIn - swapFeeUsd;
|
|
183
186
|
let usdOut = usdInAfterFees;
|