@gmx-io/sdk 1.5.0-alpha-8 → 1.5.0-alpha-9

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 (67) hide show
  1. package/build/cjs/src/clients/v1/testUtil.js +1 -1
  2. package/build/cjs/src/clients/v2/index.js +6 -2
  3. package/build/cjs/src/codegen/prebuilt/hashedKinkModelMarketRatesKeys.json +34 -0
  4. package/build/cjs/src/codegen/prebuilt/hashedMarketConfigKeys.json +230 -0
  5. package/build/cjs/src/codegen/prebuilt/hashedMarketValuesKeys.json +62 -0
  6. package/build/cjs/src/configs/__tests__/tokens.spec.js +1 -0
  7. package/build/cjs/src/configs/api.js +4 -3
  8. package/build/cjs/src/configs/batch.js +13 -0
  9. package/build/cjs/src/configs/chainIds.js +2 -1
  10. package/build/cjs/src/configs/chains.js +48 -2
  11. package/build/cjs/src/configs/contracts.js +67 -0
  12. package/build/cjs/src/configs/express.js +2 -0
  13. package/build/cjs/src/configs/gasLimits.js +7 -0
  14. package/build/cjs/src/configs/markets.js +30 -0
  15. package/build/cjs/src/configs/oracleKeeper.js +2 -0
  16. package/build/cjs/src/configs/tokens.js +89 -1
  17. package/build/cjs/src/utils/__tests__/orderTransactions/decreaseOrders.spec.js +2 -0
  18. package/build/cjs/src/utils/__tests__/orderTransactions/increaseOrders.spec.js +2 -0
  19. package/build/cjs/src/utils/__tests__/orderTransactions/swapOrders.spec.js +1 -0
  20. package/build/cjs/src/utils/__tests__/uiFeeReceiver.spec.js +24 -0
  21. package/build/cjs/src/utils/errors/parseError.js +3 -0
  22. package/build/cjs/src/utils/staking/api.js +11 -0
  23. package/build/cjs/src/utils/staking/types.js +2 -0
  24. package/build/cjs/src/utils/trade/decrease.js +131 -6
  25. package/build/cjs/src/utils/trade/increase.js +2 -6
  26. package/build/cjs/src/utils/twap/uiFeeReceiver.js +29 -13
  27. package/build/esm/src/clients/v1/testUtil.js +1 -1
  28. package/build/esm/src/clients/v2/index.js +4 -0
  29. package/build/esm/src/codegen/prebuilt/hashedKinkModelMarketRatesKeys.json +34 -0
  30. package/build/esm/src/codegen/prebuilt/hashedMarketConfigKeys.json +230 -0
  31. package/build/esm/src/codegen/prebuilt/hashedMarketValuesKeys.json +62 -0
  32. package/build/esm/src/configs/__tests__/tokens.spec.js +2 -1
  33. package/build/esm/src/configs/api.js +5 -4
  34. package/build/esm/src/configs/batch.js +14 -1
  35. package/build/esm/src/configs/chainIds.js +1 -0
  36. package/build/esm/src/configs/chains.js +48 -3
  37. package/build/esm/src/configs/contracts.js +68 -1
  38. package/build/esm/src/configs/express.js +3 -1
  39. package/build/esm/src/configs/gasLimits.js +8 -1
  40. package/build/esm/src/configs/markets.js +31 -1
  41. package/build/esm/src/configs/oracleKeeper.js +3 -1
  42. package/build/esm/src/configs/tokens.js +90 -2
  43. package/build/esm/src/utils/__tests__/orderTransactions/decreaseOrders.spec.js +2 -0
  44. package/build/esm/src/utils/__tests__/orderTransactions/increaseOrders.spec.js +2 -0
  45. package/build/esm/src/utils/__tests__/orderTransactions/swapOrders.spec.js +1 -0
  46. package/build/esm/src/utils/__tests__/uiFeeReceiver.spec.js +25 -1
  47. package/build/esm/src/utils/errors/parseError.js +3 -0
  48. package/build/esm/src/utils/staking/api.js +7 -0
  49. package/build/esm/src/utils/staking/types.js +1 -0
  50. package/build/esm/src/utils/trade/decrease.js +127 -7
  51. package/build/esm/src/utils/trade/increase.js +3 -7
  52. package/build/esm/src/utils/twap/uiFeeReceiver.js +27 -12
  53. package/build/types/src/clients/v1/index.d.ts +1 -1
  54. package/build/types/src/clients/v1/modules/base.d.ts +1 -1
  55. package/build/types/src/clients/v1/multicall.d.ts +1 -1
  56. package/build/types/src/clients/v2/index.d.ts +5 -0
  57. package/build/types/src/codegen/prebuilt/index.d.ts +326 -0
  58. package/build/types/src/configs/chainIds.d.ts +1 -0
  59. package/build/types/src/configs/chains.d.ts +26 -6
  60. package/build/types/src/configs/contracts.d.ts +64 -0
  61. package/build/types/src/utils/errors/parseError.d.ts +3 -0
  62. package/build/types/src/utils/staking/api.d.ts +7 -0
  63. package/build/types/src/utils/staking/types.d.ts +14 -0
  64. package/build/types/src/utils/trade/decrease.d.ts +41 -2
  65. package/build/types/src/utils/trade/types.d.ts +8 -0
  66. package/build/types/src/utils/twap/uiFeeReceiver.d.ts +9 -6
  67. package/package.json +1 -1
@@ -203,6 +203,73 @@ exports.CONTRACTS = {
203
203
  StBTC: "0xF4586028FFdA7Eca636864F80f8a3f2589E33795",
204
204
  PBTC: "0x0D2437F93Fed6EA64Ef01cCde385FB1263910C56",
205
205
  },
