@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
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { BASIS_POINTS_DIVISOR_BIGINT } from "../configs/factors";
|
|
2
2
|
import { bigMath } from "./bigmath";
|
|
3
|
-
import { getPositionFee, getPriceImpactForPosition } from "./fees";
|
|
4
|
-
import { getCappedPoolPnl, getMarketPnl, getPoolUsdWithoutPnl } from "./markets";
|
|
5
|
-
import { applyFactor, expandDecimals } from "./numbers";
|
|
3
|
+
import { capPositionImpactUsdByMaxImpactPool, capPositionImpactUsdByMaxPriceImpactFactor, getMaxPositionImpactFactors, getPositionFee, getPriceImpactForPosition, getProportionalPendingImpactValues, } from "./fees";
|
|
4
|
+
import { getCappedPoolPnl, getMarketPnl, getOpenInterestUsd, getPoolUsdWithoutPnl } from "./markets";
|
|
5
|
+
import { applyFactor, expandDecimals, PRECISION } from "./numbers";
|
|
6
6
|
import { convertToUsd, getIsEquivalentTokens } from "./tokens";
|
|
7
7
|
export function getPositionKey(account, marketAddress, collateralAddress, isLong) {
|
|
8
8
|
return `${account}:${marketAddress}:${collateralAddress}:${isLong}`;
|
|
@@ -48,9 +48,19 @@ export function getPositionPendingFeesUsd(p) {
|
|
|
48
48
|
return pendingBorrowingFeesUsd + pendingFundingFeesUsd;
|
|
49
49
|
}
|
|
50
50
|
export function getPositionNetValue(p) {
|
|
51
|
-
const { pnl, closingFeeUsd, collateralUsd, uiFeeUsd } = p;
|
|
51
|
+
const { pnl, closingFeeUsd, collateralUsd, uiFeeUsd, totalPendingImpactDeltaUsd, priceImpactDiffUsd } = p;
|
|
52
52
|
const pendingFeesUsd = getPositionPendingFeesUsd(p);
|
|
53
|
-
return collateralUsd - pendingFeesUsd - closingFeeUsd - uiFeeUsd + pnl;
|
|
53
|
+
return (collateralUsd - pendingFeesUsd - closingFeeUsd - uiFeeUsd + pnl + totalPendingImpactDeltaUsd + priceImpactDiffUsd);
|
|
54
|
+
}
|
|
55
|
+
export function getPositionPnlAfterFees({ pnl, pendingBorrowingFeesUsd, pendingFundingFeesUsd, closingFeeUsd, uiFeeUsd, totalPendingImpactDeltaUsd, priceImpactDiffUsd, }) {
|
|
56
|
+
const pnlAfterFees = pnl -
|
|
57
|
+
pendingBorrowingFeesUsd -
|
|
58
|
+
pendingFundingFeesUsd -
|
|
59
|
+
closingFeeUsd -
|
|
60
|
+
uiFeeUsd +
|
|
61
|
+
totalPendingImpactDeltaUsd +
|
|
62
|
+
priceImpactDiffUsd;
|
|
63
|
+
return pnlAfterFees;
|
|
54
64
|
}
|
|
55
65
|
export function getLeverage(p) {
|
|
56
66
|
const { pnl, sizeInUsd, collateralUsd, pendingBorrowingFeesUsd, pendingFundingFeesUsd } = p;
|
|
@@ -62,7 +72,7 @@ export function getLeverage(p) {
|
|
|
62
72
|
return bigMath.mulDiv(sizeInUsd, BASIS_POINTS_DIVISOR_BIGINT, remainingCollateralUsd);
|
|
63
73
|
}
|
|
64
74
|
export function getLiquidationPrice(p) {
|
|
65
|
-
const { sizeInUsd, sizeInTokens, collateralUsd, collateralAmount, marketInfo, collateralToken, pendingFundingFeesUsd, pendingBorrowingFeesUsd, minCollateralUsd, isLong, userReferralInfo, useMaxPriceImpact, } = p;
|
|
75
|
+
const { sizeInUsd, sizeInTokens, collateralUsd, collateralAmount, marketInfo, collateralToken, pendingFundingFeesUsd, pendingBorrowingFeesUsd, pendingImpactAmount, minCollateralUsd, isLong, userReferralInfo, useMaxPriceImpact, } = p;
|
|
66
76
|
if (sizeInUsd <= 0 || sizeInTokens <= 0) {
|
|
67
77
|
return undefined;
|
|
68
78
|
}
|
|
@@ -76,16 +86,21 @@ export function getLiquidationPrice(p) {
|
|
|
76
86
|
priceImpactDeltaUsd = maxNegativePriceImpactUsd;
|
|
77
87
|
}
|
|
78
88
|
else {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
89
|
+
const priceImpactForPosition = getPriceImpactForPosition(marketInfo, -sizeInUsd, isLong, { fallbackToZero: true });
|
|
90
|
+
priceImpactDeltaUsd = priceImpactForPosition.priceImpactDeltaUsd;
|
|
91
|
+
if (priceImpactDeltaUsd > 0) {
|
|
92
|
+
priceImpactDeltaUsd = capPositionImpactUsdByMaxPriceImpactFactor(marketInfo, sizeInUsd, priceImpactDeltaUsd);
|
|
82
93
|
}
|
|
83
|
-
|
|
94
|
+
const pendingImpactUsd = convertToUsd(pendingImpactAmount, marketInfo.indexToken.decimals, pendingImpactAmount > 0 ? marketInfo.indexToken.prices.minPrice : marketInfo.indexToken.prices.maxPrice);
|
|
95
|
+
priceImpactDeltaUsd = priceImpactDeltaUsd + pendingImpactUsd;
|
|
84
96
|
if (priceImpactDeltaUsd > 0) {
|
|
85
97
|
priceImpactDeltaUsd = 0n;
|
|
86
98
|
}
|
|
99
|
+
else if (priceImpactDeltaUsd < maxNegativePriceImpactUsd) {
|
|
100
|
+
priceImpactDeltaUsd = maxNegativePriceImpactUsd;
|
|
101
|
+
}
|
|
87
102
|
}
|
|
88
|
-
let liquidationCollateralUsd = applyFactor(sizeInUsd, marketInfo.
|
|
103
|
+
let liquidationCollateralUsd = applyFactor(sizeInUsd, marketInfo.minCollateralFactorForLiquidation);
|
|
89
104
|
if (liquidationCollateralUsd < minCollateralUsd) {
|
|
90
105
|
liquidationCollateralUsd = minCollateralUsd;
|
|
91
106
|
}
|
|
@@ -131,3 +146,52 @@ export function getLiquidationPrice(p) {
|
|
|
131
146
|
}
|
|
132
147
|
return liquidationPrice;
|
|
133
148
|
}
|
|
149
|
+
export function getNetPriceImpactDeltaUsdForDecrease({ marketInfo, sizeInUsd, pendingImpactAmount, priceImpactDeltaUsd, sizeDeltaUsd, }) {
|
|
150
|
+
const { proportionalPendingImpactDeltaUsd } = getProportionalPendingImpactValues({
|
|
151
|
+
sizeInUsd,
|
|
152
|
+
sizeDeltaUsd,
|
|
153
|
+
pendingImpactAmount,
|
|
154
|
+
indexToken: marketInfo.indexToken,
|
|
155
|
+
});
|
|
156
|
+
let totalImpactDeltaUsd = priceImpactDeltaUsd + proportionalPendingImpactDeltaUsd;
|
|
157
|
+
const priceImpactDiffUsd = getPriceImpactDiffUsd({
|
|
158
|
+
totalImpactDeltaUsd,
|
|
159
|
+
marketInfo,
|
|
160
|
+
sizeDeltaUsd,
|
|
161
|
+
});
|
|
162
|
+
if (totalImpactDeltaUsd > 0) {
|
|
163
|
+
totalImpactDeltaUsd = capPositionImpactUsdByMaxPriceImpactFactor(marketInfo, sizeDeltaUsd, totalImpactDeltaUsd);
|
|
164
|
+
}
|
|
165
|
+
totalImpactDeltaUsd = capPositionImpactUsdByMaxImpactPool(marketInfo, totalImpactDeltaUsd);
|
|
166
|
+
return {
|
|
167
|
+
totalImpactDeltaUsd,
|
|
168
|
+
proportionalPendingImpactDeltaUsd,
|
|
169
|
+
priceImpactDiffUsd,
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
export function getPriceImpactDiffUsd({ totalImpactDeltaUsd, marketInfo, sizeDeltaUsd, }) {
|
|
173
|
+
if (totalImpactDeltaUsd > 0) {
|
|
174
|
+
return 0n;
|
|
175
|
+
}
|
|
176
|
+
const { maxNegativeImpactFactor } = getMaxPositionImpactFactors(marketInfo);
|
|
177
|
+
const maxNegativeImpactUsd = -applyFactor(sizeDeltaUsd, maxNegativeImpactFactor);
|
|
178
|
+
let priceImpactDiffUsd = 0n;
|
|
179
|
+
if (totalImpactDeltaUsd < maxNegativeImpactUsd) {
|
|
180
|
+
priceImpactDiffUsd = maxNegativeImpactUsd - totalImpactDeltaUsd;
|
|
181
|
+
}
|
|
182
|
+
return priceImpactDiffUsd;
|
|
183
|
+
}
|
|
184
|
+
export function getMinCollateralFactorForPosition(position, openInterestDelta) {
|
|
185
|
+
const marketInfo = position.marketInfo;
|
|
186
|
+
const isLong = position.isLong;
|
|
187
|
+
const openInterest = getOpenInterestUsd(marketInfo, isLong) + openInterestDelta;
|
|
188
|
+
const minCollateralFactorMultiplier = isLong
|
|
189
|
+
? marketInfo.minCollateralFactorForOpenInterestLong
|
|
190
|
+
: marketInfo.minCollateralFactorForOpenInterestShort;
|
|
191
|
+
let minCollateralFactor = bigMath.mulDiv(openInterest, minCollateralFactorMultiplier, PRECISION);
|
|
192
|
+
const minCollateralFactorForMarket = marketInfo.minCollateralFactor;
|
|
193
|
+
if (minCollateralFactorForMarket > minCollateralFactor) {
|
|
194
|
+
minCollateralFactor = minCollateralFactorForMarket;
|
|
195
|
+
}
|
|
196
|
+
return minCollateralFactor;
|
|
197
|
+
}
|
|
@@ -2,11 +2,8 @@ import { BASIS_POINTS_DIVISOR_BIGINT, DEFAULT_ACCEPTABLE_PRICE_IMPACT_BUFFER } f
|
|
|
2
2
|
import { OrderType } from "../types/orders";
|
|
3
3
|
import { TriggerThresholdType } from "../types/trade";
|
|
4
4
|
import { bigMath } from "./bigmath";
|
|
5
|
-
import { getPriceImpactByAcceptablePrice } from "./fees";
|
|
6
|
-
import {
|
|
7
|
-
import { expandDecimals, getBasisPoints } from "./numbers";
|
|
8
|
-
import { roundUpMagnitudeDivision } from "./numbers";
|
|
9
|
-
import { applyFactor } from "./numbers";
|
|
5
|
+
import { getCappedPositionImpactUsd, getPriceImpactByAcceptablePrice } from "./fees";
|
|
6
|
+
import { expandDecimals, getBasisPoints, roundUpMagnitudeDivision } from "./numbers";
|
|
10
7
|
import { convertToTokenAmount } from "./tokens";
|
|
11
8
|
export function getMarkPrice(p) {
|
|
12
9
|
const { prices, isIncrease, isLong } = p;
|
|
@@ -41,9 +38,12 @@ export function getAcceptablePriceInfo(p) {
|
|
|
41
38
|
const values = {
|
|
42
39
|
acceptablePrice: 0n,
|
|
43
40
|
acceptablePriceDeltaBps: 0n,
|
|
41
|
+
cappedPriceImpactDeltaUsd: 0n,
|
|
42
|
+
cappedPriceImpactDeltaAmount: 0n,
|
|
44
43
|
priceImpactDeltaAmount: 0n,
|
|
45
44
|
priceImpactDeltaUsd: 0n,
|
|
46
45
|
priceImpactDiffUsd: 0n,
|
|
46
|
+
balanceWasImproved: false,
|
|
47
47
|
};
|
|
48
48
|
if (sizeDeltaUsd <= 0 || indexPrice == 0n) {
|
|
49
49
|
return values;
|
|
@@ -66,28 +66,34 @@ export function getAcceptablePriceInfo(p) {
|
|
|
66
66
|
values.priceImpactDeltaAmount = priceImpact.priceImpactDeltaAmount;
|
|
67
67
|
return values;
|
|
68
68
|
}
|
|
69
|
-
|
|
69
|
+
const { priceImpactDeltaUsd, balanceWasImproved } = getCappedPositionImpactUsd(marketInfo, sizeDeltaUsd, isLong, isIncrease, {
|
|
70
70
|
fallbackToZero: !isIncrease,
|
|
71
|
+
shouldCapNegativeImpact: false,
|
|
71
72
|
});
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
73
|
+
/**
|
|
74
|
+
* We display this value as price impact on action (increase or decrease)
|
|
75
|
+
* But for acceptable price calculation uncapped price impact is used
|
|
76
|
+
* Also on decrease action we calculate totalImpactUsd which will be deducted from the collateral
|
|
77
|
+
*/
|
|
78
|
+
values.priceImpactDeltaUsd = priceImpactDeltaUsd;
|
|
79
|
+
values.balanceWasImproved = balanceWasImproved;
|
|
79
80
|
if (values.priceImpactDeltaUsd > 0) {
|
|
80
81
|
values.priceImpactDeltaAmount = convertToTokenAmount(values.priceImpactDeltaUsd, indexToken.decimals, indexToken.prices.maxPrice);
|
|
81
82
|
}
|
|
82
83
|
else {
|
|
83
84
|
values.priceImpactDeltaAmount = roundUpMagnitudeDivision(values.priceImpactDeltaUsd * expandDecimals(1, indexToken.decimals), indexToken.prices.minPrice);
|
|
84
85
|
}
|
|
86
|
+
// Use uncapped price impact for the acceptable price calculation
|
|
87
|
+
const { priceImpactDeltaUsd: priceImpactDeltaUsdForAcceptablePrice } = getCappedPositionImpactUsd(marketInfo, sizeDeltaUsd, isLong, isIncrease, {
|
|
88
|
+
fallbackToZero: !isIncrease,
|
|
89
|
+
shouldCapNegativeImpact: false,
|
|
90
|
+
});
|
|
85
91
|
const acceptablePriceValues = getAcceptablePriceByPriceImpact({
|
|
86
92
|
isIncrease,
|
|
87
93
|
isLong,
|
|
88
94
|
indexPrice,
|
|
89
95
|
sizeDeltaUsd,
|
|
90
|
-
priceImpactDeltaUsd:
|
|
96
|
+
priceImpactDeltaUsd: priceImpactDeltaUsdForAcceptablePrice,
|
|
91
97
|
});
|
|
92
98
|
values.acceptablePrice = acceptablePriceValues.acceptablePrice;
|
|
93
99
|
values.acceptablePriceDeltaBps = acceptablePriceValues.acceptablePriceDeltaBps;
|
|
@@ -39,6 +39,7 @@ const baseGasLimits = {
|
|
|
39
39
|
cancelOrderGasLimit: 1000000n,
|
|
40
40
|
gelatoRelayFeeMultiplierFactor: 1000000000000000000000000000000n,
|
|
41
41
|
tokenPermitGasLimit: 1000000n,
|
|
42
|
+
gmxAccountCollateralGasLimit: 0n,
|
|
42
43
|
};
|
|
43
44
|
const baseGasPrice = 1650000002n;
|
|
44
45
|
describe("mockRouting", () => {
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { encodeFunctionData } from "viem";
|
|
2
|
+
import StBTCABI from "../../abis/StBTC.json";
|
|
3
|
+
import { BOTANIX } from "../../configs/chains";
|
|
4
|
+
import { getContract } from "../../configs/contracts";
|
|
5
|
+
import { ExternalSwapAggregator } from "../../types/trade";
|
|
6
|
+
import { bigMath } from "../bigmath";
|
|
7
|
+
import { BASIS_POINTS_DIVISOR_BIGINT, getBasisPoints } from "../numbers";
|
|
8
|
+
import { AVAILABLE_BOTANIX_DEPOSIT_PAIRS, AVAILABLE_BOTANIX_WITHDRAW_PAIRS } from "./externalSwapPath";
|
|
9
|
+
import { convertToUsd, getMidPrice, getTokenData } from "../tokens";
|
|
10
|
+
const COEF_REDUCER = getBasisPoints(1n, 10000n);
|
|
11
|
+
export const getBotanixStakingExternalSwapQuote = ({ tokenInAddress, tokenOutAddress, amountIn, gasPrice, receiverAddress, tokensData, assetsPerShare, }) => {
|
|
12
|
+
const inTokenData = getTokenData(tokensData, tokenInAddress);
|
|
13
|
+
const outTokenData = getTokenData(tokensData, tokenOutAddress);
|
|
14
|
+
const assetsPerShareRate = getBasisPoints(assetsPerShare, 10n ** 18n) - COEF_REDUCER;
|
|
15
|
+
const sharesPerAssetRate = getBasisPoints(10n ** 18n, assetsPerShare) - COEF_REDUCER;
|
|
16
|
+
if (!inTokenData || !outTokenData) {
|
|
17
|
+
return undefined;
|
|
18
|
+
}
|
|
19
|
+
if (AVAILABLE_BOTANIX_DEPOSIT_PAIRS.some((pair) => pair.from === tokenInAddress && pair.to === tokenOutAddress)) {
|
|
20
|
+
const priceIn = getMidPrice(inTokenData.prices);
|
|
21
|
+
const priceOut = bigMath.mulDiv(priceIn, sharesPerAssetRate, BASIS_POINTS_DIVISOR_BIGINT);
|
|
22
|
+
const usdIn = convertToUsd(amountIn, inTokenData.decimals, priceIn);
|
|
23
|
+
const amountOut = amountIn > 0n ? bigMath.mulDiv(amountIn, sharesPerAssetRate, BASIS_POINTS_DIVISOR_BIGINT) - gasPrice : 0n;
|
|
24
|
+
const usdOut = amountOut > 0n ? convertToUsd(amountOut, outTokenData.decimals, priceOut) : 0n;
|
|
25
|
+
return {
|
|
26
|
+
aggregator: ExternalSwapAggregator.BotanixStaking,
|
|
27
|
+
inTokenAddress: tokenInAddress,
|
|
28
|
+
outTokenAddress: tokenOutAddress,
|
|
29
|
+
receiver: receiverAddress,
|
|
30
|
+
amountIn,
|
|
31
|
+
amountOut,
|
|
32
|
+
usdIn: usdIn,
|
|
33
|
+
usdOut: usdOut,
|
|
34
|
+
priceIn,
|
|
35
|
+
priceOut,
|
|
36
|
+
feesUsd: gasPrice,
|
|
37
|
+
needSpenderApproval: true,
|
|
38
|
+
txnData: {
|
|
39
|
+
to: getContract(BOTANIX, "StBTC"),
|
|
40
|
+
data: encodeFunctionData({
|
|
41
|
+
abi: StBTCABI.abi,
|
|
42
|
+
functionName: "deposit",
|
|
43
|
+
args: [amountIn, receiverAddress],
|
|
44
|
+
}),
|
|
45
|
+
value: 0n,
|
|
46
|
+
estimatedGas: gasPrice,
|
|
47
|
+
estimatedExecutionFee: gasPrice,
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
if (AVAILABLE_BOTANIX_WITHDRAW_PAIRS.some((pair) => pair.from === tokenInAddress && pair.to === tokenOutAddress)) {
|
|
52
|
+
const priceIn = getMidPrice(inTokenData.prices);
|
|
53
|
+
const priceOut = bigMath.mulDiv(priceIn, assetsPerShareRate, BASIS_POINTS_DIVISOR_BIGINT);
|
|
54
|
+
const usdIn = convertToUsd(amountIn, inTokenData.decimals, priceIn);
|
|
55
|
+
const amountOut = amountIn > 0n ? bigMath.mulDiv(amountIn, assetsPerShareRate, BASIS_POINTS_DIVISOR_BIGINT) - gasPrice : 0n;
|
|
56
|
+
const usdOut = amountOut > 0n ? convertToUsd(amountOut, outTokenData.decimals, priceOut) : 0n;
|
|
57
|
+
return {
|
|
58
|
+
aggregator: ExternalSwapAggregator.BotanixStaking,
|
|
59
|
+
inTokenAddress: tokenInAddress,
|
|
60
|
+
outTokenAddress: tokenOutAddress,
|
|
61
|
+
receiver: receiverAddress,
|
|
62
|
+
amountIn,
|
|
63
|
+
amountOut,
|
|
64
|
+
usdIn: usdIn,
|
|
65
|
+
usdOut: usdOut,
|
|
66
|
+
priceIn,
|
|
67
|
+
priceOut,
|
|
68
|
+
feesUsd: gasPrice,
|
|
69
|
+
needSpenderApproval: true,
|
|
70
|
+
txnData: {
|
|
71
|
+
to: getContract(BOTANIX, "StBTC"),
|
|
72
|
+
data: encodeFunctionData({
|
|
73
|
+
abi: StBTCABI.abi,
|
|
74
|
+
functionName: "withdraw",
|
|
75
|
+
args: [amountIn, receiverAddress, getContract(BOTANIX, "ExternalHandler")],
|
|
76
|
+
}),
|
|
77
|
+
value: 0n,
|
|
78
|
+
estimatedGas: gasPrice,
|
|
79
|
+
estimatedExecutionFee: gasPrice,
|
|
80
|
+
},
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
};
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
import { bigMath } from "../bigmath";
|
|
2
|
+
import { convertToTokenAmount, convertToUsd, getIsEquivalentTokens, getIsStake, getIsUnstake, getMidPrice, } from "../tokens";
|
|
3
|
+
import { getAvailableExternalSwapPaths } from "./externalSwapPath";
|
|
4
|
+
import { getExternalSwapQuoteByPath } from "./externalSwapQuoteByPath";
|
|
5
|
+
import { createFindSwapPath } from "./swapPath";
|
|
6
|
+
/*
|
|
7
|
+
Order/Priority of getting swap strategy:
|
|
8
|
+
1. Check if it needs a swap and return noSwap if tokens are equivalent, stake or unstake [noSwap]
|
|
9
|
+
2. Check if there is a swap path stats for the internal swap quote and return internalSwap if there is [internalSwap]
|
|
10
|
+
3. Check if there is a combined swap strategy and return combinedSwap if there is [combinedSwap]
|
|
11
|
+
4. Return defaultSwapStrategy (noSwap) if there is no other swap strategy [noSwap]
|
|
12
|
+
*/
|
|
13
|
+
export function buildSwapStrategy({ amountIn, tokenIn, tokenOut, marketsInfoData, chainId, swapOptimizationOrder, externalSwapQuoteParams, }) {
|
|
14
|
+
const priceIn = tokenIn.prices.minPrice;
|
|
15
|
+
const usdIn = convertToUsd(amountIn, tokenIn.decimals, priceIn);
|
|
16
|
+
if (amountIn < 0n) {
|
|
17
|
+
amountIn = 0n;
|
|
18
|
+
}
|
|
19
|
+
const defaultSwapStrategy = {
|
|
20
|
+
type: "noSwap",
|
|
21
|
+
externalSwapQuote: undefined,
|
|
22
|
+
swapPathStats: undefined,
|
|
23
|
+
amountIn,
|
|
24
|
+
amountOut: convertToTokenAmount(usdIn, tokenOut.decimals, tokenOut.prices.maxPrice),
|
|
25
|
+
usdIn,
|
|
26
|
+
usdOut: usdIn,
|
|
27
|
+
priceIn,
|
|
28
|
+
priceOut: tokenOut.prices.maxPrice,
|
|
29
|
+
feesUsd: 0n,
|
|
30
|
+
};
|
|
31
|
+
if (getIsEquivalentTokens(tokenIn, tokenOut) || getIsStake(tokenIn, tokenOut) || getIsUnstake(tokenIn, tokenOut)) {
|
|
32
|
+
return defaultSwapStrategy;
|
|
33
|
+
}
|
|
34
|
+
const findSwapPath = createFindSwapPath({
|
|
35
|
+
chainId,
|
|
36
|
+
fromTokenAddress: tokenIn.address,
|
|
37
|
+
toTokenAddress: tokenOut.address,
|
|
38
|
+
marketsInfoData,
|
|
39
|
+
isExpressFeeSwap: false,
|
|
40
|
+
});
|
|
41
|
+
const swapPathStats = findSwapPath(usdIn, { order: swapOptimizationOrder });
|
|
42
|
+
if (swapPathStats) {
|
|
43
|
+
return {
|
|
44
|
+
type: "internalSwap",
|
|
45
|
+
swapPathStats,
|
|
46
|
+
externalSwapQuote: undefined,
|
|
47
|
+
amountIn,
|
|
48
|
+
amountOut: swapPathStats.amountOut,
|
|
49
|
+
usdIn: usdIn,
|
|
50
|
+
usdOut: swapPathStats.usdOut,
|
|
51
|
+
priceIn: priceIn,
|
|
52
|
+
priceOut: tokenOut.prices.maxPrice,
|
|
53
|
+
feesUsd: usdIn - swapPathStats.usdOut,
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
const availableExternalSwapPaths = getAvailableExternalSwapPaths({ chainId, fromTokenAddress: tokenIn.address });
|
|
57
|
+
const suitableSwapPath = availableExternalSwapPaths.find((path) => {
|
|
58
|
+
const findSwapPath = createFindSwapPath({
|
|
59
|
+
chainId,
|
|
60
|
+
fromTokenAddress: path.outTokenAddress,
|
|
61
|
+
toTokenAddress: tokenOut.address,
|
|
62
|
+
marketsInfoData,
|
|
63
|
+
isExpressFeeSwap: false,
|
|
64
|
+
});
|
|
65
|
+
const swapPathStats = findSwapPath(usdIn);
|
|
66
|
+
return Boolean(swapPathStats);
|
|
67
|
+
});
|
|
68
|
+
if (suitableSwapPath && suitableSwapPath.outTokenAddress !== tokenOut.address) {
|
|
69
|
+
const externalSwapQuoteForCombinedSwap = getExternalSwapQuoteByPath({
|
|
70
|
+
amountIn,
|
|
71
|
+
externalSwapPath: suitableSwapPath,
|
|
72
|
+
externalSwapQuoteParams,
|
|
73
|
+
});
|
|
74
|
+
const findSwapPathForSuitableSwapPath = createFindSwapPath({
|
|
75
|
+
chainId,
|
|
76
|
+
fromTokenAddress: suitableSwapPath.outTokenAddress,
|
|
77
|
+
toTokenAddress: tokenOut.address,
|
|
78
|
+
marketsInfoData,
|
|
79
|
+
isExpressFeeSwap: false,
|
|
80
|
+
});
|
|
81
|
+
const swapPathStatsForCombinedSwap = externalSwapQuoteForCombinedSwap
|
|
82
|
+
? findSwapPathForSuitableSwapPath(externalSwapQuoteForCombinedSwap.usdOut)
|
|
83
|
+
: undefined;
|
|
84
|
+
return externalSwapQuoteForCombinedSwap && swapPathStatsForCombinedSwap
|
|
85
|
+
? {
|
|
86
|
+
type: "combinedSwap",
|
|
87
|
+
externalSwapQuote: externalSwapQuoteForCombinedSwap,
|
|
88
|
+
swapPathStats: swapPathStatsForCombinedSwap,
|
|
89
|
+
amountIn,
|
|
90
|
+
amountOut: swapPathStatsForCombinedSwap.amountOut,
|
|
91
|
+
usdIn: externalSwapQuoteForCombinedSwap.usdIn,
|
|
92
|
+
usdOut: swapPathStatsForCombinedSwap.usdOut,
|
|
93
|
+
priceIn: externalSwapQuoteForCombinedSwap.priceIn,
|
|
94
|
+
priceOut: tokenOut.prices.maxPrice,
|
|
95
|
+
feesUsd: externalSwapQuoteForCombinedSwap.usdIn - swapPathStatsForCombinedSwap.usdOut,
|
|
96
|
+
}
|
|
97
|
+
: defaultSwapStrategy;
|
|
98
|
+
}
|
|
99
|
+
return defaultSwapStrategy;
|
|
100
|
+
}
|
|
101
|
+
// Used for getting swap amounts by to value
|
|
102
|
+
export function buildReverseSwapStrategy({ amountOut, tokenIn, tokenOut, marketsInfoData, chainId, externalSwapQuoteParams, swapOptimizationOrder, }) {
|
|
103
|
+
const priceIn = getMidPrice(tokenIn.prices);
|
|
104
|
+
const priceOut = getMidPrice(tokenOut.prices);
|
|
105
|
+
const preferredUsdOut = convertToUsd(amountOut, tokenOut.decimals, getMidPrice(tokenOut.prices));
|
|
106
|
+
const approximateAmountIn = convertToTokenAmount(preferredUsdOut, tokenIn.decimals, getMidPrice(tokenIn.prices));
|
|
107
|
+
const approximateUsdIn = preferredUsdOut;
|
|
108
|
+
const defaultSwapStrategy = {
|
|
109
|
+
type: "noSwap",
|
|
110
|
+
externalSwapQuote: undefined,
|
|
111
|
+
swapPathStats: undefined,
|
|
112
|
+
amountIn: approximateAmountIn,
|
|
113
|
+
amountOut: amountOut,
|
|
114
|
+
usdIn: approximateUsdIn,
|
|
115
|
+
usdOut: preferredUsdOut,
|
|
116
|
+
priceIn,
|
|
117
|
+
priceOut,
|
|
118
|
+
feesUsd: 0n,
|
|
119
|
+
};
|
|
120
|
+
if (getIsEquivalentTokens(tokenIn, tokenOut) || getIsStake(tokenIn, tokenOut) || getIsUnstake(tokenIn, tokenOut)) {
|
|
121
|
+
return defaultSwapStrategy;
|
|
122
|
+
}
|
|
123
|
+
const findSwapPath = createFindSwapPath({
|
|
124
|
+
chainId,
|
|
125
|
+
fromTokenAddress: tokenIn.address,
|
|
126
|
+
toTokenAddress: tokenOut.address,
|
|
127
|
+
marketsInfoData,
|
|
128
|
+
isExpressFeeSwap: false,
|
|
129
|
+
});
|
|
130
|
+
const approximateSwapPathStats = findSwapPath(approximateUsdIn, { order: swapOptimizationOrder });
|
|
131
|
+
if (approximateSwapPathStats) {
|
|
132
|
+
// Increase or decrease usdIn the same way preferred usdOut is different from swapStrategy.usdOut
|
|
133
|
+
// preferred_in / approximate_in = preferred_out / approximate_out
|
|
134
|
+
// preferred_in = approximate_in * preferred_out / approximate_out
|
|
135
|
+
const adjustedUsdIn = approximateSwapPathStats.usdOut > 0
|
|
136
|
+
? bigMath.mulDiv(approximateUsdIn, preferredUsdOut, approximateSwapPathStats.usdOut)
|
|
137
|
+
: 0n;
|
|
138
|
+
const adjustedAmountIn = convertToTokenAmount(adjustedUsdIn, tokenIn.decimals, getMidPrice(tokenIn.prices));
|
|
139
|
+
const adjustedSwapPathStats = findSwapPath(adjustedUsdIn, { order: swapOptimizationOrder });
|
|
140
|
+
if (adjustedSwapPathStats) {
|
|
141
|
+
return {
|
|
142
|
+
type: "internalSwap",
|
|
143
|
+
swapPathStats: adjustedSwapPathStats,
|
|
144
|
+
externalSwapQuote: undefined,
|
|
145
|
+
amountIn: adjustedAmountIn,
|
|
146
|
+
amountOut: adjustedSwapPathStats.amountOut,
|
|
147
|
+
usdIn: adjustedUsdIn,
|
|
148
|
+
usdOut: adjustedSwapPathStats.usdOut,
|
|
149
|
+
priceIn: priceIn,
|
|
150
|
+
priceOut: priceOut,
|
|
151
|
+
feesUsd: adjustedUsdIn - adjustedSwapPathStats.usdOut,
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
const availableExternalSwapPaths = getAvailableExternalSwapPaths({ chainId, fromTokenAddress: tokenIn.address });
|
|
156
|
+
const suitableSwapPath = availableExternalSwapPaths.find((path) => {
|
|
157
|
+
if (path.outTokenAddress !== tokenOut.address)
|
|
158
|
+
return false;
|
|
159
|
+
const findSwapPath = createFindSwapPath({
|
|
160
|
+
chainId,
|
|
161
|
+
fromTokenAddress: tokenIn.address,
|
|
162
|
+
toTokenAddress: path.inTokenAddress,
|
|
163
|
+
marketsInfoData,
|
|
164
|
+
isExpressFeeSwap: false,
|
|
165
|
+
});
|
|
166
|
+
const swapPathStats = findSwapPath(approximateUsdIn);
|
|
167
|
+
return Boolean(swapPathStats);
|
|
168
|
+
});
|
|
169
|
+
if (suitableSwapPath) {
|
|
170
|
+
const approximateExternalSwapQuoteForCombinedSwap = getExternalSwapQuoteByPath({
|
|
171
|
+
amountIn: approximateAmountIn,
|
|
172
|
+
externalSwapPath: suitableSwapPath,
|
|
173
|
+
externalSwapQuoteParams,
|
|
174
|
+
});
|
|
175
|
+
if (!approximateExternalSwapQuoteForCombinedSwap) {
|
|
176
|
+
return defaultSwapStrategy;
|
|
177
|
+
}
|
|
178
|
+
const findSwapPathForSuitableSwapPath = createFindSwapPath({
|
|
179
|
+
chainId,
|
|
180
|
+
fromTokenAddress: tokenIn.address,
|
|
181
|
+
toTokenAddress: suitableSwapPath.inTokenAddress,
|
|
182
|
+
marketsInfoData,
|
|
183
|
+
isExpressFeeSwap: false,
|
|
184
|
+
});
|
|
185
|
+
const approximateSwapPathStatsForCombinedSwap = findSwapPathForSuitableSwapPath(approximateExternalSwapQuoteForCombinedSwap.usdOut);
|
|
186
|
+
if (!approximateSwapPathStatsForCombinedSwap) {
|
|
187
|
+
return defaultSwapStrategy;
|
|
188
|
+
}
|
|
189
|
+
const adjustedUsdIn = approximateSwapPathStatsForCombinedSwap.usdOut > 0
|
|
190
|
+
? bigMath.mulDiv(approximateUsdIn, preferredUsdOut, approximateSwapPathStatsForCombinedSwap.usdOut)
|
|
191
|
+
: 0n;
|
|
192
|
+
const adjustedAmountIn = convertToTokenAmount(adjustedUsdIn, tokenIn.decimals, getMidPrice(tokenIn.prices));
|
|
193
|
+
const adjustedExternalSwapQuoteForCombinedSwap = getExternalSwapQuoteByPath({
|
|
194
|
+
amountIn: adjustedAmountIn,
|
|
195
|
+
externalSwapPath: suitableSwapPath,
|
|
196
|
+
externalSwapQuoteParams,
|
|
197
|
+
});
|
|
198
|
+
if (!adjustedExternalSwapQuoteForCombinedSwap) {
|
|
199
|
+
return defaultSwapStrategy;
|
|
200
|
+
}
|
|
201
|
+
const adjustedSwapPathStatsForCombinedSwap = findSwapPathForSuitableSwapPath(adjustedExternalSwapQuoteForCombinedSwap.usdOut);
|
|
202
|
+
if (!adjustedSwapPathStatsForCombinedSwap) {
|
|
203
|
+
return defaultSwapStrategy;
|
|
204
|
+
}
|
|
205
|
+
return {
|
|
206
|
+
type: "combinedSwap",
|
|
207
|
+
externalSwapQuote: adjustedExternalSwapQuoteForCombinedSwap,
|
|
208
|
+
swapPathStats: adjustedSwapPathStatsForCombinedSwap,
|
|
209
|
+
amountIn: adjustedAmountIn,
|
|
210
|
+
amountOut: adjustedSwapPathStatsForCombinedSwap.amountOut,
|
|
211
|
+
usdIn: adjustedExternalSwapQuoteForCombinedSwap.usdIn,
|
|
212
|
+
usdOut: adjustedSwapPathStatsForCombinedSwap.usdOut,
|
|
213
|
+
priceIn: adjustedExternalSwapQuoteForCombinedSwap.priceIn,
|
|
214
|
+
priceOut: priceOut,
|
|
215
|
+
feesUsd: adjustedExternalSwapQuoteForCombinedSwap.usdIn - adjustedSwapPathStatsForCombinedSwap.usdOut,
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
return defaultSwapStrategy;
|
|
219
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { BOTANIX } from "../../configs/chains";
|
|
2
|
+
import { NATIVE_TOKEN_ADDRESS, getTokenBySymbol } from "../../configs/tokens";
|
|
3
|
+
import { ExternalSwapAggregator } from "../../types/trade";
|
|
4
|
+
const BBTC_ADDRESS = NATIVE_TOKEN_ADDRESS;
|
|
5
|
+
const PBTC_ADDRESS = getTokenBySymbol(BOTANIX, "PBTC").address;
|
|
6
|
+
const STBTC_ADDRESS = getTokenBySymbol(BOTANIX, "STBTC").address;
|
|
7
|
+
export const AVAILABLE_BOTANIX_DEPOSIT_PAIRS = [
|
|
8
|
+
{
|
|
9
|
+
from: BBTC_ADDRESS,
|
|
10
|
+
to: STBTC_ADDRESS,
|
|
11
|
+
},
|
|
12
|
+
{
|
|
13
|
+
from: PBTC_ADDRESS,
|
|
14
|
+
to: STBTC_ADDRESS,
|
|
15
|
+
},
|
|
16
|
+
];
|
|
17
|
+
export const AVAILABLE_BOTANIX_WITHDRAW_PAIRS = [
|
|
18
|
+
{
|
|
19
|
+
from: STBTC_ADDRESS,
|
|
20
|
+
to: PBTC_ADDRESS,
|
|
21
|
+
},
|
|
22
|
+
];
|
|
23
|
+
const getBotanixStakingExternalSwapPaths = ({ fromTokenAddress }) => {
|
|
24
|
+
return [...AVAILABLE_BOTANIX_DEPOSIT_PAIRS, ...AVAILABLE_BOTANIX_WITHDRAW_PAIRS]
|
|
25
|
+
.filter((pair) => pair.from === fromTokenAddress)
|
|
26
|
+
.map((pair) => ({
|
|
27
|
+
aggregator: ExternalSwapAggregator.BotanixStaking,
|
|
28
|
+
inTokenAddress: pair.from,
|
|
29
|
+
outTokenAddress: pair.to,
|
|
30
|
+
}));
|
|
31
|
+
};
|
|
32
|
+
export const getAvailableExternalSwapPaths = ({ chainId, fromTokenAddress, }) => {
|
|
33
|
+
if (chainId === BOTANIX) {
|
|
34
|
+
return getBotanixStakingExternalSwapPaths({ fromTokenAddress });
|
|
35
|
+
}
|
|
36
|
+
return [];
|
|
37
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { ExternalSwapAggregator } from "../../types/trade";
|
|
2
|
+
import { getBotanixStakingExternalSwapQuote } from "./botanixStaking";
|
|
3
|
+
export const getExternalSwapQuoteByPath = ({ amountIn, externalSwapPath, externalSwapQuoteParams, }) => {
|
|
4
|
+
if (amountIn === undefined ||
|
|
5
|
+
externalSwapQuoteParams.gasPrice === undefined ||
|
|
6
|
+
externalSwapQuoteParams.tokensData === undefined ||
|
|
7
|
+
externalSwapQuoteParams.botanixStakingAssetsPerShare === undefined ||
|
|
8
|
+
externalSwapQuoteParams.receiverAddress === undefined) {
|
|
9
|
+
return undefined;
|
|
10
|
+
}
|
|
11
|
+
if (externalSwapPath.aggregator === ExternalSwapAggregator.BotanixStaking) {
|
|
12
|
+
return getBotanixStakingExternalSwapQuote({
|
|
13
|
+
tokenInAddress: externalSwapPath.inTokenAddress,
|
|
14
|
+
tokenOutAddress: externalSwapPath.outTokenAddress,
|
|
15
|
+
amountIn,
|
|
16
|
+
gasPrice: externalSwapQuoteParams.gasPrice,
|
|
17
|
+
receiverAddress: externalSwapQuoteParams.receiverAddress,
|
|
18
|
+
tokensData: externalSwapQuoteParams.tokensData,
|
|
19
|
+
assetsPerShare: externalSwapQuoteParams.botanixStakingAssetsPerShare,
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
return undefined;
|
|
23
|
+
};
|
|
@@ -143,8 +143,11 @@ export function getSwapStats(p) {
|
|
|
143
143
|
const priceOut = tokenOut.prices.maxPrice;
|
|
144
144
|
const amountIn = convertToTokenAmount(usdIn, tokenIn.decimals, priceIn);
|
|
145
145
|
let priceImpactDeltaUsd;
|
|
146
|
+
let balanceWasImproved;
|
|
146
147
|
try {
|
|
147
|
-
|
|
148
|
+
const priceImpactValues = getPriceImpactForSwap(marketInfo, tokenIn, tokenOut, usdIn, usdIn * -1n);
|
|
149
|
+
priceImpactDeltaUsd = priceImpactValues.priceImpactDeltaUsd;
|
|
150
|
+
balanceWasImproved = priceImpactValues.balanceWasImproved;
|
|
148
151
|
}
|
|
149
152
|
catch (e) {
|
|
150
153
|
// Approximate if the market would be out of capacity
|
|
@@ -170,8 +173,8 @@ export function getSwapStats(p) {
|
|
|
170
173
|
isOutCapacity,
|
|
171
174
|
};
|
|
172
175
|
}
|
|
173
|
-
const swapFeeAmount = getSwapFee(marketInfo, amountIn,
|
|
174
|
-
const swapFeeUsd = getSwapFee(marketInfo, usdIn,
|
|
176
|
+
const swapFeeAmount = getSwapFee(marketInfo, amountIn, balanceWasImproved, isAtomicSwap);
|
|
177
|
+
const swapFeeUsd = getSwapFee(marketInfo, usdIn, balanceWasImproved, isAtomicSwap);
|
|
175
178
|
const amountInAfterFees = amountIn - swapFeeAmount;
|
|
176
179
|
const usdInAfterFees = usdIn - swapFeeUsd;
|
|
177
180
|
let usdOut = usdInAfterFees;
|