@gmx-io/sdk 1.1.2 → 1.2.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/StBTC.json +1225 -0
- package/build/cjs/src/abis/SyntheticsReader.json +480 -19
- package/build/cjs/src/configs/__tests__/tokens.spec.js +1 -0
- package/build/cjs/src/configs/chains.js +57 -6
- package/build/cjs/src/configs/contracts.js +64 -68
- package/build/cjs/src/configs/dataStore.js +4 -3
- package/build/cjs/src/configs/express.js +2 -1
- package/build/cjs/src/configs/markets.js +37 -0
- package/build/cjs/src/configs/oracleKeeper.js +1 -0
- package/build/cjs/src/configs/tokens.js +130 -1
- package/build/cjs/src/modules/orders/helpers.js +12 -13
- package/build/cjs/src/modules/positions/positions.js +9 -0
- package/build/cjs/src/prebuilt/hashedKinkModelMarketRatesKeys.json +42 -0
- package/build/cjs/src/prebuilt/hashedMarketConfigKeys.json +257 -0
- package/build/cjs/src/prebuilt/hashedMarketValuesKeys.json +77 -0
- package/build/cjs/src/types/subsquid.js +103 -1
- package/build/cjs/src/utils/__tests__/numbers.spec.js +17 -9
- package/build/cjs/src/utils/__tests__/orderTransactions/decreaseOrders.spec.js +2 -0
- package/build/cjs/src/utils/__tests__/orderTransactions/increaseOrders.spec.js +2 -0
- package/build/cjs/src/utils/__tests__/orderTransactions/swapOrders.spec.js +1 -0
- package/build/cjs/src/utils/__tests__/prices.spec.js +3 -2
- package/build/cjs/src/utils/__tests__/uiFeeReceiver.spec.js +9 -0
- package/build/cjs/src/utils/errors/transactionsErrors.js +2 -1
- package/build/cjs/src/utils/fees/executionFee.js +9 -1
- package/build/cjs/src/utils/numbers.js +52 -34
- package/build/cjs/src/utils/orderTransactions.js +3 -0
- package/build/cjs/src/utils/orders.js +8 -2
- package/build/cjs/src/utils/prices.js +1 -1
- package/build/cjs/src/utils/swap/swapValues.js +40 -0
- package/build/cjs/src/utils/tokens.js +10 -1
- package/build/cjs/src/utils/twap/uiFeeReceiver.js +26 -6
- package/build/esm/src/abis/StBTC.json +1225 -0
- package/build/esm/src/abis/SyntheticsReader.json +480 -19
- package/build/esm/src/configs/__tests__/tokens.spec.js +2 -1
- package/build/esm/src/configs/chains.js +56 -5
- package/build/esm/src/configs/contracts.js +65 -69
- package/build/esm/src/configs/dataStore.js +1 -0
- package/build/esm/src/configs/express.js +4 -3
- package/build/esm/src/configs/markets.js +38 -1
- package/build/esm/src/configs/oracleKeeper.js +2 -1
- package/build/esm/src/configs/tokens.js +131 -2
- package/build/esm/src/modules/orders/helpers.js +12 -13
- package/build/esm/src/modules/positions/positions.js +9 -0
- package/build/esm/src/prebuilt/hashedKinkModelMarketRatesKeys.json +42 -0
- package/build/esm/src/prebuilt/hashedMarketConfigKeys.json +257 -0
- package/build/esm/src/prebuilt/hashedMarketValuesKeys.json +77 -0
- package/build/esm/src/types/subsquid.js +102 -0
- package/build/esm/src/utils/__tests__/numbers.spec.js +18 -10
- package/build/esm/src/utils/__tests__/orderTransactions/decreaseOrders.spec.js +2 -0
- package/build/esm/src/utils/__tests__/orderTransactions/increaseOrders.spec.js +2 -0
- package/build/esm/src/utils/__tests__/orderTransactions/swapOrders.spec.js +1 -0
- package/build/esm/src/utils/__tests__/prices.spec.js +3 -2
- package/build/esm/src/utils/__tests__/uiFeeReceiver.spec.js +9 -0
- package/build/esm/src/utils/errors/transactionsErrors.js +2 -1
- package/build/esm/src/utils/fees/executionFee.js +11 -3
- package/build/esm/src/utils/numbers.js +50 -31
- package/build/esm/src/utils/orderTransactions.js +3 -0
- package/build/esm/src/utils/orders.js +8 -2
- package/build/esm/src/utils/prices.js +1 -1
- package/build/esm/src/utils/swap/swapValues.js +41 -1
- package/build/esm/src/utils/tokens.js +7 -0
- package/build/esm/src/utils/twap/uiFeeReceiver.js +24 -5
- package/build/types/src/configs/chains.d.ts +20 -37
- package/build/types/src/configs/contracts.d.ts +60 -63
- package/build/types/src/configs/dataStore.d.ts +1 -0
- package/build/types/src/configs/markets.d.ts +2 -1
- package/build/types/src/configs/oracleKeeper.d.ts +1 -1
- 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/orders/helpers.d.ts +10 -2
- package/build/types/src/modules/orders/orders.d.ts +10 -1
- package/build/types/src/types/orders.d.ts +1 -1
- package/build/types/src/types/sdk.d.ts +2 -1
- package/build/types/src/types/subsquid.d.ts +388 -0
- package/build/types/src/types/tokens.d.ts +1 -0
- package/build/types/src/types/trade.d.ts +1 -0
- package/build/types/src/utils/multicall.d.ts +1 -1
- package/build/types/src/utils/numbers.d.ts +8 -3
- package/build/types/src/utils/orderTransactions.d.ts +2 -3
- package/build/types/src/utils/prices.d.ts +1 -1
- package/build/types/src/utils/tokens.d.ts +2 -0
- package/build/types/src/utils/twap/uiFeeReceiver.d.ts +13 -0
- package/package.json +1 -1
|
@@ -63,7 +63,8 @@ const prices_1 = require("../prices");
|
|
|
63
63
|
const result = (0, prices_1.getOrderThresholdType)(orders_1.OrderType.StopIncrease, false);
|
|
64
64
|
(0, vitest_1.expect)(result).toBe(trade_1.TriggerThresholdType.Below);
|
|
65
65
|
});
|
|
66
|
-
(0, vitest_1.it)("
|
|
67
|
-
|
|
66
|
+
(0, vitest_1.it)("returns undefined for invalid order type", () => {
|
|
67
|
+
const result = (0, prices_1.getOrderThresholdType)("SomeInvalidType", true);
|
|
68
|
+
(0, vitest_1.expect)(result).toBeUndefined();
|
|
68
69
|
});
|
|
69
70
|
});
|
|
@@ -13,12 +13,21 @@ const uiFeeReceiver_1 = require("../twap/uiFeeReceiver");
|
|
|
13
13
|
(0, vitest_1.it)("should return undefined if twapId inside is 0000", () => {
|
|
14
14
|
(0, vitest_1.expect)((0, uiFeeReceiver_1.decodeTwapUiFeeReceiver)("0xff00000000000000000000000000000000000001")).toBeUndefined();
|
|
15
15
|
});
|
|
16
|
+
(0, vitest_1.it)("should correctly decode isExpress", () => {
|
|
17
|
+
(0, vitest_1.expect)((0, uiFeeReceiver_1.decodeTwapUiFeeReceiver)("0xff0000000000000000000000000000010a123401")).toEqual({
|
|
18
|
+
isExpress: true,
|
|
19
|
+
twapId: "1234",
|
|
20
|
+
numberOfParts: 10,
|
|
21
|
+
});
|
|
22
|
+
});
|
|
16
23
|
(0, vitest_1.it)("should return the twapId and numberOfParts if the uiFeeReceiver is valid twap", () => {
|
|
17
24
|
(0, vitest_1.expect)((0, uiFeeReceiver_1.decodeTwapUiFeeReceiver)("0xff0000000000000000000000000000000a123401")).toEqual({
|
|
25
|
+
isExpress: false,
|
|
18
26
|
twapId: "1234",
|
|
19
27
|
numberOfParts: 10,
|
|
20
28
|
});
|
|
21
29
|
(0, vitest_1.expect)((0, uiFeeReceiver_1.decodeTwapUiFeeReceiver)("0xff000000000000000000000000000000153a4f01")).toEqual({
|
|
30
|
+
isExpress: false,
|
|
22
31
|
twapId: "3a4f",
|
|
23
32
|
numberOfParts: 21,
|
|
24
33
|
});
|
|
@@ -19,9 +19,10 @@ const TX_ERROR_PATTERNS = {
|
|
|
19
19
|
[TxErrorType.UserDenied]: [
|
|
20
20
|
{ msg: "User denied transaction signature" },
|
|
21
21
|
{ msg: "User rejected" },
|
|
22
|
+
{ msg: "User cancelled" },
|
|
22
23
|
{ msg: "user rejected action" },
|
|
23
24
|
{ msg: "ethers-user-denied" },
|
|
24
|
-
{ msg: "
|
|
25
|
+
{ msg: "Action cancelled by user" },
|
|
25
26
|
{ msg: "Signing aborted by user" },
|
|
26
27
|
],
|
|
27
28
|
[TxErrorType.Slippage]: [
|
|
@@ -5,6 +5,7 @@ const chains_1 = require("../../configs/chains");
|
|
|
5
5
|
const factors_1 = require("../../configs/factors");
|
|
6
6
|
const tokens_1 = require("../../configs/tokens");
|
|
7
7
|
const orders_1 = require("../../types/orders");
|
|
8
|
+
const bigmath_1 = require("../bigmath");
|
|
8
9
|
const numbers_1 = require("../numbers");
|
|
9
10
|
const tokens_2 = require("../tokens");
|
|
10
11
|
function getExecutionFee(chainId, gasLimits,
|
|
@@ -19,7 +20,14 @@ tokensData, estimatedGasLimit, gasPrice, oraclePriceCount, numberOfParts) {
|
|
|
19
20
|
const multiplierFactor = gasLimits.estimatedFeeMultiplierFactor;
|
|
20
21
|
const gasLimit = baseGasLimit + (0, numbers_1.applyFactor)(estimatedGasLimit, multiplierFactor);
|
|
21
22
|
// #endregion
|
|
22
|
-
|
|
23
|
+
// avoid botanix gas spikes when chain is not actively used
|
|
24
|
+
const minGasCostUsd = chains_1.MIN_EXECUTION_FEE_USD[chainId];
|
|
25
|
+
const minGasCost = (0, tokens_2.convertToTokenAmount)(minGasCostUsd, nativeToken.decimals, nativeToken.prices.minPrice);
|
|
26
|
+
let feeTokenAmountPerExecution = gasLimit * gasPrice;
|
|
27
|
+
if (minGasCost) {
|
|
28
|
+
feeTokenAmountPerExecution = bigmath_1.bigMath.max(feeTokenAmountPerExecution, minGasCost);
|
|
29
|
+
}
|
|
30
|
+
const feeTokenAmount = feeTokenAmountPerExecution * BigInt(numberOfParts ?? 1);
|
|
23
31
|
const feeUsd = (0, tokens_2.convertToUsd)(feeTokenAmount, nativeToken.decimals, nativeToken.prices.minPrice);
|
|
24
32
|
const isFeeHigh = feeUsd > (0, numbers_1.expandDecimals)((0, chains_1.getHighExecutionFee)(chainId), factors_1.USD_DECIMALS);
|
|
25
33
|
const isFeeVeryHigh = feeUsd > (0, numbers_1.expandDecimals)((0, chains_1.getExcessiveExecutionFee)(chainId), factors_1.USD_DECIMALS);
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.roundToOrder = exports.roundToTwoDecimals = exports.roundUpDivision = exports.parseValue = exports.bigNumberify = 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.
|
|
4
|
-
exports.absDiffBps = exports.clamp = exports.calculateDisplayDecimals = exports.deserializeBigIntsInObject = exports.serializeBigIntsInObject = exports.removeTrailingZeros = exports.maxbigint = exports.minBigNumber =
|
|
3
|
+
exports.roundBigIntToDecimals = exports.roundToOrder = exports.roundToTwoDecimals = exports.roundUpDivision = exports.parseValue = exports.bigNumberify = 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.formatUsd = exports.adjustForDecimals = exports.bigintToNumber = exports.trimZeroDecimals = exports.numberToBigint = exports.applyFactor = exports.roundUpMagnitudeDivision = exports.getBasisPoints = exports.basisPointsToFloat = exports.expandDecimals = exports.TRIGGER_PREFIX_BELOW = exports.TRIGGER_PREFIX_ABOVE = exports.PERCENT_PRECISION_DECIMALS = exports.MaxUint256 = exports.BN_NEGATIVE_ONE = exports.BN_ONE = exports.BN_ZERO = 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 = void 0;
|
|
5
5
|
const viem_1 = require("viem");
|
|
6
6
|
const bigmath_1 = require("./bigmath");
|
|
7
7
|
exports.USD_DECIMALS = 30;
|
|
@@ -143,7 +143,8 @@ function formatPercentage(percentage, opts = {}) {
|
|
|
143
143
|
}
|
|
144
144
|
exports.formatPercentage = formatPercentage;
|
|
145
145
|
function formatTokenAmount(amount, tokenDecimals, symbol, opts = {}) {
|
|
146
|
-
const {
|
|
146
|
+
const { showAllSignificant = false, fallbackToZero = false, useCommas = false, minThreshold = "0", maxThreshold, } = opts;
|
|
147
|
+
const displayDecimals = opts.displayDecimals ?? (opts.isStable ? 2 : 4);
|
|
147
148
|
const symbolStr = symbol ? ` ${symbol}` : "";
|
|
148
149
|
if (typeof amount !== "bigint" || !tokenDecimals) {
|
|
149
150
|
if (fallbackToZero) {
|
|
@@ -163,7 +164,7 @@ function formatTokenAmount(amount, tokenDecimals, symbol, opts = {}) {
|
|
|
163
164
|
else {
|
|
164
165
|
const exceedingInfo = getLimitedDisplay(amount, tokenDecimals, { maxThreshold, minThreshold });
|
|
165
166
|
const symbol = exceedingInfo.symbol ? `${exceedingInfo.symbol} ` : "";
|
|
166
|
-
amountStr = `${symbol}${sign}${(0, exports.formatAmount)(exceedingInfo.value, tokenDecimals, displayDecimals, useCommas)}`;
|
|
167
|
+
amountStr = `${symbol}${sign}${(0, exports.formatAmount)(exceedingInfo.value, tokenDecimals, displayDecimals, useCommas, undefined)}`;
|
|
167
168
|
}
|
|
168
169
|
return `${amountStr}${symbolStr}`;
|
|
169
170
|
}
|
|
@@ -245,14 +246,20 @@ function formatAmountHuman(amount, tokenDecimals, showDollar = false, displayDec
|
|
|
245
246
|
return `${isNegative ? "-" : ""}${sign}${absN.toFixed(displayDecimals)}`;
|
|
246
247
|
}
|
|
247
248
|
exports.formatAmountHuman = formatAmountHuman;
|
|
248
|
-
function formatBalanceAmount(amount, tokenDecimals, tokenSymbol, showZero = false, toExponential = true) {
|
|
249
|
+
function formatBalanceAmount(amount, tokenDecimals, tokenSymbol, { showZero = false, toExponential = true, isStable = false, } = {}) {
|
|
249
250
|
if (amount === undefined)
|
|
250
251
|
return "-";
|
|
251
252
|
if (amount === 0n) {
|
|
252
253
|
if (showZero === true) {
|
|
253
254
|
if (tokenSymbol) {
|
|
255
|
+
if (isStable) {
|
|
256
|
+
return `0.00 ${tokenSymbol}`;
|
|
257
|
+
}
|
|
254
258
|
return `0.0000 ${tokenSymbol}`;
|
|
255
259
|
}
|
|
260
|
+
if (isStable) {
|
|
261
|
+
return "0.00";
|
|
262
|
+
}
|
|
256
263
|
return "0.0000";
|
|
257
264
|
}
|
|
258
265
|
return "-";
|
|
@@ -260,14 +267,15 @@ function formatBalanceAmount(amount, tokenDecimals, tokenSymbol, showZero = fals
|
|
|
260
267
|
const absAmount = bigmath_1.bigMath.abs(amount);
|
|
261
268
|
const absAmountFloat = bigintToNumber(absAmount, tokenDecimals);
|
|
262
269
|
let value = "";
|
|
270
|
+
const baseDecimals = isStable ? 2 : 4;
|
|
263
271
|
if (absAmountFloat >= 1)
|
|
264
|
-
value = (0, exports.formatAmount)(amount, tokenDecimals,
|
|
272
|
+
value = (0, exports.formatAmount)(amount, tokenDecimals, baseDecimals, true);
|
|
265
273
|
else if (absAmountFloat >= 0.1)
|
|
266
|
-
value = (0, exports.formatAmount)(amount, tokenDecimals,
|
|
274
|
+
value = (0, exports.formatAmount)(amount, tokenDecimals, baseDecimals + 1, true);
|
|
267
275
|
else if (absAmountFloat >= 0.01)
|
|
268
|
-
value = (0, exports.formatAmount)(amount, tokenDecimals,
|
|
276
|
+
value = (0, exports.formatAmount)(amount, tokenDecimals, baseDecimals + 2, true);
|
|
269
277
|
else if (absAmountFloat >= 0.001)
|
|
270
|
-
value = (0, exports.formatAmount)(amount, tokenDecimals,
|
|
278
|
+
value = (0, exports.formatAmount)(amount, tokenDecimals, baseDecimals + 3, true);
|
|
271
279
|
else if (absAmountFloat >= 1e-8)
|
|
272
280
|
value = (0, exports.formatAmount)(amount, tokenDecimals, 8, true);
|
|
273
281
|
else {
|
|
@@ -285,16 +293,6 @@ function formatBalanceAmount(amount, tokenDecimals, tokenSymbol, showZero = fals
|
|
|
285
293
|
return value;
|
|
286
294
|
}
|
|
287
295
|
exports.formatBalanceAmount = formatBalanceAmount;
|
|
288
|
-
function formatBalanceAmountWithUsd(amount, amountUsd, tokenDecimals, tokenSymbol, showZero = false) {
|
|
289
|
-
if (showZero === false && amount === 0n) {
|
|
290
|
-
return "-";
|
|
291
|
-
}
|
|
292
|
-
const value = formatBalanceAmount(amount, tokenDecimals, tokenSymbol, showZero);
|
|
293
|
-
const usd = formatUsd(amountUsd);
|
|
294
|
-
// Regular space
|
|
295
|
-
return `${value} (${usd})`;
|
|
296
|
-
}
|
|
297
|
-
exports.formatBalanceAmountWithUsd = formatBalanceAmountWithUsd;
|
|
298
296
|
function formatFactor(factor) {
|
|
299
297
|
if (factor == 0n) {
|
|
300
298
|
return "0";
|
|
@@ -589,26 +587,46 @@ function deserializeBigIntsInObject(obj) {
|
|
|
589
587
|
return result;
|
|
590
588
|
}
|
|
591
589
|
exports.deserializeBigIntsInObject = deserializeBigIntsInObject;
|
|
592
|
-
function calculateDisplayDecimals(price, decimals = exports.USD_DECIMALS, visualMultiplier = 1) {
|
|
590
|
+
function calculateDisplayDecimals(price, decimals = exports.USD_DECIMALS, visualMultiplier = 1, isStable = false) {
|
|
593
591
|
if (price === undefined || price === 0n)
|
|
594
592
|
return 2;
|
|
595
593
|
const priceNumber = bigintToNumber(bigmath_1.bigMath.abs(price) * BigInt(visualMultiplier), decimals);
|
|
596
594
|
if (isNaN(priceNumber))
|
|
597
595
|
return 2;
|
|
598
|
-
if (
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
596
|
+
if (isStable) {
|
|
597
|
+
if (priceNumber >= 0.1)
|
|
598
|
+
return 2;
|
|
599
|
+
if (priceNumber >= 0.01)
|
|
600
|
+
return 3;
|
|
601
|
+
if (priceNumber >= 0.001)
|
|
602
|
+
return 4;
|
|
603
|
+
if (priceNumber >= 0.0001)
|
|
604
|
+
return 5;
|
|
605
|
+
if (priceNumber >= 0.00001)
|
|
606
|
+
return 6;
|
|
607
|
+
if (priceNumber >= 0.000001)
|
|
608
|
+
return 7;
|
|
609
|
+
if (priceNumber >= 0.0000001)
|
|
610
|
+
return 8;
|
|
611
|
+
if (priceNumber >= 0.00000001)
|
|
612
|
+
return 9;
|
|
613
|
+
}
|
|
614
|
+
else {
|
|
615
|
+
if (priceNumber >= 1000)
|
|
616
|
+
return 2;
|
|
617
|
+
if (priceNumber >= 100)
|
|
618
|
+
return 3;
|
|
619
|
+
if (priceNumber >= 1)
|
|
620
|
+
return 4;
|
|
621
|
+
if (priceNumber >= 0.1)
|
|
622
|
+
return 5;
|
|
623
|
+
if (priceNumber >= 0.01)
|
|
624
|
+
return 6;
|
|
625
|
+
if (priceNumber >= 0.0001)
|
|
626
|
+
return 7;
|
|
627
|
+
if (priceNumber >= 0.00001)
|
|
628
|
+
return 8;
|
|
629
|
+
}
|
|
612
630
|
return 9;
|
|
613
631
|
}
|
|
614
632
|
exports.calculateDisplayDecimals = calculateDisplayDecimals;
|
|
@@ -180,6 +180,9 @@ function buildTwapOrdersPayloads(p, twapParams) {
|
|
|
180
180
|
referralCode: params.referralCode,
|
|
181
181
|
autoCancel: params.autoCancel,
|
|
182
182
|
allowedSlippage: 0,
|
|
183
|
+
...(params.expectedOutputAmount !== undefined && {
|
|
184
|
+
expectedOutputAmount: params.expectedOutputAmount / BigInt(twapParams.numberOfParts),
|
|
185
|
+
}),
|
|
183
186
|
payTokenAmount: params.payTokenAmount / BigInt(twapParams.numberOfParts),
|
|
184
187
|
executionFeeAmount: params.executionFeeAmount / BigInt(twapParams.numberOfParts),
|
|
185
188
|
validFromTime: (0, twap_1.getTwapValidFromTime)(twapParams.duration, twapParams.numberOfParts, i),
|
|
@@ -179,7 +179,10 @@ function getOrderInfo(p) {
|
|
|
179
179
|
shouldApplyPriceImpact: true,
|
|
180
180
|
isAtomicSwap: false,
|
|
181
181
|
});
|
|
182
|
-
|
|
182
|
+
let triggerThresholdType;
|
|
183
|
+
if (!isMarketOrderType(order.orderType)) {
|
|
184
|
+
triggerThresholdType = (0, prices_1.getOrderThresholdType)(order.orderType, order.isLong);
|
|
185
|
+
}
|
|
183
186
|
const orderInfo = {
|
|
184
187
|
...order,
|
|
185
188
|
swapPathStats,
|
|
@@ -198,7 +201,10 @@ function getOrderInfo(p) {
|
|
|
198
201
|
}
|
|
199
202
|
exports.getOrderInfo = getOrderInfo;
|
|
200
203
|
function isVisibleOrder(orderType) {
|
|
201
|
-
return isLimitOrderType(orderType) ||
|
|
204
|
+
return (isLimitOrderType(orderType) ||
|
|
205
|
+
isTriggerDecreaseOrderType(orderType) ||
|
|
206
|
+
isLimitSwapOrderType(orderType) ||
|
|
207
|
+
isMarketOrderType(orderType));
|
|
202
208
|
}
|
|
203
209
|
exports.isVisibleOrder = isVisibleOrder;
|
|
204
210
|
function isOrderForPosition(order, positionKey) {
|
|
@@ -38,7 +38,7 @@ function getOrderThresholdType(orderType, isLong) {
|
|
|
38
38
|
if (orderType === orders_1.OrderType.StopLossDecrease) {
|
|
39
39
|
return isLong ? trade_1.TriggerThresholdType.Below : trade_1.TriggerThresholdType.Above;
|
|
40
40
|
}
|
|
41
|
-
|
|
41
|
+
return undefined;
|
|
42
42
|
}
|
|
43
43
|
exports.getOrderThresholdType = getOrderThresholdType;
|
|
44
44
|
function getAcceptablePriceInfo(p) {
|
|
@@ -42,6 +42,9 @@ function getSwapAmountsByFromValue(p) {
|
|
|
42
42
|
swapPathStats: undefined,
|
|
43
43
|
};
|
|
44
44
|
}
|
|
45
|
+
if ((0, tokens_1.getIsStake)(tokenIn, tokenOut) || (0, tokens_1.getIsUnstake)(tokenIn, tokenOut)) {
|
|
46
|
+
return getPlainSwapAmountsByFromToken(tokenIn, tokenOut, amountIn);
|
|
47
|
+
}
|
|
45
48
|
const swapPathStats = findSwapPath(defaultAmounts.usdIn, { order: swapOptimizationOrder });
|
|
46
49
|
const totalSwapVolume = (0, fees_1.getTotalSwapVolumeFromSwapStats)(swapPathStats?.swapSteps);
|
|
47
50
|
const swapUiFeeUsd = (0, numbers_1.applyFactor)(totalSwapVolume, uiFeeFactor);
|
|
@@ -123,6 +126,9 @@ function getSwapAmountsByToValue(p) {
|
|
|
123
126
|
swapPathStats: undefined,
|
|
124
127
|
};
|
|
125
128
|
}
|
|
129
|
+
if ((0, tokens_1.getIsStake)(tokenIn, tokenOut) || (0, tokens_1.getIsUnstake)(tokenIn, tokenOut)) {
|
|
130
|
+
return getPlainSwapAmountsByToToken(tokenIn, tokenOut, amountOut);
|
|
131
|
+
}
|
|
126
132
|
const baseUsdIn = usdOut;
|
|
127
133
|
const swapPathStats = findSwapPath(baseUsdIn, { order: swapOptimizationOrder });
|
|
128
134
|
if (!swapPathStats) {
|
|
@@ -188,3 +194,37 @@ function getSwapPathComparator(order) {
|
|
|
188
194
|
};
|
|
189
195
|
}
|
|
190
196
|
exports.getSwapPathComparator = getSwapPathComparator;
|
|
197
|
+
function getPlainSwapAmountsByFromToken(tokenIn, tokenOut, amountIn) {
|
|
198
|
+
const usdIn = (0, tokens_1.convertToUsd)(amountIn, tokenIn.decimals, tokenIn.prices.minPrice);
|
|
199
|
+
const usdOut = usdIn;
|
|
200
|
+
const amountOut = (0, tokens_1.convertToTokenAmount)(usdOut, tokenOut.decimals, tokenOut.prices.maxPrice);
|
|
201
|
+
const priceIn = tokenIn.prices.minPrice;
|
|
202
|
+
const priceOut = tokenOut.prices.maxPrice;
|
|
203
|
+
return {
|
|
204
|
+
amountIn,
|
|
205
|
+
usdIn,
|
|
206
|
+
amountOut,
|
|
207
|
+
usdOut,
|
|
208
|
+
minOutputAmount: amountOut,
|
|
209
|
+
priceIn,
|
|
210
|
+
priceOut,
|
|
211
|
+
swapPathStats: undefined,
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
function getPlainSwapAmountsByToToken(tokenIn, tokenOut, amountOut) {
|
|
215
|
+
const priceIn = tokenIn.prices.minPrice;
|
|
216
|
+
const priceOut = tokenOut.prices.maxPrice;
|
|
217
|
+
const usdOut = (0, tokens_1.convertToUsd)(amountOut, tokenOut.decimals, priceOut);
|
|
218
|
+
const usdIn = usdOut;
|
|
219
|
+
const amountIn = (0, tokens_1.convertToTokenAmount)(usdIn, tokenIn.decimals, priceIn);
|
|
220
|
+
return {
|
|
221
|
+
amountIn,
|
|
222
|
+
usdIn,
|
|
223
|
+
amountOut,
|
|
224
|
+
usdOut,
|
|
225
|
+
minOutputAmount: amountOut,
|
|
226
|
+
priceIn,
|
|
227
|
+
priceOut,
|
|
228
|
+
swapPathStats: undefined,
|
|
229
|
+
};
|
|
230
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getTokensRatioByPrice = exports.getIsUnwrap = exports.getIsWrap = exports.getAmountByRatio = exports.getTokensRatioByMinOutputAmountAndTriggerPrice = exports.getTokensRatioByAmounts = exports.getTokenData = exports.getIsEquivalentTokens = exports.getMidPrice = exports.convertBetweenTokens = exports.convertToUsd = exports.convertToTokenAmount = exports.convertToContractTokenPrices = exports.convertToContractPrice = exports.parseContractPrice = void 0;
|
|
3
|
+
exports.getTokensRatioByPrice = exports.getIsUnstake = exports.getIsStake = exports.getIsUnwrap = exports.getIsWrap = exports.getAmountByRatio = exports.getTokensRatioByMinOutputAmountAndTriggerPrice = exports.getTokensRatioByAmounts = exports.getTokenData = exports.getIsEquivalentTokens = exports.getMidPrice = exports.convertBetweenTokens = exports.convertToUsd = exports.convertToTokenAmount = exports.convertToContractTokenPrices = exports.convertToContractPrice = exports.parseContractPrice = void 0;
|
|
4
4
|
const factors_1 = require("../configs/factors");
|
|
5
5
|
const tokens_1 = require("../configs/tokens");
|
|
6
6
|
const bigmath_1 = require("./bigmath");
|
|
@@ -149,6 +149,15 @@ function getIsUnwrap(token1, token2) {
|
|
|
149
149
|
return token1.isWrapped && token2.isNative;
|
|
150
150
|
}
|
|
151
151
|
exports.getIsUnwrap = getIsUnwrap;
|
|
152
|
+
function getIsStake(token1, token2) {
|
|
153
|
+
return (token1.isWrapped || token1.isNative) && token2.isStaking;
|
|
154
|
+
}
|
|
155
|
+
exports.getIsStake = getIsStake;
|
|
156
|
+
function getIsUnstake(token1, token2) {
|
|
157
|
+
// can't unstake straight to native token
|
|
158
|
+
return token1.isStaking && token2.isWrapped;
|
|
159
|
+
}
|
|
160
|
+
exports.getIsUnstake = getIsUnstake;
|
|
152
161
|
function getTokensRatioByPrice(p) {
|
|
153
162
|
const { fromToken, toToken, fromPrice, toPrice } = p;
|
|
154
163
|
const [largestToken, smallestToken, largestPrice, smallestPrice] = fromPrice > toPrice ? [fromToken, toToken, fromPrice, toPrice] : [toToken, fromToken, toPrice, fromPrice];
|
|
@@ -1,9 +1,20 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isValidTwapUiFeeReceiver = exports.decodeTwapUiFeeReceiver = exports.createTwapUiFeeReceiver = exports.generateTwapId = void 0;
|
|
3
|
+
exports.setUiFeeReceiverIsExpress = exports.isValidTwapUiFeeReceiver = exports.decodeTwapUiFeeReceiver = exports.createTwapUiFeeReceiver = exports.generateTwapId = void 0;
|
|
4
4
|
const viem_1 = require("viem");
|
|
5
5
|
const VERSION = "01";
|
|
6
|
-
const PREFIX = "
|
|
6
|
+
const PREFIX = "0xff0000";
|
|
7
|
+
/**
|
|
8
|
+
* Ui fee receiver structure:
|
|
9
|
+
* 0..3 bytes (0..7 chars) - PREFIX
|
|
10
|
+
* 4..16 bytes (8..27 chars) - 12 bytes buffer
|
|
11
|
+
* 17 byte (27..29 chars) - isExpress flag
|
|
12
|
+
* 18 byte (30..32 chars) - numberOfParts (hex encoded)
|
|
13
|
+
* 19..20 bytes (34..38 chars) - twapId
|
|
14
|
+
* 20 byte (38..40 chars) - VERSION
|
|
15
|
+
*
|
|
16
|
+
* Total: 20 bytes (40 hex characters)
|
|
17
|
+
*/
|
|
7
18
|
function generateTwapId() {
|
|
8
19
|
return Math.floor(Math.random() * 256 * 256)
|
|
9
20
|
.toString(16)
|
|
@@ -13,19 +24,28 @@ exports.generateTwapId = generateTwapId;
|
|
|
13
24
|
function createTwapUiFeeReceiver({ numberOfParts }) {
|
|
14
25
|
const twapId = generateTwapId();
|
|
15
26
|
const numberOfPartsInHex = numberOfParts.toString(16).padStart(2, "0");
|
|
16
|
-
|
|
27
|
+
const isExpressHex = "00";
|
|
28
|
+
const buffer = "00".repeat(12);
|
|
29
|
+
return `${PREFIX}${buffer}${isExpressHex}${numberOfPartsInHex}${twapId}${VERSION}`;
|
|
17
30
|
}
|
|
18
31
|
exports.createTwapUiFeeReceiver = createTwapUiFeeReceiver;
|
|
19
32
|
function decodeTwapUiFeeReceiver(address) {
|
|
20
|
-
const
|
|
33
|
+
const byteString = address.slice(2);
|
|
34
|
+
const twapId = byteString.slice(34, 38);
|
|
35
|
+
const isExpress = byteString.slice(30, 32) === "01";
|
|
21
36
|
if (!isValidTwapUiFeeReceiver(address) || twapId === "0000") {
|
|
22
37
|
return;
|
|
23
38
|
}
|
|
24
|
-
const numberOfParts = parseInt(
|
|
25
|
-
return { twapId, numberOfParts };
|
|
39
|
+
const numberOfParts = parseInt(byteString.slice(32, 34), 16);
|
|
40
|
+
return { twapId, numberOfParts, isExpress };
|
|
26
41
|
}
|
|
27
42
|
exports.decodeTwapUiFeeReceiver = decodeTwapUiFeeReceiver;
|
|
28
43
|
function isValidTwapUiFeeReceiver(address) {
|
|
29
44
|
return (0, viem_1.isAddress)(address) && address.toLowerCase().startsWith(PREFIX.toLowerCase());
|
|
30
45
|
}
|
|
31
46
|
exports.isValidTwapUiFeeReceiver = isValidTwapUiFeeReceiver;
|
|
47
|
+
function setUiFeeReceiverIsExpress(uiFeeReceiver, isExpress) {
|
|
48
|
+
const isExpressInHex = isExpress ? "01" : "00";
|
|
49
|
+
return `${uiFeeReceiver.slice(0, 27)}${isExpressInHex}${uiFeeReceiver.slice(29)}`;
|
|
50
|
+
}
|
|
51
|
+
exports.setUiFeeReceiverIsExpress = setUiFeeReceiverIsExpress;
|