@gmx-io/sdk 1.6.3 → 1.6.4
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/README.md +1 -1
- package/build/cjs/scripts/generate-synthetics-abis.js +181 -0
- package/build/cjs/scripts/subsquid-codegen.js +1 -1
- package/build/cjs/src/abis/AbstractSubaccountApprovalNonceable.js +23 -0
- package/build/cjs/src/abis/CustomErrors.js +198 -88
- package/build/cjs/src/abis/ExchangeRouter.js +155 -272
- package/build/cjs/src/abis/GelatoRelayRouter.js +331 -0
- package/build/cjs/src/abis/SimulationRouter.js +583 -0
- package/build/cjs/src/abis/SubaccountGelatoRelayRouter.js +393 -0
- package/build/cjs/src/abis/SyntheticsReader.js +40 -0
- package/build/cjs/src/abis/index.js +4 -24
- package/build/cjs/src/clients/v1/modules/markets/index.js +10 -6
- package/build/cjs/src/clients/v1/modules/orders/utils.js +1 -0
- package/build/cjs/src/clients/v1/modules/orders/utils.spec.js +66 -0
- package/build/cjs/src/clients/v1/modules/positions/positions.js +2 -10
- package/build/cjs/src/clients/v1/modules/positions/positions.spec.js +1 -1
- package/build/cjs/src/clients/v1/modules/trades/trades.js +5 -1
- package/build/cjs/src/clients/v1/simulateExecuteOrder.js +33 -54
- package/build/cjs/src/clients/v1/testUtil.js +1 -1
- package/build/cjs/src/clients/v2/__tests__/editCancel.spec.js +3 -1
- package/build/cjs/src/clients/v2/__tests__/increase.spec.js +8 -6
- package/build/cjs/src/clients/v2/index.js +6 -0
- package/build/cjs/src/codegen/prebuilt/hashedKinkModelMarketRatesKeys.json +0 -26
- package/build/cjs/src/codegen/prebuilt/hashedMarketConfigKeys.json +1692 -561
- package/build/cjs/src/codegen/prebuilt/hashedMarketValuesKeys.json +0 -47
- package/build/cjs/src/codegen/subsquid.js +101 -2
- package/build/cjs/src/configs/__tests__/tokens.spec.js +0 -1
- package/build/cjs/src/configs/api.js +0 -4
- package/build/cjs/src/configs/batch.js +0 -1
- package/build/cjs/src/configs/chainIds.js +1 -2
- package/build/cjs/src/configs/chains.js +2 -57
- package/build/cjs/src/configs/contracts.js +77 -133
- package/build/cjs/src/configs/dataStore.js +23 -8
- package/build/cjs/src/configs/express.js +0 -2
- package/build/cjs/src/configs/gasLimits.js +0 -7
- package/build/cjs/src/configs/markets.js +0 -23
- package/build/cjs/src/configs/multichainTokens.js +1 -1
- package/build/cjs/src/configs/oracleKeeper.js +0 -2
- package/build/cjs/src/configs/tokens.js +2 -104
- package/build/cjs/src/test/fixtures/arbitrumMarkets.json +1210 -0
- package/build/cjs/src/test/fixtures/arbitrumTickers.json +1002 -0
- package/build/cjs/src/test/fixtures/arbitrumTokens.json +136 -0
- package/build/cjs/src/test/mockCrossFetch.js +28 -0
- package/build/cjs/src/utils/__tests__/fees.spec.js +264 -0
- package/build/cjs/src/utils/__tests__/marketsRisk.spec.js +80 -0
- package/build/cjs/src/utils/__tests__/numbers.spec.js +49 -0
- package/build/cjs/src/utils/__tests__/orderTransactions/decreaseOrders.spec.js +6 -25
- package/build/cjs/src/utils/__tests__/orderTransactions/increaseOrders.spec.js +6 -25
- package/build/cjs/src/utils/__tests__/orderTransactions/swapOrders.spec.js +3 -13
- package/build/cjs/src/utils/__tests__/parseError.spec.js +23 -0
- package/build/cjs/src/utils/__tests__/uiFeeReceiver.spec.js +159 -58
- package/build/cjs/src/utils/errors/parseError.js +13 -1
- package/build/cjs/src/utils/errors/transactionsErrors.js +2 -0
- package/build/cjs/src/utils/express/utils/batchOrderUtils.js +1 -2
- package/build/cjs/src/utils/express/utils/estimateExpressParams.js +2 -0
- package/build/cjs/src/utils/express/utils/estimateExpressParams.spec.js +81 -0
- package/build/cjs/src/utils/express/utils/expressParamsUtils.js +5 -1
- package/build/cjs/src/utils/express/utils/expressParamsUtils.spec.js +27 -0
- package/build/cjs/src/utils/fees/executionFee.js +0 -1
- package/build/cjs/src/utils/fees/priceImpact.js +61 -20
- package/build/cjs/src/utils/http/validation.js +33 -0
- package/build/cjs/src/utils/markets/__tests__/api.spec.js +168 -0
- package/build/cjs/src/utils/markets/__tests__/mockMarkets.js +14 -5
- package/build/cjs/src/utils/markets/api.js +45 -3
- package/build/cjs/src/utils/markets/hashKeys.js +34 -6
- package/build/cjs/src/utils/markets/multicall.js +110 -11
- package/build/cjs/src/utils/markets/types.js +23 -0
- package/build/cjs/src/utils/markets/utils.js +15 -1
- package/build/cjs/src/utils/numbers/utils.js +26 -5
- package/build/cjs/src/utils/orderTransactions/simulation.js +13 -0
- package/build/cjs/src/utils/orderTransactions/utils.js +21 -8
- package/build/cjs/src/utils/positions/__tests__/api.spec.js +50 -0
- package/build/cjs/src/utils/positions/api.js +7 -1
- package/build/cjs/src/utils/positions/utils.js +7 -1
- package/build/cjs/src/utils/prices/utils.js +2 -0
- package/build/cjs/src/utils/subaccount/sdkClient.js +0 -1
- package/build/cjs/src/utils/swap/buildSwapStrategy.js +6 -123
- package/build/cjs/src/utils/swap/index.js +0 -3
- package/build/cjs/src/utils/swap/swapValues.js +0 -67
- package/build/cjs/src/utils/tokens/utils.js +9 -10
- package/build/cjs/src/utils/trade/__tests__/getMaxWithdrawAmount.spec.js +44 -0
- package/build/cjs/src/utils/trade/decrease.js +47 -23
- package/build/cjs/src/utils/trade/increase.js +22 -6
- package/build/cjs/src/utils/trade/types.js +0 -1
- package/build/cjs/src/utils/tradeHistory/types.js +3 -1
- package/build/cjs/src/utils/tradeHistory/utils.js +7 -0
- package/build/cjs/src/utils/twap/uiFeeReceiver.js +134 -47
- package/build/esm/scripts/generate-synthetics-abis.js +153 -0
- package/build/esm/scripts/subsquid-codegen.js +1 -1
- package/build/esm/src/abis/AbstractSubaccountApprovalNonceable.js +21 -0
- package/build/esm/src/abis/CustomErrors.js +198 -88
- package/build/esm/src/abis/ExchangeRouter.js +155 -272
- package/build/esm/src/abis/GelatoRelayRouter.js +331 -0
- package/build/esm/src/abis/SimulationRouter.js +581 -0
- package/build/esm/src/abis/SubaccountGelatoRelayRouter.js +393 -0
- package/build/esm/src/abis/SyntheticsReader.js +40 -0
- package/build/esm/src/abis/index.js +3 -23
- package/build/esm/src/clients/v1/modules/markets/index.js +10 -6
- package/build/esm/src/clients/v1/modules/orders/utils.js +1 -0
- package/build/esm/src/clients/v1/modules/orders/utils.spec.js +64 -0
- package/build/esm/src/clients/v1/modules/positions/positions.js +2 -10
- package/build/esm/src/clients/v1/modules/positions/positions.spec.js +1 -1
- package/build/esm/src/clients/v1/modules/trades/trades.js +5 -1
- package/build/esm/src/clients/v1/simulateExecuteOrder.js +35 -55
- package/build/esm/src/clients/v1/testUtil.js +1 -1
- package/build/esm/src/clients/v2/__tests__/editCancel.spec.js +3 -1
- package/build/esm/src/clients/v2/__tests__/increase.spec.js +8 -6
- package/build/esm/src/clients/v2/__tests__/swap.spec.js +1 -1
- package/build/esm/src/clients/v2/index.js +7 -1
- package/build/esm/src/codegen/prebuilt/hashedKinkModelMarketRatesKeys.json +0 -26
- package/build/esm/src/codegen/prebuilt/hashedMarketConfigKeys.json +1692 -561
- package/build/esm/src/codegen/prebuilt/hashedMarketValuesKeys.json +0 -47
- package/build/esm/src/codegen/subsquid.js +99 -0
- package/build/esm/src/configs/__tests__/multichainTokens.spec.js +1 -1
- package/build/esm/src/configs/__tests__/tokens.spec.js +1 -2
- package/build/esm/src/configs/api.js +1 -5
- package/build/esm/src/configs/batch.js +1 -2
- package/build/esm/src/configs/chainIds.js +0 -1
- package/build/esm/src/configs/chains.js +3 -57
- package/build/esm/src/configs/contracts.js +76 -133
- package/build/esm/src/configs/dataStore.js +16 -4
- package/build/esm/src/configs/express.js +1 -3
- package/build/esm/src/configs/gasLimits.js +1 -8
- package/build/esm/src/configs/markets.js +1 -24
- package/build/esm/src/configs/multichainTokens.js +1 -1
- package/build/esm/src/configs/oracleKeeper.js +1 -3
- package/build/esm/src/configs/tokens.js +3 -105
- package/build/esm/src/test/fixtures/arbitrumMarkets.json +1210 -0
- package/build/esm/src/test/fixtures/arbitrumTickers.json +1002 -0
- package/build/esm/src/test/fixtures/arbitrumTokens.json +136 -0
- package/build/esm/src/test/mockCrossFetch.js +23 -0
- package/build/esm/src/utils/__tests__/fees.spec.js +266 -2
- package/build/esm/src/utils/__tests__/marketsRisk.spec.js +78 -0
- package/build/esm/src/utils/__tests__/numbers.spec.js +50 -1
- package/build/esm/src/utils/__tests__/orderTransactions/decreaseOrders.spec.js +6 -25
- package/build/esm/src/utils/__tests__/orderTransactions/increaseOrders.spec.js +6 -25
- package/build/esm/src/utils/__tests__/orderTransactions/swapOrders.spec.js +3 -13
- package/build/esm/src/utils/__tests__/parseError.spec.js +25 -2
- package/build/esm/src/utils/__tests__/uiFeeReceiver.spec.js +161 -60
- package/build/esm/src/utils/errors/parseError.js +12 -1
- package/build/esm/src/utils/errors/transactionsErrors.js +2 -0
- package/build/esm/src/utils/express/utils/batchOrderUtils.js +2 -3
- package/build/esm/src/utils/express/utils/estimateExpressParams.js +2 -0
- package/build/esm/src/utils/express/utils/estimateExpressParams.spec.js +79 -0
- package/build/esm/src/utils/express/utils/expressParamsUtils.js +3 -0
- package/build/esm/src/utils/express/utils/expressParamsUtils.spec.js +25 -0
- package/build/esm/src/utils/fees/executionFee.js +0 -1
- package/build/esm/src/utils/fees/priceImpact.js +54 -13
- package/build/esm/src/utils/http/validation.js +28 -0
- package/build/esm/src/utils/markets/__tests__/api.spec.js +166 -0
- package/build/esm/src/utils/markets/__tests__/mockMarkets.js +15 -6
- package/build/esm/src/utils/markets/api.js +42 -2
- package/build/esm/src/utils/markets/hashKeys.js +35 -7
- package/build/esm/src/utils/markets/multicall.js +108 -11
- package/build/esm/src/utils/markets/types.js +22 -1
- package/build/esm/src/utils/markets/utils.js +13 -0
- package/build/esm/src/utils/numbers/utils.js +21 -3
- package/build/esm/src/utils/orderTransactions/simulation.js +9 -0
- package/build/esm/src/utils/orderTransactions/utils.js +22 -9
- package/build/esm/src/utils/positions/__tests__/api.spec.js +48 -0
- package/build/esm/src/utils/positions/api.js +7 -1
- package/build/esm/src/utils/positions/utils.js +7 -1
- package/build/esm/src/utils/prices/utils.js +2 -0
- package/build/esm/src/utils/subaccount/sdkClient.js +0 -1
- package/build/esm/src/utils/swap/buildSwapStrategy.js +7 -124
- package/build/esm/src/utils/swap/index.js +0 -3
- package/build/esm/src/utils/swap/swapValues.js +1 -68
- package/build/esm/src/utils/tokens/utils.js +8 -8
- package/build/esm/src/utils/trade/__tests__/getMaxWithdrawAmount.spec.js +45 -1
- package/build/esm/src/utils/trade/decrease.js +44 -22
- package/build/esm/src/utils/trade/increase.js +23 -7
- package/build/esm/src/utils/trade/types.js +0 -1
- package/build/esm/src/utils/tradeHistory/types.js +2 -0
- package/build/esm/src/utils/tradeHistory/utils.js +7 -0
- package/build/esm/src/utils/twap/uiFeeReceiver.js +127 -42
- package/build/tsconfig.tsbuildinfo +1 -1
- package/build/types/scripts/generate-synthetics-abis.d.ts +17 -0
- package/build/types/src/abis/AbstractSubaccountApprovalNonceable.d.ts +16 -0
- package/build/types/src/abis/CustomErrors.d.ts +331 -139
- package/build/types/src/abis/ExchangeRouter.d.ts +121 -209
- package/build/types/src/abis/GelatoRelayRouter.d.ts +258 -0
- package/build/types/src/abis/SimulationRouter.d.ts +448 -0
- package/build/types/src/abis/SubaccountGelatoRelayRouter.d.ts +307 -0
- package/build/types/src/abis/SyntheticsReader.d.ts +32 -0
- package/build/types/src/abis/index.d.ts +8108 -7891
- package/build/types/src/clients/v1/index.d.ts +1 -1
- package/build/types/src/clients/v1/modules/base.d.ts +1 -1
- package/build/types/src/clients/v1/modules/orders/utils.spec.d.ts +1 -0
- package/build/types/src/clients/v1/multicall.d.ts +1 -1
- package/build/types/src/clients/v1/simulateExecuteOrder.d.ts +0 -1
- package/build/types/src/clients/v2/index.d.ts +2 -0
- package/build/types/src/codegen/prebuilt/index.d.ts +1692 -634
- package/build/types/src/codegen/subsquid.d.ts +368 -0
- package/build/types/src/configs/chainIds.d.ts +0 -1
- package/build/types/src/configs/chains.d.ts +9 -34
- package/build/types/src/configs/contracts.d.ts +5 -66
- package/build/types/src/configs/dataStore.d.ts +8 -2
- package/build/types/src/configs/multichainTokens.d.ts +1 -1
- package/build/types/src/test/mockCrossFetch.d.ts +2 -0
- package/build/types/src/utils/__tests__/marketsRisk.spec.d.ts +1 -0
- package/build/types/src/utils/errors/parseError.d.ts +1 -0
- package/build/types/src/utils/errors/transactionsErrors.d.ts +2 -0
- package/build/types/src/utils/express/types.d.ts +2 -1
- package/build/types/src/utils/express/utils/batchOrderUtils.d.ts +1 -1
- package/build/types/src/utils/express/utils/estimateExpressParams.spec.d.ts +1 -0
- package/build/types/src/utils/express/utils/expressParamsUtils.d.ts +1 -0
- package/build/types/src/utils/express/utils/expressParamsUtils.spec.d.ts +1 -0
- package/build/types/src/utils/express/utils/globalExpressParams.d.ts +1 -1
- package/build/types/src/utils/fees/priceImpact.d.ts +2 -0
- package/build/types/src/utils/http/validation.d.ts +7 -0
- package/build/types/src/utils/markets/__tests__/api.spec.d.ts +1 -0
- package/build/types/src/utils/markets/api.d.ts +7 -1
- package/build/types/src/utils/markets/hashKeys.d.ts +1 -1
- package/build/types/src/utils/markets/multicall.d.ts +13 -2
- package/build/types/src/utils/markets/types.d.ts +18 -4
- package/build/types/src/utils/markets/utils.d.ts +2 -1
- package/build/types/src/utils/numbers/utils.d.ts +5 -1
- package/build/types/src/utils/orderTransactions/simulation.d.ts +1 -0
- package/build/types/src/utils/orders/types.d.ts +2 -0
- package/build/types/src/utils/positions/__tests__/api.spec.d.ts +1 -0
- package/build/types/src/utils/positions/types.d.ts +1 -0
- package/build/types/src/utils/positions/utils.d.ts +1 -0
- package/build/types/src/utils/prices/utils.d.ts +1 -0
- package/build/types/src/utils/swap/buildSwapStrategy.d.ts +3 -5
- package/build/types/src/utils/swap/index.d.ts +0 -3
- package/build/types/src/utils/swap/swapValues.d.ts +1 -1
- package/build/types/src/utils/swap/types.d.ts +2 -7
- package/build/types/src/utils/tokens/types.d.ts +0 -1
- package/build/types/src/utils/tokens/utils.d.ts +1 -2
- package/build/types/src/utils/trade/decrease.d.ts +11 -0
- package/build/types/src/utils/trade/types.d.ts +2 -8
- package/build/types/src/utils/tradeHistory/types.d.ts +7 -0
- package/build/types/src/utils/twap/types.d.ts +1 -1
- package/build/types/src/utils/twap/uiFeeReceiver.d.ts +23 -22
- package/package.json +10 -7
- package/build/cjs/src/abis/StBTC.js +0 -513
- package/build/cjs/src/utils/swap/botanixStaking.js +0 -90
- package/build/cjs/src/utils/swap/externalSwapPath.js +0 -41
- package/build/cjs/src/utils/swap/externalSwapQuoteByPath.js +0 -27
- package/build/esm/src/abis/StBTC.js +0 -511
- package/build/esm/src/utils/swap/botanixStaking.js +0 -83
- package/build/esm/src/utils/swap/externalSwapPath.js +0 -37
- package/build/esm/src/utils/swap/externalSwapQuoteByPath.js +0 -23
- package/build/types/src/abis/StBTC.d.ts +0 -932
- package/build/types/src/utils/swap/botanixStaking.d.ts +0 -11
- package/build/types/src/utils/swap/externalSwapPath.d.ts +0 -13
- package/build/types/src/utils/swap/externalSwapQuoteByPath.d.ts +0 -6
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const vitest_1 = require("vitest");
|
|
4
|
+
const estimateExpressParams_1 = require("./estimateExpressParams");
|
|
5
|
+
const TOKEN_ADDRESS = "0x0000000000000000000000000000000000000001";
|
|
6
|
+
function makeGasPaymentToken(balances) {
|
|
7
|
+
return {
|
|
8
|
+
address: TOKEN_ADDRESS,
|
|
9
|
+
symbol: "USDC",
|
|
10
|
+
decimals: 6,
|
|
11
|
+
...balances,
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
const GAS_AMOUNT = 1000000n;
|
|
15
|
+
const BUFFERED_GAS_AMOUNT = 1300000n;
|
|
16
|
+
const ALLOWANCE = { [TOKEN_ADDRESS]: 10000000000n };
|
|
17
|
+
(0, vitest_1.describe)("getGasPaymentValidations", () => {
|
|
18
|
+
(0, vitest_1.it)("flags an unloaded wallet balance as out-of-balance but not loaded, so submit stays blocked without confirming insufficiency", () => {
|
|
19
|
+
const validations = (0, estimateExpressParams_1.getGasPaymentValidations)({
|
|
20
|
+
gasPaymentToken: makeGasPaymentToken({ walletBalance: undefined }),
|
|
21
|
+
gasPaymentTokenAmount: GAS_AMOUNT,
|
|
22
|
+
gasPaymentTokenAsCollateralAmount: 0n,
|
|
23
|
+
gasPaymentAllowanceData: ALLOWANCE,
|
|
24
|
+
tokenPermits: [],
|
|
25
|
+
isGmxAccount: false,
|
|
26
|
+
});
|
|
27
|
+
(0, vitest_1.expect)(validations.isGasPaymentTokenBalanceLoaded).toBe(false);
|
|
28
|
+
(0, vitest_1.expect)(validations.isOutGasTokenBalance).toBe(true);
|
|
29
|
+
(0, vitest_1.expect)(validations.isValid).toBe(false);
|
|
30
|
+
});
|
|
31
|
+
(0, vitest_1.it)("confirms insufficiency when the loaded balance can't cover buffered gas", () => {
|
|
32
|
+
const validations = (0, estimateExpressParams_1.getGasPaymentValidations)({
|
|
33
|
+
gasPaymentToken: makeGasPaymentToken({ walletBalance: BUFFERED_GAS_AMOUNT - 1n }),
|
|
34
|
+
gasPaymentTokenAmount: GAS_AMOUNT,
|
|
35
|
+
gasPaymentTokenAsCollateralAmount: 0n,
|
|
36
|
+
gasPaymentAllowanceData: ALLOWANCE,
|
|
37
|
+
tokenPermits: [],
|
|
38
|
+
isGmxAccount: false,
|
|
39
|
+
});
|
|
40
|
+
(0, vitest_1.expect)(validations.isGasPaymentTokenBalanceLoaded).toBe(true);
|
|
41
|
+
(0, vitest_1.expect)(validations.isOutGasTokenBalance).toBe(true);
|
|
42
|
+
(0, vitest_1.expect)(validations.isValid).toBe(false);
|
|
43
|
+
});
|
|
44
|
+
(0, vitest_1.it)("passes when the loaded balance covers buffered gas plus the collateral overlap", () => {
|
|
45
|
+
const collateralAmount = 5000000n;
|
|
46
|
+
const validations = (0, estimateExpressParams_1.getGasPaymentValidations)({
|
|
47
|
+
gasPaymentToken: makeGasPaymentToken({ walletBalance: BUFFERED_GAS_AMOUNT + collateralAmount }),
|
|
48
|
+
gasPaymentTokenAmount: GAS_AMOUNT,
|
|
49
|
+
gasPaymentTokenAsCollateralAmount: collateralAmount,
|
|
50
|
+
gasPaymentAllowanceData: ALLOWANCE,
|
|
51
|
+
tokenPermits: [],
|
|
52
|
+
isGmxAccount: false,
|
|
53
|
+
});
|
|
54
|
+
(0, vitest_1.expect)(validations.isGasPaymentTokenBalanceLoaded).toBe(true);
|
|
55
|
+
(0, vitest_1.expect)(validations.isOutGasTokenBalance).toBe(false);
|
|
56
|
+
(0, vitest_1.expect)(validations.isValid).toBe(true);
|
|
57
|
+
});
|
|
58
|
+
(0, vitest_1.it)("checks gmxAccountBalance and ignores walletBalance when isGmxAccount is true", () => {
|
|
59
|
+
const unloaded = (0, estimateExpressParams_1.getGasPaymentValidations)({
|
|
60
|
+
gasPaymentToken: makeGasPaymentToken({ walletBalance: 10000000n, gmxAccountBalance: undefined }),
|
|
61
|
+
gasPaymentTokenAmount: GAS_AMOUNT,
|
|
62
|
+
gasPaymentTokenAsCollateralAmount: 0n,
|
|
63
|
+
gasPaymentAllowanceData: undefined,
|
|
64
|
+
tokenPermits: [],
|
|
65
|
+
isGmxAccount: true,
|
|
66
|
+
});
|
|
67
|
+
(0, vitest_1.expect)(unloaded.isGasPaymentTokenBalanceLoaded).toBe(false);
|
|
68
|
+
(0, vitest_1.expect)(unloaded.isOutGasTokenBalance).toBe(true);
|
|
69
|
+
const loaded = (0, estimateExpressParams_1.getGasPaymentValidations)({
|
|
70
|
+
gasPaymentToken: makeGasPaymentToken({ walletBalance: 0n, gmxAccountBalance: BUFFERED_GAS_AMOUNT }),
|
|
71
|
+
gasPaymentTokenAmount: GAS_AMOUNT,
|
|
72
|
+
gasPaymentTokenAsCollateralAmount: 0n,
|
|
73
|
+
gasPaymentAllowanceData: undefined,
|
|
74
|
+
tokenPermits: [],
|
|
75
|
+
isGmxAccount: true,
|
|
76
|
+
});
|
|
77
|
+
(0, vitest_1.expect)(loaded.isGasPaymentTokenBalanceLoaded).toBe(true);
|
|
78
|
+
(0, vitest_1.expect)(loaded.isOutGasTokenBalance).toBe(false);
|
|
79
|
+
(0, vitest_1.expect)(loaded.isValid).toBe(true);
|
|
80
|
+
});
|
|
81
|
+
});
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getIsValidExpressParams = exports.ExpressEstimationInsufficientGasPaymentTokenBalanceError = exports.ExpressEstimationError = void 0;
|
|
3
|
+
exports.getIsConfirmedOutOfGasPaymentTokenBalance = exports.getIsValidExpressParams = exports.ExpressEstimationInsufficientGasPaymentTokenBalanceError = exports.ExpressEstimationError = void 0;
|
|
4
4
|
class ExpressEstimationError extends Error {
|
|
5
5
|
constructor() {
|
|
6
6
|
super(...arguments);
|
|
@@ -20,3 +20,7 @@ function getIsValidExpressParams({ gasPaymentValidations, }) {
|
|
|
20
20
|
return gasPaymentValidations.isValid;
|
|
21
21
|
}
|
|
22
22
|
exports.getIsValidExpressParams = getIsValidExpressParams;
|
|
23
|
+
function getIsConfirmedOutOfGasPaymentTokenBalance(gasPaymentValidations) {
|
|
24
|
+
return Boolean(gasPaymentValidations?.isGasPaymentTokenBalanceLoaded && gasPaymentValidations.isOutGasTokenBalance);
|
|
25
|
+
}
|
|
26
|
+
exports.getIsConfirmedOutOfGasPaymentTokenBalance = getIsConfirmedOutOfGasPaymentTokenBalance;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const vitest_1 = require("vitest");
|
|
4
|
+
const expressParamsUtils_1 = require("./expressParamsUtils");
|
|
5
|
+
(0, vitest_1.describe)("getIsConfirmedOutOfGasPaymentTokenBalance", () => {
|
|
6
|
+
(0, vitest_1.it)("does not confirm insufficiency while the balance has not loaded yet", () => {
|
|
7
|
+
(0, vitest_1.expect)((0, expressParamsUtils_1.getIsConfirmedOutOfGasPaymentTokenBalance)({
|
|
8
|
+
isOutGasTokenBalance: true,
|
|
9
|
+
isGasPaymentTokenBalanceLoaded: false,
|
|
10
|
+
})).toBe(false);
|
|
11
|
+
});
|
|
12
|
+
(0, vitest_1.it)("confirms insufficiency once the balance is loaded and too low", () => {
|
|
13
|
+
(0, vitest_1.expect)((0, expressParamsUtils_1.getIsConfirmedOutOfGasPaymentTokenBalance)({
|
|
14
|
+
isOutGasTokenBalance: true,
|
|
15
|
+
isGasPaymentTokenBalanceLoaded: true,
|
|
16
|
+
})).toBe(true);
|
|
17
|
+
});
|
|
18
|
+
(0, vitest_1.it)("does not confirm when the loaded balance is sufficient", () => {
|
|
19
|
+
(0, vitest_1.expect)((0, expressParamsUtils_1.getIsConfirmedOutOfGasPaymentTokenBalance)({
|
|
20
|
+
isOutGasTokenBalance: false,
|
|
21
|
+
isGasPaymentTokenBalanceLoaded: true,
|
|
22
|
+
})).toBe(false);
|
|
23
|
+
});
|
|
24
|
+
(0, vitest_1.it)("does not confirm when validations are missing", () => {
|
|
25
|
+
(0, vitest_1.expect)((0, expressParamsUtils_1.getIsConfirmedOutOfGasPaymentTokenBalance)(undefined)).toBe(false);
|
|
26
|
+
});
|
|
27
|
+
});
|
|
@@ -28,7 +28,6 @@ tokensData, estimatedGasLimit, gasPrice, oraclePriceCount, numberOfParts) {
|
|
|
28
28
|
const multiplierFactor = gasLimits.estimatedFeeMultiplierFactor;
|
|
29
29
|
const gasLimit = baseGasLimit + (0, numbers_1.applyFactor)(estimatedGasLimit, multiplierFactor);
|
|
30
30
|
// #endregion
|
|
31
|
-
// avoid botanix gas spikes when chain is not actively used
|
|
32
31
|
const minGasCostUsd = (0, chains_1.getMinExecutionFeeUsd)(chainId);
|
|
33
32
|
const minGasCost = (0, tokens_2.convertToTokenAmount)(minGasCostUsd, nativeToken.decimals, nativeToken.prices.minPrice);
|
|
34
33
|
let feeTokenAmountPerExecution = gasLimit * gasPrice;
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.getMaxNegativeImpactBps = exports.getCappedPriceImpactPercentageFromFees = exports.applyImpactFactor = exports.calculateImpactForCrossoverRebalance = exports.calculateImpactForSameSideRebalance = exports.getPriceImpactUsd = exports.getNextPoolAmountsParams = exports.getPriceImpactForSwap = exports.getProportionalPendingImpactValues = exports.getPriceImpactForPosition = exports.getMaxPositionImpactFactors = exports.capPositionImpactUsdByMaxPriceImpactFactor = exports.capPositionImpactUsdByMaxImpactPool = exports.getCappedPositionImpactUsd = exports.applySwapImpactWithCap = exports.getPriceImpactByAcceptablePrice = void 0;
|
|
4
|
+
const viem_1 = require("viem");
|
|
4
5
|
const bigmath_1 = require("../bigmath");
|
|
5
6
|
const markets_1 = require("../markets");
|
|
6
7
|
const numbers_1 = require("../numbers");
|
|
7
|
-
const
|
|
8
|
+
const utils_1 = require("../tokens/utils");
|
|
8
9
|
function getPriceImpactByAcceptablePrice(p) {
|
|
9
10
|
const { sizeDeltaUsd, acceptablePrice, indexPrice: markPrice, isLong, isIncrease } = p;
|
|
10
11
|
const shouldFlipPriceDiff = isIncrease ? !isLong : isLong;
|
|
@@ -31,7 +32,7 @@ function applySwapImpactWithCap(marketInfo, token, priceImpactDeltaUsd) {
|
|
|
31
32
|
let cappedDiffUsd = 0n;
|
|
32
33
|
if (priceImpactDeltaUsd > 0) {
|
|
33
34
|
// round positive impactAmount down, this will be deducted from the swap impact pool for the user
|
|
34
|
-
impactDeltaAmount = (0,
|
|
35
|
+
impactDeltaAmount = (0, utils_1.convertToTokenAmount)(priceImpactDeltaUsd, token.decimals, price);
|
|
35
36
|
const maxImpactAmount = isLongCollateral
|
|
36
37
|
? marketInfo.swapImpactPoolAmountLong
|
|
37
38
|
: marketInfo.swapImpactPoolAmountShort;
|
|
@@ -66,7 +67,7 @@ function capPositionImpactUsdByMaxImpactPool(marketInfo, positionImpactDeltaUsd)
|
|
|
66
67
|
}
|
|
67
68
|
const { indexToken } = marketInfo;
|
|
68
69
|
const impactPoolAmount = marketInfo.positionImpactPoolAmount;
|
|
69
|
-
const maxPriceImpactUsdBasedOnImpactPool = (0,
|
|
70
|
+
const maxPriceImpactUsdBasedOnImpactPool = (0, utils_1.convertToUsd)(impactPoolAmount, indexToken.decimals, indexToken.prices.minPrice);
|
|
70
71
|
if (positionImpactDeltaUsd > maxPriceImpactUsdBasedOnImpactPool) {
|
|
71
72
|
positionImpactDeltaUsd = maxPriceImpactUsdBasedOnImpactPool;
|
|
72
73
|
}
|
|
@@ -95,10 +96,28 @@ exports.getMaxPositionImpactFactors = getMaxPositionImpactFactors;
|
|
|
95
96
|
function getPriceImpactForPosition(marketInfo, sizeDeltaUsd, isLong, opts = {}) {
|
|
96
97
|
const longInterestUsd = (0, markets_1.getOpenInterestForBalance)(marketInfo, true);
|
|
97
98
|
const shortInterestUsd = (0, markets_1.getOpenInterestForBalance)(marketInfo, false);
|
|
99
|
+
let effectiveUsdDelta = sizeDeltaUsd;
|
|
100
|
+
if (marketInfo.useOpenInterestInTokensForBalance) {
|
|
101
|
+
if (sizeDeltaUsd < 0n && opts.sizeDeltaInTokens === undefined) {
|
|
102
|
+
throw new Error(`Missing sizeDeltaInTokens for token-OI decrease market ${marketInfo.marketTokenAddress}`);
|
|
103
|
+
}
|
|
104
|
+
const midPrice = (0, utils_1.getMidPrice)(marketInfo.indexToken.prices);
|
|
105
|
+
const sizeDeltaInTokens = opts.sizeDeltaInTokens ??
|
|
106
|
+
(midPrice > 0n
|
|
107
|
+
? (0, utils_1.convertToTokenAmountForIncrease)(sizeDeltaUsd, marketInfo.indexToken.decimals, isLong ? marketInfo.indexToken.prices.maxPrice : marketInfo.indexToken.prices.minPrice, isLong)
|
|
108
|
+
: 0n);
|
|
109
|
+
effectiveUsdDelta = (0, utils_1.convertToUsd)(sizeDeltaInTokens, marketInfo.indexToken.decimals, midPrice) ?? 0n;
|
|
110
|
+
if (sizeDeltaUsd < 0n && effectiveUsdDelta > 0n) {
|
|
111
|
+
effectiveUsdDelta = -effectiveUsdDelta;
|
|
112
|
+
}
|
|
113
|
+
else if (sizeDeltaUsd > 0n && effectiveUsdDelta < 0n) {
|
|
114
|
+
effectiveUsdDelta = -effectiveUsdDelta;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
98
117
|
const { currentLongUsd, currentShortUsd, nextLongUsd, nextShortUsd } = getNextOpenInterestParams({
|
|
99
118
|
currentLongUsd: longInterestUsd,
|
|
100
119
|
currentShortUsd: shortInterestUsd,
|
|
101
|
-
usdDelta:
|
|
120
|
+
usdDelta: effectiveUsdDelta,
|
|
102
121
|
isLong: isLong,
|
|
103
122
|
});
|
|
104
123
|
const { priceImpactDeltaUsd, balanceWasImproved } = getPriceImpactUsd({
|
|
@@ -112,24 +131,25 @@ function getPriceImpactForPosition(marketInfo, sizeDeltaUsd, isLong, opts = {})
|
|
|
112
131
|
exponentFactorNegative: marketInfo.positionImpactExponentFactorNegative,
|
|
113
132
|
fallbackToZero: opts.fallbackToZero,
|
|
114
133
|
});
|
|
115
|
-
if (priceImpactDeltaUsd
|
|
134
|
+
if (priceImpactDeltaUsd >= 0n) {
|
|
116
135
|
return {
|
|
117
136
|
priceImpactDeltaUsd,
|
|
118
137
|
balanceWasImproved,
|
|
119
138
|
};
|
|
120
139
|
}
|
|
121
|
-
|
|
140
|
+
const { hasVirtualInventory, virtualInventory } = getVirtualInventoryForPositionImpact(marketInfo);
|
|
141
|
+
if (!hasVirtualInventory) {
|
|
122
142
|
return {
|
|
123
143
|
priceImpactDeltaUsd,
|
|
124
144
|
balanceWasImproved,
|
|
125
145
|
};
|
|
126
146
|
}
|
|
127
147
|
const virtualInventoryParams = getNextOpenInterestForVirtualInventory({
|
|
128
|
-
virtualInventory
|
|
129
|
-
usdDelta:
|
|
148
|
+
virtualInventory,
|
|
149
|
+
usdDelta: effectiveUsdDelta,
|
|
130
150
|
isLong: isLong,
|
|
131
151
|
});
|
|
132
|
-
const { priceImpactDeltaUsd: priceImpactUsdForVirtualInventory } = getPriceImpactUsd({
|
|
152
|
+
const { priceImpactDeltaUsd: priceImpactUsdForVirtualInventory, balanceWasImproved: virtualBalanceWasImproved } = getPriceImpactUsd({
|
|
133
153
|
currentLongUsd: virtualInventoryParams.currentLongUsd,
|
|
134
154
|
currentShortUsd: virtualInventoryParams.currentShortUsd,
|
|
135
155
|
nextLongUsd: virtualInventoryParams.nextLongUsd,
|
|
@@ -140,19 +160,40 @@ function getPriceImpactForPosition(marketInfo, sizeDeltaUsd, isLong, opts = {})
|
|
|
140
160
|
exponentFactorNegative: marketInfo.positionImpactExponentFactorNegative,
|
|
141
161
|
fallbackToZero: opts.fallbackToZero,
|
|
142
162
|
});
|
|
163
|
+
if (priceImpactUsdForVirtualInventory < priceImpactDeltaUsd) {
|
|
164
|
+
return {
|
|
165
|
+
priceImpactDeltaUsd: priceImpactUsdForVirtualInventory,
|
|
166
|
+
balanceWasImproved: virtualBalanceWasImproved,
|
|
167
|
+
};
|
|
168
|
+
}
|
|
143
169
|
return {
|
|
144
|
-
priceImpactDeltaUsd
|
|
145
|
-
? priceImpactUsdForVirtualInventory
|
|
146
|
-
: priceImpactDeltaUsd,
|
|
170
|
+
priceImpactDeltaUsd,
|
|
147
171
|
balanceWasImproved,
|
|
148
172
|
};
|
|
149
173
|
}
|
|
150
174
|
exports.getPriceImpactForPosition = getPriceImpactForPosition;
|
|
175
|
+
function getVirtualInventoryForPositionImpact(marketInfo) {
|
|
176
|
+
const hasConfiguredVirtualInventory = typeof marketInfo.virtualIndexTokenId === "string" ? marketInfo.virtualIndexTokenId !== viem_1.zeroHash : undefined;
|
|
177
|
+
if (hasConfiguredVirtualInventory === false) {
|
|
178
|
+
return { hasVirtualInventory: false, virtualInventory: 0n };
|
|
179
|
+
}
|
|
180
|
+
let virtualInventory = marketInfo.virtualInventoryForPositions;
|
|
181
|
+
if (marketInfo.useOpenInterestInTokensForBalance) {
|
|
182
|
+
if (typeof marketInfo.virtualInventoryForPositionsInTokens !== "bigint") {
|
|
183
|
+
throw new Error(`Missing virtualInventoryForPositionsInTokens for token-OI market ${marketInfo.marketTokenAddress}`);
|
|
184
|
+
}
|
|
185
|
+
virtualInventory = (0, utils_1.convertToUsd)(marketInfo.virtualInventoryForPositionsInTokens, marketInfo.indexToken.decimals, (0, utils_1.getMidPrice)(marketInfo.indexToken.prices));
|
|
186
|
+
}
|
|
187
|
+
return {
|
|
188
|
+
hasVirtualInventory: hasConfiguredVirtualInventory ?? virtualInventory !== 0n,
|
|
189
|
+
virtualInventory,
|
|
190
|
+
};
|
|
191
|
+
}
|
|
151
192
|
function getProportionalPendingImpactValues({ sizeInUsd, pendingImpactAmount, sizeDeltaUsd, indexToken, }) {
|
|
152
193
|
const proportionalPendingImpactDeltaAmount = sizeDeltaUsd !== 0n && sizeInUsd !== 0n
|
|
153
194
|
? bigmath_1.bigMath.mulDiv(pendingImpactAmount, sizeDeltaUsd, sizeInUsd, pendingImpactAmount < 0n)
|
|
154
195
|
: 0n;
|
|
155
|
-
const proportionalPendingImpactDeltaUsd = (0,
|
|
196
|
+
const proportionalPendingImpactDeltaUsd = (0, utils_1.convertToUsd)(proportionalPendingImpactDeltaAmount, indexToken.decimals, proportionalPendingImpactDeltaAmount > 0 ? indexToken.prices.minPrice : indexToken.prices.maxPrice);
|
|
156
197
|
return { proportionalPendingImpactDeltaAmount, proportionalPendingImpactDeltaUsd };
|
|
157
198
|
}
|
|
158
199
|
exports.getProportionalPendingImpactValues = getProportionalPendingImpactValues;
|
|
@@ -253,10 +294,10 @@ function getNextOpenInterestParams(p) {
|
|
|
253
294
|
let nextLongUsd = currentLongUsd;
|
|
254
295
|
let nextShortUsd = currentShortUsd;
|
|
255
296
|
if (isLong) {
|
|
256
|
-
nextLongUsd =
|
|
297
|
+
nextLongUsd = usdDelta < 0n && -usdDelta > currentLongUsd ? 0n : currentLongUsd + usdDelta;
|
|
257
298
|
}
|
|
258
299
|
else {
|
|
259
|
-
nextShortUsd =
|
|
300
|
+
nextShortUsd = usdDelta < 0n && -usdDelta > currentShortUsd ? 0n : currentShortUsd + usdDelta;
|
|
260
301
|
}
|
|
261
302
|
return {
|
|
262
303
|
currentLongUsd,
|
|
@@ -267,10 +308,10 @@ function getNextOpenInterestParams(p) {
|
|
|
267
308
|
}
|
|
268
309
|
function getNextPoolAmountsParams(p) {
|
|
269
310
|
const { longToken, shortToken, longPoolAmount, shortPoolAmount, longDeltaUsd, shortDeltaUsd } = p;
|
|
270
|
-
const longPrice = (0,
|
|
271
|
-
const shortPrice = (0,
|
|
272
|
-
const longPoolUsd = (0,
|
|
273
|
-
const shortPoolUsd = (0,
|
|
311
|
+
const longPrice = (0, utils_1.getMidPrice)(longToken.prices);
|
|
312
|
+
const shortPrice = (0, utils_1.getMidPrice)(shortToken.prices);
|
|
313
|
+
const longPoolUsd = (0, utils_1.convertToUsd)(longPoolAmount, longToken.decimals, longPrice);
|
|
314
|
+
const shortPoolUsd = (0, utils_1.convertToUsd)(shortPoolAmount, shortToken.decimals, shortPrice);
|
|
274
315
|
const nextLongPoolUsd = longPoolUsd + longDeltaUsd;
|
|
275
316
|
const nextShortPoolUsd = shortPoolUsd + shortDeltaUsd;
|
|
276
317
|
return {
|
|
@@ -299,7 +340,7 @@ function getPriceImpactUsd(p) {
|
|
|
299
340
|
}
|
|
300
341
|
const currentDiff = bigmath_1.bigMath.abs(p.currentLongUsd - p.currentShortUsd);
|
|
301
342
|
const nextDiff = bigmath_1.bigMath.abs(nextLongUsd - nextShortUsd);
|
|
302
|
-
const isSameSideRebalance = p.currentLongUsd
|
|
343
|
+
const isSameSideRebalance = p.currentLongUsd <= p.currentShortUsd === nextLongUsd <= nextShortUsd;
|
|
303
344
|
const balanceWasImproved = nextDiff < currentDiff;
|
|
304
345
|
let priceImpactDeltaUsd;
|
|
305
346
|
if (isSameSideRebalance) {
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.assertApiFields = exports.assertApiRecords = void 0;
|
|
4
|
+
function assertApiRecords(value, endpoint) {
|
|
5
|
+
if (!Array.isArray(value)) {
|
|
6
|
+
throw new Error(`Invalid ${endpoint} response: expected an array`);
|
|
7
|
+
}
|
|
8
|
+
for (let index = 0; index < value.length; index++) {
|
|
9
|
+
const record = value[index];
|
|
10
|
+
if (!record || typeof record !== "object" || Array.isArray(record)) {
|
|
11
|
+
throw new Error(`Invalid ${endpoint} response at index ${index}: expected an object`);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
exports.assertApiRecords = assertApiRecords;
|
|
16
|
+
function assertApiFields(record, fields, endpoint, index) {
|
|
17
|
+
const identifier = typeof record.marketTokenAddress === "string"
|
|
18
|
+
? ` for market ${record.marketTokenAddress}`
|
|
19
|
+
: typeof record.key === "string"
|
|
20
|
+
? ` for ${record.key}`
|
|
21
|
+
: ` at index ${index}`;
|
|
22
|
+
for (const field of fields) {
|
|
23
|
+
if (!Object.prototype.hasOwnProperty.call(record, field.name)) {
|
|
24
|
+
throw new Error(`Invalid ${endpoint} response${identifier}: missing ${field.name}`);
|
|
25
|
+
}
|
|
26
|
+
const value = record[field.name];
|
|
27
|
+
const hasExpectedType = field.type === "numberOrNull" ? value === null || typeof value === "number" : typeof value === field.type;
|
|
28
|
+
if (!hasExpectedType) {
|
|
29
|
+
throw new Error(`Invalid ${endpoint} response${identifier}: ${field.name} must be ${field.type}`);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
exports.assertApiFields = assertApiFields;
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const vitest_1 = require("vitest");
|
|
4
|
+
const api_1 = require("../api");
|
|
5
|
+
const MARKET_ADDRESS = "0x1111111111111111111111111111111111111111";
|
|
6
|
+
const VIRTUAL_INDEX_TOKEN_ID = "0x2222222222222222222222222222222222222222222222222222222222222222";
|
|
7
|
+
const CONFIG_FIELD_CASES = [
|
|
8
|
+
["marketTokenAddress", 1, "string"],
|
|
9
|
+
["useOpenInterestInTokensForBalance", "true", "boolean"],
|
|
10
|
+
["maxCollateralSumLongTokenLong", "invalid", "bigint"],
|
|
11
|
+
["maxCollateralSumLongTokenShort", "invalid", "bigint"],
|
|
12
|
+
["maxCollateralSumShortTokenLong", "invalid", "bigint"],
|
|
13
|
+
["maxCollateralSumShortTokenShort", "invalid", "bigint"],
|
|
14
|
+
["minFundingIncreaseRatePerSecond", "invalid", "bigint"],
|
|
15
|
+
["minFundingFactorPerSecondLong", "invalid", "bigint"],
|
|
16
|
+
["minFundingFactorPerSecondShort", "invalid", "bigint"],
|
|
17
|
+
["maxFundingFactorPerSecondLong", "invalid", "bigint"],
|
|
18
|
+
["maxFundingFactorPerSecondShort", "invalid", "bigint"],
|
|
19
|
+
["virtualIndexTokenId", 1, "string"],
|
|
20
|
+
];
|
|
21
|
+
const VALUES_FIELD_CASES = [
|
|
22
|
+
["marketTokenAddress", 1, "string"],
|
|
23
|
+
["virtualInventoryForPositions", "invalid", "bigint"],
|
|
24
|
+
["virtualInventoryForPositionsInTokens", "invalid", "bigint"],
|
|
25
|
+
["updatedAt", "invalid", "numberOrNull"],
|
|
26
|
+
];
|
|
27
|
+
function createApi(response) {
|
|
28
|
+
return {
|
|
29
|
+
url: "https://example.test",
|
|
30
|
+
fetchJson: async () => response,
|
|
31
|
+
postJson: async () => {
|
|
32
|
+
throw new Error("Unexpected POST request");
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
function createMarketConfig(overrides = {}) {
|
|
37
|
+
return {
|
|
38
|
+
marketTokenAddress: MARKET_ADDRESS,
|
|
39
|
+
useOpenInterestInTokensForBalance: true,
|
|
40
|
+
maxCollateralSumLongTokenLong: "1",
|
|
41
|
+
maxCollateralSumLongTokenShort: "2",
|
|
42
|
+
maxCollateralSumShortTokenLong: "3",
|
|
43
|
+
maxCollateralSumShortTokenShort: "4",
|
|
44
|
+
minFundingIncreaseRatePerSecond: "5",
|
|
45
|
+
minFundingFactorPerSecondLong: "6",
|
|
46
|
+
minFundingFactorPerSecondShort: "7",
|
|
47
|
+
maxFundingFactorPerSecondLong: "8",
|
|
48
|
+
maxFundingFactorPerSecondShort: "9",
|
|
49
|
+
virtualIndexTokenId: VIRTUAL_INDEX_TOKEN_ID,
|
|
50
|
+
...overrides,
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
function createMarketValues(virtualInventoryForPositionsInTokens, overrides = {}) {
|
|
54
|
+
return {
|
|
55
|
+
marketTokenAddress: MARKET_ADDRESS,
|
|
56
|
+
virtualInventoryForPositions: "-10",
|
|
57
|
+
virtualInventoryForPositionsInTokens,
|
|
58
|
+
updatedAt: 123,
|
|
59
|
+
...overrides,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
function createMarketInfo(virtualInventoryForPositionsInTokens = "-20") {
|
|
63
|
+
const marketInfo = {
|
|
64
|
+
...createMarketConfig(),
|
|
65
|
+
...createMarketValues(virtualInventoryForPositionsInTokens),
|
|
66
|
+
};
|
|
67
|
+
delete marketInfo.updatedAt;
|
|
68
|
+
return marketInfo;
|
|
69
|
+
}
|
|
70
|
+
function omitField(record, field) {
|
|
71
|
+
const result = { ...record };
|
|
72
|
+
delete result[field];
|
|
73
|
+
return result;
|
|
74
|
+
}
|
|
75
|
+
(0, vitest_1.describe)("market API validation", () => {
|
|
76
|
+
(0, vitest_1.describe)("fetchApiMarketsValues", () => {
|
|
77
|
+
vitest_1.it.each([
|
|
78
|
+
["negative", "-123", -123n],
|
|
79
|
+
["zero", "0", 0n],
|
|
80
|
+
["positive", "123", 123n],
|
|
81
|
+
])("deserializes %s signed token inventory", async (_label, serialized, expected) => {
|
|
82
|
+
const result = await (0, api_1.fetchApiMarketsValues)({
|
|
83
|
+
api: createApi([createMarketValues(serialized)]),
|
|
84
|
+
});
|
|
85
|
+
(0, vitest_1.expect)(result[0].virtualInventoryForPositions).toBe(-10n);
|
|
86
|
+
(0, vitest_1.expect)(result[0].virtualInventoryForPositionsInTokens).toBe(expected);
|
|
87
|
+
(0, vitest_1.expect)(result[0].updatedAt).toBe(123);
|
|
88
|
+
});
|
|
89
|
+
(0, vitest_1.it)("accepts a null freshness timestamp", async () => {
|
|
90
|
+
const result = await (0, api_1.fetchApiMarketsValues)({
|
|
91
|
+
api: createApi([createMarketValues("0", { updatedAt: null })]),
|
|
92
|
+
});
|
|
93
|
+
(0, vitest_1.expect)(result[0].updatedAt).toBeNull();
|
|
94
|
+
});
|
|
95
|
+
vitest_1.it.each(VALUES_FIELD_CASES)("rejects a missing %s field", async (field) => {
|
|
96
|
+
const response = omitField(createMarketValues("0"), field);
|
|
97
|
+
await (0, vitest_1.expect)((0, api_1.fetchApiMarketsValues)({ api: createApi([response]) })).rejects.toThrow(`missing ${field}`);
|
|
98
|
+
});
|
|
99
|
+
vitest_1.it.each(VALUES_FIELD_CASES)("rejects malformed %s", async (field, invalidValue, expectedType) => {
|
|
100
|
+
const response = createMarketValues("0", { [field]: invalidValue });
|
|
101
|
+
await (0, vitest_1.expect)((0, api_1.fetchApiMarketsValues)({ api: createApi([response]) })).rejects.toThrow(`${field} must be ${expectedType}`);
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
(0, vitest_1.describe)("fetchApiMarketsConfig", () => {
|
|
105
|
+
(0, vitest_1.it)("deserializes required v2.2c bigint fields", async () => {
|
|
106
|
+
const result = await (0, api_1.fetchApiMarketsConfig)({
|
|
107
|
+
api: createApi([createMarketConfig()]),
|
|
108
|
+
});
|
|
109
|
+
(0, vitest_1.expect)(result[0]).toMatchObject({
|
|
110
|
+
maxCollateralSumLongTokenLong: 1n,
|
|
111
|
+
maxCollateralSumLongTokenShort: 2n,
|
|
112
|
+
maxCollateralSumShortTokenLong: 3n,
|
|
113
|
+
maxCollateralSumShortTokenShort: 4n,
|
|
114
|
+
minFundingIncreaseRatePerSecond: 5n,
|
|
115
|
+
minFundingFactorPerSecondLong: 6n,
|
|
116
|
+
minFundingFactorPerSecondShort: 7n,
|
|
117
|
+
maxFundingFactorPerSecondLong: 8n,
|
|
118
|
+
maxFundingFactorPerSecondShort: 9n,
|
|
119
|
+
virtualIndexTokenId: VIRTUAL_INDEX_TOKEN_ID,
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
vitest_1.it.each(CONFIG_FIELD_CASES)("rejects a missing %s field", async (field) => {
|
|
123
|
+
const response = omitField(createMarketConfig(), field);
|
|
124
|
+
await (0, vitest_1.expect)((0, api_1.fetchApiMarketsConfig)({ api: createApi([response]) })).rejects.toThrow(`missing ${field}`);
|
|
125
|
+
});
|
|
126
|
+
vitest_1.it.each(CONFIG_FIELD_CASES)("rejects malformed %s", async (field, invalidValue, expectedType) => {
|
|
127
|
+
const response = createMarketConfig({ [field]: invalidValue });
|
|
128
|
+
await (0, vitest_1.expect)((0, api_1.fetchApiMarketsConfig)({ api: createApi([response]) })).rejects.toThrow(`${field} must be ${expectedType}`);
|
|
129
|
+
});
|
|
130
|
+
});
|
|
131
|
+
(0, vitest_1.describe)("fetchApiMarketsInfo", () => {
|
|
132
|
+
(0, vitest_1.it)("validates and deserializes the combined market response", async () => {
|
|
133
|
+
const result = await (0, api_1.fetchApiMarketsInfo)({
|
|
134
|
+
api: createApi([createMarketInfo("-20")]),
|
|
135
|
+
});
|
|
136
|
+
(0, vitest_1.expect)(result[0].virtualInventoryForPositions).toBe(-10n);
|
|
137
|
+
(0, vitest_1.expect)(result[0].virtualInventoryForPositionsInTokens).toBe(-20n);
|
|
138
|
+
(0, vitest_1.expect)(result[0].maxCollateralSumLongTokenLong).toBe(1n);
|
|
139
|
+
(0, vitest_1.expect)(result[0].virtualIndexTokenId).toBe(VIRTUAL_INDEX_TOKEN_ID);
|
|
140
|
+
});
|
|
141
|
+
vitest_1.it.each(["virtualIndexTokenId", "virtualInventoryForPositionsInTokens"])("rejects a combined response missing %s", async (field) => {
|
|
142
|
+
const response = omitField(createMarketInfo(), field);
|
|
143
|
+
await (0, vitest_1.expect)((0, api_1.fetchApiMarketsInfo)({ api: createApi([response]) })).rejects.toThrow(`missing ${field}`);
|
|
144
|
+
});
|
|
145
|
+
vitest_1.it.each([
|
|
146
|
+
["virtualIndexTokenId", 1, "string"],
|
|
147
|
+
["virtualInventoryForPositionsInTokens", "invalid", "bigint"],
|
|
148
|
+
])("rejects malformed %s in the combined response", async (field, invalidValue, expectedType) => {
|
|
149
|
+
const response = createMarketInfo();
|
|
150
|
+
response[field] = invalidValue;
|
|
151
|
+
await (0, vitest_1.expect)((0, api_1.fetchApiMarketsInfo)({ api: createApi([response]) })).rejects.toThrow(`${field} must be ${expectedType}`);
|
|
152
|
+
});
|
|
153
|
+
});
|
|
154
|
+
vitest_1.it.each([
|
|
155
|
+
["info", api_1.fetchApiMarketsInfo],
|
|
156
|
+
["config", api_1.fetchApiMarketsConfig],
|
|
157
|
+
["values", api_1.fetchApiMarketsValues],
|
|
158
|
+
])("rejects a non-array /markets/%s response", async (_endpoint, fetcher) => {
|
|
159
|
+
await (0, vitest_1.expect)(fetcher({ api: createApi({}) })).rejects.toThrow("expected an array");
|
|
160
|
+
});
|
|
161
|
+
vitest_1.it.each([
|
|
162
|
+
["info", api_1.fetchApiMarketsInfo],
|
|
163
|
+
["config", api_1.fetchApiMarketsConfig],
|
|
164
|
+
["values", api_1.fetchApiMarketsValues],
|
|
165
|
+
])("rejects a malformed record in the /markets/%s array", async (_endpoint, fetcher) => {
|
|
166
|
+
await (0, vitest_1.expect)(fetcher({ api: createApi([null]) })).rejects.toThrow("at index 0: expected an object");
|
|
167
|
+
});
|
|
168
|
+
});
|
|
@@ -60,6 +60,10 @@ function mockMarketsInfoData(tokensData, marketKeys, overrides = {}) {
|
|
|
60
60
|
openInterestReserveFactorShort: (0, numbers_1.expandDecimals)(5, 29),
|
|
61
61
|
maxOpenInterestLong: (0, numbers_1.expandDecimals)(5, 29),
|
|
62
62
|
maxOpenInterestShort: (0, numbers_1.expandDecimals)(5, 29),
|
|
63
|
+
maxCollateralSumLongTokenLong: 0n,
|
|
64
|
+
maxCollateralSumLongTokenShort: 0n,
|
|
65
|
+
maxCollateralSumShortTokenLong: 0n,
|
|
66
|
+
maxCollateralSumShortTokenShort: 0n,
|
|
63
67
|
positionImpactPoolAmount: (0, mock_1.usdToToken)(1000, indexToken),
|
|
64
68
|
positionImpactPoolDistributionRate: 0n,
|
|
65
69
|
minPositionImpactPoolAmount: 0n,
|
|
@@ -94,9 +98,12 @@ function mockMarketsInfoData(tokensData, marketKeys, overrides = {}) {
|
|
|
94
98
|
fundingFactor: 0n,
|
|
95
99
|
fundingExponentFactor: 0n,
|
|
96
100
|
fundingIncreaseFactorPerSecond: 0n,
|
|
101
|
+
minFundingIncreaseRatePerSecond: 0n,
|
|
97
102
|
fundingDecreaseFactorPerSecond: 0n,
|
|
98
|
-
|
|
99
|
-
|
|
103
|
+
maxFundingFactorPerSecondLong: 0n,
|
|
104
|
+
maxFundingFactorPerSecondShort: 0n,
|
|
105
|
+
minFundingFactorPerSecondLong: 0n,
|
|
106
|
+
minFundingFactorPerSecondShort: 0n,
|
|
100
107
|
thresholdForDecreaseFunding: 0n,
|
|
101
108
|
thresholdForStableFunding: 0n,
|
|
102
109
|
totalBorrowingFees: 0n,
|
|
@@ -122,9 +129,11 @@ function mockMarketsInfoData(tokensData, marketKeys, overrides = {}) {
|
|
|
122
129
|
virtualPoolAmountForLongToken: 0n,
|
|
123
130
|
virtualPoolAmountForShortToken: 0n,
|
|
124
131
|
virtualInventoryForPositions: 0n,
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
132
|
+
virtualInventoryForPositionsInTokens: 0n,
|
|
133
|
+
virtualMarketId: viem_1.zeroHash,
|
|
134
|
+
virtualIndexTokenId: viem_1.zeroHash,
|
|
135
|
+
virtualLongTokenId: viem_1.zeroHash,
|
|
136
|
+
virtualShortTokenId: viem_1.zeroHash,
|
|
128
137
|
...(overrides[key] || {}),
|
|
129
138
|
};
|
|
130
139
|
return acc;
|
|
@@ -1,12 +1,54 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.fetchApiMarketsTickers = exports.fetchApiMarkets = exports.fetchApiTokensData = exports.fetchApiMarketsInfo = void 0;
|
|
3
|
+
exports.fetchApiMarketsTickers = exports.fetchApiMarkets = exports.fetchApiTokensData = exports.fetchApiMarketsValues = exports.fetchApiMarketsConfig = exports.fetchApiMarketsInfo = void 0;
|
|
4
|
+
const validation_1 = require("../http/validation");
|
|
4
5
|
const numbers_1 = require("../numbers");
|
|
6
|
+
const MARKET_CONFIG_V22C_FIELDS = [
|
|
7
|
+
{ name: "marketTokenAddress", type: "string" },
|
|
8
|
+
{ name: "useOpenInterestInTokensForBalance", type: "boolean" },
|
|
9
|
+
{ name: "maxCollateralSumLongTokenLong", type: "bigint" },
|
|
10
|
+
{ name: "maxCollateralSumLongTokenShort", type: "bigint" },
|
|
11
|
+
{ name: "maxCollateralSumShortTokenLong", type: "bigint" },
|
|
12
|
+
{ name: "maxCollateralSumShortTokenShort", type: "bigint" },
|
|
13
|
+
{ name: "minFundingIncreaseRatePerSecond", type: "bigint" },
|
|
14
|
+
{ name: "minFundingFactorPerSecondLong", type: "bigint" },
|
|
15
|
+
{ name: "minFundingFactorPerSecondShort", type: "bigint" },
|
|
16
|
+
{ name: "maxFundingFactorPerSecondLong", type: "bigint" },
|
|
17
|
+
{ name: "maxFundingFactorPerSecondShort", type: "bigint" },
|
|
18
|
+
{ name: "virtualIndexTokenId", type: "string" },
|
|
19
|
+
];
|
|
20
|
+
const MARKET_VALUES_V22C_FIELDS = [
|
|
21
|
+
{ name: "marketTokenAddress", type: "string" },
|
|
22
|
+
{ name: "virtualInventoryForPositions", type: "bigint" },
|
|
23
|
+
{ name: "virtualInventoryForPositionsInTokens", type: "bigint" },
|
|
24
|
+
{ name: "updatedAt", type: "numberOrNull" },
|
|
25
|
+
];
|
|
26
|
+
function parseApiRecords(raw, endpoint, fields) {
|
|
27
|
+
(0, validation_1.assertApiRecords)(raw, endpoint);
|
|
28
|
+
return raw.map((record, index) => {
|
|
29
|
+
const parsed = (0, numbers_1.deserializeBigIntsInObject)(record, { handleInts: true });
|
|
30
|
+
(0, validation_1.assertApiFields)(parsed, fields, endpoint, index);
|
|
31
|
+
return parsed;
|
|
32
|
+
});
|
|
33
|
+
}
|
|
5
34
|
async function fetchApiMarketsInfo(ctx) {
|
|
6
|
-
const
|
|
7
|
-
return
|
|
35
|
+
const marketInfos = await ctx.api.fetchJson("/v1/markets/info");
|
|
36
|
+
return parseApiRecords(marketInfos, "/v1/markets/info", [
|
|
37
|
+
...MARKET_CONFIG_V22C_FIELDS,
|
|
38
|
+
...MARKET_VALUES_V22C_FIELDS.filter((field) => field.name !== "updatedAt"),
|
|
39
|
+
]);
|
|
8
40
|
}
|
|
9
41
|
exports.fetchApiMarketsInfo = fetchApiMarketsInfo;
|
|
42
|
+
async function fetchApiMarketsConfig(ctx) {
|
|
43
|
+
const configs = await ctx.api.fetchJson("/v1/markets/config");
|
|
44
|
+
return parseApiRecords(configs, "/v1/markets/config", MARKET_CONFIG_V22C_FIELDS);
|
|
45
|
+
}
|
|
46
|
+
exports.fetchApiMarketsConfig = fetchApiMarketsConfig;
|
|
47
|
+
async function fetchApiMarketsValues(ctx) {
|
|
48
|
+
const values = await ctx.api.fetchJson("/v1/markets/values");
|
|
49
|
+
return parseApiRecords(values, "/v1/markets/values", MARKET_VALUES_V22C_FIELDS);
|
|
50
|
+
}
|
|
51
|
+
exports.fetchApiMarketsValues = fetchApiMarketsValues;
|
|
10
52
|
async function fetchApiTokensData(ctx) {
|
|
11
53
|
const tInfos = await ctx.api.fetchJson("/v1/tokens/info");
|
|
12
54
|
return tInfos.map((tInfo) => (0, numbers_1.deserializeBigIntsInObject)(tInfo, { handleInts: true }));
|