@gmx-io/sdk 1.5.0-alpha-12 → 1.5.0-alpha-14

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.
Files changed (44) hide show
  1. package/build/cjs/src/clients/v2/__tests__/decrease.spec.js +12 -12
  2. package/build/cjs/src/codegen/prebuilt/hashedKinkModelMarketRatesKeys.json +40 -0
  3. package/build/cjs/src/codegen/prebuilt/hashedMarketConfigKeys.json +305 -0
  4. package/build/cjs/src/codegen/prebuilt/hashedMarketValuesKeys.json +75 -0
  5. package/build/cjs/src/codegen/subsquid.js +24 -0
  6. package/build/cjs/src/configs/marketHours.js +41 -0
  7. package/build/cjs/src/configs/markets.js +35 -0
  8. package/build/cjs/src/configs/tokens.js +50 -0
  9. package/build/cjs/src/utils/__tests__/markets.spec.js +44 -9
  10. package/build/cjs/src/utils/markets/utils.js +24 -13
  11. package/build/cjs/src/utils/orderTransactions/utils.js +2 -2
  12. package/build/cjs/src/utils/orderTransactions/validateTypedData.js +3 -3
  13. package/build/cjs/src/utils/positions/utils.js +1 -1
  14. package/build/cjs/src/utils/swap/swapRouting.js +1 -1
  15. package/build/cjs/src/utils/trade/trade.js +0 -7
  16. package/build/cjs/src/utils/tradeHistory/utils.js +1 -0
  17. package/build/cjs/src/utils/twap/index.js +4 -10
  18. package/build/esm/src/clients/v2/__tests__/decrease.spec.js +12 -12
  19. package/build/esm/src/codegen/prebuilt/hashedKinkModelMarketRatesKeys.json +40 -0
  20. package/build/esm/src/codegen/prebuilt/hashedMarketConfigKeys.json +305 -0
  21. package/build/esm/src/codegen/prebuilt/hashedMarketValuesKeys.json +75 -0
  22. package/build/esm/src/codegen/subsquid.js +24 -0
  23. package/build/esm/src/configs/marketHours.js +38 -0
  24. package/build/esm/src/configs/markets.js +35 -0
  25. package/build/esm/src/configs/tokens.js +50 -0
  26. package/build/esm/src/utils/__tests__/markets.spec.js +45 -10
  27. package/build/esm/src/utils/markets/utils.js +24 -14
  28. package/build/esm/src/utils/orderTransactions/utils.js +2 -2
  29. package/build/esm/src/utils/orderTransactions/validateTypedData.js +3 -3
  30. package/build/esm/src/utils/positions/utils.js +1 -1
  31. package/build/esm/src/utils/subaccount/index.js +2 -2
  32. package/build/esm/src/utils/swap/swapRouting.js +1 -1
  33. package/build/esm/src/utils/trade/trade.js +0 -7
  34. package/build/esm/src/utils/tradeHistory/utils.js +1 -0
  35. package/build/esm/src/utils/twap/index.js +4 -10
  36. package/build/types/src/codegen/prebuilt/index.d.ts +420 -0
  37. package/build/types/src/codegen/subsquid.d.ts +61 -0
  38. package/build/types/src/configs/marketHours.d.ts +7 -0
  39. package/build/types/src/configs/tokens.d.ts +1 -39
  40. package/build/types/src/utils/markets/utils.d.ts +2 -1
  41. package/build/types/src/utils/subaccount/index.d.ts +2 -2
  42. package/build/types/src/utils/tokens/types.d.ts +2 -0
  43. package/build/types/src/utils/tradeHistory/types.d.ts +1 -0
  44. package/package.json +1 -1
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+ // Per-market UI leverage caps keyed off the contract's `minCollateralFactor`.
3
+ // The contracts team runs a cron (see Linear ENG-54) that toggles each market's
4
+ // MCF between its on-hours and off-hours value. Matching either sentinel here
5
+ // lets the UI apply the correct cap without knowing the session schedule.
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.MARKET_HOURS_MARKETS = void 0;
8
+ const GOLD_SILVER_CFG = {
9
+ onHoursMcf: 9n * 10n ** 27n,
10
+ onHoursMaxLeverage: 100,
11
+ offHoursMcf: 35n * 10n ** 27n,
12
+ offHoursMaxLeverage: 25,
13
+ };
14
+ exports.MARKET_HOURS_MARKETS = {
15
+ // Arbitrum
16
+ "0x0Df2BE76F517BCF0000AbfFcB6344B3b2aC4Cc4f": GOLD_SILVER_CFG, // GOLD/USD
17
+ "0x448Fa722717df299ee197E2F6d8EB7911EFF6cEc": GOLD_SILVER_CFG, // SILVER/USD
18
+ "0xda81cdd397210C08cFc567f93982E148A3aac8a6": GOLD_SILVER_CFG, // WTIOIL/USD
19
+ "0x6F287D071800BfA847B4a7a7104BE33F87Ce9E74": GOLD_SILVER_CFG, // BRENTOIL/USD
20
+ "0x2Ce2bc8B0f9d000f359d756a5816C125474Bb39b": {
21
+ // NATGAS/USD
22
+ onHoursMcf: 22n * 10n ** 27n,
23
+ onHoursMaxLeverage: 40,
24
+ offHoursMcf: 40n * 10n ** 27n,
25
+ offHoursMaxLeverage: 20,
26
+ },
27
+ "0xE26E7b91143f367445f1c0a5dCf4f4aC8EaDcDb1": {
28
+ // MegaETH MEGA/USD
29
+ onHoursMcf: 22n * 10n ** 27n,
30
+ onHoursMaxLeverage: 40,
31
+ offHoursMcf: 22n * 10n ** 27n,
32
+ offHoursMaxLeverage: 40,
33
+ },
34
+ "0x1b997cb4841c4cb360E384192fFd7fb26eb10e5f": {
35
+ // Arbitrum MEGA/USD
36
+ onHoursMcf: 18n * 10n ** 27n,
37
+ onHoursMaxLeverage: 50,
38
+ offHoursMcf: 18n * 10n ** 27n,
39
+ offHoursMaxLeverage: 50,
40
+ },
41
+ };
@@ -918,6 +918,34 @@ exports.MARKETS = {
918
918
  longTokenAddress: "0x82aF49447D8a07e3bd95BD0d56f35241523fBab1",
919
919
  shortTokenAddress: "0xaf88d065e77c8cC2239327C5EDb3A432268e5831",
920
920
  },
