@gmx-io/sdk 2.2.0 → 2.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/cjs/src/abis/TransitStation.js +2222 -0
- package/build/cjs/src/abis/index.js +2 -0
- package/build/cjs/src/clients/v2/index.js +54 -27
- package/build/cjs/src/codegen/prebuilt/hashedKinkModelMarketRatesKeys.json +32 -0
- package/build/cjs/src/codegen/prebuilt/hashedMarketConfigKeys.json +272 -0
- package/build/cjs/src/codegen/prebuilt/hashedMarketValuesKeys.json +60 -0
- package/build/cjs/src/configs/markets.js +28 -0
- package/build/cjs/src/configs/tokens.js +9 -0
- package/build/cjs/src/test/mock.js +32 -4
- package/build/cjs/src/utils/markets/utils.js +45 -1
- package/build/cjs/src/utils/paxos/__tests__/api.spec.js +128 -0
- package/build/cjs/src/utils/paxos/api.js +129 -0
- package/build/cjs/src/utils/paxos/types.js +2 -0
- package/build/cjs/src/utils/trade/__tests__/increaseMarginCheck.spec.js +14 -35
- package/build/cjs/src/utils/trade/__tests__/twapIncreaseMarginCheck.spec.js +368 -0
- package/build/cjs/src/utils/trade/increaseMarginCheck.js +27 -13
- package/build/cjs/src/utils/trade/twapIncreaseMarginCheck.js +180 -0
- package/build/cjs/src/utils/twap/index.js +13 -4
- package/build/cjs/src/utils/usdgBoostApr/api.js +7 -0
- package/build/cjs/src/utils/usdgBoostApr/types.js +2 -0
- package/build/cjs/src/utils/whitelists/api.js +9 -0
- package/build/cjs/src/utils/whitelists/types.js +2 -0
- package/build/esm/src/abis/TransitStation.js +2220 -0
- package/build/esm/src/abis/index.js +2 -0
- package/build/esm/src/clients/v2/index.js +27 -0
- package/build/esm/src/codegen/prebuilt/hashedKinkModelMarketRatesKeys.json +32 -0
- package/build/esm/src/codegen/prebuilt/hashedMarketConfigKeys.json +272 -0
- package/build/esm/src/codegen/prebuilt/hashedMarketValuesKeys.json +60 -0
- package/build/esm/src/configs/markets.js +28 -0
- package/build/esm/src/configs/tokens.js +9 -0
- package/build/esm/src/test/mock.js +27 -0
- package/build/esm/src/utils/markets/utils.js +44 -1
- package/build/esm/src/utils/paxos/__tests__/api.spec.js +126 -0
- package/build/esm/src/utils/paxos/api.js +120 -0
- package/build/esm/src/utils/paxos/types.js +1 -0
- package/build/esm/src/utils/trade/__tests__/increaseMarginCheck.spec.js +15 -36
- package/build/esm/src/utils/trade/__tests__/twapIncreaseMarginCheck.spec.js +366 -0
- package/build/esm/src/utils/trade/increaseMarginCheck.js +24 -11
- package/build/esm/src/utils/trade/twapIncreaseMarginCheck.js +172 -0
- package/build/esm/src/utils/twap/index.js +10 -3
- package/build/esm/src/utils/usdgBoostApr/api.js +3 -0
- package/build/esm/src/utils/usdgBoostApr/types.js +1 -0
- package/build/esm/src/utils/whitelists/api.js +5 -0
- package/build/esm/src/utils/whitelists/types.js +1 -0
- package/build/tsconfig.tsbuildinfo +1 -1
- package/build/types/src/abis/TransitStation.d.ts +1710 -0
- package/build/types/src/abis/index.d.ts +1709 -0
- package/build/types/src/clients/v2/index.d.ts +20 -0
- package/build/types/src/codegen/prebuilt/index.d.ts +364 -0
- package/build/types/src/test/mock.d.ts +5 -0
- package/build/types/src/utils/markets/utils.d.ts +3 -0
- package/build/types/src/utils/paxos/__tests__/api.spec.d.ts +1 -0
- package/build/types/src/utils/paxos/api.d.ts +24 -0
- package/build/types/src/utils/paxos/types.d.ts +139 -0
- package/build/types/src/utils/trade/__tests__/twapIncreaseMarginCheck.spec.d.ts +1 -0
- package/build/types/src/utils/trade/increaseMarginCheck.d.ts +15 -7
- package/build/types/src/utils/trade/twapIncreaseMarginCheck.d.ts +50 -0
- package/build/types/src/utils/trade/types.d.ts +2 -0
- package/build/types/src/utils/twap/index.d.ts +2 -0
- package/build/types/src/utils/usdgBoostApr/api.d.ts +5 -0
- package/build/types/src/utils/usdgBoostApr/types.d.ts +19 -0
- package/build/types/src/utils/whitelists/api.d.ts +7 -0
- package/build/types/src/utils/whitelists/types.d.ts +6 -0
- package/package.json +10 -1
|
@@ -126,6 +126,15 @@ exports.TOKENS = {
|
|
|
126
126
|
explorerUrl: "https://arbiscan.io/address/0xaf88d065e77c8cC2239327C5EDb3A432268e5831",
|
|
127
127
|
isPermitSupported: true,
|
|
128
128
|
},
|
|
129
|
+
{
|
|
130
|
+
name: "Global Dollar",
|
|
131
|
+
symbol: "USDG",
|
|
132
|
+
decimals: 6,
|
|
133
|
+
address: "0x004B506865409877C9fA29bfb1ebA929984B9bbC",
|
|
134
|
+
isStable: true,
|
|
135
|
+
imageUrl: "https://coin-images.coingecko.com/coins/images/51281/small/GDN_USDG_Token_200x200.png?1730484111",
|
|
136
|
+
explorerUrl: "https://arbiscan.io/token/0x004B506865409877C9fA29bfb1ebA929984B9bbC",
|
|
137
|
+
},
|
|
129
138
|
{
|
|
130
139
|
name: "Tether",
|
|
131
140
|
symbol: "USDT",
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createSeededRandom = exports.mockExternalSwap = exports.mockMarketsInfoData = exports.mockMarketsData = exports.mockTokensData = exports.mockMarketKeys = exports.MOCK_GAS_PRICE = exports.usdToToken = void 0;
|
|
3
|
+
exports.createSeededRandom = exports.mockExternalSwap = exports.mockMarginCheckMarketInfo = exports.mockMarketsInfoData = exports.mockMarketsData = exports.mockTokensData = exports.mockMarketKeys = exports.MOCK_GAS_PRICE = exports.usdToToken = void 0;
|
|
4
4
|
const factors_1 = require("../configs/factors");
|
|
5
|
+
const mockMarkets_1 = require("../utils/markets/__tests__/mockMarkets");
|
|
5
6
|
const numbers_1 = require("../utils/numbers");
|
|
6
7
|
const tokens_1 = require("../utils/tokens");
|
|
7
8
|
const types_1 = require("../utils/trade/types");
|
|
@@ -134,9 +135,36 @@ exports.mockTokensData = mockTokensData;
|
|
|
134
135
|
/**
|
|
135
136
|
* @param marketKeys - array of market keys in the following format: indexToken-longToken-shortToken
|
|
136
137
|
*/
|
|
137
|
-
var
|
|
138
|
-
Object.defineProperty(exports, "mockMarketsData", { enumerable: true, get: function () { return
|
|
139
|
-
Object.defineProperty(exports, "mockMarketsInfoData", { enumerable: true, get: function () { return
|
|
138
|
+
var mockMarkets_2 = require("../utils/markets/__tests__/mockMarkets");
|
|
139
|
+
Object.defineProperty(exports, "mockMarketsData", { enumerable: true, get: function () { return mockMarkets_2.mockMarketsData; } });
|
|
140
|
+
Object.defineProperty(exports, "mockMarketsInfoData", { enumerable: true, get: function () { return mockMarkets_2.mockMarketsInfoData; } });
|
|
141
|
+
/**
|
|
142
|
+
* A BTC market with a 1% min collateral factor (100x) and 0.5% for liquidation.
|
|
143
|
+
* Fees, price impact and open interest are off unless overridden.
|
|
144
|
+
*/
|
|
145
|
+
function mockMarginCheckMarketInfo(tokensData, overrides = {}) {
|
|
146
|
+
return (0, mockMarkets_1.mockMarketsInfoData)(tokensData, ["BTC-BTC-USDC"], {
|
|
147
|
+
"BTC-BTC-USDC": {
|
|
148
|
+
minCollateralFactor: (0, numbers_1.expandDecimals)(1, 28),
|
|
149
|
+
minCollateralFactorForLiquidation: (0, numbers_1.expandDecimals)(5, 27),
|
|
150
|
+
minCollateralFactorForOpenInterestLong: 0n,
|
|
151
|
+
minCollateralFactorForOpenInterestShort: 0n,
|
|
152
|
+
positionFeeFactorForBalanceWasImproved: 0n,
|
|
153
|
+
positionFeeFactorForBalanceWasNotImproved: 0n,
|
|
154
|
+
positionImpactFactorPositive: 0n,
|
|
155
|
+
positionImpactFactorNegative: 0n,
|
|
156
|
+
maxPositionImpactFactorPositive: 0n,
|
|
157
|
+
maxPositionImpactFactorNegative: 0n,
|
|
158
|
+
maxPositionImpactFactorForLiquidations: 0n,
|
|
159
|
+
longInterestUsd: 0n,
|
|
160
|
+
shortInterestUsd: 0n,
|
|
161
|
+
longInterestInTokens: 0n,
|
|
162
|
+
shortInterestInTokens: 0n,
|
|
163
|
+
...overrides,
|
|
164
|
+
},
|
|
165
|
+
})["BTC-BTC-USDC"];
|
|
166
|
+
}
|
|
167
|
+
exports.mockMarginCheckMarketInfo = mockMarginCheckMarketInfo;
|
|
140
168
|
function mockExternalSwap({ inToken, outToken, amountIn, amountOut, priceIn, priceOut, feesUsd = (0, numbers_1.expandDecimals)(5, factors_1.USD_DECIMALS), // $5 default fee
|
|
141
169
|
data = "0x1", to = "0x6352a56caadC4F1E25CD6c75970Fa768A3304e64", receiver = "0x1234567890123456789012345678901234567890", }) {
|
|
142
170
|
const usdIn = (amountIn * priceIn) / (0, numbers_1.expandDecimals)(1, inToken.decimals);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getMarketAddressByName = exports.getMarketWithTiers = exports.composeFullMarketsInfoData = exports.hydrateMarketInfo = exports.mergeMarketsConfigValues = exports.composeRawMarketsInfoData = exports.composeRawMarketInfo = exports.getMarketTicker = exports.getMarket24Stats = exports.getIsMarketDeprecated = exports.getIsMarketAvailableForExpressSwaps = exports.getPriceForPnl = exports.getOpenInterestForBalance = exports.getMarketInfoWithOpenInterestDelta = exports.getOpenInterestInTokens = exports.getOpenInterestUsd = exports.getPositiveMarketPnl = exports.getMarketPnl = exports.getOiInTokensFromRawValues = exports.getOiUsdFromRawValues = exports.getMarketDivisor = exports.getReservedUsd = exports.getAvailableUsdLiquidityForCollateral = exports.getOppositeCollateralFromConfig = exports.getOppositeCollateral = exports.getMaxAllowedLeverage = exports.getMaxLeverageByMinCollateralFactor = exports.getCappedPoolPnl = exports.getPoolUsdWithoutPnl = exports.getTokenPoolType = exports.getContractMarketPrices = exports.getMarketPoolName = exports.getMarketBaseName = exports.getMarketIndexName = exports.getMarketFullName = void 0;
|
|
3
|
+
exports.getMarketAddressByName = exports.getMarketWithTiers = exports.composeFullMarketsInfoData = exports.hydrateMarketInfo = exports.mergeMarketsConfigValues = exports.composeRawMarketsInfoData = exports.composeRawMarketInfo = exports.getMarketTicker = exports.getMarket24Stats = exports.getIsMarketDeprecated = exports.getIsMarketAvailableForExpressSwaps = exports.getPriceForPnl = exports.getOpenInterestForBalance = exports.getMarketInfoWithSwapDelta = exports.getMarketInfoWithOpenInterestDelta = exports.getOpenInterestInTokens = exports.getOpenInterestUsd = exports.getPositiveMarketPnl = exports.getMarketPnl = exports.getOiInTokensFromRawValues = exports.getOiUsdFromRawValues = exports.getMarketDivisor = exports.getReservedUsd = exports.getAvailableUsdLiquidityForCollateral = exports.getOppositeCollateralFromConfig = exports.getOppositeCollateral = exports.getMaxAllowedLeverage = exports.getMaxLeverageByMinCollateralFactor = exports.getCappedPoolPnl = exports.getPoolUsdWithoutPnl = exports.getTokenPoolType = exports.getContractMarketPrices = exports.getMarketPoolName = exports.getMarketBaseName = exports.getMarketIndexName = exports.getMarketFullName = void 0;
|
|
4
4
|
const viem_1 = require("viem");
|
|
5
5
|
const factors_1 = require("../../configs/factors");
|
|
6
6
|
const markets_1 = require("../../configs/markets");
|
|
@@ -334,6 +334,50 @@ function getMarketInfoWithOpenInterestDelta({ marketInfo, collateralToken, isLon
|
|
|
334
334
|
return next;
|
|
335
335
|
}
|
|
336
336
|
exports.getMarketInfoWithOpenInterestDelta = getMarketInfoWithOpenInterestDelta;
|
|
337
|
+
/** The market a swap step leaves: the pools and the swap impact pools move as in `SwapUtils._swap`. */
|
|
338
|
+
function getMarketInfoWithSwapDelta(marketInfo, swapStep) {
|
|
339
|
+
const isLongTokenIn = getTokenPoolType(marketInfo, swapStep.tokenInAddress) === "long";
|
|
340
|
+
const tokenIn = isLongTokenIn ? marketInfo.longToken : marketInfo.shortToken;
|
|
341
|
+
const tokenOut = isLongTokenIn ? marketInfo.shortToken : marketInfo.longToken;
|
|
342
|
+
let poolAmountIn = swapStep.amountInAfterFees;
|
|
343
|
+
let poolAmountOut = swapStep.amountOut;
|
|
344
|
+
let impactPoolDeltaIn = 0n;
|
|
345
|
+
let impactPoolDeltaOut = 0n;
|
|
346
|
+
if (swapStep.priceImpactDeltaUsd > 0n) {
|
|
347
|
+
const { impactDeltaAmount, cappedDiffUsd } = (0, fees_1.applySwapImpactWithCap)(marketInfo, tokenOut, swapStep.priceImpactDeltaUsd);
|
|
348
|
+
poolAmountOut -= impactDeltaAmount;
|
|
349
|
+
impactPoolDeltaOut = -impactDeltaAmount;
|
|
350
|
+
if (cappedDiffUsd > 0n) {
|
|
351
|
+
const tokenInImpactAmount = (0, fees_1.applySwapImpactWithCap)(marketInfo, tokenIn, cappedDiffUsd).impactDeltaAmount;
|
|
352
|
+
poolAmountIn += tokenInImpactAmount;
|
|
353
|
+
impactPoolDeltaIn = -tokenInImpactAmount;
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
else if (swapStep.priceImpactDeltaUsd < 0n) {
|
|
357
|
+
const { impactDeltaAmount } = (0, fees_1.applySwapImpactWithCap)(marketInfo, tokenIn, swapStep.priceImpactDeltaUsd);
|
|
358
|
+
poolAmountIn += impactDeltaAmount;
|
|
359
|
+
impactPoolDeltaIn = -impactDeltaAmount;
|
|
360
|
+
}
|
|
361
|
+
const [longDelta, shortDelta] = isLongTokenIn ? [poolAmountIn, -poolAmountOut] : [-poolAmountOut, poolAmountIn];
|
|
362
|
+
const [longImpactDelta, shortImpactDelta] = isLongTokenIn
|
|
363
|
+
? [impactPoolDeltaIn, impactPoolDeltaOut]
|
|
364
|
+
: [impactPoolDeltaOut, impactPoolDeltaIn];
|
|
365
|
+
const next = {
|
|
366
|
+
...marketInfo,
|
|
367
|
+
longPoolAmount: marketInfo.longPoolAmount + longDelta,
|
|
368
|
+
shortPoolAmount: marketInfo.shortPoolAmount + shortDelta,
|
|
369
|
+
swapImpactPoolAmountLong: marketInfo.swapImpactPoolAmountLong + longImpactDelta,
|
|
370
|
+
swapImpactPoolAmountShort: marketInfo.swapImpactPoolAmountShort + shortImpactDelta,
|
|
371
|
+
};
|
|
372
|
+
if (marketInfo.virtualPoolAmountForLongToken > 0n) {
|
|
373
|
+
next.virtualPoolAmountForLongToken = marketInfo.virtualPoolAmountForLongToken + longDelta;
|
|
374
|
+
}
|
|
375
|
+
if (marketInfo.virtualPoolAmountForShortToken > 0n) {
|
|
376
|
+
next.virtualPoolAmountForShortToken = marketInfo.virtualPoolAmountForShortToken + shortDelta;
|
|
377
|
+
}
|
|
378
|
+
return next;
|
|
379
|
+
}
|
|
380
|
+
exports.getMarketInfoWithSwapDelta = getMarketInfoWithSwapDelta;
|
|
337
381
|
function getOpenInterestForBalance(marketInfo, isLong) {
|
|
338
382
|
if (marketInfo.useOpenInterestInTokensForBalance) {
|
|
339
383
|
const interestInTokens = isLong ? marketInfo.longInterestInTokens : marketInfo.shortInterestInTokens;
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const vitest_1 = require("vitest");
|
|
4
|
+
const api_1 = require("../api");
|
|
5
|
+
const API_URL = "https://example.test";
|
|
6
|
+
const USER = "0x1111111111111111111111111111111111111111";
|
|
7
|
+
const USDC = "0xaf88d065e77c8cC2239327C5EDb3A432268e5831";
|
|
8
|
+
const USDG = "0x004B506865409877C9fA29bfb1ebA929984B9bbC";
|
|
9
|
+
const ORDER_ID = "0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef";
|
|
10
|
+
function createApi(calls, response = {}) {
|
|
11
|
+
return {
|
|
12
|
+
url: API_URL,
|
|
13
|
+
fetchJson: async (path, opts) => {
|
|
14
|
+
calls.push({ path, query: opts?.query });
|
|
15
|
+
return response;
|
|
16
|
+
},
|
|
17
|
+
postJson: async () => {
|
|
18
|
+
throw new Error("Unexpected POST request");
|
|
19
|
+
},
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
const RAW_ORDER = {
|
|
23
|
+
id: ORDER_ID,
|
|
24
|
+
offerAsset: USDC,
|
|
25
|
+
wantAsset: USDG,
|
|
26
|
+
amountDue: "50000000",
|
|
27
|
+
remainingAmountDue: "0",
|
|
28
|
+
offerAmount: "50000000",
|
|
29
|
+
receiver: USER,
|
|
30
|
+
distributorCode: "0x",
|
|
31
|
+
destinationChainId: 42161,
|
|
32
|
+
sourceChainId: 42161,
|
|
33
|
+
receiveTime: 1234567890,
|
|
34
|
+
status: "PROCESSED",
|
|
35
|
+
user: USER,
|
|
36
|
+
createdAt: "2026-01-01T00:00:00Z",
|
|
37
|
+
updatedAt: "2026-01-01T00:05:00Z",
|
|
38
|
+
orderExecuteds: [
|
|
39
|
+
{ id: "0xab", amount: "50000000", remaining: "0", timestamp: 1234567890, txHash: "0xcd", chainId: 42161 },
|
|
40
|
+
],
|
|
41
|
+
};
|
|
42
|
+
(0, vitest_1.describe)("paxos transit api", () => {
|
|
43
|
+
(0, vitest_1.it)("parses routes", async () => {
|
|
44
|
+
const calls = [];
|
|
45
|
+
const api = createApi(calls, {
|
|
46
|
+
routes: [
|
|
47
|
+
{
|
|
48
|
+
sourceChainId: 42161,
|
|
49
|
+
destinationChainId: 42161,
|
|
50
|
+
destinationChainEID: 30110,
|
|
51
|
+
offerAsset: USDC,
|
|
52
|
+
wantAsset: USDG,
|
|
53
|
+
minOrderSize: "10000000",
|
|
54
|
+
},
|
|
55
|
+
],
|
|
56
|
+
});
|
|
57
|
+
const routes = await (0, api_1.fetchApiTransitRoutes)({ api });
|
|
58
|
+
(0, vitest_1.expect)(calls).toEqual([{ path: "/v1/paxos/transit/routes", query: { filter: undefined } }]);
|
|
59
|
+
(0, vitest_1.expect)(routes).toEqual([
|
|
60
|
+
{
|
|
61
|
+
sourceChainId: 42161,
|
|
62
|
+
destinationChainId: 42161,
|
|
63
|
+
destinationChainEID: 30110,
|
|
64
|
+
offerAsset: USDC,
|
|
65
|
+
wantAsset: USDG,
|
|
66
|
+
minOrderSize: 10000000n,
|
|
67
|
+
tokenMetadataMap: {},
|
|
68
|
+
},
|
|
69
|
+
]);
|
|
70
|
+
});
|
|
71
|
+
(0, vitest_1.it)("asks for the routes of a fee tier", async () => {
|
|
72
|
+
const calls = [];
|
|
73
|
+
const api = createApi(calls, { routes: [] });
|
|
74
|
+
await (0, api_1.fetchApiTransitRoutes)({ api }, { feeTier: "zeroFee" });
|
|
75
|
+
(0, vitest_1.expect)(calls).toEqual([{ path: "/v1/paxos/transit/routes", query: { filter: undefined, feeTier: "zeroFee" } }]);
|
|
76
|
+
});
|
|
77
|
+
(0, vitest_1.it)("parses fee tier capacity as bigint", async () => {
|
|
78
|
+
const calls = [];
|
|
79
|
+
const api = createApi(calls, { feeTier: "zeroFee", zeroFeeCapacity: "250000000000" });
|
|
80
|
+
const result = await (0, api_1.fetchApiTransitFeeTier)({ api }, { userAddress: USER });
|
|
81
|
+
(0, vitest_1.expect)(calls).toEqual([{ path: "/v1/paxos/transit/fee-tier", query: { userAddress: USER } }]);
|
|
82
|
+
(0, vitest_1.expect)(result).toEqual({ feeTier: "zeroFee", zeroFeeCapacity: 250000000000n });
|
|
83
|
+
});
|
|
84
|
+
(0, vitest_1.it)("sends quote amounts as strings and parses fees", async () => {
|
|
85
|
+
const calls = [];
|
|
86
|
+
const api = createApi(calls, {
|
|
87
|
+
transaction: { to: "0x22", data: "0x33", value: "1500", functionName: "submitOrder" },
|
|
88
|
+
amountOut: "49990000",
|
|
89
|
+
protocolFee: "10000",
|
|
90
|
+
fillCostFee: "5000",
|
|
91
|
+
conversionRateFee: "5000",
|
|
92
|
+
conversionRateHundredthsBps: 100,
|
|
93
|
+
integratorFee: "0",
|
|
94
|
+
totalFees: "10000",
|
|
95
|
+
});
|
|
96
|
+
const quote = await (0, api_1.fetchApiTransitQuote)({ api }, {
|
|
97
|
+
userAddress: USER,
|
|
98
|
+
offerAsset: USDC,
|
|
99
|
+
wantAsset: USDG,
|
|
100
|
+
offerAmount: 50000000n,
|
|
101
|
+
sourceChainId: 42161,
|
|
102
|
+
destinationChainId: 42161,
|
|
103
|
+
feeTier: "standardFee",
|
|
104
|
+
});
|
|
105
|
+
(0, vitest_1.expect)(calls[0].path).toBe("/v1/paxos/transit/orders/quote");
|
|
106
|
+
(0, vitest_1.expect)(calls[0].query).toMatchObject({ offerAmount: "50000000", feeTier: "standardFee" });
|
|
107
|
+
(0, vitest_1.expect)(quote.transaction.value).toBe(1500n);
|
|
108
|
+
(0, vitest_1.expect)(quote.amountOut).toBe(49990000n);
|
|
109
|
+
(0, vitest_1.expect)(quote.totalFees).toBe(10000n);
|
|
110
|
+
(0, vitest_1.expect)(quote.estimatedLatencyMs).toBeUndefined();
|
|
111
|
+
});
|
|
112
|
+
(0, vitest_1.it)("parses a single order", async () => {
|
|
113
|
+
const calls = [];
|
|
114
|
+
const api = createApi(calls, { order: RAW_ORDER });
|
|
115
|
+
const order = await (0, api_1.fetchApiTransitOrder)({ api }, { orderId: ORDER_ID });
|
|
116
|
+
(0, vitest_1.expect)(calls).toEqual([{ path: `/v1/paxos/transit/orders/${ORDER_ID}`, query: undefined }]);
|
|
117
|
+
(0, vitest_1.expect)(order.status).toBe("PROCESSED");
|
|
118
|
+
(0, vitest_1.expect)(order.offerAmount).toBe(50000000n);
|
|
119
|
+
(0, vitest_1.expect)(order.tokenMetadata).toEqual({});
|
|
120
|
+
(0, vitest_1.expect)(order.orderExecuteds[0].amount).toBe(50000000n);
|
|
121
|
+
});
|
|
122
|
+
(0, vitest_1.it)("drops an empty next page token", async () => {
|
|
123
|
+
const api = createApi([], { orders: [RAW_ORDER], nextPageToken: "" });
|
|
124
|
+
const result = await (0, api_1.fetchApiTransitOrders)({ api }, { userAddress: USER });
|
|
125
|
+
(0, vitest_1.expect)(result.orders).toHaveLength(1);
|
|
126
|
+
(0, vitest_1.expect)(result.nextPageToken).toBeUndefined();
|
|
127
|
+
});
|
|
128
|
+
});
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.fetchApiTransitOrders = exports.fetchApiTransitOrder = exports.fetchApiTransitQuote = exports.fetchApiTransitAuthorization = exports.fetchApiTransitFeeTier = exports.fetchApiTransitRoutes = void 0;
|
|
4
|
+
async function fetchApiTransitRoutes(ctx, params) {
|
|
5
|
+
const raw = await ctx.api.fetchJson("/v1/paxos/transit/routes", {
|
|
6
|
+
query: { filter: params?.filter, feeTier: params?.feeTier },
|
|
7
|
+
});
|
|
8
|
+
return (raw.routes ?? []).map(parseRoute);
|
|
9
|
+
}
|
|
10
|
+
exports.fetchApiTransitRoutes = fetchApiTransitRoutes;
|
|
11
|
+
async function fetchApiTransitFeeTier(ctx, params) {
|
|
12
|
+
const raw = await ctx.api.fetchJson("/v1/paxos/transit/fee-tier", {
|
|
13
|
+
query: { userAddress: params.userAddress },
|
|
14
|
+
});
|
|
15
|
+
return {
|
|
16
|
+
feeTier: raw.feeTier,
|
|
17
|
+
zeroFeeCapacity: BigInt(raw.zeroFeeCapacity),
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
exports.fetchApiTransitFeeTier = fetchApiTransitFeeTier;
|
|
21
|
+
async function fetchApiTransitAuthorization(ctx, params) {
|
|
22
|
+
return ctx.api.fetchJson("/v1/paxos/transit/authorization", {
|
|
23
|
+
query: {
|
|
24
|
+
userAddress: params.userAddress,
|
|
25
|
+
tokenAddress: params.tokenAddress,
|
|
26
|
+
spenderAddress: params.spenderAddress,
|
|
27
|
+
amount: params.amount.toString(),
|
|
28
|
+
chainId: params.chainId,
|
|
29
|
+
},
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
exports.fetchApiTransitAuthorization = fetchApiTransitAuthorization;
|
|
33
|
+
async function fetchApiTransitQuote(ctx, params) {
|
|
34
|
+
const raw = await ctx.api.fetchJson("/v1/paxos/transit/orders/quote", {
|
|
35
|
+
query: {
|
|
36
|
+
userAddress: params.userAddress,
|
|
37
|
+
offerAsset: params.offerAsset,
|
|
38
|
+
wantAsset: params.wantAsset,
|
|
39
|
+
offerAmount: params.offerAmount.toString(),
|
|
40
|
+
sourceChainId: params.sourceChainId,
|
|
41
|
+
destinationChainId: params.destinationChainId,
|
|
42
|
+
permitSignature: params.permitSignature,
|
|
43
|
+
permitDeadline: params.permitDeadline,
|
|
44
|
+
responseFormat: params.responseFormat,
|
|
45
|
+
feeTier: params.feeTier,
|
|
46
|
+
},
|
|
47
|
+
});
|
|
48
|
+
return parseQuote(raw);
|
|
49
|
+
}
|
|
50
|
+
exports.fetchApiTransitQuote = fetchApiTransitQuote;
|
|
51
|
+
async function fetchApiTransitOrder(ctx, params) {
|
|
52
|
+
const raw = await ctx.api.fetchJson(`/v1/paxos/transit/orders/${encodeURIComponent(params.orderId)}`);
|
|
53
|
+
return parseOrder(raw.order);
|
|
54
|
+
}
|
|
55
|
+
exports.fetchApiTransitOrder = fetchApiTransitOrder;
|
|
56
|
+
async function fetchApiTransitOrders(ctx, params) {
|
|
57
|
+
const raw = await ctx.api.fetchJson("/v1/paxos/transit/orders", {
|
|
58
|
+
query: {
|
|
59
|
+
userAddress: params.userAddress,
|
|
60
|
+
pageSize: params.pageSize,
|
|
61
|
+
pageToken: params.pageToken,
|
|
62
|
+
filter: params.filter,
|
|
63
|
+
},
|
|
64
|
+
});
|
|
65
|
+
return {
|
|
66
|
+
orders: (raw.orders ?? []).map(parseOrder),
|
|
67
|
+
nextPageToken: raw.nextPageToken || undefined,
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
exports.fetchApiTransitOrders = fetchApiTransitOrders;
|
|
71
|
+
function parseRoute(raw) {
|
|
72
|
+
return {
|
|
73
|
+
sourceChainId: Number(raw.sourceChainId),
|
|
74
|
+
destinationChainId: Number(raw.destinationChainId),
|
|
75
|
+
destinationChainEID: Number(raw.destinationChainEID),
|
|
76
|
+
offerAsset: raw.offerAsset,
|
|
77
|
+
wantAsset: raw.wantAsset,
|
|
78
|
+
minOrderSize: BigInt(raw.minOrderSize),
|
|
79
|
+
tokenMetadataMap: raw.tokenMetadataMap ?? {},
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
function parseQuote(raw) {
|
|
83
|
+
return {
|
|
84
|
+
transaction: {
|
|
85
|
+
to: raw.transaction.to,
|
|
86
|
+
data: raw.transaction.data,
|
|
87
|
+
value: BigInt(raw.transaction.value ?? 0),
|
|
88
|
+
abi: raw.transaction.abi,
|
|
89
|
+
functionName: raw.transaction.functionName,
|
|
90
|
+
args: raw.transaction.args,
|
|
91
|
+
},
|
|
92
|
+
amountOut: BigInt(raw.amountOut),
|
|
93
|
+
protocolFee: BigInt(raw.protocolFee),
|
|
94
|
+
fillCostFee: BigInt(raw.fillCostFee),
|
|
95
|
+
conversionRateFee: BigInt(raw.conversionRateFee),
|
|
96
|
+
conversionRateHundredthsBps: Number(raw.conversionRateHundredthsBps),
|
|
97
|
+
integratorFee: BigInt(raw.integratorFee ?? 0),
|
|
98
|
+
totalFees: BigInt(raw.totalFees),
|
|
99
|
+
estimatedLatencyMs: raw.estimatedLatencyMs === undefined ? undefined : Number(raw.estimatedLatencyMs),
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
function parseOrder(raw) {
|
|
103
|
+
return {
|
|
104
|
+
id: raw.id,
|
|
105
|
+
offerAsset: raw.offerAsset,
|
|
106
|
+
wantAsset: raw.wantAsset,
|
|
107
|
+
amountDue: BigInt(raw.amountDue),
|
|
108
|
+
remainingAmountDue: BigInt(raw.remainingAmountDue),
|
|
109
|
+
offerAmount: BigInt(raw.offerAmount),
|
|
110
|
+
receiver: raw.receiver,
|
|
111
|
+
distributorCode: raw.distributorCode,
|
|
112
|
+
destinationChainId: Number(raw.destinationChainId),
|
|
113
|
+
sourceChainId: Number(raw.sourceChainId),
|
|
114
|
+
receiveTime: Number(raw.receiveTime),
|
|
115
|
+
status: raw.status,
|
|
116
|
+
user: raw.user,
|
|
117
|
+
createdAt: raw.createdAt,
|
|
118
|
+
updatedAt: raw.updatedAt,
|
|
119
|
+
tokenMetadata: raw.tokenMetadata ?? {},
|
|
120
|
+
orderExecuteds: (raw.orderExecuteds ?? []).map((execution) => ({
|
|
121
|
+
id: execution.id,
|
|
122
|
+
amount: BigInt(execution.amount),
|
|
123
|
+
remaining: BigInt(execution.remaining),
|
|
124
|
+
timestamp: Number(execution.timestamp),
|
|
125
|
+
txHash: execution.txHash,
|
|
126
|
+
chainId: Number(execution.chainId),
|
|
127
|
+
})),
|
|
128
|
+
};
|
|
129
|
+
}
|
|
@@ -8,29 +8,6 @@ const tokens_1 = require("../../tokens");
|
|
|
8
8
|
const increaseMarginCheck_1 = require("../increaseMarginCheck");
|
|
9
9
|
const BTC_PRICE = (0, numbers_1.expandDecimals)(20000, 30);
|
|
10
10
|
const tokensData = (0, mock_1.mockTokensData)();
|
|
11
|
-
/** 1% min collateral factor → 100x, 0.5% for liquidation. Fees and impact off unless a test needs them. */
|
|
12
|
-
function buildMarket(overrides = {}) {
|
|
13
|
-
return (0, mock_1.mockMarketsInfoData)(tokensData, ["BTC-BTC-USDC"], {
|
|
14
|
-
"BTC-BTC-USDC": {
|
|
15
|
-
minCollateralFactor: (0, numbers_1.expandDecimals)(1, 28),
|
|
16
|
-
minCollateralFactorForLiquidation: (0, numbers_1.expandDecimals)(5, 27),
|
|
17
|
-
minCollateralFactorForOpenInterestLong: 0n,
|
|
18
|
-
minCollateralFactorForOpenInterestShort: 0n,
|
|
19
|
-
positionFeeFactorForBalanceWasImproved: 0n,
|
|
20
|
-
positionFeeFactorForBalanceWasNotImproved: 0n,
|
|
21
|
-
positionImpactFactorPositive: 0n,
|
|
22
|
-
positionImpactFactorNegative: 0n,
|
|
23
|
-
maxPositionImpactFactorPositive: 0n,
|
|
24
|
-
maxPositionImpactFactorNegative: 0n,
|
|
25
|
-
maxPositionImpactFactorForLiquidations: 0n,
|
|
26
|
-
longInterestUsd: 0n,
|
|
27
|
-
shortInterestUsd: 0n,
|
|
28
|
-
longInterestInTokens: 0n,
|
|
29
|
-
shortInterestInTokens: 0n,
|
|
30
|
-
...overrides,
|
|
31
|
-
},
|
|
32
|
-
})["BTC-BTC-USDC"];
|
|
33
|
-
}
|
|
34
11
|
const usdc = tokensData.USDC;
|
|
35
12
|
function usdcAmount(usd) {
|
|
36
13
|
return (0, tokens_1.convertToTokenAmount)(usd10(usd), usdc.decimals, usdc.prices.minPrice);
|
|
@@ -44,7 +21,7 @@ function btcAmount(usd) {
|
|
|
44
21
|
}
|
|
45
22
|
function baseParams(overrides = {}) {
|
|
46
23
|
return {
|
|
47
|
-
marketInfo:
|
|
24
|
+
marketInfo: (0, mock_1.mockMarginCheckMarketInfo)(tokensData),
|
|
48
25
|
collateralToken: usdc,
|
|
49
26
|
// 10 000 USD of size bought exactly at the oracle price → zero pnl
|
|
50
27
|
sizeInUsd: (0, numbers_1.expandDecimals)(10000, numbers_1.USD_DECIMALS),
|
|
@@ -112,7 +89,7 @@ function baseParams(overrides = {}) {
|
|
|
112
89
|
(0, vitest_1.it)("deducts the full-close position fee once", () => {
|
|
113
90
|
// 0.5% closing fee on 10 000 = 50
|
|
114
91
|
const state = (0, increaseMarginCheck_1.getResultingPositionMarginState)(baseParams({
|
|
115
|
-
marketInfo:
|
|
92
|
+
marketInfo: (0, mock_1.mockMarginCheckMarketInfo)(tokensData, {
|
|
116
93
|
positionFeeFactorForBalanceWasImproved: (0, numbers_1.expandDecimals)(5, 27),
|
|
117
94
|
positionFeeFactorForBalanceWasNotImproved: (0, numbers_1.expandDecimals)(5, 27),
|
|
118
95
|
}),
|
|
@@ -124,7 +101,7 @@ function baseParams(overrides = {}) {
|
|
|
124
101
|
// 0.5% closing fee on 10 000 = 50; a 50% pro discount returns 25 of it
|
|
125
102
|
const state = (0, increaseMarginCheck_1.getResultingPositionMarginState)({
|
|
126
103
|
...baseParams({
|
|
127
|
-
marketInfo:
|
|
104
|
+
marketInfo: (0, mock_1.mockMarginCheckMarketInfo)(tokensData, {
|
|
128
105
|
positionFeeFactorForBalanceWasImproved: (0, numbers_1.expandDecimals)(5, 27),
|
|
129
106
|
positionFeeFactorForBalanceWasNotImproved: (0, numbers_1.expandDecimals)(5, 27),
|
|
130
107
|
}),
|
|
@@ -135,7 +112,7 @@ function baseParams(overrides = {}) {
|
|
|
135
112
|
});
|
|
136
113
|
(0, vitest_1.it)("takes the larger of the pro and referral discounts, not their sum", () => {
|
|
137
114
|
const params = baseParams({
|
|
138
|
-
marketInfo:
|
|
115
|
+
marketInfo: (0, mock_1.mockMarginCheckMarketInfo)(tokensData, {
|
|
139
116
|
positionFeeFactorForBalanceWasImproved: (0, numbers_1.expandDecimals)(5, 27),
|
|
140
117
|
positionFeeFactorForBalanceWasNotImproved: (0, numbers_1.expandDecimals)(5, 27),
|
|
141
118
|
}),
|
|
@@ -162,9 +139,11 @@ function baseParams(overrides = {}) {
|
|
|
162
139
|
});
|
|
163
140
|
});
|
|
164
141
|
(0, vitest_1.describe)("getIncreaseResultingPositionMarginState", () => {
|
|
165
|
-
const marketInfo =
|
|
142
|
+
const marketInfo = (0, mock_1.mockMarginCheckMarketInfo)(tokensData);
|
|
166
143
|
// the contract clamps negative impact at maxPositionImpactFactorForLiquidations — 1% of size here
|
|
167
|
-
const marketWithImpactCap =
|
|
144
|
+
const marketWithImpactCap = (0, mock_1.mockMarginCheckMarketInfo)(tokensData, {
|
|
145
|
+
maxPositionImpactFactorForLiquidations: (0, numbers_1.expandDecimals)(1, 28),
|
|
146
|
+
});
|
|
168
147
|
(0, vitest_1.it)("returns undefined when there is no size to validate", () => {
|
|
169
148
|
(0, vitest_1.expect)((0, increaseMarginCheck_1.getIncreaseResultingPositionMarginState)({
|
|
170
149
|
marketInfo,
|
|
@@ -237,7 +216,7 @@ function baseParams(overrides = {}) {
|
|
|
237
216
|
// shorts dominate, so increasing a long is a same-side rebalance towards balance for the
|
|
238
217
|
// close leg (positive close impact, clamped away) while the increase leg pays negative
|
|
239
218
|
// impact that is stored as pending impact on the position
|
|
240
|
-
const imbalancedMarket =
|
|
219
|
+
const imbalancedMarket = (0, mock_1.mockMarginCheckMarketInfo)(tokensData, {
|
|
241
220
|
maxPositionImpactFactorForLiquidations: (0, numbers_1.expandDecimals)(1, 28),
|
|
242
221
|
positionImpactFactorNegative: (0, numbers_1.expandDecimals)(1, 22),
|
|
243
222
|
positionImpactExponentFactorPositive: (0, numbers_1.expandDecimals)(2, 30),
|
|
@@ -334,7 +313,7 @@ function baseParams(overrides = {}) {
|
|
|
334
313
|
// linear impact (exponent 1) keeps every leg a round number:
|
|
335
314
|
// a crossover pays factorNegative on the side it lands on and earns factorPositive on the side it leaves
|
|
336
315
|
function impactMarket(useOpenInterestInTokensForBalance) {
|
|
337
|
-
return
|
|
316
|
+
return (0, mock_1.mockMarginCheckMarketInfo)(tokensData, {
|
|
338
317
|
positionImpactFactorPositive: (0, numbers_1.expandDecimals)(1, 27), // 0.1%
|
|
339
318
|
positionImpactFactorNegative: (0, numbers_1.expandDecimals)(3, 27), // 0.3%
|
|
340
319
|
positionImpactExponentFactorPositive: (0, numbers_1.expandDecimals)(1, 30),
|
|
@@ -494,7 +473,7 @@ function baseParams(overrides = {}) {
|
|
|
494
473
|
const OI_MULTIPLIER = (0, numbers_1.expandDecimals)(2, 22);
|
|
495
474
|
function gateArgs(collateralUsd, marketOverrides = {}) {
|
|
496
475
|
return {
|
|
497
|
-
marketInfo:
|
|
476
|
+
marketInfo: (0, mock_1.mockMarginCheckMarketInfo)(tokensData, {
|
|
498
477
|
minCollateralFactorForOpenInterestLong: OI_MULTIPLIER,
|
|
499
478
|
longInterestUsd: (0, numbers_1.expandDecimals)(990000, numbers_1.USD_DECIMALS),
|
|
500
479
|
...marketOverrides,
|
|
@@ -536,7 +515,7 @@ function baseParams(overrides = {}) {
|
|
|
536
515
|
});
|
|
537
516
|
});
|
|
538
517
|
(0, vitest_1.describe)("getIncreaseResultingPositionMarginState — evaluation at the trigger price", () => {
|
|
539
|
-
const marketInfo =
|
|
518
|
+
const marketInfo = (0, mock_1.mockMarginCheckMarketInfo)(tokensData);
|
|
540
519
|
const minCollateralUsd = (0, numbers_1.expandDecimals)(1, numbers_1.USD_DECIMALS);
|
|
541
520
|
// existing long opened at the current price (20 000): flat pnl at current prices
|
|
542
521
|
const existingPosition = {
|
|
@@ -766,7 +745,7 @@ function baseParams(overrides = {}) {
|
|
|
766
745
|
});
|
|
767
746
|
});
|
|
768
747
|
(0, vitest_1.describe)("getIncreaseResultingPositionMarginState — collateral delta and impact clamp", () => {
|
|
769
|
-
const marketInfo =
|
|
748
|
+
const marketInfo = (0, mock_1.mockMarginCheckMarketInfo)(tokensData);
|
|
770
749
|
const minCollateralUsd = (0, numbers_1.expandDecimals)(1, numbers_1.USD_DECIMALS);
|
|
771
750
|
// 10 000 of size opened at the oracle price with 1 000 USDC
|
|
772
751
|
const existingPosition = {
|
|
@@ -805,7 +784,7 @@ function baseParams(overrides = {}) {
|
|
|
805
784
|
(0, vitest_1.it)("the summed negative impact is clamped by maxPositionImpactFactorForLiquidations after the pending impact is added", () => {
|
|
806
785
|
// linear impact, shorts 30 000 vs longs 10 000: closing the 10 000 long widens the gap by 10 000
|
|
807
786
|
// → exit impact −2% × 10 000 = −200
|
|
808
|
-
const impactMarket = (maxPositionImpactFactorForLiquidations) =>
|
|
787
|
+
const impactMarket = (maxPositionImpactFactorForLiquidations) => (0, mock_1.mockMarginCheckMarketInfo)(tokensData, {
|
|
809
788
|
positionImpactFactorNegative: (0, numbers_1.expandDecimals)(2, 28),
|
|
810
789
|
positionImpactExponentFactorPositive: (0, numbers_1.expandDecimals)(1, 30),
|
|
811
790
|
positionImpactExponentFactorNegative: (0, numbers_1.expandDecimals)(1, 30),
|