@gmx-io/sdk 1.4.0 → 1.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (157) hide show
  1. package/build/cjs/src/abis/GlvReader.js +940 -940
  2. package/build/cjs/src/abis/IStargate.js +603 -0
  3. package/build/cjs/src/abis/LayerZeroProvider.js +273 -46
  4. package/build/cjs/src/abis/index.js +2 -2
  5. package/build/cjs/src/configs/batch.js +29 -103
  6. package/build/cjs/src/configs/chainIds.js +2 -2
  7. package/build/cjs/src/configs/chains.js +230 -151
  8. package/build/cjs/src/configs/contracts.js +1 -5
  9. package/build/cjs/src/configs/dataStore.js +5 -4
  10. package/build/cjs/src/configs/express.js +1 -1
  11. package/build/cjs/src/configs/gasLimits.js +41 -0
  12. package/build/cjs/src/configs/markets.js +102 -1
  13. package/build/cjs/src/configs/multichain.js +1 -0
  14. package/build/cjs/src/configs/oracleKeeper.js +18 -20
  15. package/build/cjs/src/configs/tokens.js +98 -180
  16. package/build/cjs/src/modules/markets/index.js +6 -2
  17. package/build/cjs/src/modules/markets/markets.spec.js +5 -5
  18. package/build/cjs/src/modules/markets/query-builders.js +8 -0
  19. package/build/cjs/src/modules/orders/helpers.js +4 -2
  20. package/build/cjs/src/modules/orders/helpers.spec.js +1 -1
  21. package/build/cjs/src/modules/orders/orders.spec.js +1 -1
  22. package/build/cjs/src/modules/positions/positions.spec.js +2 -2
  23. package/build/cjs/src/modules/tokens/tokens.spec.js +2 -2
  24. package/build/cjs/src/modules/trades/trades.spec.js +1 -1
  25. package/build/cjs/src/modules/utils/utils.js +7 -6
  26. package/build/cjs/src/prebuilt/hashedKinkModelMarketRatesKeys.json +64 -0
  27. package/build/cjs/src/prebuilt/hashedMarketConfigKeys.json +780 -0
  28. package/build/cjs/src/prebuilt/hashedMarketValuesKeys.json +120 -0
  29. package/build/cjs/src/test/mock.js +2 -0
  30. package/build/cjs/src/types/chains.js +2 -0
  31. package/build/cjs/src/types/orders.js +8 -2
  32. package/build/cjs/src/types/subsquid.js +241 -1
  33. package/build/cjs/src/types/tokens.js +7 -0
  34. package/build/cjs/src/utils/__tests__/abort.spec.js +122 -0
  35. package/build/cjs/src/utils/__tests__/numbers.spec.js +94 -14
  36. package/build/cjs/src/utils/abort.js +31 -0
  37. package/build/cjs/src/utils/callContract.js +4 -4
  38. package/build/cjs/src/utils/fees/executionFee.js +14 -11
  39. package/build/cjs/src/utils/fees/index.js +8 -2
  40. package/build/cjs/src/utils/marketKeysAndConfigs.js +8 -0
  41. package/build/cjs/src/utils/markets.js +7 -1
  42. package/build/cjs/src/utils/multicall.js +54 -17
  43. package/build/cjs/src/utils/numbers.js +25 -14
  44. package/build/cjs/src/utils/orders.js +2 -2
  45. package/build/cjs/src/utils/swap/__tests__/findSwapPathsBetweenTokens.spec.js +22 -0
  46. package/build/cjs/src/utils/swap/__tests__/mockRouting.spec.js +264 -6
  47. package/build/cjs/src/utils/swap/__tests__/swapRouting.spec.js +5 -4
  48. package/build/cjs/src/utils/swap/__tests__/swapStats.spec.js +10 -9
  49. package/build/cjs/src/utils/swap/buildSwapStrategy.js +20 -11
  50. package/build/cjs/src/utils/swap/findSwapPathsBetweenTokens.js +8 -4
  51. package/build/cjs/src/utils/swap/swapPath.js +6 -6
  52. package/build/cjs/src/utils/swap/swapRouting.js +8 -4
  53. package/build/cjs/src/utils/swap/swapStats.js +6 -6
  54. package/build/cjs/src/utils/swap/swapValues.js +17 -6
  55. package/build/cjs/src/utils/tokens.js +17 -1
  56. package/build/cjs/src/utils/trade/decrease.js +1 -1
  57. package/build/cjs/src/utils/trade/increase.js +3 -0
  58. package/build/esm/src/abis/GlvReader.js +940 -940
  59. package/build/esm/src/abis/IStargate.js +601 -0
  60. package/build/esm/src/abis/LayerZeroProvider.js +273 -46
  61. package/build/esm/src/abis/index.js +2 -2
  62. package/build/esm/src/configs/batch.js +30 -104
  63. package/build/esm/src/configs/chainIds.js +1 -1
  64. package/build/esm/src/configs/chains.js +212 -147
  65. package/build/esm/src/configs/contracts.js +1 -5
  66. package/build/esm/src/configs/dataStore.js +1 -0
  67. package/build/esm/src/configs/express.js +1 -1
  68. package/build/esm/src/configs/gasLimits.js +38 -0
  69. package/build/esm/src/configs/markets.js +95 -1
  70. package/build/esm/src/configs/multichain.js +2 -1
  71. package/build/esm/src/configs/oracleKeeper.js +17 -19
  72. package/build/esm/src/configs/tokens.js +97 -179
  73. package/build/esm/src/modules/markets/index.js +6 -2
  74. package/build/esm/src/modules/markets/markets.spec.js +5 -5
  75. package/build/esm/src/modules/markets/query-builders.js +8 -0
  76. package/build/esm/src/modules/orders/helpers.js +5 -3
  77. package/build/esm/src/modules/orders/helpers.spec.js +1 -1
  78. package/build/esm/src/modules/orders/orders.spec.js +1 -1
  79. package/build/esm/src/modules/positions/positions.spec.js +2 -2
  80. package/build/esm/src/modules/tokens/tokens.spec.js +2 -2
  81. package/build/esm/src/modules/trades/trades.spec.js +1 -1
  82. package/build/esm/src/modules/utils/utils.js +7 -6
  83. package/build/esm/src/prebuilt/hashedKinkModelMarketRatesKeys.json +64 -0
  84. package/build/esm/src/prebuilt/hashedMarketConfigKeys.json +780 -0
  85. package/build/esm/src/prebuilt/hashedMarketValuesKeys.json +120 -0
  86. package/build/esm/src/test/mock.js +2 -0
  87. package/build/esm/src/types/chains.js +1 -0
  88. package/build/esm/src/types/orders.js +8 -2
  89. package/build/esm/src/types/subsquid.js +240 -0
  90. package/build/esm/src/types/tokens.js +6 -1
  91. package/build/esm/src/utils/__tests__/abort.spec.js +120 -0
  92. package/build/esm/src/utils/__tests__/numbers.spec.js +95 -15
  93. package/build/esm/src/utils/abort.js +27 -0
  94. package/build/esm/src/utils/callContract.js +5 -5
  95. package/build/esm/src/utils/fees/executionFee.js +15 -12
  96. package/build/esm/src/utils/fees/index.js +8 -2
  97. package/build/esm/src/utils/marketKeysAndConfigs.js +9 -1
  98. package/build/esm/src/utils/markets.js +5 -0
  99. package/build/esm/src/utils/multicall.js +53 -16
  100. package/build/esm/src/utils/numbers.js +21 -12
  101. package/build/esm/src/utils/orders.js +3 -3
  102. package/build/esm/src/utils/swap/__tests__/findSwapPathsBetweenTokens.spec.js +22 -0
  103. package/build/esm/src/utils/swap/__tests__/mockRouting.spec.js +264 -6
  104. package/build/esm/src/utils/swap/__tests__/swapRouting.spec.js +5 -4
  105. package/build/esm/src/utils/swap/__tests__/swapStats.spec.js +10 -9
  106. package/build/esm/src/utils/swap/buildSwapStrategy.js +21 -12
  107. package/build/esm/src/utils/swap/findSwapPathsBetweenTokens.js +8 -4
  108. package/build/esm/src/utils/swap/swapPath.js +6 -6
  109. package/build/esm/src/utils/swap/swapRouting.js +8 -4
  110. package/build/esm/src/utils/swap/swapStats.js +6 -6
  111. package/build/esm/src/utils/swap/swapValues.js +18 -7
  112. package/build/esm/src/utils/tokens.js +15 -1
  113. package/build/esm/src/utils/trade/decrease.js +2 -2
  114. package/build/esm/src/utils/trade/increase.js +3 -0
  115. package/build/types/src/abis/IStargate.d.ts +464 -0
  116. package/build/types/src/abis/LayerZeroProvider.d.ts +12 -0
  117. package/build/types/src/abis/index.d.ts +475 -99
  118. package/build/types/src/configs/chainIds.d.ts +1 -1
  119. package/build/types/src/configs/chains.d.ts +170 -27
  120. package/build/types/src/configs/contracts.d.ts +0 -4
  121. package/build/types/src/configs/dataStore.d.ts +1 -0
  122. package/build/types/src/configs/express.d.ts +1 -1
  123. package/build/types/src/configs/gasLimits.d.ts +4 -0
  124. package/build/types/src/configs/markets.d.ts +10 -1
  125. package/build/types/src/configs/oracleKeeper.d.ts +13 -0
  126. package/build/types/src/configs/tokens.d.ts +3 -0
  127. package/build/types/src/index.d.ts +1 -1
  128. package/build/types/src/modules/base.d.ts +1 -1
  129. package/build/types/src/modules/markets/index.d.ts +2 -0
  130. package/build/types/src/modules/markets/query-builders.d.ts +1 -1
  131. package/build/types/src/modules/markets/types.d.ts +2 -2
  132. package/build/types/src/modules/utils/utils.d.ts +2 -3
  133. package/build/types/src/types/chains.d.ts +57 -0
  134. package/build/types/src/types/markets.d.ts +2 -0
  135. package/build/types/src/types/orders.d.ts +8 -2
  136. package/build/types/src/types/subsquid.d.ts +5987 -5172
  137. package/build/types/src/types/tokens.d.ts +11 -6
  138. package/build/types/src/types/trade.d.ts +5 -0
  139. package/build/types/src/utils/__tests__/abort.spec.d.ts +1 -0
  140. package/build/types/src/utils/abort.d.ts +1 -0
  141. package/build/types/src/utils/fees/estimateOraclePriceCount.d.ts +2 -2
  142. package/build/types/src/utils/fees/executionFee.d.ts +6 -6
  143. package/build/types/src/utils/fees/index.d.ts +2 -1
  144. package/build/types/src/utils/marketKeysAndConfigs.d.ts +2 -0
  145. package/build/types/src/utils/markets.d.ts +4 -2
  146. package/build/types/src/utils/multicall.d.ts +4 -2
  147. package/build/types/src/utils/numbers.d.ts +8 -1
  148. package/build/types/src/utils/swap/buildSwapStrategy.d.ts +8 -3
  149. package/build/types/src/utils/swap/swapPath.d.ts +2 -1
  150. package/build/types/src/utils/swap/swapRouting.d.ts +7 -2
  151. package/build/types/src/utils/swap/swapStats.d.ts +3 -2
  152. package/build/types/src/utils/swap/swapValues.d.ts +3 -1
  153. package/build/types/src/utils/tokens.d.ts +3 -0
  154. package/package.json +17 -5
  155. package/build/cjs/src/abis/MultichainUtils.js +0 -133
  156. package/build/esm/src/abis/MultichainUtils.js +0 -131
  157. package/build/types/src/abis/MultichainUtils.d.ts +0 -100