921
+ // WTIOIL/USD [WETH-USDC]
922
+ "0xda81cdd397210C08cFc567f93982E148A3aac8a6": {
923
+ marketTokenAddress: "0xda81cdd397210C08cFc567f93982E148A3aac8a6",
924
+ indexTokenAddress: "0xa8Ffb545d5cBF1F44E3eBA123D60372cD267D73c",
925
+ longTokenAddress: "0x82aF49447D8a07e3bd95BD0d56f35241523fBab1",
926
+ shortTokenAddress: "0xaf88d065e77c8cC2239327C5EDb3A432268e5831",
927
+ },
928
+ // BRENTOIL/USD [WETH-USDC]
929
+ "0x6F287D071800BfA847B4a7a7104BE33F87Ce9E74": {
930
+ marketTokenAddress: "0x6F287D071800BfA847B4a7a7104BE33F87Ce9E74",
931
+ indexTokenAddress: "0x9C5C4b9BA1fEBA72186f50d8Ae7C58b1D7f0B12F",
932
+ longTokenAddress: "0x82aF49447D8a07e3bd95BD0d56f35241523fBab1",
933
+ shortTokenAddress: "0xaf88d065e77c8cC2239327C5EDb3A432268e5831",
934
+ },
935
+ // NATGAS/USD [WETH-USDC]
936
+ "0x2Ce2bc8B0f9d000f359d756a5816C125474Bb39b": {
937
+ marketTokenAddress: "0x2Ce2bc8B0f9d000f359d756a5816C125474Bb39b",
938
+ indexTokenAddress: "0x620aC65BE29066Bb9D1E92C65b35B9fD321Fb963",
939
+ longTokenAddress: "0x82aF49447D8a07e3bd95BD0d56f35241523fBab1",
940
+ shortTokenAddress: "0xaf88d065e77c8cC2239327C5EDb3A432268e5831",
941
+ },
942
+ // MEGA/USD [WETH-USDC]
943
+ "0xE26E7b91143f367445f1c0a5dCf4f4aC8EaDcDb1": {
944
+ marketTokenAddress: "0xE26E7b91143f367445f1c0a5dCf4f4aC8EaDcDb1",
945
+ indexTokenAddress: "0x13983f27Ce9365055a6a553233c49fE28e70103e",
946
+ longTokenAddress: "0x82aF49447D8a07e3bd95BD0d56f35241523fBab1",
947
+ shortTokenAddress: "0xaf88d065e77c8cC2239327C5EDb3A432268e5831",
948
+ },
921
949
  },
