@gmx-io/sdk 1.0.4 → 1.1.0

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 (190) hide show
  1. package/build/cjs/src/abis/ArbitrumNodeInterface.json +105 -0
  2. package/build/cjs/src/abis/CustomErrors.json +613 -16
  3. package/build/cjs/src/abis/ERC20PermitInterface.json +118 -0
  4. package/build/cjs/src/abis/GelatoRelayRouter.json +1426 -0
  5. package/build/cjs/src/abis/RelayParams.json +110 -0
  6. package/build/cjs/src/abis/SubaccountApproval.json +17 -0
  7. package/build/cjs/src/abis/SubaccountGelatoRelayRouter.json +1885 -0
  8. package/build/cjs/src/abis/SyntheticsReader.json +1 -131
  9. package/build/cjs/src/abis/__tests__/abis.spec.js +40 -0
  10. package/build/cjs/src/abis/index.js +8 -0
  11. package/build/cjs/src/configs/__tests__/permits.spec.js +109 -0
  12. package/build/cjs/src/configs/__tests__/tokens.spec.js +1 -1
  13. package/build/cjs/src/configs/chains.js +36 -5
  14. package/build/cjs/src/configs/contracts.js +15 -3
  15. package/build/cjs/src/configs/dataStore.js +26 -4
  16. package/build/cjs/src/configs/express.js +35 -0
  17. package/build/cjs/src/configs/factors.js +4 -3
  18. package/build/cjs/src/configs/markets.js +28 -0
  19. package/build/cjs/src/configs/tokens.js +90 -0
  20. package/build/cjs/src/configs/twap.js +10 -0
  21. package/build/cjs/src/index.js +3 -3
  22. package/build/cjs/src/modules/markets/index.js +1 -0
  23. package/build/cjs/src/modules/markets/query-builders.js +4 -0
  24. package/build/cjs/src/modules/orders/helpers.js +2 -0
  25. package/build/cjs/src/modules/orders/orders.js +4 -3
  26. package/build/cjs/src/modules/utils/utils.js +11 -1
  27. package/build/cjs/src/prebuilt/hashedKinkModelMarketRatesKeys.json +32 -0
  28. package/build/cjs/src/prebuilt/hashedMarketConfigKeys.json +323 -0
  29. package/build/cjs/src/prebuilt/hashedMarketValuesKeys.json +60 -0
  30. package/build/cjs/src/test/mock.js +30 -1
  31. package/build/cjs/src/types/twap.js +2 -0
  32. package/build/cjs/src/utils/__tests__/numbers.spec.js +217 -0
  33. package/build/cjs/src/utils/__tests__/orderTransactions/batchUtils.spec.js +241 -0
  34. package/build/cjs/src/utils/__tests__/orderTransactions/decreaseOrders.spec.js +595 -0
  35. package/build/cjs/src/utils/__tests__/orderTransactions/exchangeRouterMulticall.spec.js +296 -0
  36. package/build/cjs/src/utils/__tests__/orderTransactions/externalCalls.spec.js +118 -0
  37. package/build/cjs/src/utils/__tests__/orderTransactions/increaseOrders.spec.js +762 -0
  38. package/build/cjs/src/utils/__tests__/orderTransactions/swapOrders.spec.js +469 -0
  39. package/build/cjs/src/utils/__tests__/orderTransactions/updateOrder.spec.js +80 -0
  40. package/build/cjs/src/utils/__tests__/parseError.spec.js +263 -0
  41. package/build/cjs/src/utils/__tests__/uiFeeReceiver.spec.js +45 -0
  42. package/build/cjs/src/utils/bigmath.js +3 -0
  43. package/build/cjs/src/utils/callContract.js +2 -2
  44. package/build/cjs/src/utils/errors/index.js +18 -0
  45. package/build/cjs/src/utils/errors/parseError.js +150 -0
  46. package/build/cjs/src/utils/{contracts.js → errors/transactionsErrors.js} +35 -3
  47. package/build/cjs/src/utils/fees/executionFee.js +52 -1
  48. package/build/cjs/src/utils/fees/index.js +8 -4
  49. package/build/cjs/src/utils/gelatoRelay.js +5 -0
  50. package/build/cjs/src/utils/hash.js +6 -1
  51. package/build/cjs/src/utils/marketKeysAndConfigs.js +4 -0
  52. package/build/cjs/src/utils/markets.js +5 -1
  53. package/build/cjs/src/utils/numbers.js +559 -5
  54. package/build/cjs/src/utils/orderTransactions.js +638 -0
  55. package/build/cjs/src/utils/orders.js +7 -1
  56. package/build/cjs/src/utils/simulateExecuteOrder.js +6 -2
  57. package/build/cjs/src/utils/swap/__tests__/mockRouting.spec.js +11 -6
  58. package/build/cjs/src/utils/swap/__tests__/swapRouting.spec.js +4 -4
  59. package/build/cjs/src/utils/swap/__tests__/swapStats.spec.js +9 -0
  60. package/build/cjs/src/utils/swap/swapPath.js +30 -13
  61. package/build/cjs/src/utils/swap/swapRouting.js +4 -2
  62. package/build/cjs/src/utils/swap/swapStats.js +6 -5
  63. package/build/cjs/src/utils/testUtil.js +1 -1
  64. package/build/cjs/src/utils/time.js +30 -0
  65. package/build/cjs/src/utils/tokens.js +16 -2
  66. package/build/cjs/src/utils/twap/index.js +53 -0
  67. package/build/cjs/src/utils/twap/uiFeeReceiver.js +31 -0
  68. package/build/cjs/src/utils/types.js +13 -0
  69. package/build/esm/src/abis/ArbitrumNodeInterface.json +105 -0
  70. package/build/esm/src/abis/CustomErrors.json +613 -16
  71. package/build/esm/src/abis/ERC20PermitInterface.json +118 -0
  72. package/build/esm/src/abis/GelatoRelayRouter.json +1426 -0
  73. package/build/esm/src/abis/RelayParams.json +110 -0
  74. package/build/esm/src/abis/SubaccountApproval.json +17 -0
  75. package/build/esm/src/abis/SubaccountGelatoRelayRouter.json +1885 -0
  76. package/build/esm/src/abis/SyntheticsReader.json +1 -131
  77. package/build/esm/src/abis/__tests__/abis.spec.js +35 -0
  78. package/build/esm/src/abis/index.js +8 -0
  79. package/build/esm/src/configs/__tests__/permits.spec.js +107 -0
  80. package/build/esm/src/configs/__tests__/tokens.spec.js +3 -3
  81. package/build/esm/src/configs/chains.js +33 -3
  82. package/build/esm/src/configs/contracts.js +15 -3
  83. package/build/esm/src/configs/dataStore.js +19 -1
  84. package/build/esm/src/configs/express.js +29 -0
  85. package/build/esm/src/configs/factors.js +1 -3
  86. package/build/esm/src/configs/markets.js +28 -0
  87. package/build/esm/src/configs/tokens.js +90 -0
  88. package/build/esm/src/configs/twap.js +7 -0
  89. package/build/esm/src/index.js +4 -4
  90. package/build/esm/src/modules/markets/index.js +1 -0
  91. package/build/esm/src/modules/markets/query-builders.js +4 -0
  92. package/build/esm/src/modules/orders/helpers.js +2 -0
  93. package/build/esm/src/modules/orders/orders.js +4 -3
  94. package/build/esm/src/modules/utils/utils.js +13 -3
  95. package/build/esm/src/prebuilt/hashedKinkModelMarketRatesKeys.json +32 -0
  96. package/build/esm/src/prebuilt/hashedMarketConfigKeys.json +323 -0
  97. package/build/esm/src/prebuilt/hashedMarketValuesKeys.json +60 -0
  98. package/build/esm/src/test/mock.js +28 -0
  99. package/build/esm/src/types/twap.js +1 -0
  100. package/build/esm/src/utils/__tests__/numbers.spec.js +219 -2
  101. package/build/esm/src/utils/__tests__/orderTransactions/batchUtils.spec.js +239 -0
  102. package/build/esm/src/utils/__tests__/orderTransactions/decreaseOrders.spec.js +593 -0
  103. package/build/esm/src/utils/__tests__/orderTransactions/exchangeRouterMulticall.spec.js +294 -0
  104. package/build/esm/src/utils/__tests__/orderTransactions/externalCalls.spec.js +116 -0
  105. package/build/esm/src/utils/__tests__/orderTransactions/increaseOrders.spec.js +760 -0
  106. package/build/esm/src/utils/__tests__/orderTransactions/swapOrders.spec.js +467 -0
  107. package/build/esm/src/utils/__tests__/orderTransactions/updateOrder.spec.js +78 -0
  108. package/build/esm/src/utils/__tests__/parseError.spec.js +261 -0
  109. package/build/esm/src/utils/__tests__/uiFeeReceiver.spec.js +43 -0
  110. package/build/esm/src/utils/bigmath.js +3 -0
  111. package/build/esm/src/utils/callContract.js +3 -3
  112. package/build/esm/src/utils/errors/index.js +2 -0
  113. package/build/esm/src/utils/errors/parseError.js +141 -0
  114. package/build/esm/src/utils/{contracts.js → errors/transactionsErrors.js} +30 -1
  115. package/build/esm/src/utils/fees/executionFee.js +48 -1
  116. package/build/esm/src/utils/fees/index.js +8 -4
  117. package/build/esm/src/utils/gelatoRelay.js +2 -0
  118. package/build/esm/src/utils/hash.js +5 -1
  119. package/build/esm/src/utils/marketKeysAndConfigs.js +5 -1
  120. package/build/esm/src/utils/markets.js +3 -0
  121. package/build/esm/src/utils/numbers.js +516 -1
  122. package/build/esm/src/utils/orderTransactions.js +607 -0
  123. package/build/esm/src/utils/orders.js +5 -0
  124. package/build/esm/src/utils/simulateExecuteOrder.js +6 -2
  125. package/build/esm/src/utils/swap/__tests__/mockRouting.spec.js +11 -6
  126. package/build/esm/src/utils/swap/__tests__/swapRouting.spec.js +4 -4
  127. package/build/esm/src/utils/swap/__tests__/swapStats.spec.js +9 -0
  128. package/build/esm/src/utils/swap/swapPath.js +30 -13
  129. package/build/esm/src/utils/swap/swapRouting.js +4 -2
  130. package/build/esm/src/utils/swap/swapStats.js +6 -5
  131. package/build/esm/src/utils/testUtil.js +2 -2
  132. package/build/esm/src/utils/time.js +23 -0
  133. package/build/esm/src/utils/tokens.js +14 -1
  134. package/build/esm/src/utils/twap/index.js +44 -0
  135. package/build/esm/src/utils/twap/uiFeeReceiver.js +24 -0
  136. package/build/esm/src/utils/types.js +8 -0
  137. package/build/types/src/abis/__tests__/abis.spec.d.ts +1 -0
  138. package/build/types/src/abis/index.d.ts +1 -1
  139. package/build/types/src/configs/__tests__/permits.spec.d.ts +1 -0
  140. package/build/types/src/configs/chains.d.ts +29 -1
  141. package/build/types/src/configs/contracts.d.ts +238 -1
  142. package/build/types/src/configs/dataStore.d.ts +10 -0
  143. package/build/types/src/configs/express.d.ts +12 -0
  144. package/build/types/src/configs/factors.d.ts +1 -3
  145. package/build/types/src/configs/tokens.d.ts +2 -2
  146. package/build/types/src/configs/twap.d.ts +7 -0
  147. package/build/types/src/modules/markets/query-builders.d.ts +1 -1
  148. package/build/types/src/modules/markets/types.d.ts +2 -2
  149. package/build/types/src/modules/orders/orders.d.ts +2 -1
  150. package/build/types/src/modules/orders/transactions/createIncreaseOrderTxn.d.ts +1 -1
  151. package/build/types/src/test/mock.d.ts +16 -1
  152. package/build/types/src/types/fees.d.ts +9 -0
  153. package/build/types/src/types/markets.d.ts +1 -0
  154. package/build/types/src/types/orders.d.ts +3 -0
  155. package/build/types/src/types/tokens.d.ts +27 -0
  156. package/build/types/src/types/trade.d.ts +2 -19
  157. package/build/types/src/types/twap.d.ts +11 -0
  158. package/build/types/src/utils/__tests__/orderTransactions/batchUtils.spec.d.ts +1 -0
  159. package/build/types/src/utils/__tests__/orderTransactions/decreaseOrders.spec.d.ts +1 -0
  160. package/build/types/src/utils/__tests__/orderTransactions/exchangeRouterMulticall.spec.d.ts +1 -0
  161. package/build/types/src/utils/__tests__/orderTransactions/externalCalls.spec.d.ts +1 -0
  162. package/build/types/src/utils/__tests__/orderTransactions/increaseOrders.spec.d.ts +1 -0
  163. package/build/types/src/utils/__tests__/orderTransactions/swapOrders.spec.d.ts +1 -0
  164. package/build/types/src/utils/__tests__/orderTransactions/updateOrder.spec.d.ts +1 -0
  165. package/build/types/src/utils/__tests__/parseError.spec.d.ts +1 -0
  166. package/build/types/src/utils/__tests__/uiFeeReceiver.spec.d.ts +1 -0
  167. package/build/types/src/utils/bigmath.d.ts +1 -0
  168. package/build/types/src/utils/errors/index.d.ts +2 -0
  169. package/build/types/src/utils/errors/parseError.d.ts +50 -0
  170. package/build/types/src/utils/errors/transactionsErrors.d.ts +34 -0
  171. package/build/types/src/utils/fees/executionFee.d.ts +35 -2
  172. package/build/types/src/utils/fees/index.d.ts +1 -1
  173. package/build/types/src/utils/gelatoRelay.d.ts +2 -0
  174. package/build/types/src/utils/hash.d.ts +2 -0
  175. package/build/types/src/utils/marketKeysAndConfigs.d.ts +1 -0
  176. package/build/types/src/utils/markets.d.ts +1 -0
  177. package/build/types/src/utils/numbers.d.ts +110 -1
  178. package/build/types/src/utils/orderTransactions.d.ts +231 -0
  179. package/build/types/src/utils/orders.d.ts +2 -1
  180. package/build/types/src/utils/simulateExecuteOrder.d.ts +4 -0
  181. package/build/types/src/utils/swap/swapPath.d.ts +5 -5
  182. package/build/types/src/utils/swap/swapRouting.d.ts +2 -2
  183. package/build/types/src/utils/swap/swapStats.d.ts +2 -0
  184. package/build/types/src/utils/time.d.ts +14 -0
  185. package/build/types/src/utils/tokens.d.ts +6 -5
  186. package/build/types/src/utils/twap/index.d.ts +17 -0
  187. package/build/types/src/utils/twap/uiFeeReceiver.d.ts +9 -0
  188. package/build/types/src/utils/types.d.ts +5 -0
  189. package/package.json +3 -1
  190. package/build/types/src/utils/contracts.d.ts +0 -19
