@gmx-io/sdk 1.2.1 → 1.3.1
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 +1 -16
- 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 +51 -7
- package/build/cjs/src/configs/__tests__/markets.spec.js +1 -1
- package/build/cjs/src/configs/__tests__/permits.spec.js +1 -1
- package/build/cjs/src/configs/__tests__/tokens.spec.js +1 -1
- package/build/cjs/src/configs/batch.js +48 -0
- package/build/cjs/src/configs/chains.js +41 -8
- package/build/cjs/src/configs/contracts.js +151 -41
- package/build/cjs/src/configs/dataStore.js +39 -5
- package/build/cjs/src/configs/express.js +4 -0
- package/build/cjs/src/configs/factors.js +1 -1
- package/build/cjs/src/configs/markets.js +128 -0
- package/build/cjs/src/configs/oracleKeeper.js +2 -0
- package/build/cjs/src/configs/tokens.js +217 -2
- 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 +2 -2
- package/build/cjs/src/modules/orders/helpers.spec.js +1 -1
- package/build/cjs/src/modules/orders/transactions/createDecreaseOrderTxn.js +1 -0
- package/build/cjs/src/modules/orders/transactions/createIncreaseOrderTxn.js +1 -0
- package/build/cjs/src/modules/orders/transactions/createSwapOrderTxn.js +1 -0
- 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 +1 -0
- package/build/cjs/src/prebuilt/hashedKinkModelMarketRatesKeys.json +146 -0
- package/build/cjs/src/prebuilt/hashedMarketConfigKeys.json +2207 -532
- package/build/cjs/src/prebuilt/hashedMarketValuesKeys.json +272 -0
- package/build/cjs/src/test/mock.js +9 -4
- package/build/cjs/src/types/subsquid.js +507 -1
- 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 +38 -30
- package/build/cjs/src/utils/__tests__/orderTransactions/increaseOrders.spec.js +10 -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/bigmath.js +9 -2
- package/build/cjs/src/utils/errors/parseError.js +35 -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 -1
- 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/swapStats.js +6 -3
- package/build/cjs/src/utils/trade/decrease.js +470 -0
- package/build/cjs/src/utils/trade/{amounts.js → increase.js} +86 -31
- package/build/cjs/src/utils/tradeHistory.js +3 -0
- package/build/esm/src/abis/ClaimHandler.json +1 -16
- 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 +51 -7
- package/build/esm/src/configs/__tests__/markets.spec.js +3 -3
- package/build/esm/src/configs/__tests__/permits.spec.js +2 -2
- package/build/esm/src/configs/__tests__/tokens.spec.js +2 -2
- package/build/esm/src/configs/batch.js +49 -1
- package/build/esm/src/configs/chains.js +40 -7
- package/build/esm/src/configs/contracts.js +152 -42
- package/build/esm/src/configs/dataStore.js +29 -1
- package/build/esm/src/configs/express.js +5 -1
- package/build/esm/src/configs/factors.js +1 -1
- package/build/esm/src/configs/markets.js +129 -1
- package/build/esm/src/configs/oracleKeeper.js +3 -1
- package/build/esm/src/configs/tokens.js +216 -2
- 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 +1 -1
- package/build/esm/src/modules/orders/helpers.spec.js +1 -1
- package/build/esm/src/modules/orders/transactions/createDecreaseOrderTxn.js +1 -0
- package/build/esm/src/modules/orders/transactions/createIncreaseOrderTxn.js +1 -0
- package/build/esm/src/modules/orders/transactions/createSwapOrderTxn.js +1 -0
- 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 +1 -0
- package/build/esm/src/prebuilt/hashedKinkModelMarketRatesKeys.json +146 -0
- package/build/esm/src/prebuilt/hashedMarketConfigKeys.json +2207 -532
- package/build/esm/src/prebuilt/hashedMarketValuesKeys.json +272 -0
- package/build/esm/src/test/mock.js +9 -4
- package/build/esm/src/types/subsquid.js +506 -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 +38 -30
- package/build/esm/src/utils/__tests__/orderTransactions/increaseOrders.spec.js +10 -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/bigmath.js +9 -2
- package/build/esm/src/utils/errors/parseError.js +31 -0
- 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 -1
- 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/swapStats.js +6 -3
- package/build/esm/src/utils/trade/decrease.js +460 -0
- package/build/esm/src/utils/trade/{amounts.js → increase.js} +87 -33
- package/build/esm/src/utils/tradeHistory.js +3 -0
- package/build/types/src/abis/index.d.ts +1 -1
- package/build/types/src/configs/batch.d.ts +2 -2
- package/build/types/src/configs/chains.d.ts +25 -20
- package/build/types/src/configs/contracts.d.ts +118 -10
- package/build/types/src/configs/dataStore.d.ts +16 -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/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/transactions/createDecreaseOrderTxn.d.ts +1 -0
- package/build/types/src/modules/orders/transactions/createIncreaseOrderTxn.d.ts +1 -0
- package/build/types/src/modules/orders/transactions/createSwapOrderTxn.d.ts +1 -0
- 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 +1259 -9
- package/build/types/src/types/tokens.d.ts +7 -0
- package/build/types/src/types/trade.d.ts +14 -3
- 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/swapValues.d.ts +1 -1
- package/build/types/src/utils/trade/decrease.d.ts +77 -0
- package/build/types/src/utils/trade/{amounts.d.ts → increase.d.ts} +16 -1
- package/package.json +2 -2
|
@@ -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", () => {
|
|
@@ -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;
|
|
@@ -0,0 +1,460 @@
|
|
|
1
|
+
import { DEFAULT_ACCEPTABLE_PRICE_IMPACT_BUFFER } from "../../configs/factors";
|
|
2
|
+
import { DecreasePositionSwapType, OrderType } from "../../types/orders";
|
|
3
|
+
import { bigMath } from "../bigmath";
|
|
4
|
+
import { getPositionFee } from "../fees";
|
|
5
|
+
import { applyFactor, BASIS_POINTS_DIVISOR_BIGINT, expandDecimals, getBasisPoints, roundUpDivision, USD_DECIMALS, MaxUint256, } from "../numbers";
|
|
6
|
+
import { getLeverage, getLiquidationPrice, getMinCollateralFactorForPosition, getNetPriceImpactDeltaUsdForDecrease, getPositionPnlUsd, } from "../positions";
|
|
7
|
+
import { getAcceptablePriceInfo, getDefaultAcceptablePriceImpactBps, getMarkPrice, getOrderThresholdType, } from "../prices";
|
|
8
|
+
import { getSwapStats } from "../swap";
|
|
9
|
+
import { convertToTokenAmount, convertToUsd, getIsEquivalentTokens } from "../tokens";
|
|
10
|
+
export function getDecreasePositionAmounts(p) {
|
|
11
|
+
const { marketInfo, collateralToken, isLong, position, closeSizeUsd, keepLeverage, triggerPrice, fixedAcceptablePriceImpactBps, acceptablePriceImpactBuffer, userReferralInfo, minCollateralUsd, minPositionSizeUsd, uiFeeFactor, triggerOrderType: orderType, receiveToken: receiveTokenArg, } = p;
|
|
12
|
+
const { indexToken } = marketInfo;
|
|
13
|
+
const receiveToken = receiveTokenArg ?? collateralToken;
|
|
14
|
+
const values = {
|
|
15
|
+
isFullClose: false,
|
|
16
|
+
sizeDeltaUsd: 0n,
|
|
17
|
+
sizeDeltaInTokens: 0n,
|
|
18
|
+
collateralDeltaUsd: 0n,
|
|
19
|
+
collateralDeltaAmount: 0n,
|
|
20
|
+
indexPrice: 0n,
|
|
21
|
+
collateralPrice: 0n,
|
|
22
|
+
triggerPrice: 0n,
|
|
23
|
+
acceptablePrice: 0n,
|
|
24
|
+
proportionalPendingImpactDeltaUsd: 0n,
|
|
25
|
+
closePriceImpactDeltaUsd: 0n,
|
|
26
|
+
totalPendingImpactDeltaUsd: 0n,
|
|
27
|
+
priceImpactDiffUsd: 0n,
|
|
28
|
+
balanceWasImproved: false,
|
|
29
|
+
acceptablePriceDeltaBps: 0n,
|
|
30
|
+
recommendedAcceptablePriceDeltaBps: 0n,
|
|
31
|
+
estimatedPnl: 0n,
|
|
32
|
+
estimatedPnlPercentage: 0n,
|
|
33
|
+
realizedPnl: 0n,
|
|
34
|
+
realizedPnlPercentage: 0n,
|
|
35
|
+
positionFeeUsd: 0n,
|
|
36
|
+
uiFeeUsd: 0n,
|
|
37
|
+
swapUiFeeUsd: 0n,
|
|
38
|
+
borrowingFeeUsd: 0n,
|
|
39
|
+
fundingFeeUsd: 0n,
|
|
40
|
+
feeDiscountUsd: 0n,
|
|
41
|
+
swapProfitFeeUsd: 0n,
|
|
42
|
+
payedOutputUsd: 0n,
|
|
43
|
+
payedRemainingCollateralUsd: 0n,
|
|
44
|
+
payedRemainingCollateralAmount: 0n,
|
|
45
|
+
receiveTokenAmount: 0n,
|
|
46
|
+
receiveUsd: 0n,
|
|
47
|
+
triggerOrderType: orderType,
|
|
48
|
+
triggerThresholdType: undefined,
|
|
49
|
+
decreaseSwapType: DecreasePositionSwapType.NoSwap,
|
|
50
|
+
};
|
|
51
|
+
const pnlToken = isLong ? marketInfo.longToken : marketInfo.shortToken;
|
|
52
|
+
values.decreaseSwapType = getDecreaseSwapType(pnlToken, collateralToken, receiveToken);
|
|
53
|
+
const markPrice = getMarkPrice({ prices: indexToken.prices, isIncrease: false, isLong });
|
|
54
|
+
const isTrigger = orderType !== undefined;
|
|
55
|
+
if (orderType) {
|
|
56
|
+
values.triggerPrice = triggerPrice;
|
|
57
|
+
values.indexPrice = triggerPrice ?? markPrice;
|
|
58
|
+
values.collateralPrice = getIsEquivalentTokens(indexToken, collateralToken)
|
|
59
|
+
? triggerPrice ?? markPrice
|
|
60
|
+
: collateralToken.prices.minPrice;
|
|
61
|
+
values.triggerThresholdType = getOrderThresholdType(orderType, isLong);
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
values.indexPrice = markPrice;
|
|
65
|
+
values.collateralPrice = collateralToken.prices.minPrice;
|
|
66
|
+
}
|
|
67
|
+
if (closeSizeUsd <= 0) {
|
|
68
|
+
return values;
|
|
69
|
+
}
|
|
70
|
+
values.sizeDeltaUsd = closeSizeUsd;
|
|
71
|
+
if (!position || position.sizeInUsd <= 0 || position.sizeInTokens <= 0) {
|
|
72
|
+
applyAcceptablePrice({
|
|
73
|
+
position,
|
|
74
|
+
marketInfo,
|
|
75
|
+
isLong,
|
|
76
|
+
isTrigger,
|
|
77
|
+
fixedAcceptablePriceImpactBps,
|
|
78
|
+
acceptablePriceImpactBuffer,
|
|
79
|
+
values,
|
|
80
|
+
});
|
|
81
|
+
const positionFeeInfo = getPositionFee(marketInfo, values.sizeDeltaUsd, values.balanceWasImproved, userReferralInfo);
|
|
82
|
+
values.positionFeeUsd = positionFeeInfo.positionFeeUsd;
|
|
83
|
+
values.feeDiscountUsd = positionFeeInfo.discountUsd;
|
|
84
|
+
values.uiFeeUsd = applyFactor(values.sizeDeltaUsd, uiFeeFactor);
|
|
85
|
+
const totalFeesUsd = getTotalFeesUsdForDecrease({
|
|
86
|
+
positionFeeUsd: values.positionFeeUsd,
|
|
87
|
+
borrowingFeeUsd: 0n,
|
|
88
|
+
fundingFeeUsd: 0n,
|
|
89
|
+
swapProfitFeeUsd: 0n,
|
|
90
|
+
swapUiFeeUsd: 0n,
|
|
91
|
+
uiFeeUsd: values.uiFeeUsd,
|
|
92
|
+
pnlUsd: 0n,
|
|
93
|
+
totalPendingImpactDeltaUsd: values.totalPendingImpactDeltaUsd,
|
|
94
|
+
});
|
|
95
|
+
values.payedOutputUsd = totalFeesUsd;
|
|
96
|
+
return values;
|
|
97
|
+
}
|
|
98
|
+
const estimatedCollateralUsd = convertToUsd(position.collateralAmount, collateralToken.decimals, values.collateralPrice);
|
|
99
|
+
let estimatedCollateralDeltaUsd = 0n;
|
|
100
|
+
if (keepLeverage) {
|
|
101
|
+
estimatedCollateralDeltaUsd = bigMath.mulDiv(values.sizeDeltaUsd, estimatedCollateralUsd, position.sizeInUsd);
|
|
102
|
+
}
|
|
103
|
+
values.isFullClose = getIsFullClose({
|
|
104
|
+
position,
|
|
105
|
+
sizeDeltaUsd: values.sizeDeltaUsd,
|
|
106
|
+
indexPrice: values.indexPrice,
|
|
107
|
+
remainingCollateralUsd: estimatedCollateralUsd - estimatedCollateralDeltaUsd,
|
|
108
|
+
minCollateralUsd,
|
|
109
|
+
minPositionSizeUsd,
|
|
110
|
+
});
|
|
111
|
+
if (values.isFullClose) {
|
|
112
|
+
values.sizeDeltaUsd = position.sizeInUsd;
|
|
113
|
+
values.sizeDeltaInTokens = position.sizeInTokens;
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
116
|
+
if (position.isLong) {
|
|
117
|
+
values.sizeDeltaInTokens = roundUpDivision(position.sizeInTokens * values.sizeDeltaUsd, position.sizeInUsd);
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
values.sizeDeltaInTokens = bigMath.mulDiv(position.sizeInTokens, values.sizeDeltaUsd, position.sizeInUsd);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
// PNL
|
|
124
|
+
values.estimatedPnl = getPositionPnlUsd({
|
|
125
|
+
marketInfo,
|
|
126
|
+
sizeInUsd: position.sizeInUsd,
|
|
127
|
+
sizeInTokens: position.sizeInTokens,
|
|
128
|
+
markPrice: values.indexPrice,
|
|
129
|
+
isLong,
|
|
130
|
+
});
|
|
131
|
+
values.realizedPnl = bigMath.mulDiv(values.estimatedPnl, values.sizeDeltaInTokens, position.sizeInTokens);
|
|
132
|
+
values.realizedPnlPercentage =
|
|
133
|
+
estimatedCollateralUsd !== 0n ? getBasisPoints(values.realizedPnl, estimatedCollateralUsd) : 0n;
|
|
134
|
+
values.estimatedPnlPercentage =
|
|
135
|
+
estimatedCollateralUsd !== 0n ? getBasisPoints(values.estimatedPnl, estimatedCollateralUsd) : 0n;
|
|
136
|
+
applyAcceptablePrice({
|
|
137
|
+
position,
|
|
138
|
+
marketInfo,
|
|
139
|
+
isLong,
|
|
140
|
+
isTrigger,
|
|
141
|
+
fixedAcceptablePriceImpactBps,
|
|
142
|
+
acceptablePriceImpactBuffer,
|
|
143
|
+
values,
|
|
144
|
+
});
|
|
145
|
+
// Profit
|
|
146
|
+
let profitUsd = 0n;
|
|
147
|
+
if (values.realizedPnl > 0) {
|
|
148
|
+
profitUsd = profitUsd + values.realizedPnl;
|
|
149
|
+
}
|
|
150
|
+
if (values.totalPendingImpactDeltaUsd > 0) {
|
|
151
|
+
profitUsd = profitUsd + values.totalPendingImpactDeltaUsd;
|
|
152
|
+
}
|
|
153
|
+
const profitAmount = convertToTokenAmount(profitUsd, collateralToken.decimals, values.collateralPrice);
|
|
154
|
+
// Fees
|
|
155
|
+
const positionFeeInfo = getPositionFee(marketInfo, values.sizeDeltaUsd, values.balanceWasImproved, userReferralInfo);
|
|
156
|
+
const estimatedPositionFeeCost = estimateCollateralCost(positionFeeInfo.positionFeeUsd, collateralToken, values.collateralPrice);
|
|
157
|
+
const estimatedDiscountCost = estimateCollateralCost(positionFeeInfo.discountUsd, collateralToken, values.collateralPrice);
|
|
158
|
+
values.positionFeeUsd = estimatedPositionFeeCost.usd;
|
|
159
|
+
values.feeDiscountUsd = estimatedDiscountCost.usd;
|
|
160
|
+
values.uiFeeUsd = applyFactor(values.sizeDeltaUsd, uiFeeFactor);
|
|
161
|
+
const borrowFeeCost = estimateCollateralCost(position.pendingBorrowingFeesUsd, collateralToken, values.collateralPrice);
|
|
162
|
+
values.borrowingFeeUsd = borrowFeeCost.usd;
|
|
163
|
+
const fundingFeeCost = estimateCollateralCost(position.pendingFundingFeesUsd, collateralToken, values.collateralPrice);
|
|
164
|
+
values.fundingFeeUsd = fundingFeeCost.usd;
|
|
165
|
+
if (profitUsd > 0 && values.decreaseSwapType === DecreasePositionSwapType.SwapPnlTokenToCollateralToken) {
|
|
166
|
+
const swapProfitStats = getSwapStats({
|
|
167
|
+
marketInfo,
|
|
168
|
+
tokenInAddress: pnlToken.address,
|
|
169
|
+
tokenOutAddress: collateralToken.address,
|
|
170
|
+
usdIn: profitUsd,
|
|
171
|
+
shouldApplyPriceImpact: true,
|
|
172
|
+
isAtomicSwap: false,
|
|
173
|
+
});
|
|
174
|
+
values.swapProfitFeeUsd = swapProfitStats.swapFeeUsd - swapProfitStats.priceImpactDeltaUsd;
|
|
175
|
+
values.swapUiFeeUsd = applyFactor(swapProfitStats.usdIn, uiFeeFactor);
|
|
176
|
+
}
|
|
177
|
+
else {
|
|
178
|
+
values.swapProfitFeeUsd = 0n;
|
|
179
|
+
}
|
|
180
|
+
const totalFeesUsd = getTotalFeesUsdForDecrease({
|
|
181
|
+
positionFeeUsd: values.positionFeeUsd,
|
|
182
|
+
borrowingFeeUsd: values.borrowingFeeUsd,
|
|
183
|
+
fundingFeeUsd: values.fundingFeeUsd,
|
|
184
|
+
swapProfitFeeUsd: values.swapProfitFeeUsd,
|
|
185
|
+
swapUiFeeUsd: values.swapUiFeeUsd,
|
|
186
|
+
uiFeeUsd: values.uiFeeUsd,
|
|
187
|
+
pnlUsd: values.realizedPnl,
|
|
188
|
+
totalPendingImpactDeltaUsd: values.totalPendingImpactDeltaUsd,
|
|
189
|
+
});
|
|
190
|
+
const payedInfo = payForCollateralCost({
|
|
191
|
+
initialCostUsd: totalFeesUsd,
|
|
192
|
+
collateralToken,
|
|
193
|
+
collateralPrice: values.collateralPrice,
|
|
194
|
+
outputAmount: profitAmount,
|
|
195
|
+
remainingCollateralAmount: position.collateralAmount,
|
|
196
|
+
});
|
|
197
|
+
values.payedOutputUsd = convertToUsd(payedInfo.paidOutputAmount, collateralToken.decimals, values.collateralPrice);
|
|
198
|
+
values.payedRemainingCollateralAmount = payedInfo.paidRemainingCollateralAmount;
|
|
199
|
+
values.payedRemainingCollateralUsd = convertToUsd(payedInfo.paidRemainingCollateralAmount, collateralToken.decimals, values.collateralPrice);
|
|
200
|
+
// Collateral delta
|
|
201
|
+
if (values.isFullClose) {
|
|
202
|
+
values.collateralDeltaUsd = estimatedCollateralUsd;
|
|
203
|
+
values.collateralDeltaAmount = position.collateralAmount;
|
|
204
|
+
values.receiveTokenAmount = payedInfo.outputAmount + payedInfo.remainingCollateralAmount;
|
|
205
|
+
}
|
|
206
|
+
else if (keepLeverage &&
|
|
207
|
+
position.sizeInUsd > 0 &&
|
|
208
|
+
estimatedCollateralUsd > 0 &&
|
|
209
|
+
payedInfo.remainingCollateralAmount > 0) {
|
|
210
|
+
const remainingCollateralUsd = convertToUsd(payedInfo.remainingCollateralAmount, collateralToken.decimals, values.collateralPrice);
|
|
211
|
+
const nextSizeInUsd = position.sizeInUsd - values.sizeDeltaUsd;
|
|
212
|
+
const leverageWithoutPnl = getLeverage({
|
|
213
|
+
sizeInUsd: position.sizeInUsd,
|
|
214
|
+
collateralUsd: estimatedCollateralUsd,
|
|
215
|
+
pendingBorrowingFeesUsd: position.pendingBorrowingFeesUsd,
|
|
216
|
+
pendingFundingFeesUsd: position.pendingFundingFeesUsd,
|
|
217
|
+
pnl: undefined,
|
|
218
|
+
});
|
|
219
|
+
values.collateralDeltaUsd =
|
|
220
|
+
/**
|
|
221
|
+
* 1. @see https://app.asana.com/0/1204313444805313/1207549197964321/f
|
|
222
|
+
* 2. leverageWithoutPnl may be zero if sizeInUsd is defaulted to 0n when position not ready yet
|
|
223
|
+
*/
|
|
224
|
+
leverageWithoutPnl !== undefined && leverageWithoutPnl !== 0n
|
|
225
|
+
? remainingCollateralUsd - bigMath.mulDiv(nextSizeInUsd, BASIS_POINTS_DIVISOR_BIGINT, leverageWithoutPnl)
|
|
226
|
+
: 0n;
|
|
227
|
+
values.collateralDeltaAmount = convertToTokenAmount(values.collateralDeltaUsd, collateralToken.decimals, values.collateralPrice);
|
|
228
|
+
values.receiveTokenAmount = payedInfo.outputAmount + values.collateralDeltaAmount;
|
|
229
|
+
}
|
|
230
|
+
else {
|
|
231
|
+
values.collateralDeltaUsd = 0n;
|
|
232
|
+
values.collateralDeltaAmount = 0n;
|
|
233
|
+
values.receiveTokenAmount = payedInfo.outputAmount;
|
|
234
|
+
}
|
|
235
|
+
values.receiveUsd = convertToUsd(values.receiveTokenAmount, collateralToken.decimals, values.collateralPrice);
|
|
236
|
+
return values;
|
|
237
|
+
}
|
|
238
|
+
export function getIsFullClose(p) {
|
|
239
|
+
const { position, sizeDeltaUsd, indexPrice, remainingCollateralUsd, minCollateralUsd, minPositionSizeUsd } = p;
|
|
240
|
+
const { marketInfo, isLong } = position;
|
|
241
|
+
if (position.sizeInUsd - sizeDeltaUsd < expandDecimals(1, USD_DECIMALS)) {
|
|
242
|
+
return true;
|
|
243
|
+
}
|
|
244
|
+
const estimatedPnl = getPositionPnlUsd({
|
|
245
|
+
marketInfo,
|
|
246
|
+
sizeInUsd: position.sizeInUsd,
|
|
247
|
+
sizeInTokens: position.sizeInTokens,
|
|
248
|
+
markPrice: indexPrice,
|
|
249
|
+
isLong,
|
|
250
|
+
});
|
|
251
|
+
const realizedPnl = bigMath.mulDiv(estimatedPnl, sizeDeltaUsd, position.sizeInUsd);
|
|
252
|
+
const estimatedRemainingPnl = estimatedPnl - realizedPnl;
|
|
253
|
+
if (realizedPnl < 0) {
|
|
254
|
+
const estimatedRemainingCollateralUsd = remainingCollateralUsd - realizedPnl;
|
|
255
|
+
let minCollateralFactor = isLong
|
|
256
|
+
? marketInfo.minCollateralFactorForOpenInterestLong
|
|
257
|
+
: marketInfo.minCollateralFactorForOpenInterestShort;
|
|
258
|
+
const minCollateralFactorForMarket = marketInfo.minCollateralFactor;
|
|
259
|
+
if (minCollateralFactorForMarket > minCollateralFactor) {
|
|
260
|
+
minCollateralFactor = minCollateralFactorForMarket;
|
|
261
|
+
}
|
|
262
|
+
const minCollateralUsdForLeverage = applyFactor(position.sizeInUsd, minCollateralFactor);
|
|
263
|
+
const willCollateralBeSufficient = estimatedRemainingCollateralUsd >= minCollateralUsdForLeverage;
|
|
264
|
+
if (!willCollateralBeSufficient) {
|
|
265
|
+
if (estimatedRemainingCollateralUsd + estimatedRemainingPnl < minCollateralUsd ||
|
|
266
|
+
position.sizeInUsd - sizeDeltaUsd < minPositionSizeUsd) {
|
|
267
|
+
return true;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
return false;
|
|
272
|
+
}
|
|
273
|
+
export function getMinCollateralUsdForLeverage(position, openInterestDelta) {
|
|
274
|
+
const minCollateralFactor = getMinCollateralFactorForPosition(position, openInterestDelta);
|
|
275
|
+
return applyFactor(position.sizeInUsd, minCollateralFactor);
|
|
276
|
+
}
|
|
277
|
+
export function payForCollateralCost(p) {
|
|
278
|
+
const { initialCostUsd, collateralToken, collateralPrice, outputAmount, remainingCollateralAmount } = p;
|
|
279
|
+
const values = {
|
|
280
|
+
outputAmount: BigInt(outputAmount),
|
|
281
|
+
remainingCollateralAmount: BigInt(remainingCollateralAmount),
|
|
282
|
+
paidOutputAmount: 0n,
|
|
283
|
+
paidRemainingCollateralAmount: 0n,
|
|
284
|
+
};
|
|
285
|
+
let remainingCostAmount = convertToTokenAmount(initialCostUsd, collateralToken.decimals, collateralPrice);
|
|
286
|
+
if (remainingCostAmount == 0n) {
|
|
287
|
+
return values;
|
|
288
|
+
}
|
|
289
|
+
if (values.outputAmount > 0) {
|
|
290
|
+
if (values.outputAmount > remainingCostAmount) {
|
|
291
|
+
values.outputAmount = values.outputAmount - remainingCostAmount;
|
|
292
|
+
values.paidOutputAmount = remainingCostAmount;
|
|
293
|
+
remainingCostAmount = 0n;
|
|
294
|
+
}
|
|
295
|
+
else {
|
|
296
|
+
remainingCostAmount = remainingCostAmount - values.outputAmount;
|
|
297
|
+
values.paidOutputAmount = values.outputAmount;
|
|
298
|
+
values.outputAmount = 0n;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
if (remainingCostAmount == 0n) {
|
|
302
|
+
return values;
|
|
303
|
+
}
|
|
304
|
+
if (values.remainingCollateralAmount > remainingCostAmount) {
|
|
305
|
+
values.remainingCollateralAmount = values.remainingCollateralAmount - remainingCostAmount;
|
|
306
|
+
values.paidRemainingCollateralAmount = remainingCostAmount;
|
|
307
|
+
remainingCostAmount = 0n;
|
|
308
|
+
}
|
|
309
|
+
else {
|
|
310
|
+
remainingCostAmount = remainingCostAmount - remainingCollateralAmount;
|
|
311
|
+
values.paidRemainingCollateralAmount = values.remainingCollateralAmount;
|
|
312
|
+
values.remainingCollateralAmount = 0n;
|
|
313
|
+
}
|
|
314
|
+
return values;
|
|
315
|
+
}
|
|
316
|
+
function applyAcceptablePrice(p) {
|
|
317
|
+
const { position, marketInfo, isLong, values, isTrigger, fixedAcceptablePriceImpactBps, acceptablePriceImpactBuffer, } = p;
|
|
318
|
+
const acceptablePriceInfo = getAcceptablePriceInfo({
|
|
319
|
+
marketInfo,
|
|
320
|
+
isIncrease: false,
|
|
321
|
+
isLimit: false,
|
|
322
|
+
isLong,
|
|
323
|
+
indexPrice: values.indexPrice,
|
|
324
|
+
sizeDeltaUsd: values.sizeDeltaUsd,
|
|
325
|
+
});
|
|
326
|
+
values.acceptablePrice = acceptablePriceInfo.acceptablePrice;
|
|
327
|
+
values.acceptablePriceDeltaBps = acceptablePriceInfo.acceptablePriceDeltaBps;
|
|
328
|
+
values.balanceWasImproved = acceptablePriceInfo.balanceWasImproved;
|
|
329
|
+
const totalImpactValues = getNetPriceImpactDeltaUsdForDecrease({
|
|
330
|
+
marketInfo,
|
|
331
|
+
sizeInUsd: position?.sizeInUsd ?? 0n,
|
|
332
|
+
pendingImpactAmount: position?.pendingImpactAmount ?? 0n,
|
|
333
|
+
sizeDeltaUsd: values.sizeDeltaUsd,
|
|
334
|
+
priceImpactDeltaUsd: acceptablePriceInfo.priceImpactDeltaUsd,
|
|
335
|
+
});
|
|
336
|
+
values.closePriceImpactDeltaUsd = acceptablePriceInfo.priceImpactDeltaUsd;
|
|
337
|
+
values.totalPendingImpactDeltaUsd = totalImpactValues.totalImpactDeltaUsd;
|
|
338
|
+
values.proportionalPendingImpactDeltaUsd = totalImpactValues.proportionalPendingImpactDeltaUsd;
|
|
339
|
+
values.priceImpactDiffUsd = totalImpactValues.priceImpactDiffUsd;
|
|
340
|
+
if (isTrigger) {
|
|
341
|
+
if (values.triggerOrderType === OrderType.StopLossDecrease) {
|
|
342
|
+
if (isLong) {
|
|
343
|
+
values.acceptablePrice = 0n;
|
|
344
|
+
}
|
|
345
|
+
else {
|
|
346
|
+
values.acceptablePrice = MaxUint256;
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
else {
|
|
350
|
+
let maxNegativePriceImpactBps = fixedAcceptablePriceImpactBps;
|
|
351
|
+
values.recommendedAcceptablePriceDeltaBps = getDefaultAcceptablePriceImpactBps({
|
|
352
|
+
isIncrease: false,
|
|
353
|
+
isLong,
|
|
354
|
+
indexPrice: values.indexPrice,
|
|
355
|
+
sizeDeltaUsd: values.sizeDeltaUsd,
|
|
356
|
+
priceImpactDeltaUsd: values.closePriceImpactDeltaUsd,
|
|
357
|
+
acceptablePriceImapctBuffer: acceptablePriceImpactBuffer || DEFAULT_ACCEPTABLE_PRICE_IMPACT_BUFFER,
|
|
358
|
+
});
|
|
359
|
+
if (maxNegativePriceImpactBps === undefined) {
|
|
360
|
+
maxNegativePriceImpactBps = values.recommendedAcceptablePriceDeltaBps;
|
|
361
|
+
}
|
|
362
|
+
const triggerAcceptablePriceInfo = getAcceptablePriceInfo({
|
|
363
|
+
marketInfo,
|
|
364
|
+
isIncrease: false,
|
|
365
|
+
isLimit: false,
|
|
366
|
+
isLong,
|
|
367
|
+
indexPrice: values.indexPrice,
|
|
368
|
+
sizeDeltaUsd: values.sizeDeltaUsd,
|
|
369
|
+
maxNegativePriceImpactBps,
|
|
370
|
+
});
|
|
371
|
+
values.acceptablePrice = triggerAcceptablePriceInfo.acceptablePrice;
|
|
372
|
+
values.acceptablePriceDeltaBps = triggerAcceptablePriceInfo.acceptablePriceDeltaBps;
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
return values;
|
|
376
|
+
}
|
|
377
|
+
export function estimateCollateralCost(baseUsd, collateralToken, collateralPrice) {
|
|
378
|
+
const amount = convertToTokenAmount(baseUsd, collateralToken.decimals, collateralToken.prices.minPrice);
|
|
379
|
+
const usd = convertToUsd(amount, collateralToken.decimals, collateralPrice);
|
|
380
|
+
return {
|
|
381
|
+
amount,
|
|
382
|
+
usd,
|
|
383
|
+
};
|
|
384
|
+
}
|
|
385
|
+
export function getTotalFeesUsdForDecrease({ positionFeeUsd, borrowingFeeUsd, fundingFeeUsd, swapProfitFeeUsd, swapUiFeeUsd, uiFeeUsd, pnlUsd, totalPendingImpactDeltaUsd, }) {
|
|
386
|
+
const negativePriceImpactUsd = totalPendingImpactDeltaUsd < 0 ? bigMath.abs(totalPendingImpactDeltaUsd) : 0n;
|
|
387
|
+
const negativePnlUsd = pnlUsd < 0 ? bigMath.abs(pnlUsd) : 0n;
|
|
388
|
+
const totalFeesUsd = positionFeeUsd +
|
|
389
|
+
borrowingFeeUsd +
|
|
390
|
+
fundingFeeUsd +
|
|
391
|
+
swapProfitFeeUsd +
|
|
392
|
+
swapUiFeeUsd +
|
|
393
|
+
uiFeeUsd +
|
|
394
|
+
negativePnlUsd +
|
|
395
|
+
negativePriceImpactUsd;
|
|
396
|
+
return totalFeesUsd;
|
|
397
|
+
}
|
|
398
|
+
export function getNextPositionValuesForDecreaseTrade(p) {
|
|
399
|
+
const { existingPosition, marketInfo, collateralToken, sizeDeltaUsd, sizeDeltaInTokens, realizedPnl, estimatedPnl, collateralDeltaUsd, collateralDeltaAmount, payedRemainingCollateralUsd, payedRemainingCollateralAmount, showPnlInLeverage, proportionalPendingImpactDeltaUsd, isLong, minCollateralUsd, userReferralInfo, } = p;
|
|
400
|
+
const nextSizeUsd = existingPosition ? existingPosition.sizeInUsd - sizeDeltaUsd : 0n;
|
|
401
|
+
const nextSizeInTokens = existingPosition ? existingPosition.sizeInTokens - sizeDeltaInTokens : 0n;
|
|
402
|
+
let nextCollateralUsd = existingPosition
|
|
403
|
+
? existingPosition.collateralUsd - collateralDeltaUsd - payedRemainingCollateralUsd
|
|
404
|
+
: 0n;
|
|
405
|
+
if (nextCollateralUsd < 0) {
|
|
406
|
+
nextCollateralUsd = 0n;
|
|
407
|
+
}
|
|
408
|
+
let nextCollateralAmount = existingPosition
|
|
409
|
+
? existingPosition.collateralAmount - collateralDeltaAmount - payedRemainingCollateralAmount
|
|
410
|
+
: 0n;
|
|
411
|
+
if (nextCollateralAmount < 0) {
|
|
412
|
+
nextCollateralAmount = 0n;
|
|
413
|
+
}
|
|
414
|
+
const nextPnl = estimatedPnl ? estimatedPnl - realizedPnl : 0n;
|
|
415
|
+
const nextPnlPercentage = nextCollateralUsd !== 0n ? getBasisPoints(nextPnl, nextCollateralUsd) : 0n;
|
|
416
|
+
const nextLeverage = getLeverage({
|
|
417
|
+
sizeInUsd: nextSizeUsd,
|
|
418
|
+
collateralUsd: nextCollateralUsd,
|
|
419
|
+
pnl: showPnlInLeverage ? nextPnl : undefined,
|
|
420
|
+
pendingBorrowingFeesUsd: 0n, // deducted on order
|
|
421
|
+
pendingFundingFeesUsd: 0n, // deducted on order
|
|
422
|
+
});
|
|
423
|
+
const nextLiqPrice = getLiquidationPrice({
|
|
424
|
+
marketInfo,
|
|
425
|
+
collateralToken,
|
|
426
|
+
sizeInTokens: nextSizeInTokens,
|
|
427
|
+
sizeInUsd: nextSizeUsd,
|
|
428
|
+
collateralUsd: nextCollateralUsd,
|
|
429
|
+
collateralAmount: nextCollateralAmount,
|
|
430
|
+
minCollateralUsd,
|
|
431
|
+
userReferralInfo,
|
|
432
|
+
pendingImpactAmount: existingPosition?.pendingImpactAmount ?? 0n,
|
|
433
|
+
pendingBorrowingFeesUsd: 0n, // deducted on order
|
|
434
|
+
pendingFundingFeesUsd: 0n, // deducted on order
|
|
435
|
+
isLong: isLong,
|
|
436
|
+
});
|
|
437
|
+
const nextPendingImpactDeltaUsd = existingPosition?.pendingImpactUsd !== undefined && proportionalPendingImpactDeltaUsd !== undefined
|
|
438
|
+
? existingPosition.pendingImpactUsd - proportionalPendingImpactDeltaUsd
|
|
439
|
+
: 0n;
|
|
440
|
+
return {
|
|
441
|
+
nextSizeUsd,
|
|
442
|
+
nextCollateralUsd,
|
|
443
|
+
nextLiqPrice,
|
|
444
|
+
nextPnl,
|
|
445
|
+
nextPnlPercentage,
|
|
446
|
+
nextLeverage,
|
|
447
|
+
nextPendingImpactDeltaUsd,
|
|
448
|
+
};
|
|
449
|
+
}
|
|
450
|
+
function getDecreaseSwapType(pnlToken, collateralToken, receiveToken) {
|
|
451
|
+
if (getIsEquivalentTokens(pnlToken, collateralToken)) {
|
|
452
|
+
return DecreasePositionSwapType.NoSwap;
|
|
453
|
+
}
|
|
454
|
+
else if (getIsEquivalentTokens(pnlToken, receiveToken)) {
|
|
455
|
+
return DecreasePositionSwapType.SwapCollateralTokenToPnlToken;
|
|
456
|
+
}
|
|
457
|
+
else {
|
|
458
|
+
return DecreasePositionSwapType.SwapPnlTokenToCollateralToken;
|
|
459
|
+
}
|
|
460
|
+
}
|