206
+ [chains_1.MEGAETH]: {
207
+ // Synthetics
208
+ DataStore: "0xE43C7B694f6b652a9F4A0f275C008d18758Dce35",
209
+ EventEmitter: "0xAf2E131d483cedE068e21a9228aD91E623a989C2",
210
+ SubaccountRouter: "0x3133aC88af73d3187f1700a2426AD95B5d6E0562",
211
+ ExchangeRouter: "0x73B3593F01CF8e573a412D1d0c972b581794ebE0",
212
+ DepositVault: "0x8231A60862F9b0bA93fFA050c0E94AC902D901d2",
213
+ WithdrawalVault: "0x0Ec53dda9676219dE63eC703212219b07811F33C",
214
+ OrderVault: "0xD5AE04762E2afb1506695b3F36286EBE7B0E6772",
215
+ ShiftVault: "0xC255c70b50623054CADbAD9A02E1CFE73d286666",
216
+ SyntheticsReader: "0x0f038EB4a38B08cd3c937a3256b51aa01904a684",
217
+ SyntheticsRouter: "0x1eAfB14236C489C28845EC04F78DECA5Fb9879Aa",
218
+ GlvReader: "0x424527a588D56513cB2F5161958D83883EE8aB0f",
219
+ GlvRouter: "0x505F0cCADA00F0CcB4EEbf6467531cF4dd907B0E",
220
+ GlvVault: "0x52e4875EB5603d21912d30A1dBA6B0B97192459A",
221
+ GelatoRelayRouter: "0x24eD625B9C47fDEbF088A4d12B7f9B4B2f556297",
222
+ SubaccountGelatoRelayRouter: "0xD515fA0B4d704f3E2C57270F1F53BEeE16348B3b",
223
+ MultichainClaimsRouter: "0xfE9fD31e499bA6d8733Aec49ECe5b41381103433",
224
+ MultichainGlvRouter: "0x7EF7d01316425de5d7C2EFDf8b802A250c222faB",
225
+ MultichainGmRouter: "0x041336A3DaF0a12d004a95f1511393d9A3d7236d",
226
+ MultichainOrderRouter: "0x976363dFbA3AeB8Fb10b733baD74e7099cCB558A",
227
+ MultichainSubaccountRouter: "0xeB8f828A4B89dc3A854f278227A2A5E136E50bF9",
228
+ MultichainTransferRouter: "0xCa62C570D8667a00A56EB989881ECbA4364BFe9e",
229
+ MultichainVault: "0xd6922E889cE4CF14e59427F20e7d857ff81A5A9D",
230
+ LayerZeroProvider: "0x9c41F854f123a7905907FfcF2578dFB7E47D02E0",
231
+ ChainlinkPriceFeedProvider: "0x7452c558d45f8afC8c83dAe62C3f8A5BE19c71f6",
232
+ ClaimHandler: "0x7e42e350FEF7c0A766590A6b2F4eF3F38D8A2988",
233
+ // External
234
+ ExternalHandler: "0xa7EE2737249e0099906cB079BCEe85f0bbd837d4",
235
+ OpenOceanRouter: viem_1.zeroAddress,
236
+ Multicall: "0xF516BC01c50eebdBad4d7E506c8f690ae8EAFc52",
237
+ LayerZeroEndpoint: "0x6F475642a6e85809B1c36Fa62763669b1b48DD5B",
238
+ ArbitrumNodeInterface: viem_1.zeroAddress,
239
+ GelatoRelayAddress: "0xcd565435e0d2109feFde337a66491541Df0D1420",
240
+ Vault: viem_1.zeroAddress,
241
+ Reader: viem_1.zeroAddress,
242
+ PositionRouter: viem_1.zeroAddress,
243
+ ReferralStorage: "0xAd917849372eaEF498E982F90bA6459a43ecbd31",
244
+ VaultReader: viem_1.zeroAddress,
245
+ GlpManager: viem_1.zeroAddress,
246
+ RewardRouter: viem_1.zeroAddress,
247
+ RewardReader: viem_1.zeroAddress,
248
+ GlpRewardRouter: viem_1.zeroAddress,
249
+ StakedGmxTracker: viem_1.zeroAddress,
250
+ FeeGmxTracker: viem_1.zeroAddress,
251
+ GLP: viem_1.zeroAddress,
252
+ GMX: viem_1.zeroAddress,
253
+ ES_GMX: viem_1.zeroAddress,
254
+ BN_GMX: viem_1.zeroAddress,
255
+ USDG: viem_1.zeroAddress,
256
+ BonusGmxTracker: viem_1.zeroAddress,
257
+ StakedGlpTracker: viem_1.zeroAddress,
258
+ FeeGlpTracker: viem_1.zeroAddress,
259
+ ExtendedGmxTracker: viem_1.zeroAddress,
260
+ StakedGmxDistributor: viem_1.zeroAddress,
261
+ StakedGlpDistributor: viem_1.zeroAddress,
262
+ GmxVester: viem_1.zeroAddress,
263
+ GlpVester: viem_1.zeroAddress,
264
+ AffiliateVester: viem_1.zeroAddress,
265
+ Router: viem_1.zeroAddress,
266
+ GovToken: viem_1.zeroAddress,
267
+ ES_GMX_IOU: viem_1.zeroAddress,
268
+ OrderBook: viem_1.zeroAddress,
269
+ UniswapGmxEthPool: viem_1.zeroAddress,
270
+ // megaeth specific
271
+ NATIVE_TOKEN: "0x4200000000000000000000000000000000000006",
272
+ },
206
273
  [chains_1.AVALANCHE_FUJI]: {
207
274
  // V1
208
275
  Vault: viem_1.zeroAddress,
@@ -17,6 +17,7 @@ exports.GELATO_API_KEYS = {
17
17
  [chains_1.AVALANCHE]: "FalsQh9loL6V0rwPy4gWgnQPR6uTHfWjSVT2qlTzUq4_",
18
18
  [chains_1.BOTANIX]: "s5GgkfX7dvd_2uYqsRSCjzMekUrXh0dibUvfLab1Anc_",
19
19
  [chains_1.ARBITRUM_SEPOLIA]: "nx5nyAg4h2kI_64YtOuPt7LSPDEXo4u8eJY_idF9xDw_",
20
+ [chains_1.MEGAETH]: "MDPcJgFTWgc8ifFElmv_vHLaioG_h8GU2ZDju1eyQ4Y_",
20
21
  };
21
22
  exports.EXPRESS_EXTRA_EXECUTION_FEE_BUFFER_BPS = 1000;
22
23
  const GAS_PAYMENT_TOKENS = {
@@ -39,6 +40,7 @@ const GAS_PAYMENT_TOKENS = {
39
40
  (0, tokens_1.getTokenBySymbol)(chains_1.ARBITRUM_SEPOLIA, "WETH").address,
40
41
  ],
41
42
  [chains_1.BOTANIX]: [(0, tokens_1.getTokenBySymbol)(chains_1.BOTANIX, "pBTC").address],
43
+ [chains_1.MEGAETH]: [(0, tokens_1.getTokenBySymbol)(chains_1.MEGAETH, "USDM").address, (0, tokens_1.getTokenBySymbol)(chains_1.MEGAETH, "WETH").address],
42
44
  };
43
45
  function getGasPaymentTokens(chainId) {
44
46
  return GAS_PAYMENT_TOKENS[chainId];
@@ -38,4 +38,11 @@ exports.GAS_LIMITS_STATIC_CONFIG = {
38
38
  tokenPermitGasLimit: 90000n,
39
39
  gmxAccountCollateralGasLimit: 0n,
40
40
  },
41
+ [chainIds_1.MEGAETH]: {
42
+ createOrderGasLimit: 4000000n,
43
+ updateOrderGasLimit: 3200000n,
44
+ cancelOrderGasLimit: 2800000n,
45
+ tokenPermitGasLimit: 360000n,
46
+ gmxAccountCollateralGasLimit: 0n,
47
+ },
41
48
  };
@@ -1249,6 +1249,36 @@ exports.MARKETS = {
1249
1249
  shortTokenAddress: "0x0D2437F93Fed6EA64Ef01cCde385FB1263910C56",
1250
1250
  },
1251
1251
  },