@@ -231,38 +231,39 @@ const numbers_1 = require("../numbers");
231
231
  });
232
232
  });
233
233
  (0, vitest_1.describe)("formatBalanceAmount", () => {
234
+ const NBSP = "\u00a0";
234
235
  (0, vitest_1.it)("should display balance amount", () => {
235
- (0, vitest_1.expect)((0, numbers_1.formatBalanceAmount)(ONE_USD * 1000n, factors_1.USD_DECIMALS)).toBe("1,000.0000");
236
+ (0, vitest_1.expect)((0, numbers_1.formatBalanceAmount)(ONE_USD * 1000n, factors_1.USD_DECIMALS)).toBe("1,000.00");
236
237
  (0, vitest_1.expect)((0, numbers_1.formatBalanceAmount)(0n, factors_1.USD_DECIMALS)).toBe("-");
237
238
  (0, vitest_1.expect)((0, numbers_1.formatBalanceAmount)(0n, factors_1.USD_DECIMALS, undefined, { showZero: true })).toBe("0.0000");
238
239
  (0, vitest_1.expect)((0, numbers_1.formatBalanceAmount)(ONE_USD * 1n, factors_1.USD_DECIMALS)).toBe("1.0000");
239
240
  (0, vitest_1.expect)((0, numbers_1.formatBalanceAmount)(ONE_USD / 10n, factors_1.USD_DECIMALS)).toBe("0.10000");
240
241
  (0, vitest_1.expect)((0, numbers_1.formatBalanceAmount)(ONE_USD / 100n, factors_1.USD_DECIMALS)).toBe("0.010000");
241
242
  (0, vitest_1.expect)((0, numbers_1.formatBalanceAmount)(ONE_USD / 1000n, factors_1.USD_DECIMALS)).toBe("0.0010000");
242
- (0, vitest_1.expect)((0, numbers_1.formatBalanceAmount)(ONE_USD / 10000n, factors_1.USD_DECIMALS)).toBe("0.00010000");
243
+ (0, vitest_1.expect)((0, numbers_1.formatBalanceAmount)(ONE_USD / 10000n, factors_1.USD_DECIMALS)).toBe("0.0001000");
243
244
  (0, vitest_1.expect)((0, numbers_1.formatBalanceAmount)(ONE_USD / 100000n, factors_1.USD_DECIMALS)).toBe("0.00001000");
244
- (0, vitest_1.expect)((0, numbers_1.formatBalanceAmount)(ONE_USD / 1000000n, factors_1.USD_DECIMALS)).toBe("0.00000100");
245
- (0, vitest_1.expect)((0, numbers_1.formatBalanceAmount)(ONE_USD / 10000000n, factors_1.USD_DECIMALS)).toBe("0.00000010");
246
- (0, vitest_1.expect)((0, numbers_1.formatBalanceAmount)(ONE_USD / 100000000n, factors_1.USD_DECIMALS)).toBe("0.00000001");
245
+ (0, vitest_1.expect)((0, numbers_1.formatBalanceAmount)(ONE_USD / 1000000n, factors_1.USD_DECIMALS)).toBe("0.000001000");
246
+ (0, vitest_1.expect)((0, numbers_1.formatBalanceAmount)(ONE_USD / 10000000n, factors_1.USD_DECIMALS)).toBe("0.000000100");
247
+ (0, vitest_1.expect)((0, numbers_1.formatBalanceAmount)(ONE_USD / 100000000n, factors_1.USD_DECIMALS)).toBe("0.000000010");
247
248
  (0, vitest_1.expect)((0, numbers_1.formatBalanceAmount)(ONE_USD / 1000000000n, factors_1.USD_DECIMALS)).toBe("1.00e-9");
248
249
  (0, vitest_1.expect)((0, numbers_1.formatBalanceAmount)(ONE_USD / 1000000000000n, factors_1.USD_DECIMALS)).toBe("1.00e-12");
249
250
  (0, vitest_1.expect)((0, numbers_1.formatBalanceAmount)(ONE_USD * -1n, factors_1.USD_DECIMALS)).toBe("-1.0000");
250
251
  });
251
252
  (0, vitest_1.it)("should display balance amount with symbol", () => {
252
- (0, vitest_1.expect)((0, numbers_1.formatBalanceAmount)(ONE_USD, factors_1.USD_DECIMALS, "USDC")).toBe("1.0000 USDC");
253
- (0, vitest_1.expect)((0, numbers_1.formatBalanceAmount)(0n, factors_1.USD_DECIMALS, "USDC", { showZero: true })).toBe("0.0000 USDC");
253
+ (0, vitest_1.expect)((0, numbers_1.formatBalanceAmount)(ONE_USD, factors_1.USD_DECIMALS, "USDC")).toBe(`1.0000${NBSP}USDC`);
254
+ (0, vitest_1.expect)((0, numbers_1.formatBalanceAmount)(0n, factors_1.USD_DECIMALS, "USDC", { showZero: true })).toBe(`0.0000${NBSP}USDC`);
254
255
  (0, vitest_1.expect)((0, numbers_1.formatBalanceAmount)(0n, factors_1.USD_DECIMALS, "USDC", { showZero: false })).toBe("-");
255
256
  });
256
257
  (0, vitest_1.it)("should display balance of stable token correctly", () => {
257
258
  (0, vitest_1.expect)((0, numbers_1.formatBalanceAmount)(ONE_USD, factors_1.USD_DECIMALS, undefined, { isStable: true })).toBe("1.00");
258
- (0, vitest_1.expect)((0, numbers_1.formatBalanceAmount)(ONE_USD / 10n, factors_1.USD_DECIMALS, undefined, { isStable: true })).toBe("0.100");
259
- (0, vitest_1.expect)((0, numbers_1.formatBalanceAmount)(ONE_USD / 100n, factors_1.USD_DECIMALS, undefined, { isStable: true })).toBe("0.0100");
260
- (0, vitest_1.expect)((0, numbers_1.formatBalanceAmount)(ONE_USD / 1000n, factors_1.USD_DECIMALS, undefined, { isStable: true })).toBe("0.00100");
261
- (0, vitest_1.expect)((0, numbers_1.formatBalanceAmount)(ONE_USD / 10000n, factors_1.USD_DECIMALS, undefined, { isStable: true })).toBe("0.00010000");
262
- (0, vitest_1.expect)((0, numbers_1.formatBalanceAmount)(ONE_USD / 100000n, factors_1.USD_DECIMALS, undefined, { isStable: true })).toBe("0.00001000");
263
- (0, vitest_1.expect)((0, numbers_1.formatBalanceAmount)(ONE_USD / 1000000n, factors_1.USD_DECIMALS, undefined, { isStable: true })).toBe("0.00000100");
259
+ (0, vitest_1.expect)((0, numbers_1.formatBalanceAmount)(ONE_USD / 10n, factors_1.USD_DECIMALS, undefined, { isStable: true })).toBe("0.10");
260
+ (0, vitest_1.expect)((0, numbers_1.formatBalanceAmount)(ONE_USD / 100n, factors_1.USD_DECIMALS, undefined, { isStable: true })).toBe("0.010");
261
+ (0, vitest_1.expect)((0, numbers_1.formatBalanceAmount)(ONE_USD / 1000n, factors_1.USD_DECIMALS, undefined, { isStable: true })).toBe("0.0010");
262
+ (0, vitest_1.expect)((0, numbers_1.formatBalanceAmount)(ONE_USD / 10000n, factors_1.USD_DECIMALS, undefined, { isStable: true })).toBe("0.00010");
263
+ (0, vitest_1.expect)((0, numbers_1.formatBalanceAmount)(ONE_USD / 100000n, factors_1.USD_DECIMALS, undefined, { isStable: true })).toBe("0.000010");
264
+ (0, vitest_1.expect)((0, numbers_1.formatBalanceAmount)(ONE_USD / 1000000n, factors_1.USD_DECIMALS, undefined, { isStable: true })).toBe("0.0000010");
264
265
  (0, vitest_1.expect)((0, numbers_1.formatBalanceAmount)(ONE_USD / 10000000n, factors_1.USD_DECIMALS, undefined, { isStable: true })).toBe("0.00000010");
265
- (0, vitest_1.expect)((0, numbers_1.formatBalanceAmount)(ONE_USD / 100000000n, factors_1.USD_DECIMALS, undefined, { isStable: true })).toBe("0.00000001");
266
+ (0, vitest_1.expect)((0, numbers_1.formatBalanceAmount)(ONE_USD / 100000000n, factors_1.USD_DECIMALS, undefined, { isStable: true })).toBe("0.000000010");
266
267
  (0, vitest_1.expect)((0, numbers_1.formatBalanceAmount)(ONE_USD / 1000000000n, factors_1.USD_DECIMALS, undefined, { isStable: true })).toBe("1.00e-9");
267
268
  (0, vitest_1.expect)((0, numbers_1.formatBalanceAmount)(0n, factors_1.USD_DECIMALS, undefined, { isStable: true, showZero: true })).toBe("0.00");
268
269
  (0, vitest_1.expect)((0, numbers_1.formatBalanceAmount)(ONE_USD, factors_1.USD_DECIMALS, undefined, { isStable: true, signed: true })).toBe("+1.00");
@@ -308,3 +309,82 @@ const numbers_1 = require("../numbers");
308
309
  (0, vitest_1.expect)((0, numbers_1.formatPercentage)((0, numbers_1.toBigNumberWithDecimals)("0.999", numbers_1.PERCENT_PRECISION_DECIMALS), { bps: false, displayDecimals: 5 })).toBe("0.99900%");
309
310
  });
310
311
  });