@@ -1,8 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.hashDataMap = exports.hashString = exports.hashData = void 0;
3
+ exports.keccakString = exports.hashDataMap = exports.hashString = exports.hashData = exports.ZERO_DATA = void 0;
4
4
  const viem_1 = require("viem");
5
5
  const LruCache_1 = require("./LruCache");
6
+ exports.ZERO_DATA = "0x";
6
7
  const dataCache = new LruCache_1.LRUCache(10000);
7
8
  function hashData(dataTypes, dataValues) {
8
9
  const key = JSON.stringify({ dataTypes, dataValues }, (_, val) => (typeof val === "bigint" ? String(val) : val));
@@ -39,3 +40,7 @@ function hashDataMap(map) {
39
40
  return result;
40
41
  }
41
42
  exports.hashDataMap = hashDataMap;
43
+ function keccakString(string) {
44
+ return (0, viem_1.keccak256)((0, viem_1.stringToBytes)(string));
45
+ }
46
+ exports.keccakString = keccakString;
@@ -174,6 +174,10 @@ function hashMarketConfigKeys(market) {
174
174
  ["bytes32", "address", "bool"],
175
175
  [dataStore_1.SWAP_FEE_FACTOR_KEY, marketAddress, false],
176
176
  ],
177
+ atomicSwapFeeFactor: [
178
+ ["bytes32", "address"],
179
+ [dataStore_1.ATOMIC_SWAP_FEE_FACTOR_KEY, marketAddress],
180
+ ],
177
181
  swapImpactFactorPositive: [
178
182
  ["bytes32", "address", "bool"],
179
183
  [dataStore_1.SWAP_IMPACT_FACTOR_KEY, marketAddress, true],
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getPriceForPnl = 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.getMarketIndexName = exports.getMarketFullName = void 0;
3
+ exports.getIsMarketAvailableForExpressSwaps = exports.getPriceForPnl = 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.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");
@@ -167,3 +167,7 @@ function getPriceForPnl(prices, isLong, maximize) {
167
167
  return maximize ? prices.minPrice : prices.maxPrice;
168
168
  }
169
169
  exports.getPriceForPnl = getPriceForPnl;
170
+ function getIsMarketAvailableForExpressSwaps(marketInfo) {
171
+ return [marketInfo.indexToken, marketInfo.longToken, marketInfo.shortToken].every((token) => token.hasPriceFeedProvider);
172
+ }
173
+ exports.getIsMarketAvailableForExpressSwaps = getIsMarketAvailableForExpressSwaps;
@@ -1,24 +1,35 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.adjustForDecimals = exports.bigintToNumber = exports.numberToBigint = exports.applyFactor = exports.roundUpMagnitudeDivision = exports.getBasisPoints = exports.basisPointsToFloat = exports.expandDecimals = exports.BN_NEGATIVE_ONE = exports.BN_ONE = exports.BN_ZERO = exports.PRECISION = exports.PRECISION_DECIMALS = void 0;
4
- const factors_1 = require("../configs/factors");
3
+ exports.roundBigIntToDecimals = 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.formatBalanceAmountWithUsd = exports.formatBalanceAmount = exports.formatAmountHuman = exports.formatPercentageDisplay = exports.formatUsdPrice = exports.formatRatePercentage = exports.formatTokenAmountWithUsd = exports.formatTokenAmount = exports.formatPercentage = exports.formatDeltaUsd = 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_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 = void 0;
5
+ const viem_1 = require("viem");
6
+ const bigmath_1 = require("./bigmath");
7
+ exports.USD_DECIMALS = 30;
8
+ exports.BASIS_POINTS_DIVISOR = 10000;
9
+ exports.BASIS_POINTS_DIVISOR_BIGINT = 10000n;
5
10
  exports.PRECISION_DECIMALS = 30;
6
11
  exports.PRECISION = expandDecimals(1, exports.PRECISION_DECIMALS);
7
12
  exports.BN_ZERO = 0n;
8
13
  exports.BN_ONE = 1n;
9
14
  exports.BN_NEGATIVE_ONE = -1n;
15
+ exports.MaxUint256 = BigInt("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
16
+ exports.PERCENT_PRECISION_DECIMALS = exports.PRECISION_DECIMALS - 2;
17
+ const MAX_EXCEEDING_THRESHOLD = "1000000000";
18
+ const MIN_EXCEEDING_THRESHOLD = "0.01";
19
+ exports.TRIGGER_PREFIX_ABOVE = ">";
20
+ exports.TRIGGER_PREFIX_BELOW = "<";
10
21
  function expandDecimals(n, decimals) {
11
22
  return BigInt(n) * 10n ** BigInt(decimals);
12
23
  }
13
24
  exports.expandDecimals = expandDecimals;
14
25
  function basisPointsToFloat(basisPoints) {
15
- return (basisPoints * exports.PRECISION) / factors_1.BASIS_POINTS_DIVISOR_BIGINT;
26
+ return (basisPoints * exports.PRECISION) / exports.BASIS_POINTS_DIVISOR_BIGINT;
16
27
  }
17
28
  exports.basisPointsToFloat = basisPointsToFloat;
18
29
  function getBasisPoints(numerator, denominator, shouldRoundUp = false) {
19
- const result = (numerator * factors_1.BASIS_POINTS_DIVISOR_BIGINT) / denominator;
30
+ const result = (numerator * exports.BASIS_POINTS_DIVISOR_BIGINT) / denominator;
20
31
  if (shouldRoundUp) {
21
- const remainder = (numerator * factors_1.BASIS_POINTS_DIVISOR_BIGINT) % denominator;
32
+ const remainder = (numerator * exports.BASIS_POINTS_DIVISOR_BIGINT) % denominator;
22
33
  if (remainder !== 0n) {
23
34
  return result < 0n ? result - 1n : result + 1n;
24
35
  }
@@ -56,6 +67,13 @@ function numberToBigint(value, decimals) {
56
67
  return negative ? -res : res;
57
68
  }
58
69
  exports.numberToBigint = numberToBigint;
70
+ const trimZeroDecimals = (amount) => {
71
+ if (parseFloat(amount) === parseInt(amount)) {
72
+ return parseInt(amount).toString();
73
+ }
74
+ return amount;
75
+ };
76
+ exports.trimZeroDecimals = trimZeroDecimals;
59
77
  function bigintToNumber(value, decimals) {
60
78
  const negative = value < 0;
61
79
  if (negative)
@@ -71,3 +89,539 @@ function adjustForDecimals(amount, divDecimals, mulDecimals) {
71
89
  return (amount * expandDecimals(1, mulDecimals)) / expandDecimals(1, divDecimals);
72
90
  }
73
91
  exports.adjustForDecimals = adjustForDecimals;
92
+ function formatUsd(usd, opts = {}) {
93
+ const { fallbackToZero = false, displayDecimals = 2 } = opts;
94
+ if (typeof usd !== "bigint") {
95
+ if (fallbackToZero) {
96
+ usd = 0n;
97
+ }
98
+ else {
99
+ return undefined;
100
+ }
101
+ }
102
+ if (opts.visualMultiplier) {
103
+ usd *= BigInt(opts.visualMultiplier);
104
+ }
105
+ const defaultMinThreshold = displayDecimals > 1 ? "0." + "0".repeat(displayDecimals - 1) + "1" : undefined;
106
+ const exceedingInfo = getLimitedDisplay(usd, exports.USD_DECIMALS, {
107
+ maxThreshold: opts.maxThreshold,
108
+ minThreshold: opts.minThreshold ?? defaultMinThreshold,
109
+ });
110
+ const maybePlus = opts.displayPlus ? "+" : "";
111
+ const sign = usd < 0n ? "-" : maybePlus;
112
+ const symbol = exceedingInfo.symbol ? `${exceedingInfo.symbol} ` : "";
113
+ const displayUsd = (0, exports.formatAmount)(exceedingInfo.value, exports.USD_DECIMALS, displayDecimals, true);
114
+ return `${symbol}${sign}$${displayUsd}`;
115
+ }
116
+ exports.formatUsd = formatUsd;
117
+ function formatDeltaUsd(deltaUsd, percentage, opts = {}) {
118
+ if (typeof deltaUsd !== "bigint") {
119
+ if (opts.fallbackToZero) {
120
+ return `${formatUsd(0n)} (${(0, exports.formatAmount)(0n, 2, 2)}%)`;
121
+ }
122
+ return undefined;
123
+ }
124
+ const sign = getPlusOrMinusSymbol(deltaUsd, { showPlusForZero: opts.showPlusForZero });
125
+ const exceedingInfo = getLimitedDisplay(deltaUsd, exports.USD_DECIMALS);
126
+ const percentageStr = percentage !== undefined ? ` (${sign}${formatPercentage(bigmath_1.bigMath.abs(percentage))})` : "";
127
+ const deltaUsdStr = (0, exports.formatAmount)(exceedingInfo.value, exports.USD_DECIMALS, 2, true);
128
+ const symbol = exceedingInfo.symbol ? `${exceedingInfo.symbol} ` : "";
129
+ return `${symbol}${sign}$${deltaUsdStr}${percentageStr}`;
130
+ }
131
+ exports.formatDeltaUsd = formatDeltaUsd;
132
+ function formatPercentage(percentage, opts = {}) {
133
+ const { fallbackToZero = false, signed = false, displayDecimals = 2, bps = true } = opts;
134
+ if (percentage === undefined) {
135
+ if (fallbackToZero) {
136
+ return `${(0, exports.formatAmount)(0n, exports.PERCENT_PRECISION_DECIMALS, displayDecimals)}%`;
137
+ }
138
+ return undefined;
139
+ }
140
+ const sign = signed ? getPlusOrMinusSymbol(percentage) : "";
141
+ return `${sign}${(0, exports.formatAmount)(bigmath_1.bigMath.abs(percentage), bps ? 2 : exports.PERCENT_PRECISION_DECIMALS, displayDecimals)}%`;
142
+ }
143
+ exports.formatPercentage = formatPercentage;
144
+ function formatTokenAmount(amount, tokenDecimals, symbol, opts = {}) {
145
+ const { displayDecimals = 4, showAllSignificant = false, fallbackToZero = false, useCommas = false, minThreshold = "0", maxThreshold, } = opts;
146
+ const symbolStr = symbol ? ` ${symbol}` : "";
147
+ if (typeof amount !== "bigint" || !tokenDecimals) {
148
+ if (fallbackToZero) {
149
+ amount = 0n;
150
+ tokenDecimals = displayDecimals;
151
+ }
152
+ else {
153
+ return undefined;
154
+ }
155
+ }
156
+ let amountStr;
157
+ const maybePlus = opts.displayPlus ? "+" : "";
158
+ const sign = amount < 0n ? "-" : maybePlus;
159
+ if (showAllSignificant) {
160
+ amountStr = (0, exports.formatAmountFree)(amount, tokenDecimals, tokenDecimals);
161
+ }
162
+ else {
163
+ const exceedingInfo = getLimitedDisplay(amount, tokenDecimals, { maxThreshold, minThreshold });
164
+ const symbol = exceedingInfo.symbol ? `${exceedingInfo.symbol} ` : "";
165
+ amountStr = `${symbol}${sign}${(0, exports.formatAmount)(exceedingInfo.value, tokenDecimals, displayDecimals, useCommas)}`;
166
+ }
167
+ return `${amountStr}${symbolStr}`;
168
+ }
169
+ exports.formatTokenAmount = formatTokenAmount;
170
+ function formatTokenAmountWithUsd(tokenAmount, usdAmount, tokenSymbol, tokenDecimals, opts = {}) {
171
+ if (typeof tokenAmount !== "bigint" || typeof usdAmount !== "bigint" || !tokenSymbol || !tokenDecimals) {
172
+ if (!opts.fallbackToZero) {
173
+ return undefined;
174
+ }
175
+ }
176
+ const tokenStr = formatTokenAmount(tokenAmount, tokenDecimals, tokenSymbol, {
177
+ ...opts,
178
+ useCommas: true,
179
+ displayPlus: opts.displayPlus,
180
+ });
181
+ const usdStr = formatUsd(usdAmount, {
182
+ fallbackToZero: opts.fallbackToZero,
183
+ displayPlus: opts.displayPlus,
184
+ });
185
+ return `${tokenStr} (${usdStr})`;
186
+ }
187
+ exports.formatTokenAmountWithUsd = formatTokenAmountWithUsd;
188
+ /**
189
+ *
190
+ * @param opts.signed - Default `true`. whether to display a `+` or `-` sign for all non-zero values.
191
+ */
192
+ function formatRatePercentage(rate, opts) {
193
+ if (typeof rate !== "bigint") {
194
+ return "-";
195
+ }
196
+ const signed = opts?.signed ?? true;
197
+ const plurOrMinus = signed ? getPlusOrMinusSymbol(rate) : "";
198
+ const amount = bigmath_1.bigMath.abs(rate * 100n);
199
+ return `${plurOrMinus}${(0, exports.formatAmount)(amount, 30, opts?.displayDecimals ?? 4)}%`;
200
+ }
201
+ exports.formatRatePercentage = formatRatePercentage;
202
+ function formatUsdPrice(price, opts = {}) {
203
+ if (price === undefined) {
204
+ return;
205
+ }
206
+ if (price < 0n) {
207
+ return "NA";
208
+ }
209
+ const decimals = calculateDisplayDecimals(price, undefined, opts.visualMultiplier);
210
+ return formatUsd(price, {
211
+ ...opts,
212
+ displayDecimals: decimals,
213
+ });
214
+ }
215
+ exports.formatUsdPrice = formatUsdPrice;
216
+ function formatPercentageDisplay(percentage, hideThreshold) {
217
+ if (hideThreshold && percentage < hideThreshold) {
218
+ return "";
219
+ }
220
+ return `${percentage}%`;
221
+ }
222
+ exports.formatPercentageDisplay = formatPercentageDisplay;
223
+ function formatAmountHuman(amount, tokenDecimals, showDollar = false, displayDecimals = 1) {
224
+ if (amount === undefined) {
225
+ return "...";
226
+ }
227
+ let n = Number((0, exports.formatAmount)(amount, tokenDecimals));
228
+ // For large numbers, we can neglect the decimals to avoid decimals in cases like 9999999.99999
229
+ if (n >= 1000000) {
230
+ n = Math.round(n);
231
+ }
232
+ const isNegative = n < 0;
233
+ const absN = Math.abs(n);
234
+ const sign = showDollar ? "$" : "";
235
+ if (absN >= 1000000000) {
236
+ return `${isNegative ? "-" : ""}${sign}${(absN / 1000000000).toFixed(displayDecimals)}b`;
237
+ }
238
+ if (absN >= 1000000) {
239
+ return `${isNegative ? "-" : ""}${sign}${(absN / 1000000).toFixed(displayDecimals)}m`;
240
+ }
241
+ if (absN >= 1000) {
242
+ return `${isNegative ? "-" : ""}${sign}${(absN / 1000).toFixed(displayDecimals)}k`;
243
+ }
244
+ return `${isNegative ? "-" : ""}${sign}${absN.toFixed(displayDecimals)}`;
245
+ }
246
+ exports.formatAmountHuman = formatAmountHuman;
247
+ function formatBalanceAmount(amount, tokenDecimals, tokenSymbol, showZero = false, toExponential = true) {
248
+ if (amount === undefined)
249
+ return "-";
250
+ if (amount === 0n) {
251
+ if (showZero === true) {
252
+ if (tokenSymbol) {
253
+ return `0.0000 ${tokenSymbol}`;
254
+ }
255
+ return "0.0000";
256
+ }
257
+ return "-";
258
+ }
259
+ const absAmount = bigmath_1.bigMath.abs(amount);
260
+ const absAmountFloat = bigintToNumber(absAmount, tokenDecimals);
261
+ let value = "";
262
+ if (absAmountFloat >= 1)
263
+ value = (0, exports.formatAmount)(amount, tokenDecimals, 4, true);
264
+ else if (absAmountFloat >= 0.1)
265
+ value = (0, exports.formatAmount)(amount, tokenDecimals, 5, true);
266
+ else if (absAmountFloat >= 0.01)
267
+ value = (0, exports.formatAmount)(amount, tokenDecimals, 6, true);
268
+ else if (absAmountFloat >= 0.001)
269
+ value = (0, exports.formatAmount)(amount, tokenDecimals, 7, true);
270
+ else if (absAmountFloat >= 1e-8)
271
+ value = (0, exports.formatAmount)(amount, tokenDecimals, 8, true);
272
+ else {
273
+ if (toExponential) {
274
+ value = bigintToNumber(amount, tokenDecimals).toExponential(2);
275
+ }
276
+ else {
277
+ value = bigintToNumber(amount, tokenDecimals).toFixed(8);
278
+ }
279
+ }
280
+ if (tokenSymbol) {
281
+ // Non-breaking space
282
+ return `${value} ${tokenSymbol}`;
283
+ }
284
+ return value;
285
+ }
286
+ exports.formatBalanceAmount = formatBalanceAmount;
287
+ function formatBalanceAmountWithUsd(amount, amountUsd, tokenDecimals, tokenSymbol, showZero = false) {
288
+ if (showZero === false && amount === 0n) {
289
+ return "-";
290
+ }
291
+ const value = formatBalanceAmount(amount, tokenDecimals, tokenSymbol, showZero);
292
+ const usd = formatUsd(amountUsd);
293
+ // Regular space
294
+ return `${value} (${usd})`;
295
+ }
296
+ exports.formatBalanceAmountWithUsd = formatBalanceAmountWithUsd;
297
+ function formatFactor(factor) {
298
+ if (factor == 0n) {
299
+ return "0";
300
+ }
301
+ if (bigmath_1.bigMath.abs(factor) > exports.PRECISION * 1000n) {
302
+ return (factor / exports.PRECISION).toString();
303
+ }
304
+ const trailingZeroes = bigmath_1.bigMath
305
+ .abs(factor)
306
+ .toString()
307
+ .match(/^(.+?)(?<zeroes>0*)$/)?.groups?.zeroes?.length || 0;
308
+ const factorDecimals = 30 - trailingZeroes;
309
+ return (0, exports.formatAmount)(factor, 30, factorDecimals);
310
+ }
311
+ exports.formatFactor = formatFactor;
312
+ function numberWithCommas(x) {
313
+ if (x === undefined || x === null) {
314
+ return "...";
315
+ }
316
+ const parts = x.toString().split(".");
317
+ parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
318
+ return parts.join(".");
319
+ }
320
+ exports.numberWithCommas = numberWithCommas;
321
+ const formatAmount = (amount, tokenDecimals, displayDecimals, useCommas, defaultValue, visualMultiplier) => {
322
+ if (defaultValue === undefined || defaultValue === null) {
323
+ defaultValue = "...";
324
+ }
325
+ if (amount === undefined || amount === null || amount === "") {
326
+ return defaultValue;
327
+ }
328
+ if (displayDecimals === undefined) {
329
+ displayDecimals = 4;
330
+ }
331
+ const amountBigInt = roundWithDecimals(BigInt(amount) * BigInt(visualMultiplier ?? 1), {
332
+ displayDecimals,
333
+ decimals: tokenDecimals,
334
+ });
335
+ let amountStr = (0, viem_1.formatUnits)(amountBigInt, tokenDecimals);
336
+ amountStr = (0, exports.limitDecimals)(amountStr, displayDecimals);
337
+ if (displayDecimals !== 0) {
338
+ amountStr = (0, exports.padDecimals)(amountStr, displayDecimals);
339
+ }
340
+ if (useCommas) {
341
+ return numberWithCommas(amountStr);
342
+ }
343
+ return amountStr;
344
+ };
345
+ exports.formatAmount = formatAmount;
346
+ const formatKeyAmount = (map, key, tokenDecimals, displayDecimals, useCommas) => {
347
+ const value = map ? map[key] ?? undefined : undefined;
348
+ if (value === undefined || value === null) {
349
+ return "...";
350
+ }
351
+ return (0, exports.formatAmount)(value, tokenDecimals, displayDecimals, useCommas);
352
+ };
353
+ exports.formatKeyAmount = formatKeyAmount;
354
+ const formatArrayAmount = (arr, index, tokenDecimals, displayDecimals, useCommas) => {
355
+ if (!arr || arr[index] === undefined || arr[index] === null) {
356
+ return "...";
357
+ }
358
+ return (0, exports.formatAmount)(arr[index], tokenDecimals, displayDecimals, useCommas);
359
+ };
360
+ exports.formatArrayAmount = formatArrayAmount;
361
+ const formatAmountFree = (amount, tokenDecimals, displayDecimals) => {
362
+ if (amount === undefined || amount === null) {
363
+ return "...";
364
+ }
365
+ amount = BigInt(amount);
366
+ let amountStr = (0, viem_1.formatUnits)(amount, tokenDecimals);
367
+ amountStr = (0, exports.limitDecimals)(amountStr, displayDecimals);
368
+ return (0, exports.trimZeroDecimals)(amountStr);
369
+ };
370
+ exports.formatAmountFree = formatAmountFree;
371
+ function getLimitedDisplay(amount, tokenDecimals, opts = {}) {
372
+ const { maxThreshold = MAX_EXCEEDING_THRESHOLD, minThreshold = MIN_EXCEEDING_THRESHOLD } = opts;
373
+ const max = maxThreshold === null ? null : expandDecimals(BigInt(maxThreshold), tokenDecimals);
374
+ const min = (0, viem_1.parseUnits)(minThreshold.toString(), tokenDecimals);
375
+ const absAmount = bigmath_1.bigMath.abs(amount);
376
+ if (absAmount == 0n) {
377
+ return {
378
+ symbol: "",
379
+ value: absAmount,
380
+ };
381
+ }
382
+ const symbol = max !== null && absAmount > max ? exports.TRIGGER_PREFIX_ABOVE : absAmount < min ? exports.TRIGGER_PREFIX_BELOW : "";
383
+ const value = max !== null && absAmount > max ? max : absAmount < min ? min : absAmount;
384
+ return {
385
+ symbol,
386
+ value,
387
+ };
388
+ }
389
+ exports.getLimitedDisplay = getLimitedDisplay;
390
+ const limitDecimals = (amount, maxDecimals) => {
391
+ let amountStr = amount.toString();
392
+ if (maxDecimals === undefined) {
393
+ return amountStr;
394
+ }
395
+ if (maxDecimals === 0) {
396
+ return amountStr.split(".")[0];
397
+ }
398
+ const dotIndex = amountStr.indexOf(".");
399
+ if (dotIndex !== -1) {
400
+ let decimals = amountStr.length - dotIndex - 1;
401
+ if (decimals > maxDecimals) {
402
+ amountStr = amountStr.substr(0, amountStr.length - (decimals - maxDecimals));
403
+ }
404
+ }
405
+ return amountStr;
406
+ };
407
+ exports.limitDecimals = limitDecimals;
408
+ const padDecimals = (amount, minDecimals) => {
409
+ let amountStr = amount.toString();
410
+ const dotIndex = amountStr.indexOf(".");
411
+ if (dotIndex !== -1) {
412
+ const decimals = amountStr.length - dotIndex - 1;
413
+ if (decimals < minDecimals) {
414
+ amountStr = amountStr.padEnd(amountStr.length + (minDecimals - decimals), "0");
415
+ }
416
+ }
417
+ else {
418
+ amountStr = amountStr + "." + "0".repeat(minDecimals);
419
+ }
420
+ return amountStr;
421
+ };
422
+ exports.padDecimals = padDecimals;
423
+ function getPlusOrMinusSymbol(value, opts = {}) {
424
+ if (value === undefined) {
425
+ return "";
426
+ }
427
+ const { showPlusForZero = false } = opts;
428
+ return value === 0n ? (showPlusForZero ? "+" : "") : value < 0n ? "-" : "+";
429
+ }
430
+ exports.getPlusOrMinusSymbol = getPlusOrMinusSymbol;
431
+ function roundWithDecimals(value, opts) {
432
+ if (opts.displayDecimals === opts.decimals) {
433
+ return BigInt(value);
434
+ }
435
+ let valueString = value.toString();
436
+ let isNegative = false;
437
+ if (valueString[0] === "-") {
438
+ valueString = valueString.slice(1);
439
+ isNegative = true;
440
+ }
441
+ if (valueString.length < opts.decimals) {
442
+ valueString = valueString.padStart(opts.decimals, "0");
443
+ }
444
+ const mainPart = valueString.slice(0, valueString.length - opts.decimals + opts.displayDecimals);
445
+ const partToRound = valueString.slice(valueString.length - opts.decimals + opts.displayDecimals);
446
+ let mainPartBigInt = BigInt(mainPart);
447
+ let returnValue = mainPartBigInt;
448
+ if (partToRound.length !== 0) {
449
+ if (Number(partToRound[0]) >= 5) {
450
+ mainPartBigInt += 1n;
451
+ }
452
+ returnValue = BigInt(mainPartBigInt.toString() + new Array(partToRound.length).fill("0").join(""));
453
+ }
454
+ return isNegative ? returnValue * -1n : returnValue;
455
+ }
456
+ exports.roundWithDecimals = roundWithDecimals;
457
+ // TODO: Remove this function
458
+ function toBigNumberWithDecimals(value, decimals) {
459
+ if (!value)
460
+ return exports.BN_ZERO;
461
+ const parts = value.split(".");
462
+ const integerPart = parts[0];
463
+ const decimalPart = parts.length > 1 ? parts[1] : "";
464
+ const paddingZeros = decimals - decimalPart.length;
465
+ if (paddingZeros >= 0) {
466
+ const result = integerPart + decimalPart + "0".repeat(paddingZeros);
467
+ return BigInt(result);
468
+ }
469
+ else {
470
+ const result = integerPart + decimalPart.substring(0, decimals);
471
+ return BigInt(result);
472
+ }
473
+ }
474
+ exports.toBigNumberWithDecimals = toBigNumberWithDecimals;
475
+ /**
476
+ *
477
+ * @deprecated Use BigInt instead
478
+ */
479
+ function bigNumberify(n) {
480
+ try {
481
+ if (n === undefined)
482
+ throw new Error("n is undefined");
483
+ if (n === null)
484
+ throw new Error("n is null");
485
+ return BigInt(n);
486
+ }
487
+ catch (e) {
488
+ // eslint-disable-next-line no-console
489
+ console.error("bigNumberify error", e);
490
+ return undefined;
491
+ }
492
+ }
493
+ exports.bigNumberify = bigNumberify;
494
+ const parseValue = (value, tokenDecimals) => {
495
+ const pValue = parseFloat(value);
496
+ if (isNaN(pValue)) {
497
+ return undefined;
498
+ }
499
+ value = (0, exports.limitDecimals)(value, tokenDecimals);
500
+ const amount = (0, viem_1.parseUnits)(value, tokenDecimals);
501
+ return bigNumberify(amount);
502
+ };
503
+ exports.parseValue = parseValue;
504
+ function roundUpDivision(a, b) {
505
+ return (a + b - 1n) / b;
506
+ }
507
+ exports.roundUpDivision = roundUpDivision;
508
+ function roundToTwoDecimals(n) {
509
+ return Math.round(n * 100) / 100;
510
+ }
511
+ exports.roundToTwoDecimals = roundToTwoDecimals;
512
+ function roundToOrder(n, significantDigits = 1) {
513
+ const decimals = Math.max(n.toString().length - significantDigits, 0);
514
+ return (n / expandDecimals(1, decimals)) * expandDecimals(1, decimals);
515
+ }
516
+ exports.roundToOrder = roundToOrder;
517
+ function roundBigIntToDecimals(value, tokenDecimals, roundToDecimals) {
518
+ const excessDecimals = tokenDecimals - roundToDecimals;
519
+ const divisor = BigInt(10 ** excessDecimals);
520
+ const scaledValue = value / divisor;
521
+ const remainder = scaledValue % 10n;
522
+ const roundedValue = remainder >= 5n ? scaledValue + 10n - remainder : scaledValue - remainder;
523
+ return roundedValue * divisor;
524
+ }
525
+ exports.roundBigIntToDecimals = roundBigIntToDecimals;
526
+ function minBigNumber(...args) {
527
+ if (!args.length)
528
+ return undefined;
529
+ return args.reduce((acc, num) => (num < acc ? num : acc), args[0]);
530
+ }
531
+ exports.minBigNumber = minBigNumber;
532
+ function maxbigint(...args) {
533
+ if (!args.length)
534
+ return undefined;
535
+ return args.reduce((acc, num) => (num > acc ? num : acc), args[0]);
536
+ }
537
+ exports.maxbigint = maxbigint;
538
+ function removeTrailingZeros(amount) {
539
+ const amountWithoutZeros = Number(amount);
540
+ if (!amountWithoutZeros)
541
+ return amount;
542
+ return amountWithoutZeros;
543
+ }
544
+ exports.removeTrailingZeros = removeTrailingZeros;
545
+ function serializeBigIntsInObject(obj) {
546
+ const result = Array.isArray(obj) ? [] : {};
547
+ for (const key in obj) {
548
+ const value = obj[key];
549
+ if (typeof value === "bigint") {
550
+ result[key] = { type: "bigint", value: String(value) };
551
+ }
552
+ else if (value && typeof value === "object") {
553
+ result[key] = serializeBigIntsInObject(value);
554
+ }
555
+ else {
556
+ result[key] = value;
557
+ }
558
+ }
559
+ return result;
560
+ }
561
+ exports.serializeBigIntsInObject = serializeBigIntsInObject;
562
+ function deserializeBigIntsInObject(obj) {
563
+ const result = Array.isArray(obj) ? [] : {};
564
+ for (const key in obj) {
565
+ const value = obj[key];
566
+ if (typeof value === "object" &&
567
+ value !== null &&
568
+ (("type" in value && value.type === "bigint") || ("_type" in value && value._type === "BigNumber"))) {
569
+ if ("value" in value && typeof value.value === "string") {
570
+ result[key] = BigInt(value.value);
571
+ }
572
+ else if ("hex" in value && typeof value.hex === "string") {
573
+ if (value.hex.startsWith("-")) {
574
+ result[key] = BigInt(value.hex.slice(1)) * -1n;
575
+ }
576
+ else {
577
+ result[key] = BigInt(value.hex);
578
+ }
579
+ }
580
+ }
581
+ else if (value && typeof value === "object") {
582
+ result[key] = deserializeBigIntsInObject(value);
583
+ }
584
+ else {
585
+ result[key] = value;
586
+ }
587
+ }
588
+ return result;
589
+ }
590
+ exports.deserializeBigIntsInObject = deserializeBigIntsInObject;
591
+ function calculateDisplayDecimals(price, decimals = exports.USD_DECIMALS, visualMultiplier = 1) {
592
+ if (price === undefined || price === 0n)
593
+ return 2;
594
+ const priceNumber = bigintToNumber(bigmath_1.bigMath.abs(price) * BigInt(visualMultiplier), decimals);
595
+ if (isNaN(priceNumber))
596
+ return 2;
597
+ if (priceNumber >= 1000)
598
+ return 2;
599
+ if (priceNumber >= 100)
600
+ return 3;
601
+ if (priceNumber >= 1)
602
+ return 4;
603
+ if (priceNumber >= 0.1)
604
+ return 5;
605
+ if (priceNumber >= 0.01)
606
+ return 6;
607
+ if (priceNumber >= 0.0001)
608
+ return 7;
609
+ if (priceNumber >= 0.00001)
610
+ return 8;
611
+ return 9;
612
+ }
613
+ exports.calculateDisplayDecimals = calculateDisplayDecimals;
614
+ function clamp(value, min, max) {
615
+ return Math.max(min, Math.min(value, max));
616
+ }
617
+ exports.clamp = clamp;
618
+ function absDiffBps(value, base) {
619
+ if ((value === 0n && base !== 0n) || (value !== 0n && base === 0n)) {
620
+ return exports.BASIS_POINTS_DIVISOR_BIGINT;
621
+ }
622
+ if (value === 0n && base === 0n) {
623
+ return 0n;
624
+ }
625
+ return bigmath_1.bigMath.mulDiv(bigmath_1.bigMath.abs(value - base), exports.BASIS_POINTS_DIVISOR_BIGINT, base);
626
+ }
627
+ exports.absDiffBps = absDiffBps;