1252
+ [chains_1.MEGAETH]: {
1253
+ // BTC/USD [USDM-USDM]
1254
+ "0x31EdCc52bE2Fa55Ba68f50409F9e6b7d9EbF3D59": {
1255
+ marketTokenAddress: "0x31EdCc52bE2Fa55Ba68f50409F9e6b7d9EbF3D59",
1256
+ indexTokenAddress: "0xc258141Ad80cafF9ab82a04bEb1c10F4B5bCb8F4",
1257
+ longTokenAddress: "0xFAfDdbb3FC7688494971a79cc65DCa3EF82079E7",
1258
+ shortTokenAddress: "0xFAfDdbb3FC7688494971a79cc65DCa3EF82079E7",
1259
+ },
1260
+ // ETH/USD [USDM-USDM]
1261
+ "0x9b1B72720f6D277F3b1e607a0c5fab1B300248b1": {
1262
+ marketTokenAddress: "0x9b1B72720f6D277F3b1e607a0c5fab1B300248b1",
1263
+ indexTokenAddress: "0x4200000000000000000000000000000000000006",
1264
+ longTokenAddress: "0xFAfDdbb3FC7688494971a79cc65DCa3EF82079E7",
1265
+ shortTokenAddress: "0xFAfDdbb3FC7688494971a79cc65DCa3EF82079E7",
1266
+ },
1267
+ // SOL/USD [USDM-USDM]
1268
+ "0xe8E716F1cddfFD0698B86919D41A8228d701fEe9": {
1269
+ marketTokenAddress: "0xe8E716F1cddfFD0698B86919D41A8228d701fEe9",
1270
+ indexTokenAddress: "0x30998706b1e5C95e9d3645D2e9E0EB1466819962",
1271
+ longTokenAddress: "0xFAfDdbb3FC7688494971a79cc65DCa3EF82079E7",
1272
+ shortTokenAddress: "0xFAfDdbb3FC7688494971a79cc65DCa3EF82079E7",
1273
+ },
1274
+ // SWAP-ONLY [ETH-USDM]
1275
+ "0xc5c9B5E23810565763De41144741477eeCB25e2e": {
1276
+ marketTokenAddress: "0xc5c9B5E23810565763De41144741477eeCB25e2e",
1277
+ indexTokenAddress: "0x0000000000000000000000000000000000000000",
1278
+ longTokenAddress: "0x4200000000000000000000000000000000000006",
1279
+ shortTokenAddress: "0xFAfDdbb3FC7688494971a79cc65DCa3EF82079E7",
1280
+ },
1281
+ },
1252
1282
  };
