@gmx-io/sdk 1.6.3 → 1.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +49 -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__/data.spec.js +5 -5
- package/build/cjs/src/clients/v2/__tests__/decrease.spec.js +6 -6
- package/build/cjs/src/clients/v2/__tests__/editCancel.spec.js +15 -13
- package/build/cjs/src/clients/v2/__tests__/errors.spec.js +1 -47
- package/build/cjs/src/clients/v2/__tests__/errorsLifecycle.spec.js +53 -0
- package/build/cjs/src/clients/v2/__tests__/globalCleanup.js +80 -0
- package/build/cjs/src/clients/v2/__tests__/increase.spec.js +22 -20
- package/build/cjs/src/clients/v2/__tests__/multichain.spec.js +5 -6
- package/build/cjs/src/clients/v2/__tests__/prepareErrors.spec.js +167 -0
- package/build/cjs/src/clients/v2/__tests__/setupAllowance.js +32 -0
- package/build/cjs/src/clients/v2/__tests__/subaccount.spec.js +16 -123
- package/build/cjs/src/clients/v2/__tests__/subaccountLifecycle.spec.js +108 -0
- package/build/cjs/src/clients/v2/__tests__/testUtil.js +38 -5
- package/build/cjs/src/clients/v2/__tests__/tradingCapacity.spec.js +371 -0
- package/build/cjs/src/clients/v2/index.js +12 -1
- 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 +377 -14
- package/build/cjs/src/configs/__tests__/markets.spec.js +8 -0
- 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__/markets.spec.js +60 -0
- package/build/cjs/src/utils/__tests__/marketsRisk.spec.js +80 -0
- package/build/cjs/src/utils/__tests__/numbers.spec.js +88 -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__/positionPnlCap.spec.js +78 -0
- package/build/cjs/src/utils/__tests__/positions.spec.js +16 -4
- 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 +134 -4
- package/build/cjs/src/utils/markets/hashKeys.js +34 -6
- package/build/cjs/src/utils/markets/multicall.js +114 -17
- package/build/cjs/src/utils/markets/types.js +31 -0
- package/build/cjs/src/utils/markets/utils.js +81 -9
- package/build/cjs/src/utils/numbers/utils.js +52 -12
- package/build/cjs/src/utils/orderTransactions/api.js +160 -2
- 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 +8 -2
- 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/__tests__/utils.spec.js +64 -0
- package/build/cjs/src/utils/tradeHistory/types.js +3 -1
- package/build/cjs/src/utils/tradeHistory/utils.js +9 -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__/data.spec.js +6 -6
- package/build/esm/src/clients/v2/__tests__/decrease.spec.js +7 -7
- package/build/esm/src/clients/v2/__tests__/editCancel.spec.js +16 -14
- package/build/esm/src/clients/v2/__tests__/errors.spec.js +2 -48
- package/build/esm/src/clients/v2/__tests__/errorsLifecycle.spec.js +51 -0
- package/build/esm/src/clients/v2/__tests__/globalCleanup.js +77 -0
- package/build/esm/src/clients/v2/__tests__/increase.spec.js +23 -21
- package/build/esm/src/clients/v2/__tests__/multichain.spec.js +6 -7
- package/build/esm/src/clients/v2/__tests__/prepareErrors.spec.js +165 -0
- package/build/esm/src/clients/v2/__tests__/setupAllowance.js +30 -0
- package/build/esm/src/clients/v2/__tests__/subaccount.spec.js +17 -124
- package/build/esm/src/clients/v2/__tests__/subaccountLifecycle.spec.js +106 -0
- package/build/esm/src/clients/v2/__tests__/swap.spec.js +1 -1
- package/build/esm/src/clients/v2/__tests__/testUtil.js +33 -4
- package/build/esm/src/clients/v2/__tests__/tradingCapacity.spec.js +369 -0
- package/build/esm/src/clients/v2/index.js +12 -2
- 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 +375 -12
- package/build/esm/src/configs/__tests__/markets.spec.js +9 -1
- 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__/markets.spec.js +61 -1
- package/build/esm/src/utils/__tests__/marketsRisk.spec.js +78 -0
- package/build/esm/src/utils/__tests__/numbers.spec.js +89 -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__/positionPnlCap.spec.js +76 -0
- package/build/esm/src/utils/__tests__/positions.spec.js +17 -5
- 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 +130 -4
- package/build/esm/src/utils/markets/hashKeys.js +35 -7
- package/build/esm/src/utils/markets/multicall.js +112 -17
- package/build/esm/src/utils/markets/types.js +30 -1
- package/build/esm/src/utils/markets/utils.js +78 -8
- package/build/esm/src/utils/numbers/utils.js +45 -10
- package/build/esm/src/utils/orderTransactions/api.js +157 -1
- 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 +9 -3
- 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/__tests__/utils.spec.js +62 -0
- package/build/esm/src/utils/tradeHistory/types.js +2 -0
- package/build/esm/src/utils/tradeHistory/utils.js +9 -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/__tests__/errorsLifecycle.spec.d.ts +1 -0
- package/build/types/src/clients/v2/__tests__/globalCleanup.d.ts +1 -0
- package/build/types/src/clients/v2/__tests__/prepareErrors.spec.d.ts +1 -0
- package/build/types/src/clients/v2/__tests__/setupAllowance.d.ts +1 -0
- package/build/types/src/clients/v2/__tests__/subaccountLifecycle.spec.d.ts +1 -0
- package/build/types/src/clients/v2/__tests__/testUtil.d.ts +7 -1
- package/build/types/src/clients/v2/__tests__/tradingCapacity.spec.d.ts +1 -0
- package/build/types/src/clients/v2/index.d.ts +10 -5
- package/build/types/src/codegen/prebuilt/index.d.ts +1692 -634
- package/build/types/src/codegen/subsquid.d.ts +1490 -32
- 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/__tests__/positionPnlCap.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 +12 -2
- 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 +45 -4
- package/build/types/src/utils/markets/utils.d.ts +9 -9
- package/build/types/src/utils/numbers/utils.d.ts +7 -1
- package/build/types/src/utils/orderTransactions/api.d.ts +50 -0
- 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/__tests__/utils.spec.d.ts +1 -0
- package/build/types/src/utils/tradeHistory/types.d.ts +9 -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 +11 -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
|
@@ -1,9 +1,59 @@
|
|
|
1
1
|
import { HASHED_MARKET_CONFIG_KEYS, HASHED_MARKET_VALUES_KEYS } from "../../codegen/prebuilt";
|
|
2
2
|
import { getContract } from "../../configs/contracts";
|
|
3
|
-
import { CLAIMABLE_FUNDING_AMOUNT, MAX_PNL_FACTOR_FOR_TRADERS_KEY } from "../../configs/dataStore";
|
|
3
|
+
import { CLAIMABLE_FUNDING_AMOUNT, MAX_PNL_FACTOR_FOR_TRADERS_KEY, maxCollateralSumKey, virtualInventoryForPositionsInTokensKey, virtualTokenIdKey, } from "../../configs/dataStore";
|
|
4
4
|
import { hashDataMap } from "../hash";
|
|
5
5
|
import { getContractMarketPrices, getOiInTokensFromRawValues, getOiUsdFromRawValues } from "./";
|
|
6
6
|
import { getByKey } from "../objects";
|
|
7
|
+
export function buildMarketsRiskValuesRequest(chainId, markets) {
|
|
8
|
+
const dataStoreAddress = getContract(chainId, "DataStore");
|
|
9
|
+
return markets.reduce((request, market) => {
|
|
10
|
+
const marketAddress = market.marketTokenAddress;
|
|
11
|
+
request[marketAddress] = {
|
|
12
|
+
contractAddress: dataStoreAddress,
|
|
13
|
+
abiId: "DataStore",
|
|
14
|
+
calls: {
|
|
15
|
+
maxCollateralSumLongTokenLong: {
|
|
16
|
+
methodName: "getUint",
|
|
17
|
+
params: [maxCollateralSumKey(marketAddress, market.longTokenAddress, true)],
|
|
18
|
+
},
|
|
19
|
+
maxCollateralSumLongTokenShort: {
|
|
20
|
+
methodName: "getUint",
|
|
21
|
+
params: [maxCollateralSumKey(marketAddress, market.longTokenAddress, false)],
|
|
22
|
+
},
|
|
23
|
+
maxCollateralSumShortTokenLong: {
|
|
24
|
+
methodName: "getUint",
|
|
25
|
+
params: [maxCollateralSumKey(marketAddress, market.shortTokenAddress, true)],
|
|
26
|
+
},
|
|
27
|
+
maxCollateralSumShortTokenShort: {
|
|
28
|
+
methodName: "getUint",
|
|
29
|
+
params: [maxCollateralSumKey(marketAddress, market.shortTokenAddress, false)],
|
|
30
|
+
},
|
|
31
|
+
virtualInventoryForPositionsInTokens: {
|
|
32
|
+
methodName: "getInt",
|
|
33
|
+
params: [virtualInventoryForPositionsInTokensKey(market.virtualIndexTokenId)],
|
|
34
|
+
},
|
|
35
|
+
},
|
|
36
|
+
};
|
|
37
|
+
return request;
|
|
38
|
+
}, {});
|
|
39
|
+
}
|
|
40
|
+
export function parseMarketsRiskValuesResponse(res, markets) {
|
|
41
|
+
return markets.reduce((result, market) => {
|
|
42
|
+
const values = res.data[market.marketTokenAddress];
|
|
43
|
+
const errors = res.errors[market.marketTokenAddress];
|
|
44
|
+
if (!values || errors) {
|
|
45
|
+
return result;
|
|
46
|
+
}
|
|
47
|
+
result[market.marketTokenAddress] = {
|
|
48
|
+
maxCollateralSumLongTokenLong: values.maxCollateralSumLongTokenLong.returnValues[0],
|
|
49
|
+
maxCollateralSumLongTokenShort: values.maxCollateralSumLongTokenShort.returnValues[0],
|
|
50
|
+
maxCollateralSumShortTokenLong: values.maxCollateralSumShortTokenLong.returnValues[0],
|
|
51
|
+
maxCollateralSumShortTokenShort: values.maxCollateralSumShortTokenShort.returnValues[0],
|
|
52
|
+
virtualInventoryForPositionsInTokens: values.virtualInventoryForPositionsInTokens.returnValues[0],
|
|
53
|
+
};
|
|
54
|
+
return result;
|
|
55
|
+
}, {});
|
|
56
|
+
}
|
|
7
57
|
export function buildClaimableFundingDataRequest({ marketsAddresses, marketsData, chainId, account, }) {
|
|
8
58
|
if (!marketsAddresses) {
|
|
9
59
|
return {};
|
|
@@ -40,13 +90,14 @@ export function buildClaimableFundingDataRequest({ marketsAddresses, marketsData
|
|
|
40
90
|
return request;
|
|
41
91
|
}, {});
|
|
42
92
|
}
|
|
43
|
-
export async function buildMarketsValuesRequest(chainId, { marketsAddresses, marketsData, tokensData, }) {
|
|
93
|
+
export async function buildMarketsValuesRequest(chainId, { marketsAddresses, marketsData, marketsConfigsData, tokensData, }) {
|
|
44
94
|
const dataStoreAddress = getContract(chainId, "DataStore");
|
|
45
95
|
const syntheticsReaderAddress = getContract(chainId, "SyntheticsReader");
|
|
46
96
|
const request = {};
|
|
47
97
|
for (const marketAddress of marketsAddresses || []) {
|
|
48
98
|
const market = getByKey(marketsData, marketAddress);
|
|
49
|
-
|
|
99
|
+
const marketConfig = getByKey(marketsConfigsData, marketAddress);
|
|
100
|
+
if (!market || !marketConfig?.virtualIndexTokenId) {
|
|
50
101
|
continue;
|
|
51
102
|
}
|
|
52
103
|
const marketPrices = getContractMarketPrices(tokensData, market);
|
|
@@ -153,6 +204,10 @@ export async function buildMarketsValuesRequest(chainId, { marketsAddresses, mar
|
|
|
153
204
|
methodName: "getUint",
|
|
154
205
|
params: [prebuiltHashedKeys.shortInterestInTokensUsingShortToken],
|
|
155
206
|
},
|
|
207
|
+
virtualInventoryForPositionsInTokens: {
|
|
208
|
+
methodName: "getInt",
|
|
209
|
+
params: [virtualInventoryForPositionsInTokensKey(marketConfig.virtualIndexTokenId)],
|
|
210
|
+
},
|
|
156
211
|
},
|
|
157
212
|
};
|
|
158
213
|
}
|
|
@@ -172,13 +227,13 @@ export function parseMarketsValuesResponse(res, marketsAddresses, marketsData, g
|
|
|
172
227
|
return acc;
|
|
173
228
|
}
|
|
174
229
|
const marketDivisor = getMarketDivisor(market);
|
|
175
|
-
const
|
|
230
|
+
const oiUsd = getOiUsdFromRawValues({
|
|
176
231
|
longInterestUsingLongToken: BigInt(dataStoreValues.longInterestUsingLongToken.returnValues[0]),
|
|
177
232
|
longInterestUsingShortToken: BigInt(dataStoreValues.longInterestUsingShortToken.returnValues[0]),
|
|
178
233
|
shortInterestUsingLongToken: BigInt(dataStoreValues.shortInterestUsingLongToken.returnValues[0]),
|
|
179
234
|
shortInterestUsingShortToken: BigInt(dataStoreValues.shortInterestUsingShortToken.returnValues[0]),
|
|
180
235
|
}, marketDivisor);
|
|
181
|
-
const
|
|
236
|
+
const oiInTokens = getOiInTokensFromRawValues({
|
|
182
237
|
longInterestInTokensUsingLongToken: BigInt(dataStoreValues.longInterestInTokensUsingLongToken.returnValues[0]),
|
|
183
238
|
longInterestInTokensUsingShortToken: BigInt(dataStoreValues.longInterestInTokensUsingShortToken.returnValues[0]),
|
|
184
239
|
shortInterestInTokensUsingLongToken: BigInt(dataStoreValues.shortInterestInTokensUsingLongToken.returnValues[0]),
|
|
@@ -188,10 +243,8 @@ export function parseMarketsValuesResponse(res, marketsAddresses, marketsData, g
|
|
|
188
243
|
const [, poolValueInfoMin] = readerValues.marketTokenPriceMin.returnValues;
|
|
189
244
|
const [, poolValueInfoMax] = readerValues.marketTokenPriceMax.returnValues;
|
|
190
245
|
acc[marketAddress] = {
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
longInterestInTokens,
|
|
194
|
-
shortInterestInTokens,
|
|
246
|
+
...oiUsd,
|
|
247
|
+
...oiInTokens,
|
|
195
248
|
longPoolAmount: dataStoreValues.longPoolAmount.returnValues[0] / marketDivisor,
|
|
196
249
|
shortPoolAmount: dataStoreValues.shortPoolAmount.returnValues[0] / marketDivisor,
|
|
197
250
|
poolValueMin: poolValueInfoMin.poolValue,
|
|
@@ -207,16 +260,18 @@ export function parseMarketsValuesResponse(res, marketsAddresses, marketsData, g
|
|
|
207
260
|
virtualPoolAmountForLongToken: virtualInventory.virtualPoolAmountForLongToken,
|
|
208
261
|
virtualPoolAmountForShortToken: virtualInventory.virtualPoolAmountForShortToken,
|
|
209
262
|
virtualInventoryForPositions: virtualInventory.virtualInventoryForPositions,
|
|
263
|
+
virtualInventoryForPositionsInTokens: dataStoreValues.virtualInventoryForPositionsInTokens.returnValues[0],
|
|
210
264
|
};
|
|
211
265
|
return acc;
|
|
212
266
|
}, {});
|
|
213
267
|
}
|
|
214
|
-
export async function buildMarketsConfigsRequest(chainId, { marketsAddresses, }) {
|
|
268
|
+
export async function buildMarketsConfigsRequest(chainId, { marketsAddresses, marketsData, }) {
|
|
215
269
|
const dataStoreAddress = getContract(chainId, "DataStore");
|
|
216
270
|
const request = {};
|
|
217
271
|
for (const marketAddress of marketsAddresses || []) {
|
|
272
|
+
const market = getByKey(marketsData, marketAddress);
|
|
218
273
|
const prebuiltHashedKeys = HASHED_MARKET_CONFIG_KEYS[chainId]?.[marketAddress];
|
|
219
|
-
if (!prebuiltHashedKeys) {
|
|
274
|
+
if (!market || !prebuiltHashedKeys) {
|
|
220
275
|
throw new Error(`No pre-built hashed config keys found for the market ${marketAddress}.`);
|
|
221
276
|
}
|
|
222
277
|
request[`${marketAddress}-dataStore`] = {
|
|
@@ -267,6 +322,22 @@ export async function buildMarketsConfigsRequest(chainId, { marketsAddresses, })
|
|
|
267
322
|
methodName: "getUint",
|
|
268
323
|
params: [prebuiltHashedKeys.maxOpenInterestShort],
|
|
269
324
|
},
|
|
325
|
+
maxCollateralSumLongTokenLong: {
|
|
326
|
+
methodName: "getUint",
|
|
327
|
+
params: [prebuiltHashedKeys.maxCollateralSumLongTokenLong],
|
|
328
|
+
},
|
|
329
|
+
maxCollateralSumLongTokenShort: {
|
|
330
|
+
methodName: "getUint",
|
|
331
|
+
params: [prebuiltHashedKeys.maxCollateralSumLongTokenShort],
|
|
332
|
+
},
|
|
333
|
+
maxCollateralSumShortTokenLong: {
|
|
334
|
+
methodName: "getUint",
|
|
335
|
+
params: [prebuiltHashedKeys.maxCollateralSumShortTokenLong],
|
|
336
|
+
},
|
|
337
|
+
maxCollateralSumShortTokenShort: {
|
|
338
|
+
methodName: "getUint",
|
|
339
|
+
params: [prebuiltHashedKeys.maxCollateralSumShortTokenShort],
|
|
340
|
+
},
|
|
270
341
|
minPositionImpactPoolAmount: {
|
|
271
342
|
methodName: "getUint",
|
|
272
343
|
params: [prebuiltHashedKeys.minPositionImpactPoolAmount],
|
|
@@ -303,6 +374,10 @@ export async function buildMarketsConfigsRequest(chainId, { marketsAddresses, })
|
|
|
303
374
|
methodName: "getUint",
|
|
304
375
|
params: [prebuiltHashedKeys.fundingIncreaseFactorPerSecond],
|
|
305
376
|
},
|
|
377
|
+
minFundingIncreaseRatePerSecond: {
|
|
378
|
+
methodName: "getUint",
|
|
379
|
+
params: [prebuiltHashedKeys.minFundingIncreaseRatePerSecond],
|
|
380
|
+
},
|
|
306
381
|
fundingDecreaseFactorPerSecond: {
|
|
307
382
|
methodName: "getUint",
|
|
308
383
|
params: [prebuiltHashedKeys.fundingDecreaseFactorPerSecond],
|
|
@@ -315,13 +390,21 @@ export async function buildMarketsConfigsRequest(chainId, { marketsAddresses, })
|
|
|
315
390
|
methodName: "getUint",
|
|
316
391
|
params: [prebuiltHashedKeys.thresholdForDecreaseFunding],
|
|
317
392
|
},
|
|
318
|
-
|
|
393
|
+
minFundingFactorPerSecondLong: {
|
|
394
|
+
methodName: "getUint",
|
|
395
|
+
params: [prebuiltHashedKeys.minFundingFactorPerSecondLong],
|
|
396
|
+
},
|
|
397
|
+
minFundingFactorPerSecondShort: {
|
|
398
|
+
methodName: "getUint",
|
|
399
|
+
params: [prebuiltHashedKeys.minFundingFactorPerSecondShort],
|
|
400
|
+
},
|
|
401
|
+
maxFundingFactorPerSecondLong: {
|
|
319
402
|
methodName: "getUint",
|
|
320
|
-
params: [prebuiltHashedKeys.
|
|
403
|
+
params: [prebuiltHashedKeys.maxFundingFactorPerSecondLong],
|
|
321
404
|
},
|
|
322
|
-
|
|
405
|
+
maxFundingFactorPerSecondShort: {
|
|
323
406
|
methodName: "getUint",
|
|
324
|
-
params: [prebuiltHashedKeys.
|
|
407
|
+
params: [prebuiltHashedKeys.maxFundingFactorPerSecondShort],
|
|
325
408
|
},
|
|
326
409
|
maxPnlFactorForTradersLong: {
|
|
327
410
|
methodName: "getUint",
|
|
@@ -451,6 +534,10 @@ export async function buildMarketsConfigsRequest(chainId, { marketsAddresses, })
|
|
|
451
534
|
methodName: "getBytes32",
|
|
452
535
|
params: [prebuiltHashedKeys.virtualMarketId],
|
|
453
536
|
},
|
|
537
|
+
virtualIndexTokenId: {
|
|
538
|
+
methodName: "getBytes32",
|
|
539
|
+
params: [virtualTokenIdKey(market.indexTokenAddress)],
|
|
540
|
+
},
|
|
454
541
|
virtualShortTokenId: {
|
|
455
542
|
methodName: "getBytes32",
|
|
456
543
|
params: [prebuiltHashedKeys.virtualShortTokenId],
|
|
@@ -483,6 +570,10 @@ export function parseMarketsConfigsResponse(res, marketsAddresses) {
|
|
|
483
570
|
openInterestReserveFactorShort: dataStoreValues.openInterestReserveFactorShort.returnValues[0],
|
|
484
571
|
maxOpenInterestLong: dataStoreValues.maxOpenInterestLong.returnValues[0],
|
|
485
572
|
maxOpenInterestShort: dataStoreValues.maxOpenInterestShort.returnValues[0],
|
|
573
|
+
maxCollateralSumLongTokenLong: dataStoreValues.maxCollateralSumLongTokenLong.returnValues[0],
|
|
574
|
+
maxCollateralSumLongTokenShort: dataStoreValues.maxCollateralSumLongTokenShort.returnValues[0],
|
|
575
|
+
maxCollateralSumShortTokenLong: dataStoreValues.maxCollateralSumShortTokenLong.returnValues[0],
|
|
576
|
+
maxCollateralSumShortTokenShort: dataStoreValues.maxCollateralSumShortTokenShort.returnValues[0],
|
|
486
577
|
minPositionImpactPoolAmount: dataStoreValues.minPositionImpactPoolAmount.returnValues[0],
|
|
487
578
|
positionImpactPoolDistributionRate: dataStoreValues.positionImpactPoolDistributionRate.returnValues[0],
|
|
488
579
|
borrowingFactorLong: dataStoreValues.borrowingFactorLong.returnValues[0],
|
|
@@ -492,11 +583,14 @@ export function parseMarketsConfigsResponse(res, marketsAddresses) {
|
|
|
492
583
|
fundingFactor: dataStoreValues.fundingFactor.returnValues[0],
|
|
493
584
|
fundingExponentFactor: dataStoreValues.fundingExponentFactor.returnValues[0],
|
|
494
585
|
fundingIncreaseFactorPerSecond: dataStoreValues.fundingIncreaseFactorPerSecond.returnValues[0],
|
|
586
|
+
minFundingIncreaseRatePerSecond: dataStoreValues.minFundingIncreaseRatePerSecond.returnValues[0],
|
|
495
587
|
fundingDecreaseFactorPerSecond: dataStoreValues.fundingDecreaseFactorPerSecond.returnValues[0],
|
|
496
588
|
thresholdForDecreaseFunding: dataStoreValues.thresholdForDecreaseFunding.returnValues[0],
|
|
497
589
|
thresholdForStableFunding: dataStoreValues.thresholdForStableFunding.returnValues[0],
|
|
498
|
-
|
|
499
|
-
|
|
590
|
+
minFundingFactorPerSecondLong: dataStoreValues.minFundingFactorPerSecondLong.returnValues[0],
|
|
591
|
+
minFundingFactorPerSecondShort: dataStoreValues.minFundingFactorPerSecondShort.returnValues[0],
|
|
592
|
+
maxFundingFactorPerSecondLong: dataStoreValues.maxFundingFactorPerSecondLong.returnValues[0],
|
|
593
|
+
maxFundingFactorPerSecondShort: dataStoreValues.maxFundingFactorPerSecondShort.returnValues[0],
|
|
500
594
|
maxPnlFactorForTradersLong: dataStoreValues.maxPnlFactorForTradersLong.returnValues[0],
|
|
501
595
|
maxPnlFactorForTradersShort: dataStoreValues.maxPnlFactorForTradersShort.returnValues[0],
|
|
502
596
|
maxPnlFactorForDepositsLong: dataStoreValues.maxPnlFactorForDepositsLong.returnValues[0],
|
|
@@ -529,6 +623,7 @@ export function parseMarketsConfigsResponse(res, marketsAddresses) {
|
|
|
529
623
|
withdrawalFeeFactorBalanceWasNotImproved: dataStoreValues.withdrawalFeeFactorBalanceWasNotImproved.returnValues[0],
|
|
530
624
|
swapImpactExponentFactor: dataStoreValues.swapImpactExponentFactor.returnValues[0],
|
|
531
625
|
virtualMarketId: dataStoreValues.virtualMarketId.returnValues[0],
|
|
626
|
+
virtualIndexTokenId: dataStoreValues.virtualIndexTokenId.returnValues[0],
|
|
532
627
|
virtualLongTokenId: dataStoreValues.virtualLongTokenId.returnValues[0],
|
|
533
628
|
virtualShortTokenId: dataStoreValues.virtualShortTokenId.returnValues[0],
|
|
534
629
|
};
|
|
@@ -1 +1,30 @@
|
|
|
1
|
-
export
|
|
1
|
+
export const MARKET_VALUES_KEYS = [
|
|
2
|
+
"longInterestUsd",
|
|
3
|
+
"shortInterestUsd",
|
|
4
|
+
"longInterestInTokens",
|
|
5
|
+
"shortInterestInTokens",
|
|
6
|
+
"longInterestUsdUsingLongToken",
|
|
7
|
+
"longInterestUsdUsingShortToken",
|
|
8
|
+
"shortInterestUsdUsingLongToken",
|
|
9
|
+
"shortInterestUsdUsingShortToken",
|
|
10
|
+
"longInterestInTokensUsingLongToken",
|
|
11
|
+
"longInterestInTokensUsingShortToken",
|
|
12
|
+
"shortInterestInTokensUsingLongToken",
|
|
13
|
+
"shortInterestInTokensUsingShortToken",
|
|
14
|
+
"longPoolAmount",
|
|
15
|
+
"shortPoolAmount",
|
|
16
|
+
"poolValueMin",
|
|
17
|
+
"poolValueMax",
|
|
18
|
+
"totalBorrowingFees",
|
|
19
|
+
"positionImpactPoolAmount",
|
|
20
|
+
"swapImpactPoolAmountLong",
|
|
21
|
+
"swapImpactPoolAmountShort",
|
|
22
|
+
"borrowingFactorPerSecondForLongs",
|
|
23
|
+
"borrowingFactorPerSecondForShorts",
|
|
24
|
+
"fundingFactorPerSecond",
|
|
25
|
+
"longsPayShorts",
|
|
26
|
+
"virtualPoolAmountForLongToken",
|
|
27
|
+
"virtualPoolAmountForShortToken",
|
|
28
|
+
"virtualInventoryForPositions",
|
|
29
|
+
"virtualInventoryForPositionsInTokens",
|
|
30
|
+
];
|
|
@@ -162,13 +162,20 @@ export function getMarketDivisor({ longTokenAddress, shortTokenAddress, }) {
|
|
|
162
162
|
return longTokenAddress === shortTokenAddress ? 2n : 1n;
|
|
163
163
|
}
|
|
164
164
|
export function getOiUsdFromRawValues(rawValues, marketDivisor) {
|
|
165
|
-
const
|
|
166
|
-
const
|
|
167
|
-
const
|
|
168
|
-
const
|
|
169
|
-
const longInterestUsd =
|
|
170
|
-
const shortInterestUsd =
|
|
171
|
-
return {
|
|
165
|
+
const longInterestUsdUsingLongToken = rawValues.longInterestUsingLongToken / marketDivisor;
|
|
166
|
+
const longInterestUsdUsingShortToken = rawValues.longInterestUsingShortToken / marketDivisor;
|
|
167
|
+
const shortInterestUsdUsingLongToken = rawValues.shortInterestUsingLongToken / marketDivisor;
|
|
168
|
+
const shortInterestUsdUsingShortToken = rawValues.shortInterestUsingShortToken / marketDivisor;
|
|
169
|
+
const longInterestUsd = longInterestUsdUsingLongToken + longInterestUsdUsingShortToken;
|
|
170
|
+
const shortInterestUsd = shortInterestUsdUsingLongToken + shortInterestUsdUsingShortToken;
|
|
171
|
+
return {
|
|
172
|
+
longInterestUsd,
|
|
173
|
+
shortInterestUsd,
|
|
174
|
+
longInterestUsdUsingLongToken,
|
|
175
|
+
longInterestUsdUsingShortToken,
|
|
176
|
+
shortInterestUsdUsingLongToken,
|
|
177
|
+
shortInterestUsdUsingShortToken,
|
|
178
|
+
};
|
|
172
179
|
}
|
|
173
180
|
export function getOiInTokensFromRawValues(rawValues, marketDivisor) {
|
|
174
181
|
const longInterestInTokensUsingLongToken = rawValues.longInterestInTokensUsingLongToken / marketDivisor;
|
|
@@ -177,7 +184,14 @@ export function getOiInTokensFromRawValues(rawValues, marketDivisor) {
|
|
|
177
184
|
const shortInterestInTokensUsingShortToken = rawValues.shortInterestInTokensUsingShortToken / marketDivisor;
|
|
178
185
|
const longInterestInTokens = longInterestInTokensUsingLongToken + longInterestInTokensUsingShortToken;
|
|
179
186
|
const shortInterestInTokens = shortInterestInTokensUsingLongToken + shortInterestInTokensUsingShortToken;
|
|
180
|
-
return {
|
|
187
|
+
return {
|
|
188
|
+
longInterestInTokens,
|
|
189
|
+
shortInterestInTokens,
|
|
190
|
+
longInterestInTokensUsingLongToken,
|
|
191
|
+
longInterestInTokensUsingShortToken,
|
|
192
|
+
shortInterestInTokensUsingLongToken,
|
|
193
|
+
shortInterestInTokensUsingShortToken,
|
|
194
|
+
};
|
|
181
195
|
}
|
|
182
196
|
export function getMarketPnl(marketInfo, isLong, forMaxPoolValue) {
|
|
183
197
|
const maximize = !forMaxPoolValue;
|
|
@@ -191,6 +205,49 @@ export function getMarketPnl(marketInfo, isLong, forMaxPoolValue) {
|
|
|
191
205
|
const pnl = isLong ? openInterestValue - openInterestUsd : openInterestUsd - openInterestValue;
|
|
192
206
|
return pnl;
|
|
193
207
|
}
|
|
208
|
+
function getMarketPnlByCollateralToken(marketInfo, isLong, isLongCollateral, forMaxPoolValue) {
|
|
209
|
+
const maximize = !forMaxPoolValue;
|
|
210
|
+
let openInterestUsd;
|
|
211
|
+
let openInterestInTokens;
|
|
212
|
+
if (isLong) {
|
|
213
|
+
openInterestUsd = isLongCollateral
|
|
214
|
+
? marketInfo.longInterestUsdUsingLongToken
|
|
215
|
+
: marketInfo.longInterestUsdUsingShortToken;
|
|
216
|
+
openInterestInTokens = isLongCollateral
|
|
217
|
+
? marketInfo.longInterestInTokensUsingLongToken
|
|
218
|
+
: marketInfo.longInterestInTokensUsingShortToken;
|
|
219
|
+
}
|
|
220
|
+
else {
|
|
221
|
+
openInterestUsd = isLongCollateral
|
|
222
|
+
? marketInfo.shortInterestUsdUsingLongToken
|
|
223
|
+
: marketInfo.shortInterestUsdUsingShortToken;
|
|
224
|
+
openInterestInTokens = isLongCollateral
|
|
225
|
+
? marketInfo.shortInterestInTokensUsingLongToken
|
|
226
|
+
: marketInfo.shortInterestInTokensUsingShortToken;
|
|
227
|
+
}
|
|
228
|
+
if (openInterestUsd === undefined || openInterestInTokens === undefined) {
|
|
229
|
+
return undefined;
|
|
230
|
+
}
|
|
231
|
+
if (openInterestUsd === 0n || openInterestInTokens === 0n) {
|
|
232
|
+
return 0n;
|
|
233
|
+
}
|
|
234
|
+
const price = getPriceForPnl(marketInfo.indexToken.prices, isLong, maximize);
|
|
235
|
+
const openInterestValue = convertToUsd(openInterestInTokens, marketInfo.indexToken.decimals, price);
|
|
236
|
+
return isLong ? openInterestValue - openInterestUsd : openInterestUsd - openInterestValue;
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* Mirrors MarketUtils.getPositivePnl, the denominator of the per-position pnl cap since v2.2c.
|
|
240
|
+
* Pool value keeps using the net {@link getMarketPnl}. Falls back to it without the collateral split.
|
|
241
|
+
*/
|
|
242
|
+
export function getPositiveMarketPnl(marketInfo, isLong, forMaxPoolValue) {
|
|
243
|
+
const pnlUsingLongTokenAsCollateral = getMarketPnlByCollateralToken(marketInfo, isLong, true, forMaxPoolValue);
|
|
244
|
+
const pnlUsingShortTokenAsCollateral = getMarketPnlByCollateralToken(marketInfo, isLong, false, forMaxPoolValue);
|
|
245
|
+
if (pnlUsingLongTokenAsCollateral === undefined || pnlUsingShortTokenAsCollateral === undefined) {
|
|
246
|
+
return getMarketPnl(marketInfo, isLong, forMaxPoolValue);
|
|
247
|
+
}
|
|
248
|
+
return ((pnlUsingLongTokenAsCollateral > 0 ? pnlUsingLongTokenAsCollateral : 0n) +
|
|
249
|
+
(pnlUsingShortTokenAsCollateral > 0 ? pnlUsingShortTokenAsCollateral : 0n));
|
|
250
|
+
}
|
|
194
251
|
export function getOpenInterestUsd(marketInfo, isLong) {
|
|
195
252
|
return isLong ? marketInfo.longInterestUsd : marketInfo.shortInterestUsd;
|
|
196
253
|
}
|
|
@@ -313,6 +370,19 @@ export function composeRawMarketsInfoData({ marketsAddresses, marketsData, marke
|
|
|
313
370
|
}
|
|
314
371
|
return data;
|
|
315
372
|
}
|
|
373
|
+
export function mergeMarketsConfigValues(configs, values) {
|
|
374
|
+
const valuesByAddress = new Map(values.map((value) => [value.marketTokenAddress, value]));
|
|
375
|
+
const data = {};
|
|
376
|
+
for (const config of configs) {
|
|
377
|
+
const value = valuesByAddress.get(config.marketTokenAddress);
|
|
378
|
+
if (!value) {
|
|
379
|
+
continue;
|
|
380
|
+
}
|
|
381
|
+
const { updatedAt: _updatedAt, ...valueFields } = value;
|
|
382
|
+
data[config.marketTokenAddress] = { ...config, ...valueFields };
|
|
383
|
+
}
|
|
384
|
+
return data;
|
|
385
|
+
}
|
|
316
386
|
export function hydrateMarketInfo({ chainId, rawMarketInfo, tokensData, claimableFundingData, }) {
|
|
317
387
|
const longToken = getByKey(tokensData, rawMarketInfo.longTokenAddress);
|
|
318
388
|
const shortToken = getByKey(tokensData, rawMarketInfo.shortTokenAddress);
|
|
@@ -13,6 +13,17 @@ export const BN_ONE = 1n;
|
|
|
13
13
|
export const BN_NEGATIVE_ONE = -1n;
|
|
14
14
|
export const MaxUint256 = BigInt("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
|
|
15
15
|
export const MaxInt256 = BigInt("0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
|
|
16
|
+
const UINT_DECIMAL_STRING_REGEX = /^(0|[1-9]\d*)$/;
|
|
17
|
+
export function parseUint256DecimalString(value) {
|
|
18
|
+
if (typeof value !== "string" || !UINT_DECIMAL_STRING_REGEX.test(value)) {
|
|
19
|
+
return undefined;
|
|
20
|
+
}
|
|
21
|
+
const parsed = BigInt(value);
|
|
22
|
+
return parsed <= MaxUint256 ? parsed : undefined;
|
|
23
|
+
}
|
|
24
|
+
export function isUint256(value) {
|
|
25
|
+
return typeof value === "bigint" && value >= 0n && value <= MaxUint256;
|
|
26
|
+
}
|
|
16
27
|
export const PERCENT_PRECISION_DECIMALS = PRECISION_DECIMALS - 2;
|
|
17
28
|
const MAX_EXCEEDING_THRESHOLD = "1000000000";
|
|
18
29
|
const MIN_EXCEEDING_THRESHOLD = "0.01";
|
|
@@ -56,12 +67,30 @@ export function getBasisPoints(numerator, denominator, shouldRoundUp = false) {
|
|
|
56
67
|
}
|
|
57
68
|
return result;
|
|
58
69
|
}
|
|
70
|
+
export function formatBasisPoints(bpsValue) {
|
|
71
|
+
if (bpsValue === undefined) {
|
|
72
|
+
return undefined;
|
|
73
|
+
}
|
|
74
|
+
const sign = getPlusOrMinusSymbol(bpsValue);
|
|
75
|
+
return `${sign}${formatAmount(bigMath.abs(bpsValue), 0, 0, true)} bps`;
|
|
76
|
+
}
|
|
77
|
+
export function formatPriceImpactBps(priceImpactUsd, sizeUsd) {
|
|
78
|
+
return formatBasisPoints(priceImpactUsd !== undefined && sizeUsd > 0n ? getBasisPoints(priceImpactUsd, sizeUsd) : undefined);
|
|
79
|
+
}
|
|
59
80
|
export function roundUpMagnitudeDivision(a, b) {
|
|
60
81
|
if (a < 0n) {
|
|
61
82
|
return (a - b + 1n) / b;
|
|
62
83
|
}
|
|
63
84
|
return (a + b - 1n) / b;
|
|
64
85
|
}
|
|
86
|
+
// True when the amount would render as zero at displayDecimals.
|
|
87
|
+
export function roundsToZero(amount, decimals, displayDecimals) {
|
|
88
|
+
const hiddenDecimals = decimals - displayDecimals;
|
|
89
|
+
if (hiddenDecimals <= 0) {
|
|
90
|
+
return false;
|
|
91
|
+
}
|
|
92
|
+
return bigMath.abs(amount) < 10n ** BigInt(hiddenDecimals);
|
|
93
|
+
}
|
|
65
94
|
export function applyFactor(value, factor) {
|
|
66
95
|
return (value * factor) / PRECISION;
|
|
67
96
|
}
|
|
@@ -171,16 +200,16 @@ export function formatDeltaUsd(deltaUsd, percentage, opts = {}) {
|
|
|
171
200
|
return `${symbol}${sign}$\u200a${deltaUsdStr}${percentageStr}`;
|
|
172
201
|
}
|
|
173
202
|
export function formatPercentage(percentage, opts = {}) {
|
|
174
|
-
const { fallbackToZero = false, signed = false, displayDecimals = 2, bps = true, showPlus = true } = opts;
|
|
203
|
+
const { fallbackToZero = false, signed = false, displayDecimals = 2, bps = true, showPlus = true, useCommas = false, } = opts;
|
|
175
204
|
if (percentage === undefined) {
|
|
176
205
|
if (fallbackToZero) {
|
|
177
|
-
return `${formatAmount(0n, PERCENT_PRECISION_DECIMALS, displayDecimals)}%`;
|
|
206
|
+
return `${formatAmount(0n, PERCENT_PRECISION_DECIMALS, displayDecimals, useCommas)}%`;
|
|
178
207
|
}
|
|
179
208
|
return undefined;
|
|
180
209
|
}
|
|
181
210
|
const sign = signed ? `${getPlusOrMinusSymbol(percentage)}` : "";
|
|
182
211
|
const displaySign = !showPlus && sign === "+" ? "" : `${sign}`;
|
|
183
|
-
return `${displaySign}${displaySign ? "\u200a" : ""}${formatAmount(bigMath.abs(percentage), bps ? 2 : PERCENT_PRECISION_DECIMALS, displayDecimals)}%`;
|
|
212
|
+
return `${displaySign}${displaySign ? "\u200a" : ""}${formatAmount(bigMath.abs(percentage), bps ? 2 : PERCENT_PRECISION_DECIMALS, displayDecimals, useCommas)}%`;
|
|
184
213
|
}
|
|
185
214
|
export function formatTokenAmount(amount, tokenDecimals, symbol, opts = {}) {
|
|
186
215
|
const { showAllSignificant = false, fallbackToZero = false, useCommas = false, minThreshold = "0", maxThreshold, } = opts;
|
|
@@ -343,8 +372,8 @@ export function formatFactor(factor) {
|
|
|
343
372
|
.abs(factor)
|
|
344
373
|
.toString()
|
|
345
374
|
.match(/^(.+?)(?<zeroes>0*)$/)?.groups?.zeroes?.length || 0;
|
|
346
|
-
const factorDecimals =
|
|
347
|
-
return formatAmount(factor,
|
|
375
|
+
const factorDecimals = Math.max(PRECISION_DECIMALS - trailingZeroes, 0);
|
|
376
|
+
return formatAmount(factor, PRECISION_DECIMALS, factorDecimals);
|
|
348
377
|
}
|
|
349
378
|
export function numberWithCommas(x, { showDollar = false } = {}) {
|
|
350
379
|
if (x === undefined || x === null) {
|
|
@@ -512,8 +541,14 @@ export const parseValue = (value, tokenDecimals) => {
|
|
|
512
541
|
if (isNaN(pValue) || !isFinite(pValue)) {
|
|
513
542
|
return undefined;
|
|
514
543
|
}
|
|
515
|
-
|
|
516
|
-
|
|
544
|
+
let amount;
|
|
545
|
+
try {
|
|
546
|
+
value = limitDecimals(value, tokenDecimals);
|
|
547
|
+
amount = parseUnits(value, tokenDecimals);
|
|
548
|
+
}
|
|
549
|
+
catch {
|
|
550
|
+
return undefined;
|
|
551
|
+
}
|
|
517
552
|
// Cap at a safe maximum to prevent downstream BigInt overflow errors
|
|
518
553
|
const MAX_ALLOWED = expandDecimals(1, 62);
|
|
519
554
|
if (amount > MAX_ALLOWED || amount < -MAX_ALLOWED) {
|
|
@@ -550,10 +585,10 @@ export function maxbigint(...args) {
|
|
|
550
585
|
return args.reduce((acc, num) => (num > acc ? num : acc), args[0]);
|
|
551
586
|
}
|
|
552
587
|
export function removeTrailingZeros(amount) {
|
|
553
|
-
|
|
554
|
-
if (!amountWithoutZeros)
|
|
588
|
+
if (typeof amount === "number" || !amount.includes(".") || amount.includes(",") || /^[-+]?0*\.0*$/.test(amount)) {
|
|
555
589
|
return amount;
|
|
556
|
-
|
|
590
|
+
}
|
|
591
|
+
return amount.replace(/0+$/, "").replace(/\.$/, "");
|
|
557
592
|
}
|
|
558
593
|
const NUMERIC_STRING_REGEX = /^-?\d+$/;
|
|
559
594
|
export function serializeBigIntsInObject(obj) {
|
|
@@ -1,7 +1,108 @@
|
|
|
1
|
+
import { HttpError } from "../http/http";
|
|
2
|
+
import { parseTradingCapacity } from "../markets/api";
|
|
3
|
+
import { isUint256, parseUint256DecimalString } from "../numbers";
|
|
4
|
+
import { getRecord, getString, isRecord } from "../objects";
|
|
1
5
|
import { validateOrderTypedData } from "./validateTypedData";
|
|
6
|
+
const PREPARE_ORDER_ERROR_CODES = [
|
|
7
|
+
"INVALID_PARAMS",
|
|
8
|
+
"MARKET_NOT_FOUND",
|
|
9
|
+
"TOKEN_NOT_FOUND",
|
|
10
|
+
"NO_SWAP_PATH",
|
|
11
|
+
"POSITION_NOT_FOUND",
|
|
12
|
+
];
|
|
13
|
+
function isDeserializedTradingCapacity(value) {
|
|
14
|
+
const raw = getRecord(value);
|
|
15
|
+
if (!raw) {
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
const parsed = parseTradingCapacity(raw);
|
|
19
|
+
return parsed !== undefined && Object.entries(parsed).every(([key, parsedValue]) => raw[key] === parsedValue);
|
|
20
|
+
}
|
|
21
|
+
function isPrepareOrderFieldValidationErrors(value) {
|
|
22
|
+
if (!isRecord(value)) {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
return Object.values(value).every((fieldError) => isRecord(fieldError) && typeof fieldError.message === "string");
|
|
26
|
+
}
|
|
27
|
+
function parsePrepareOrderErrorBody(value) {
|
|
28
|
+
if (!isRecord(value)) {
|
|
29
|
+
return undefined;
|
|
30
|
+
}
|
|
31
|
+
const body = value;
|
|
32
|
+
if (body.code === "INSUFFICIENT_LIQUIDITY") {
|
|
33
|
+
const details = getRecord(body.details);
|
|
34
|
+
const capacity = parseTradingCapacity(details);
|
|
35
|
+
const requestedSizeUsd = parseUint256DecimalString(details?.requestedSizeUsd);
|
|
36
|
+
if (!capacity || requestedSizeUsd === undefined || typeof body.message !== "string") {
|
|
37
|
+
return undefined;
|
|
38
|
+
}
|
|
39
|
+
return {
|
|
40
|
+
code: body.code,
|
|
41
|
+
message: body.message,
|
|
42
|
+
details: {
|
|
43
|
+
...capacity,
|
|
44
|
+
requestedSizeUsd,
|
|
45
|
+
},
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
if (PREPARE_ORDER_ERROR_CODES.includes(body.code) && typeof body.message === "string") {
|
|
49
|
+
return {
|
|
50
|
+
code: body.code,
|
|
51
|
+
message: body.message,
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
if (typeof body.code === "string" && body.code.length > 0 && typeof body.message === "string") {
|
|
55
|
+
return {
|
|
56
|
+
code: "UNKNOWN_PREPARE_ORDER_ERROR",
|
|
57
|
+
originalCode: body.code,
|
|
58
|
+
message: body.message,
|
|
59
|
+
...(typeof body.traceId === "string" ? { traceId: body.traceId } : {}),
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
if (body.code !== undefined) {
|
|
63
|
+
return undefined;
|
|
64
|
+
}
|
|
65
|
+
if (typeof body.message === "string") {
|
|
66
|
+
return { message: body.message };
|
|
67
|
+
}
|
|
68
|
+
if (isPrepareOrderFieldValidationErrors(body.message)) {
|
|
69
|
+
return { message: body.message };
|
|
70
|
+
}
|
|
71
|
+
return undefined;
|
|
72
|
+
}
|
|
73
|
+
export function parsePrepareOrderError(error) {
|
|
74
|
+
if (!(error instanceof HttpError) || error.statusCode !== 400) {
|
|
75
|
+
return undefined;
|
|
76
|
+
}
|
|
77
|
+
return parsePrepareOrderErrorBody(error.body);
|
|
78
|
+
}
|
|
79
|
+
/** Validates an already deserialized prepare error. Use parsePrepareOrderError for a caught HttpError. */
|
|
80
|
+
export function isPrepareOrderError(value) {
|
|
81
|
+
if (!isRecord(value)) {
|
|
82
|
+
return false;
|
|
83
|
+
}
|
|
84
|
+
const error = value;
|
|
85
|
+
if (error.code === "INSUFFICIENT_LIQUIDITY") {
|
|
86
|
+
const details = getRecord(error.details);
|
|
87
|
+
const requestedSizeUsd = details?.requestedSizeUsd;
|
|
88
|
+
return Boolean(isDeserializedTradingCapacity(details) && isUint256(requestedSizeUsd) && typeof error.message === "string");
|
|
89
|
+
}
|
|
90
|
+
if (PREPARE_ORDER_ERROR_CODES.includes(error.code)) {
|
|
91
|
+
return typeof error.message === "string";
|
|
92
|
+
}
|
|
93
|
+
if (error.code === "UNKNOWN_PREPARE_ORDER_ERROR") {
|
|
94
|
+
return (typeof error.originalCode === "string" &&
|
|
95
|
+
error.originalCode.length > 0 &&
|
|
96
|
+
typeof error.message === "string" &&
|
|
97
|
+
(error.traceId === undefined || typeof error.traceId === "string"));
|
|
98
|
+
}
|
|
99
|
+
return (error.code === undefined &&
|
|
100
|
+
(typeof error.message === "string" || isPrepareOrderFieldValidationErrors(error.message)));
|
|
101
|
+
}
|
|
2
102
|
function parseEstimates(raw) {
|
|
3
103
|
if (!raw)
|
|
4
104
|
return undefined;
|
|
105
|
+
const tradingCapacity = raw.tradingCapacity === undefined ? undefined : parseTradingCapacity(raw.tradingCapacity);
|
|
5
106
|
return {
|
|
6
107
|
positionPriceImpactDeltaUsd: BigInt(raw.positionPriceImpactDeltaUsd ?? "0"),
|
|
7
108
|
swapPriceImpactDeltaUsd: BigInt(raw.swapPriceImpactDeltaUsd ?? "0"),
|
|
@@ -11,10 +112,65 @@ function parseEstimates(raw) {
|
|
|
11
112
|
positionFeeUsd: BigInt(raw.positionFeeUsd ?? "0"),
|
|
12
113
|
borrowingFeeUsd: BigInt(raw.borrowingFeeUsd ?? "0"),
|
|
13
114
|
fundingFeeUsd: BigInt(raw.fundingFeeUsd ?? "0"),
|
|
115
|
+
tradingCapacity,
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
function parseValidationWarning(raw) {
|
|
119
|
+
const warning = getRecord(raw);
|
|
120
|
+
const code = getString(warning?.code);
|
|
121
|
+
const message = getString(warning?.message);
|
|
122
|
+
if (!warning || !code || !message) {
|
|
123
|
+
return undefined;
|
|
124
|
+
}
|
|
125
|
+
const details = getRecord(warning.details);
|
|
126
|
+
if (code === "INSUFFICIENT_LIQUIDITY") {
|
|
127
|
+
const capacity = parseTradingCapacity(details);
|
|
128
|
+
const requestedSizeUsd = parseUint256DecimalString(details?.requestedSizeUsd);
|
|
129
|
+
if (capacity && requestedSizeUsd !== undefined) {
|
|
130
|
+
return {
|
|
131
|
+
code,
|
|
132
|
+
message,
|
|
133
|
+
details: {
|
|
134
|
+
...capacity,
|
|
135
|
+
requestedSizeUsd,
|
|
136
|
+
},
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
const reason = getString(details?.reason);
|
|
141
|
+
if (code === "TRADING_CAPACITY_UNAVAILABLE" &&
|
|
142
|
+
(reason === "STALE_MARKET_DATA" ||
|
|
143
|
+
reason === "JIT_DATA_UNAVAILABLE" ||
|
|
144
|
+
reason === "JIT_ACCOUNT_ELIGIBILITY_UNKNOWN" ||
|
|
145
|
+
reason === "COLLATERAL_SWAP")) {
|
|
146
|
+
return {
|
|
147
|
+
code,
|
|
148
|
+
message,
|
|
149
|
+
details: { reason },
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
return {
|
|
153
|
+
code: "UNKNOWN_VALIDATION_WARNING",
|
|
154
|
+
originalCode: code,
|
|
155
|
+
message,
|
|
156
|
+
...(warning.details === undefined ? {} : { details: warning.details }),
|
|
14
157
|
};
|
|
15
158
|
}
|
|
159
|
+
function parseValidationWarnings(raw) {
|
|
160
|
+
if (raw === undefined) {
|
|
161
|
+
return undefined;
|
|
162
|
+
}
|
|
163
|
+
if (!Array.isArray(raw)) {
|
|
164
|
+
return undefined;
|
|
165
|
+
}
|
|
166
|
+
return raw.map(parseValidationWarning).filter((warning) => warning !== undefined);
|
|
167
|
+
}
|
|
16
168
|
function parsePrepareResponse(raw) {
|
|
17
|
-
return {
|
|
169
|
+
return {
|
|
170
|
+
...raw,
|
|
171
|
+
estimates: parseEstimates(raw.estimates),
|
|
172
|
+
validationWarnings: parseValidationWarnings(raw.validationWarnings),
|
|
173
|
+
};
|
|
18
174
|
}
|
|
19
175
|
export async function prepareOrder(ctx, request) {
|
|
20
176
|
return ctx.api.postJson("/v1/orders/txns/prepare", request, {
|