@gmx-io/sdk 1.5.0-alpha-1 → 1.5.0-alpha-2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/cjs/scripts/generate-platform-tokens.js +200 -0
- package/build/cjs/scripts/subsquid-codegen.js +1 -1
- package/build/cjs/src/clients/v1/modules/accounts/accounts.js +1 -1
- package/build/cjs/src/clients/v1/modules/markets/index.js +1 -1
- package/build/cjs/src/clients/v1/modules/markets/markets.spec.js +2 -2
- package/build/cjs/src/clients/v1/modules/orders/orders.js +4 -4
- package/build/cjs/src/clients/v1/modules/positions/positions.js +9 -151
- package/build/cjs/src/clients/v1/modules/tokens/tokens.js +1 -1
- package/build/cjs/src/clients/v1/modules/tokens/tokens.spec.js +2 -2
- package/build/cjs/src/clients/v1/modules/trades/trades.js +1 -1
- package/build/cjs/src/clients/v1/modules/utils/utils.js +1 -1
- package/build/cjs/src/clients/v2/index.js +8 -0
- package/build/cjs/src/codegen/platformTokens.json +134 -0
- package/build/cjs/src/codegen/prebuilt/hashedKinkModelMarketRatesKeys.json +24 -0
- package/build/cjs/src/codegen/prebuilt/hashedMarketConfigKeys.json +171 -0
- package/build/cjs/src/codegen/prebuilt/hashedMarketValuesKeys.json +45 -0
- package/build/cjs/src/codegen/subsquid.js +142 -22
- package/build/cjs/src/configs/markets.js +21 -0
- package/build/cjs/src/configs/tokens.js +35 -0
- package/build/cjs/src/utils/__tests__/fees.spec.js +1 -1
- package/build/cjs/src/utils/__tests__/numbers.spec.js +1 -1
- package/build/cjs/src/utils/__tests__/orderTransactions/batchUtils.spec.js +1 -1
- package/build/cjs/src/utils/__tests__/orderTransactions/decreaseOrders.spec.js +1 -1
- package/build/cjs/src/utils/__tests__/orderTransactions/exchangeRouterMulticall.spec.js +1 -1
- package/build/cjs/src/utils/__tests__/orderTransactions/externalCalls.spec.js +1 -1
- package/build/cjs/src/utils/__tests__/orderTransactions/increaseOrders.spec.js +1 -1
- package/build/cjs/src/utils/__tests__/orderTransactions/swapOrders.spec.js +1 -1
- package/build/cjs/src/utils/__tests__/orderTransactions/updateOrder.spec.js +1 -1
- package/build/cjs/src/utils/__tests__/parseError.spec.js +40 -0
- package/build/cjs/src/utils/__tests__/positions.spec.js +1 -1
- package/build/cjs/src/utils/__tests__/prices.spec.js +1 -1
- package/build/cjs/src/utils/__tests__/tokens.spec.js +191 -4
- package/build/cjs/src/utils/errors/parseError.js +67 -25
- package/build/cjs/src/utils/fees/executionFee.js +1 -1
- package/build/cjs/src/utils/fees/priceImpact.js +6 -1
- package/build/cjs/src/utils/markets/__tests__/mockMarkets.js +1 -1
- package/build/cjs/src/utils/numbers/utils.js +4 -2
- package/build/cjs/src/utils/orders/api.js +13 -0
- package/build/cjs/src/utils/orders/index.js +1 -0
- package/build/cjs/src/utils/orders/utils.js +2 -2
- package/build/cjs/src/utils/positions/api.js +14 -0
- package/build/cjs/src/utils/positions/index.js +1 -0
- package/build/cjs/src/utils/positions/utils.js +213 -1
- package/build/cjs/src/utils/swap/__tests__/mockRouting.spec.js +1 -1
- package/build/cjs/src/utils/swap/__tests__/swapRouting.spec.js +1 -1
- package/build/cjs/src/utils/swap/__tests__/swapStats.spec.js +1 -1
- package/build/cjs/src/utils/swap/buildSwapStrategy.js +1 -1
- package/build/cjs/src/utils/swap/externalSwapQuoteByPath.js +1 -1
- package/build/cjs/src/utils/swap/swapPath.js +1 -1
- package/build/cjs/src/utils/swap/swapValues.js +1 -1
- package/build/cjs/src/utils/tokens/utils.js +23 -3
- package/build/cjs/src/utils/trade/decrease.js +1 -1
- package/build/cjs/src/utils/trade/increase.js +1 -1
- package/build/cjs/src/utils/trade/trade.js +1 -1
- package/build/esm/scripts/generate-platform-tokens.js +195 -0
- package/build/esm/scripts/subsquid-codegen.js +1 -1
- package/build/esm/src/clients/v1/modules/accounts/accounts.js +1 -1
- package/build/esm/src/clients/v1/modules/markets/index.js +1 -1
- package/build/esm/src/clients/v1/modules/markets/markets.spec.js +1 -1
- package/build/esm/src/clients/v1/modules/orders/orders.js +4 -4
- package/build/esm/src/clients/v1/modules/positions/positions.js +12 -154
- package/build/esm/src/clients/v1/modules/tokens/tokens.js +1 -1
- package/build/esm/src/clients/v1/modules/tokens/tokens.spec.js +1 -1
- package/build/esm/src/clients/v1/modules/trades/trades.js +1 -1
- package/build/esm/src/clients/v1/modules/utils/utils.js +1 -1
- package/build/esm/src/clients/v2/index.js +8 -0
- package/build/esm/src/codegen/platformTokens.json +134 -0
- package/build/esm/src/codegen/prebuilt/hashedKinkModelMarketRatesKeys.json +24 -0
- package/build/esm/src/codegen/prebuilt/hashedMarketConfigKeys.json +171 -0
- package/build/esm/src/codegen/prebuilt/hashedMarketValuesKeys.json +45 -0
- package/build/esm/src/codegen/subsquid.js +141 -21
- package/build/esm/src/configs/markets.js +21 -0
- package/build/esm/src/configs/tokens.js +35 -0
- package/build/esm/src/utils/__tests__/fees.spec.js +1 -1
- package/build/esm/src/utils/__tests__/numbers.spec.js +1 -1
- package/build/esm/src/utils/__tests__/orderTransactions/batchUtils.spec.js +1 -1
- package/build/esm/src/utils/__tests__/orderTransactions/decreaseOrders.spec.js +1 -1
- package/build/esm/src/utils/__tests__/orderTransactions/exchangeRouterMulticall.spec.js +1 -1
- package/build/esm/src/utils/__tests__/orderTransactions/externalCalls.spec.js +1 -1
- package/build/esm/src/utils/__tests__/orderTransactions/increaseOrders.spec.js +1 -1
- package/build/esm/src/utils/__tests__/orderTransactions/swapOrders.spec.js +1 -1
- package/build/esm/src/utils/__tests__/orderTransactions/updateOrder.spec.js +1 -1
- package/build/esm/src/utils/__tests__/parseError.spec.js +40 -0
- package/build/esm/src/utils/__tests__/positions.spec.js +1 -1
- package/build/esm/src/utils/__tests__/prices.spec.js +1 -1
- package/build/esm/src/utils/__tests__/tokens.spec.js +192 -5
- package/build/esm/src/utils/errors/parseError.js +64 -24
- package/build/esm/src/utils/fees/executionFee.js +1 -1
- package/build/esm/src/utils/fees/priceImpact.js +5 -1
- package/build/esm/src/utils/markets/__tests__/mockMarkets.js +1 -1
- package/build/esm/src/utils/numbers/utils.js +2 -0
- package/build/esm/src/utils/orders/api.js +9 -0
- package/build/esm/src/utils/orders/index.js +1 -0
- package/build/esm/src/utils/orders/utils.js +2 -2
- package/build/esm/src/utils/positions/api.js +10 -0
- package/build/esm/src/utils/positions/index.js +1 -0
- package/build/esm/src/utils/positions/utils.js +213 -3
- package/build/esm/src/utils/swap/__tests__/mockRouting.spec.js +1 -1
- package/build/esm/src/utils/swap/__tests__/swapRouting.spec.js +1 -1
- package/build/esm/src/utils/swap/__tests__/swapStats.spec.js +1 -1
- package/build/esm/src/utils/swap/buildSwapStrategy.js +1 -1
- package/build/esm/src/utils/swap/externalSwapQuoteByPath.js +1 -1
- package/build/esm/src/utils/swap/swapPath.js +1 -1
- package/build/esm/src/utils/swap/swapValues.js +1 -1
- package/build/esm/src/utils/tokens/utils.js +23 -3
- package/build/esm/src/utils/trade/decrease.js +1 -1
- package/build/esm/src/utils/trade/increase.js +1 -1
- package/build/esm/src/utils/trade/trade.js +1 -1
- package/build/types/scripts/generate-platform-tokens.d.ts +27 -0
- package/build/types/src/clients/v1/modules/markets/index.d.ts +1 -1
- package/build/types/src/clients/v1/modules/oracle.d.ts +1 -1
- package/build/types/src/clients/v1/modules/orders/helpers.d.ts +1 -1
- package/build/types/src/clients/v1/modules/orders/orders.d.ts +2 -2
- package/build/types/src/clients/v1/modules/orders/transactions/cancelOrdersTxn.d.ts +1 -1
- package/build/types/src/clients/v1/modules/orders/transactions/createDecreaseOrderTxn.d.ts +1 -1
- package/build/types/src/clients/v1/modules/orders/transactions/createIncreaseOrderTxn.d.ts +1 -1
- package/build/types/src/clients/v1/modules/orders/transactions/createSwapOrderTxn.d.ts +1 -1
- package/build/types/src/clients/v1/modules/orders/transactions/createWrapOrUnwrapTxn.d.ts +1 -1
- package/build/types/src/clients/v1/modules/orders/transactions/updateOrderTxn.d.ts +1 -1
- package/build/types/src/clients/v1/modules/orders/utils.d.ts +2 -2
- package/build/types/src/clients/v1/modules/positions/positions.d.ts +1 -1
- package/build/types/src/clients/v1/modules/tokens/tokens.d.ts +1 -1
- package/build/types/src/clients/v1/modules/trades/trades.d.ts +2 -2
- package/build/types/src/clients/v1/modules/utils/utils.d.ts +1 -1
- package/build/types/src/clients/v2/index.d.ts +7 -0
- package/build/types/src/codegen/prebuilt/index.d.ts +240 -0
- package/build/types/src/codegen/subsquid.d.ts +492 -60
- package/build/types/src/utils/errors/parseError.d.ts +13 -1
- package/build/types/src/utils/fees/executionFee.d.ts +1 -1
- package/build/types/src/utils/fees/getNaiveEstimatedGasBySwapCount.d.ts +1 -1
- package/build/types/src/utils/fees/index.d.ts +1 -1
- package/build/types/src/utils/fees/priceImpact.d.ts +1 -0
- package/build/types/src/utils/markets/api.d.ts +1 -1
- package/build/types/src/utils/markets/multicall.d.ts +1 -1
- package/build/types/src/utils/markets/utils.d.ts +1 -1
- package/build/types/src/utils/numbers/utils.d.ts +2 -0
- package/build/types/src/utils/orders/api.d.ts +7 -0
- package/build/types/src/utils/orders/index.d.ts +1 -0
- package/build/types/src/utils/orders/types.d.ts +27 -0
- package/build/types/src/utils/orders/utils.d.ts +1 -1
- package/build/types/src/utils/positions/api.d.ts +8 -0
- package/build/types/src/utils/positions/index.d.ts +1 -0
- package/build/types/src/utils/positions/types.d.ts +5 -16
- package/build/types/src/utils/positions/utils.d.ts +42 -1
- package/build/types/src/utils/swap/buildSwapStrategy.d.ts +1 -1
- package/build/types/src/utils/swap/findSwapPathsBetweenTokens.d.ts +1 -1
- package/build/types/src/utils/swap/preparedSwapData.d.ts +1 -1
- package/build/types/src/utils/swap/swapRouting.d.ts +1 -1
- package/build/types/src/utils/tokens/types.d.ts +1 -1
- package/build/types/src/utils/trade/decrease.d.ts +1 -1
- package/build/types/src/utils/trade/increase.d.ts +1 -1
- package/build/types/src/utils/trade/trade.d.ts +1 -1
- package/build/types/src/utils/tradeHistory/types.d.ts +2 -2
- package/build/types/src/utils/tradeHistory/utils.d.ts +1 -1
- package/build/types/src/utils/twap/index.d.ts +1 -1
- package/package.json +4 -3
|
@@ -2,9 +2,9 @@ import { encodeFunctionData, erc20Abi } from "viem";
|
|
|
2
2
|
import { describe, expect, it } from "vitest";
|
|
3
3
|
import { ARBITRUM } from "../../../configs/chains";
|
|
4
4
|
import { getTokenBySymbol, getWrappedToken } from "../../../configs/tokens";
|
|
5
|
+
import { mockExternalSwap } from "../../../test/mock";
|
|
5
6
|
import { expandDecimals, MaxUint256, parseValue, USD_DECIMALS } from "../../numbers";
|
|
6
7
|
import { combineExternalCalls, getExternalCallsPayload } from "../../orderTransactions";
|
|
7
|
-
import { mockExternalSwap } from "../../../test/mock";
|
|
8
8
|
describe("External Calls", () => {
|
|
9
9
|
const CHAIN_ID = ARBITRUM;
|
|
10
10
|
const ACCOUNT = "0x1234567890123456789012345678901234567890";
|
|
@@ -4,11 +4,11 @@ import { ARBITRUM } from "../../../configs/chains";
|
|
|
4
4
|
import { getContract } from "../../../configs/contracts";
|
|
5
5
|
import { MARKETS } from "../../../configs/markets";
|
|
6
6
|
import { getTokenBySymbol, getWrappedToken, NATIVE_TOKEN_ADDRESS } from "../../../configs/tokens";
|
|
7
|
+
import { MOCK_GAS_PRICE, mockExternalSwap } from "../../../test/mock";
|
|
7
8
|
import { MaxUint256, parseValue, USD_DECIMALS } from "../../numbers";
|
|
8
9
|
import { DecreasePositionSwapType, OrderType } from "../../orders/types";
|
|
9
10
|
import { buildIncreaseOrderPayload, buildTwapOrdersPayloads, getIsTwapOrderPayload, } from "../../orderTransactions";
|
|
10
11
|
import { decodeTwapUiFeeReceiver } from "../../twap/uiFeeReceiver";
|
|
11
|
-
import { MOCK_GAS_PRICE, mockExternalSwap } from "../../../test/mock";
|
|
12
12
|
beforeAll(() => {
|
|
13
13
|
vi.spyOn(Math, "random").mockReturnValue(0.5);
|
|
14
14
|
});
|
|
@@ -4,11 +4,11 @@ import { ARBITRUM } from "../../../configs/chains";
|
|
|
4
4
|
import { getContract } from "../../../configs/contracts";
|
|
5
5
|
import { MARKETS } from "../../../configs/markets";
|
|
6
6
|
import { getTokenBySymbol, getWrappedToken, NATIVE_TOKEN_ADDRESS } from "../../../configs/tokens";
|
|
7
|
+
import { MOCK_GAS_PRICE } from "../../../test/mock";
|
|
7
8
|
import { parseValue } from "../../numbers";
|
|
8
9
|
import { DecreasePositionSwapType, OrderType } from "../../orders/types";
|
|
9
10
|
import { buildSwapOrderPayload, buildTwapOrdersPayloads, getIsTwapOrderPayload, } from "../../orderTransactions";
|
|
10
11
|
import { decodeTwapUiFeeReceiver } from "../../twap/uiFeeReceiver";
|
|
11
|
-
import { MOCK_GAS_PRICE } from "../../../test/mock";
|
|
12
12
|
beforeAll(() => {
|
|
13
13
|
// Mock Math.random to return a consistent value
|
|
14
14
|
vi.spyOn(Math, "random").mockReturnValue(0.5);
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { beforeAll, describe, expect, it, vi } from "vitest";
|
|
2
2
|
import { ARBITRUM } from "../../../configs/chains";
|
|
3
3
|
import { getTokenBySymbol, getWrappedToken } from "../../../configs/tokens";
|
|
4
|
+
import { MOCK_GAS_PRICE } from "../../../test/mock";
|
|
4
5
|
import { parseValue, USD_DECIMALS } from "../../numbers";
|
|
5
6
|
import { OrderType } from "../../orders/types";
|
|
6
7
|
import { buildUpdateOrderPayload } from "../../orderTransactions";
|
|
7
|
-
import { MOCK_GAS_PRICE } from "../../../test/mock";
|
|
8
8
|
beforeAll(() => {
|
|
9
9
|
vi.spyOn(Math, "random").mockReturnValue(0.5);
|
|
10
10
|
});
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { CallExecutionError, ContractFunctionExecutionError, ContractFunctionRevertedError, InvalidInputRpcError, RpcRequestError, InsufficientFundsError, } from "viem";
|
|
1
2
|
import { describe, expect, it } from "vitest";
|
|
2
3
|
import { parseError } from "../errors";
|
|
3
4
|
import { TxErrorType } from "../errors/transactionsErrors";
|
|
@@ -258,4 +259,43 @@ describe("parseError", () => {
|
|
|
258
259
|
}));
|
|
259
260
|
});
|
|
260
261
|
});
|
|
262
|
+
describe("viem errors", () => {
|
|
263
|
+
it("should handle viem ContractFunctionExecutionError", () => {
|
|
264
|
+
const error = new ContractFunctionExecutionError(new ContractFunctionRevertedError({
|
|
265
|
+
abi: [],
|
|
266
|
+
functionName: "test_function",
|
|
267
|
+
data: "0x4e48dcda",
|
|
268
|
+
message: "test message",
|
|
269
|
+
}), {
|
|
270
|
+
abi: [],
|
|
271
|
+
functionName: "test_function",
|
|
272
|
+
});
|
|
273
|
+
const result = parseError(error);
|
|
274
|
+
expect(result).toEqual(expect.objectContaining({
|
|
275
|
+
contractError: "EndOfOracleSimulation",
|
|
276
|
+
}));
|
|
277
|
+
});
|
|
278
|
+
it("should handle viem InsufficientFundsError", () => {
|
|
279
|
+
const error = new ContractFunctionExecutionError(new CallExecutionError(new InsufficientFundsError({
|
|
280
|
+
cause: new InvalidInputRpcError(new InvalidInputRpcError(new RpcRequestError({
|
|
281
|
+
error: {
|
|
282
|
+
message: "err: insufficient funds for gas * price + value: address 0x6f9f3106F0209dc560A53C6808f8BF32E38468C3 have 4174472651641805 want 10000000000000000000 (supplied gas 1100000000)",
|
|
283
|
+
code: -32000,
|
|
284
|
+
},
|
|
285
|
+
body: {},
|
|
286
|
+
url: "https://example.com",
|
|
287
|
+
}))),
|
|
288
|
+
}), {}), {
|
|
289
|
+
abi: [],
|
|
290
|
+
functionName: "test_function",
|
|
291
|
+
});
|
|
292
|
+
const result = parseError(error);
|
|
293
|
+
expect(result).toEqual(expect.objectContaining({
|
|
294
|
+
txErrorType: TxErrorType.NotEnoughFunds,
|
|
295
|
+
isUserError: true,
|
|
296
|
+
isUserRejectedError: false,
|
|
297
|
+
errorGroup: "Txn Error: NOT_ENOUGH_FUNDS",
|
|
298
|
+
}));
|
|
299
|
+
});
|
|
300
|
+
});
|
|
261
301
|
});
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { describe, expect, it, vi, beforeEach } from "vitest";
|
|
2
|
-
import { expandDecimals, USD_DECIMALS } from "../numbers";
|
|
3
2
|
import { bigMath } from "../bigmath";
|
|
4
3
|
import { getPositionFee, getPriceImpactForPosition } from "../fees";
|
|
5
4
|
import { getCappedPoolPnl, getMarketPnl, getPoolUsdWithoutPnl } from "../markets";
|
|
5
|
+
import { expandDecimals, USD_DECIMALS } from "../numbers";
|
|
6
6
|
import { getEntryPrice, getLeverage, getLiquidationPrice, getPositionKey, getPositionNetValue, getPositionPendingFeesUsd, getPositionPnlUsd, getPositionValueUsd, parsePositionKey, } from "../positions";
|
|
7
7
|
import { convertToUsd, getIsEquivalentTokens } from "../tokens";
|
|
8
8
|
vi.mock("../markets", () => ({
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { describe, expect, it } from "vitest";
|
|
2
2
|
import { OrderType } from "../orders/types";
|
|
3
|
-
import { TriggerThresholdType } from "../trade/types";
|
|
4
3
|
import { getMarkPrice, getOrderThresholdType, getShouldUseMaxPrice } from "../prices";
|
|
4
|
+
import { TriggerThresholdType } from "../trade/types";
|
|
5
5
|
describe("getMarkPrice", () => {
|
|
6
6
|
it("returns maxPrice if getShouldUseMaxPrice => true", () => {
|
|
7
7
|
const prices = { minPrice: 1000n, maxPrice: 1500n };
|
|
@@ -2,7 +2,7 @@ import { describe, expect, it } from "vitest";
|
|
|
2
2
|
import { ARBITRUM } from "../../configs/chains";
|
|
3
3
|
import { TOKENS } from "../../configs/tokens";
|
|
4
4
|
import { expandDecimals, PRECISION } from "../numbers";
|
|
5
|
-
import { parseContractPrice, convertToContractPrice, convertToContractTokenPrices, convertToTokenAmount, convertToUsd, getMidPrice, getIsEquivalentTokens, getTokenData, getTokensRatioByAmounts, getTokensRatioByMinOutputAmountAndTriggerPrice, } from "../tokens";
|
|
5
|
+
import { parseContractPrice, convertToContractPrice, convertToContractTokenPrices, convertToTokenAmount, convertToUsd, getMidPrice, getIsEquivalentTokens, getTokenData, getTokensRatioByAmounts, getTokensRatioByMinOutputAmountAndTriggerPrice, getTokensRatioByPrice, } from "../tokens";
|
|
6
6
|
function getToken(symbol) {
|
|
7
7
|
return TOKENS[ARBITRUM].find((token) => token.symbol === symbol);
|
|
8
8
|
}
|
|
@@ -68,18 +68,92 @@ describe("getTokenData", () => {
|
|
|
68
68
|
});
|
|
69
69
|
});
|
|
70
70
|
describe("getTokensRatioByAmounts", () => {
|
|
71
|
-
it("returns ratio of two token amounts", () => {
|
|
72
|
-
const fromToken = { decimals: 2 };
|
|
73
|
-
const toToken = { decimals: 2 };
|
|
71
|
+
it("for non-stablecoin pairs: returns ratio of two token amounts", () => {
|
|
72
|
+
const fromToken = { decimals: 2, isStable: false };
|
|
73
|
+
const toToken = { decimals: 2, isStable: false };
|
|
74
74
|
const result = getTokensRatioByAmounts({
|
|
75
75
|
fromToken,
|
|
76
76
|
toToken,
|
|
77
77
|
fromTokenAmount: 1000n,
|
|
78
78
|
toTokenAmount: 500n,
|
|
79
79
|
});
|
|
80
|
-
expect(result.largestToken).toEqual(
|
|
80
|
+
expect(result.largestToken).toEqual(toToken);
|
|
81
81
|
expect(result.ratio).toBe((((1000n * PRECISION) / expandDecimals(1, 2)) * PRECISION) / ((500n * PRECISION) / expandDecimals(1, 2)));
|
|
82
82
|
});
|
|
83
|
+
it("for non-stablecoin pairs: largestToken flips when amounts flip", () => {
|
|
84
|
+
const tokenA = { symbol: "A", decimals: 6, isStable: false };
|
|
85
|
+
const tokenB = { symbol: "B", decimals: 6, isStable: false };
|
|
86
|
+
const result1 = getTokensRatioByAmounts({
|
|
87
|
+
fromToken: tokenA,
|
|
88
|
+
toToken: tokenB,
|
|
89
|
+
fromTokenAmount: 1000n,
|
|
90
|
+
toTokenAmount: 500n,
|
|
91
|
+
});
|
|
92
|
+
expect(result1.largestToken).toBe(tokenB);
|
|
93
|
+
const result2 = getTokensRatioByAmounts({
|
|
94
|
+
fromToken: tokenA,
|
|
95
|
+
toToken: tokenB,
|
|
96
|
+
fromTokenAmount: 500n,
|
|
97
|
+
toTokenAmount: 1000n,
|
|
98
|
+
});
|
|
99
|
+
expect(result2.largestToken).toBe(tokenA);
|
|
100
|
+
});
|
|
101
|
+
it("for stablecoin pairs: largestToken is always toToken (fixed order)", () => {
|
|
102
|
+
const usdcToken = { symbol: "USDC", decimals: 6, isStable: true };
|
|
103
|
+
const usdtToken = { symbol: "USDT", decimals: 6, isStable: true };
|
|
104
|
+
const result1 = getTokensRatioByAmounts({
|
|
105
|
+
fromToken: usdcToken,
|
|
106
|
+
toToken: usdtToken,
|
|
107
|
+
fromTokenAmount: expandDecimals(100n, 6),
|
|
108
|
+
toTokenAmount: expandDecimals(99n, 6),
|
|
109
|
+
});
|
|
110
|
+
expect(result1.largestToken).toBe(usdtToken);
|
|
111
|
+
expect(result1.smallestToken).toBe(usdcToken);
|
|
112
|
+
const result2 = getTokensRatioByAmounts({
|
|
113
|
+
fromToken: usdcToken,
|
|
114
|
+
toToken: usdtToken,
|
|
115
|
+
fromTokenAmount: expandDecimals(99n, 6),
|
|
116
|
+
toTokenAmount: expandDecimals(100n, 6),
|
|
117
|
+
});
|
|
118
|
+
expect(result2.largestToken).toBe(usdtToken);
|
|
119
|
+
expect(result2.smallestToken).toBe(usdcToken);
|
|
120
|
+
});
|
|
121
|
+
it("for stablecoin pairs: ratio = fromAmount / toAmount", () => {
|
|
122
|
+
const usdcToken = { symbol: "USDC", decimals: 6, isStable: true };
|
|
123
|
+
const usdtToken = { symbol: "USDT", decimals: 6, isStable: true };
|
|
124
|
+
const fromAmount = expandDecimals(100n, 6);
|
|
125
|
+
const toAmount = expandDecimals(99n, 6);
|
|
126
|
+
const result = getTokensRatioByAmounts({
|
|
127
|
+
fromToken: usdcToken,
|
|
128
|
+
toToken: usdtToken,
|
|
129
|
+
fromTokenAmount: fromAmount,
|
|
130
|
+
toTokenAmount: toAmount,
|
|
131
|
+
});
|
|
132
|
+
const adjustedFromAmount = (fromAmount * PRECISION) / expandDecimals(1, 6);
|
|
133
|
+
const adjustedToAmount = (toAmount * PRECISION) / expandDecimals(1, 6);
|
|
134
|
+
const expectedRatio = (adjustedFromAmount * PRECISION) / adjustedToAmount;
|
|
135
|
+
expect(result.ratio).toBe(expectedRatio);
|
|
136
|
+
});
|
|
137
|
+
it("for stablecoin pairs: swapping from/to order changes the result", () => {
|
|
138
|
+
const usdcToken = { symbol: "USDC", decimals: 6, isStable: true };
|
|
139
|
+
const usdtToken = { symbol: "USDT", decimals: 6, isStable: true };
|
|
140
|
+
const usdcAmount = expandDecimals(100n, 6);
|
|
141
|
+
const usdtAmount = expandDecimals(99n, 6);
|
|
142
|
+
const result1 = getTokensRatioByAmounts({
|
|
143
|
+
fromToken: usdcToken,
|
|
144
|
+
toToken: usdtToken,
|
|
145
|
+
fromTokenAmount: usdcAmount,
|
|
146
|
+
toTokenAmount: usdtAmount,
|
|
147
|
+
});
|
|
148
|
+
const result2 = getTokensRatioByAmounts({
|
|
149
|
+
fromToken: usdtToken,
|
|
150
|
+
toToken: usdcToken,
|
|
151
|
+
fromTokenAmount: usdtAmount,
|
|
152
|
+
toTokenAmount: usdcAmount,
|
|
153
|
+
});
|
|
154
|
+
expect(result1.largestToken).toBe(usdtToken);
|
|
155
|
+
expect(result2.largestToken).toBe(usdcToken);
|
|
156
|
+
});
|
|
83
157
|
});
|
|
84
158
|
describe("getTokensRatioByMinOutputAmountAndTriggerPrice", () => {
|
|
85
159
|
it("returns ratio of two token amounts in case if triggerPrice is 0n", () => {
|
|
@@ -110,4 +184,117 @@ describe("getTokensRatioByMinOutputAmountAndTriggerPrice", () => {
|
|
|
110
184
|
expect(result.ratio).toBe(100n);
|
|
111
185
|
expect(result.allowedSwapSlippageBps).toBe(9999n);
|
|
112
186
|
});
|
|
187
|
+
it("for stablecoin pairs: largestToken is always toToken (fixed order)", () => {
|
|
188
|
+
const usdcToken = { symbol: "USDC", decimals: 6, isStable: true };
|
|
189
|
+
const usdtToken = { symbol: "USDT", decimals: 6, isStable: true };
|
|
190
|
+
const result1 = getTokensRatioByMinOutputAmountAndTriggerPrice({
|
|
191
|
+
fromToken: usdcToken,
|
|
192
|
+
toToken: usdtToken,
|
|
193
|
+
fromTokenAmount: expandDecimals(100n, 6),
|
|
194
|
+
toTokenAmount: expandDecimals(99n, 6),
|
|
195
|
+
triggerPrice: 0n,
|
|
196
|
+
minOutputAmount: expandDecimals(99n, 6),
|
|
197
|
+
});
|
|
198
|
+
expect(result1.largestToken).toBe(usdtToken);
|
|
199
|
+
expect(result1.smallestToken).toBe(usdcToken);
|
|
200
|
+
const result2 = getTokensRatioByMinOutputAmountAndTriggerPrice({
|
|
201
|
+
fromToken: usdcToken,
|
|
202
|
+
toToken: usdtToken,
|
|
203
|
+
fromTokenAmount: expandDecimals(99n, 6),
|
|
204
|
+
toTokenAmount: expandDecimals(100n, 6),
|
|
205
|
+
triggerPrice: 0n,
|
|
206
|
+
minOutputAmount: expandDecimals(100n, 6),
|
|
207
|
+
});
|
|
208
|
+
expect(result2.largestToken).toBe(usdtToken);
|
|
209
|
+
expect(result2.smallestToken).toBe(usdcToken);
|
|
210
|
+
});
|
|
211
|
+
});
|
|
212
|
+
describe("getTokensRatioByPrice", () => {
|
|
213
|
+
it("for non-stablecoin pairs: largestToken is determined by price", () => {
|
|
214
|
+
const ethToken = { symbol: "ETH", isStable: false };
|
|
215
|
+
const usdcToken = { symbol: "USDC", isStable: true };
|
|
216
|
+
const result = getTokensRatioByPrice({
|
|
217
|
+
fromToken: ethToken,
|
|
218
|
+
toToken: usdcToken,
|
|
219
|
+
fromPrice: 2000n * PRECISION,
|
|
220
|
+
toPrice: 1n * PRECISION,
|
|
221
|
+
});
|
|
222
|
+
expect(result.largestToken).toBe(ethToken);
|
|
223
|
+
expect(result.smallestToken).toBe(usdcToken);
|
|
224
|
+
expect(result.ratio).toBe((2000n * PRECISION * PRECISION) / (1n * PRECISION));
|
|
225
|
+
});
|
|
226
|
+
it("for non-stablecoin pairs: largestToken flips when prices flip", () => {
|
|
227
|
+
const tokenA = { symbol: "A", isStable: false };
|
|
228
|
+
const tokenB = { symbol: "B", isStable: false };
|
|
229
|
+
const result1 = getTokensRatioByPrice({
|
|
230
|
+
fromToken: tokenA,
|
|
231
|
+
toToken: tokenB,
|
|
232
|
+
fromPrice: 100n * PRECISION,
|
|
233
|
+
toPrice: 50n * PRECISION,
|
|
234
|
+
});
|
|
235
|
+
expect(result1.largestToken).toBe(tokenA);
|
|
236
|
+
const result2 = getTokensRatioByPrice({
|
|
237
|
+
fromToken: tokenA,
|
|
238
|
+
toToken: tokenB,
|
|
239
|
+
fromPrice: 50n * PRECISION,
|
|
240
|
+
toPrice: 100n * PRECISION,
|
|
241
|
+
});
|
|
242
|
+
expect(result2.largestToken).toBe(tokenB);
|
|
243
|
+
});
|
|
244
|
+
it("for stablecoin pairs: largestToken is always toToken (fixed order)", () => {
|
|
245
|
+
const usdcToken = { symbol: "USDC", isStable: true };
|
|
246
|
+
const usdtToken = { symbol: "USDT", isStable: true };
|
|
247
|
+
const result1 = getTokensRatioByPrice({
|
|
248
|
+
fromToken: usdcToken,
|
|
249
|
+
toToken: usdtToken,
|
|
250
|
+
fromPrice: expandDecimals(1001n, 27),
|
|
251
|
+
toPrice: expandDecimals(999n, 27),
|
|
252
|
+
});
|
|
253
|
+
expect(result1.largestToken).toBe(usdtToken);
|
|
254
|
+
expect(result1.smallestToken).toBe(usdcToken);
|
|
255
|
+
const result2 = getTokensRatioByPrice({
|
|
256
|
+
fromToken: usdcToken,
|
|
257
|
+
toToken: usdtToken,
|
|
258
|
+
fromPrice: expandDecimals(999n, 27),
|
|
259
|
+
toPrice: expandDecimals(1001n, 27),
|
|
260
|
+
});
|
|
261
|
+
expect(result2.largestToken).toBe(usdtToken);
|
|
262
|
+
expect(result2.smallestToken).toBe(usdcToken);
|
|
263
|
+
});
|
|
264
|
+
it("for stablecoin pairs: ratio = toPrice / fromPrice", () => {
|
|
265
|
+
const usdcToken = { symbol: "USDC", isStable: true };
|
|
266
|
+
const usdtToken = { symbol: "USDT", isStable: true };
|
|
267
|
+
const fromPrice = expandDecimals(1001n, 27);
|
|
268
|
+
const toPrice = expandDecimals(999n, 27);
|
|
269
|
+
const result = getTokensRatioByPrice({
|
|
270
|
+
fromToken: usdcToken,
|
|
271
|
+
toToken: usdtToken,
|
|
272
|
+
fromPrice,
|
|
273
|
+
toPrice,
|
|
274
|
+
});
|
|
275
|
+
const expectedRatio = (toPrice * PRECISION) / fromPrice;
|
|
276
|
+
expect(result.ratio).toBe(expectedRatio);
|
|
277
|
+
});
|
|
278
|
+
it("for stablecoin pairs: swapping from/to order changes the result", () => {
|
|
279
|
+
const usdcToken = { symbol: "USDC", isStable: true };
|
|
280
|
+
const usdtToken = { symbol: "USDT", isStable: true };
|
|
281
|
+
const usdcPrice = expandDecimals(1001n, 27);
|
|
282
|
+
const usdtPrice = expandDecimals(999n, 27);
|
|
283
|
+
const result1 = getTokensRatioByPrice({
|
|
284
|
+
fromToken: usdcToken,
|
|
285
|
+
toToken: usdtToken,
|
|
286
|
+
fromPrice: usdcPrice,
|
|
287
|
+
toPrice: usdtPrice,
|
|
288
|
+
});
|
|
289
|
+
const result2 = getTokensRatioByPrice({
|
|
290
|
+
fromToken: usdtToken,
|
|
291
|
+
toToken: usdcToken,
|
|
292
|
+
fromPrice: usdtPrice,
|
|
293
|
+
toPrice: usdcPrice,
|
|
294
|
+
});
|
|
295
|
+
expect(result1.largestToken).toBe(usdtToken);
|
|
296
|
+
expect(result2.largestToken).toBe(usdcToken);
|
|
297
|
+
expect(result1.ratio).toBe((usdtPrice * PRECISION) / usdcPrice);
|
|
298
|
+
expect(result2.ratio).toBe((usdcPrice * PRECISION) / usdtPrice);
|
|
299
|
+
});
|
|
113
300
|
});
|
|
@@ -1,7 +1,29 @@
|
|
|
1
1
|
import cryptoJs from "crypto-js";
|
|
2
|
-
import { decodeErrorResult } from "viem";
|
|
2
|
+
import { decodeErrorResult, } from "viem";
|
|
3
3
|
import { abis } from "../../abis";
|
|
4
4
|
import { extractDataFromError, extractTxnError, getIsUserError, getIsUserRejectedError, } from "./transactionsErrors";
|
|
5
|
+
export function extractErrorDataFromViemError(error) {
|
|
6
|
+
if (!("walk" in error && typeof error.walk === "function")) {
|
|
7
|
+
return undefined;
|
|
8
|
+
}
|
|
9
|
+
let data;
|
|
10
|
+
error.walk((e) => {
|
|
11
|
+
if (typeof e?.data === "string") {
|
|
12
|
+
data = e.data;
|
|
13
|
+
return true;
|
|
14
|
+
}
|
|
15
|
+
if (typeof e?.data?.data === "string") {
|
|
16
|
+
data = e.data.data;
|
|
17
|
+
return true;
|
|
18
|
+
}
|
|
19
|
+
if (typeof e?.raw === "string") {
|
|
20
|
+
data = e.raw;
|
|
21
|
+
return true;
|
|
22
|
+
}
|
|
23
|
+
return false;
|
|
24
|
+
});
|
|
25
|
+
return data;
|
|
26
|
+
}
|
|
5
27
|
const URL_REGEXP = /((?:http[s]?:\/\/.)?(?:www\.)?[-a-zA-Z0-9@%._\\+~#=]{2,256}\.[a-z]{2,6}\b(?::\d+)?)(?:[-a-zA-Z0-9@:%_\\+.~#?&\\/\\/=]*)/gi;
|
|
6
28
|
const MAX_ERRORS_DEPTH = 1;
|
|
7
29
|
export function extendError(error, params) {
|
|
@@ -67,16 +89,23 @@ export function parseError(error, errorDepth = 0) {
|
|
|
67
89
|
catch (e) {
|
|
68
90
|
//
|
|
69
91
|
}
|
|
70
|
-
if (
|
|
71
|
-
const errorData =
|
|
92
|
+
if (!contractError) {
|
|
93
|
+
const errorData = (error && typeof error === "object" ? extractErrorDataFromViemError(error) : undefined) ??
|
|
94
|
+
extractDataFromError(errorMessage) ??
|
|
95
|
+
extractDataFromError(error?.message);
|
|
72
96
|
if (errorData) {
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
97
|
+
try {
|
|
98
|
+
const parsedError = decodeErrorResult({
|
|
99
|
+
abi: abis.CustomErrors,
|
|
100
|
+
data: errorData,
|
|
101
|
+
});
|
|
102
|
+
if (parsedError) {
|
|
103
|
+
contractError = parsedError.errorName;
|
|
104
|
+
contractErrorArgs = parsedError.args;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
catch {
|
|
108
|
+
//
|
|
80
109
|
}
|
|
81
110
|
}
|
|
82
111
|
}
|
|
@@ -150,23 +179,34 @@ export class CustomError extends Error {
|
|
|
150
179
|
export function isCustomError(error) {
|
|
151
180
|
return error?.isGmxCustomError === true;
|
|
152
181
|
}
|
|
153
|
-
export function
|
|
154
|
-
const data = error?.info?.error?.data ?? error?.data;
|
|
155
|
-
let prettyErrorName = error.name;
|
|
156
|
-
let prettyErrorMessage = error.message;
|
|
157
|
-
let prettyErrorArgs = undefined;
|
|
182
|
+
export function tryDecodeCustomError(reasonBytes) {
|
|
158
183
|
try {
|
|
159
|
-
const
|
|
184
|
+
const decoded = decodeErrorResult({
|
|
160
185
|
abi: abis.CustomErrors,
|
|
161
|
-
data:
|
|
186
|
+
data: reasonBytes,
|
|
162
187
|
});
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
188
|
+
// Convert args array to key-value dictionary for backward compatibility
|
|
189
|
+
if (decoded.args &&
|
|
190
|
+
Array.isArray(decoded.args) &&
|
|
191
|
+
decoded.abiItem &&
|
|
192
|
+
"inputs" in decoded.abiItem &&
|
|
193
|
+
decoded.abiItem.inputs) {
|
|
194
|
+
const argsDict = {};
|
|
195
|
+
for (const [index, input] of decoded.abiItem.inputs.entries()) {
|
|
196
|
+
argsDict[input.name] = decoded.args[index];
|
|
197
|
+
}
|
|
198
|
+
return { name: decoded.errorName, args: argsDict };
|
|
199
|
+
}
|
|
200
|
+
return { name: decoded.errorName, args: undefined };
|
|
201
|
+
}
|
|
202
|
+
catch {
|
|
203
|
+
return undefined;
|
|
166
204
|
}
|
|
167
|
-
|
|
168
|
-
|
|
205
|
+
}
|
|
206
|
+
export function decodeErrorFromViemError(error) {
|
|
207
|
+
const data = extractErrorDataFromViemError(error);
|
|
208
|
+
if (!data) {
|
|
209
|
+
return undefined;
|
|
169
210
|
}
|
|
170
|
-
|
|
171
|
-
return prettyError;
|
|
211
|
+
return tryDecodeCustomError(data);
|
|
172
212
|
}
|
|
@@ -3,8 +3,8 @@ import { USD_DECIMALS } from "../../configs/factors";
|
|
|
3
3
|
import { NATIVE_TOKEN_ADDRESS } from "../../configs/tokens";
|
|
4
4
|
import { bigMath } from "../bigmath";
|
|
5
5
|
import { applyFactor, expandDecimals } from "../numbers";
|
|
6
|
-
import { convertBetweenTokens, convertToTokenAmount, convertToUsd, getTokenData } from "../tokens";
|
|
7
6
|
import { DecreasePositionSwapType } from "../orders/types";
|
|
7
|
+
import { convertBetweenTokens, convertToTokenAmount, convertToUsd, getTokenData } from "../tokens";
|
|
8
8
|
export function getExecutionFee(chainId, gasLimits,
|
|
9
9
|
// TODO optimize we only need the native token data
|
|
10
10
|
tokensData, estimatedGasLimit, gasPrice, oraclePriceCount, numberOfParts) {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { bigMath } from "../bigmath";
|
|
2
2
|
import { getOpenInterestForBalance, getTokenPoolType } from "../markets";
|
|
3
|
-
import { applyFactor, expandDecimals, getBasisPoints, roundUpMagnitudeDivision } from "../numbers";
|
|
3
|
+
import { applyFactor, BASIS_POINTS_DECIMALS, expandDecimals, getBasisPoints, PRECISION_DECIMALS, roundUpMagnitudeDivision, } from "../numbers";
|
|
4
4
|
import { convertToTokenAmount, convertToUsd, getMidPrice } from "../tokens";
|
|
5
5
|
import { bigNumberify } from "../tradeHistory";
|
|
6
6
|
export function getPriceImpactByAcceptablePrice(p) {
|
|
@@ -352,3 +352,7 @@ export function getCappedPriceImpactPercentageFromFees({ fees, isSwap, }) {
|
|
|
352
352
|
}
|
|
353
353
|
return fees?.positionNetPriceImpact?.precisePercentage ?? 0n;
|
|
354
354
|
}
|
|
355
|
+
export function getMaxNegativeImpactBps(marketInfo) {
|
|
356
|
+
const factor = marketInfo.maxPositionImpactFactorNegative;
|
|
357
|
+
return factor / expandDecimals(1, PRECISION_DECIMALS - BASIS_POINTS_DECIMALS);
|
|
358
|
+
}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { zeroAddress } from "viem";
|
|
2
2
|
import { USD_DECIMALS } from "../../../configs/factors";
|
|
3
|
+
import { usdToToken } from "../../../test/mock";
|
|
3
4
|
import { getMarketFullName } from "..";
|
|
4
5
|
import { expandDecimals } from "../../numbers";
|
|
5
6
|
import { getTokenData } from "../../tokens";
|
|
6
|
-
import { usdToToken } from "../../../test/mock";
|
|
7
7
|
/**
|
|
8
8
|
* @param marketKeys - array of market keys in the following format: indexToken-longToken-shortToken
|
|
9
9
|
*/
|
|
@@ -6,6 +6,8 @@ export const BASIS_POINTS_DIVISOR_BIGINT = 10000n;
|
|
|
6
6
|
export const BASIS_POINTS_DECIMALS = 4;
|
|
7
7
|
export const PRECISION_DECIMALS = 30;
|
|
8
8
|
export const PRECISION = expandDecimals(1, PRECISION_DECIMALS);
|
|
9
|
+
export const FLOAT_PRECISION_SQRT_DECIMALS = 15;
|
|
10
|
+
export const FLOAT_PRECISION_SQRT = expandDecimals(1, FLOAT_PRECISION_SQRT_DECIMALS);
|
|
9
11
|
export const BN_ZERO = 0n;
|
|
10
12
|
export const BN_ONE = 1n;
|
|
11
13
|
export const BN_NEGATIVE_ONE = -1n;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { deserializeBigIntsInObject } from "../numbers";
|
|
2
|
+
export async function fetchApiOrders(ctx, params) {
|
|
3
|
+
const orders = await ctx.api.fetchJson("/orders", {
|
|
4
|
+
query: {
|
|
5
|
+
account: params.account,
|
|
6
|
+
},
|
|
7
|
+
});
|
|
8
|
+
return orders.map((o) => deserializeBigIntsInObject(o, { handleInts: true }));
|
|
9
|
+
}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { BASIS_POINTS_DIVISOR_BIGINT, DEFAULT_ALLOWED_SWAP_SLIPPAGE_BPS } from "../../configs/factors";
|
|
2
|
-
import { getSwapPathOutputAddresses, getSwapPathStats } from "../swap/swapStats";
|
|
3
2
|
import { bigMath } from "../bigmath";
|
|
4
3
|
import { getByKey } from "../objects";
|
|
5
|
-
import { OrderType, SwapPricingType, } from "./types";
|
|
6
4
|
import { getPositionKey, parsePositionKey } from "../positions";
|
|
7
5
|
import { getOrderThresholdType } from "../prices";
|
|
6
|
+
import { getSwapPathOutputAddresses, getSwapPathStats } from "../swap/swapStats";
|
|
8
7
|
import { convertToTokenAmount, convertToUsd, getTokensRatioByAmounts, getTokensRatioByMinOutputAmountAndTriggerPrice, parseContractPrice, } from "../tokens";
|
|
8
|
+
import { OrderType, SwapPricingType, } from "./types";
|
|
9
9
|
export function isMarketOrderType(orderType) {
|
|
10
10
|
return [OrderType.MarketDecrease, OrderType.MarketIncrease, OrderType.MarketSwap].includes(orderType);
|
|
11
11
|
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { deserializeBigIntsInObject } from "../numbers";
|
|
2
|
+
export async function fetchApiPositionsInfo(ctx, params) {
|
|
3
|
+
const positions = await ctx.api.fetchJson("/positions", {
|
|
4
|
+
query: {
|
|
5
|
+
account: params.account,
|
|
6
|
+
includeRelatedOrders: params.includeRelatedOrders || undefined,
|
|
7
|
+
},
|
|
8
|
+
});
|
|
9
|
+
return positions.map((p) => deserializeBigIntsInObject(p, { handleInts: true }));
|
|
10
|
+
}
|