922
950
  [chains_1.AVALANCHE]: {
923
951
  // BTC/USD [BTC-USDC]
@@ -1313,6 +1341,13 @@ exports.MARKETS = {
1313
1341
  longTokenAddress: "0xFAfDdbb3FC7688494971a79cc65DCa3EF82079E7",
1314
1342
  shortTokenAddress: "0xFAfDdbb3FC7688494971a79cc65DCa3EF82079E7",
1315
1343
  },
1344
+ // MEGA/USD [USDM-USDM]
1345
+ "0x1b997cb4841c4cb360E384192fFd7fb26eb10e5f": {
1346
+ marketTokenAddress: "0x1b997cb4841c4cb360E384192fFd7fb26eb10e5f",
1347
+ indexTokenAddress: "0x28B7E77f82B25B95953825F1E3eA0E36c1c29861",
1348
+ longTokenAddress: "0xFAfDdbb3FC7688494971a79cc65DCa3EF82079E7",
1349
+ shortTokenAddress: "0xFAfDdbb3FC7688494971a79cc65DCa3EF82079E7",
1350
+ },
1316
1351
  // SWAP-ONLY [ETH-USDM]
1317
1352
  "0xc5c9B5E23810565763De41144741477eeCB25e2e": {
1318
1353
  marketTokenAddress: "0xc5c9B5E23810565763De41144741477eeCB25e2e",
@@ -1200,6 +1200,48 @@ exports.TOKENS = {
1200
1200
  imageUrl: "https://s2.coinmarketcap.com/static/img/coins/64x64/39343.png",
1201
1201
  explorerUrl: "https://arbiscan.io/token/0xE41902f9aD379A8CC34A34efa00F5c3EE5112bC8",
1202
1202
  },
1203
+ {
1204
+ name: "WTI Crude Oil",
1205
+ symbol: "WTIOIL",
1206
+ address: "0xa8Ffb545d5cBF1F44E3eBA123D60372cD267D73c",
1207
+ isSynthetic: true,
1208
+ decimals: 18,
1209
+ categories: ["rwa"],
1210
+ searchAliases: ["CL"],
1211
+ imageUrl: "https://s2.coinmarketcap.com/static/img/coins/64x64/39487.png",
1212
+ explorerUrl: "https://arbiscan.io/token/0xa8Ffb545d5cBF1F44E3eBA123D60372cD267D73c",
1213
+ },
1214
+ {
1215
+ name: "Brent Crude Oil",
1216
+ symbol: "BRENTOIL",
1217
+ address: "0x9C5C4b9BA1fEBA72186f50d8Ae7C58b1D7f0B12F",
1218
+ isSynthetic: true,
1219
+ decimals: 18,
1220
+ categories: ["rwa"],
1221
+ searchAliases: ["BZ"],
1222
+ imageUrl: "https://s2.coinmarketcap.com/static/img/coins/64x64/39804.png",
1223
+ explorerUrl: "https://arbiscan.io/token/0x9C5C4b9BA1fEBA72186f50d8Ae7C58b1D7f0B12F",
1224
+ },
1225
+ {
1226
+ name: "Natural Gas",
1227
+ symbol: "NATGAS",
1228
+ address: "0x620aC65BE29066Bb9D1E92C65b35B9fD321Fb963",
1229
+ isSynthetic: true,
1230
+ decimals: 18,
1231
+ categories: ["rwa"],
1232
+ imageUrl: "https://s2.coinmarketcap.com/static/img/coins/64x64/39464.png",
1233
+ explorerUrl: "https://arbiscan.io/token/0x620aC65BE29066Bb9D1E92C65b35B9fD321Fb963",
1234
+ },
1235
+ {
1236
+ name: "MegaETH",
1237
+ symbol: "MEGA",
1238
+ address: "0x13983f27Ce9365055a6a553233c49fE28e70103e",
1239
+ isSynthetic: true,
1240
+ decimals: 18,
1241
+ categories: ["layer2", "defi"],
1242
+ imageUrl: "https://assets.coingecko.com/coins/images/69995/standard/ICON.png?1760337992",
1243
+ explorerUrl: "https://arbiscan.io/token/0x13983f27Ce9365055a6a553233c49fE28e70103e",
1244
+ },
1203
1245
  ],
1204
1246
  [chains_1.AVALANCHE]: [
1205
1247
  {
@@ -1948,6 +1990,14 @@ exports.TOKENS = {
1948
1990
  isStable: true,
1949
1991
  imageUrl: "https://assets.coingecko.com/coins/images/69955/standard/USDm.png?1760170136",
1950
1992
  },
1993
+ {
1994
+ name: "MegaETH",
1995
+ symbol: "MEGA",
1996
+ address: "0x28B7E77f82B25B95953825F1E3eA0E36c1c29861",
1997
+ decimals: 18,
1998
+ categories: ["layer2", "defi"],
1999
+ imageUrl: "https://assets.coingecko.com/coins/images/69995/standard/ICON.png?1760337992",
2000
+ },
1951
2001
  {
1952
2002
  name: "GMX",
1953
2003
  symbol: "GMX",
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const vitest_1 = require("vitest");
4
4
  const chains_1 = require("../../configs/chains");
5
+ const factors_1 = require("../../configs/factors");
5
6
  const tokens_1 = require("../../configs/tokens");
6
7
  const markets_1 = require("../markets");
7
8
  const numbers_1 = require("../numbers");
@@ -142,18 +143,52 @@ function getToken(symbol) {
142
143
  });
143
144
  });
144
145
  (0, vitest_1.describe)("getMaxAllowedLeverageByMinCollateralFactor", () => {
145
- (0, vitest_1.it)("returns half of max leverage", () => {
146
- (0, vitest_1.expect)((0, markets_1.getMaxAllowedLeverageByMinCollateralFactor)(1000000000000000000n)).toBe(5000000000000000);
147
- });
148
- (0, vitest_1.it)("returns 100x for on-hours MCF (0.009 → 110x contract max)", () => {
149
- // MCF 0.009 = 9e27 → maxLeverage 110x → allowed 100x
146
+ const GOLD_MARKET = "0x0Df2BE76F517BCF0000AbfFcB6344B3b2aC4Cc4f";
147
+ const SILVER_MARKET = "0x448Fa722717df299ee197E2F6d8EB7911EFF6cEc";
148
+ const WTIOIL_MARKET = "0xda81cdd397210C08cFc567f93982E148A3aac8a6";
149
+ const BRENTOIL_MARKET = "0x6F287D071800BfA847B4a7a7104BE33F87Ce9E74";
150
+ const NATGAS_MARKET = "0x2Ce2bc8B0f9d000f359d756a5816C125474Bb39b";
151
+ const NON_MARKET_HOURS_MARKET = "0x1234567890abcdef1234567890abcdef12345678";
152
+ (0, vitest_1.it)("returns half of max leverage when no market address is provided", () => {
153
+ (0, vitest_1.expect)((0, markets_1.getMaxAllowedLeverageByMinCollateralFactor)(1000000000000000000n, undefined)).toBe(5000000000000000);
154
+ });
155
+ (0, vitest_1.it)("returns half of max leverage for a regular market", () => {
156
+ (0, vitest_1.expect)((0, markets_1.getMaxAllowedLeverageByMinCollateralFactor)(1000000000000000000n, NON_MARKET_HOURS_MARKET)).toBe(5000000000000000);
157
+ });
158
+ (0, vitest_1.it)("returns 100x for GOLD on on-hours MCF (0.009 → 110x contract max)", () => {
150
159
  const onHoursFactor = 9n * 10n ** 27n;
151
- (0, vitest_1.expect)((0, markets_1.getMaxAllowedLeverageByMinCollateralFactor)(onHoursFactor)).toBe(100 * 10000);
160
+ (0, vitest_1.expect)((0, markets_1.getMaxAllowedLeverageByMinCollateralFactor)(onHoursFactor, GOLD_MARKET)).toBe(100 * factors_1.BASIS_POINTS_DIVISOR);
152
161
  });
153
- (0, vitest_1.it)("returns 25x for off-hours MCF (0.035 → 30x contract max)", () => {
154
- // MCF 0.035 = 35e27 → maxLeverage 30x → allowed 25x
162
+ (0, vitest_1.it)("returns 25x for GOLD on off-hours MCF (0.035 → 30x contract max)", () => {
155
163
  const offHoursFactor = 35n * 10n ** 27n;
156
- (0, vitest_1.expect)((0, markets_1.getMaxAllowedLeverageByMinCollateralFactor)(offHoursFactor)).toBe(25 * 10000);
164
+ (0, vitest_1.expect)((0, markets_1.getMaxAllowedLeverageByMinCollateralFactor)(offHoursFactor, GOLD_MARKET)).toBe(25 * factors_1.BASIS_POINTS_DIVISOR);
165
+ });
166
+ (0, vitest_1.it)("returns 100x / 25x for SILVER with the same GOLD MCFs", () => {
167
+ (0, vitest_1.expect)((0, markets_1.getMaxAllowedLeverageByMinCollateralFactor)(9n * 10n ** 27n, SILVER_MARKET)).toBe(100 * factors_1.BASIS_POINTS_DIVISOR);
168
+ (0, vitest_1.expect)((0, markets_1.getMaxAllowedLeverageByMinCollateralFactor)(35n * 10n ** 27n, SILVER_MARKET)).toBe(25 * factors_1.BASIS_POINTS_DIVISOR);
169
+ });
170
+ (0, vitest_1.it)("returns 100x for WTIOIL on-hours MCF and 25x for off-hours MCF", () => {
171
+ (0, vitest_1.expect)((0, markets_1.getMaxAllowedLeverageByMinCollateralFactor)(9n * 10n ** 27n, WTIOIL_MARKET)).toBe(100 * factors_1.BASIS_POINTS_DIVISOR);
172
+ (0, vitest_1.expect)((0, markets_1.getMaxAllowedLeverageByMinCollateralFactor)(35n * 10n ** 27n, WTIOIL_MARKET)).toBe(25 * factors_1.BASIS_POINTS_DIVISOR);
173
+ });
174
+ (0, vitest_1.it)("returns 100x for BRENTOIL on-hours MCF and 25x for off-hours MCF", () => {
175
+ (0, vitest_1.expect)((0, markets_1.getMaxAllowedLeverageByMinCollateralFactor)(9n * 10n ** 27n, BRENTOIL_MARKET)).toBe(100 * factors_1.BASIS_POINTS_DIVISOR);
176
+ (0, vitest_1.expect)((0, markets_1.getMaxAllowedLeverageByMinCollateralFactor)(35n * 10n ** 27n, BRENTOIL_MARKET)).toBe(25 * factors_1.BASIS_POINTS_DIVISOR);
177
+ });
178
+ (0, vitest_1.it)("returns 40x for NATGAS on-hours MCF and 20x for off-hours MCF", () => {
179
+ (0, vitest_1.expect)((0, markets_1.getMaxAllowedLeverageByMinCollateralFactor)(22n * 10n ** 27n, NATGAS_MARKET)).toBe(40 * factors_1.BASIS_POINTS_DIVISOR);
180
+ (0, vitest_1.expect)((0, markets_1.getMaxAllowedLeverageByMinCollateralFactor)(40n * 10n ** 27n, NATGAS_MARKET)).toBe(20 * factors_1.BASIS_POINTS_DIVISOR);
181
+ });
182
+ (0, vitest_1.it)("does not apply market-hours overrides to other markets with same MCF", () => {
183
+ const onHoursFactor = 9n * 10n ** 27n;
184
+ const offHoursFactor = 35n * 10n ** 27n;
185
+ (0, vitest_1.expect)((0, markets_1.getMaxAllowedLeverageByMinCollateralFactor)(onHoursFactor, NON_MARKET_HOURS_MARKET)).toBe(55.5 * factors_1.BASIS_POINTS_DIVISOR);
186
+ (0, vitest_1.expect)((0, markets_1.getMaxAllowedLeverageByMinCollateralFactor)(offHoursFactor, NON_MARKET_HOURS_MARKET)).toBe(14.5 * factors_1.BASIS_POINTS_DIVISOR);
187
+ });
188
+ (0, vitest_1.it)("rounds allowed leverage to nearest .0 or .5", () => {
189
+ (0, vitest_1.expect)((0, markets_1.getMaxAllowedLeverageByMinCollateralFactor)((0, numbers_1.numberToBigint)(1 / 24, numbers_1.PRECISION_DECIMALS), undefined)).toBe(12 * factors_1.BASIS_POINTS_DIVISOR);
190
+ (0, vitest_1.expect)((0, markets_1.getMaxAllowedLeverageByMinCollateralFactor)((0, numbers_1.numberToBigint)(1 / 25, numbers_1.PRECISION_DECIMALS), undefined)).toBe(12.5 * factors_1.BASIS_POINTS_DIVISOR);
191
+ (0, vitest_1.expect)((0, markets_1.getMaxAllowedLeverageByMinCollateralFactor)((0, numbers_1.numberToBigint)(1 / 50, numbers_1.PRECISION_DECIMALS), undefined)).toBe(25 * factors_1.BASIS_POINTS_DIVISOR);
157
192
  });
158
193
  });
159
194
  (0, vitest_1.describe)("getOppositeCollateral", () => {
@@ -1,9 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getMarketAddressByName = exports.getMarketWithTiers = exports.composeFullMarketsInfoData = exports.hydrateMarketInfo = exports.composeRawMarketsInfoData = exports.composeRawMarketInfo = exports.getMarketTicker = exports.getMarket24Stats = exports.getIsMarketAvailableForExpressSwaps = exports.getPriceForPnl = exports.getOpenInterestForBalance = exports.getOpenInterestInTokens = exports.getOpenInterestUsd = exports.getMarketPnl = exports.getOiInTokensFromRawValues = exports.getOiUsdFromRawValues = exports.getMarketDivisor = exports.getReservedUsd = exports.getAvailableUsdLiquidityForCollateral = exports.getOppositeCollateralFromConfig = exports.getOppositeCollateral = exports.getMaxAllowedLeverageByMinCollateralFactor = 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.composeRawMarketsInfoData = exports.composeRawMarketInfo = exports.getMarketTicker = exports.getMarket24Stats = exports.getIsMarketDeprecated = exports.getIsMarketAvailableForExpressSwaps = exports.getPriceForPnl = exports.getOpenInterestForBalance = exports.getOpenInterestInTokens = exports.getOpenInterestUsd = exports.getMarketPnl = exports.getOiInTokensFromRawValues = exports.getOiUsdFromRawValues = exports.getMarketDivisor = exports.getReservedUsd = exports.getAvailableUsdLiquidityForCollateral = exports.getOppositeCollateralFromConfig = exports.getOppositeCollateral = exports.getMaxAllowedLeverageByMinCollateralFactor = 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
+ const marketHours_1 = require("../../configs/marketHours");
6
7
  const tokens_1 = require("../../configs/tokens");
8
+ const bigmath_1 = require("../bigmath");
7
9
  const fees_1 = require("../fees");
8
10
  const numbers_1 = require("../numbers");
9
11
  const objects_1 = require("../objects");
@@ -93,25 +95,30 @@ function getCappedPoolPnl(p) {
93
95
  return poolPnl > maxPnl ? maxPnl : poolPnl;
94
96
  }
95
97
  exports.getCappedPoolPnl = getCappedPoolPnl;
98
+ /**
99
+ * Rounds to nearest .0 leverage, so that getMaxAllowedLeverageByMinCollateralFactor can get .0 or .5 leverage.
100
+ */
101
+ const ROUNDING_VALUE = 10000;
96
102
  function getMaxLeverageByMinCollateralFactor(minCollateralFactor) {
97
103
  if (minCollateralFactor === undefined)
98
104
  return 100 * factors_1.BASIS_POINTS_DIVISOR;
99
105
  if (minCollateralFactor === 0n)
100
106
  return 100 * factors_1.BASIS_POINTS_DIVISOR;
101
- const x = Number(numbers_1.PRECISION / minCollateralFactor);
102
- const rounded = Math.round(x / 10) * 10;
103
- return rounded * factors_1.BASIS_POINTS_DIVISOR;
107
+ const x = bigmath_1.bigMath.mulDiv(numbers_1.PRECISION, factors_1.BASIS_POINTS_DIVISOR_BIGINT, minCollateralFactor);
108
+ const rounded = Math.round(Number(x) / ROUNDING_VALUE) * ROUNDING_VALUE;
109
+ return rounded;
104
110
  }
105
111
  exports.getMaxLeverageByMinCollateralFactor = getMaxLeverageByMinCollateralFactor;
106
- // GOLD/SILVER on-hours: MCF 0.009 (9e27) → 100x UI
107
- const MCF_ON_HOURS = 9n * 10n ** 27n;
108
- // GOLD/SILVER off-hours: MCF 0.035 (35e27) → 25x UI
109
- const MCF_OFF_HOURS = 35n * 10n ** 27n;
110
- function getMaxAllowedLeverageByMinCollateralFactor(minCollateralFactor) {
111
- if (minCollateralFactor === MCF_ON_HOURS)
112
- return 100 * factors_1.BASIS_POINTS_DIVISOR;
113
- if (minCollateralFactor === MCF_OFF_HOURS)
114
- return 25 * factors_1.BASIS_POINTS_DIVISOR;
112
+ function getMaxAllowedLeverageByMinCollateralFactor(minCollateralFactor, marketAddress) {
113
+ if (marketAddress) {
114
+ const cfg = marketHours_1.MARKET_HOURS_MARKETS[marketAddress];
115
+ if (cfg) {
116
+ if (minCollateralFactor === cfg.onHoursMcf)
117
+ return cfg.onHoursMaxLeverage * factors_1.BASIS_POINTS_DIVISOR;
118
+ if (minCollateralFactor === cfg.offHoursMcf)
119
+ return cfg.offHoursMaxLeverage * factors_1.BASIS_POINTS_DIVISOR;
120
+ }
121
+ }
115
122
  return getMaxLeverageByMinCollateralFactor(minCollateralFactor) / 2;
116
123
  }
117
124
  exports.getMaxAllowedLeverageByMinCollateralFactor = getMaxAllowedLeverageByMinCollateralFactor;
@@ -224,6 +231,10 @@ function getIsMarketAvailableForExpressSwaps(marketInfo) {
224
231
  return [marketInfo.indexToken, marketInfo.longToken, marketInfo.shortToken].every((token) => token.hasPriceFeedProvider);
225
232
  }
226
233
  exports.getIsMarketAvailableForExpressSwaps = getIsMarketAvailableForExpressSwaps;
234
+ function getIsMarketDeprecated(marketInfo) {
235
+ return marketInfo.maxOpenInterestLong <= (0, numbers_1.expandDecimals)(1n, 30) && marketInfo.maxOpenInterestShort <= (0, numbers_1.expandDecimals)(1n, 30);
236
+ }
237
+ exports.getIsMarketDeprecated = getIsMarketDeprecated;
227
238
  function getMarket24Stats(dayPriceCandle) {
228
239
  const open24h = dayPriceCandle.open;
229
240
  const high24h = dayPriceCandle.high;
@@ -167,8 +167,8 @@ function buildDecreaseOrderPayload(p) {
167
167
  }
168
168
  exports.buildDecreaseOrderPayload = buildDecreaseOrderPayload;
169
169
  function buildTwapOrdersPayloads(p, twapParams) {
170
- if (!twapParams.numberOfParts || twapParams.numberOfParts < 2) {
171
- throw new Error(`TWAP numberOfParts must be >= 2, got ${twapParams.numberOfParts}`);
170
+ if (!twapParams.numberOfParts || twapParams.numberOfParts <= 0) {
171
+ throw new Error(`TWAP numberOfParts must be > 0, got ${twapParams.numberOfParts}`);
172
172
  }
173
173
  const uiFeeReceiver = (0, uiFeeReceiver_1.createTwapUiFeeReceiver)({ numberOfParts: twapParams.numberOfParts, source: twapParams.source });
174
174
  if ((0, orders_1.isSwapOrderType)(p.orderType)) {
@@ -73,7 +73,8 @@ accountAddress) {
73
73
  const cancellationReceiver = order?.addresses?.cancellationReceiver;
74
74
  if (cancellationReceiver) {
75
75
  const checksummedCancellationReceiver = (0, viem_1.getAddress)(cancellationReceiver);
76
- if (checksummedCancellationReceiver !== ZERO_ADDRESS && !allowedReceivers.has(checksummedCancellationReceiver)) {
76
+ if (checksummedCancellationReceiver !== ZERO_ADDRESS &&
77
+ !allowedReceivers.has(checksummedCancellationReceiver)) {
77
78
  throw new Error(`Order cancellationReceiver "${cancellationReceiver}" does not match signer "${signerAddress}". Possible malicious typed data.`);
78
79
  }
79
80
  }
@@ -88,8 +89,7 @@ function validateSubaccountApprovalTypedData(domain, message, chainId, signerAdd
88
89
  if (message.account && (0, viem_1.getAddress)(message.account) !== checksummedSigner) {
89
90
  throw new Error(`Subaccount approval account "${message.account}" does not match signer "${signerAddress}"`);
90
91
  }
91
- if (message.subaccount &&
92
- (0, viem_1.getAddress)(message.subaccount) !== (0, viem_1.getAddress)(expectedSubaccountAddress)) {
92
+ if (message.subaccount && (0, viem_1.getAddress)(message.subaccount) !== (0, viem_1.getAddress)(expectedSubaccountAddress)) {
93
93
  throw new Error(`Subaccount address "${message.subaccount}" does not match expected "${expectedSubaccountAddress}"`);
94
94
  }
95
95
  }
@@ -346,7 +346,7 @@ function getPositionInfo(p) {
346
346
  pendingFundingFeesUsd,
347
347
  pnl: undefined,
348
348
  });
349
- const maxAllowedLeverage = (0, markets_1.getMaxAllowedLeverageByMinCollateralFactor)(marketInfo.minCollateralFactor);
349
+ const maxAllowedLeverage = (0, markets_1.getMaxAllowedLeverageByMinCollateralFactor)(marketInfo.minCollateralFactor, marketInfo.marketTokenAddress);
350
350
  const hasLowCollateral = (leverage !== undefined && leverage > maxAllowedLeverage) || false;
351
351
  const liquidationPrice = getLiquidationPrice({
352
352
  marketInfo,
@@ -103,7 +103,7 @@ function getBestSwapPath({ routes, usdIn, estimator, networkEstimator, }) {
103
103
  if (routes.length === 0) {
104
104
  return undefined;
105
105
  }
106
- let bestRoute = undefined;
106
+ let bestRoute = routes[0];
107
107
  let bestUsdOut = 0n;
108
108
  for (const route of routes) {
109
109
  try {
@@ -6,11 +6,7 @@ const bigmath_1 = require("../bigmath");
6
6
  const types_1 = require("../orders/types");
7
7
  const prices_1 = require("../prices");
8
8
  const types_2 = require("./types");
9
- const MAX_ALLOWED_SLIPPAGE = 5000; // 50%
10
9
  function applySlippageToPrice(allowedSlippage, price, isIncrease, isLong) {
11
- if (allowedSlippage < 0 || allowedSlippage > MAX_ALLOWED_SLIPPAGE) {
12
- throw new Error(`allowedSlippage must be between 0 and ${MAX_ALLOWED_SLIPPAGE} bps, got ${allowedSlippage}`);
13
- }
14
10
  const shouldIncreasePrice = (0, prices_1.getShouldUseMaxPrice)(isIncrease, isLong);
15
11
  const slippageBasisPoints = shouldIncreasePrice
16
12
  ? factors_1.BASIS_POINTS_DIVISOR + allowedSlippage
@@ -19,9 +15,6 @@ function applySlippageToPrice(allowedSlippage, price, isIncrease, isLong) {
19
15
  }
20
16
  exports.applySlippageToPrice = applySlippageToPrice;
21
17
  function applySlippageToMinOut(allowedSlippage, minOutputAmount) {
22
- if (allowedSlippage < 0 || allowedSlippage > MAX_ALLOWED_SLIPPAGE) {
23
- throw new Error(`allowedSlippage must be between 0 and ${MAX_ALLOWED_SLIPPAGE} bps, got ${allowedSlippage}`);
24
- }
25
18
  const slippageBasisPoints = factors_1.BASIS_POINTS_DIVISOR - allowedSlippage;
26
19
  return bigmath_1.bigMath.mulDiv(minOutputAmount, BigInt(slippageBasisPoints), factors_1.BASIS_POINTS_DIVISOR_BIGINT);
27
20
  }
@@ -121,6 +121,7 @@ function createRawTradeActionTransformer(marketsInfoData, wrappedToken, tokensDa
121
121
  positionFeeAmount: rawAction.positionFeeAmount ? BigInt(rawAction.positionFeeAmount) : undefined,
122
122
  borrowingFeeAmount: rawAction.borrowingFeeAmount ? BigInt(rawAction.borrowingFeeAmount) : undefined,
123
123
  fundingFeeAmount: rawAction.fundingFeeAmount ? BigInt(rawAction.fundingFeeAmount) : undefined,
124
+ swapFeeUsd: rawAction.swapFeeUsd ? BigInt(rawAction.swapFeeUsd) : undefined,
124
125
  liquidationFeeAmount: rawAction.liquidationFeeAmount ? BigInt(rawAction.liquidationFeeAmount) : undefined,
125
126
  reason: rawAction.reason ?? undefined,
126
127
  reasonBytes: rawAction.reasonBytes ?? undefined,
@@ -33,13 +33,10 @@ function getTwapDurationInSeconds(duration) {
33
33
  }
34
34
  exports.getTwapDurationInSeconds = getTwapDurationInSeconds;
35
35
  function getTwapValidFromTime(duration, numberOfParts, partIndex) {
36
- if (numberOfParts < 2) {
37
- throw new Error(`TWAP numberOfParts must be >= 2, got ${numberOfParts}`);
38
- }
39
36
  const durationMinutes = duration.hours * 60 + duration.minutes;
40
- const durationSeconds = durationMinutes * 60;
37
+ const durationMs = durationMinutes * 60;
41
38
  const startTime = Math.ceil(Date.now() / 1000);
42
- return BigInt(Math.floor(startTime + (durationSeconds / (numberOfParts - 1)) * partIndex));
39
+ return BigInt(Math.floor(startTime + (durationMs / (numberOfParts - 1)) * partIndex));
43
40
  }
44
41
  exports.getTwapValidFromTime = getTwapValidFromTime;
45
42
  function changeTwapNumberOfPartsValue(value) {
@@ -64,14 +61,11 @@ function getTwapOrderKey({ twapId, orderType, pool, isLong, collateralTokenSymbo
64
61
  }
65
62
  exports.getTwapOrderKey = getTwapOrderKey;
66
63
  function makeTwapValidFromTimeGetter(duration, numberOfParts) {
67
- if (numberOfParts < 2) {
68
- throw new Error(`TWAP numberOfParts must be >= 2, got ${numberOfParts}`);
69
- }
70
64
  const durationMinutes = duration.hours * 60 + duration.minutes;
71
- const durationSeconds = durationMinutes * 60;
65
+ const durationMs = durationMinutes * 60;
72
66
  const startTime = Math.ceil(Date.now() / 1000);
73
67
  return (part) => {
74
- return BigInt(Math.floor(startTime + (durationSeconds / (numberOfParts - 1)) * part));
68
+ return BigInt(Math.floor(startTime + (durationMs / (numberOfParts - 1)) * part));
75
69
  };
76
70
  }
77
71
  exports.makeTwapValidFromTimeGetter = makeTwapValidFromTimeGetter;
@@ -93,7 +93,7 @@ describe("decrease orders", () => {
93
93
  const pos = await ensureLongPosition();
94
94
  const sizeBefore = BigInt(pos.sizeInUsd);
95
95
  const collateralBefore = BigInt(pos.collateralUsd);
96
- const leverageBefore = sizeBefore * 10000n / collateralBefore;
96
+ const leverageBefore = (sizeBefore * 10000n) / collateralBefore;
97
97
  const decreaseSize = sizeBefore / 5n;
98
98
  const { submitted } = await expressFlow(sdk, signer, {
99
99
  kind: "decrease",
@@ -117,12 +117,10 @@ describe("decrease orders", () => {
117
117
  expect(posAfter).toBeDefined();
118
118
  const sizeAfter = BigInt(posAfter.sizeInUsd);
119
119
  const collateralAfter = BigInt(posAfter.collateralUsd);
120
- const leverageAfter = sizeAfter * 10000n / collateralAfter;
120
+ const leverageAfter = (sizeAfter * 10000n) / collateralAfter;
121
121
  // Leverage preserved within 5% tolerance
122
- const diff = leverageAfter > leverageBefore
123
- ? leverageAfter - leverageBefore
124
- : leverageBefore - leverageAfter;
125
- expect(diff * 100n / leverageBefore).toBeLessThanOrEqual(5n);
122
+ const diff = leverageAfter > leverageBefore ? leverageAfter - leverageBefore : leverageBefore - leverageAfter;
123
+ expect((diff * 100n) / leverageBefore).toBeLessThanOrEqual(5n);
126
124
  // Collateral also removed proportionally
127
125
  expect(collateralAfter).toBeLessThan(collateralBefore);
128
126
  });
@@ -153,10 +151,8 @@ describe("decrease orders", () => {
153
151
  expect(posAfter).toBeDefined();
154
152
  const collateralAfter = BigInt(posAfter.collateralUsd);
155
153
  // Collateral stays roughly the same (may change slightly due to fees)
156
- const collateralDiff = collateralAfter > collateralBefore
157
- ? collateralAfter - collateralBefore
158
- : collateralBefore - collateralAfter;
159
- expect(collateralDiff * 100n / collateralBefore).toBeLessThanOrEqual(10n);
154
+ const collateralDiff = collateralAfter > collateralBefore ? collateralAfter - collateralBefore : collateralBefore - collateralAfter;
155
+ expect((collateralDiff * 100n) / collateralBefore).toBeLessThanOrEqual(10n);
160
156
  });
161
157
  });
162
158
  describe("receiveToken", () => {
@@ -210,7 +206,9 @@ describe("decrease orders", () => {
210
206
  },
211
207
  });
212
208
  }
213
- catch { /* cleanup */ }
209
+ catch {
210
+ /* cleanup */
211
+ }
214
212
  });
215
213
  it("TWAP market decrease — partial close split into parts", async () => {
216
214
  const pos = await ensureLongPosition();
@@ -300,7 +298,9 @@ describe("decrease orders", () => {
300
298
  },
301
299
  });
302
300
  }
303
- catch { /* cleanup */ }
301
+ catch {
302
+ /* cleanup */
303
+ }
304
304
  });
305
305
  it("stop-loss: estimates present", async () => {
306
306
  await ensureLongPosition();
@@ -50,6 +50,14 @@
50
50
  "aboveOptimalUsageBorrowingFactorLong": "0x7a6c2c5606edbf4ad6d99540c5788fc9bdc10e90e63a84e11c4129a2f04c8cf9",
51
51
  "aboveOptimalUsageBorrowingFactorShort": "0xc6f8f13437ac5a034a7a93a8a4bfa46e8d83b49c61b45c01240003e491de9d72"
52
52
  },
53
+ "0x1b997cb4841c4cb360E384192fFd7fb26eb10e5f": {
54
+ "optimalUsageFactorLong": "0x4d65f516c890bfdc01bfaf9ef1fe05bc6a78d2a4015d14de0837219c887d7098",
55
+ "optimalUsageFactorShort": "0x42250297858b187815cfd16f31654b887069a6ae5a174f5e8764ef61e68a550e",
56
+ "baseBorrowingFactorLong": "0x64e7d45b8a6b5b607d007c813ac753232dda12a3ba6953a7abdc9ec1495663f4",
57
+ "baseBorrowingFactorShort": "0x2a8628640685fef975df5c0e898fc5d0796e46b0c11cc7f1fcb30ccfcf03900a",
58
+ "aboveOptimalUsageBorrowingFactorLong": "0xcd5fef2dcde9921645b4f25cc06658e4a232efdc005a237a71b5db4aa1795cad",
59
+ "aboveOptimalUsageBorrowingFactorShort": "0x02bf1759ac43e1a8ce374831520870154c462dc29fca7a2a19bac560c4a6ae87"
60
+ },
53
61
  "0xc5c9B5E23810565763De41144741477eeCB25e2e": {
54
62
  "optimalUsageFactorLong": "0x009bc89771e196346b3d41cf47c8234c6843f977392651e5a122511445fdc304",
55
63
  "optimalUsageFactorShort": "0x522a6a2270cc21374e753b06f2df5dabe018d2135aa58d75bd64ab43b16dc98f",
@@ -1091,6 +1099,38 @@
1091
1099
  "baseBorrowingFactorShort": "0x05640382d9ac2b646b854e08d798e98f04cd0723d0898053d14d73aa8ad67aa1",
1092
1100
  "aboveOptimalUsageBorrowingFactorLong": "0xe1de87148bd67bda9217fbb7a4605e13a6819cf6b02f99ed4c0df17281800ee6",
1093
1101
  "aboveOptimalUsageBorrowingFactorShort": "0xf4d2054fd2e503b3e5535b68952c816ff1e6c76ed5a6db2b0a7071d88d1834c7"
1102
+ },
1103
+ "0xda81cdd397210C08cFc567f93982E148A3aac8a6": {
1104
+ "optimalUsageFactorLong": "0x986daec772e7197f321cf18ae605db6da936f87bcb03cff3d4303cd21e88fa51",
1105
+ "optimalUsageFactorShort": "0x41638decee9a2957f1416690587fb5f120bcb5a8636713df019f82f98eb9b4d7",
1106
+ "baseBorrowingFactorLong": "0x776135184fcbae1f6122486c6d370bbbc629d19b20298f5556291b8d8218ef42",
1107
+ "baseBorrowingFactorShort": "0x6e65b5a36df631c8e2251438f025977d1acffe836288cf5b3cb7b0b8812a8be0",
1108
+ "aboveOptimalUsageBorrowingFactorLong": "0x98e679255838f4a852cfe1711d2490d29a4f22d0becbe8464894f95c15c21372",
1109
+ "aboveOptimalUsageBorrowingFactorShort": "0x0945c48086a7b3eca228da45ae3e5811634c1d985fba1ef05591c2557769ccd5"
1110
+ },
1111
+ "0x6F287D071800BfA847B4a7a7104BE33F87Ce9E74": {
1112
+ "optimalUsageFactorLong": "0x90bbb46df9295b17c763bce05f9ac5d856a64dfd24e3298c6b242adedfbe7510",
1113
+ "optimalUsageFactorShort": "0x2e1131e3a19d1a6ba5dfe35f8f7b671fb05e46e448d6a4edb08575654e568070",
1114
+ "baseBorrowingFactorLong": "0x7f6e709e6b17fac1405b0a8cea7b76adeefd421663d8d84965caed5647597285",
1115
+ "baseBorrowingFactorShort": "0x6f4862a2271d2e3ebdd3280a1754297681dcb6d8c204fd6b44751178ce50a3d9",
1116
+ "aboveOptimalUsageBorrowingFactorLong": "0x19fd2a53a0b6aaf23cda804c4e832456f338926fa1e5427ac30936a881413d9a",
1117
+ "aboveOptimalUsageBorrowingFactorShort": "0x94b097a23f795ddb86ecc36fc491bf94fc246623aeb400f320091bc3520d37b1"
1118
+ },
1119
+ "0x2Ce2bc8B0f9d000f359d756a5816C125474Bb39b": {
1120
+ "optimalUsageFactorLong": "0xf925bb5c2878443d47d98b6e9f0d39dd21212f50b015a0c01fcd43adbf0c55a0",
1121
+ "optimalUsageFactorShort": "0x02f76508457d07fb8a2d7c475498c1d182054a87eca61b1c947521cfac21bbaa",
1122
+ "baseBorrowingFactorLong": "0x41c82ada55554195955056b52630f479dc5acad3eaa96970fd93735a72f50ca7",
1123
+ "baseBorrowingFactorShort": "0x85a4a75fcc2ced632df6a9dadbc46b5d4171f166572e298ac8e0f5dc0a713818",
1124
+ "aboveOptimalUsageBorrowingFactorLong": "0xfdc145dc5c86925a834513f0c78ef57f8da0f095253e561506b6367106d5caac",
1125
+ "aboveOptimalUsageBorrowingFactorShort": "0xc961a42b4a07b7f63f14fbd2a3c14c41643eb80dd5a8fdcde32ad1365bcb51a7"
1126
+ },
1127
+ "0xE26E7b91143f367445f1c0a5dCf4f4aC8EaDcDb1": {
1128
+ "optimalUsageFactorLong": "0x7dd92ed8d2f972a0bedc134931f1ce1257fdc1ba4ebc043c270e81880efee700",
1129
+ "optimalUsageFactorShort": "0xec58abc0c1c902e8df7ed4f6968dbfd88228e741bfd63bf3efc635e0d7c9bb9d",
1130
+ "baseBorrowingFactorLong": "0x0df8b0065fbf53780b168beeff39d576cca7b15e1f341b4041d7b485a8ef4ec6",
1131
+ "baseBorrowingFactorShort": "0x7d507d9ca2d236ab9c89efaeb23488e5e23a215bd060a72d159eaa1f2fd0b604",
1132
+ "aboveOptimalUsageBorrowingFactorLong": "0x337fd48d8d99cf5949ebc95f32f43bfd55a3bdc9ba8c5ed407f7d6985d3bf30e",
1133
+ "aboveOptimalUsageBorrowingFactorShort": "0x580ab973f5bd7e0ab5f89478b225d431776863715a24acce5ee7fa1bd82ae9b0"
1094
1134
  }
1095
1135
  },
1096
1136
  "43113": {