312
+ (0, vitest_1.describe)("formatTokenAmount", () => {
313
+ const TOKEN_DECIMALS = 18;
314
+ const ONE_TOKEN = (0, numbers_1.expandDecimals)(1, TOKEN_DECIMALS);
315
+ const NBSP = "\u00a0";
316
+ (0, vitest_1.it)("should return undefined for undefined amount without fallbackToZero", () => {
317
+ (0, vitest_1.expect)((0, numbers_1.formatTokenAmount)(undefined, TOKEN_DECIMALS)).toBeUndefined();
318
+ (0, vitest_1.expect)((0, numbers_1.formatTokenAmount)(undefined, TOKEN_DECIMALS, "ETH")).toBeUndefined();
319
+ });
320
+ (0, vitest_1.it)("should return zero with fallbackToZero", () => {
321
+ (0, vitest_1.expect)((0, numbers_1.formatTokenAmount)(undefined, TOKEN_DECIMALS, undefined, { fallbackToZero: true })).toBe("0.0000");
322
+ (0, vitest_1.expect)((0, numbers_1.formatTokenAmount)(undefined, TOKEN_DECIMALS, "ETH", { fallbackToZero: true })).toBe(`0.0000${NBSP}ETH`);
323
+ });
324
+ (0, vitest_1.it)("should format token amount with symbol", () => {
325
+ (0, vitest_1.expect)((0, numbers_1.formatTokenAmount)(ONE_TOKEN, TOKEN_DECIMALS, "ETH")).toBe(`1.0000${NBSP}ETH`);
326
+ (0, vitest_1.expect)((0, numbers_1.formatTokenAmount)(ONE_TOKEN * 100n, TOKEN_DECIMALS, "ETH")).toBe(`100.000${NBSP}ETH`);
327
+ });
328
+ (0, vitest_1.it)("should format token amount without symbol", () => {
329
+ (0, vitest_1.expect)((0, numbers_1.formatTokenAmount)(ONE_TOKEN, TOKEN_DECIMALS)).toBe("1.0000");
330
+ (0, vitest_1.expect)((0, numbers_1.formatTokenAmount)(ONE_TOKEN * 1000n, TOKEN_DECIMALS)).toBe("1000.00");
331
+ });
332
+ (0, vitest_1.it)("should adjust decimals based on amount magnitude (non-stable)", () => {
333
+ (0, vitest_1.expect)((0, numbers_1.formatTokenAmount)(ONE_TOKEN * 1000n, TOKEN_DECIMALS)).toBe("1000.00");
334
+ (0, vitest_1.expect)((0, numbers_1.formatTokenAmount)(ONE_TOKEN * 100n, TOKEN_DECIMALS)).toBe("100.000");
335
+ (0, vitest_1.expect)((0, numbers_1.formatTokenAmount)(ONE_TOKEN * 10n, TOKEN_DECIMALS)).toBe("10.0000");
336
+ (0, vitest_1.expect)((0, numbers_1.formatTokenAmount)(ONE_TOKEN, TOKEN_DECIMALS)).toBe("1.0000");
337
+ (0, vitest_1.expect)((0, numbers_1.formatTokenAmount)(ONE_TOKEN / 10n, TOKEN_DECIMALS)).toBe("0.10000");
338
+ (0, vitest_1.expect)((0, numbers_1.formatTokenAmount)(ONE_TOKEN / 100n, TOKEN_DECIMALS)).toBe("0.010000");
339
+ (0, vitest_1.expect)((0, numbers_1.formatTokenAmount)(ONE_TOKEN / 1000n, TOKEN_DECIMALS)).toBe("0.0010000");
340
+ (0, vitest_1.expect)((0, numbers_1.formatTokenAmount)(ONE_TOKEN / 10000n, TOKEN_DECIMALS)).toBe("0.0001000");
341
+ });
342
+ (0, vitest_1.it)("should adjust decimals based on amount magnitude (stable)", () => {
343
+ (0, vitest_1.expect)((0, numbers_1.formatTokenAmount)(ONE_TOKEN * 1000n, TOKEN_DECIMALS, undefined, { isStable: true })).toBe("1000.00");
344
+ (0, vitest_1.expect)((0, numbers_1.formatTokenAmount)(ONE_TOKEN * 100n, TOKEN_DECIMALS, undefined, { isStable: true })).toBe("100.00");
345
+ (0, vitest_1.expect)((0, numbers_1.formatTokenAmount)(ONE_TOKEN * 10n, TOKEN_DECIMALS, undefined, { isStable: true })).toBe("10.00");
346
+ (0, vitest_1.expect)((0, numbers_1.formatTokenAmount)(ONE_TOKEN, TOKEN_DECIMALS, undefined, { isStable: true })).toBe("1.00");
347
+ (0, vitest_1.expect)((0, numbers_1.formatTokenAmount)(ONE_TOKEN / 10n, TOKEN_DECIMALS, undefined, { isStable: true })).toBe("0.10");
348
+ (0, vitest_1.expect)((0, numbers_1.formatTokenAmount)(ONE_TOKEN / 100n, TOKEN_DECIMALS, undefined, { isStable: true })).toBe("0.010");
349
+ (0, vitest_1.expect)((0, numbers_1.formatTokenAmount)(ONE_TOKEN / 1000n, TOKEN_DECIMALS, undefined, { isStable: true })).toBe("0.0010");
350
+ (0, vitest_1.expect)((0, numbers_1.formatTokenAmount)(ONE_TOKEN / 10000n, TOKEN_DECIMALS, undefined, { isStable: true })).toBe("0.00010");
351
+ });
352
+ (0, vitest_1.it)("should respect explicit displayDecimals override", () => {
353
+ (0, vitest_1.expect)((0, numbers_1.formatTokenAmount)(ONE_TOKEN, TOKEN_DECIMALS, undefined, { displayDecimals: 2 })).toBe("1.00");
354
+ (0, vitest_1.expect)((0, numbers_1.formatTokenAmount)(ONE_TOKEN, TOKEN_DECIMALS, undefined, { displayDecimals: 6 })).toBe("1.000000");
355
+ (0, vitest_1.expect)((0, numbers_1.formatTokenAmount)(ONE_TOKEN / 10n, TOKEN_DECIMALS, undefined, { displayDecimals: 2 })).toBe("0.10");
356
+ });
357
+ (0, vitest_1.it)("should format with commas when useCommas is true", () => {
358
+ (0, vitest_1.expect)((0, numbers_1.formatTokenAmount)(ONE_TOKEN * 1000000n, TOKEN_DECIMALS, undefined, { useCommas: true })).toBe("1,000,000.00");
359
+ (0, vitest_1.expect)((0, numbers_1.formatTokenAmount)(ONE_TOKEN * 123456n, TOKEN_DECIMALS, "ETH", { useCommas: true })).toBe(`123,456.00${NBSP}ETH`);
360
+ });
361
+ (0, vitest_1.it)("should show all significant digits with showAllSignificant", () => {
362
+ const preciseAmount = ONE_TOKEN + ONE_TOKEN / 123456789n;
363
+ (0, vitest_1.expect)((0, numbers_1.formatTokenAmount)(preciseAmount, TOKEN_DECIMALS, undefined, { showAllSignificant: true })).not.toContain("...");
364
+ });
365
+ (0, vitest_1.it)("should display plus sign for positive values with displayPlus", () => {
366
+ (0, vitest_1.expect)((0, numbers_1.formatTokenAmount)(ONE_TOKEN, TOKEN_DECIMALS, undefined, { displayPlus: true })).toBe("+1.0000");
367
+ (0, vitest_1.expect)((0, numbers_1.formatTokenAmount)(-ONE_TOKEN, TOKEN_DECIMALS, undefined, { displayPlus: true })).toBe("-1.0000");
368
+ });
369
+ (0, vitest_1.it)("should handle negative amounts", () => {
370
+ (0, vitest_1.expect)((0, numbers_1.formatTokenAmount)(-ONE_TOKEN, TOKEN_DECIMALS)).toBe("-1.0000");
371
+ (0, vitest_1.expect)((0, numbers_1.formatTokenAmount)(-ONE_TOKEN * 100n, TOKEN_DECIMALS, "ETH")).toBe(`-100.000${NBSP}ETH`);
372
+ });
373
+ (0, vitest_1.it)("should handle zero amount", () => {
374
+ (0, vitest_1.expect)((0, numbers_1.formatTokenAmount)(0n, TOKEN_DECIMALS)).toBe("0.00");
375
+ (0, vitest_1.expect)((0, numbers_1.formatTokenAmount)(0n, TOKEN_DECIMALS, "ETH")).toBe(`0.00${NBSP}ETH`);
376
+ (0, vitest_1.expect)((0, numbers_1.formatTokenAmount)(0n, TOKEN_DECIMALS, undefined, { isStable: true })).toBe("0.00");
377
+ });
378
+ (0, vitest_1.it)("should handle maxThreshold", () => {
379
+ const result = (0, numbers_1.formatTokenAmount)(ONE_TOKEN * 10000000000n, TOKEN_DECIMALS, undefined, {
380
+ maxThreshold: "1000000000",
381
+ });
382
+ (0, vitest_1.expect)(result).toContain(">");
383
+ });
384
+ (0, vitest_1.it)("should handle minThreshold", () => {
385
+ const result = (0, numbers_1.formatTokenAmount)(ONE_TOKEN / 1000000000n, TOKEN_DECIMALS, undefined, {
386
+ minThreshold: "0.00000001",
387
+ });
388
+ (0, vitest_1.expect)(result).toContain("<");
389
+ });
390
+ });
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.combineAbortSignals = void 0;
4
+ function combineAbortSignals(...signals) {
5
+ const controller = new AbortController();
6
+ const abortHandler = () => {
7
+ for (const signal of signals) {
8
+ signal.removeEventListener("abort", abortHandler);
9
+ }
10
+ controller.abort();
11
+ };
12
+ const signalsWithListeners = [];
13
+ for (const signal of signals) {
14
+ if (signal.aborted) {
15
+ for (const signalWithListener of signalsWithListeners) {
16
+ signalWithListener.removeEventListener("abort", abortHandler);
17
+ }
18
+ controller.abort();
19
+ return controller;
20
+ }
21
+ signal.addEventListener("abort", abortHandler);
22
+ signalsWithListeners.push(signal);
23
+ }
24
+ controller.signal.addEventListener("abort", () => {
25
+ for (const signal of signals) {
26
+ signal.removeEventListener("abort", abortHandler);
27
+ }
28
+ });
29
+ return controller;
30
+ }
31
+ exports.combineAbortSignals = combineAbortSignals;
@@ -6,8 +6,8 @@ const chains_1 = require("../configs/chains");
6
6
  const factors_1 = require("../configs/factors");