1253
1283
  function getMarketsByChainId(chainId) {
1254
1284
  if (!exports.MARKETS[chainId]) {
@@ -20,6 +20,7 @@ const ORACLE_KEEPER_URLS = {
20
20
  [chains_1.AVALANCHE]: "https://avalanche-api.gmxinfra.io",
21
21
  [chains_1.AVALANCHE_FUJI]: "https://synthetics-api-avax-fuji-upovm.ondigitalocean.app",
22
22
  [chains_1.BOTANIX]: "https://botanix-api.gmxinfra.io",
23
+ [chains_1.MEGAETH]: "https://megaeth-api.gmxinfra.io",
23
24
  [chains_1.ARBITRUM_SEPOLIA]: "https://dolphin-app-a2dup.ondigitalocean.app",
24
25
  };
25
26
  const ORACLE_KEEPER_FALLBACK_URLS = {
@@ -27,6 +28,7 @@ const ORACLE_KEEPER_FALLBACK_URLS = {
27
28
  [chains_1.AVALANCHE]: ["https://avalanche-api-fallback.gmxinfra.io", "https://avalanche-api-fallback.gmxinfra2.io"],
28
29
  [chains_1.AVALANCHE_FUJI]: ["https://synthetics-api-avax-fuji.gmxinfra.io"],
29
30
  [chains_1.BOTANIX]: ["https://botanix-api-fallback.gmxinfra.io", "https://botanix-api-fallback.gmxinfra2.io"],
31
+ [chains_1.MEGAETH]: ["https://megaeth-api-fallback.gmxinfra.io"],
30
32
  [chains_1.ARBITRUM_SEPOLIA]: ["https://dolphin-app-a2dup.ondigitalocean.app"],
31
33
  };
32
34
  function getOracleKeeperUrl(chainId) {
@@ -1964,6 +1964,94 @@ exports.TOKENS = {
1964
1964
  isPlatformToken: true,
1965
1965
  },
1966
1966
  ],
1967
+ [chains_1.MEGAETH]: [
1968
+ {
1969
+ name: "Ethereum",
1970
+ symbol: "ETH",
1971
+ address: exports.NATIVE_TOKEN_ADDRESS,
1972
+ decimals: 18,
1973
+ isNative: true,
1974
+ isShortable: true,
1975
+ categories: ["layer1"],
1976
+ imageUrl: "https://assets.coingecko.com/coins/images/279/small/ethereum.png?1595348880",
1977
+ },
1978
+ {
1979
+ name: "Wrapped Ethereum",
1980
+ symbol: "WETH",
1981
+ address: "0x4200000000000000000000000000000000000006",
1982
+ decimals: 18,
1983
+ isWrapped: true,
1984
+ baseSymbol: "ETH",
1985
+ imageUrl: "https://assets.coingecko.com/coins/images/2518/thumb/weth.png?1628852295",
1986
+ },
1987
+ {
1988
+ name: "Bitcoin",
1989
+ symbol: "BTC",
1990
+ address: "0xc258141Ad80cafF9ab82a04bEb1c10F4B5bCb8F4",
1991
+ isSynthetic: true,
1992
+ decimals: 8,
1993
+ categories: ["layer1"],
1994
+ imageUrl: "https://assets.coingecko.com/coins/images/7598/thumb/wrapped_bitcoin_wbtc.png?1548822744",
1995
+ },
1996
+ {
1997
+ name: "Solana",
1998
+ symbol: "SOL",
1999
+ address: "0x30998706b1e5C95e9d3645D2e9E0EB1466819962",
2000
+ isSynthetic: true,
2001
+ priceDecimals: 3,
2002
+ decimals: 9,
2003
+ categories: ["layer1"],
2004
+ imageUrl: "https://assets.coingecko.com/coins/images/4128/small/solana.png?1640133422",
2005
+ },
2006
+ {
2007
+ name: "USDM",
2008
+ symbol: "USDM",
2009
+ address: "0xFAfDdbb3FC7688494971a79cc65DCa3EF82079E7",
2010
+ decimals: 18,
2011
+ isStable: true,
2012
+ imageUrl: "https://assets.coingecko.com/coins/images/69955/standard/USDm.png?1760170136",
2013
+ },
2014
+ {
2015
+ name: "GMX",
2016
+ symbol: "GMX",
2017
+ address: GMX_STUB_ADDRESS,
2018
+ decimals: 18,
2019
+ imageUrl: "https://assets.coingecko.com/coins/images/18323/small/arbit.png?1631532468",
2020
+ isPlatformToken: true,
2021
+ },
2022
+ {
2023
+ name: "Escrowed GMX",
2024
+ symbol: "ESGMX",
2025
+ address: ESGMX_STUB_ADDRESS,
2026
+ decimals: 18,
2027
+ isPlatformToken: true,
2028
+ },
2029
+ {
2030
+ name: "GMX LP",
2031
+ symbol: "GLP",
2032
+ address: GLP_STUB_ADDRESS,
2033
+ decimals: 18,
2034
+ imageUrl: "https://github.com/gmx-io/gmx-assets/blob/main/GMX-Assets/PNG/GLP_LOGO%20ONLY.png?raw=true",
2035
+ isPlatformToken: true,
2036
+ },
2037
+ /** Placeholder tokens */
2038
+ {
2039
+ name: "GMX Market tokens",
2040
+ symbol: "GM",
2041
+ address: exports.GM_STUB_ADDRESS,
2042
+ decimals: 18,
2043
+ imageUrl: "https://raw.githubusercontent.com/gmx-io/gmx-assets/main/GMX-Assets/PNG/GM_LOGO.png",
2044
+ isPlatformToken: true,
2045
+ },
2046
+ {
2047
+ name: "GLV Market tokens",
2048
+ symbol: "GLV",
2049
+ address: exports.GLV_STUB_ADDRESS,
2050
+ decimals: 18,
2051
+ imageUrl: "https://raw.githubusercontent.com/gmx-io/gmx-assets/main/GMX-Assets/PNG/GLV_LOGO.png",
2052
+ isPlatformToken: true,
2053
+ },
2054
+ ],
1967
2055
  };
1968
2056
  exports.TOKEN_COLOR_MAP = {
1969
2057
  ETH: "#6062a6",
@@ -2007,7 +2095,7 @@ exports.SYNTHETIC_TOKENS = {};
2007
2095
  exports.TOKENS_BY_SYMBOL_MAP = {};
2008
2096
  exports.WRAPPED_TOKENS_MAP = {};
2009
2097
  exports.NATIVE_TOKENS_MAP = {};
2010
- const CHAIN_IDS = [chains_1.ARBITRUM, chains_1.AVALANCHE, chains_1.AVALANCHE_FUJI, chains_1.BOTANIX, chains_1.ARBITRUM_SEPOLIA];
2098
+ const CHAIN_IDS = [chains_1.ARBITRUM, chains_1.AVALANCHE, chains_1.AVALANCHE_FUJI, chains_1.BOTANIX, chains_1.ARBITRUM_SEPOLIA, chains_1.MEGAETH];
2011
2099
  for (let j = 0; j < CHAIN_IDS.length; j++) {
2012
2100
  const chainId = CHAIN_IDS[j];
2013
2101
  exports.TOKENS_MAP[chainId] = {};
@@ -376,6 +376,7 @@ const uiFeeReceiver_1 = require("../../twap/uiFeeReceiver");
376
376
  twapId: "8000",
377
377
  numberOfParts: twapParams.numberOfParts,
378
378
  isExpress: false,
379
+ isJit: false,
379
380
  });
380
381
  return {
381
382
  orderPayload: {
@@ -470,6 +471,7 @@ const uiFeeReceiver_1 = require("../../twap/uiFeeReceiver");
470
471
  const decoded = (0, uiFeeReceiver_1.decodeTwapUiFeeReceiver)(uiFeeReceiver);
471
472
  (0, vitest_1.expect)(decoded).toEqual({
472
473
  isExpress: false,
474
+ isJit: false,
473
475
  twapId: "8000",
474
476
  numberOfParts: twapParams.numberOfParts,
475
477
  });
@@ -544,6 +544,7 @@ const uiFeeReceiver_1 = require("../../twap/uiFeeReceiver");
544
544
  const decoded = (0, uiFeeReceiver_1.decodeTwapUiFeeReceiver)(uiFeeReceiver);
545
545
  (0, vitest_1.expect)(decoded).toEqual({
546
546
  isExpress: false,
547
+ isJit: false,
547
548
  twapId: "8000",
548
549
  numberOfParts: twapParams.numberOfParts,
549
550
  });
@@ -640,6 +641,7 @@ const uiFeeReceiver_1 = require("../../twap/uiFeeReceiver");
640
641
  const decoded = (0, uiFeeReceiver_1.decodeTwapUiFeeReceiver)(uiFeeReceiver);
641
642
  (0, vitest_1.expect)(decoded).toEqual({
642
643
  isExpress: false,
644
+ isJit: false,
643
645
  twapId: "8000",
644
646
  numberOfParts: twapParams.numberOfParts,
645
647
  });
@@ -342,6 +342,7 @@ const uiFeeReceiver_1 = require("../../twap/uiFeeReceiver");
342
342
  twapId: "8000",
343
343
  numberOfParts: twapParams.numberOfParts,
344
344
  isExpress: false,
345
+ isJit: false,
345
346
  });
346
347
  return {
347
348
  orderPayload: {
@@ -16,6 +16,15 @@ const uiFeeReceiver_1 = require("../twap/uiFeeReceiver");
16
16
  (0, vitest_1.it)("should correctly decode isExpress", () => {
17
17
  (0, vitest_1.expect)((0, uiFeeReceiver_1.decodeTwapUiFeeReceiver)("0xff0000000000000000000000000000010a123401")).toEqual({
18
18
  isExpress: true,
19
+ isJit: false,
20
+ twapId: "1234",
21
+ numberOfParts: 10,
22
+ });
23
+ });
24
+ (0, vitest_1.it)("should correctly decode isJit", () => {
25
+ (0, vitest_1.expect)((0, uiFeeReceiver_1.decodeTwapUiFeeReceiver)("0xff0000000000000000000000000001000a123401")).toEqual({
26
+ isExpress: false,
27
+ isJit: true,
19
28
  twapId: "1234",
20
29
  numberOfParts: 10,
21
30
  });
@@ -23,11 +32,13 @@ const uiFeeReceiver_1 = require("../twap/uiFeeReceiver");
23
32
  (0, vitest_1.it)("should return the twapId and numberOfParts if the uiFeeReceiver is valid twap", () => {
24
33
  (0, vitest_1.expect)((0, uiFeeReceiver_1.decodeTwapUiFeeReceiver)("0xff0000000000000000000000000000000a123401")).toEqual({
25
34
  isExpress: false,
35
+ isJit: false,
26
36
  twapId: "1234",
27
37
  numberOfParts: 10,
28
38
  });
29
39
  (0, vitest_1.expect)((0, uiFeeReceiver_1.decodeTwapUiFeeReceiver)("0xff000000000000000000000000000000153a4f01")).toEqual({
30
40
  isExpress: false,
41
+ isJit: false,
31
42
  twapId: "3a4f",
32
43
  numberOfParts: 21,
33
44
  });
@@ -62,3 +73,16 @@ const uiFeeReceiver_1 = require("../twap/uiFeeReceiver");
62
73
  (0, vitest_1.expect)((0, uiFeeReceiver_1.setUiFeeReceiverIsExpress)("0xff0000000000000000000000000000000a123401", false)).toEqual("0xff0000000000000000000000000000000a123401");
63
74
  });
64
75
  });
76
+ (0, vitest_1.describe)("setUiFeeReceiverIsJit", () => {
77
+ (0, vitest_1.it)("should correctly set isJit for simple uiFeeReceiver", () => {
78
+ (0, vitest_1.expect)((0, uiFeeReceiver_1.setUiFeeReceiverIsJit)("0xff00000000000000000000000000000000000001", true)).toEqual("0xff00000000000000000000000000010000000001");
79
+ (0, vitest_1.expect)((0, uiFeeReceiver_1.setUiFeeReceiverIsJit)("0xff00000000000000000000000000010000000001", false)).toEqual("0xff00000000000000000000000000000000000001");
80
+ });
81
+ (0, vitest_1.it)("should preserve isExpress when setting isJit", () => {
82
+ (0, vitest_1.expect)((0, uiFeeReceiver_1.setUiFeeReceiverIsJit)("0xff0000000000000000000000000000010a123401", true)).toEqual("0xff0000000000000000000000000001010a123401");
83
+ });
84
+ (0, vitest_1.it)("should correctly set isJit for twap uiFeeReceiver", () => {
85
+ (0, vitest_1.expect)((0, uiFeeReceiver_1.setUiFeeReceiverIsJit)("0xff0000000000000000000000000000000a123401", true)).toEqual("0xff0000000000000000000000000001000a123401");
86
+ (0, vitest_1.expect)((0, uiFeeReceiver_1.setUiFeeReceiverIsJit)("0xff0000000000000000000000000001000a123401", false)).toEqual("0xff0000000000000000000000000000000a123401");
87
+ });
88
+ });
@@ -38,6 +38,7 @@ function extendError(error, params) {
38
38
  error.errorSource = params.errorSource;
39
39
  error.isAdditionalValidationPassed = params.isAdditionalValidationPassed;
40
40
  error.additionalValidationType = params.additionalValidationType;
41
+ error.simulationMethod = params.simulationMethod;
41
42
  error.data = params.data;
42
43
  return error;
43
44
  }
@@ -52,6 +53,7 @@ function parseError(error, errorDepth = 0) {
52
53
  const errorContext = typeof error === "string" ? undefined : error?.errorContext;
53
54
  const isAdditionalValidationPassed = typeof error === "string" ? undefined : error?.isAdditionalValidationPassed;
54
55
  const additionalValidationType = typeof error === "string" ? undefined : error?.additionalValidationType;
56
+ const simulationMethod = typeof error === "string" ? undefined : error?.simulationMethod;
55
57
  const data = typeof error === "string" ? undefined : error?.data;
56
58
  let errorMessage = "Unknown error";
57
59
  let errorStack = undefined;
@@ -161,6 +163,7 @@ function parseError(error, errorDepth = 0) {
161
163
  parentError,
162
164
  isAdditionalValidationPassed,
163
165
  additionalValidationType,
166
+ simulationMethod,
164
167
  errorDepth,
165
168
  };
166
169
  }
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.fetchApiStakingPower = void 0;
4
+ const numbers_1 = require("../numbers");
5
+ async function fetchApiStakingPower(ctx, params) {
6
+ const data = await ctx.api.fetchJson("/staking/power", {
7
+ query: { address: params.address },
8
+ });
9
+ return (0, numbers_1.deserializeBigIntsInObject)(data, { handleInts: true });
10
+ }
11
+ exports.fetchApiStakingPower = fetchApiStakingPower;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getNextPositionValuesForDecreaseTrade = exports.getTotalFeesUsdForDecrease = exports.estimateCollateralCost = exports.payForCollateralCost = exports.getMinCollateralUsdForLeverage = exports.getIsFullClose = exports.getDecreasePositionAmounts = void 0;
3
+ exports.getOptimalDecreaseAndSwapAmounts = exports.selectBetterDecreaseSwapType = exports.getCollateralToPnlInternalSwapStats = exports.isThirdTokenDecreaseSwap = exports.getDecreaseSwapType = exports.getNextPositionValuesForDecreaseTrade = exports.getTotalFeesUsdForDecrease = exports.estimateCollateralCost = exports.payForCollateralCost = exports.getMinCollateralUsdForLeverage = exports.getIsFullClose = exports.getDecreasePositionAmounts = void 0;
4
4
  const factors_1 = require("../../configs/factors");
5
5
  const bigmath_1 = require("../bigmath");
6
6
  const fees_1 = require("../fees");
@@ -11,7 +11,7 @@ const prices_1 = require("../prices");
11
11
  const swap_1 = require("../swap");
12
12
  const tokens_1 = require("../tokens");
13
13
  function getDecreasePositionAmounts(p) {
14
- const { marketInfo, collateralToken, isLong, position, closeSizeUsd, keepLeverage, triggerPrice, fixedAcceptablePriceImpactBps, acceptablePriceImpactBuffer, userReferralInfo, minCollateralUsd, minPositionSizeUsd, uiFeeFactor, triggerOrderType: orderType, receiveToken: receiveTokenArg, isSetAcceptablePriceImpactEnabled, } = p;
14
+ const { marketInfo, collateralToken, isLong, position, closeSizeUsd, keepLeverage, triggerPrice, fixedAcceptablePriceImpactBps, acceptablePriceImpactBuffer, userReferralInfo, minCollateralUsd, minPositionSizeUsd, uiFeeFactor, triggerOrderType: orderType, receiveToken: receiveTokenArg, isSetAcceptablePriceImpactEnabled, forceDecreaseSwapType, } = p;
15
15
  const { indexToken } = marketInfo;
16
16
  const receiveToken = receiveTokenArg ?? collateralToken;
17
17
  const values = {
@@ -42,17 +42,20 @@ function getDecreasePositionAmounts(p) {
42
42
  fundingFeeUsd: 0n,
43
43
  feeDiscountUsd: 0n,
44
44
  swapProfitFeeUsd: 0n,
45
+ swapProfitUsdIn: 0n,
45
46
  payedOutputUsd: 0n,
46
47
  payedRemainingCollateralUsd: 0n,
47
48
  payedRemainingCollateralAmount: 0n,
48
49
  receiveTokenAmount: 0n,
49
50
  receiveUsd: 0n,
51
+ primaryOutput: { tokenAddress: "", amount: 0n, usd: 0n },
52
+ secondaryOutput: { tokenAddress: "", amount: 0n, usd: 0n },
50
53
  triggerOrderType: orderType,
51
54
  triggerThresholdType: undefined,
52
55
  decreaseSwapType: types_1.DecreasePositionSwapType.NoSwap,
53
56
  };
54
57
  const pnlToken = isLong ? marketInfo.longToken : marketInfo.shortToken;
55
- values.decreaseSwapType = getDecreaseSwapType(pnlToken, collateralToken, receiveToken);
58
+ values.decreaseSwapType = forceDecreaseSwapType ?? getDecreaseSwapType(pnlToken, collateralToken, receiveToken);
56
59
  const markPrice = (0, prices_1.getMarkPrice)({ prices: indexToken.prices, isIncrease: false, isLong });
57
60
  const isTrigger = orderType !== undefined;
58
61
  if (orderType) {
@@ -167,20 +170,25 @@ function getDecreasePositionAmounts(p) {
167
170
  values.borrowingFeeUsd = borrowFeeCost.usd;
168
171
  const fundingFeeCost = estimateCollateralCost(position.pendingFundingFeesUsd, collateralToken, values.collateralPrice);
169
172
  values.fundingFeeUsd = fundingFeeCost.usd;
170
- if (profitUsd > 0 && values.decreaseSwapType === types_1.DecreasePositionSwapType.SwapPnlTokenToCollateralToken) {
173
+ if (profitUsd > 0 &&
174
+ (values.decreaseSwapType === types_1.DecreasePositionSwapType.SwapPnlTokenToCollateralToken ||
175
+ values.decreaseSwapType === types_1.DecreasePositionSwapType.SwapCollateralTokenToPnlToken)) {
176
+ const isSwapToCollateral = values.decreaseSwapType === types_1.DecreasePositionSwapType.SwapPnlTokenToCollateralToken;
171
177
  const swapProfitStats = (0, swap_1.getSwapStats)({
172
178
  marketInfo,
173
- tokenInAddress: pnlToken.address,
174
- tokenOutAddress: collateralToken.address,
179
+ tokenInAddress: isSwapToCollateral ? pnlToken.address : collateralToken.address,
180
+ tokenOutAddress: isSwapToCollateral ? collateralToken.address : pnlToken.address,
175
181
  usdIn: profitUsd,
176
182
  shouldApplyPriceImpact: true,
177
183
  swapPricingType: types_1.SwapPricingType.Swap,
178
184
  });
179
185
  values.swapProfitFeeUsd = swapProfitStats.swapFeeUsd - swapProfitStats.priceImpactDeltaUsd;
186
+ values.swapProfitUsdIn = swapProfitStats.usdIn;
180
187
  values.swapUiFeeUsd = (0, numbers_1.applyFactor)(swapProfitStats.usdIn, uiFeeFactor);
181
188
  }
182
189
  else {
183
190
  values.swapProfitFeeUsd = 0n;
191
+ values.swapProfitUsdIn = 0n;
184
192
  }
185
193
  const totalFeesUsd = getTotalFeesUsdForDecrease({
186
194
  positionFeeUsd: values.positionFeeUsd,
@@ -238,6 +246,24 @@ function getDecreasePositionAmounts(p) {
238
246
  values.receiveTokenAmount = payedInfo.outputAmount;
239
247
  }
240
248
  values.receiveUsd = (0, tokens_1.convertToUsd)(values.receiveTokenAmount, collateralToken.decimals, values.collateralPrice);
249
+ // SwapCollateralTokenToPnlToken → primaryOutput (pnl token), otherwise → secondaryOutput (collateral token)
250
+ const pnlTokenPrice = pnlToken.prices.minPrice;
251
+ if (values.decreaseSwapType === types_1.DecreasePositionSwapType.SwapCollateralTokenToPnlToken) {
252
+ values.primaryOutput = {
253
+ tokenAddress: pnlToken.address,
254
+ amount: (0, tokens_1.convertToTokenAmount)(values.receiveUsd, pnlToken.decimals, pnlTokenPrice) ?? 0n,
255
+ usd: values.receiveUsd,
256
+ };
257
+ values.secondaryOutput = { tokenAddress: collateralToken.address, amount: 0n, usd: 0n };
258
+ }
259
+ else {
260
+ values.primaryOutput = { tokenAddress: pnlToken.address, amount: 0n, usd: 0n };
261
+ values.secondaryOutput = {
262
+ tokenAddress: collateralToken.address,
263
+ amount: values.receiveTokenAmount,
264
+ usd: values.receiveUsd,
265
+ };
266
+ }
241
267
  return values;
242
268
  }
243
269
  exports.getDecreasePositionAmounts = getDecreasePositionAmounts;
@@ -465,3 +491,102 @@ function getDecreaseSwapType(pnlToken, collateralToken, receiveToken) {
465
491
  return types_1.DecreasePositionSwapType.SwapPnlTokenToCollateralToken;
466
492
  }
467
493
  }
494
+ exports.getDecreaseSwapType = getDecreaseSwapType;
495
+ function isThirdTokenDecreaseSwap(pnlToken, collateralToken, receiveToken) {
496
+ return (!(0, tokens_1.getIsEquivalentTokens)(receiveToken, pnlToken) &&
497
+ !(0, tokens_1.getIsEquivalentTokens)(receiveToken, collateralToken) &&
498
+ !(0, tokens_1.getIsEquivalentTokens)(pnlToken, collateralToken));
499
+ }
500
+ exports.isThirdTokenDecreaseSwap = isThirdTokenDecreaseSwap;
501
+ function getCollateralToPnlInternalSwapStats(p) {
502
+ const { pnlToken, receiveUsd } = p;
503
+ // Fees already deducted in getDecreasePositionAmounts — convert at oracle price to avoid double-counting
504
+ const amountOut = (0, tokens_1.convertToTokenAmount)(receiveUsd, pnlToken.decimals, pnlToken.prices.minPrice) ?? 0n;
505
+ return { amountOut };
506
+ }
507
+ exports.getCollateralToPnlInternalSwapStats = getCollateralToPnlInternalSwapStats;
508
+ function selectBetterDecreaseSwapType(p) {
509
+ return p.pathBUsdOut > p.pathAUsdOut ? "pathB" : "pathA";
510
+ }
511
+ exports.selectBetterDecreaseSwapType = selectBetterDecreaseSwapType;
512
+ function getOptimalDecreaseAndSwapAmounts(p) {
513
+ const { marketInfo, collateralToken, isLong, position, closeSizeUsd, keepLeverage, triggerPrice, fixedAcceptablePriceImpactBps, acceptablePriceImpactBuffer, userReferralInfo, minCollateralUsd, minPositionSizeUsd, uiFeeFactor, triggerOrderType, isSetAcceptablePriceImpactEnabled, receiveToken, findSwapPath, findSwapPathFromPnl, marketsInfoData, chainId, } = p;
514
+ const decreaseBaseParams = {
515
+ marketInfo,
516
+ collateralToken,
517
+ isLong,
518
+ position,
519
+ closeSizeUsd,
520
+ keepLeverage,
521
+ triggerPrice,
522
+ fixedAcceptablePriceImpactBps,
523
+ acceptablePriceImpactBuffer,
524
+ userReferralInfo,
525
+ minCollateralUsd,
526
+ minPositionSizeUsd,
527
+ uiFeeFactor,
528
+ triggerOrderType,
529
+ isSetAcceptablePriceImpactEnabled,
530
+ receiveToken,
531
+ };
532
+ const pathADecrease = getDecreasePositionAmounts(decreaseBaseParams);
533
+ const pnlToken = isLong ? marketInfo.longToken : marketInfo.shortToken;
534
+ const shouldSwap = !(0, tokens_1.getIsEquivalentTokens)(collateralToken, receiveToken);
535
+ if (!shouldSwap) {
536
+ return { decreaseAmounts: pathADecrease, swapAmounts: undefined };
537
+ }
538
+ const isThirdToken = isThirdTokenDecreaseSwap(pnlToken, collateralToken, receiveToken);
539
+ const pathASwap = pathADecrease.receiveTokenAmount > 0n
540
+ ? (0, swap_1.getSwapAmountsByFromValue)({
541
+ tokenIn: collateralToken,
542
+ tokenOut: receiveToken,
543
+ amountIn: pathADecrease.receiveTokenAmount,
544
+ isLimit: false,
545
+ findSwapPath,
546
+ uiFeeFactor,
547
+ marketsInfoData,
548
+ chainId,
549
+ externalSwapQuoteParams: undefined,
550
+ allowSameTokenSwap: false,
551
+ })
552
+ : undefined;
553
+ if (!isThirdToken) {
554
+ return { decreaseAmounts: pathADecrease, swapAmounts: pathASwap };
555
+ }
556
+ // Path B: force internal collateral→pnl swap, then external pnl→receive
557
+ const pathBDecrease = getDecreasePositionAmounts({
558
+ ...decreaseBaseParams,
559
+ forceDecreaseSwapType: types_1.DecreasePositionSwapType.SwapCollateralTokenToPnlToken,
560
+ });
561
+ if (pathBDecrease.primaryOutput.usd <= 0n) {
562
+ return { decreaseAmounts: pathADecrease, swapAmounts: pathASwap };
563
+ }
564
+ const internalSwapStats = getCollateralToPnlInternalSwapStats({
565
+ pnlToken,
566
+ receiveUsd: pathBDecrease.primaryOutput.usd,
567
+ });
568
+ if (internalSwapStats.amountOut <= 0n) {
569
+ return { decreaseAmounts: pathADecrease, swapAmounts: pathASwap };
570
+ }
571
+ const pathBSwap = (0, swap_1.getSwapAmountsByFromValue)({
572
+ tokenIn: pnlToken,
573
+ tokenOut: receiveToken,
574
+ amountIn: internalSwapStats.amountOut,
575
+ isLimit: false,
576
+ findSwapPath: findSwapPathFromPnl,
577
+ uiFeeFactor,
578
+ marketsInfoData,
579
+ chainId,
580
+ externalSwapQuoteParams: undefined,
581
+ allowSameTokenSwap: false,
582
+ });
583
+ const winner = selectBetterDecreaseSwapType({
584
+ pathAUsdOut: pathASwap?.usdOut ?? 0n,
585
+ pathBUsdOut: pathBSwap.usdOut,
586
+ });
587
+ if (winner === "pathB") {
588
+ return { decreaseAmounts: pathBDecrease, swapAmounts: pathBSwap };
589
+ }
590
+ return { decreaseAmounts: pathADecrease, swapAmounts: pathASwap };
591
+ }
592
+ exports.getOptimalDecreaseAndSwapAmounts = getOptimalDecreaseAndSwapAmounts;
@@ -334,12 +334,8 @@ function getIncreasePositionPrices({ triggerPrice, indexToken, initialCollateral
334
334
  let collateralPrice;
335
335
  if (triggerPrice !== undefined && triggerPrice > 0 && limitOrderType !== undefined) {
336
336
  indexPrice = triggerPrice;
337
- initialCollateralPrice = (0, tokens_1.getIsEquivalentTokens)(indexToken, initialCollateralToken)
338
- ? triggerPrice
339
- : initialCollateralToken.prices.minPrice;
340
- collateralPrice = (0, tokens_1.getIsEquivalentTokens)(indexToken, collateralToken)
341
- ? triggerPrice
342
- : collateralToken.prices.minPrice;
337
+ initialCollateralPrice = initialCollateralToken.prices.minPrice;
338
+ collateralPrice = collateralToken.prices.minPrice;
343
339
  triggerThresholdType = (0, prices_1.getOrderThresholdType)(limitOrderType, isLong);
344
340
  }
345
341
  else {