@gmx-io/sdk 1.5.0-alpha-1 → 1.5.0-alpha-3
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/scripts/generate-platform-tokens.js +200 -0
- package/build/cjs/scripts/subsquid-codegen.js +1 -1
- package/build/cjs/src/clients/v1/modules/accounts/accounts.js +1 -1
- package/build/cjs/src/clients/v1/modules/markets/index.js +1 -1
- package/build/cjs/src/clients/v1/modules/markets/markets.spec.js +2 -2
- package/build/cjs/src/clients/v1/modules/orders/orders.js +4 -4
- package/build/cjs/src/clients/v1/modules/positions/positions.js +9 -151
- package/build/cjs/src/clients/v1/modules/tokens/tokens.js +1 -1
- package/build/cjs/src/clients/v1/modules/tokens/tokens.spec.js +2 -2
- package/build/cjs/src/clients/v1/modules/trades/trades.js +1 -1
- package/build/cjs/src/clients/v1/modules/utils/utils.js +1 -1
- package/build/cjs/src/clients/v2/index.js +13 -1
- package/build/cjs/src/codegen/platformTokens.json +134 -0
- package/build/cjs/src/codegen/prebuilt/hashedKinkModelMarketRatesKeys.json +24 -0
- package/build/cjs/src/codegen/prebuilt/hashedMarketConfigKeys.json +171 -0
- package/build/cjs/src/codegen/prebuilt/hashedMarketValuesKeys.json +45 -0
- package/build/cjs/src/codegen/subsquid.js +142 -22
- package/build/cjs/src/configs/api.js +10 -6
- package/build/cjs/src/configs/markets.js +21 -0
- package/build/cjs/src/configs/tokens.js +35 -0
- package/build/cjs/src/utils/__tests__/fees.spec.js +1 -1
- package/build/cjs/src/utils/__tests__/numbers.spec.js +1 -1
- package/build/cjs/src/utils/__tests__/orderTransactions/batchUtils.spec.js +1 -1
- package/build/cjs/src/utils/__tests__/orderTransactions/decreaseOrders.spec.js +1 -1
- package/build/cjs/src/utils/__tests__/orderTransactions/exchangeRouterMulticall.spec.js +1 -1
- package/build/cjs/src/utils/__tests__/orderTransactions/externalCalls.spec.js +1 -1
- package/build/cjs/src/utils/__tests__/orderTransactions/increaseOrders.spec.js +1 -1
- package/build/cjs/src/utils/__tests__/orderTransactions/swapOrders.spec.js +1 -1
- package/build/cjs/src/utils/__tests__/orderTransactions/updateOrder.spec.js +1 -1
- package/build/cjs/src/utils/__tests__/parseError.spec.js +40 -0
- package/build/cjs/src/utils/__tests__/positions.spec.js +1 -1
- package/build/cjs/src/utils/__tests__/prices.spec.js +1 -1
- package/build/cjs/src/utils/__tests__/tokens.spec.js +191 -4
- package/build/cjs/src/utils/errors/parseError.js +67 -25
- package/build/cjs/src/utils/fees/executionFee.js +1 -1
- package/build/cjs/src/utils/fees/priceImpact.js +6 -1
- package/build/cjs/src/utils/markets/__tests__/mockMarkets.js +1 -1
- package/build/cjs/src/utils/markets/utils.js +15 -4
- package/build/cjs/src/utils/numbers/utils.js +4 -2
- package/build/cjs/src/utils/orders/api.js +13 -0
- package/build/cjs/src/utils/orders/index.js +1 -0
- package/build/cjs/src/utils/orders/utils.js +2 -2
- package/build/cjs/src/utils/positions/api.js +14 -0
- package/build/cjs/src/utils/positions/index.js +1 -0
- package/build/cjs/src/utils/positions/utils.js +213 -1
- package/build/cjs/src/utils/swap/__tests__/mockRouting.spec.js +1 -1
- package/build/cjs/src/utils/swap/__tests__/swapRouting.spec.js +1 -1
- package/build/cjs/src/utils/swap/__tests__/swapStats.spec.js +1 -1
- package/build/cjs/src/utils/swap/buildSwapStrategy.js +1 -1
- package/build/cjs/src/utils/swap/externalSwapQuoteByPath.js +1 -1
- package/build/cjs/src/utils/swap/swapPath.js +1 -1
- package/build/cjs/src/utils/swap/swapValues.js +1 -1
- package/build/cjs/src/utils/tokens/utils.js +23 -3
- package/build/cjs/src/utils/trade/decrease.js +1 -1
- package/build/cjs/src/utils/trade/increase.js +1 -1
- package/build/cjs/src/utils/trade/trade.js +1 -1
- package/build/esm/scripts/generate-platform-tokens.js +195 -0
- package/build/esm/scripts/subsquid-codegen.js +1 -1
- package/build/esm/src/clients/v1/modules/accounts/accounts.js +1 -1
- package/build/esm/src/clients/v1/modules/markets/index.js +1 -1
- package/build/esm/src/clients/v1/modules/markets/markets.spec.js +1 -1
- package/build/esm/src/clients/v1/modules/orders/orders.js +4 -4
- package/build/esm/src/clients/v1/modules/positions/positions.js +12 -154
- package/build/esm/src/clients/v1/modules/tokens/tokens.js +1 -1
- package/build/esm/src/clients/v1/modules/tokens/tokens.spec.js +1 -1
- package/build/esm/src/clients/v1/modules/trades/trades.js +1 -1
- package/build/esm/src/clients/v1/modules/utils/utils.js +1 -1
- package/build/esm/src/clients/v2/index.js +13 -1
- package/build/esm/src/codegen/platformTokens.json +134 -0
- package/build/esm/src/codegen/prebuilt/hashedKinkModelMarketRatesKeys.json +24 -0
- package/build/esm/src/codegen/prebuilt/hashedMarketConfigKeys.json +171 -0
- package/build/esm/src/codegen/prebuilt/hashedMarketValuesKeys.json +45 -0
- package/build/esm/src/codegen/subsquid.js +141 -21
- package/build/esm/src/configs/api.js +8 -5
- package/build/esm/src/configs/markets.js +21 -0
- package/build/esm/src/configs/tokens.js +35 -0
- package/build/esm/src/utils/__tests__/fees.spec.js +1 -1
- package/build/esm/src/utils/__tests__/numbers.spec.js +1 -1
- package/build/esm/src/utils/__tests__/orderTransactions/batchUtils.spec.js +1 -1
- package/build/esm/src/utils/__tests__/orderTransactions/decreaseOrders.spec.js +1 -1
- package/build/esm/src/utils/__tests__/orderTransactions/exchangeRouterMulticall.spec.js +1 -1
- package/build/esm/src/utils/__tests__/orderTransactions/externalCalls.spec.js +1 -1
- package/build/esm/src/utils/__tests__/orderTransactions/increaseOrders.spec.js +1 -1
- package/build/esm/src/utils/__tests__/orderTransactions/swapOrders.spec.js +1 -1
- package/build/esm/src/utils/__tests__/orderTransactions/updateOrder.spec.js +1 -1
- package/build/esm/src/utils/__tests__/parseError.spec.js +40 -0
- package/build/esm/src/utils/__tests__/positions.spec.js +1 -1
- package/build/esm/src/utils/__tests__/prices.spec.js +1 -1
- package/build/esm/src/utils/__tests__/tokens.spec.js +192 -5
- package/build/esm/src/utils/errors/parseError.js +64 -24
- package/build/esm/src/utils/fees/executionFee.js +1 -1
- package/build/esm/src/utils/fees/priceImpact.js +5 -1
- package/build/esm/src/utils/markets/__tests__/mockMarkets.js +1 -1
- package/build/esm/src/utils/markets/utils.js +15 -4
- package/build/esm/src/utils/numbers/utils.js +2 -0
- package/build/esm/src/utils/orders/api.js +9 -0
- package/build/esm/src/utils/orders/index.js +1 -0
- package/build/esm/src/utils/orders/utils.js +2 -2
- package/build/esm/src/utils/positions/api.js +10 -0
- package/build/esm/src/utils/positions/index.js +1 -0
- package/build/esm/src/utils/positions/utils.js +213 -3
- package/build/esm/src/utils/swap/__tests__/mockRouting.spec.js +1 -1
- package/build/esm/src/utils/swap/__tests__/swapRouting.spec.js +1 -1
- package/build/esm/src/utils/swap/__tests__/swapStats.spec.js +1 -1
- package/build/esm/src/utils/swap/buildSwapStrategy.js +1 -1
- package/build/esm/src/utils/swap/externalSwapQuoteByPath.js +1 -1
- package/build/esm/src/utils/swap/swapPath.js +1 -1
- package/build/esm/src/utils/swap/swapValues.js +1 -1
- package/build/esm/src/utils/tokens/utils.js +23 -3
- package/build/esm/src/utils/trade/decrease.js +1 -1
- package/build/esm/src/utils/trade/increase.js +1 -1
- package/build/esm/src/utils/trade/trade.js +1 -1
- package/build/types/scripts/generate-platform-tokens.d.ts +27 -0
- package/build/types/src/clients/v1/modules/markets/index.d.ts +1 -1
- package/build/types/src/clients/v1/modules/oracle.d.ts +1 -1
- package/build/types/src/clients/v1/modules/orders/helpers.d.ts +1 -1
- package/build/types/src/clients/v1/modules/orders/orders.d.ts +2 -2
- package/build/types/src/clients/v1/modules/orders/transactions/cancelOrdersTxn.d.ts +1 -1
- package/build/types/src/clients/v1/modules/orders/transactions/createDecreaseOrderTxn.d.ts +1 -1
- package/build/types/src/clients/v1/modules/orders/transactions/createIncreaseOrderTxn.d.ts +1 -1
- package/build/types/src/clients/v1/modules/orders/transactions/createSwapOrderTxn.d.ts +1 -1
- package/build/types/src/clients/v1/modules/orders/transactions/createWrapOrUnwrapTxn.d.ts +1 -1
- package/build/types/src/clients/v1/modules/orders/transactions/updateOrderTxn.d.ts +1 -1
- package/build/types/src/clients/v1/modules/orders/utils.d.ts +2 -2
- package/build/types/src/clients/v1/modules/positions/positions.d.ts +1 -1
- package/build/types/src/clients/v1/modules/tokens/tokens.d.ts +1 -1
- package/build/types/src/clients/v1/modules/trades/trades.d.ts +2 -2
- package/build/types/src/clients/v1/modules/utils/utils.d.ts +1 -1
- package/build/types/src/clients/v2/index.d.ts +7 -0
- package/build/types/src/codegen/prebuilt/index.d.ts +240 -0
- package/build/types/src/codegen/subsquid.d.ts +492 -60
- package/build/types/src/configs/api.d.ts +1 -0
- package/build/types/src/utils/errors/parseError.d.ts +13 -1
- package/build/types/src/utils/fees/executionFee.d.ts +1 -1
- package/build/types/src/utils/fees/getNaiveEstimatedGasBySwapCount.d.ts +1 -1
- package/build/types/src/utils/fees/index.d.ts +1 -1
- package/build/types/src/utils/fees/priceImpact.d.ts +1 -0
- package/build/types/src/utils/markets/api.d.ts +1 -1
- package/build/types/src/utils/markets/multicall.d.ts +1 -1
- package/build/types/src/utils/markets/types.d.ts +8 -2
- package/build/types/src/utils/markets/utils.d.ts +1 -1
- package/build/types/src/utils/numbers/utils.d.ts +2 -0
- package/build/types/src/utils/orders/api.d.ts +7 -0
- package/build/types/src/utils/orders/index.d.ts +1 -0
- package/build/types/src/utils/orders/types.d.ts +27 -0
- package/build/types/src/utils/orders/utils.d.ts +1 -1
- package/build/types/src/utils/positions/api.d.ts +8 -0
- package/build/types/src/utils/positions/index.d.ts +1 -0
- package/build/types/src/utils/positions/types.d.ts +5 -16
- package/build/types/src/utils/positions/utils.d.ts +42 -1
- package/build/types/src/utils/swap/buildSwapStrategy.d.ts +1 -1
- package/build/types/src/utils/swap/findSwapPathsBetweenTokens.d.ts +1 -1
- package/build/types/src/utils/swap/preparedSwapData.d.ts +1 -1
- package/build/types/src/utils/swap/swapRouting.d.ts +1 -1
- package/build/types/src/utils/tokens/types.d.ts +1 -1
- package/build/types/src/utils/trade/decrease.d.ts +1 -1
- package/build/types/src/utils/trade/increase.d.ts +1 -1
- package/build/types/src/utils/trade/trade.d.ts +1 -1
- package/build/types/src/utils/tradeHistory/types.d.ts +2 -2
- package/build/types/src/utils/tradeHistory/utils.d.ts +1 -1
- package/build/types/src/utils/twap/index.d.ts +1 -1
- package/package.json +4 -3
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getCappedPriceImpactPercentageFromFees = exports.applyImpactFactor = exports.calculateImpactForCrossoverRebalance = exports.calculateImpactForSameSideRebalance = exports.getPriceImpactUsd = exports.getNextPoolAmountsParams = exports.getPriceImpactForSwap = exports.getProportionalPendingImpactValues = exports.getPriceImpactForPosition = exports.getMaxPositionImpactFactors = exports.capPositionImpactUsdByMaxPriceImpactFactor = exports.capPositionImpactUsdByMaxImpactPool = exports.getCappedPositionImpactUsd = exports.applySwapImpactWithCap = exports.getPriceImpactByAcceptablePrice = void 0;
|
|
3
|
+
exports.getMaxNegativeImpactBps = exports.getCappedPriceImpactPercentageFromFees = exports.applyImpactFactor = exports.calculateImpactForCrossoverRebalance = exports.calculateImpactForSameSideRebalance = exports.getPriceImpactUsd = exports.getNextPoolAmountsParams = exports.getPriceImpactForSwap = exports.getProportionalPendingImpactValues = exports.getPriceImpactForPosition = exports.getMaxPositionImpactFactors = exports.capPositionImpactUsdByMaxPriceImpactFactor = exports.capPositionImpactUsdByMaxImpactPool = exports.getCappedPositionImpactUsd = exports.applySwapImpactWithCap = exports.getPriceImpactByAcceptablePrice = void 0;
|
|
4
4
|
const bigmath_1 = require("../bigmath");
|
|
5
5
|
const markets_1 = require("../markets");
|
|
6
6
|
const numbers_1 = require("../numbers");
|
|
@@ -370,3 +370,8 @@ function getCappedPriceImpactPercentageFromFees({ fees, isSwap, }) {
|
|
|
370
370
|
return fees?.positionNetPriceImpact?.precisePercentage ?? 0n;
|
|
371
371
|
}
|
|
372
372
|
exports.getCappedPriceImpactPercentageFromFees = getCappedPriceImpactPercentageFromFees;
|
|
373
|
+
function getMaxNegativeImpactBps(marketInfo) {
|
|
374
|
+
const factor = marketInfo.maxPositionImpactFactorNegative;
|
|
375
|
+
return factor / (0, numbers_1.expandDecimals)(1, numbers_1.PRECISION_DECIMALS - numbers_1.BASIS_POINTS_DECIMALS);
|
|
376
|
+
}
|
|
377
|
+
exports.getMaxNegativeImpactBps = getMaxNegativeImpactBps;
|
|
@@ -3,10 +3,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.mockMarketsInfoData = exports.mockMarketsData = void 0;
|
|
4
4
|
const viem_1 = require("viem");
|
|
5
5
|
const factors_1 = require("../../../configs/factors");
|
|
6
|
+
const mock_1 = require("../../../test/mock");
|
|
6
7
|
const markets_1 = require("..");
|
|
7
8
|
const numbers_1 = require("../../numbers");
|
|
8
9
|
const tokens_1 = require("../../tokens");
|
|
9
|
-
const mock_1 = require("../../../test/mock");
|
|
10
10
|
/**
|
|
11
11
|
* @param marketKeys - array of market keys in the following format: indexToken-longToken-shortToken
|
|
12
12
|
*/
|
|
@@ -234,6 +234,7 @@ function getMarket24Stats(dayPriceCandle) {
|
|
|
234
234
|
}
|
|
235
235
|
exports.getMarket24Stats = getMarket24Stats;
|
|
236
236
|
function getMarketTicker(marketInfo, dayPriceCandle) {
|
|
237
|
+
const { minPrice, maxPrice } = marketInfo.indexToken.prices;
|
|
237
238
|
const markPrice = (0, tokens_2.getMidPrice)(marketInfo.indexToken.prices);
|
|
238
239
|
const SECONDS_PER_HOUR = BigInt((0, time_1.periodToSeconds)(1, "1h"));
|
|
239
240
|
const fundingRateLong = (0, fees_1.getFundingFactorPerPeriod)(marketInfo, true, SECONDS_PER_HOUR);
|
|
@@ -242,8 +243,12 @@ function getMarketTicker(marketInfo, dayPriceCandle) {
|
|
|
242
243
|
const borrowingRateShort = (0, fees_1.getBorrowingFactorPerPeriod)(marketInfo, false, SECONDS_PER_HOUR);
|
|
243
244
|
const netRateLong = fundingRateLong - borrowingRateLong;
|
|
244
245
|
const netRateShort = fundingRateShort - borrowingRateShort;
|
|
245
|
-
const
|
|
246
|
-
const
|
|
246
|
+
const longInterestInTokens = getOpenInterestInTokens(marketInfo, true);
|
|
247
|
+
const shortInterestInTokens = getOpenInterestInTokens(marketInfo, false);
|
|
248
|
+
const longInterestUsd = getOpenInterestUsd(marketInfo, true);
|
|
249
|
+
const shortInterestUsd = getOpenInterestUsd(marketInfo, false);
|
|
250
|
+
const longInterestUsdMark = (0, tokens_2.convertToUsd)(longInterestInTokens, marketInfo.indexToken.decimals, markPrice);
|
|
251
|
+
const shortInterestUsdMark = (0, tokens_2.convertToUsd)(shortInterestInTokens, marketInfo.indexToken.decimals, markPrice);
|
|
247
252
|
const availableLiquidityLong = getAvailableUsdLiquidityForCollateral(marketInfo, true);
|
|
248
253
|
const availableLiquidityShort = getAvailableUsdLiquidityForCollateral(marketInfo, false);
|
|
249
254
|
const poolAmountLongUsd = getPoolUsdWithoutPnl(marketInfo, true, "midPrice");
|
|
@@ -252,6 +257,8 @@ function getMarketTicker(marketInfo, dayPriceCandle) {
|
|
|
252
257
|
return {
|
|
253
258
|
symbol: marketInfo.name,
|
|
254
259
|
marketTokenAddress: marketInfo.marketTokenAddress,
|
|
260
|
+
minPrice,
|
|
261
|
+
maxPrice,
|
|
255
262
|
markPrice,
|
|
256
263
|
high24h,
|
|
257
264
|
low24h,
|
|
@@ -259,8 +266,12 @@ function getMarketTicker(marketInfo, dayPriceCandle) {
|
|
|
259
266
|
close24h,
|
|
260
267
|
priceChange24h,
|
|
261
268
|
priceChangePercent24hBps,
|
|
262
|
-
|
|
263
|
-
|
|
269
|
+
longInterestInTokens,
|
|
270
|
+
shortInterestInTokens,
|
|
271
|
+
longInterestUsd,
|
|
272
|
+
shortInterestUsd,
|
|
273
|
+
longInterestUsdMark,
|
|
274
|
+
shortInterestUsdMark,
|
|
264
275
|
availableLiquidityLong,
|
|
265
276
|
availableLiquidityShort,
|
|
266
277
|
poolAmountLongUsd,
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
exports.absDiffBps = exports.clamp = exports.calculateDisplayDecimals = exports.deserializeBigIntsInObject = exports.serializeBigIntsInObject = exports.removeTrailingZeros = exports.maxbigint = exports.minBigNumber = exports.roundBigIntToDecimals = exports.roundToOrder = exports.roundToTwoDecimals = exports.roundUpDivision = void 0;
|
|
3
|
+
exports.toBigNumberWithDecimals = exports.roundWithDecimals = exports.getPlusOrMinusSymbol = exports.padDecimals = exports.limitDecimals = exports.getLimitedDisplay = exports.formatAmountFree = exports.formatArrayAmount = exports.formatKeyAmount = exports.formatAmount = exports.numberWithCommas = exports.formatFactor = exports.formatBalanceAmount = exports.formatAmountHuman = exports.formatPercentageDisplay = exports.formatUsdPrice = exports.formatRatePercentage = exports.formatTokenAmountWithUsd = exports.formatTokenAmount = exports.formatPercentage = exports.formatDeltaUsd = exports.formatBigUsd = exports.formatUsd = exports.adjustForDecimals = exports.bigintToNumber = exports.trimZeroDecimals = exports.numberToBigint = exports.applyFactor = exports.roundUpMagnitudeDivision = exports.getBasisPoints = exports.basisPointsToFloat = exports.expandDecimals = exports.safeDivide = exports.isNonZero = exports.TRIGGER_PREFIX_BELOW = exports.TRIGGER_PREFIX_ABOVE = exports.PERCENT_PRECISION_DECIMALS = exports.MaxInt256 = exports.MaxUint256 = exports.BN_NEGATIVE_ONE = exports.BN_ONE = exports.BN_ZERO = exports.FLOAT_PRECISION_SQRT = exports.FLOAT_PRECISION_SQRT_DECIMALS = exports.PRECISION = exports.PRECISION_DECIMALS = exports.BASIS_POINTS_DECIMALS = exports.BASIS_POINTS_DIVISOR_BIGINT = exports.BASIS_POINTS_DIVISOR = exports.USD_DECIMALS = void 0;
|
|
4
|
+
exports.absDiffBps = exports.clamp = exports.calculateDisplayDecimals = exports.deserializeBigIntsInObject = exports.serializeBigIntsInObject = exports.removeTrailingZeros = exports.maxbigint = exports.minBigNumber = exports.roundBigIntToDecimals = exports.roundToOrder = exports.roundToTwoDecimals = exports.roundUpDivision = exports.parseValue = exports.bigNumberify = void 0;
|
|
5
5
|
const viem_1 = require("viem");
|
|
6
6
|
const bigmath_1 = require("../bigmath");
|
|
7
7
|
exports.USD_DECIMALS = 30;
|
|
@@ -10,6 +10,8 @@ exports.BASIS_POINTS_DIVISOR_BIGINT = 10000n;
|
|
|
10
10
|
exports.BASIS_POINTS_DECIMALS = 4;
|
|
11
11
|
exports.PRECISION_DECIMALS = 30;
|
|
12
12
|
exports.PRECISION = expandDecimals(1, exports.PRECISION_DECIMALS);
|
|
13
|
+
exports.FLOAT_PRECISION_SQRT_DECIMALS = 15;
|
|
14
|
+
exports.FLOAT_PRECISION_SQRT = expandDecimals(1, exports.FLOAT_PRECISION_SQRT_DECIMALS);
|
|
13
15
|
exports.BN_ZERO = 0n;
|
|
14
16
|
exports.BN_ONE = 1n;
|
|
15
17
|
exports.BN_NEGATIVE_ONE = -1n;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.fetchApiOrders = void 0;
|
|
4
|
+
const numbers_1 = require("../numbers");
|
|
5
|
+
async function fetchApiOrders(ctx, params) {
|
|
6
|
+
const orders = await ctx.api.fetchJson("/orders", {
|
|
7
|
+
query: {
|
|
8
|
+
account: params.account,
|
|
9
|
+
},
|
|
10
|
+
});
|
|
11
|
+
return orders.map((o) => (0, numbers_1.deserializeBigIntsInObject)(o, { handleInts: true }));
|
|
12
|
+
}
|
|
13
|
+
exports.fetchApiOrders = fetchApiOrders;
|
|
@@ -14,5 +14,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./api"), exports);
|
|
17
18
|
__exportStar(require("./types"), exports);
|
|
18
19
|
__exportStar(require("./utils"), exports);
|
|
@@ -2,13 +2,13 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.getOrderTradeboxKey = exports.isOrderForPositionByData = exports.isOrderForPosition = exports.isVisibleOrder = exports.getOrderInfo = exports.getOrderKeys = exports.isPositionOrder = exports.isSwapOrder = exports.isTwapPositionOrder = exports.isTwapSwapOrder = exports.isTwapOrder = exports.isStopIncreaseOrderType = exports.isLimitIncreaseOrderType = exports.isLimitDecreaseOrderType = exports.isStopLossOrderType = exports.isLiquidationOrderType = exports.isLimitSwapOrderType = exports.isSwapOrderType = exports.isIncreaseOrderType = exports.isDecreaseOrderType = exports.isTriggerDecreaseOrderType = exports.isLimitOrderType = exports.isMarketOrderType = void 0;
|
|
4
4
|
const factors_1 = require("../../configs/factors");
|
|
5
|
-
const swapStats_1 = require("../swap/swapStats");
|
|
6
5
|
const bigmath_1 = require("../bigmath");
|
|
7
6
|
const objects_1 = require("../objects");
|
|
8
|
-
const types_1 = require("./types");
|
|
9
7
|
const positions_1 = require("../positions");
|
|
10
8
|
const prices_1 = require("../prices");
|
|
9
|
+
const swapStats_1 = require("../swap/swapStats");
|
|
11
10
|
const tokens_1 = require("../tokens");
|
|
11
|
+
const types_1 = require("./types");
|
|
12
12
|
function isMarketOrderType(orderType) {
|
|
13
13
|
return [types_1.OrderType.MarketDecrease, types_1.OrderType.MarketIncrease, types_1.OrderType.MarketSwap].includes(orderType);
|
|
14
14
|
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.fetchApiPositionsInfo = void 0;
|
|
4
|
+
const numbers_1 = require("../numbers");
|
|
5
|
+
async function fetchApiPositionsInfo(ctx, params) {
|
|
6
|
+
const positions = await ctx.api.fetchJson("/positions", {
|
|
7
|
+
query: {
|
|
8
|
+
account: params.account,
|
|
9
|
+
includeRelatedOrders: params.includeRelatedOrders || undefined,
|
|
10
|
+
},
|
|
11
|
+
});
|
|
12
|
+
return positions.map((p) => (0, numbers_1.deserializeBigIntsInObject)(p, { handleInts: true }));
|
|
13
|
+
}
|
|
14
|
+
exports.fetchApiPositionsInfo = fetchApiPositionsInfo;
|
|
@@ -14,5 +14,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./api"), exports);
|
|
17
18
|
__exportStar(require("./types"), exports);
|
|
18
19
|
__exportStar(require("./utils"), exports);
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
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;
|
|
3
|
+
exports.getPositionInfo = exports.getContractPositionDynamicFees = 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");
|
|
7
7
|
const markets_1 = require("../markets");
|
|
8
8
|
const numbers_1 = require("../numbers");
|
|
9
|
+
const prices_1 = require("../prices");
|
|
9
10
|
const tokens_1 = require("../tokens");
|
|
10
11
|
function getPositionKey(account, marketAddress, collateralAddress, isLong) {
|
|
11
12
|
return `${account}:${marketAddress}:${collateralAddress}:${isLong}`;
|
|
@@ -211,3 +212,214 @@ function getMinCollateralFactorForPosition(position, openInterestDelta) {
|
|
|
211
212
|
return minCollateralFactor;
|
|
212
213
|
}
|
|
213
214
|
exports.getMinCollateralFactorForPosition = getMinCollateralFactorForPosition;
|
|
215
|
+
function getFundingAmount(latestFundingAmountPerSize, positionFundingAmountPerSize, positionSizeInUsd) {
|
|
216
|
+
const fundingDiffFactor = latestFundingAmountPerSize - positionFundingAmountPerSize;
|
|
217
|
+
// fundingAmountPerSize values are stored with FLOAT_PRECISION_SQRT precision
|
|
218
|
+
return bigmath_1.bigMath.mulDiv(positionSizeInUsd, fundingDiffFactor, numbers_1.PRECISION * numbers_1.FLOAT_PRECISION_SQRT);
|
|
219
|
+
}
|
|
220
|
+
function getContractPositionDynamicFees({ position, marketInfo, marketFeeState, referralInfo, }) {
|
|
221
|
+
const { sizeInUsd, isLong, borrowingFactor } = position;
|
|
222
|
+
const isCollateralLongToken = position.collateralTokenAddress === marketInfo.longToken.address;
|
|
223
|
+
const collateralToken = isCollateralLongToken ? marketInfo.longToken : marketInfo.shortToken;
|
|
224
|
+
const collateralMinPrice = collateralToken.prices.minPrice;
|
|
225
|
+
const cumulativeBorrowingFactor = isLong
|
|
226
|
+
? marketFeeState.cumulativeBorrowingFactorLong
|
|
227
|
+
: marketFeeState.cumulativeBorrowingFactorShort;
|
|
228
|
+
const borrowingDiffFactor = cumulativeBorrowingFactor > borrowingFactor ? cumulativeBorrowingFactor - borrowingFactor : 0n;
|
|
229
|
+
const pendingBorrowingFeesUsd = (0, numbers_1.applyFactor)(sizeInUsd, borrowingDiffFactor);
|
|
230
|
+
let latestFundingFeeAmountPerSize;
|
|
231
|
+
if (isLong) {
|
|
232
|
+
latestFundingFeeAmountPerSize = isCollateralLongToken
|
|
233
|
+
? marketFeeState.fundingFeeAmountPerSizeLongLong
|
|
234
|
+
: marketFeeState.fundingFeeAmountPerSizeLongShort;
|
|
235
|
+
}
|
|
236
|
+
else {
|
|
237
|
+
latestFundingFeeAmountPerSize = isCollateralLongToken
|
|
238
|
+
? marketFeeState.fundingFeeAmountPerSizeShortLong
|
|
239
|
+
: marketFeeState.fundingFeeAmountPerSizeShortShort;
|
|
240
|
+
}
|
|
241
|
+
const fundingFeeAmount = getFundingAmount(latestFundingFeeAmountPerSize, position.fundingFeeAmountPerSize, sizeInUsd);
|
|
242
|
+
let latestLongTokenClaimableFundingAmountPerSize;
|
|
243
|
+
let latestShortTokenClaimableFundingAmountPerSize;
|
|
244
|
+
if (isLong) {
|
|
245
|
+
latestLongTokenClaimableFundingAmountPerSize = marketFeeState.claimableFundingAmountPerSizeLongLong;
|
|
246
|
+
latestShortTokenClaimableFundingAmountPerSize = marketFeeState.claimableFundingAmountPerSizeLongShort;
|
|
247
|
+
}
|
|
248
|
+
else {
|
|
249
|
+
latestLongTokenClaimableFundingAmountPerSize = marketFeeState.claimableFundingAmountPerSizeShortLong;
|
|
250
|
+
latestShortTokenClaimableFundingAmountPerSize = marketFeeState.claimableFundingAmountPerSizeShortShort;
|
|
251
|
+
}
|
|
252
|
+
const claimableLongTokenAmount = getFundingAmount(latestLongTokenClaimableFundingAmountPerSize, position.longTokenClaimableFundingAmountPerSize, sizeInUsd);
|
|
253
|
+
const claimableShortTokenAmount = getFundingAmount(latestShortTokenClaimableFundingAmountPerSize, position.shortTokenClaimableFundingAmountPerSize, sizeInUsd);
|
|
254
|
+
const { balanceWasImproved } = (0, fees_1.getPriceImpactForPosition)(marketInfo, -sizeInUsd, isLong);
|
|
255
|
+
const { positionFeeUsd, discountUsd, uiFeeUsd } = (0, fees_1.getPositionFee)(marketInfo, sizeInUsd, balanceWasImproved, referralInfo);
|
|
256
|
+
const positionFeeAmount = (0, tokens_1.convertToTokenAmount)(positionFeeUsd, collateralToken.decimals, collateralMinPrice) ?? 0n;
|
|
257
|
+
const traderDiscountAmount = (0, tokens_1.convertToTokenAmount)(discountUsd, collateralToken.decimals, collateralMinPrice) ?? 0n;
|
|
258
|
+
const uiFeeAmount = (0, tokens_1.convertToTokenAmount)(uiFeeUsd, collateralToken.decimals, collateralMinPrice) ?? 0n;
|
|
259
|
+
return {
|
|
260
|
+
pendingBorrowingFeesUsd,
|
|
261
|
+
fundingFeeAmount,
|
|
262
|
+
claimableLongTokenAmount,
|
|
263
|
+
claimableShortTokenAmount,
|
|
264
|
+
positionFeeAmount,
|
|
265
|
+
traderDiscountAmount,
|
|
266
|
+
uiFeeAmount,
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
exports.getContractPositionDynamicFees = getContractPositionDynamicFees;
|
|
270
|
+
function getPositionInfo(p) {
|
|
271
|
+
const { position, marketInfo, minCollateralUsd, userReferralInfo, showPnlInLeverage = true, uiFeeFactor } = p;
|
|
272
|
+
const { indexToken, longToken, shortToken } = marketInfo;
|
|
273
|
+
const isCollateralLongToken = position.collateralTokenAddress === longToken.address;
|
|
274
|
+
const collateralToken = isCollateralLongToken ? longToken : shortToken;
|
|
275
|
+
const pnlToken = position.isLong ? longToken : shortToken;
|
|
276
|
+
const collateralMinPrice = collateralToken.prices.minPrice;
|
|
277
|
+
const markPrice = (0, prices_1.getMarkPrice)({ prices: indexToken.prices, isLong: position.isLong, isIncrease: false });
|
|
278
|
+
const entryPrice = getEntryPrice({
|
|
279
|
+
sizeInTokens: position.sizeInTokens,
|
|
280
|
+
sizeInUsd: position.sizeInUsd,
|
|
281
|
+
indexToken,
|
|
282
|
+
});
|
|
283
|
+
const pendingFundingFeesUsd = (0, tokens_1.convertToUsd)(position.fundingFeeAmount, collateralToken.decimals, collateralMinPrice);
|
|
284
|
+
const pendingClaimableFundingFeesLongUsd = (0, tokens_1.convertToUsd)(position.claimableLongTokenAmount, longToken.decimals, longToken.prices.minPrice);
|
|
285
|
+
const pendingClaimableFundingFeesShortUsd = (0, tokens_1.convertToUsd)(position.claimableShortTokenAmount, shortToken.decimals, shortToken.prices.minPrice);
|
|
286
|
+
const pendingClaimableFundingFeesUsd = pendingClaimableFundingFeesLongUsd + pendingClaimableFundingFeesShortUsd;
|
|
287
|
+
const totalPendingFeesUsd = getPositionPendingFeesUsd({
|
|
288
|
+
pendingBorrowingFeesUsd: position.pendingBorrowingFeesUsd,
|
|
289
|
+
pendingFundingFeesUsd,
|
|
290
|
+
});
|
|
291
|
+
const collateralUsd = (0, tokens_1.convertToUsd)(position.collateralAmount, collateralToken.decimals, collateralMinPrice);
|
|
292
|
+
const remainingCollateralUsd = collateralUsd - totalPendingFeesUsd;
|
|
293
|
+
const remainingCollateralAmount = (0, tokens_1.convertToTokenAmount)(remainingCollateralUsd, collateralToken.decimals, collateralMinPrice);
|
|
294
|
+
const pnl = getPositionPnlUsd({
|
|
295
|
+
marketInfo,
|
|
296
|
+
sizeInUsd: position.sizeInUsd,
|
|
297
|
+
sizeInTokens: position.sizeInTokens,
|
|
298
|
+
markPrice,
|
|
299
|
+
isLong: position.isLong,
|
|
300
|
+
});
|
|
301
|
+
const pnlPercentage = collateralUsd !== 0n ? (0, numbers_1.getBasisPoints)(pnl, collateralUsd) : 0n;
|
|
302
|
+
const closeAcceptablePriceInfo = (0, prices_1.getAcceptablePriceInfo)({
|
|
303
|
+
marketInfo,
|
|
304
|
+
isIncrease: false,
|
|
305
|
+
isLimit: false,
|
|
306
|
+
isLong: position.isLong,
|
|
307
|
+
indexPrice: markPrice,
|
|
308
|
+
sizeDeltaUsd: position.sizeInUsd,
|
|
309
|
+
});
|
|
310
|
+
const positionFeeInfo = (0, fees_1.getPositionFee)(marketInfo, position.sizeInUsd, closeAcceptablePriceInfo.balanceWasImproved, userReferralInfo, uiFeeFactor);
|
|
311
|
+
const closingFeeUsd = positionFeeInfo.positionFeeUsd;
|
|
312
|
+
const uiFeeUsd = positionFeeInfo.uiFeeUsd ?? 0n;
|
|
313
|
+
const netPriceImpactValues = getNetPriceImpactDeltaUsdForDecrease({
|
|
314
|
+
marketInfo,
|
|
315
|
+
sizeInUsd: position.sizeInUsd,
|
|
316
|
+
pendingImpactAmount: position.pendingImpactAmount,
|
|
317
|
+
sizeDeltaUsd: position.sizeInUsd,
|
|
318
|
+
priceImpactDeltaUsd: closeAcceptablePriceInfo.priceImpactDeltaUsd,
|
|
319
|
+
});
|
|
320
|
+
const netValue = getPositionNetValue({
|
|
321
|
+
collateralUsd,
|
|
322
|
+
pnl,
|
|
323
|
+
pendingBorrowingFeesUsd: position.pendingBorrowingFeesUsd,
|
|
324
|
+
pendingFundingFeesUsd,
|
|
325
|
+
closingFeeUsd,
|
|
326
|
+
uiFeeUsd,
|
|
327
|
+
totalPendingImpactDeltaUsd: netPriceImpactValues.totalImpactDeltaUsd,
|
|
328
|
+
priceImpactDiffUsd: netPriceImpactValues.priceImpactDiffUsd,
|
|
329
|
+
});
|
|
330
|
+
const pnlAfterFees = getPositionPnlAfterFees({
|
|
331
|
+
pnl,
|
|
332
|
+
pendingBorrowingFeesUsd: position.pendingBorrowingFeesUsd,
|
|
333
|
+
pendingFundingFeesUsd,
|
|
334
|
+
closingFeeUsd,
|
|
335
|
+
uiFeeUsd,
|
|
336
|
+
totalPendingImpactDeltaUsd: netPriceImpactValues.totalImpactDeltaUsd,
|
|
337
|
+
priceImpactDiffUsd: netPriceImpactValues.priceImpactDiffUsd,
|
|
338
|
+
});
|
|
339
|
+
const pnlAfterFeesPercentage = collateralUsd !== 0n ? (0, numbers_1.getBasisPoints)(pnlAfterFees, collateralUsd + closingFeeUsd) : 0n;
|
|
340
|
+
const leverage = getLeverage({
|
|
341
|
+
sizeInUsd: position.sizeInUsd,
|
|
342
|
+
collateralUsd,
|
|
343
|
+
pnl: showPnlInLeverage ? pnl : undefined,
|
|
344
|
+
pendingBorrowingFeesUsd: position.pendingBorrowingFeesUsd,
|
|
345
|
+
pendingFundingFeesUsd,
|
|
346
|
+
});
|
|
347
|
+
const leverageWithPnl = getLeverage({
|
|
348
|
+
sizeInUsd: position.sizeInUsd,
|
|
349
|
+
collateralUsd,
|
|
350
|
+
pnl,
|
|
351
|
+
pendingBorrowingFeesUsd: position.pendingBorrowingFeesUsd,
|
|
352
|
+
pendingFundingFeesUsd,
|
|
353
|
+
});
|
|
354
|
+
const leverageWithoutPnl = getLeverage({
|
|
355
|
+
sizeInUsd: position.sizeInUsd,
|
|
356
|
+
collateralUsd,
|
|
357
|
+
pendingBorrowingFeesUsd: position.pendingBorrowingFeesUsd,
|
|
358
|
+
pendingFundingFeesUsd,
|
|
359
|
+
pnl: undefined,
|
|
360
|
+
});
|
|
361
|
+
const maxAllowedLeverage = (0, markets_1.getMaxAllowedLeverageByMinCollateralFactor)(marketInfo.minCollateralFactor);
|
|
362
|
+
const hasLowCollateral = (leverage !== undefined && leverage > maxAllowedLeverage) || false;
|
|
363
|
+
const liquidationPrice = getLiquidationPrice({
|
|
364
|
+
marketInfo,
|
|
365
|
+
collateralToken,
|
|
366
|
+
sizeInUsd: position.sizeInUsd,
|
|
367
|
+
sizeInTokens: position.sizeInTokens,
|
|
368
|
+
collateralUsd,
|
|
369
|
+
collateralAmount: position.collateralAmount,
|
|
370
|
+
pendingImpactAmount: position.pendingImpactAmount,
|
|
371
|
+
userReferralInfo,
|
|
372
|
+
minCollateralUsd,
|
|
373
|
+
pendingBorrowingFeesUsd: position.pendingBorrowingFeesUsd,
|
|
374
|
+
pendingFundingFeesUsd,
|
|
375
|
+
isLong: position.isLong,
|
|
376
|
+
});
|
|
377
|
+
const indexName = (0, markets_1.getMarketIndexName)({ indexToken, isSpotOnly: false });
|
|
378
|
+
const poolName = (0, markets_1.getMarketPoolName)({ longToken, shortToken });
|
|
379
|
+
const market = {
|
|
380
|
+
marketTokenAddress: marketInfo.marketTokenAddress,
|
|
381
|
+
indexTokenAddress: marketInfo.indexTokenAddress,
|
|
382
|
+
longTokenAddress: marketInfo.longTokenAddress,
|
|
383
|
+
shortTokenAddress: marketInfo.shortTokenAddress,
|
|
384
|
+
isSameCollaterals: marketInfo.isSameCollaterals,
|
|
385
|
+
isSpotOnly: marketInfo.isSpotOnly,
|
|
386
|
+
name: marketInfo.name,
|
|
387
|
+
data: marketInfo.data,
|
|
388
|
+
};
|
|
389
|
+
return {
|
|
390
|
+
...position,
|
|
391
|
+
market,
|
|
392
|
+
marketInfo,
|
|
393
|
+
indexName,
|
|
394
|
+
poolName,
|
|
395
|
+
indexToken,
|
|
396
|
+
collateralToken,
|
|
397
|
+
pnlToken,
|
|
398
|
+
longToken,
|
|
399
|
+
shortToken,
|
|
400
|
+
markPrice,
|
|
401
|
+
entryPrice,
|
|
402
|
+
liquidationPrice,
|
|
403
|
+
collateralUsd,
|
|
404
|
+
remainingCollateralUsd,
|
|
405
|
+
remainingCollateralAmount,
|
|
406
|
+
hasLowCollateral,
|
|
407
|
+
leverage,
|
|
408
|
+
leverageWithPnl,
|
|
409
|
+
leverageWithoutPnl,
|
|
410
|
+
pnl,
|
|
411
|
+
pnlPercentage,
|
|
412
|
+
pnlAfterFees,
|
|
413
|
+
pnlAfterFeesPercentage,
|
|
414
|
+
netValue,
|
|
415
|
+
netPriceImapctDeltaUsd: netPriceImpactValues.totalImpactDeltaUsd,
|
|
416
|
+
priceImpactDiffUsd: netPriceImpactValues.priceImpactDiffUsd,
|
|
417
|
+
pendingImpactUsd: netPriceImpactValues.proportionalPendingImpactDeltaUsd,
|
|
418
|
+
closePriceImpactDeltaUsd: closeAcceptablePriceInfo.priceImpactDeltaUsd,
|
|
419
|
+
closingFeeUsd,
|
|
420
|
+
uiFeeUsd,
|
|
421
|
+
pendingFundingFeesUsd,
|
|
422
|
+
pendingClaimableFundingFeesUsd,
|
|
423
|
+
};
|
|
424
|
+
}
|
|
425
|
+
exports.getPositionInfo = getPositionInfo;
|
|
@@ -3,13 +3,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
const vitest_1 = require("vitest");
|
|
4
4
|
const factors_1 = require("../../../configs/factors");
|
|
5
5
|
const tokens_1 = require("../../../configs/tokens");
|
|
6
|
+
const mock_1 = require("../../../test/mock");
|
|
6
7
|
const numbers_1 = require("../../numbers");
|
|
7
8
|
const types_1 = require("../../orders/types");
|
|
8
9
|
const swap_1 = require("..");
|
|
9
10
|
const buildMarketsAdjacencyGraph_1 = require("../buildMarketsAdjacencyGraph");
|
|
10
11
|
const findSwapPathsBetweenTokens_1 = require("../findSwapPathsBetweenTokens");
|
|
11
12
|
const swapRouting_1 = require("../swapRouting");
|
|
12
|
-
const mock_1 = require("../../../test/mock");
|
|
13
13
|
const marketKeys = [
|
|
14
14
|
"ETH-ETH-USDC",
|
|
15
15
|
"BTC-BTC-USDC",
|
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const vitest_1 = require("vitest");
|
|
4
4
|
const factors_1 = require("../../../configs/factors");
|
|
5
|
+
const mock_1 = require("../../../test/mock");
|
|
5
6
|
const bigmath_1 = require("../../bigmath");
|
|
6
7
|
const types_1 = require("../../orders/types");
|
|
7
|
-
const mock_1 = require("../../../test/mock");
|
|
8
8
|
const buildMarketsAdjacencyGraph_1 = require("../buildMarketsAdjacencyGraph");
|
|
9
9
|
const swapRouting_1 = require("../swapRouting");
|
|
10
10
|
const dollar = 10n ** BigInt(factors_1.USD_DECIMALS);
|
|
@@ -3,8 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
const vitest_1 = require("vitest");
|
|
4
4
|
const factors_1 = require("../../../configs/factors");
|
|
5
5
|
const tokens_1 = require("../../../configs/tokens");
|
|
6
|
-
const types_1 = require("../../orders/types");
|
|
7
6
|
const mock_1 = require("../../../test/mock");
|
|
7
|
+
const types_1 = require("../../orders/types");
|
|
8
8
|
const swapStats_1 = require("../swapStats");
|
|
9
9
|
const someWrappedToken = "0x0000000000000000000000000000000000000001";
|
|
10
10
|
const someNativeToken = "0x0000000000000000000000000000000000000000";
|
|
@@ -2,11 +2,11 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.buildReverseSwapStrategy = exports.buildSwapStrategy = void 0;
|
|
4
4
|
const bigmath_1 = require("../bigmath");
|
|
5
|
+
const types_1 = require("../orders/types");
|
|
5
6
|
const tokens_1 = require("../tokens");
|
|
6
7
|
const externalSwapPath_1 = require("./externalSwapPath");
|
|
7
8
|
const externalSwapQuoteByPath_1 = require("./externalSwapQuoteByPath");
|
|
8
9
|
const swapPath_1 = require("./swapPath");
|
|
9
|
-
const types_1 = require("../orders/types");
|
|
10
10
|
/*
|
|
11
11
|
Order/Priority of getting swap strategy:
|
|
12
12
|
1. Check if it needs a swap and return noSwap if tokens are equivalent, stake or unstake [noSwap]
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.getExternalSwapQuoteByPath = void 0;
|
|
4
|
-
const botanixStaking_1 = require("./botanixStaking");
|
|
5
4
|
const types_1 = require("../trade/types");
|
|
5
|
+
const botanixStaking_1 = require("./botanixStaking");
|
|
6
6
|
const getExternalSwapQuoteByPath = ({ amountIn, externalSwapPath, externalSwapQuoteParams, }) => {
|
|
7
7
|
if (amountIn === undefined ||
|
|
8
8
|
externalSwapQuoteParams.gasPrice === undefined ||
|
|
@@ -5,10 +5,10 @@ const markets_1 = require("../../configs/markets");
|
|
|
5
5
|
const tokens_1 = require("../../configs/tokens");
|
|
6
6
|
const LruCache_1 = require("../LruCache");
|
|
7
7
|
const markets_2 = require("../markets");
|
|
8
|
+
const types_1 = require("../orders/types");
|
|
8
9
|
const buildMarketsAdjacencyGraph_1 = require("./buildMarketsAdjacencyGraph");
|
|
9
10
|
const swapRouting_1 = require("./swapRouting");
|
|
10
11
|
const swapStats_1 = require("./swapStats");
|
|
11
|
-
const types_1 = require("../orders/types");
|
|
12
12
|
const getWrappedAddress = (chainId, address) => {
|
|
13
13
|
return address ? (0, tokens_1.convertTokenAddress)(chainId, address, "wrapped") : undefined;
|
|
14
14
|
};
|
|
@@ -5,9 +5,9 @@ const factors_1 = require("../../configs/factors");
|
|
|
5
5
|
const bigmath_1 = require("../bigmath");
|
|
6
6
|
const fees_1 = require("../fees");
|
|
7
7
|
const numbers_1 = require("../numbers");
|
|
8
|
+
const types_1 = require("../orders/types");
|
|
8
9
|
const tokens_1 = require("../tokens");
|
|
9
10
|
const buildSwapStrategy_1 = require("./buildSwapStrategy");
|
|
10
|
-
const types_1 = require("../orders/types");
|
|
11
11
|
function getSwapAmountsByFromValue(p) {
|
|
12
12
|
const { tokenIn, tokenOut, amountIn, triggerRatio, isLimit, swapOptimizationOrder, uiFeeFactor, allowedSwapSlippageBps, marketsInfoData, chainId, externalSwapQuoteParams, allowSameTokenSwap, } = p;
|
|
13
13
|
if (!externalSwapQuoteParams) {
|
|
@@ -87,6 +87,11 @@ function getTokensRatioByAmounts(p) {
|
|
|
87
87
|
const { fromToken, toToken, fromTokenAmount, toTokenAmount } = p;
|
|
88
88
|
const adjustedFromAmount = (fromTokenAmount * numbers_1.PRECISION) / (0, numbers_1.expandDecimals)(1, fromToken.decimals);
|
|
89
89
|
const adjustedToAmount = (toTokenAmount * numbers_1.PRECISION) / (0, numbers_1.expandDecimals)(1, toToken.decimals);
|
|
90
|
+
const areBothStablecoins = fromToken.isStable && toToken.isStable;
|
|
91
|
+
if (areBothStablecoins) {
|
|
92
|
+
const ratio = adjustedToAmount > 0 ? (adjustedFromAmount * numbers_1.PRECISION) / adjustedToAmount : 0n;
|
|
93
|
+
return { ratio, largestToken: toToken, smallestToken: fromToken };
|
|
94
|
+
}
|
|
90
95
|
const [smallestToken, largestToken, largestAmount, smallestAmount] = adjustedFromAmount > adjustedToAmount
|
|
91
96
|
? [fromToken, toToken, adjustedFromAmount, adjustedToAmount]
|
|
92
97
|
: [toToken, fromToken, adjustedToAmount, adjustedFromAmount];
|
|
@@ -106,17 +111,27 @@ function getTokensRatioByMinOutputAmountAndTriggerPrice(p) {
|
|
|
106
111
|
const adjustedFromAmount = (fromTokenAmount * numbers_1.PRECISION) / (0, numbers_1.expandDecimals)(1, fromToken.decimals);
|
|
107
112
|
const adjustedToAmount = (minOutputAmount * numbers_1.PRECISION) / (0, numbers_1.expandDecimals)(1, toToken.decimals);
|
|
108
113
|
const adjustedMinOutputAmount = (minOutputAmount * numbers_1.PRECISION) / (0, numbers_1.expandDecimals)(1, toToken.decimals);
|
|
109
|
-
|
|
110
|
-
|
|
114
|
+
const areBothStablecoins = fromToken.isStable && toToken.isStable;
|
|
115
|
+
const isFromAmountLarger = adjustedFromAmount > adjustedToAmount;
|
|
116
|
+
if (areBothStablecoins) {
|
|
117
|
+
smallestToken = fromToken;
|
|
118
|
+
largestToken = toToken;
|
|
119
|
+
largestAmount = adjustedFromAmount;
|
|
120
|
+
smallestAmount = adjustedToAmount;
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
[smallestToken, largestToken, largestAmount, smallestAmount] = isFromAmountLarger
|
|
111
124
|
? [fromToken, toToken, adjustedFromAmount, adjustedToAmount]
|
|
112
125
|
: [toToken, fromToken, adjustedToAmount, adjustedFromAmount];
|
|
126
|
+
}
|
|
113
127
|
ratio = smallestAmount > 0 ? (largestAmount * numbers_1.PRECISION) / smallestAmount : 0n;
|
|
114
128
|
if (triggerPrice === 0n) {
|
|
115
129
|
allowedSwapSlippageBps = factors_1.DEFAULT_ALLOWED_SWAP_SLIPPAGE_BPS;
|
|
116
130
|
acceptablePrice = ratio;
|
|
117
131
|
}
|
|
118
132
|
else {
|
|
119
|
-
const
|
|
133
|
+
const shouldDivide = areBothStablecoins ? true : isFromAmountLarger;
|
|
134
|
+
const outputAtTriggerPrice = shouldDivide
|
|
120
135
|
? (adjustedFromAmount * numbers_1.PRECISION) / triggerPrice
|
|
121
136
|
: (adjustedFromAmount * triggerPrice) / numbers_1.PRECISION;
|
|
122
137
|
allowedSwapSlippageBps =
|
|
@@ -160,6 +175,11 @@ function getIsUnstake(token1, token2) {
|
|
|
160
175
|
exports.getIsUnstake = getIsUnstake;
|
|
161
176
|
function getTokensRatioByPrice(p) {
|
|
162
177
|
const { fromToken, toToken, fromPrice, toPrice } = p;
|
|
178
|
+
const areBothStablecoins = fromToken.isStable && toToken.isStable;
|
|
179
|
+
if (areBothStablecoins) {
|
|
180
|
+
const ratio = fromPrice > 0n ? (toPrice * numbers_1.PRECISION) / fromPrice : numbers_1.PRECISION;
|
|
181
|
+
return { ratio, largestToken: toToken, smallestToken: fromToken };
|
|
182
|
+
}
|
|
163
183
|
const [largestToken, smallestToken, largestPrice, smallestPrice] = fromPrice > toPrice ? [fromToken, toToken, fromPrice, toPrice] : [toToken, fromToken, toPrice, fromPrice];
|
|
164
184
|
const ratio = (largestPrice * numbers_1.PRECISION) / smallestPrice;
|
|
165
185
|
return { ratio, largestToken, smallestToken };
|
|
@@ -5,11 +5,11 @@ const factors_1 = require("../../configs/factors");
|
|
|
5
5
|
const bigmath_1 = require("../bigmath");
|
|
6
6
|
const fees_1 = require("../fees");
|
|
7
7
|
const numbers_1 = require("../numbers");
|
|
8
|
+
const types_1 = require("../orders/types");
|
|
8
9
|
const positions_1 = require("../positions");
|
|
9
10
|
const prices_1 = require("../prices");
|
|
10
11
|
const swap_1 = require("../swap");
|
|
11
12
|
const tokens_1 = require("../tokens");
|
|
12
|
-
const types_1 = require("../orders/types");
|
|
13
13
|
function getDecreasePositionAmounts(p) {
|
|
14
14
|
const { marketInfo, collateralToken, isLong, position, closeSizeUsd, keepLeverage, triggerPrice, fixedAcceptablePriceImpactBps, acceptablePriceImpactBuffer, userReferralInfo, minCollateralUsd, minPositionSizeUsd, uiFeeFactor, triggerOrderType: orderType, receiveToken: receiveTokenArg, isSetAcceptablePriceImpactEnabled, } = p;
|
|
15
15
|
const { indexToken } = marketInfo;
|
|
@@ -6,11 +6,11 @@ const factors_1 = require("../../configs/factors");
|
|
|
6
6
|
const bigmath_1 = require("../bigmath");
|
|
7
7
|
const fees_1 = require("../fees");
|
|
8
8
|
const numbers_1 = require("../numbers");
|
|
9
|
+
const types_1 = require("../orders/types");
|
|
9
10
|
const positions_1 = require("../positions");
|
|
10
11
|
const prices_1 = require("../prices");
|
|
11
12
|
const swap_1 = require("../swap");
|
|
12
13
|
const tokens_1 = require("../tokens");
|
|
13
|
-
const types_1 = require("../orders/types");
|
|
14
14
|
function getIncreasePositionAmounts(p) {
|
|
15
15
|
const { marketInfo, indexToken, initialCollateralToken, collateralToken, initialCollateralAmount, indexTokenAmount, isLong, leverage, triggerPrice, limitOrderType, position, fixedAcceptablePriceImpactBps, acceptablePriceImpactBuffer, externalSwapQuote, findSwapPath, userReferralInfo, uiFeeFactor, strategy, marketsInfoData, chainId, externalSwapQuoteParams, isSetAcceptablePriceImpactEnabled, } = p;
|
|
16
16
|
const swapStrategy = {
|
|
@@ -5,7 +5,7 @@ const factors_1 = require("../../configs/factors");
|
|
|
5
5
|
const bigmath_1 = require("../bigmath");
|
|
6
6
|
const types_1 = require("../orders/types");
|
|
7
7
|
const prices_1 = require("../prices");
|
|
8
|
-
const types_2 = require("
|
|
8
|
+
const types_2 = require("./types");
|
|
9
9
|
function applySlippageToPrice(allowedSlippage, price, isIncrease, isLong) {
|
|
10
10
|
const shouldIncreasePrice = (0, prices_1.getShouldUseMaxPrice)(isIncrease, isLong);
|
|
11
11
|
const slippageBasisPoints = shouldIncreasePrice
|