7
7
  const bigmath_1 = require("./bigmath");
8
8
  async function getGasPrice(client, chainId) {
9
- let maxFeePerGas = chains_1.MAX_FEE_PER_GAS_MAP[chainId];
10
- const premium = chains_1.GAS_PRICE_PREMIUM_MAP[chainId] || 0n;
9
+ let maxFeePerGas = (0, chains_1.getMaxFeePerGas)(chainId);
10
+ const premium = (0, chains_1.getGasPricePremium)(chainId) || 0n;
11
11
  const feeData = await (0, viem_1.withRetry)(() => client.estimateFeesPerGas({
12
12
  type: "legacy",
13
13
  chain: (0, chains_1.getViemChain)(chainId),
@@ -28,7 +28,7 @@ async function getGasPrice(client, chainId) {
28
28
  const block = await client.getBlock({ blockTag: "pending" });
29
29
  if (block.baseFeePerGas !== undefined && block.baseFeePerGas !== null) {
30
30
  const baseFeePerGas = block.baseFeePerGas;
31
- const maxPriorityFeePerGas = bigmath_1.bigMath.max(chains_1.MAX_PRIORITY_FEE_PER_GAS_MAP[chainId] ?? 0n, premium);
31
+ const maxPriorityFeePerGas = bigmath_1.bigMath.max((0, chains_1.getMaxPriorityFeePerGas)(chainId) || 0n, premium);
32
32
  // Calculate maxFeePerGas
33
33
  const calculatedMaxFeePerGas = baseFeePerGas + maxPriorityFeePerGas + premium;
34
34
  return {
@@ -40,7 +40,7 @@ async function getGasPrice(client, chainId) {
40
40
  if (gasPrice === null || gasPrice === undefined) {
41
41
  throw new Error("Can't fetch gas price");
42
42
  }
43
- const bufferBps = chains_1.GAS_PRICE_BUFFER_MAP[chainId] || 0n;
43
+ const bufferBps = (0, chains_1.getGasPriceBuffer)(chainId) || 0n;
44
44
  const buffer = bigmath_1.bigMath.mulDiv(gasPrice, bufferBps, factors_1.BASIS_POINTS_DIVISOR_BIGINT);
45
45
  return {
46
46
  gasPrice: gasPrice + buffer + premium,
@@ -21,7 +21,7 @@ tokensData, estimatedGasLimit, gasPrice, oraclePriceCount, numberOfParts) {
21
21
  const gasLimit = baseGasLimit + (0, numbers_1.applyFactor)(estimatedGasLimit, multiplierFactor);
22
22
  // #endregion
23
23
  // avoid botanix gas spikes when chain is not actively used
24
- const minGasCostUsd = chains_1.MIN_EXECUTION_FEE_USD[chainId];
24
+ const minGasCostUsd = (0, chains_1.getMinExecutionFeeUsd)(chainId);
25
25
  const minGasCost = (0, tokens_2.convertToTokenAmount)(minGasCostUsd, nativeToken.decimals, nativeToken.prices.minPrice);
26
26
  let feeTokenAmountPerExecution = gasLimit * gasPrice;
27
27
  if (minGasCost) {
@@ -128,12 +128,12 @@ exports.estimateExecuteSwapOrderGasLimit = estimateExecuteSwapOrderGasLimit;
128
128
  */
129
129
  function estimateExecuteDepositGasLimit(gasLimits, deposit) {
130
130
  const gasPerSwap = gasLimits.singleSwap;
131
- const swapsCount = BigInt((deposit.longTokenSwapsCount ?? 0) + (deposit.shortTokenSwapsCount ?? 0));
131
+ const swapsCount = BigInt(deposit.swapsCount ?? 0);
132
132
  const gasForSwaps = swapsCount * gasPerSwap;
133
133
  return gasLimits.depositToken + (deposit.callbackGasLimit ?? 0n) + gasForSwaps;
134
134
  }
135
135
  exports.estimateExecuteDepositGasLimit = estimateExecuteDepositGasLimit;
136
- function estimateExecuteGlvDepositGasLimit(gasLimits, { marketsCount, isMarketTokenDeposit, }) {
136
+ function estimateExecuteGlvDepositGasLimit(gasLimits, { marketsCount, isMarketTokenDeposit, swapsCount, }) {
137
137
  const gasPerGlvPerMarket = gasLimits.glvPerMarketGasLimit;
138
138
  const gasForGlvMarkets = gasPerGlvPerMarket * marketsCount;
139
139
  const glvDepositGasLimit = gasLimits.glvDepositGasLimit;
@@ -141,15 +141,19 @@ function estimateExecuteGlvDepositGasLimit(gasLimits, { marketsCount, isMarketTo
141
141
  if (isMarketTokenDeposit) {
142
142
  return gasLimit;
143
143
  }
144
- return gasLimit + gasLimits.depositToken;
144
+ const gasPerSwap = gasLimits.singleSwap;
145
+ const gasForSwaps = swapsCount * gasPerSwap;
146
+ return gasLimit + gasLimits.depositToken + gasForSwaps;
145
147
  }
146
148
  exports.estimateExecuteGlvDepositGasLimit = estimateExecuteGlvDepositGasLimit;
147
- function estimateExecuteGlvWithdrawalGasLimit(gasLimits, { marketsCount, }) {
149
+ function estimateExecuteGlvWithdrawalGasLimit(gasLimits, { marketsCount, swapsCount, }) {
148
150
  const gasPerGlvPerMarket = gasLimits.glvPerMarketGasLimit;
149
151
  const gasForGlvMarkets = gasPerGlvPerMarket * marketsCount;
150
152
  const glvWithdrawalGasLimit = gasLimits.glvWithdrawalGasLimit;
151
153
  const gasLimit = glvWithdrawalGasLimit + gasForGlvMarkets;
152
- return gasLimit + gasLimits.withdrawalMultiToken;
154
+ const gasPerSwap = gasLimits.singleSwap;
155
+ const gasForSwaps = swapsCount * gasPerSwap;
156
+ return gasLimit + gasLimits.withdrawalMultiToken + gasForSwaps;
153
157
  }
154
158
  exports.estimateExecuteGlvWithdrawalGasLimit = estimateExecuteGlvWithdrawalGasLimit;
155
159
  /**
@@ -158,11 +162,10 @@ exports.estimateExecuteGlvWithdrawalGasLimit = estimateExecuteGlvWithdrawalGasLi
158
162
  * Copy from contract: `estimateExecuteWithdrawalGasLimit`
159
163
  */
160
164
  function estimateExecuteWithdrawalGasLimit(gasLimits, withdrawal) {
161
- // Swap is not used but supported in the contract.
162
- // const gasPerSwap = gasLimits.singleSwap;
163
- // const swapsCount = 0n;
164
- // const gasForSwaps = swapsCount * gasPerSwap;
165
- return gasLimits.withdrawalMultiToken + (withdrawal.callbackGasLimit ?? 0n);
165
+ const gasPerSwap = gasLimits.singleSwap;
166
+ const swapsCount = withdrawal.swapsCount ?? 0n;
167
+ const gasForSwaps = swapsCount * gasPerSwap;
168
+ return gasLimits.withdrawalMultiToken + (withdrawal.callbackGasLimit ?? 0n) + gasForSwaps;
166
169
  }
167
170
  exports.estimateExecuteWithdrawalGasLimit = estimateExecuteWithdrawalGasLimit;
168
171
  /**
@@ -16,17 +16,23 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  exports.getTotalSwapVolumeFromSwapStats = exports.getTotalFeeItem = exports.getFeeItem = exports.getIsHighPriceImpact = exports.getBorrowingFeeRateUsd = exports.getBorrowingFactorPerPeriod = exports.getFundingFeeRateUsd = exports.getFundingFactorPerPeriod = exports.getPositionFee = exports.getSwapFee = void 0;
18
18
  const factors_1 = require("../../configs/factors");
19
+ const orders_1 = require("../../types/orders");
19
20
  const bigmath_1 = require("../bigmath");
20
21
  const markets_1 = require("../markets");
21
22
  const numbers_1 = require("../numbers");
22
23
  __exportStar(require("./estimateOraclePriceCount"), exports);
23
24
  __exportStar(require("./executionFee"), exports);
24
25
  __exportStar(require("./priceImpact"), exports);
25
- function getSwapFee(marketInfo, swapAmount, balanceWasImproved, isAtomicSwap) {
26
+ function getSwapFee(marketInfo, swapAmount, balanceWasImproved, swapPricingType) {
26
27
  let factor;
27
- if (isAtomicSwap) {
28
+ if (swapPricingType === orders_1.SwapPricingType.AtomicSwap) {
28
29
  factor = marketInfo.atomicSwapFeeFactor;
29
30
  }
31
+ else if (swapPricingType === orders_1.SwapPricingType.Withdrawal) {
32
+ factor = balanceWasImproved
33
+ ? marketInfo.withdrawalFeeFactorBalanceWasImproved ?? marketInfo.swapFeeFactorForBalanceWasImproved
34
+ : marketInfo.withdrawalFeeFactorBalanceWasNotImproved ?? marketInfo.swapFeeFactorForBalanceWasNotImproved;
35
+ }
30
36
  else {
31
37
  factor = balanceWasImproved
32
38
  ? marketInfo.swapFeeFactorForBalanceWasImproved
@@ -206,6 +206,14 @@ function hashMarketConfigKeys(market) {
206
206
  ["bytes32", "address"],
207
207
  [dataStore_1.SWAP_IMPACT_EXPONENT_FACTOR_KEY, marketAddress],
208
208
  ],
209
+ withdrawalFeeFactorBalanceWasImproved: [
210
+ ["bytes32", "address", "bool"],
211
+ [dataStore_1.WITHDRAWAL_FEE_FACTOR_KEY, marketAddress, true],
212
+ ],
213
+ withdrawalFeeFactorBalanceWasNotImproved: [
214
+ ["bytes32", "address", "bool"],
215
+ [dataStore_1.WITHDRAWAL_FEE_FACTOR_KEY, marketAddress, false],
216
+ ],
209
217
  virtualMarketId: [
210
218
  ["bytes32", "address"],
211
219
  [dataStore_1.VIRTUAL_MARKET_ID_KEY, marketAddress],
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getIsMarketAvailableForExpressSwaps = exports.getPriceForPnl = exports.getOpenInterestForBalance = exports.getOpenInterestInTokens = exports.getOpenInterestUsd = exports.getMarketPnl = exports.getMarketDivisor = exports.getReservedUsd = exports.getAvailableUsdLiquidityForCollateral = 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.getIsMarketAvailableForExpressSwaps = exports.getPriceForPnl = exports.getOpenInterestForBalance = exports.getOpenInterestInTokens = exports.getOpenInterestUsd = exports.getMarketPnl = 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 factors_1 = require("../configs/factors");
5
5
  const tokens_1 = require("../configs/tokens");
6
6
  const numbers_1 = require("./numbers");
@@ -115,6 +115,12 @@ function getOppositeCollateral(marketInfo, tokenAddress) {
115
115
  return undefined;
116
116
  }
117
117
  exports.getOppositeCollateral = getOppositeCollateral;
118
+ function getOppositeCollateralFromConfig(marketConfig, tokenAddress) {
119
+ return marketConfig.shortTokenAddress === tokenAddress
120
+ ? marketConfig.longTokenAddress
121
+ : marketConfig.shortTokenAddress;
122
+ }
123
+ exports.getOppositeCollateralFromConfig = getOppositeCollateralFromConfig;
118
124
  function getAvailableUsdLiquidityForCollateral(marketInfo, isLong) {
119
125
  const poolUsd = getPoolUsdWithoutPnl(marketInfo, isLong, "minPrice");
120
126
  if (marketInfo.isSpotOnly) {
@@ -1,9 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Multicall = exports.MAX_TIMEOUT = void 0;
3
+ exports.Multicall = exports.MAX_CONCURRENT_BATCHES = exports.MAX_CALLS_PER_BATCH = exports.MAX_TIMEOUT = void 0;
4
4
  const abis_1 = require("../abis");
5
5
  const common_1 = require("./common");
6
- exports.MAX_TIMEOUT = 20000;
6
+ exports.MAX_TIMEOUT = 40000;
7
+ exports.MAX_CALLS_PER_BATCH = 1000;
8
+ exports.MAX_CONCURRENT_BATCHES = 1;
7
9
  class Multicall {
8
10
  static async getInstance(sdk) {
9
11
  const chainId = sdk.chainId;
@@ -98,21 +100,56 @@ class Multicall {
98
100
  return multicallResult;
99
101
  };
100
102
  const timeoutController = new AbortController();
101
- const result = await Promise.race([
102
- client.multicall({ contracts: encodedPayload }),
103
- (0, common_1.sleep)(maxTimeout, timeoutController.signal).then(() => Promise.reject(new Error("multicall timeout"))),
104
- ])
105
- .then((response) => {
106
- timeoutController.abort();
107
- return processResponse(response);
108
- })
109
- .catch((_viemError) => {
110
- timeoutController.abort();
111
- const e = new Error(_viemError.message.slice(0, 150));
112
- /* eslint-disable-next-line */
113
- console.error(e);
114
- throw e;
115
- });
103
+ const payloadSize = encodedPayload.length;
104
+ let result;
105
+ if (payloadSize > exports.MAX_CALLS_PER_BATCH) {
106
+ const batches = [];
107
+ for (let i = 0; i < encodedPayload.length; i += exports.MAX_CALLS_PER_BATCH) {
108
+ batches.push(encodedPayload.slice(i, i + exports.MAX_CALLS_PER_BATCH));
109
+ }
110
+ const batchResults = [];
111
+ for (let i = 0; i < batches.length; i += exports.MAX_CONCURRENT_BATCHES) {
112
+ const batchGroup = batches.slice(i, i + exports.MAX_CONCURRENT_BATCHES);
113
+ const groupResults = await Promise.all(batchGroup.map(async (batch) => {
114
+ const batchTimeoutController = new AbortController();
115
+ try {
116
+ const response = await Promise.race([
117
+ client.multicall({ contracts: batch }),
118
+ (0, common_1.sleep)(maxTimeout, batchTimeoutController.signal).then(() => Promise.reject(new Error("multicall timeout"))),
119
+ ]);
120
+ batchTimeoutController.abort();
121
+ return response;
122
+ }
123
+ catch (error) {
124
+ batchTimeoutController.abort();
125
+ const e = new Error((error instanceof Error ? error.message : String(error)).slice(0, 150));
126
+ /* eslint-disable-next-line */
127
+ console.error(e);
128
+ throw e;
129
+ }
130
+ }));
131
+ batchResults.push(...groupResults);
132
+ }
133
+ const combinedResponse = batchResults.flat();
134
+ result = processResponse(combinedResponse);
135
+ }
136
+ else {
137
+ result = await Promise.race([
138
+ client.multicall({ contracts: encodedPayload }),
139
+ (0, common_1.sleep)(maxTimeout, timeoutController.signal).then(() => Promise.reject(new Error("multicall timeout"))),
140
+ ])
141
+ .then((response) => {
142
+ timeoutController.abort();
143
+ return processResponse(response);
144
+ })
145
+ .catch((_viemError) => {
146
+ timeoutController.abort();
147
+ const e = new Error(_viemError.message.slice(0, 150));
148
+ /* eslint-disable-next-line */
149
+ console.error(e);
150
+ throw e;
151
+ });
152
+ }
116
153
  if (result.success) {
117
154
  return result;
118
155
  }
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.roundToOrder = exports.roundToTwoDecimals = exports.roundUpDivision = exports.parseValue = exports.bigNumberify = exports.toBigNumberWithDecimals = exports.roundWithDecimals = exports.getPlusOrMinusSymbol = exports.padDecimals = exports.limitDecimals = exports.getLimitedDisplay = exports.formatAmountFree = exports.formatArrayAmount = exports.formatKeyAmount = exports.formatAmount = exports.numberWithCommas = exports.formatFactor = exports.formatBalanceAmount = exports.formatAmountHuman = exports.formatPercentageDisplay = exports.formatUsdPrice = exports.formatRatePercentage = exports.formatTokenAmountWithUsd = exports.formatTokenAmount = exports.formatPercentage = exports.formatDeltaUsd = exports.formatBigUsd = exports.formatUsd = exports.adjustForDecimals = exports.bigintToNumber = exports.trimZeroDecimals = exports.numberToBigint = exports.applyFactor = exports.roundUpMagnitudeDivision = exports.getBasisPoints = exports.basisPointsToFloat = exports.expandDecimals = exports.TRIGGER_PREFIX_BELOW = exports.TRIGGER_PREFIX_ABOVE = exports.PERCENT_PRECISION_DECIMALS = exports.MaxUint256 = exports.BN_NEGATIVE_ONE = exports.BN_ONE = exports.BN_ZERO = exports.PRECISION = exports.PRECISION_DECIMALS = exports.BASIS_POINTS_DECIMALS = exports.BASIS_POINTS_DIVISOR_BIGINT = exports.BASIS_POINTS_DIVISOR = exports.USD_DECIMALS = void 0;
4
- exports.absDiffBps = exports.clamp = exports.calculateDisplayDecimals = exports.deserializeBigIntsInObject = exports.serializeBigIntsInObject = exports.removeTrailingZeros = exports.maxbigint = exports.minBigNumber = exports.roundBigIntToDecimals = void 0;
3
+ exports.parseValue = exports.bigNumberify = exports.toBigNumberWithDecimals = exports.roundWithDecimals = exports.getPlusOrMinusSymbol = exports.padDecimals = exports.limitDecimals = exports.getLimitedDisplay = exports.formatAmountFree = exports.formatArrayAmount = exports.formatKeyAmount = exports.formatAmount = exports.numberWithCommas = exports.formatFactor = exports.formatBalanceAmount = exports.formatAmountHuman = exports.formatPercentageDisplay = exports.formatUsdPrice = exports.formatRatePercentage = exports.formatTokenAmountWithUsd = exports.formatTokenAmount = exports.formatPercentage = exports.formatDeltaUsd = exports.formatBigUsd = exports.formatUsd = exports.adjustForDecimals = exports.bigintToNumber = exports.trimZeroDecimals = exports.numberToBigint = exports.applyFactor = exports.roundUpMagnitudeDivision = exports.getBasisPoints = exports.basisPointsToFloat = exports.expandDecimals = exports.safeDivide = exports.isNonZero = exports.TRIGGER_PREFIX_BELOW = exports.TRIGGER_PREFIX_ABOVE = exports.PERCENT_PRECISION_DECIMALS = exports.MaxInt256 = exports.MaxUint256 = exports.BN_NEGATIVE_ONE = exports.BN_ONE = exports.BN_ZERO = exports.PRECISION = exports.PRECISION_DECIMALS = exports.BASIS_POINTS_DECIMALS = exports.BASIS_POINTS_DIVISOR_BIGINT = exports.BASIS_POINTS_DIVISOR = exports.USD_DECIMALS = void 0;
4
+ exports.absDiffBps = exports.clamp = exports.calculateDisplayDecimals = exports.deserializeBigIntsInObject = exports.serializeBigIntsInObject = exports.removeTrailingZeros = exports.maxbigint = exports.minBigNumber = exports.roundBigIntToDecimals = exports.roundToOrder = exports.roundToTwoDecimals = exports.roundUpDivision = void 0;
5
5
  const viem_1 = require("viem");
6
6
  const bigmath_1 = require("./bigmath");
7
7
  exports.USD_DECIMALS = 30;
@@ -14,11 +14,20 @@ exports.BN_ZERO = 0n;
14
14
  exports.BN_ONE = 1n;
15
15
  exports.BN_NEGATIVE_ONE = -1n;
16
16
  exports.MaxUint256 = BigInt("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
17
+ exports.MaxInt256 = BigInt("0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
17
18
  exports.PERCENT_PRECISION_DECIMALS = exports.PRECISION_DECIMALS - 2;
18
19
  const MAX_EXCEEDING_THRESHOLD = "1000000000";
19
20
  const MIN_EXCEEDING_THRESHOLD = "0.01";
20
21
  exports.TRIGGER_PREFIX_ABOVE = ">";
21
22
  exports.TRIGGER_PREFIX_BELOW = "<";
23
+ function isNonZero(value) {
24
+ return value !== 0 && value !== 0n;
25
+ }
26
+ exports.isNonZero = isNonZero;
27
+ function safeDivide(a, b) {
28
+ return (a / b);
29
+ }
30
+ exports.safeDivide = safeDivide;
22
31
  function expandDecimals(n, decimals) {
23
32
  return BigInt(n) * 10n ** BigInt(decimals);
24
33
  }
@@ -149,7 +158,16 @@ function formatPercentage(percentage, opts = {}) {
149
158
  exports.formatPercentage = formatPercentage;
150
159
  function formatTokenAmount(amount, tokenDecimals, symbol, opts = {}) {
151
160
  const { showAllSignificant = false, fallbackToZero = false, useCommas = false, minThreshold = "0", maxThreshold, } = opts;
152
- const displayDecimals = opts.displayDecimals ?? (opts.isStable ? 2 : 4);
161
+ let displayDecimals;
162
+ if (opts.displayDecimals !== undefined) {
163
+ displayDecimals = opts.displayDecimals;
164
+ }
165
+ else if (typeof amount === "bigint" && tokenDecimals) {
166
+ displayDecimals = calculateDisplayDecimals(amount, tokenDecimals, 1, opts.isStable);
167
+ }
168
+ else {
169
+ displayDecimals = opts.isStable ? 2 : 4;
170
+ }
153
171
  const symbolStr = symbol ? ` ${symbol}` : "";
154
172
  if (typeof amount !== "bigint" || !tokenDecimals) {
155
173
  if (fallbackToZero) {
@@ -273,17 +291,10 @@ function formatBalanceAmount(amount, tokenDecimals, tokenSymbol, { showZero = fa
273
291
  const absAmount = bigmath_1.bigMath.abs(amount);
274
292
  const absAmountFloat = bigintToNumber(absAmount, tokenDecimals);
275
293
  let value = "";
276
- const baseDecimals = isStable ? 2 : 4;
277
- if (absAmountFloat >= 1)
278
- value = (0, exports.formatAmount)(absAmount, tokenDecimals, baseDecimals, true);
279
- else if (absAmountFloat >= 0.1)
280
- value = (0, exports.formatAmount)(absAmount, tokenDecimals, baseDecimals + 1, true);
281
- else if (absAmountFloat >= 0.01)
282
- value = (0, exports.formatAmount)(absAmount, tokenDecimals, baseDecimals + 2, true);
283
- else if (absAmountFloat >= 0.001)
284
- value = (0, exports.formatAmount)(absAmount, tokenDecimals, baseDecimals + 3, true);
285
- else if (absAmountFloat >= 1e-8)
286
- value = (0, exports.formatAmount)(absAmount, tokenDecimals, 8, true);
294
+ if (absAmountFloat >= 1e-8) {
295
+ const displayDecimals = calculateDisplayDecimals(absAmount, tokenDecimals, 1, isStable);
296
+ value = (0, exports.formatAmount)(absAmount, tokenDecimals, displayDecimals, true);
297
+ }
287
298
  else {
288
299
  if (toExponential) {
289
300
  value = bigintToNumber(absAmount, tokenDecimals).toExponential(2);
@@ -105,7 +105,7 @@ function getOrderInfo(p) {
105
105
  usdIn: (0, tokens_1.convertToUsd)(order.initialCollateralDeltaAmount, initialCollateralToken.decimals, initialCollateralToken.prices.minPrice),
106
106
  shouldUnwrapNativeToken: order.shouldUnwrapNativeToken,
107
107
  shouldApplyPriceImpact: true,
108
- isAtomicSwap: false,
108
+ swapPricingType: orders_1.SwapPricingType.Swap,
109
109
  });
110
110
  const priceImpactAmount = (0, tokens_1.convertToTokenAmount)(swapPathStats?.totalSwapPriceImpactDeltaUsd, targetCollateralToken.decimals, targetCollateralToken.prices.minPrice);
111
111
  const swapFeeAmount = (0, tokens_1.convertToTokenAmount)(swapPathStats?.totalSwapFeeUsd, targetCollateralToken.decimals, targetCollateralToken.prices.minPrice);
@@ -178,7 +178,7 @@ function getOrderInfo(p) {
178
178
  usdIn: (0, tokens_1.convertToUsd)(order.initialCollateralDeltaAmount, initialCollateralToken.decimals, initialCollateralToken.prices.minPrice),
179
179
  shouldUnwrapNativeToken: order.shouldUnwrapNativeToken,
180
180
  shouldApplyPriceImpact: true,
181
- isAtomicSwap: false,
181
+ swapPricingType: orders_1.SwapPricingType.Swap,
182
182
  });
183
183
  let triggerThresholdType;
184
184
  if (!isMarketOrderType(order.orderType)) {
@@ -37,12 +37,26 @@ const findSwapPathsBetweenTokens_1 = require("../findSwapPathsBetweenTokens");
37
37
  const result = (0, findSwapPathsBetweenTokens_1.findSwapPathsBetweenTokens)(graph);
38
38
  (0, vitest_1.expect)(result).toEqual({
39
39
  BTC: {
40
+ BTC: [
41
+ ["ETH", "USDC"],
42
+ ["USDC", "ETH"],
43
+ ],
40
44
  ETH: [[], ["USDC"]],
41
45
  USDC: [[], ["ETH"]],
42
46
  },
43
47
  ETH: {
48
+ ETH: [
49
+ ["USDC", "BTC"],
50
+ ["BTC", "USDC"],
51
+ ],
44
52
  USDC: [[], ["BTC"]],
45
53
  },
54
+ USDC: {
55
+ USDC: [
56
+ ["ETH", "BTC"],
57
+ ["BTC", "ETH"],
58
+ ],
59
+ },
46
60
  });
47
61
  });
48
62
  (0, vitest_1.it)("should handle multiple markets between same tokens", () => {
@@ -57,8 +71,12 @@ const findSwapPathsBetweenTokens_1 = require("../findSwapPathsBetweenTokens");
57
71
  const result = (0, findSwapPathsBetweenTokens_1.findSwapPathsBetweenTokens)(graph);
58
72
  (0, vitest_1.expect)(result).toEqual({
59
73
  ETH: {
74
+ ETH: [["USDC"]],
60
75
  USDC: [[], ["USDC", "ETH"]],
61
76
  },
77
+ USDC: {
78
+ USDC: [["ETH"]],
79
+ },
62
80
  });
63
81
  });
64
82
  (0, vitest_1.it)("should not include self-loops", () => {
@@ -139,12 +157,16 @@ const findSwapPathsBetweenTokens_1 = require("../findSwapPathsBetweenTokens");
139
157
  const result = (0, findSwapPathsBetweenTokens_1.findSwapPathsBetweenTokens)(graph);
140
158
  (0, vitest_1.expect)(result).toEqual({
141
159
  A: {
160
+ A: [["USDC"]],
142
161
  B: [["USDC"]],
143
162
  USDC: [[], ["USDC", "A"]],
144
163
  },
145
164
  B: {
146
165
  USDC: [[], ["USDC", "A"]],
147
166
  },
167
+ USDC: {
168
+ USDC: [["A"]],
169
+ },
148
170
  });
149
171
  });
150
172
  });