@gmx-io/sdk 1.1.2 → 1.2.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 +439 -0
- package/build/cjs/src/abis/StBTC.json +1225 -0
- package/build/cjs/src/abis/SyntheticsReader.json +480 -19
- package/build/cjs/src/abis/index.js +4 -0
- package/build/cjs/src/configs/__tests__/markets.spec.js +1 -1
- package/build/cjs/src/configs/__tests__/tokens.spec.js +2 -1
- package/build/cjs/src/configs/batch.js +12 -0
- package/build/cjs/src/configs/chains.js +57 -6
- package/build/cjs/src/configs/contracts.js +66 -68
- package/build/cjs/src/configs/dataStore.js +14 -3
- package/build/cjs/src/configs/express.js +4 -2
- package/build/cjs/src/configs/markets.js +72 -0
- package/build/cjs/src/configs/oracleKeeper.js +19 -34
- package/build/cjs/src/configs/tokens.js +185 -1
- package/build/cjs/src/index.js +3 -3
- package/build/cjs/src/modules/orders/helpers.js +21 -13
- package/build/cjs/src/modules/orders/helpers.spec.js +1 -1
- package/build/cjs/src/modules/orders/orders.js +3 -3
- package/build/cjs/src/modules/positions/positions.js +9 -0
- package/build/cjs/src/modules/utils/utils.js +3 -3
- package/build/cjs/src/prebuilt/hashedKinkModelMarketRatesKeys.json +82 -0
- package/build/cjs/src/prebuilt/hashedMarketConfigKeys.json +512 -0
- package/build/cjs/src/prebuilt/hashedMarketValuesKeys.json +152 -0
- package/build/cjs/src/types/subsquid.js +193 -1
- package/build/cjs/src/types/swapStrategy.js +2 -0
- package/build/cjs/src/types/trade.js +1 -0
- 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/exchangeRouterMulticall.spec.js +2 -0
- package/build/cjs/src/utils/__tests__/orderTransactions/increaseOrders.spec.js +4 -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 +19 -0
- package/build/cjs/src/utils/errors/transactionsErrors.js +9 -2
- 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 -1
- package/build/cjs/src/utils/orders.js +8 -2
- package/build/cjs/src/utils/prices.js +1 -1
- package/build/cjs/src/utils/swap/botanixStaking.js +90 -0
- package/build/cjs/src/utils/swap/buildSwapStrategy.js +224 -0
- package/build/cjs/src/utils/swap/externalSwapPath.js +41 -0
- package/build/cjs/src/utils/swap/externalSwapQuoteByPath.js +27 -0
- package/build/cjs/src/utils/swap/swapValues.js +288 -9
- package/build/cjs/src/utils/tokens.js +10 -1
- package/build/cjs/src/utils/trade/amounts.js +89 -41
- package/build/cjs/src/utils/trade/trade.js +2 -2
- package/build/cjs/src/utils/twap/uiFeeReceiver.js +26 -6
- package/build/esm/src/abis/ClaimHandler.json +439 -0
- package/build/esm/src/abis/StBTC.json +1225 -0
- package/build/esm/src/abis/SyntheticsReader.json +480 -19
- package/build/esm/src/abis/index.js +4 -0
- package/build/esm/src/configs/__tests__/markets.spec.js +1 -1
- package/build/esm/src/configs/__tests__/tokens.spec.js +3 -2
- package/build/esm/src/configs/batch.js +13 -1
- package/build/esm/src/configs/chains.js +56 -5
- package/build/esm/src/configs/contracts.js +67 -69
- package/build/esm/src/configs/dataStore.js +9 -0
- package/build/esm/src/configs/express.js +5 -3
- package/build/esm/src/configs/markets.js +73 -1
- package/build/esm/src/configs/oracleKeeper.js +18 -29
- package/build/esm/src/configs/tokens.js +186 -2
- package/build/esm/src/index.js +3 -3
- package/build/esm/src/modules/orders/helpers.js +21 -13
- package/build/esm/src/modules/orders/helpers.spec.js +1 -1
- package/build/esm/src/modules/orders/orders.js +3 -3
- package/build/esm/src/modules/positions/positions.js +9 -0
- package/build/esm/src/modules/utils/utils.js +3 -3
- package/build/esm/src/prebuilt/hashedKinkModelMarketRatesKeys.json +82 -0
- package/build/esm/src/prebuilt/hashedMarketConfigKeys.json +512 -0
- package/build/esm/src/prebuilt/hashedMarketValuesKeys.json +152 -0
- package/build/esm/src/types/subsquid.js +192 -0
- package/build/esm/src/types/swapStrategy.js +1 -0
- package/build/esm/src/types/trade.js +1 -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/exchangeRouterMulticall.spec.js +2 -0
- package/build/esm/src/utils/__tests__/orderTransactions/increaseOrders.spec.js +4 -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 +20 -1
- package/build/esm/src/utils/errors/transactionsErrors.js +9 -2
- 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 -1
- package/build/esm/src/utils/orders.js +8 -2
- package/build/esm/src/utils/prices.js +1 -1
- package/build/esm/src/utils/swap/botanixStaking.js +83 -0
- package/build/esm/src/utils/swap/buildSwapStrategy.js +219 -0
- package/build/esm/src/utils/swap/externalSwapPath.js +37 -0
- package/build/esm/src/utils/swap/externalSwapQuoteByPath.js +23 -0
- package/build/esm/src/utils/swap/swapValues.js +287 -8
- package/build/esm/src/utils/tokens.js +7 -0
- package/build/esm/src/utils/trade/amounts.js +89 -41
- package/build/esm/src/utils/trade/trade.js +2 -2
- package/build/esm/src/utils/twap/uiFeeReceiver.js +24 -5
- package/build/types/src/abis/index.d.ts +1 -1
- package/build/types/src/configs/batch.d.ts +2 -1
- package/build/types/src/configs/chains.d.ts +20 -37
- package/build/types/src/configs/contracts.d.ts +62 -63
- package/build/types/src/configs/dataStore.d.ts +5 -0
- package/build/types/src/configs/express.d.ts +1 -0
- package/build/types/src/configs/markets.d.ts +2 -1
- package/build/types/src/configs/oracleKeeper.d.ts +2 -3
- package/build/types/src/configs/tokens.d.ts +1 -0
- package/build/types/src/index.d.ts +1 -1
- package/build/types/src/modules/base.d.ts +1 -1
- package/build/types/src/modules/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 +691 -0
- package/build/types/src/types/swapStrategy.d.ts +33 -0
- package/build/types/src/types/tokens.d.ts +6 -0
- package/build/types/src/types/trade.d.ts +19 -4
- 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/swap/botanixStaking.d.ts +11 -0
- package/build/types/src/utils/swap/buildSwapStrategy.d.ts +22 -0
- package/build/types/src/utils/swap/externalSwapPath.d.ts +13 -0
- package/build/types/src/utils/swap/externalSwapQuoteByPath.d.ts +6 -0
- package/build/types/src/utils/swap/swapValues.d.ts +10 -3
- package/build/types/src/utils/tokens.d.ts +2 -0
- package/build/types/src/utils/trade/amounts.d.ts +5 -2
- package/build/types/src/utils/twap/uiFeeReceiver.d.ts +13 -0
- package/package.json +1 -1
|
@@ -19,16 +19,23 @@ const TX_ERROR_PATTERNS = {
|
|
|
19
19
|
[TxErrorType.UserDenied]: [
|
|
20
20
|
{ msg: "User denied transaction signature" },
|
|
21
21
|
{ msg: "User rejected" },
|
|
22
|
+
{ msg: "User cancelled" },
|
|
23
|
+
{ msg: "Cancelled" },
|
|
24
|
+
{ msg: "Cancelled by user" },
|
|
22
25
|
{ msg: "user rejected action" },
|
|
23
26
|
{ msg: "ethers-user-denied" },
|
|
24
|
-
{ msg: "
|
|
27
|
+
{ msg: "Action cancelled by user" },
|
|
25
28
|
{ msg: "Signing aborted by user" },
|
|
26
29
|
],
|
|
27
30
|
[TxErrorType.Slippage]: [
|
|
28
31
|
{ msg: "Router: mark price lower than limit" },
|
|
29
32
|
{ msg: "Router: mark price higher than limit" },
|
|
30
33
|
],
|
|
31
|
-
[TxErrorType.NetworkChanged]: [
|
|
34
|
+
[TxErrorType.NetworkChanged]: [
|
|
35
|
+
{ msg: "network changed" },
|
|
36
|
+
{ msg: "Invalid network" },
|
|
37
|
+
{ msg: "chainId should be same" },
|
|
38
|
+
],
|
|
32
39
|
[TxErrorType.Expired]: [{ msg: "Request expired" }],
|
|
33
40
|
[TxErrorType.RpcError]: [
|
|
34
41
|
// @see https://eips.ethereum.org/EIPS/eip-1474#error-codes
|
|
@@ -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),
|
|
@@ -377,7 +380,6 @@ function buildTokenTransfersParamsForIncreaseOrSwap({ chainId, receiver, payToke
|
|
|
377
380
|
* */
|
|
378
381
|
initialCollateralTokenAddress = (0, tokens_1.convertTokenAddress)(chainId, externalSwapQuote.outTokenAddress, "wrapped");
|
|
379
382
|
initialCollateralDeltaAmount = 0n;
|
|
380
|
-
swapPath = [];
|
|
381
383
|
externalCalls = getExternalCallsPayload({
|
|
382
384
|
chainId,
|
|
383
385
|
account: receiver,
|
|
@@ -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) {
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.getBotanixStakingExternalSwapQuote = void 0;
|
|
7
|
+
const viem_1 = require("viem");
|
|
8
|
+
const StBTC_json_1 = __importDefault(require("../../abis/StBTC.json"));
|
|
9
|
+
const chains_1 = require("../../configs/chains");
|
|
10
|
+
const contracts_1 = require("../../configs/contracts");
|
|
11
|
+
const trade_1 = require("../../types/trade");
|
|
12
|
+
const bigmath_1 = require("../bigmath");
|
|
13
|
+
const numbers_1 = require("../numbers");
|
|
14
|
+
const externalSwapPath_1 = require("./externalSwapPath");
|
|
15
|
+
const tokens_1 = require("../tokens");
|
|
16
|
+
const COEF_REDUCER = (0, numbers_1.getBasisPoints)(1n, 10000n);
|
|
17
|
+
const getBotanixStakingExternalSwapQuote = ({ tokenInAddress, tokenOutAddress, amountIn, gasPrice, receiverAddress, tokensData, assetsPerShare, }) => {
|
|
18
|
+
const inTokenData = (0, tokens_1.getTokenData)(tokensData, tokenInAddress);
|
|
19
|
+
const outTokenData = (0, tokens_1.getTokenData)(tokensData, tokenOutAddress);
|
|
20
|
+
const assetsPerShareRate = (0, numbers_1.getBasisPoints)(assetsPerShare, 10n ** 18n) - COEF_REDUCER;
|
|
21
|
+
const sharesPerAssetRate = (0, numbers_1.getBasisPoints)(10n ** 18n, assetsPerShare) - COEF_REDUCER;
|
|
22
|
+
if (!inTokenData || !outTokenData) {
|
|
23
|
+
return undefined;
|
|
24
|
+
}
|
|
25
|
+
if (externalSwapPath_1.AVAILABLE_BOTANIX_DEPOSIT_PAIRS.some((pair) => pair.from === tokenInAddress && pair.to === tokenOutAddress)) {
|
|
26
|
+
const priceIn = (0, tokens_1.getMidPrice)(inTokenData.prices);
|
|
27
|
+
const priceOut = bigmath_1.bigMath.mulDiv(priceIn, sharesPerAssetRate, numbers_1.BASIS_POINTS_DIVISOR_BIGINT);
|
|
28
|
+
const usdIn = (0, tokens_1.convertToUsd)(amountIn, inTokenData.decimals, priceIn);
|
|
29
|
+
const amountOut = amountIn > 0n ? bigmath_1.bigMath.mulDiv(amountIn, sharesPerAssetRate, numbers_1.BASIS_POINTS_DIVISOR_BIGINT) - gasPrice : 0n;
|
|
30
|
+
const usdOut = amountOut > 0n ? (0, tokens_1.convertToUsd)(amountOut, outTokenData.decimals, priceOut) : 0n;
|
|
31
|
+
return {
|
|
32
|
+
aggregator: trade_1.ExternalSwapAggregator.BotanixStaking,
|
|
33
|
+
inTokenAddress: tokenInAddress,
|
|
34
|
+
outTokenAddress: tokenOutAddress,
|
|
35
|
+
receiver: receiverAddress,
|
|
36
|
+
amountIn,
|
|
37
|
+
amountOut,
|
|
38
|
+
usdIn: usdIn,
|
|
39
|
+
usdOut: usdOut,
|
|
40
|
+
priceIn,
|
|
41
|
+
priceOut,
|
|
42
|
+
feesUsd: gasPrice,
|
|
43
|
+
needSpenderApproval: true,
|
|
44
|
+
txnData: {
|
|
45
|
+
to: (0, contracts_1.getContract)(chains_1.BOTANIX, "StBTC"),
|
|
46
|
+
data: (0, viem_1.encodeFunctionData)({
|
|
47
|
+
abi: StBTC_json_1.default.abi,
|
|
48
|
+
functionName: "deposit",
|
|
49
|
+
args: [amountIn, receiverAddress],
|
|
50
|
+
}),
|
|
51
|
+
value: 0n,
|
|
52
|
+
estimatedGas: gasPrice,
|
|
53
|
+
estimatedExecutionFee: gasPrice,
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
if (externalSwapPath_1.AVAILABLE_BOTANIX_WITHDRAW_PAIRS.some((pair) => pair.from === tokenInAddress && pair.to === tokenOutAddress)) {
|
|
58
|
+
const priceIn = (0, tokens_1.getMidPrice)(inTokenData.prices);
|
|
59
|
+
const priceOut = bigmath_1.bigMath.mulDiv(priceIn, assetsPerShareRate, numbers_1.BASIS_POINTS_DIVISOR_BIGINT);
|
|
60
|
+
const usdIn = (0, tokens_1.convertToUsd)(amountIn, inTokenData.decimals, priceIn);
|
|
61
|
+
const amountOut = amountIn > 0n ? bigmath_1.bigMath.mulDiv(amountIn, assetsPerShareRate, numbers_1.BASIS_POINTS_DIVISOR_BIGINT) - gasPrice : 0n;
|
|
62
|
+
const usdOut = amountOut > 0n ? (0, tokens_1.convertToUsd)(amountOut, outTokenData.decimals, priceOut) : 0n;
|
|
63
|
+
return {
|
|
64
|
+
aggregator: trade_1.ExternalSwapAggregator.BotanixStaking,
|
|
65
|
+
inTokenAddress: tokenInAddress,
|
|
66
|
+
outTokenAddress: tokenOutAddress,
|
|
67
|
+
receiver: receiverAddress,
|
|
68
|
+
amountIn,
|
|
69
|
+
amountOut,
|
|
70
|
+
usdIn: usdIn,
|
|
71
|
+
usdOut: usdOut,
|
|
72
|
+
priceIn,
|
|
73
|
+
priceOut,
|
|
74
|
+
feesUsd: gasPrice,
|
|
75
|
+
needSpenderApproval: true,
|
|
76
|
+
txnData: {
|
|
77
|
+
to: (0, contracts_1.getContract)(chains_1.BOTANIX, "StBTC"),
|
|
78
|
+
data: (0, viem_1.encodeFunctionData)({
|
|
79
|
+
abi: StBTC_json_1.default.abi,
|
|
80
|
+
functionName: "withdraw",
|
|
81
|
+
args: [amountIn, receiverAddress, (0, contracts_1.getContract)(chains_1.BOTANIX, "ExternalHandler")],
|
|
82
|
+
}),
|
|
83
|
+
value: 0n,
|
|
84
|
+
estimatedGas: gasPrice,
|
|
85
|
+
estimatedExecutionFee: gasPrice,
|
|
86
|
+
},
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
exports.getBotanixStakingExternalSwapQuote = getBotanixStakingExternalSwapQuote;
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.buildReverseSwapStrategy = exports.buildSwapStrategy = void 0;
|
|
4
|
+
const bigmath_1 = require("../bigmath");
|
|
5
|
+
const tokens_1 = require("../tokens");
|
|
6
|
+
const externalSwapPath_1 = require("./externalSwapPath");
|
|
7
|
+
const externalSwapQuoteByPath_1 = require("./externalSwapQuoteByPath");
|
|
8
|
+
const swapPath_1 = require("./swapPath");
|
|
9
|
+
/*
|
|
10
|
+
Order/Priority of getting swap strategy:
|
|
11
|
+
1. Check if it needs a swap and return noSwap if tokens are equivalent, stake or unstake [noSwap]
|
|
12
|
+
2. Check if there is a swap path stats for the internal swap quote and return internalSwap if there is [internalSwap]
|
|
13
|
+
3. Check if there is a combined swap strategy and return combinedSwap if there is [combinedSwap]
|
|
14
|
+
4. Return defaultSwapStrategy (noSwap) if there is no other swap strategy [noSwap]
|
|
15
|
+
*/
|
|
16
|
+
function buildSwapStrategy({ amountIn, tokenIn, tokenOut, marketsInfoData, chainId, swapOptimizationOrder, externalSwapQuoteParams, }) {
|
|
17
|
+
const priceIn = tokenIn.prices.minPrice;
|
|
18
|
+
const usdIn = (0, tokens_1.convertToUsd)(amountIn, tokenIn.decimals, priceIn);
|
|
19
|
+
if (amountIn < 0n) {
|
|
20
|
+
amountIn = 0n;
|
|
21
|
+
}
|
|
22
|
+
const defaultSwapStrategy = {
|
|
23
|
+
type: "noSwap",
|
|
24
|
+
externalSwapQuote: undefined,
|
|
25
|
+
swapPathStats: undefined,
|
|
26
|
+
amountIn,
|
|
27
|
+
amountOut: (0, tokens_1.convertToTokenAmount)(usdIn, tokenOut.decimals, tokenOut.prices.maxPrice),
|
|
28
|
+
usdIn,
|
|
29
|
+
usdOut: usdIn,
|
|
30
|
+
priceIn,
|
|
31
|
+
priceOut: tokenOut.prices.maxPrice,
|
|
32
|
+
feesUsd: 0n,
|
|
33
|
+
};
|
|
34
|
+
if ((0, tokens_1.getIsEquivalentTokens)(tokenIn, tokenOut) || (0, tokens_1.getIsStake)(tokenIn, tokenOut) || (0, tokens_1.getIsUnstake)(tokenIn, tokenOut)) {
|
|
35
|
+
return defaultSwapStrategy;
|
|
36
|
+
}
|
|
37
|
+
const findSwapPath = (0, swapPath_1.createFindSwapPath)({
|
|
38
|
+
chainId,
|
|
39
|
+
fromTokenAddress: tokenIn.address,
|
|
40
|
+
toTokenAddress: tokenOut.address,
|
|
41
|
+
marketsInfoData,
|
|
42
|
+
isExpressFeeSwap: false,
|
|
43
|
+
});
|
|
44
|
+
const swapPathStats = findSwapPath(usdIn, { order: swapOptimizationOrder });
|
|
45
|
+
if (swapPathStats) {
|
|
46
|
+
return {
|
|
47
|
+
type: "internalSwap",
|
|
48
|
+
swapPathStats,
|
|
49
|
+
externalSwapQuote: undefined,
|
|
50
|
+
amountIn,
|
|
51
|
+
amountOut: swapPathStats.amountOut,
|
|
52
|
+
usdIn: usdIn,
|
|
53
|
+
usdOut: swapPathStats.usdOut,
|
|
54
|
+
priceIn: priceIn,
|
|
55
|
+
priceOut: tokenOut.prices.maxPrice,
|
|
56
|
+
feesUsd: usdIn - swapPathStats.usdOut,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
const availableExternalSwapPaths = (0, externalSwapPath_1.getAvailableExternalSwapPaths)({ chainId, fromTokenAddress: tokenIn.address });
|
|
60
|
+
const suitableSwapPath = availableExternalSwapPaths.find((path) => {
|
|
61
|
+
const findSwapPath = (0, swapPath_1.createFindSwapPath)({
|
|
62
|
+
chainId,
|
|
63
|
+
fromTokenAddress: path.outTokenAddress,
|
|
64
|
+
toTokenAddress: tokenOut.address,
|
|
65
|
+
marketsInfoData,
|
|
66
|
+
isExpressFeeSwap: false,
|
|
67
|
+
});
|
|
68
|
+
const swapPathStats = findSwapPath(usdIn);
|
|
69
|
+
return Boolean(swapPathStats);
|
|
70
|
+
});
|
|
71
|
+
if (suitableSwapPath && suitableSwapPath.outTokenAddress !== tokenOut.address) {
|
|
72
|
+
const externalSwapQuoteForCombinedSwap = (0, externalSwapQuoteByPath_1.getExternalSwapQuoteByPath)({
|
|
73
|
+
amountIn,
|
|
74
|
+
externalSwapPath: suitableSwapPath,
|
|
75
|
+
externalSwapQuoteParams,
|
|
76
|
+
});
|
|
77
|
+
const findSwapPathForSuitableSwapPath = (0, swapPath_1.createFindSwapPath)({
|
|
78
|
+
chainId,
|
|
79
|
+
fromTokenAddress: suitableSwapPath.outTokenAddress,
|
|
80
|
+
toTokenAddress: tokenOut.address,
|
|
81
|
+
marketsInfoData,
|
|
82
|
+
isExpressFeeSwap: false,
|
|
83
|
+
});
|
|
84
|
+
const swapPathStatsForCombinedSwap = externalSwapQuoteForCombinedSwap
|
|
85
|
+
? findSwapPathForSuitableSwapPath(externalSwapQuoteForCombinedSwap.usdOut)
|
|
86
|
+
: undefined;
|
|
87
|
+
return externalSwapQuoteForCombinedSwap && swapPathStatsForCombinedSwap
|
|
88
|
+
? {
|
|
89
|
+
type: "combinedSwap",
|
|
90
|
+
externalSwapQuote: externalSwapQuoteForCombinedSwap,
|
|
91
|
+
swapPathStats: swapPathStatsForCombinedSwap,
|
|
92
|
+
amountIn,
|
|
93
|
+
amountOut: swapPathStatsForCombinedSwap.amountOut,
|
|
94
|
+
usdIn: externalSwapQuoteForCombinedSwap.usdIn,
|
|
95
|
+
usdOut: swapPathStatsForCombinedSwap.usdOut,
|
|
96
|
+
priceIn: externalSwapQuoteForCombinedSwap.priceIn,
|
|
97
|
+
priceOut: tokenOut.prices.maxPrice,
|
|
98
|
+
feesUsd: externalSwapQuoteForCombinedSwap.usdIn - swapPathStatsForCombinedSwap.usdOut,
|
|
99
|
+
}
|
|
100
|
+
: defaultSwapStrategy;
|
|
101
|
+
}
|
|
102
|
+
return defaultSwapStrategy;
|
|
103
|
+
}
|
|
104
|
+
exports.buildSwapStrategy = buildSwapStrategy;
|
|
105
|
+
// Used for getting swap amounts by to value
|
|
106
|
+
function buildReverseSwapStrategy({ amountOut, tokenIn, tokenOut, marketsInfoData, chainId, externalSwapQuoteParams, swapOptimizationOrder, }) {
|
|
107
|
+
const priceIn = (0, tokens_1.getMidPrice)(tokenIn.prices);
|
|
108
|
+
const priceOut = (0, tokens_1.getMidPrice)(tokenOut.prices);
|
|
109
|
+
const preferredUsdOut = (0, tokens_1.convertToUsd)(amountOut, tokenOut.decimals, (0, tokens_1.getMidPrice)(tokenOut.prices));
|
|
110
|
+
const approximateAmountIn = (0, tokens_1.convertToTokenAmount)(preferredUsdOut, tokenIn.decimals, (0, tokens_1.getMidPrice)(tokenIn.prices));
|
|
111
|
+
const approximateUsdIn = preferredUsdOut;
|
|
112
|
+
const defaultSwapStrategy = {
|
|
113
|
+
type: "noSwap",
|
|
114
|
+
externalSwapQuote: undefined,
|
|
115
|
+
swapPathStats: undefined,
|
|
116
|
+
amountIn: approximateAmountIn,
|
|
117
|
+
amountOut: amountOut,
|
|
118
|
+
usdIn: approximateUsdIn,
|
|
119
|
+
usdOut: preferredUsdOut,
|
|
120
|
+
priceIn,
|
|
121
|
+
priceOut,
|
|
122
|
+
feesUsd: 0n,
|
|
123
|
+
};
|
|
124
|
+
if ((0, tokens_1.getIsEquivalentTokens)(tokenIn, tokenOut) || (0, tokens_1.getIsStake)(tokenIn, tokenOut) || (0, tokens_1.getIsUnstake)(tokenIn, tokenOut)) {
|
|
125
|
+
return defaultSwapStrategy;
|
|
126
|
+
}
|
|
127
|
+
const findSwapPath = (0, swapPath_1.createFindSwapPath)({
|
|
128
|
+
chainId,
|
|
129
|
+
fromTokenAddress: tokenIn.address,
|
|
130
|
+
toTokenAddress: tokenOut.address,
|
|
131
|
+
marketsInfoData,
|
|
132
|
+
isExpressFeeSwap: false,
|
|
133
|
+
});
|
|
134
|
+
const approximateSwapPathStats = findSwapPath(approximateUsdIn, { order: swapOptimizationOrder });
|
|
135
|
+
if (approximateSwapPathStats) {
|
|
136
|
+
// Increase or decrease usdIn the same way preferred usdOut is different from swapStrategy.usdOut
|
|
137
|
+
// preferred_in / approximate_in = preferred_out / approximate_out
|
|
138
|
+
// preferred_in = approximate_in * preferred_out / approximate_out
|
|
139
|
+
const adjustedUsdIn = approximateSwapPathStats.usdOut > 0
|
|
140
|
+
? bigmath_1.bigMath.mulDiv(approximateUsdIn, preferredUsdOut, approximateSwapPathStats.usdOut)
|
|
141
|
+
: 0n;
|
|
142
|
+
const adjustedAmountIn = (0, tokens_1.convertToTokenAmount)(adjustedUsdIn, tokenIn.decimals, (0, tokens_1.getMidPrice)(tokenIn.prices));
|
|
143
|
+
const adjustedSwapPathStats = findSwapPath(adjustedUsdIn, { order: swapOptimizationOrder });
|
|
144
|
+
if (adjustedSwapPathStats) {
|
|
145
|
+
return {
|
|
146
|
+
type: "internalSwap",
|
|
147
|
+
swapPathStats: adjustedSwapPathStats,
|
|
148
|
+
externalSwapQuote: undefined,
|
|
149
|
+
amountIn: adjustedAmountIn,
|
|
150
|
+
amountOut: adjustedSwapPathStats.amountOut,
|
|
151
|
+
usdIn: adjustedUsdIn,
|
|
152
|
+
usdOut: adjustedSwapPathStats.usdOut,
|
|
153
|
+
priceIn: priceIn,
|
|
154
|
+
priceOut: priceOut,
|
|
155
|
+
feesUsd: adjustedUsdIn - adjustedSwapPathStats.usdOut,
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
const availableExternalSwapPaths = (0, externalSwapPath_1.getAvailableExternalSwapPaths)({ chainId, fromTokenAddress: tokenIn.address });
|
|
160
|
+
const suitableSwapPath = availableExternalSwapPaths.find((path) => {
|
|
161
|
+
if (path.outTokenAddress !== tokenOut.address)
|
|
162
|
+
return false;
|
|
163
|
+
const findSwapPath = (0, swapPath_1.createFindSwapPath)({
|
|
164
|
+
chainId,
|
|
165
|
+
fromTokenAddress: tokenIn.address,
|
|
166
|
+
toTokenAddress: path.inTokenAddress,
|
|
167
|
+
marketsInfoData,
|
|
168
|
+
isExpressFeeSwap: false,
|
|
169
|
+
});
|
|
170
|
+
const swapPathStats = findSwapPath(approximateUsdIn);
|
|
171
|
+
return Boolean(swapPathStats);
|
|
172
|
+
});
|
|
173
|
+
if (suitableSwapPath) {
|
|
174
|
+
const approximateExternalSwapQuoteForCombinedSwap = (0, externalSwapQuoteByPath_1.getExternalSwapQuoteByPath)({
|
|
175
|
+
amountIn: approximateAmountIn,
|
|
176
|
+
externalSwapPath: suitableSwapPath,
|
|
177
|
+
externalSwapQuoteParams,
|
|
178
|
+
});
|
|
179
|
+
if (!approximateExternalSwapQuoteForCombinedSwap) {
|
|
180
|
+
return defaultSwapStrategy;
|
|
181
|
+
}
|
|
182
|
+
const findSwapPathForSuitableSwapPath = (0, swapPath_1.createFindSwapPath)({
|
|
183
|
+
chainId,
|
|
184
|
+
fromTokenAddress: tokenIn.address,
|
|
185
|
+
toTokenAddress: suitableSwapPath.inTokenAddress,
|
|
186
|
+
marketsInfoData,
|
|
187
|
+
isExpressFeeSwap: false,
|
|
188
|
+
});
|
|
189
|
+
const approximateSwapPathStatsForCombinedSwap = findSwapPathForSuitableSwapPath(approximateExternalSwapQuoteForCombinedSwap.usdOut);
|
|
190
|
+
if (!approximateSwapPathStatsForCombinedSwap) {
|
|
191
|
+
return defaultSwapStrategy;
|
|
192
|
+
}
|
|
193
|
+
const adjustedUsdIn = approximateSwapPathStatsForCombinedSwap.usdOut > 0
|
|
194
|
+
? bigmath_1.bigMath.mulDiv(approximateUsdIn, preferredUsdOut, approximateSwapPathStatsForCombinedSwap.usdOut)
|
|
195
|
+
: 0n;
|
|
196
|
+
const adjustedAmountIn = (0, tokens_1.convertToTokenAmount)(adjustedUsdIn, tokenIn.decimals, (0, tokens_1.getMidPrice)(tokenIn.prices));
|
|
197
|
+
const adjustedExternalSwapQuoteForCombinedSwap = (0, externalSwapQuoteByPath_1.getExternalSwapQuoteByPath)({
|
|
198
|
+
amountIn: adjustedAmountIn,
|
|
199
|
+
externalSwapPath: suitableSwapPath,
|
|
200
|
+
externalSwapQuoteParams,
|
|
201
|
+
});
|
|
202
|
+
if (!adjustedExternalSwapQuoteForCombinedSwap) {
|
|
203
|
+
return defaultSwapStrategy;
|
|
204
|
+
}
|
|
205
|
+
const adjustedSwapPathStatsForCombinedSwap = findSwapPathForSuitableSwapPath(adjustedExternalSwapQuoteForCombinedSwap.usdOut);
|
|
206
|
+
if (!adjustedSwapPathStatsForCombinedSwap) {
|
|
207
|
+
return defaultSwapStrategy;
|
|
208
|
+
}
|
|
209
|
+
return {
|
|
210
|
+
type: "combinedSwap",
|
|
211
|
+
externalSwapQuote: adjustedExternalSwapQuoteForCombinedSwap,
|
|
212
|
+
swapPathStats: adjustedSwapPathStatsForCombinedSwap,
|
|
213
|
+
amountIn: adjustedAmountIn,
|
|
214
|
+
amountOut: adjustedSwapPathStatsForCombinedSwap.amountOut,
|
|
215
|
+
usdIn: adjustedExternalSwapQuoteForCombinedSwap.usdIn,
|
|
216
|
+
usdOut: adjustedSwapPathStatsForCombinedSwap.usdOut,
|
|
217
|
+
priceIn: adjustedExternalSwapQuoteForCombinedSwap.priceIn,
|
|
218
|
+
priceOut: priceOut,
|
|
219
|
+
feesUsd: adjustedExternalSwapQuoteForCombinedSwap.usdIn - adjustedSwapPathStatsForCombinedSwap.usdOut,
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
return defaultSwapStrategy;
|
|
223
|
+
}
|
|
224
|
+
exports.buildReverseSwapStrategy = buildReverseSwapStrategy;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getAvailableExternalSwapPaths = exports.AVAILABLE_BOTANIX_WITHDRAW_PAIRS = exports.AVAILABLE_BOTANIX_DEPOSIT_PAIRS = void 0;
|
|
4
|
+
const chains_1 = require("../../configs/chains");
|
|
5
|
+
const tokens_1 = require("../../configs/tokens");
|
|
6
|
+
const trade_1 = require("../../types/trade");
|
|
7
|
+
const BBTC_ADDRESS = tokens_1.NATIVE_TOKEN_ADDRESS;
|
|
8
|
+
const PBTC_ADDRESS = (0, tokens_1.getTokenBySymbol)(chains_1.BOTANIX, "PBTC").address;
|
|
9
|
+
const STBTC_ADDRESS = (0, tokens_1.getTokenBySymbol)(chains_1.BOTANIX, "STBTC").address;
|
|
10
|
+
exports.AVAILABLE_BOTANIX_DEPOSIT_PAIRS = [
|
|
11
|
+
{
|
|
12
|
+
from: BBTC_ADDRESS,
|
|
13
|
+
to: STBTC_ADDRESS,
|
|
14
|
+
},
|
|
15
|
+
{
|
|
16
|
+
from: PBTC_ADDRESS,
|
|
17
|
+
to: STBTC_ADDRESS,
|
|
18
|
+
},
|
|
19
|
+
];
|
|
20
|
+
exports.AVAILABLE_BOTANIX_WITHDRAW_PAIRS = [
|
|
21
|
+
{
|
|
22
|
+
from: STBTC_ADDRESS,
|
|
23
|
+
to: PBTC_ADDRESS,
|
|
24
|
+
},
|
|
25
|
+
];
|
|
26
|
+
const getBotanixStakingExternalSwapPaths = ({ fromTokenAddress }) => {
|
|
27
|
+
return [...exports.AVAILABLE_BOTANIX_DEPOSIT_PAIRS, ...exports.AVAILABLE_BOTANIX_WITHDRAW_PAIRS]
|
|
28
|
+
.filter((pair) => pair.from === fromTokenAddress)
|
|
29
|
+
.map((pair) => ({
|
|
30
|
+
aggregator: trade_1.ExternalSwapAggregator.BotanixStaking,
|
|
31
|
+
inTokenAddress: pair.from,
|
|
32
|
+
outTokenAddress: pair.to,
|
|
33
|
+
}));
|
|
34
|
+
};
|
|
35
|
+
const getAvailableExternalSwapPaths = ({ chainId, fromTokenAddress, }) => {
|
|
36
|
+
if (chainId === chains_1.BOTANIX) {
|
|
37
|
+
return getBotanixStakingExternalSwapPaths({ fromTokenAddress });
|
|
38
|
+
}
|
|
39
|
+
return [];
|
|
40
|
+
};
|
|
41
|
+
exports.getAvailableExternalSwapPaths = getAvailableExternalSwapPaths;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getExternalSwapQuoteByPath = void 0;
|
|
4
|
+
const trade_1 = require("../../types/trade");
|
|
5
|
+
const botanixStaking_1 = require("./botanixStaking");
|
|
6
|
+
const getExternalSwapQuoteByPath = ({ amountIn, externalSwapPath, externalSwapQuoteParams, }) => {
|
|
7
|
+
if (amountIn === undefined ||
|
|
8
|
+
externalSwapQuoteParams.gasPrice === undefined ||
|
|
9
|
+
externalSwapQuoteParams.tokensData === undefined ||
|
|
10
|
+
externalSwapQuoteParams.botanixStakingAssetsPerShare === undefined ||
|
|
11
|
+
externalSwapQuoteParams.receiverAddress === undefined) {
|
|
12
|
+
return undefined;
|
|
13
|
+
}
|
|
14
|
+
if (externalSwapPath.aggregator === trade_1.ExternalSwapAggregator.BotanixStaking) {
|
|
15
|
+
return (0, botanixStaking_1.getBotanixStakingExternalSwapQuote)({
|
|
16
|
+
tokenInAddress: externalSwapPath.inTokenAddress,
|
|
17
|
+
tokenOutAddress: externalSwapPath.outTokenAddress,
|
|
18
|
+
amountIn,
|
|
19
|
+
gasPrice: externalSwapQuoteParams.gasPrice,
|
|
20
|
+
receiverAddress: externalSwapQuoteParams.receiverAddress,
|
|
21
|
+
tokensData: externalSwapQuoteParams.tokensData,
|
|
22
|
+
assetsPerShare: externalSwapQuoteParams.botanixStakingAssetsPerShare,
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
return undefined;
|
|
26
|
+
};
|
|
27
|
+
exports.getExternalSwapQuoteByPath = getExternalSwapQuoteByPath;
|