@gmx-io/sdk 1.0.5 → 1.0.7

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 (194) 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 +6 -4
  18. package/build/cjs/src/configs/tokens.js +50 -0
  19. package/build/cjs/src/configs/twap.js +10 -0
  20. package/build/cjs/src/index.js +3 -3
  21. package/build/cjs/src/modules/markets/index.js +1 -0
  22. package/build/cjs/src/modules/markets/query-builders.js +4 -0
  23. package/build/cjs/src/modules/orders/helpers.js +3 -1
  24. package/build/cjs/src/modules/orders/transactions/createDecreaseOrderTxn.js +1 -1
  25. package/build/cjs/src/modules/orders/transactions/createIncreaseOrderTxn.js +6 -3
  26. package/build/cjs/src/modules/orders/transactions/createSwapOrderTxn.js +1 -1
  27. package/build/cjs/src/modules/utils/utils.js +11 -1
  28. package/build/cjs/src/prebuilt/hashedMarketConfigKeys.json +119 -0
  29. package/build/cjs/src/test/mock.js +30 -1
  30. package/build/cjs/src/types/subsquid.js +2303 -0
  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 +7 -3
  53. package/build/cjs/src/utils/numbers.js +560 -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/tradeHistory.js +4 -4
  67. package/build/cjs/src/utils/twap/index.js +53 -0
  68. package/build/cjs/src/utils/twap/uiFeeReceiver.js +31 -0
  69. package/build/cjs/src/utils/types.js +13 -0
  70. package/build/esm/src/abis/ArbitrumNodeInterface.json +105 -0
  71. package/build/esm/src/abis/CustomErrors.json +613 -16
  72. package/build/esm/src/abis/ERC20PermitInterface.json +118 -0
  73. package/build/esm/src/abis/GelatoRelayRouter.json +1426 -0
  74. package/build/esm/src/abis/RelayParams.json +110 -0
  75. package/build/esm/src/abis/SubaccountApproval.json +17 -0
  76. package/build/esm/src/abis/SubaccountGelatoRelayRouter.json +1885 -0
  77. package/build/esm/src/abis/SyntheticsReader.json +1 -131
  78. package/build/esm/src/abis/__tests__/abis.spec.js +35 -0
  79. package/build/esm/src/abis/index.js +8 -0
  80. package/build/esm/src/configs/__tests__/permits.spec.js +107 -0
  81. package/build/esm/src/configs/__tests__/tokens.spec.js +3 -3
  82. package/build/esm/src/configs/chains.js +33 -3
  83. package/build/esm/src/configs/contracts.js +15 -3
  84. package/build/esm/src/configs/dataStore.js +19 -1
  85. package/build/esm/src/configs/express.js +29 -0
  86. package/build/esm/src/configs/factors.js +1 -3
  87. package/build/esm/src/configs/tokens.js +50 -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 +3 -1
  93. package/build/esm/src/modules/orders/transactions/createDecreaseOrderTxn.js +1 -1
  94. package/build/esm/src/modules/orders/transactions/createIncreaseOrderTxn.js +6 -3
  95. package/build/esm/src/modules/orders/transactions/createSwapOrderTxn.js +1 -1
  96. package/build/esm/src/modules/utils/utils.js +13 -3
  97. package/build/esm/src/prebuilt/hashedMarketConfigKeys.json +119 -0
  98. package/build/esm/src/test/mock.js +28 -0
  99. package/build/esm/src/types/subsquid.js +2300 -0
  100. package/build/esm/src/types/twap.js +1 -0
  101. package/build/esm/src/utils/__tests__/numbers.spec.js +219 -2
  102. package/build/esm/src/utils/__tests__/orderTransactions/batchUtils.spec.js +239 -0
  103. package/build/esm/src/utils/__tests__/orderTransactions/decreaseOrders.spec.js +593 -0
  104. package/build/esm/src/utils/__tests__/orderTransactions/exchangeRouterMulticall.spec.js +294 -0
  105. package/build/esm/src/utils/__tests__/orderTransactions/externalCalls.spec.js +116 -0
  106. package/build/esm/src/utils/__tests__/orderTransactions/increaseOrders.spec.js +760 -0
  107. package/build/esm/src/utils/__tests__/orderTransactions/swapOrders.spec.js +467 -0
  108. package/build/esm/src/utils/__tests__/orderTransactions/updateOrder.spec.js +78 -0
  109. package/build/esm/src/utils/__tests__/parseError.spec.js +261 -0
  110. package/build/esm/src/utils/__tests__/uiFeeReceiver.spec.js +43 -0
  111. package/build/esm/src/utils/bigmath.js +3 -0
  112. package/build/esm/src/utils/callContract.js +3 -3
  113. package/build/esm/src/utils/errors/index.js +2 -0
  114. package/build/esm/src/utils/errors/parseError.js +141 -0
  115. package/build/esm/src/utils/{contracts.js → errors/transactionsErrors.js} +30 -1
  116. package/build/esm/src/utils/fees/executionFee.js +48 -1
  117. package/build/esm/src/utils/fees/index.js +8 -4
  118. package/build/esm/src/utils/gelatoRelay.js +2 -0
  119. package/build/esm/src/utils/hash.js +5 -1
  120. package/build/esm/src/utils/marketKeysAndConfigs.js +5 -1
  121. package/build/esm/src/utils/markets.js +5 -2
  122. package/build/esm/src/utils/numbers.js +517 -1
  123. package/build/esm/src/utils/orderTransactions.js +607 -0
  124. package/build/esm/src/utils/orders.js +5 -0
  125. package/build/esm/src/utils/simulateExecuteOrder.js +6 -2
  126. package/build/esm/src/utils/swap/__tests__/mockRouting.spec.js +11 -6
  127. package/build/esm/src/utils/swap/__tests__/swapRouting.spec.js +4 -4
  128. package/build/esm/src/utils/swap/__tests__/swapStats.spec.js +9 -0
  129. package/build/esm/src/utils/swap/swapPath.js +30 -13
  130. package/build/esm/src/utils/swap/swapRouting.js +4 -2
  131. package/build/esm/src/utils/swap/swapStats.js +6 -5
  132. package/build/esm/src/utils/testUtil.js +2 -2
  133. package/build/esm/src/utils/time.js +23 -0
  134. package/build/esm/src/utils/tokens.js +14 -1
  135. package/build/esm/src/utils/tradeHistory.js +4 -4
  136. package/build/esm/src/utils/twap/index.js +44 -0
  137. package/build/esm/src/utils/twap/uiFeeReceiver.js +24 -0
  138. package/build/esm/src/utils/types.js +8 -0
  139. package/build/types/src/abis/__tests__/abis.spec.d.ts +1 -0
  140. package/build/types/src/abis/index.d.ts +1 -1
  141. package/build/types/src/configs/__tests__/permits.spec.d.ts +1 -0
  142. package/build/types/src/configs/chains.d.ts +29 -1
  143. package/build/types/src/configs/contracts.d.ts +238 -1
  144. package/build/types/src/configs/dataStore.d.ts +10 -0
  145. package/build/types/src/configs/express.d.ts +12 -0
  146. package/build/types/src/configs/factors.d.ts +1 -3
  147. package/build/types/src/configs/tokens.d.ts +2 -2
  148. package/build/types/src/configs/twap.d.ts +7 -0
  149. package/build/types/src/modules/markets/query-builders.d.ts +1 -1
  150. package/build/types/src/modules/markets/types.d.ts +2 -2
  151. package/build/types/src/modules/orders/transactions/createIncreaseOrderTxn.d.ts +1 -1
  152. package/build/types/src/test/mock.d.ts +16 -1
  153. package/build/types/src/types/fees.d.ts +9 -0
  154. package/build/types/src/types/markets.d.ts +1 -0
  155. package/build/types/src/types/orders.d.ts +3 -0
  156. package/build/types/src/types/subsquid.d.ts +6916 -0
  157. package/build/types/src/types/tokens.d.ts +27 -0
  158. package/build/types/src/types/trade.d.ts +2 -19
  159. package/build/types/src/types/tradeHistory.d.ts +0 -40
  160. package/build/types/src/types/twap.d.ts +11 -0
  161. package/build/types/src/utils/__tests__/orderTransactions/batchUtils.spec.d.ts +1 -0
  162. package/build/types/src/utils/__tests__/orderTransactions/decreaseOrders.spec.d.ts +1 -0
  163. package/build/types/src/utils/__tests__/orderTransactions/exchangeRouterMulticall.spec.d.ts +1 -0
  164. package/build/types/src/utils/__tests__/orderTransactions/externalCalls.spec.d.ts +1 -0
  165. package/build/types/src/utils/__tests__/orderTransactions/increaseOrders.spec.d.ts +1 -0
  166. package/build/types/src/utils/__tests__/orderTransactions/swapOrders.spec.d.ts +1 -0
  167. package/build/types/src/utils/__tests__/orderTransactions/updateOrder.spec.d.ts +1 -0
  168. package/build/types/src/utils/__tests__/parseError.spec.d.ts +1 -0
  169. package/build/types/src/utils/__tests__/uiFeeReceiver.spec.d.ts +1 -0
  170. package/build/types/src/utils/bigmath.d.ts +1 -0
  171. package/build/types/src/utils/errors/index.d.ts +2 -0
  172. package/build/types/src/utils/errors/parseError.d.ts +50 -0
  173. package/build/types/src/utils/errors/transactionsErrors.d.ts +34 -0
  174. package/build/types/src/utils/fees/executionFee.d.ts +35 -2
  175. package/build/types/src/utils/fees/index.d.ts +1 -1
  176. package/build/types/src/utils/gelatoRelay.d.ts +2 -0
  177. package/build/types/src/utils/hash.d.ts +2 -0
  178. package/build/types/src/utils/marketKeysAndConfigs.d.ts +1 -0
  179. package/build/types/src/utils/markets.d.ts +2 -1
  180. package/build/types/src/utils/numbers.d.ts +111 -1
  181. package/build/types/src/utils/orderTransactions.d.ts +231 -0
  182. package/build/types/src/utils/orders.d.ts +2 -1
  183. package/build/types/src/utils/simulateExecuteOrder.d.ts +4 -0
  184. package/build/types/src/utils/swap/swapPath.d.ts +5 -5
  185. package/build/types/src/utils/swap/swapRouting.d.ts +2 -2
  186. package/build/types/src/utils/swap/swapStats.d.ts +2 -0
  187. package/build/types/src/utils/time.d.ts +14 -0
  188. package/build/types/src/utils/tokens.d.ts +6 -5
  189. package/build/types/src/utils/tradeHistory.d.ts +2 -2
  190. package/build/types/src/utils/twap/index.d.ts +17 -0
  191. package/build/types/src/utils/twap/uiFeeReceiver.d.ts +9 -0
  192. package/build/types/src/utils/types.d.ts +5 -0
  193. package/package.json +9 -2
  194. package/build/types/src/utils/contracts.d.ts +0 -19
