@gmx-io/sdk 1.5.0-alpha-9 → 1.5.0-alpha-11
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/subsquid-codegen.js +1 -1
- package/build/cjs/src/clients/v2/__tests__/data.spec.js +85 -0
- package/build/cjs/src/clients/v2/__tests__/decrease.spec.js +484 -0
- package/build/cjs/src/clients/v2/__tests__/editCancel.spec.js +260 -0
- package/build/cjs/src/clients/v2/__tests__/errors.spec.js +263 -0
- package/build/cjs/src/clients/v2/__tests__/increase.spec.js +777 -0
- package/build/cjs/src/clients/v2/__tests__/subaccount.spec.js +74 -0
- package/build/cjs/src/clients/v2/__tests__/swap.spec.js +192 -0
- package/build/cjs/src/clients/v2/__tests__/testUtil.js +135 -0
- package/build/cjs/src/clients/v2/index.js +171 -27
- package/build/cjs/src/codegen/prebuilt/hashedKinkModelMarketRatesKeys.json +48 -0
- package/build/cjs/src/codegen/prebuilt/hashedMarketConfigKeys.json +1082 -0
- package/build/cjs/src/codegen/prebuilt/hashedMarketValuesKeys.json +90 -0
- package/build/cjs/src/codegen/subsquid.js +323 -1
- package/build/cjs/src/configs/api.js +17 -5
- package/build/cjs/src/configs/chains.js +11 -1
- package/build/cjs/src/configs/contracts.js +7 -7
- package/build/cjs/src/configs/markets.js +43 -1
- package/build/cjs/src/configs/tokens.js +63 -132
- package/build/cjs/src/test/mock.js +1 -1
- package/build/cjs/src/utils/__tests__/markets.spec.js +10 -0
- 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__/positions.spec.js +59 -7
- package/build/cjs/src/utils/__tests__/uiFeeReceiver.spec.js +34 -0
- package/build/cjs/src/utils/balances/api.js +63 -0
- package/build/cjs/src/utils/buyback/api.js +7 -0
- package/build/cjs/src/utils/buyback/types.js +2 -0
- package/build/cjs/src/utils/errors/index.js +1 -0
- package/build/cjs/src/utils/errors/isIgnoredEstimateGasError.js +23 -0
- package/build/cjs/src/utils/express/index.js +18 -0
- package/build/cjs/src/utils/express/types.js +2 -0
- package/build/cjs/src/utils/express/utils/batchOrderUtils.js +218 -0
- package/build/cjs/src/utils/express/utils/bridgeOutUtils.js +73 -0
- package/build/cjs/src/utils/express/utils/estimateExpressParams.js +243 -0
- package/build/cjs/src/utils/express/utils/expressParamsUtils.js +22 -0
- package/build/cjs/src/utils/express/utils/fetchGelatoGasTankBalance.js +13 -0
- package/build/cjs/src/utils/express/utils/gelatoRelayUtils.js +77 -0
- package/build/cjs/src/utils/express/utils/globalExpressParams.js +40 -0
- package/build/cjs/src/utils/express/utils/index.js +27 -0
- package/build/cjs/src/utils/express/utils/oracleParamsUtils.js +49 -0
- package/build/cjs/src/utils/express/utils/referralUtils.js +35 -0
- package/build/cjs/src/utils/express/utils/relayParamsUtils.js +201 -0
- package/build/cjs/src/utils/fees/executionFee.js +8 -1
- package/build/cjs/src/utils/fees/priceImpact.js +1 -2
- package/build/cjs/src/utils/http/http.js +86 -13
- package/build/cjs/src/utils/http/httpFallback.js +65 -0
- package/build/cjs/src/utils/markets/__tests__/mockMarkets.js +4 -0
- package/build/cjs/src/utils/markets/hashKeys.js +16 -0
- package/build/cjs/src/utils/markets/multicall.js +20 -0
- package/build/cjs/src/utils/markets/utils.js +8 -0
- package/build/cjs/src/utils/metrics/index.js +7 -0
- package/build/cjs/src/utils/numbers/utils.js +29 -27
- package/build/cjs/src/utils/orderTransactions/api.js +88 -0
- package/build/cjs/src/utils/orderTransactions/index.js +1 -0
- package/build/cjs/src/utils/orderTransactions/utils.js +4 -1
- package/build/cjs/src/utils/orderTransactions/validateTypedData.js +89 -0
- package/build/cjs/src/utils/positions/utils.js +5 -19
- package/build/cjs/src/utils/rpc/createViemRpc.js +39 -0
- package/build/cjs/src/utils/rpc/index.js +5 -0
- package/build/cjs/src/utils/signer/index.js +5 -0
- package/build/cjs/src/utils/signer/privateKeySigner.js +42 -0
- package/build/cjs/src/utils/subaccount/api.js +22 -0
- package/build/cjs/src/utils/subaccount/generateSubaccount.js +19 -0
- package/build/cjs/src/utils/subaccount/getEmptySubaccountApproval.js +24 -0
- package/build/cjs/src/utils/subaccount/getSubaccountApprovalTypedData.js +46 -0
- package/build/cjs/src/utils/subaccount/hashSubaccountApproval.js +28 -0
- package/build/cjs/src/utils/subaccount/index.js +30 -0
- package/build/cjs/src/utils/subaccount/types.js +2 -0
- package/build/cjs/src/utils/swap/__tests__/mockRouting.spec.js +49 -0
- package/build/cjs/src/utils/swap/__tests__/swapStats.spec.js +63 -0
- package/build/cjs/src/utils/swap/buildSwapStrategy.js +10 -2
- package/build/cjs/src/utils/swap/swapRouting.js +7 -2
- package/build/cjs/src/utils/swap/swapStats.js +90 -6
- package/build/cjs/src/utils/swap/swapValues.js +8 -2
- package/build/cjs/src/utils/trade/increase.js +7 -1
- package/build/cjs/src/utils/trade/types.js +1 -1
- package/build/cjs/src/utils/tradeHistory/utils.js +14 -28
- package/build/cjs/src/utils/twap/index.js +4 -1
- package/build/cjs/src/utils/twap/uiFeeReceiver.js +23 -7
- package/build/esm/scripts/subsquid-codegen.js +1 -1
- package/build/esm/src/clients/v2/__tests__/data.spec.js +83 -0
- package/build/esm/src/clients/v2/__tests__/decrease.spec.js +482 -0
- package/build/esm/src/clients/v2/__tests__/editCancel.spec.js +258 -0
- package/build/esm/src/clients/v2/__tests__/errors.spec.js +261 -0
- package/build/esm/src/clients/v2/__tests__/increase.spec.js +775 -0
- package/build/esm/src/clients/v2/__tests__/subaccount.spec.js +72 -0
- package/build/esm/src/clients/v2/__tests__/swap.spec.js +190 -0
- package/build/esm/src/clients/v2/__tests__/testUtil.js +123 -0
- package/build/esm/src/clients/v2/index.js +145 -5
- package/build/esm/src/codegen/prebuilt/hashedKinkModelMarketRatesKeys.json +48 -0
- package/build/esm/src/codegen/prebuilt/hashedMarketConfigKeys.json +1082 -0
- package/build/esm/src/codegen/prebuilt/hashedMarketValuesKeys.json +90 -0
- package/build/esm/src/codegen/subsquid.js +321 -0
- package/build/esm/src/configs/api.js +15 -4
- package/build/esm/src/configs/chains.js +11 -1
- package/build/esm/src/configs/contracts.js +7 -7
- package/build/esm/src/configs/markets.js +43 -1
- package/build/esm/src/configs/tokens.js +62 -130
- package/build/esm/src/test/mock.js +1 -1
- package/build/esm/src/utils/__tests__/markets.spec.js +10 -0
- 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__/positions.spec.js +60 -8
- package/build/esm/src/utils/__tests__/uiFeeReceiver.spec.js +35 -1
- package/build/esm/src/utils/balances/api.js +54 -0
- package/build/esm/src/utils/buyback/api.js +3 -0
- package/build/esm/src/utils/buyback/types.js +1 -0
- package/build/esm/src/utils/errors/index.js +1 -0
- package/build/esm/src/utils/errors/isIgnoredEstimateGasError.js +19 -0
- package/build/esm/src/utils/express/index.js +2 -0
- package/build/esm/src/utils/express/types.js +1 -0
- package/build/esm/src/utils/express/utils/batchOrderUtils.js +210 -0
- package/build/esm/src/utils/express/utils/bridgeOutUtils.js +69 -0
- package/build/esm/src/utils/express/utils/estimateExpressParams.js +237 -0
- package/build/esm/src/utils/express/utils/expressParamsUtils.js +16 -0
- package/build/esm/src/utils/express/utils/fetchGelatoGasTankBalance.js +9 -0
- package/build/esm/src/utils/express/utils/gelatoRelayUtils.js +71 -0
- package/build/esm/src/utils/express/utils/globalExpressParams.js +36 -0
- package/build/esm/src/utils/express/utils/index.js +11 -0
- package/build/esm/src/utils/express/utils/oracleParamsUtils.js +42 -0
- package/build/esm/src/utils/express/utils/referralUtils.js +30 -0
- package/build/esm/src/utils/express/utils/relayParamsUtils.js +192 -0
- package/build/esm/src/utils/fees/executionFee.js +6 -0
- package/build/esm/src/utils/fees/priceImpact.js +1 -2
- package/build/esm/src/utils/http/http.js +84 -12
- package/build/esm/src/utils/http/httpFallback.js +61 -0
- package/build/esm/src/utils/markets/__tests__/mockMarkets.js +4 -0
- package/build/esm/src/utils/markets/hashKeys.js +17 -1
- package/build/esm/src/utils/markets/multicall.js +20 -0
- package/build/esm/src/utils/markets/utils.js +8 -0
- package/build/esm/src/utils/metrics/index.js +4 -0
- package/build/esm/src/utils/numbers/utils.js +25 -24
- package/build/esm/src/utils/orderTransactions/api.js +77 -0
- package/build/esm/src/utils/orderTransactions/index.js +1 -0
- package/build/esm/src/utils/orderTransactions/utils.js +4 -1
- package/build/esm/src/utils/orderTransactions/validateTypedData.js +84 -0
- package/build/esm/src/utils/positions/utils.js +5 -19
- package/build/esm/src/utils/rpc/createViemRpc.js +35 -0
- package/build/esm/src/utils/rpc/index.js +1 -0
- package/build/esm/src/utils/signer/index.js +1 -0
- package/build/esm/src/utils/signer/privateKeySigner.js +38 -0
- package/build/esm/src/utils/subaccount/api.js +16 -0
- package/build/esm/src/utils/subaccount/generateSubaccount.js +15 -0
- package/build/esm/src/utils/subaccount/getEmptySubaccountApproval.js +20 -0
- package/build/esm/src/utils/subaccount/getSubaccountApprovalTypedData.js +42 -0
- package/build/esm/src/utils/subaccount/hashSubaccountApproval.js +24 -0
- package/build/esm/src/utils/subaccount/index.js +6 -0
- package/build/esm/src/utils/subaccount/types.js +1 -0
- package/build/esm/src/utils/swap/__tests__/mockRouting.spec.js +49 -0
- package/build/esm/src/utils/swap/__tests__/swapStats.spec.js +64 -1
- package/build/esm/src/utils/swap/buildSwapStrategy.js +10 -2
- package/build/esm/src/utils/swap/swapRouting.js +7 -2
- package/build/esm/src/utils/swap/swapStats.js +90 -7
- package/build/esm/src/utils/swap/swapValues.js +8 -2
- package/build/esm/src/utils/trade/increase.js +7 -1
- package/build/esm/src/utils/trade/types.js +1 -1
- package/build/esm/src/utils/tradeHistory/utils.js +13 -26
- package/build/esm/src/utils/twap/index.js +1 -0
- package/build/esm/src/utils/twap/uiFeeReceiver.js +20 -6
- package/build/types/src/clients/v2/__tests__/data.spec.d.ts +1 -0
- package/build/types/src/clients/v2/__tests__/decrease.spec.d.ts +1 -0
- package/build/types/src/clients/v2/__tests__/editCancel.spec.d.ts +1 -0
- package/build/types/src/clients/v2/__tests__/errors.spec.d.ts +1 -0
- package/build/types/src/clients/v2/__tests__/increase.spec.d.ts +1 -0
- package/build/types/src/clients/v2/__tests__/subaccount.spec.d.ts +1 -0
- package/build/types/src/clients/v2/__tests__/swap.spec.d.ts +1 -0
- package/build/types/src/clients/v2/__tests__/testUtil.d.ts +24 -0
- package/build/types/src/clients/v2/index.d.ts +62 -1
- package/build/types/src/codegen/prebuilt/index.d.ts +1220 -0
- package/build/types/src/codegen/subsquid.d.ts +1276 -44
- package/build/types/src/configs/api.d.ts +1 -0
- package/build/types/src/configs/chains.d.ts +10 -1
- package/build/types/src/configs/contracts.d.ts +6 -6
- package/build/types/src/configs/tokens.d.ts +6 -1
- package/build/types/src/utils/balances/api.d.ts +39 -0
- package/build/types/src/utils/buyback/api.d.ts +5 -0
- package/build/types/src/utils/buyback/types.d.ts +15 -0
- package/build/types/src/utils/chains/types.d.ts +1 -0
- package/build/types/src/utils/errors/index.d.ts +1 -0
- package/build/types/src/utils/errors/isIgnoredEstimateGasError.d.ts +2 -0
- package/build/types/src/utils/express/index.d.ts +2 -0
- package/build/types/src/utils/express/types.d.ts +95 -0
- package/build/types/src/utils/express/utils/batchOrderUtils.d.ts +98 -0
- package/build/types/src/utils/express/utils/bridgeOutUtils.d.ts +21 -0
- package/build/types/src/utils/express/utils/estimateExpressParams.d.ts +37 -0
- package/build/types/src/utils/express/utils/expressParamsUtils.d.ts +19 -0
- package/build/types/src/utils/express/utils/fetchGelatoGasTankBalance.d.ts +5 -0
- package/build/types/src/utils/express/utils/gelatoRelayUtils.d.ts +25 -0
- package/build/types/src/utils/express/utils/globalExpressParams.d.ts +31 -0
- package/build/types/src/utils/express/utils/index.d.ts +11 -0
- package/build/types/src/utils/express/utils/oracleParamsUtils.d.ts +10 -0
- package/build/types/src/utils/express/utils/referralUtils.d.ts +17 -0
- package/build/types/src/utils/express/utils/relayParamsUtils.d.ts +49 -0
- package/build/types/src/utils/fees/executionFee.d.ts +1 -0
- package/build/types/src/utils/http/http.d.ts +16 -1
- package/build/types/src/utils/http/httpFallback.d.ts +18 -0
- package/build/types/src/utils/http/types.d.ts +3 -0
- package/build/types/src/utils/markets/types.d.ts +5 -1
- package/build/types/src/utils/metrics/index.d.ts +4 -0
- package/build/types/src/utils/numbers/utils.d.ts +2 -5
- package/build/types/src/utils/orderTransactions/api.d.ts +158 -0
- package/build/types/src/utils/orderTransactions/index.d.ts +1 -0
- package/build/types/src/utils/orderTransactions/validateTypedData.d.ts +6 -0
- package/build/types/src/utils/positions/utils.d.ts +1 -9
- package/build/types/src/utils/rpc/createViemRpc.d.ts +3 -0
- package/build/types/src/utils/rpc/index.d.ts +26 -0
- package/build/types/src/utils/signer/index.d.ts +23 -0
- package/build/types/src/utils/signer/privateKeySigner.d.ts +19 -0
- package/build/types/src/utils/subaccount/api.d.ts +51 -0
- package/build/types/src/utils/subaccount/generateSubaccount.d.ts +8 -0
- package/build/types/src/utils/subaccount/getEmptySubaccountApproval.d.ts +9 -0
- package/build/types/src/utils/subaccount/getSubaccountApprovalTypedData.d.ts +24 -0
- package/build/types/src/utils/subaccount/hashSubaccountApproval.d.ts +1 -0
- package/build/types/src/utils/subaccount/index.d.ts +7 -0
- package/build/types/src/utils/subaccount/types.d.ts +55 -0
- package/build/types/src/utils/swap/buildSwapStrategy.d.ts +6 -2
- package/build/types/src/utils/swap/swapStats.d.ts +14 -0
- package/build/types/src/utils/swap/swapValues.d.ts +4 -0
- package/build/types/src/utils/tokens/types.d.ts +0 -1
- package/build/types/src/utils/trade/increase.d.ts +2 -0
- package/build/types/src/utils/trade/types.d.ts +2 -1
- package/build/types/src/utils/tradeHistory/utils.d.ts +0 -1
- package/build/types/src/utils/twap/index.d.ts +1 -0
- package/build/types/src/utils/twap/types.d.ts +2 -0
- package/build/types/src/utils/twap/uiFeeReceiver.d.ts +10 -2
- package/package.json +40 -3
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.hashRelayParams = exports.getRawRelayerParams = exports.getBestFeeSwapStrategy = exports.getRelayerFeeParams = exports.getGelatoRelayRouterDomain = exports.getExpressContractAddress = void 0;
|
|
4
|
+
const viem_1 = require("viem");
|
|
5
|
+
const abis_1 = require("../../../abis");
|
|
6
|
+
const contracts_1 = require("../../../configs/contracts");
|
|
7
|
+
const types_1 = require("../../orders/types");
|
|
8
|
+
const orderTransactions_1 = require("../../orderTransactions");
|
|
9
|
+
const swap_1 = require("../../swap");
|
|
10
|
+
const time_1 = require("../../time");
|
|
11
|
+
const oracleParamsUtils_1 = require("./oracleParamsUtils");
|
|
12
|
+
function getExpressContractAddress(chainId, { isSubaccount, isMultichain, scope, }) {
|
|
13
|
+
let contractName;
|
|
14
|
+
if (isMultichain) {
|
|
15
|
+
switch (scope) {
|
|
16
|
+
case "claims":
|
|
17
|
+
contractName = "MultichainClaimsRouter";
|
|
18
|
+
break;
|
|
19
|
+
case "order":
|
|
20
|
+
contractName = "MultichainOrderRouter";
|
|
21
|
+
break;
|
|
22
|
+
case "subaccount":
|
|
23
|
+
contractName = "MultichainSubaccountRouter";
|
|
24
|
+
break;
|
|
25
|
+
case "glv":
|
|
26
|
+
contractName = "MultichainGlvRouter";
|
|
27
|
+
break;
|
|
28
|
+
case "gm":
|
|
29
|
+
contractName = "MultichainGmRouter";
|
|
30
|
+
break;
|
|
31
|
+
case "transfer":
|
|
32
|
+
contractName = "MultichainTransferRouter";
|
|
33
|
+
break;
|
|
34
|
+
default:
|
|
35
|
+
throw new Error(`Invalid scope: ${scope}`);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
if (isSubaccount) {
|
|
40
|
+
contractName = "SubaccountGelatoRelayRouter";
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
contractName = "GelatoRelayRouter";
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return (0, contracts_1.getContract)(chainId, contractName);
|
|
47
|
+
}
|
|
48
|
+
exports.getExpressContractAddress = getExpressContractAddress;
|
|
49
|
+
function getGelatoRelayRouterDomain(chainId, relayRouterAddress) {
|
|
50
|
+
return {
|
|
51
|
+
name: "GmxBaseGelatoRelayRouter",
|
|
52
|
+
version: "1",
|
|
53
|
+
chainId,
|
|
54
|
+
verifyingContract: relayRouterAddress,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
exports.getGelatoRelayRouterDomain = getGelatoRelayRouterDomain;
|
|
58
|
+
function getRelayerFeeParams({ chainId, account, gasPaymentToken, relayerFeeToken, relayerFeeAmount, totalRelayerFeeTokenAmount, gasPaymentTokenAsCollateralAmount, transactionExternalCalls, feeExternalSwapQuote, findFeeSwapPath, }) {
|
|
59
|
+
const gasPaymentParams = {
|
|
60
|
+
gasPaymentToken: gasPaymentToken,
|
|
61
|
+
relayFeeToken: relayerFeeToken,
|
|
62
|
+
gasPaymentTokenAddress: gasPaymentToken.address,
|
|
63
|
+
relayerFeeTokenAddress: relayerFeeToken.address,
|
|
64
|
+
relayerFeeAmount,
|
|
65
|
+
totalRelayerFeeTokenAmount,
|
|
66
|
+
gasPaymentTokenAmount: 0n,
|
|
67
|
+
gasPaymentTokenAsCollateralAmount,
|
|
68
|
+
};
|
|
69
|
+
let feeParams;
|
|
70
|
+
let externalCalls = transactionExternalCalls ?? (0, orderTransactions_1.getEmptyExternalCallsPayload)();
|
|
71
|
+
let feeExternalSwapGasLimit = 0n;
|
|
72
|
+
if (relayerFeeToken.address === gasPaymentToken.address) {
|
|
73
|
+
feeParams = {
|
|
74
|
+
feeToken: relayerFeeToken.address,
|
|
75
|
+
feeAmount: totalRelayerFeeTokenAmount,
|
|
76
|
+
feeSwapPath: [],
|
|
77
|
+
};
|
|
78
|
+
gasPaymentParams.gasPaymentTokenAmount = totalRelayerFeeTokenAmount;
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
let feeSwapAmounts;
|
|
82
|
+
if (findFeeSwapPath) {
|
|
83
|
+
feeSwapAmounts = (0, swap_1.getSwapAmountsByToValue)({
|
|
84
|
+
tokenIn: gasPaymentToken,
|
|
85
|
+
tokenOut: relayerFeeToken,
|
|
86
|
+
amountOut: totalRelayerFeeTokenAmount,
|
|
87
|
+
isLimit: false,
|
|
88
|
+
findSwapPath: findFeeSwapPath,
|
|
89
|
+
swapOptimizationOrder: ["length"],
|
|
90
|
+
uiFeeFactor: 0n,
|
|
91
|
+
marketsInfoData: undefined,
|
|
92
|
+
chainId,
|
|
93
|
+
externalSwapQuoteParams: undefined,
|
|
94
|
+
allowSameTokenSwap: false,
|
|
95
|
+
swapPricingType: types_1.SwapPricingType.AtomicSwap,
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
const bestFeeSwapStrategy = getBestFeeSwapStrategy(feeSwapAmounts, feeExternalSwapQuote);
|
|
99
|
+
if (bestFeeSwapStrategy?.swapPath) {
|
|
100
|
+
feeParams = {
|
|
101
|
+
feeToken: gasPaymentToken.address,
|
|
102
|
+
feeAmount: bestFeeSwapStrategy.amountIn,
|
|
103
|
+
feeSwapPath: bestFeeSwapStrategy.swapPath,
|
|
104
|
+
};
|
|
105
|
+
gasPaymentParams.gasPaymentTokenAmount = bestFeeSwapStrategy.amountIn;
|
|
106
|
+
gasPaymentParams.totalRelayerFeeTokenAmount = bestFeeSwapStrategy.amountOut;
|
|
107
|
+
}
|
|
108
|
+
else if (bestFeeSwapStrategy?.externalSwapQuote) {
|
|
109
|
+
externalCalls = (0, orderTransactions_1.combineExternalCalls)([
|
|
110
|
+
externalCalls,
|
|
111
|
+
(0, orderTransactions_1.getExternalCallsPayload)({
|
|
112
|
+
chainId,
|
|
113
|
+
account,
|
|
114
|
+
quote: bestFeeSwapStrategy.externalSwapQuote,
|
|
115
|
+
}),
|
|
116
|
+
]);
|
|
117
|
+
feeExternalSwapGasLimit = bestFeeSwapStrategy.externalSwapQuote.txnData.estimatedGas;
|
|
118
|
+
feeParams = {
|
|
119
|
+
feeToken: relayerFeeToken.address,
|
|
120
|
+
feeAmount: 0n,
|
|
121
|
+
feeSwapPath: [],
|
|
122
|
+
};
|
|
123
|
+
gasPaymentParams.gasPaymentTokenAmount = bestFeeSwapStrategy.externalSwapQuote.amountIn;
|
|
124
|
+
gasPaymentParams.totalRelayerFeeTokenAmount = bestFeeSwapStrategy.externalSwapQuote.amountOut;
|
|
125
|
+
}
|
|
126
|
+
else {
|
|
127
|
+
return undefined;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return {
|
|
131
|
+
feeParams,
|
|
132
|
+
externalCalls,
|
|
133
|
+
feeExternalSwapGasLimit,
|
|
134
|
+
gasPaymentParams,
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
exports.getRelayerFeeParams = getRelayerFeeParams;
|
|
138
|
+
function getBestFeeSwapStrategy(internalSwapAmounts, externalSwapQuote) {
|
|
139
|
+
if (externalSwapQuote && externalSwapQuote.usdOut > (internalSwapAmounts?.swapStrategy.swapPathStats?.usdOut ?? 0n)) {
|
|
140
|
+
return {
|
|
141
|
+
amountIn: externalSwapQuote.amountIn,
|
|
142
|
+
amountOut: externalSwapQuote.amountOut,
|
|
143
|
+
externalSwapQuote,
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
else if (internalSwapAmounts?.swapStrategy.swapPathStats) {
|
|
147
|
+
return {
|
|
148
|
+
amountIn: internalSwapAmounts.amountIn,
|
|
149
|
+
amountOut: internalSwapAmounts.amountOut,
|
|
150
|
+
swapPath: internalSwapAmounts.swapStrategy.swapPathStats.swapPath,
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
return undefined;
|
|
154
|
+
}
|
|
155
|
+
exports.getBestFeeSwapStrategy = getBestFeeSwapStrategy;
|
|
156
|
+
function getRawRelayerParams({ chainId, gasPaymentTokenAddress, relayerFeeTokenAddress, feeParams, externalCalls, tokenPermits, }) {
|
|
157
|
+
const oracleParams = (0, oracleParamsUtils_1.getOracleParamsForRelayParams)({
|
|
158
|
+
chainId,
|
|
159
|
+
externalCalls,
|
|
160
|
+
feeSwapPath: feeParams.feeSwapPath,
|
|
161
|
+
gasPaymentTokenAddress,
|
|
162
|
+
relayerFeeTokenAddress,
|
|
163
|
+
});
|
|
164
|
+
return {
|
|
165
|
+
oracleParams,
|
|
166
|
+
tokenPermits,
|
|
167
|
+
externalCalls,
|
|
168
|
+
fee: feeParams,
|
|
169
|
+
desChainId: BigInt(chainId),
|
|
170
|
+
userNonce: BigInt((0, time_1.nowInSeconds)()),
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
exports.getRawRelayerParams = getRawRelayerParams;
|
|
174
|
+
function hashRelayParams(relayParams) {
|
|
175
|
+
const encoded = (0, viem_1.encodeAbiParameters)(abis_1.abis.RelayParams, [
|
|
176
|
+
{
|
|
177
|
+
tokens: relayParams.oracleParams.tokens,
|
|
178
|
+
providers: relayParams.oracleParams.providers,
|
|
179
|
+
data: relayParams.oracleParams.data,
|
|
180
|
+
},
|
|
181
|
+
{
|
|
182
|
+
sendTokens: relayParams.externalCalls.sendTokens,
|
|
183
|
+
sendAmounts: relayParams.externalCalls.sendAmounts,
|
|
184
|
+
externalCallTargets: relayParams.externalCalls.externalCallTargets,
|
|
185
|
+
externalCallDataList: relayParams.externalCalls.externalCallDataList,
|
|
186
|
+
refundTokens: relayParams.externalCalls.refundTokens,
|
|
187
|
+
refundReceivers: relayParams.externalCalls.refundReceivers,
|
|
188
|
+
},
|
|
189
|
+
relayParams.tokenPermits,
|
|
190
|
+
{
|
|
191
|
+
feeToken: relayParams.fee.feeToken,
|
|
192
|
+
feeAmount: relayParams.fee.feeAmount,
|
|
193
|
+
feeSwapPath: relayParams.fee.feeSwapPath,
|
|
194
|
+
},
|
|
195
|
+
relayParams.userNonce,
|
|
196
|
+
relayParams.deadline,
|
|
197
|
+
relayParams.desChainId,
|
|
198
|
+
]);
|
|
199
|
+
return (0, viem_1.keccak256)(encoded);
|
|
200
|
+
}
|
|
201
|
+
exports.hashRelayParams = hashRelayParams;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.estimateExecuteShiftGasLimit = exports.estimateExecuteWithdrawalGasLimit = exports.estimateExecuteGlvWithdrawalGasLimit = exports.estimateExecuteGlvDepositGasLimit = exports.estimateExecuteDepositGasLimit = exports.estimateExecuteSwapOrderGasLimit = exports.estimateExecuteDecreaseOrderGasLimit = exports.estimateExecuteIncreaseOrderGasLimit = exports.estimateBatchMinGasPaymentTokenAmount = exports.estimateBatchGasLimit = exports.approximateL1GasBuffer = exports.estimateRelayerGasLimit = exports.getExecutionFee = exports.getIsExecutionFeeVeryHigh = exports.getIsExecutionFeeHigh = void 0;
|
|
3
|
+
exports.estimateExecuteShiftGasLimit = exports.estimateExecuteWithdrawalGasLimit = exports.estimateExecuteGlvWithdrawalGasLimit = exports.estimateExecuteGlvDepositGasLimit = exports.estimateExecuteDepositGasLimit = exports.estimateExecuteSwapOrderGasLimit = exports.estimateExecuteDecreaseOrderGasLimit = exports.estimateExecuteIncreaseOrderGasLimit = exports.estimateBatchMinGasPaymentTokenAmount = exports.estimateBatchGasLimit = exports.approximateL1GasBuffer = exports.applyGasBuffer = exports.estimateRelayerGasLimit = exports.getExecutionFee = exports.getIsExecutionFeeVeryHigh = exports.getIsExecutionFeeHigh = void 0;
|
|
4
4
|
const chains_1 = require("../../configs/chains");
|
|
5
5
|
const factors_1 = require("../../configs/factors");
|
|
6
6
|
const tokens_1 = require("../../configs/tokens");
|
|
@@ -57,6 +57,13 @@ function estimateRelayerGasLimit({ gasLimits, tokenPermitsCount, feeSwapsCount,
|
|
|
57
57
|
return relayParamsGasLimit + transactionPayloadGasLimit + l1GasLimit;
|
|
58
58
|
}
|
|
59
59
|
exports.estimateRelayerGasLimit = estimateRelayerGasLimit;
|
|
60
|
+
const MIN_GAS_LIMIT = 22000n;
|
|
61
|
+
const GAS_BUFFER_BPS = 1000n; // 10%
|
|
62
|
+
function applyGasBuffer(gasLimit) {
|
|
63
|
+
const clamped = gasLimit < MIN_GAS_LIMIT ? MIN_GAS_LIMIT : gasLimit;
|
|
64
|
+
return clamped + (clamped * GAS_BUFFER_BPS) / 10000n;
|
|
65
|
+
}
|
|
66
|
+
exports.applyGasBuffer = applyGasBuffer;
|
|
60
67
|
function approximateL1GasBuffer({ l1Reference, sizeOfData, }) {
|
|
61
68
|
const evaluated = Math.round((Number(l1Reference.gasLimit) * Math.log(Number(sizeOfData))) / Math.log(Number(l1Reference.sizeOfData)));
|
|
62
69
|
const l1GasLimit = Math.abs(evaluated) < Infinity ? BigInt(evaluated) : l1Reference.gasLimit;
|
|
@@ -5,7 +5,6 @@ const bigmath_1 = require("../bigmath");
|
|
|
5
5
|
const markets_1 = require("../markets");
|
|
6
6
|
const numbers_1 = require("../numbers");
|
|
7
7
|
const tokens_1 = require("../tokens");
|
|
8
|
-
const tradeHistory_1 = require("../tradeHistory");
|
|
9
8
|
function getPriceImpactByAcceptablePrice(p) {
|
|
10
9
|
const { sizeDeltaUsd, acceptablePrice, indexPrice: markPrice, isLong, isIncrease } = p;
|
|
11
10
|
const shouldFlipPriceDiff = isIncrease ? !isLong : isLong;
|
|
@@ -362,7 +361,7 @@ function applyImpactFactor(diff, factor, exponent) {
|
|
|
362
361
|
if (!isFinite(powered) || isNaN(powered)) {
|
|
363
362
|
return 0n;
|
|
364
363
|
}
|
|
365
|
-
let result =
|
|
364
|
+
let result = BigInt(Math.round(powered));
|
|
366
365
|
result = (result * factor) / (0, numbers_1.expandDecimals)(1, 30);
|
|
367
366
|
return result;
|
|
368
367
|
}
|
|
@@ -3,31 +3,104 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.HttpClient = void 0;
|
|
6
|
+
exports.HttpClient = exports.HttpError = void 0;
|
|
7
7
|
const cross_fetch_1 = __importDefault(require("cross-fetch"));
|
|
8
8
|
const buildUrl_1 = require("../buildUrl");
|
|
9
|
+
const DEFAULT_TIMEOUT_MS = 30000;
|
|
10
|
+
function bigIntReplacer(_key, value) {
|
|
11
|
+
return typeof value === "bigint" ? value.toString() : value;
|
|
12
|
+
}
|
|
13
|
+
class HttpError extends Error {
|
|
14
|
+
constructor(statusCode, message, body) {
|
|
15
|
+
super(message);
|
|
16
|
+
this.name = "HttpError";
|
|
17
|
+
this.statusCode = statusCode;
|
|
18
|
+
this.code = body?.code ?? body?.error?.code;
|
|
19
|
+
this.traceId = body?.traceId;
|
|
20
|
+
this.body = body;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
exports.HttpError = HttpError;
|
|
24
|
+
function throwHttpError(statusCode, statusText, errorBody) {
|
|
25
|
+
let errorMessage = `HTTP ${statusCode}: ${statusText}`;
|
|
26
|
+
if (errorBody?.message) {
|
|
27
|
+
const msg = typeof errorBody.message === "string" ? errorBody.message : JSON.stringify(errorBody.message);
|
|
28
|
+
errorMessage = `HTTP ${statusCode}: ${msg}`;
|
|
29
|
+
}
|
|
30
|
+
throw new HttpError(statusCode, errorMessage, errorBody);
|
|
31
|
+
}
|
|
9
32
|
class HttpClient {
|
|
10
|
-
constructor(url) {
|
|
33
|
+
constructor(url, timeoutMs = DEFAULT_TIMEOUT_MS) {
|
|
11
34
|
this.url = url;
|
|
35
|
+
this.timeoutMs = timeoutMs;
|
|
12
36
|
}
|
|
13
37
|
async fetchJson(path, opts) {
|
|
14
38
|
const query = opts?.query
|
|
15
39
|
? Object.fromEntries(Object.entries(opts.query).filter(([, value]) => value !== undefined && value !== null))
|
|
16
40
|
: undefined;
|
|
17
41
|
const url = (0, buildUrl_1.buildUrl)(this.url, path, query);
|
|
18
|
-
const
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
42
|
+
const controller = new AbortController();
|
|
43
|
+
const timeoutId = setTimeout(() => controller.abort(), this.timeoutMs);
|
|
44
|
+
try {
|
|
45
|
+
const response = await (0, cross_fetch_1.default)(url, {
|
|
46
|
+
headers: {
|
|
47
|
+
Accept: "application/json",
|
|
48
|
+
},
|
|
49
|
+
signal: controller.signal,
|
|
50
|
+
});
|
|
51
|
+
if (!response.ok) {
|
|
52
|
+
let errorBody;
|
|
53
|
+
try {
|
|
54
|
+
errorBody = await response.json();
|
|
55
|
+
}
|
|
56
|
+
catch {
|
|
57
|
+
// ignore parse errors
|
|
58
|
+
}
|
|
59
|
+
throwHttpError(response.status, response.statusText, errorBody);
|
|
60
|
+
}
|
|
61
|
+
const json = await response.json();
|
|
62
|
+
if (opts?.transform) {
|
|
63
|
+
return opts.transform(json);
|
|
64
|
+
}
|
|
65
|
+
return json;
|
|
66
|
+
}
|
|
67
|
+
finally {
|
|
68
|
+
clearTimeout(timeoutId);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
async postJson(path, body, opts) {
|
|
72
|
+
const url = (0, buildUrl_1.buildUrl)(this.url, path);
|
|
73
|
+
const controller = new AbortController();
|
|
74
|
+
const timeoutId = setTimeout(() => controller.abort(), this.timeoutMs);
|
|
75
|
+
try {
|
|
76
|
+
const response = await (0, cross_fetch_1.default)(url, {
|
|
77
|
+
method: "POST",
|
|
78
|
+
headers: {
|
|
79
|
+
"Content-Type": "application/json",
|
|
80
|
+
Accept: "application/json",
|
|
81
|
+
},
|
|
82
|
+
body: JSON.stringify(body, bigIntReplacer),
|
|
83
|
+
signal: controller.signal,
|
|
84
|
+
});
|
|
85
|
+
if (!response.ok) {
|
|
86
|
+
let errorBody;
|
|
87
|
+
try {
|
|
88
|
+
errorBody = await response.json();
|
|
89
|
+
}
|
|
90
|
+
catch {
|
|
91
|
+
// ignore parse errors
|
|
92
|
+
}
|
|
93
|
+
throwHttpError(response.status, response.statusText, errorBody);
|
|
94
|
+
}
|
|
95
|
+
const json = await response.json();
|
|
96
|
+
if (opts?.transform) {
|
|
97
|
+
return opts.transform(json);
|
|
98
|
+
}
|
|
99
|
+
return json;
|
|
25
100
|
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
return opts.transform(json);
|
|
101
|
+
finally {
|
|
102
|
+
clearTimeout(timeoutId);
|
|
29
103
|
}
|
|
30
|
-
return json;
|
|
31
104
|
}
|
|
32
105
|
}
|
|
33
106
|
exports.HttpClient = HttpClient;
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.HttpClientWithFallback = void 0;
|
|
4
|
+
const http_1 = require("./http");
|
|
5
|
+
const FAILURE_WINDOW_MS = 60000;
|
|
6
|
+
const FAILURES_BEFORE_ROTATION = 3;
|
|
7
|
+
function isServerOrNetworkError(e) {
|
|
8
|
+
if (e instanceof http_1.HttpError) {
|
|
9
|
+
return e.statusCode >= 500;
|
|
10
|
+
}
|
|
11
|
+
return true;
|
|
12
|
+
}
|
|
13
|
+
class HttpClientWithFallback {
|
|
14
|
+
constructor(urls, timeoutMs) {
|
|
15
|
+
this.failureTimestamps = [];
|
|
16
|
+
if (urls.length === 0) {
|
|
17
|
+
throw new Error("At least one URL is required");
|
|
18
|
+
}
|
|
19
|
+
this.clients = urls.map((url) => new http_1.HttpClient(url, timeoutMs));
|
|
20
|
+
this.primaryIndex = Math.floor(Math.random() * this.clients.length);
|
|
21
|
+
}
|
|
22
|
+
get url() {
|
|
23
|
+
return this.clients[this.primaryIndex].url;
|
|
24
|
+
}
|
|
25
|
+
async fetchJson(path, opts) {
|
|
26
|
+
return this.withFallback((client) => client.fetchJson(path, opts));
|
|
27
|
+
}
|
|
28
|
+
async postJson(path, body, opts) {
|
|
29
|
+
return this.withFallback((client) => client.postJson(path, body, opts));
|
|
30
|
+
}
|
|
31
|
+
async withFallback(fn) {
|
|
32
|
+
const order = this.getClientOrder();
|
|
33
|
+
for (let i = 0; i < order.length; i++) {
|
|
34
|
+
try {
|
|
35
|
+
return await fn(order[i]);
|
|
36
|
+
}
|
|
37
|
+
catch (e) {
|
|
38
|
+
if (!isServerOrNetworkError(e) || i === order.length - 1) {
|
|
39
|
+
throw e;
|
|
40
|
+
}
|
|
41
|
+
this.recordFailure(order[i]);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
throw new Error("All endpoints failed");
|
|
45
|
+
}
|
|
46
|
+
getClientOrder() {
|
|
47
|
+
const primary = this.clients[this.primaryIndex];
|
|
48
|
+
const rest = this.clients.filter((_, i) => i !== this.primaryIndex);
|
|
49
|
+
return [primary, ...rest];
|
|
50
|
+
}
|
|
51
|
+
recordFailure(client) {
|
|
52
|
+
if (client !== this.clients[this.primaryIndex])
|
|
53
|
+
return;
|
|
54
|
+
if (this.clients.length <= 1)
|
|
55
|
+
return;
|
|
56
|
+
const now = Date.now();
|
|
57
|
+
this.failureTimestamps.push(now);
|
|
58
|
+
this.failureTimestamps = this.failureTimestamps.filter((t) => t >= now - FAILURE_WINDOW_MS);
|
|
59
|
+
if (this.failureTimestamps.length >= FAILURES_BEFORE_ROTATION) {
|
|
60
|
+
this.primaryIndex = (this.primaryIndex + 1) % this.clients.length;
|
|
61
|
+
this.failureTimestamps = [];
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
exports.HttpClientWithFallback = HttpClientWithFallback;
|
|
@@ -114,6 +114,10 @@ function mockMarketsInfoData(tokensData, marketKeys, overrides = {}) {
|
|
|
114
114
|
shortInterestInTokens: (0, mock_1.usdToToken)(500, indexToken),
|
|
115
115
|
maxPnlFactorForTradersLong: (0, numbers_1.expandDecimals)(1, 30),
|
|
116
116
|
maxPnlFactorForTradersShort: (0, numbers_1.expandDecimals)(1, 30),
|
|
117
|
+
maxPnlFactorForDepositsLong: (0, numbers_1.expandDecimals)(1, 30),
|
|
118
|
+
maxPnlFactorForDepositsShort: (0, numbers_1.expandDecimals)(1, 30),
|
|
119
|
+
maxPnlFactorForWithdrawalsLong: (0, numbers_1.expandDecimals)(1, 30),
|
|
120
|
+
maxPnlFactorForWithdrawalsShort: (0, numbers_1.expandDecimals)(1, 30),
|
|
117
121
|
data: "",
|
|
118
122
|
virtualPoolAmountForLongToken: 0n,
|
|
119
123
|
virtualPoolAmountForShortToken: 0n,
|
|
@@ -114,6 +114,22 @@ function hashMarketConfigKeys(market) {
|
|
|
114
114
|
["bytes32", "bytes32", "address", "bool"],
|
|
115
115
|
[dataStore_1.MAX_PNL_FACTOR_KEY, dataStore_1.MAX_PNL_FACTOR_FOR_TRADERS_KEY, marketAddress, false],
|
|
116
116
|
],
|
|
117
|
+
maxPnlFactorForDepositsLong: [
|
|
118
|
+
["bytes32", "bytes32", "address", "bool"],
|
|
119
|
+
[dataStore_1.MAX_PNL_FACTOR_KEY, dataStore_1.MAX_PNL_FACTOR_FOR_DEPOSITS_KEY, marketAddress, true],
|
|
120
|
+
],
|
|
121
|
+
maxPnlFactorForDepositsShort: [
|
|
122
|
+
["bytes32", "bytes32", "address", "bool"],
|
|
123
|
+
[dataStore_1.MAX_PNL_FACTOR_KEY, dataStore_1.MAX_PNL_FACTOR_FOR_DEPOSITS_KEY, marketAddress, false],
|
|
124
|
+
],
|
|
125
|
+
maxPnlFactorForWithdrawalsLong: [
|
|
126
|
+
["bytes32", "bytes32", "address", "bool"],
|
|
127
|
+
[dataStore_1.MAX_PNL_FACTOR_KEY, dataStore_1.MAX_PNL_FACTOR_FOR_WITHDRAWALS_KEY, marketAddress, true],
|
|
128
|
+
],
|
|
129
|
+
maxPnlFactorForWithdrawalsShort: [
|
|
130
|
+
["bytes32", "bytes32", "address", "bool"],
|
|
131
|
+
[dataStore_1.MAX_PNL_FACTOR_KEY, dataStore_1.MAX_PNL_FACTOR_FOR_WITHDRAWALS_KEY, marketAddress, false],
|
|
132
|
+
],
|
|
117
133
|
positionFeeFactorForBalanceWasImproved: [
|
|
118
134
|
["bytes32", "address", "bool"],
|
|
119
135
|
[dataStore_1.POSITION_FEE_FACTOR_KEY, marketAddress, true],
|
|
@@ -337,6 +337,22 @@ async function buildMarketsConfigsRequest(chainId, { marketsAddresses, }) {
|
|
|
337
337
|
methodName: "getUint",
|
|
338
338
|
params: [prebuiltHashedKeys.maxPnlFactorForTradersShort],
|
|
339
339
|
},
|
|
340
|
+
maxPnlFactorForDepositsLong: {
|
|
341
|
+
methodName: "getUint",
|
|
342
|
+
params: [prebuiltHashedKeys.maxPnlFactorForDepositsLong],
|
|
343
|
+
},
|
|
344
|
+
maxPnlFactorForDepositsShort: {
|
|
345
|
+
methodName: "getUint",
|
|
346
|
+
params: [prebuiltHashedKeys.maxPnlFactorForDepositsShort],
|
|
347
|
+
},
|
|
348
|
+
maxPnlFactorForWithdrawalsLong: {
|
|
349
|
+
methodName: "getUint",
|
|
350
|
+
params: [prebuiltHashedKeys.maxPnlFactorForWithdrawalsLong],
|
|
351
|
+
},
|
|
352
|
+
maxPnlFactorForWithdrawalsShort: {
|
|
353
|
+
methodName: "getUint",
|
|
354
|
+
params: [prebuiltHashedKeys.maxPnlFactorForWithdrawalsShort],
|
|
355
|
+
},
|
|
340
356
|
positionFeeFactorForBalanceWasImproved: {
|
|
341
357
|
methodName: "getUint",
|
|
342
358
|
params: [prebuiltHashedKeys.positionFeeFactorForBalanceWasImproved],
|
|
@@ -490,6 +506,10 @@ function parseMarketsConfigsResponse(res, marketsAddresses) {
|
|
|
490
506
|
maxFundingFactorPerSecond: dataStoreValues.maxFundingFactorPerSecond.returnValues[0],
|
|
491
507
|
maxPnlFactorForTradersLong: dataStoreValues.maxPnlFactorForTradersLong.returnValues[0],
|
|
492
508
|
maxPnlFactorForTradersShort: dataStoreValues.maxPnlFactorForTradersShort.returnValues[0],
|
|
509
|
+
maxPnlFactorForDepositsLong: dataStoreValues.maxPnlFactorForDepositsLong.returnValues[0],
|
|
510
|
+
maxPnlFactorForDepositsShort: dataStoreValues.maxPnlFactorForDepositsShort.returnValues[0],
|
|
511
|
+
maxPnlFactorForWithdrawalsLong: dataStoreValues.maxPnlFactorForWithdrawalsLong.returnValues[0],
|
|
512
|
+
maxPnlFactorForWithdrawalsShort: dataStoreValues.maxPnlFactorForWithdrawalsShort.returnValues[0],
|
|
493
513
|
minCollateralFactor: dataStoreValues.minCollateralFactor.returnValues[0],
|
|
494
514
|
minCollateralFactorForOpenInterestLong: dataStoreValues.minCollateralFactorForOpenInterestLong.returnValues[0],
|
|
495
515
|
minCollateralFactorForOpenInterestShort: dataStoreValues.minCollateralFactorForOpenInterestShort.returnValues[0],
|
|
@@ -103,7 +103,15 @@ function getMaxLeverageByMinCollateralFactor(minCollateralFactor) {
|
|
|
103
103
|
return rounded * factors_1.BASIS_POINTS_DIVISOR;
|
|
104
104
|
}
|
|
105
105
|
exports.getMaxLeverageByMinCollateralFactor = getMaxLeverageByMinCollateralFactor;
|
|
106
|
+
// GOLD/SILVER on-hours: MCF 0.009 (9e27) → 100x UI
|
|
107
|
+
const MCF_ON_HOURS = 9n * 10n ** 27n;
|
|
108
|
+
// GOLD/SILVER off-hours: MCF 0.035 (35e27) → 25x UI
|
|
109
|
+
const MCF_OFF_HOURS = 35n * 10n ** 27n;
|
|
106
110
|
function getMaxAllowedLeverageByMinCollateralFactor(minCollateralFactor) {
|
|
111
|
+
if (minCollateralFactor === MCF_ON_HOURS)
|
|
112
|
+
return 100 * factors_1.BASIS_POINTS_DIVISOR;
|
|
113
|
+
if (minCollateralFactor === MCF_OFF_HOURS)
|
|
114
|
+
return 25 * factors_1.BASIS_POINTS_DIVISOR;
|
|
107
115
|
return getMaxLeverageByMinCollateralFactor(minCollateralFactor) / 2;
|
|
108
116
|
}
|
|
109
117
|
exports.getMaxAllowedLeverageByMinCollateralFactor = getMaxAllowedLeverageByMinCollateralFactor;
|
|
@@ -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 = exports.parseValue = exports.
|
|
3
|
+
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.formatNumberHuman = 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.toBigInt = 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.toBigNumberWithDecimals = exports.roundWithDecimals = void 0;
|
|
5
5
|
const viem_1 = require("viem");
|
|
6
6
|
const bigmath_1 = require("../bigmath");
|
|
7
7
|
exports.USD_DECIMALS = 30;
|
|
@@ -34,6 +34,23 @@ function expandDecimals(n, decimals) {
|
|
|
34
34
|
return BigInt(n) * 10n ** BigInt(decimals);
|
|
35
35
|
}
|
|
36
36
|
exports.expandDecimals = expandDecimals;
|
|
37
|
+
function toBigInt(value) {
|
|
38
|
+
try {
|
|
39
|
+
if (value === undefined || value === null) {
|
|
40
|
+
return undefined;
|
|
41
|
+
}
|
|
42
|
+
if (typeof value === "bigint") {
|
|
43
|
+
return value;
|
|
44
|
+
}
|
|
45
|
+
return BigInt(value);
|
|
46
|
+
}
|
|
47
|
+
catch (e) {
|
|
48
|
+
// eslint-disable-next-line no-console
|
|
49
|
+
console.error("toBigInt error", e);
|
|
50
|
+
return undefined;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
exports.toBigInt = toBigInt;
|
|
37
54
|
function basisPointsToFloat(basisPoints) {
|
|
38
55
|
return (basisPoints * exports.PRECISION) / exports.BASIS_POINTS_DIVISOR_BIGINT;
|
|
39
56
|
}
|
|
@@ -274,11 +291,7 @@ function formatPercentageDisplay(percentage, hideThreshold) {
|
|
|
274
291
|
return `${percentage}%`;
|
|
275
292
|
}
|
|
276
293
|
exports.formatPercentageDisplay = formatPercentageDisplay;
|
|
277
|
-
function
|
|
278
|
-
if (amount === undefined) {
|
|
279
|
-
return "...";
|
|
280
|
-
}
|
|
281
|
-
let n = Number((0, exports.formatAmount)(amount, tokenDecimals));
|
|
294
|
+
function formatNumberHuman(n, showDollar = false, displayDecimals = 1) {
|
|
282
295
|
// For large numbers, we can neglect the decimals to avoid decimals in cases like 9999999.99999
|
|
283
296
|
if (n >= 1000000) {
|
|
284
297
|
n = Math.round(n);
|
|
@@ -297,6 +310,14 @@ function formatAmountHuman(amount, tokenDecimals, showDollar = false, displayDec
|
|
|
297
310
|
}
|
|
298
311
|
return `${isNegative ? "-" : ""}${sign}${absN.toFixed(displayDecimals)}`;
|
|
299
312
|
}
|
|
313
|
+
exports.formatNumberHuman = formatNumberHuman;
|
|
314
|
+
function formatAmountHuman(amount, tokenDecimals, showDollar = false, displayDecimals = 1) {
|
|
315
|
+
if (amount === undefined) {
|
|
316
|
+
return "...";
|
|
317
|
+
}
|
|
318
|
+
let n = Number((0, exports.formatAmount)(amount, tokenDecimals));
|
|
319
|
+
return formatNumberHuman(n, showDollar, displayDecimals);
|
|
320
|
+
}
|
|
300
321
|
exports.formatAmountHuman = formatAmountHuman;
|
|
301
322
|
function formatBalanceAmount(amount, tokenDecimals, tokenSymbol, { showZero = false, toExponential = true, isStable = false, signed = false, } = {}) {
|
|
302
323
|
if (amount === undefined)
|
|
@@ -526,25 +547,6 @@ function toBigNumberWithDecimals(value, decimals) {
|
|
|
526
547
|
}
|
|
527
548
|
}
|
|
528
549
|
exports.toBigNumberWithDecimals = toBigNumberWithDecimals;
|
|
529
|
-
/**
|
|
530
|
-
*
|
|
531
|
-
* @deprecated Use BigInt instead
|
|
532
|
-
*/
|
|
533
|
-
function bigNumberify(n) {
|
|
534
|
-
try {
|
|
535
|
-
if (n === undefined)
|
|
536
|
-
throw new Error("n is undefined");
|
|
537
|
-
if (n === null)
|
|
538
|
-
throw new Error("n is null");
|
|
539
|
-
return BigInt(n);
|
|
540
|
-
}
|
|
541
|
-
catch (e) {
|
|
542
|
-
// eslint-disable-next-line no-console
|
|
543
|
-
console.error("bigNumberify error", e);
|
|
544
|
-
return undefined;
|
|
545
|
-
}
|
|
546
|
-
}
|
|
547
|
-
exports.bigNumberify = bigNumberify;
|
|
548
550
|
const parseValue = (value, tokenDecimals) => {
|
|
549
551
|
const pValue = parseFloat(value);
|
|
550
552
|
if (isNaN(pValue) || !isFinite(pValue)) {
|
|
@@ -557,7 +559,7 @@ const parseValue = (value, tokenDecimals) => {
|
|
|
557
559
|
if (amount > MAX_ALLOWED || amount < -MAX_ALLOWED) {
|
|
558
560
|
return undefined;
|
|
559
561
|
}
|
|
560
|
-
return
|
|
562
|
+
return toBigInt(amount);
|
|
561
563
|
};
|
|
562
564
|
exports.parseValue = parseValue;
|
|
563
565
|
function roundUpDivision(a, b) {
|