@@ -0,0 +1 @@
1
+ export {};
@@ -1,6 +1,7 @@
1
1
  import { describe, expect, it } from "vitest";
2
- import { BASIS_POINTS_DIVISOR_BIGINT } from "../../configs/factors";
3
- import { BN_NEGATIVE_ONE, BN_ONE, BN_ZERO, PRECISION, applyFactor, basisPointsToFloat, bigintToNumber, expandDecimals, getBasisPoints, numberToBigint, roundUpMagnitudeDivision, } from "../numbers";
2
+ import { BASIS_POINTS_DIVISOR_BIGINT, USD_DECIMALS } from "../../configs/factors";
3
+ const ONE_USD = 1000000000000000000000000000000n;
4
+ import { applyFactor, basisPointsToFloat, bigintToNumber, BN_NEGATIVE_ONE, BN_ONE, BN_ZERO, expandDecimals, formatAmount, formatAmountHuman, formatBalanceAmount, formatBalanceAmountWithUsd, formatFactor, formatPercentage, formatUsdPrice, getBasisPoints, numberToBigint, PERCENT_PRECISION_DECIMALS, PRECISION, PRECISION_DECIMALS, roundWithDecimals, roundUpMagnitudeDivision, toBigNumberWithDecimals, } from "../numbers";
4
5
  describe("numbers utils", () => {
5
6
  it("constants", () => {
6
7
  // Just to confirm they exist and have no unexpected changes
@@ -79,3 +80,219 @@ describe("numbers utils", () => {
79
80
  expect(numberToBigint(-1.123456789, 6)).toEqual(-1123456n);
80
81
  });
81
82
  });
83
+ describe("toBigNumberWithDecimals", () => {
84
+ it("should convert string to big number with decimals", () => {
85
+ expect(toBigNumberWithDecimals("0", PRECISION_DECIMALS)).toBe(0n);
86
+ expect(toBigNumberWithDecimals("1", PRECISION_DECIMALS)).toBe(1000000000000000000000000000000n);
87
+ expect(toBigNumberWithDecimals("123.456", PRECISION_DECIMALS)).toBe(123456000000000000000000000000000n);
88
+ expect(toBigNumberWithDecimals("123.456789", PRECISION_DECIMALS)).toBe(123456789000000000000000000000000n);
89
+ expect(toBigNumberWithDecimals("-1.5", PRECISION_DECIMALS)).toBe(-1500000000000000000000000000000n);
90
+ expect(toBigNumberWithDecimals("0.000001", PRECISION_DECIMALS)).toBe(1000000000000000000000000n);
91
+ });
92
+ it("should handle strings with more decimals than token decimals parameter", () => {
93
+ expect(toBigNumberWithDecimals("0.123456789012345678901234567890", 5)).toBe(12345n);
94
+ });
95
+ it("should handle cases with different token decimals ", () => {
96
+ expect(toBigNumberWithDecimals("0.1234567890123456789012345678901", 18)).toBe(123456789012345678n);
97
+ expect(toBigNumberWithDecimals("0.12345", 5)).toBe(12345n);
98
+ expect(toBigNumberWithDecimals("0.1", 1)).toBe(1n);
99
+ });
100
+ it("should be compatible with formatAmount", () => {
101
+ expect(formatAmount(toBigNumberWithDecimals("123.456", PRECISION_DECIMALS), USD_DECIMALS, 3)).toBe("123.456");
102
+ expect(formatAmount(toBigNumberWithDecimals("0.789", PRECISION_DECIMALS), USD_DECIMALS, 2)).toBe("0.79");
103
+ });
104
+ });
105
+ describe("roundWithDecimals", () => {
106
+ it("should round small numbers correctly", () => {
107
+ expect(roundWithDecimals(toBigNumberWithDecimals("0.0000001", PRECISION_DECIMALS), {
108
+ decimals: PRECISION_DECIMALS,
109
+ displayDecimals: 0,
110
+ })).toBe(0n);
111
+ expect(roundWithDecimals(toBigNumberWithDecimals("0.5", PRECISION_DECIMALS), {
112
+ decimals: PRECISION_DECIMALS,
113
+ displayDecimals: 0,
114
+ })).toBe(toBigNumberWithDecimals("1", PRECISION_DECIMALS));
115
+ expect(roundWithDecimals(toBigNumberWithDecimals("0.499", PRECISION_DECIMALS), {
116
+ decimals: PRECISION_DECIMALS,
117
+ displayDecimals: 0,
118
+ })).toBe(0n);
119
+ expect(roundWithDecimals(toBigNumberWithDecimals("1", PRECISION_DECIMALS), {
120
+ decimals: PRECISION_DECIMALS,
121
+ displayDecimals: 1,
122
+ })).toBe(toBigNumberWithDecimals("1", PRECISION_DECIMALS));
123
+ });
124
+ it("should round numbers at specific decimal places", () => {
125
+ expect(roundWithDecimals(toBigNumberWithDecimals("1.49", PRECISION_DECIMALS), {
126
+ decimals: PRECISION_DECIMALS,
127
+ displayDecimals: 1,
128
+ })).toBe(toBigNumberWithDecimals("1.5", PRECISION_DECIMALS));
129
+ expect(roundWithDecimals(toBigNumberWithDecimals("1.44", PRECISION_DECIMALS), {
130
+ decimals: PRECISION_DECIMALS,
131
+ displayDecimals: 1,
132
+ })).toBe(toBigNumberWithDecimals("1.4", PRECISION_DECIMALS));
133
+ expect(roundWithDecimals(toBigNumberWithDecimals("1.499", PRECISION_DECIMALS), {
134
+ decimals: PRECISION_DECIMALS,
135
+ displayDecimals: 2,
136
+ })).toBe(toBigNumberWithDecimals("1.50", PRECISION_DECIMALS));
137
+ expect(roundWithDecimals(toBigNumberWithDecimals("1.495", PRECISION_DECIMALS), {
138
+ decimals: PRECISION_DECIMALS,
139
+ displayDecimals: 2,
140
+ })).toBe(toBigNumberWithDecimals("1.50", PRECISION_DECIMALS));
141
+ });
142
+ it("should round large numbers correctly", () => {
143
+ expect(roundWithDecimals(toBigNumberWithDecimals("499.999999", PRECISION_DECIMALS), {
144
+ decimals: PRECISION_DECIMALS,
145
+ displayDecimals: 5,
146
+ })).toBe(toBigNumberWithDecimals("500.00000", PRECISION_DECIMALS));
147
+ });
148
+ it("should handle complex rounding cases", () => {
149
+ expect(roundWithDecimals(toBigNumberWithDecimals("0.0000000000000000000000000001", PRECISION_DECIMALS), {
150
+ decimals: PRECISION_DECIMALS,
151
+ displayDecimals: 25,
152
+ })).toBe(0n);
153
+ expect(roundWithDecimals(toBigNumberWithDecimals("0.4999999999999999999999999999", PRECISION_DECIMALS), {
154
+ decimals: PRECISION_DECIMALS,
155
+ displayDecimals: 25,
156
+ })).toBe(toBigNumberWithDecimals("0.5", PRECISION_DECIMALS));
157
+ expect(roundWithDecimals(toBigNumberWithDecimals("123.456789", PRECISION_DECIMALS), {
158
+ decimals: PRECISION_DECIMALS,
159
+ displayDecimals: 4,
160
+ })).toBe(toBigNumberWithDecimals("123.4568", PRECISION_DECIMALS));
161
+ });
162
+ it("should round numbers with different token decimals correctly", () => {
163
+ const differentTokenDecimals = [18, 5, 1, 22];
164
+ for (const tokenDecimals of differentTokenDecimals) {
165
+ expect(roundWithDecimals(toBigNumberWithDecimals("123.456789", tokenDecimals), {
166
+ decimals: tokenDecimals,
167
+ displayDecimals: 4,
168
+ })).toBe(toBigNumberWithDecimals("123.4568", tokenDecimals));
169
+ }
170
+ });
171
+ it("should handle edge cases", () => {
172
+ expect(roundWithDecimals(0n, { decimals: PRECISION_DECIMALS, displayDecimals: 10 })).toBe(0n);
173
+ expect(roundWithDecimals(toBigNumberWithDecimals("-1.5", PRECISION_DECIMALS), {
174
+ decimals: PRECISION_DECIMALS,
175
+ displayDecimals: 0,
176
+ })).toBe(toBigNumberWithDecimals("-2", PRECISION_DECIMALS));
177
+ expect(roundWithDecimals(toBigNumberWithDecimals("-1.4", PRECISION_DECIMALS), {
178
+ decimals: PRECISION_DECIMALS,
179
+ displayDecimals: 0,
180
+ })).toBe(toBigNumberWithDecimals("-1", PRECISION_DECIMALS));
181
+ expect(roundWithDecimals(toBigNumberWithDecimals("0.0000001", PRECISION_DECIMALS), {
182
+ decimals: PRECISION_DECIMALS,
183
+ displayDecimals: 5,
184
+ })).toBe(0n);
185
+ });
186
+ });
187
+ describe("formatUsdPrice", () => {
188
+ it("should tolerate undefined", () => {
189
+ expect(formatUsdPrice()).toBeUndefined();
190
+ });
191
+ it("should return NA if negative", () => {
192
+ expect(formatUsdPrice(-1n)).toBe("NA");
193
+ });
194
+ it("should calculate correct decimals if displayDecimals not passed", () => {
195
+ expect(formatUsdPrice(ONE_USD * 10000n)).toBe("$10,000.00");
196
+ expect(formatUsdPrice(ONE_USD * 1000n)).toBe("$1,000.00");
197
+ expect(formatUsdPrice(ONE_USD * 100n)).toBe("$100.000");
198
+ expect(formatUsdPrice(ONE_USD * 10n)).toBe("$10.0000");
199
+ expect(formatUsdPrice(ONE_USD)).toBe("$1.0000");
200
+ expect(formatUsdPrice(ONE_USD / 10n)).toBe("$0.10000");
201
+ expect(formatUsdPrice(ONE_USD / 100n)).toBe("$0.010000");
202
+ expect(formatUsdPrice(ONE_USD / 1000n)).toBe("$0.0010000");
203
+ expect(formatUsdPrice(ONE_USD / 10000n)).toBe("$0.0001000");
204
+ expect(formatUsdPrice(ONE_USD / 100000n)).toBe("$0.00001000");
205
+ expect(formatUsdPrice(ONE_USD / 1000000000n)).toBe("$0.000000001");
206
+ expect(formatUsdPrice(ONE_USD / 10000000000n)).toBe("< $0.000000001");
207
+ });
208
+ });
209
+ describe("formatAmountHuman", () => {
210
+ it("positive", () => {
211
+ expect(formatAmountHuman(ONE_USD, USD_DECIMALS)).toBe("1.0");
212
+ expect(formatAmountHuman(ONE_USD * 1000n, USD_DECIMALS)).toBe("1.0k");
213
+ expect(formatAmountHuman(ONE_USD * 1000000n, USD_DECIMALS)).toBe("1.0m");
214
+ });
215
+ it("negative", () => {
216
+ expect(formatAmountHuman(-1n * ONE_USD, USD_DECIMALS)).toBe("-1.0");
217
+ expect(formatAmountHuman(-1n * ONE_USD * 1000n, USD_DECIMALS)).toBe("-1.0k");
218
+ expect(formatAmountHuman(-1n * ONE_USD * 1000000n, USD_DECIMALS)).toBe("-1.0m");
219
+ });
220
+ it("should display dollar sign", () => {
221
+ expect(formatAmountHuman(ONE_USD, USD_DECIMALS, true)).toBe("$1.0");
222
+ expect(formatAmountHuman(-1n * ONE_USD, USD_DECIMALS, true)).toBe("-$1.0");
223
+ });
224
+ it("should display decimals", () => {
225
+ expect(formatAmountHuman(ONE_USD * 1000n, USD_DECIMALS, false, 2)).toBe("1.00k");
226
+ expect(formatAmountHuman(ONE_USD * 1500000n, USD_DECIMALS, false, 2)).toBe("1.50m");
227
+ expect(formatAmountHuman(ONE_USD * 1000n, USD_DECIMALS, false, 0)).toBe("1k");
228
+ expect(formatAmountHuman(ONE_USD * 1500000n, USD_DECIMALS, false, 0)).toBe("2m");
229
+ });
230
+ });
231
+ describe("formatBalanceAmount", () => {
232
+ it("should display balance amount", () => {
233
+ expect(formatBalanceAmount(ONE_USD * 1000n, USD_DECIMALS)).toBe("1,000.0000");
234
+ expect(formatBalanceAmount(0n, USD_DECIMALS)).toBe("-");
235
+ expect(formatBalanceAmount(0n, USD_DECIMALS, undefined, true)).toBe("0.0000");
236
+ expect(formatBalanceAmount(ONE_USD * 1n, USD_DECIMALS)).toBe("1.0000");
237
+ expect(formatBalanceAmount(ONE_USD / 10n, USD_DECIMALS)).toBe("0.10000");
238
+ expect(formatBalanceAmount(ONE_USD / 100n, USD_DECIMALS)).toBe("0.010000");
239
+ expect(formatBalanceAmount(ONE_USD / 1000n, USD_DECIMALS)).toBe("0.0010000");
240
+ expect(formatBalanceAmount(ONE_USD / 10000n, USD_DECIMALS)).toBe("0.00010000");
241
+ expect(formatBalanceAmount(ONE_USD / 100000n, USD_DECIMALS)).toBe("0.00001000");
242
+ expect(formatBalanceAmount(ONE_USD / 1000000n, USD_DECIMALS)).toBe("0.00000100");
243
+ expect(formatBalanceAmount(ONE_USD / 10000000n, USD_DECIMALS)).toBe("0.00000010");
244
+ expect(formatBalanceAmount(ONE_USD / 100000000n, USD_DECIMALS)).toBe("0.00000001");
245
+ expect(formatBalanceAmount(ONE_USD / 1000000000n, USD_DECIMALS)).toBe("1.00e-9");
246
+ expect(formatBalanceAmount(ONE_USD / 1000000000000n, USD_DECIMALS)).toBe("1.00e-12");
247
+ expect(formatBalanceAmount(ONE_USD * -1n, USD_DECIMALS)).toBe("-1.0000");
248
+ });
249
+ it("should display balance amount with symbol", () => {
250
+ expect(formatBalanceAmount(ONE_USD, USD_DECIMALS, "USDC")).toBe("1.0000 USDC");
251
+ expect(formatBalanceAmount(0n, USD_DECIMALS, "USDC", true)).toBe("0.0000 USDC");
252
+ expect(formatBalanceAmount(0n, USD_DECIMALS, "USDC", false)).toBe("-");
253
+ });
254
+ it("should display balance amount with usd", () => {
255
+ expect(formatBalanceAmountWithUsd(ONE_USD, ONE_USD, USD_DECIMALS)).toBe("1.0000 ($1.00)");
256
+ expect(formatBalanceAmountWithUsd(ONE_USD, ONE_USD, USD_DECIMALS, "USDC")).toBe("1.0000 USDC ($1.00)");
257
+ expect(formatBalanceAmountWithUsd(0n, 0n, USD_DECIMALS, "USDC")).toBe("-");
258
+ expect(formatBalanceAmountWithUsd(0n, 0n, USD_DECIMALS, "USDC", true)).toBe("0.0000 USDC ($0.00)");
259
+ });
260
+ });
261
+ describe("formatFactor", () => {
262
+ it("should format factor", () => {
263
+ expect(formatFactor(0n)).toBe("0");
264
+ expect(formatFactor(1n)).toBe("0.000000000000000000000000000001");
265
+ expect(formatFactor(1000n)).toBe("0.000000000000000000000000001");
266
+ expect(formatFactor(1000000n)).toBe("0.000000000000000000000001");
267
+ expect(formatFactor(1000000000n)).toBe("0.000000000000000000001");
268
+ expect(formatFactor(1000000000000n)).toBe("0.000000000000000001");
269
+ expect(formatFactor(1000000000000000n)).toBe("0.000000000000001");
270
+ expect(formatFactor(1000000000000000000n)).toBe("0.000000000001");
271
+ expect(formatFactor(1000000000000000000000n)).toBe("0.000000001");
272
+ expect(formatFactor(1000000000000000000000000n)).toBe("0.000001");
273
+ expect(formatFactor(1000000000000000000000000000n)).toBe("0.001");
274
+ expect(formatFactor(1000000000000000000000000000000n)).toBe("1");
275
+ });
276
+ });
277
+ describe("formatPercentage", () => {
278
+ it("should format a basic percentage", () => {
279
+ expect(formatPercentage(100n, { displayDecimals: 4 })).toBe("1.0000%");
280
+ expect(formatPercentage(2500n)).toBe("25.00%");
281
+ expect(formatPercentage(123456n)).toBe("1234.56%");
282
+ });
283
+ it("should handle undefined input with fallbackToZero", () => {
284
+ expect(formatPercentage(undefined, { fallbackToZero: true })).toBe("0.00%");
285
+ });
286
+ it("should display signed percentage", () => {
287
+ expect(formatPercentage(100n, { signed: true })).toBe("+1.00%");
288
+ expect(formatPercentage(-100n, { signed: true })).toBe("-1.00%");
289
+ });
290
+ it("should format with different displayDecimals", () => {
291
+ expect(formatPercentage(100n, { displayDecimals: 2 })).toBe("1.00%");
292
+ expect(formatPercentage(123456n, { displayDecimals: 1 })).toBe("1234.6%");
293
+ });
294
+ it("should handle basis points (bps) formatting", () => {
295
+ expect(formatPercentage(toBigNumberWithDecimals("1", PERCENT_PRECISION_DECIMALS), { bps: false, displayDecimals: 4 })).toBe("1.0000%");
296
+ expect(formatPercentage(toBigNumberWithDecimals("0.999", PERCENT_PRECISION_DECIMALS), { bps: false, displayDecimals: 5 })).toBe("0.99900%");
297
+ });
298
+ });
@@ -0,0 +1,239 @@
1
+ import { zeroAddress, zeroHash } from "viem";
2
+ import { describe, expect, it } from "vitest";
3
+ import { ARBITRUM } from "../../../configs/chains";
4
+ import { getTokenBySymbol, getWrappedToken, NATIVE_TOKEN_ADDRESS } from "../../../configs/tokens";
5
+ import { OrderType } from "../../../types/orders";
6
+ import { expandDecimals, parseValue, USD_DECIMALS } from "../../numbers";
7
+ import { buildIncreaseOrderPayload, getBatchExternalCalls, getBatchExternalSwapGasLimit, getBatchIsNativePayment, getBatchTotalExecutionFee, getBatchTotalPayCollateralAmount, getIsEmptyBatch, getIsInvalidBatchReceiver, } from "../../orderTransactions";
8
+ import { mockExternalSwap } from "../../../test/mock";
9
+ // Common tokens and addresses
10
+ const CHAIN_ID = ARBITRUM;
11
+ const ACCOUNT = "0x1234567890123456789012345678901234567890";
12
+ const WETH = getWrappedToken(CHAIN_ID);
13
+ const USDC = getTokenBySymbol(CHAIN_ID, "USDC");
14
+ const USDT = getTokenBySymbol(CHAIN_ID, "USDT");
15
+ // Common MarketIncrease params
16
+ const commonMarketIncreaseParams = {
17
+ chainId: CHAIN_ID,
18
+ receiver: ACCOUNT,
19
+ executionGasLimit: 0n,
20
+ payTokenAddress: WETH.address,
21
+ payTokenAmount: parseValue("1", WETH.decimals),
22
+ marketAddress: "0x1111111111111111111111111111111111111111",
23
+ indexTokenAddress: WETH.address,
24
+ isLong: true,
25
+ sizeDeltaUsd: parseValue("1000", USD_DECIMALS),
26
+ sizeDeltaInTokens: parseValue("1", WETH.decimals),
27
+ acceptablePrice: parseValue("1200", USD_DECIMALS),
28
+ collateralTokenAddress: WETH.address,
29
+ collateralDeltaAmount: parseValue("1", WETH.decimals),
30
+ swapPath: [WETH.address],
31
+ externalSwapQuote: undefined,
32
+ minOutputAmount: 0n,
33
+ triggerPrice: undefined,
34
+ referralCode: zeroHash,
35
+ autoCancel: false,
36
+ allowedSlippage: 100,
37
+ executionFeeAmount: parseValue("0.1", WETH.decimals),
38
+ validFromTime: 0n,
39
+ orderType: OrderType.MarketIncrease,
40
+ uiFeeReceiver: zeroAddress,
41
+ };
42
+ // Helper to build a batch with multiple increase orders
43
+ function buildMultiIncreaseBatch(paramsList) {
44
+ return {
45
+ createOrderParams: paramsList.map((p) => buildIncreaseOrderPayload(p)),
46
+ updateOrderParams: [],
47
+ cancelOrderParams: [],
48
+ };
49
+ }
50
+ describe("Batch Utils", () => {
51
+ describe("getIsEmptyBatch", () => {
52
+ it("undefined batch", () => {
53
+ expect(getIsEmptyBatch(undefined)).toBe(true);
54
+ });
55
+ it("zero actions batch", () => {
56
+ expect(getIsEmptyBatch({ createOrderParams: [], updateOrderParams: [], cancelOrderParams: [] })).toBe(true);
57
+ });
58
+ it("multiple orders batch with one empty order", () => {
59
+ const emptyOrder = buildIncreaseOrderPayload({
60
+ ...commonMarketIncreaseParams,
61
+ payTokenAmount: 0n,
62
+ sizeDeltaUsd: 0n,
63
+ sizeDeltaInTokens: 0n,
64
+ collateralDeltaAmount: 0n,
65
+ });
66
+ const nonEmptyOrder = buildIncreaseOrderPayload(commonMarketIncreaseParams);
67
+ const batch = {
68
+ createOrderParams: [emptyOrder, nonEmptyOrder],
69
+ updateOrderParams: [],
70
+ cancelOrderParams: [],
71
+ };
72
+ expect(getIsEmptyBatch(batch)).toBe(true);
73
+ });
74
+ it("multiple non-empty orders batch", () => {
75
+ const batch = buildMultiIncreaseBatch([commonMarketIncreaseParams, commonMarketIncreaseParams]);
76
+ expect(getIsEmptyBatch(batch)).toBe(false);
77
+ });
78
+ });
79
+ describe("getBatchIsNativePayment", () => {
80
+ it("returns true if any order has native payment", () => {
81
+ const nativeParams = { ...commonMarketIncreaseParams, payTokenAddress: NATIVE_TOKEN_ADDRESS };
82
+ const batch = buildMultiIncreaseBatch([nativeParams]);
83
+ expect(getBatchIsNativePayment(batch)).toBe(true);
84
+ });
85
+ it("returns false if no orders have native payment", () => {
86
+ const batch = buildMultiIncreaseBatch([commonMarketIncreaseParams]);
87
+ expect(getBatchIsNativePayment(batch)).toBe(false);
88
+ });
89
+ });
90
+ describe("getIsInvalidBatchReceiver", () => {
91
+ it("returns true if orders have different receivers", () => {
92
+ const params1 = { ...commonMarketIncreaseParams, receiver: ACCOUNT };
93
+ const params2 = {
94
+ ...commonMarketIncreaseParams,
95
+ receiver: "0x9999999999999999999999999999999999999999",
96
+ };
97
+ const batch = buildMultiIncreaseBatch([params1, params2]);
98
+ expect(getIsInvalidBatchReceiver(batch, ACCOUNT)).toBe(true);
99
+ });
100
+ it("returns false if all orders have the same receiver", () => {
101
+ const batch = buildMultiIncreaseBatch([commonMarketIncreaseParams, commonMarketIncreaseParams]);
102
+ expect(getIsInvalidBatchReceiver(batch, ACCOUNT)).toBe(false);
103
+ });
104
+ });
105
+ describe("getBatchExternalCalls", () => {
106
+ it("combines external calls from multiple orders", () => {
107
+ const params1 = {
108
+ ...commonMarketIncreaseParams,
109
+ payTokenAddress: WETH.address,
110
+ payTokenAmount: parseValue("1", WETH.decimals),
111
+ externalSwapQuote: mockExternalSwap({
112
+ inToken: WETH,
113
+ outToken: USDC,
114
+ to: "0x6352a56caadC4F1E25CD6c75970Fa768A3304e64",
115
+ data: "0x1",
116
+ amountIn: parseValue("1", WETH.decimals),
117
+ amountOut: parseValue("1000", USDC.decimals),
118
+ priceIn: expandDecimals(1000, USD_DECIMALS),
119
+ priceOut: expandDecimals(1, USD_DECIMALS),
120
+ }),
121
+ };
122
+ const params2 = {
123
+ ...commonMarketIncreaseParams,
124
+ payTokenAddress: USDC.address,
125
+ payTokenAmount: parseValue("1000", USDC.decimals),
126
+ externalSwapQuote: mockExternalSwap({
127
+ inToken: USDC,
128
+ outToken: WETH,
129
+ to: "0x6352a56caadC4F1E25CD6c75970Fa768A3304e64",
130
+ data: "0x2",
131
+ amountIn: parseValue("1000", USDC.decimals),
132
+ amountOut: parseValue("0.5", WETH.decimals),
133
+ priceIn: expandDecimals(1, USD_DECIMALS),
134
+ priceOut: expandDecimals(2000, USD_DECIMALS),
135
+ }),
136
+ };
137
+ const batch = buildMultiIncreaseBatch([params1, params2]);
138
+ const result = getBatchExternalCalls(batch);
139
+ expect(result).toEqual({
140
+ sendTokens: [WETH.address, USDC.address],
141
+ sendAmounts: [parseValue("1", WETH.decimals), parseValue("1000", USDC.decimals)],
142
+ externalCallTargets: [
143
+ "0x6352a56caadC4F1E25CD6c75970Fa768A3304e64",
144
+ "0x6352a56caadC4F1E25CD6c75970Fa768A3304e64",
145
+ ],
146
+ externalCallDataList: ["0x1", "0x2"],
147
+ refundTokens: [WETH.address, USDC.address],
148
+ refundReceivers: [ACCOUNT, ACCOUNT],
149
+ });
150
+ });
151
+ });
152
+ describe("getBatchTotalPayCollateralAmount", () => {
153
+ it("sums pay amounts across orders", () => {
154
+ const params1 = {
155
+ ...commonMarketIncreaseParams,
156
+ externalSwapQuote: undefined,
157
+ payTokenAmount: parseValue("1", WETH.decimals),
158
+ };
159
+ const params2 = {
160
+ ...commonMarketIncreaseParams,
161
+ externalSwapQuote: undefined,
162
+ payTokenAmount: parseValue("2", WETH.decimals),
163
+ };
164
+ const params3 = {
165
+ ...commonMarketIncreaseParams,
166
+ externalSwapQuote: undefined,
167
+ payTokenAddress: USDC.address,
168
+ payTokenAmount: parseValue("3", USDC.decimals),
169
+ };
170
+ const batch = buildMultiIncreaseBatch([params1, params2, params3]);
171
+ const result = getBatchTotalPayCollateralAmount(batch);
172
+ expect(result).toEqual({
173
+ [WETH.address]: parseValue("3", WETH.decimals),
174
+ [USDC.address]: parseValue("3", USDC.decimals),
175
+ });
176
+ });
177
+ });
178
+ describe("getBatchTotalExecutionFee", () => {
179
+ it("calculates total execution fee including top-ups", () => {
180
+ const params5 = {
181
+ ...commonMarketIncreaseParams,
182
+ externalSwapQuote: undefined,
183
+ executionFeeAmount: parseValue("0.1", WETH.decimals),
184
+ };
185
+ const batch = buildMultiIncreaseBatch([params5]);
186
+ batch.updateOrderParams = [{ updatePayload: { executionFeeTopUp: parseValue("0.05", WETH.decimals) } }];
187
+ const tokensData = {
188
+ [WETH.address]: {
189
+ ...WETH,
190
+ prices: {
191
+ minPrice: expandDecimals(2000, USD_DECIMALS),
192
+ maxPrice: expandDecimals(2000, USD_DECIMALS),
193
+ },
194
+ },
195
+ };
196
+ const result = getBatchTotalExecutionFee({ batchParams: batch, tokensData, chainId: CHAIN_ID });
197
+ expect(result).toEqual({
198
+ feeTokenAmount: parseValue("0.15", WETH.decimals),
199
+ gasLimit: 0n,
200
+ feeUsd: expandDecimals(300, USD_DECIMALS),
201
+ feeToken: tokensData[WETH.address],
202
+ isFeeHigh: true,
203
+ isFeeVeryHigh: true,
204
+ });
205
+ });
206
+ });
207
+ describe("getBatchExternalSwapGasLimit", () => {
208
+ it("sums gas limits from external swap quotes", () => {
209
+ const quote1 = mockExternalSwap({
210
+ inToken: WETH,
211
+ outToken: USDC,
212
+ amountIn: parseValue("1", WETH.decimals),
213
+ amountOut: parseValue("1000", USDC.decimals),
214
+ priceIn: expandDecimals(1000, USD_DECIMALS),
215
+ priceOut: expandDecimals(1, USD_DECIMALS),
216
+ });
217
+ const quote2 = mockExternalSwap({
218
+ inToken: USDC,
219
+ outToken: USDT,
220
+ amountIn: parseValue("1000", USDC.decimals),
221
+ amountOut: parseValue("1000", USDT.decimals),
222
+ priceIn: expandDecimals(1, USD_DECIMALS),
223
+ priceOut: expandDecimals(1, USD_DECIMALS),
224
+ });
225
+ const params6 = {
226
+ ...commonMarketIncreaseParams,
227
+ externalSwapQuote: quote1,
228
+ };
229
+ const params7 = {
230
+ ...commonMarketIncreaseParams,
231
+ externalSwapQuote: quote2,
232
+ payTokenAddress: USDC.address,
233
+ };
234
+ const batch = buildMultiIncreaseBatch([params6, params7]);
235
+ const result = getBatchExternalSwapGasLimit(batch);
236
+ expect(result).toBe(quote1.txnData.estimatedGas + quote2.txnData.estimatedGas);
237
+ });
238
+ });
239
+ });