@gmx-io/sdk 0.2.0 → 1.0.0-alpha.1

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 (435) hide show
  1. package/README.md +28 -1
  2. package/build/cjs/scripts/prebuild/index.js +11 -0
  3. package/build/cjs/scripts/prebuild/prebuildKinkModelMarketRatesKeys.js +51 -0
  4. package/build/cjs/scripts/prebuild/prebuildMarketConfigKeys.js +25 -0
  5. package/build/cjs/scripts/prebuild/prebuildMarketValuesKeys.js +25 -0
  6. package/build/cjs/src/abis/CustomErrors.json +3254 -0
  7. package/build/cjs/src/abis/DataStore.json +1440 -0
  8. package/build/cjs/src/abis/ERC721.json +442 -0
  9. package/build/cjs/src/abis/EventEmitter.json +2065 -0
  10. package/build/cjs/src/abis/ExchangeRouter.json +1801 -0
  11. package/build/cjs/src/abis/GMT.json +522 -0
  12. package/build/cjs/src/abis/GlpManager.json +591 -0
  13. package/build/cjs/src/abis/GlvReader.json +1414 -0
  14. package/build/cjs/src/abis/GlvRouter.json +706 -0
  15. package/build/cjs/src/abis/GmxMigrator.json +657 -0
  16. package/build/cjs/src/abis/GovToken.json +1121 -0
  17. package/build/cjs/src/abis/MintableBaseToken.json +696 -0
  18. package/build/cjs/src/abis/Multicall.json +333 -0
  19. package/build/cjs/src/abis/OrderBook.json +1997 -0
  20. package/build/cjs/src/abis/OrderBookReader.json +111 -0
  21. package/build/cjs/src/abis/OrderExecutor.json +122 -0
  22. package/build/cjs/src/abis/PositionManager.json +1165 -0
  23. package/build/cjs/src/abis/PositionRouter.json +2030 -0
  24. package/build/cjs/src/abis/Reader.json +355 -0
  25. package/build/cjs/src/abis/ReaderV2.json +630 -0
  26. package/build/cjs/src/abis/ReferralStorage.json +507 -0
  27. package/build/cjs/src/abis/RewardReader.json +86 -0
  28. package/build/cjs/src/abis/RewardRouter.json +1024 -0
  29. package/build/cjs/src/abis/RewardTracker.json +918 -0
  30. package/build/cjs/src/abis/Router-v2.json +605 -0
  31. package/build/cjs/src/abis/Router.json +605 -0
  32. package/build/cjs/src/abis/SubaccountRouter.json +658 -0
  33. package/build/cjs/src/abis/SyntheticsReader.json +4706 -0
  34. package/build/cjs/src/abis/SyntheticsRouter.json +77 -0
  35. package/build/cjs/src/abis/Timelock.json +1737 -0
  36. package/build/cjs/src/abis/Token.json +345 -0
  37. package/build/cjs/src/abis/Treasury.json +411 -0
  38. package/build/cjs/src/abis/UniPool.json +89 -0
  39. package/build/cjs/src/abis/UniswapV2.json +1 -0
  40. package/build/cjs/src/abis/Vault.json +3220 -0
  41. package/build/cjs/src/abis/VaultReader.json +87 -0
  42. package/build/cjs/src/abis/VaultV2.json +3019 -0
  43. package/build/cjs/src/abis/VaultV2b.json +3220 -0
  44. package/build/cjs/src/abis/Vester.json +1031 -0
  45. package/build/cjs/src/abis/WETH.json +320 -0
  46. package/build/cjs/src/abis/YieldFarm.json +599 -0
  47. package/build/cjs/src/abis/YieldToken.json +560 -0
  48. package/build/cjs/src/abis/index.js +94 -0
  49. package/build/cjs/src/configs/__tests__/markets.spec.js +32 -0
  50. package/build/cjs/src/configs/__tests__/tokens.spec.js +40 -0
  51. package/build/cjs/src/configs/batch.js +42 -0
  52. package/build/cjs/src/configs/chains.js +98 -0
  53. package/build/cjs/src/configs/contracts.js +248 -0
  54. package/build/cjs/src/configs/dataStore.js +348 -0
  55. package/build/cjs/src/configs/factors.js +14 -0
  56. package/build/cjs/src/configs/markets.js +840 -0
  57. package/build/cjs/src/configs/oracleKeeper.js +45 -0
  58. package/build/cjs/src/configs/tokens.js +1635 -0
  59. package/build/cjs/src/index.js +75 -0
  60. package/build/cjs/src/modules/accounts/accounts.js +39 -0
  61. package/build/cjs/src/modules/accounts/accounts.spec.js +10 -0
  62. package/build/cjs/src/modules/base.js +19 -0
  63. package/build/cjs/src/modules/markets/index.js +334 -0
  64. package/build/cjs/src/modules/markets/markets.spec.js +40 -0
  65. package/build/cjs/src/modules/markets/query-builders.js +385 -0
  66. package/build/cjs/src/modules/markets/types.js +2 -0
  67. package/build/cjs/src/modules/oracle.js +45 -0
  68. package/build/cjs/src/modules/orders/helpers.js +225 -0
  69. package/build/cjs/src/modules/orders/helpers.spec.js +131 -0
  70. package/build/cjs/src/modules/orders/orders.js +293 -0
  71. package/build/cjs/src/modules/orders/orders.spec.js +19 -0
  72. package/build/cjs/src/modules/orders/transactions/cancelOrdersTxn.js +20 -0
  73. package/build/cjs/src/modules/orders/transactions/createDecreaseOrderTxn.js +104 -0
  74. package/build/cjs/src/modules/orders/transactions/createIncreaseOrderTxn.js +174 -0
  75. package/build/cjs/src/modules/orders/transactions/createSwapOrderTxn.js +84 -0
  76. package/build/cjs/src/modules/orders/transactions/createWrapOrUnwrapTxn.js +17 -0
  77. package/build/cjs/src/modules/orders/transactions/updateOrderTxn.js +51 -0
  78. package/build/cjs/src/modules/orders/utils.js +157 -0
  79. package/build/cjs/src/modules/positions/positions.js +501 -0
  80. package/build/cjs/src/modules/positions/positions.spec.js +28 -0
  81. package/build/cjs/src/modules/tokens/tokens.js +135 -0
  82. package/build/cjs/src/modules/tokens/tokens.spec.js +29 -0
  83. package/build/cjs/src/modules/trades/trades.js +395 -0
  84. package/build/cjs/src/modules/trades/trades.spec.js +17 -0
  85. package/build/cjs/src/modules/utils/utils.js +211 -0
  86. package/build/cjs/src/package.json +1 -0
  87. package/build/cjs/src/prebuilt/hashedKinkModelMarketRatesKeys.json +944 -0
  88. package/build/cjs/src/prebuilt/hashedMarketConfigKeys.json +5858 -0
  89. package/build/cjs/src/prebuilt/hashedMarketValuesKeys.json +1763 -0
  90. package/build/cjs/src/prebuilt/index.js +15 -0
  91. package/build/cjs/src/test/mock.js +246 -0
  92. package/build/cjs/src/types/common.js +2 -0
  93. package/build/cjs/src/types/fees.js +2 -0
  94. package/build/cjs/src/types/markets.js +2 -0
  95. package/build/cjs/src/types/orders.js +38 -0
  96. package/build/cjs/src/types/positions.js +2 -0
  97. package/build/cjs/src/types/referrals.js +12 -0
  98. package/build/cjs/src/types/sdk.js +2 -0
  99. package/build/cjs/src/types/sidecarOrders.js +2 -0
  100. package/build/cjs/src/types/tokens.js +2 -0
  101. package/build/cjs/src/types/trade.js +25 -0
  102. package/build/cjs/src/types/tradeHistory.js +11 -0
  103. package/build/cjs/src/utils/LruCache.js +49 -0
  104. package/build/cjs/src/utils/__tests__/LruCache.spec.js +45 -0
  105. package/build/cjs/src/utils/__tests__/bigmath.spec.js +40 -0
  106. package/build/cjs/src/utils/__tests__/fees.spec.js +64 -0
  107. package/build/cjs/src/utils/__tests__/hash.spec.js +43 -0
  108. package/build/cjs/src/utils/__tests__/markets.spec.js +267 -0
  109. package/build/cjs/src/utils/__tests__/numbers.spec.js +83 -0
  110. package/build/cjs/src/utils/__tests__/objects.spec.js +139 -0
  111. package/build/cjs/src/utils/__tests__/positions.spec.js +180 -0
  112. package/build/cjs/src/utils/__tests__/prices.spec.js +69 -0
  113. package/build/cjs/src/utils/__tests__/referrals.spec.js +20 -0
  114. package/build/cjs/src/utils/__tests__/subgraph.spec.js +165 -0
  115. package/build/cjs/src/utils/__tests__/tokens.spec.js +115 -0
  116. package/build/cjs/src/utils/__tests__/trade.spec.js +38 -0
  117. package/build/cjs/src/utils/bigmath.js +34 -0
  118. package/build/cjs/src/utils/buildUrl.js +12 -0
  119. package/build/cjs/src/utils/callContract.js +143 -0
  120. package/build/cjs/src/utils/common.js +14 -0
  121. package/build/cjs/src/utils/contracts.js +93 -0
  122. package/build/cjs/src/utils/fees/estimateOraclePriceCount.js +28 -0
  123. package/build/cjs/src/utils/fees/executionFee.js +113 -0
  124. package/build/cjs/src/utils/fees/executionFee.spec.js +66 -0
  125. package/build/cjs/src/utils/fees/getNaiveEstimatedGasBySwapCount.js +8 -0
  126. package/build/cjs/src/utils/fees/index.js +128 -0
  127. package/build/cjs/src/utils/fees/priceImpact.js +300 -0
  128. package/build/cjs/src/utils/graphqlFetcher.js +24 -0
  129. package/build/cjs/src/utils/hash.js +41 -0
  130. package/build/cjs/src/utils/marketKeysAndConfigs.js +262 -0
  131. package/build/cjs/src/utils/markets.js +169 -0
  132. package/build/cjs/src/utils/multicall.js +125 -0
  133. package/build/cjs/src/utils/numbers.js +73 -0
  134. package/build/cjs/src/utils/objects.js +47 -0
  135. package/build/cjs/src/utils/orders.js +210 -0
  136. package/build/cjs/src/utils/positions.js +145 -0
  137. package/build/cjs/src/utils/prices.js +141 -0
  138. package/build/cjs/src/utils/referrals.js +34 -0
  139. package/build/cjs/src/utils/simulateExecuteOrder.js +143 -0
  140. package/build/cjs/src/utils/subgraph.js +76 -0
  141. package/build/cjs/src/utils/swap/__tests__/buildMarketsAdjacencyGraph.spec.js +118 -0
  142. package/build/cjs/src/utils/swap/__tests__/findReachableTokens.spec.js +118 -0
  143. package/build/cjs/src/utils/swap/__tests__/findSwapPathsBetweenTokens.spec.js +150 -0
  144. package/build/cjs/src/utils/swap/__tests__/mockRouting.spec.js +429 -0
  145. package/build/cjs/src/utils/swap/__tests__/swapRouting.spec.js +617 -0
  146. package/build/cjs/src/utils/swap/__tests__/swapStats.spec.js +388 -0
  147. package/build/cjs/src/utils/swap/buildMarketsAdjacencyGraph.js +25 -0
  148. package/build/cjs/src/utils/swap/constants.js +5 -0
  149. package/build/cjs/src/utils/swap/findReachableTokens.js +37 -0
  150. package/build/cjs/src/utils/swap/findSwapPathsBetweenTokens.js +75 -0
  151. package/build/cjs/src/utils/swap/index.js +18 -0
  152. package/build/cjs/src/utils/swap/preparedSwapData.js +28 -0
  153. package/build/cjs/src/utils/swap/swapPath.js +132 -0
  154. package/build/cjs/src/utils/swap/swapRouting.js +359 -0
  155. package/build/cjs/src/utils/swap/swapStats.js +258 -0
  156. package/build/cjs/src/utils/swap/swapValues.js +190 -0
  157. package/build/cjs/src/utils/testUtil.js +23 -0
  158. package/build/cjs/src/utils/tokens.js +144 -0
  159. package/build/cjs/src/utils/trade/amounts.js +317 -0
  160. package/build/cjs/src/utils/trade/index.js +17 -0
  161. package/build/cjs/src/utils/trade/trade.js +61 -0
  162. package/build/cjs/tsconfig.cjs.tsbuildinfo +1 -0
  163. package/build/esm/src/abis/CustomErrors.json +3254 -0
  164. package/build/esm/src/abis/DataStore.json +1440 -0
  165. package/build/esm/src/abis/ERC721.json +442 -0
  166. package/build/esm/src/abis/EventEmitter.json +2065 -0
  167. package/build/esm/src/abis/ExchangeRouter.json +1801 -0
  168. package/build/esm/src/abis/GMT.json +522 -0
  169. package/build/esm/src/abis/GlpManager.json +591 -0
  170. package/build/esm/src/abis/GlvReader.json +1414 -0
  171. package/build/esm/src/abis/GlvRouter.json +706 -0
  172. package/build/esm/src/abis/GmxMigrator.json +657 -0
  173. package/build/esm/src/abis/GovToken.json +1121 -0
  174. package/build/esm/src/abis/MintableBaseToken.json +696 -0
  175. package/build/esm/src/abis/Multicall.json +333 -0
  176. package/build/esm/src/abis/OrderBook.json +1997 -0
  177. package/build/esm/src/abis/OrderBookReader.json +111 -0
  178. package/build/esm/src/abis/OrderExecutor.json +122 -0
  179. package/build/esm/src/abis/PositionManager.json +1165 -0
  180. package/build/esm/src/abis/PositionRouter.json +2030 -0
  181. package/build/esm/src/abis/Reader.json +355 -0
  182. package/build/esm/src/abis/ReaderV2.json +630 -0
  183. package/build/esm/src/abis/ReferralStorage.json +507 -0
  184. package/build/esm/src/abis/RewardReader.json +86 -0
  185. package/build/esm/src/abis/RewardRouter.json +1024 -0
  186. package/build/esm/src/abis/RewardTracker.json +918 -0
  187. package/build/esm/src/abis/Router-v2.json +605 -0
  188. package/build/esm/src/abis/Router.json +605 -0
  189. package/build/esm/src/abis/SubaccountRouter.json +658 -0
  190. package/build/esm/src/abis/SyntheticsReader.json +4706 -0
  191. package/build/esm/src/abis/SyntheticsRouter.json +77 -0
  192. package/build/esm/src/abis/Timelock.json +1737 -0
  193. package/build/esm/src/abis/Token.json +345 -0
  194. package/build/esm/src/abis/Treasury.json +411 -0
  195. package/build/esm/src/abis/UniPool.json +89 -0
  196. package/build/esm/src/abis/UniswapV2.json +1 -0
  197. package/build/esm/src/abis/Vault.json +3220 -0
  198. package/build/esm/src/abis/VaultReader.json +87 -0
  199. package/build/esm/src/abis/VaultV2.json +3019 -0
  200. package/build/esm/src/abis/VaultV2b.json +3220 -0
  201. package/build/esm/src/abis/Vester.json +1031 -0
  202. package/build/esm/src/abis/WETH.json +320 -0
  203. package/build/esm/src/abis/YieldFarm.json +599 -0
  204. package/build/esm/src/abis/YieldToken.json +560 -0
  205. package/build/esm/src/abis/index.js +88 -0
  206. package/build/esm/src/configs/__tests__/markets.spec.js +30 -0
  207. package/build/esm/src/configs/__tests__/tokens.spec.js +38 -0
  208. package/build/{src → esm/src}/configs/chains.js +1 -1
  209. package/build/{src → esm/src}/configs/contracts.js +10 -4
  210. package/build/{src → esm/src}/configs/factors.js +3 -1
  211. package/build/{src → esm/src}/configs/markets.js +92 -0
  212. package/build/esm/src/configs/oracleKeeper.js +36 -0
  213. package/build/{src → esm/src}/configs/tokens.js +154 -13
  214. package/build/{src → esm/src}/modules/accounts/accounts.js +1 -2
  215. package/build/{src → esm/src}/modules/markets/index.js +1 -2
  216. package/build/{src → esm/src}/modules/markets/query-builders.js +8 -10
  217. package/build/esm/src/modules/orders/helpers.js +220 -0
  218. package/build/esm/src/modules/orders/helpers.spec.js +106 -0
  219. package/build/{src → esm/src}/modules/orders/orders.js +20 -2
  220. package/build/{src → esm/src}/modules/orders/transactions/cancelOrdersTxn.js +3 -3
  221. package/build/{src → esm/src}/modules/orders/transactions/createDecreaseOrderTxn.js +5 -5
  222. package/build/{src → esm/src}/modules/orders/transactions/createIncreaseOrderTxn.js +8 -7
  223. package/build/{src → esm/src}/modules/orders/transactions/createSwapOrderTxn.js +5 -5
  224. package/build/{src → esm/src}/modules/orders/transactions/createWrapOrUnwrapTxn.js +3 -3
  225. package/build/{src → esm/src}/modules/orders/transactions/updateOrderTxn.js +4 -4
  226. package/build/{src → esm/src}/modules/orders/utils.js +4 -6
  227. package/build/{src → esm/src}/modules/positions/positions.js +11 -14
  228. package/build/{src → esm/src}/modules/tokens/tokens.js +4 -10
  229. package/build/{src → esm/src}/modules/trades/trades.js +3 -3
  230. package/build/{src → esm/src}/modules/utils/utils.js +27 -3
  231. package/build/esm/src/package.json +1 -0
  232. package/build/esm/src/prebuilt/hashedKinkModelMarketRatesKeys.json +944 -0
  233. package/build/esm/src/prebuilt/hashedMarketConfigKeys.json +5858 -0
  234. package/build/esm/src/prebuilt/hashedMarketValuesKeys.json +1763 -0
  235. package/build/{src → esm/src}/prebuilt/index.js +3 -7
  236. package/build/esm/src/test/mock.js +238 -0
  237. package/build/{src → esm/src}/types/orders.js +2 -0
  238. package/build/{src → esm/src}/types/trade.js +5 -0
  239. package/build/{src → esm/src}/utils/__tests__/markets.spec.js +4 -4
  240. package/build/esm/src/utils/__tests__/objects.spec.js +137 -0
  241. package/build/{src → esm/src}/utils/__tests__/prices.spec.js +16 -8
  242. package/build/{src → esm/src}/utils/__tests__/tokens.spec.js +33 -3
  243. package/build/{src → esm/src}/utils/bigmath.js +3 -0
  244. package/build/{src → esm/src}/utils/fees/executionFee.js +3 -1
  245. package/build/esm/src/utils/fees/getNaiveEstimatedGasBySwapCount.js +4 -0
  246. package/build/{src → esm/src}/utils/markets.js +0 -3
  247. package/build/{src → esm/src}/utils/multicall.js +5 -3
  248. package/build/{src → esm/src}/utils/numbers.js +5 -1
  249. package/build/esm/src/utils/objects.js +36 -0
  250. package/build/{src → esm/src}/utils/orders.js +42 -13
  251. package/build/esm/src/utils/prices.js +132 -0
  252. package/build/{src → esm/src}/utils/simulateExecuteOrder.js +14 -10
  253. package/build/esm/src/utils/swap/__tests__/buildMarketsAdjacencyGraph.spec.js +116 -0
  254. package/build/esm/src/utils/swap/__tests__/findReachableTokens.spec.js +116 -0
  255. package/build/esm/src/utils/swap/__tests__/findSwapPathsBetweenTokens.spec.js +148 -0
  256. package/build/esm/src/utils/swap/__tests__/mockRouting.spec.js +427 -0
  257. package/build/esm/src/utils/swap/__tests__/swapRouting.spec.js +615 -0
  258. package/build/esm/src/utils/swap/__tests__/swapStats.spec.js +386 -0
  259. package/build/esm/src/utils/swap/buildMarketsAdjacencyGraph.js +21 -0
  260. package/build/esm/src/utils/swap/constants.js +2 -0
  261. package/build/esm/src/utils/swap/findReachableTokens.js +33 -0
  262. package/build/esm/src/utils/swap/findSwapPathsBetweenTokens.js +71 -0
  263. package/build/esm/src/utils/swap/index.js +2 -0
  264. package/build/esm/src/utils/swap/preparedSwapData.js +23 -0
  265. package/build/esm/src/utils/swap/swapPath.js +127 -0
  266. package/build/esm/src/utils/swap/swapRouting.js +341 -0
  267. package/build/{src/utils → esm/src/utils/swap}/swapStats.js +49 -7
  268. package/build/esm/src/utils/swap/swapValues.js +184 -0
  269. package/build/esm/src/utils/tokens.js +127 -0
  270. package/build/esm/src/utils/trade/amounts.js +310 -0
  271. package/build/esm/src/utils/trade/index.js +1 -0
  272. package/build/{src/utils → esm/src/utils/trade}/trade.js +26 -4
  273. package/build/esm/tsconfig.esm.tsbuildinfo +1 -0
  274. package/build/src/abis/index.d.ts +4 -0
  275. package/build/src/configs/__tests__/markets.spec.d.ts +1 -0
  276. package/build/src/configs/__tests__/tokens.spec.d.ts +1 -0
  277. package/build/src/configs/chains.d.ts +7 -22
  278. package/build/src/configs/contracts.d.ts +1 -220
  279. package/build/src/configs/factors.d.ts +3 -1
  280. package/build/src/configs/markets.d.ts +2 -1
  281. package/build/src/configs/oracleKeeper.d.ts +3 -0
  282. package/build/src/configs/tokens.d.ts +4 -4
  283. package/build/src/modules/markets/query-builders.d.ts +5 -4
  284. package/build/src/modules/oracle.d.ts +7 -3
  285. package/build/src/modules/orders/helpers.d.ts +50 -0
  286. package/build/src/modules/orders/helpers.spec.d.ts +1 -0
  287. package/build/src/modules/orders/orders.d.ts +6 -1
  288. package/build/src/modules/orders/transactions/createDecreaseOrderTxn.d.ts +1 -1
  289. package/build/src/modules/orders/transactions/createSwapOrderTxn.d.ts +1 -0
  290. package/build/src/modules/orders/transactions/updateOrderTxn.d.ts +2 -2
  291. package/build/src/modules/orders/utils.d.ts +5 -200
  292. package/build/src/modules/positions/positions.d.ts +1 -1
  293. package/build/src/modules/tokens/tokens.d.ts +2 -2
  294. package/build/src/modules/trades/trades.d.ts +2 -2
  295. package/build/src/modules/utils/utils.d.ts +4 -2
  296. package/build/src/prebuilt/index.d.ts +6 -2
  297. package/build/src/test/mock.d.ts +14 -0
  298. package/build/src/types/fees.d.ts +6 -0
  299. package/build/src/types/orders.d.ts +4 -3
  300. package/build/src/types/sdk.d.ts +4 -1
  301. package/build/src/types/tokens.d.ts +5 -0
  302. package/build/src/types/trade.d.ts +84 -9
  303. package/build/src/utils/bigmath.d.ts +1 -0
  304. package/build/src/utils/fees/executionFee.d.ts +1 -1
  305. package/build/src/utils/fees/getNaiveEstimatedGasBySwapCount.d.ts +2 -0
  306. package/build/src/utils/hash.d.ts +1 -1
  307. package/build/src/utils/multicall.d.ts +2 -1
  308. package/build/src/utils/numbers.d.ts +2 -0
  309. package/build/src/utils/objects.d.ts +6 -0
  310. package/build/src/utils/orders.d.ts +1 -0
  311. package/build/src/utils/positions.d.ts +1 -1
  312. package/build/src/utils/prices.d.ts +35 -1
  313. package/build/src/utils/swap/__tests__/buildMarketsAdjacencyGraph.spec.d.ts +1 -0
  314. package/build/src/utils/swap/__tests__/findReachableTokens.spec.d.ts +1 -0
  315. package/build/src/utils/swap/__tests__/findSwapPathsBetweenTokens.spec.d.ts +1 -0
  316. package/build/src/utils/swap/__tests__/mockRouting.spec.d.ts +1 -0
  317. package/build/src/utils/swap/__tests__/swapRouting.spec.d.ts +1 -0
  318. package/build/src/utils/swap/__tests__/swapStats.spec.d.ts +1 -0
  319. package/build/src/utils/swap/buildMarketsAdjacencyGraph.d.ts +11 -0
  320. package/build/src/utils/swap/constants.d.ts +2 -0
  321. package/build/src/utils/swap/findReachableTokens.d.ts +2 -0
  322. package/build/src/utils/swap/findSwapPathsBetweenTokens.d.ts +3 -0
  323. package/build/src/utils/swap/index.d.ts +2 -0
  324. package/build/src/utils/swap/preparedSwapData.d.ts +14 -0
  325. package/build/src/utils/swap/swapPath.d.ts +23 -0
  326. package/build/src/utils/swap/swapRouting.d.ts +67 -0
  327. package/build/src/utils/{swapStats.d.ts → swap/swapStats.d.ts} +7 -2
  328. package/build/src/utils/swap/swapValues.d.ts +26 -0
  329. package/build/src/utils/testUtil.d.ts +2 -14560
  330. package/build/src/utils/tokens.d.ts +25 -1
  331. package/build/src/utils/trade/amounts.d.ts +70 -0
  332. package/build/src/utils/trade/index.d.ts +1 -0
  333. package/build/src/utils/{trade.d.ts → trade/trade.d.ts} +2 -1
  334. package/package.json +43 -12
  335. package/build/src/abis/CustomErrors.json +0 -3254
  336. package/build/src/abis/DataStore.json +0 -1440
  337. package/build/src/abis/ERC721.json +0 -442
  338. package/build/src/abis/EventEmitter.json +0 -2065
  339. package/build/src/abis/ExchangeRouter.json +0 -1801
  340. package/build/src/abis/GMT.json +0 -522
  341. package/build/src/abis/GlpManager.json +0 -592
  342. package/build/src/abis/GlvReader.json +0 -1415
  343. package/build/src/abis/GlvRouter.json +0 -707
  344. package/build/src/abis/GmxMigrator.json +0 -657
  345. package/build/src/abis/GovToken.json +0 -1121
  346. package/build/src/abis/MintableBaseToken.json +0 -696
  347. package/build/src/abis/Multicall.json +0 -333
  348. package/build/src/abis/OrderBook.json +0 -1997
  349. package/build/src/abis/OrderBookReader.json +0 -111
  350. package/build/src/abis/OrderExecutor.json +0 -122
  351. package/build/src/abis/PositionManager.json +0 -1165
  352. package/build/src/abis/PositionRouter.json +0 -2030
  353. package/build/src/abis/Reader.json +0 -355
  354. package/build/src/abis/ReaderV2.json +0 -630
  355. package/build/src/abis/ReferralStorage.json +0 -507
  356. package/build/src/abis/RewardReader.json +0 -86
  357. package/build/src/abis/RewardRouter.json +0 -1024
  358. package/build/src/abis/RewardTracker.json +0 -918
  359. package/build/src/abis/Router-v2.json +0 -605
  360. package/build/src/abis/Router.json +0 -605
  361. package/build/src/abis/SubaccountRouter.json +0 -658
  362. package/build/src/abis/SyntheticsReader.json +0 -4709
  363. package/build/src/abis/SyntheticsRouter.json +0 -77
  364. package/build/src/abis/Timelock.json +0 -1737
  365. package/build/src/abis/Token.json +0 -345
  366. package/build/src/abis/Treasury.json +0 -411
  367. package/build/src/abis/UniPool.json +0 -89
  368. package/build/src/abis/UniswapV2.json +0 -1
  369. package/build/src/abis/Vault.json +0 -3220
  370. package/build/src/abis/VaultReader.json +0 -87
  371. package/build/src/abis/VaultV2.json +0 -3019
  372. package/build/src/abis/VaultV2b.json +0 -3220
  373. package/build/src/abis/Vester.json +0 -1031
  374. package/build/src/abis/WETH.json +0 -320
  375. package/build/src/abis/YieldFarm.json +0 -599
  376. package/build/src/abis/YieldToken.json +0 -560
  377. package/build/src/prebuilt/hashedKinkModelMarketRatesKeys.json +0 -840
  378. package/build/src/prebuilt/hashedMarketConfigKeys.json +0 -5208
  379. package/build/src/prebuilt/hashedMarketValuesKeys.json +0 -1568
  380. package/build/src/prebuilt/index.ts +0 -45
  381. package/build/src/utils/__tests__/objects.spec.js +0 -59
  382. package/build/src/utils/objects.js +0 -13
  383. package/build/src/utils/prices.js +0 -25
  384. package/build/src/utils/tokens.js +0 -68
  385. package/build/tsconfig.tsbuildinfo +0 -1
  386. package/build/{scripts → esm/scripts}/prebuild/index.js +2 -2
  387. package/build/{scripts → esm/scripts}/prebuild/prebuildKinkModelMarketRatesKeys.js +1 -1
  388. package/build/{scripts → esm/scripts}/prebuild/prebuildMarketConfigKeys.js +1 -1
  389. package/build/{scripts → esm/scripts}/prebuild/prebuildMarketValuesKeys.js +0 -0
  390. package/build/{src → esm/src}/configs/batch.js +0 -0
  391. package/build/{src → esm/src}/configs/dataStore.js +0 -0
  392. package/build/{src → esm/src}/index.js +2 -2
  393. package/build/{src → esm/src}/modules/accounts/accounts.spec.js +0 -0
  394. package/build/{src → esm/src}/modules/base.js +0 -0
  395. package/build/{src → esm/src}/modules/markets/markets.spec.js +2 -2
  396. package/build/{src → esm/src}/modules/markets/types.js +0 -0
  397. package/build/{src → esm/src}/modules/oracle.js +0 -0
  398. package/build/{src → esm/src}/modules/orders/orders.spec.js +0 -0
  399. package/build/{src → esm/src}/modules/positions/positions.spec.js +1 -1
  400. package/build/{src → esm/src}/modules/tokens/tokens.spec.js +1 -1
  401. package/build/{src → esm/src}/modules/trades/trades.spec.js +0 -0
  402. package/build/{src → esm/src}/types/common.js +0 -0
  403. package/build/{src → esm/src}/types/fees.js +0 -0
  404. package/build/{src → esm/src}/types/markets.js +0 -0
  405. package/build/{src → esm/src}/types/positions.js +0 -0
  406. package/build/{src → esm/src}/types/referrals.js +0 -0
  407. package/build/{src → esm/src}/types/sdk.js +0 -0
  408. package/build/{src → esm/src}/types/sidecarOrders.js +0 -0
  409. package/build/{src → esm/src}/types/tokens.js +0 -0
  410. package/build/{src → esm/src}/types/tradeHistory.js +0 -0
  411. package/build/{src → esm/src}/utils/LruCache.js +0 -0
  412. package/build/{src → esm/src}/utils/__tests__/LruCache.spec.js +0 -0
  413. package/build/{src → esm/src}/utils/__tests__/bigmath.spec.js +1 -1
  414. package/build/{src → esm/src}/utils/__tests__/fees.spec.js +1 -1
  415. package/build/{src → esm/src}/utils/__tests__/hash.spec.js +1 -1
  416. package/build/{src → esm/src}/utils/__tests__/numbers.spec.js +0 -0
  417. package/build/{src → esm/src}/utils/__tests__/positions.spec.js +0 -0
  418. package/build/{src → esm/src}/utils/__tests__/referrals.spec.js +1 -1
  419. package/build/{src → esm/src}/utils/__tests__/subgraph.spec.js +0 -0
  420. package/build/{src → esm/src}/utils/__tests__/trade.spec.js +0 -0
  421. package/build/{src → esm/src}/utils/buildUrl.js +0 -0
  422. package/build/{src → esm/src}/utils/callContract.js +0 -0
  423. package/build/{src → esm/src}/utils/common.js +0 -0
  424. package/build/{src → esm/src}/utils/contracts.js +0 -0
  425. package/build/{src → esm/src}/utils/fees/estimateOraclePriceCount.js +0 -0
  426. package/build/{src → esm/src}/utils/fees/executionFee.spec.js +2 -2
  427. package/build/{src → esm/src}/utils/fees/index.js +0 -0
  428. package/build/{src → esm/src}/utils/fees/priceImpact.js +0 -0
  429. package/build/{src → esm/src}/utils/graphqlFetcher.js +0 -0
  430. package/build/{src → esm/src}/utils/hash.js +0 -0
  431. package/build/{src → esm/src}/utils/marketKeysAndConfigs.js +1 -1
  432. package/build/{src → esm/src}/utils/positions.js +3 -3
  433. /package/build/{src → esm/src}/utils/referrals.js +0 -0
  434. /package/build/{src → esm/src}/utils/subgraph.js +0 -0
  435. /package/build/{src → esm/src}/utils/testUtil.js +0 -0
@@ -0,0 +1,69 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const vitest_1 = require("vitest");
4
+ const orders_1 = require("../../types/orders");
5
+ const trade_1 = require("../../types/trade");
6
+ const prices_1 = require("../prices");
7
+ (0, vitest_1.describe)("getMarkPrice", () => {
8
+ (0, vitest_1.it)("returns maxPrice if getShouldUseMaxPrice => true", () => {
9
+ const prices = { minPrice: 1000n, maxPrice: 1500n };
10
+ // isIncrease=true, isLong=true => getShouldUseMaxPrice => true
11
+ const result = (0, prices_1.getMarkPrice)({ prices, isIncrease: true, isLong: true });
12
+ (0, vitest_1.expect)(result).toBe(1500n);
13
+ });
14
+ (0, vitest_1.it)("returns minPrice if getShouldUseMaxPrice => false", () => {
15
+ const prices = { minPrice: 1000n, maxPrice: 1500n };
16
+ // isIncrease=false, isLong=true => getShouldUseMaxPrice => false
17
+ const result = (0, prices_1.getMarkPrice)({ prices, isIncrease: false, isLong: true });
18
+ (0, vitest_1.expect)(result).toBe(1000n);
19
+ });
20
+ });
21
+ (0, vitest_1.describe)("getShouldUseMaxPrice", () => {
22
+ (0, vitest_1.it)("returns isLong if isIncrease=true", () => {
23
+ // isIncrease=true => return isLong
24
+ (0, vitest_1.expect)((0, prices_1.getShouldUseMaxPrice)(true, true)).toBe(true);
25
+ (0, vitest_1.expect)((0, prices_1.getShouldUseMaxPrice)(true, false)).toBe(false);
26
+ });
27
+ (0, vitest_1.it)("returns !isLong if isIncrease=false", () => {
28
+ // isIncrease=false => return !isLong
29
+ (0, vitest_1.expect)((0, prices_1.getShouldUseMaxPrice)(false, true)).toBe(false);
30
+ (0, vitest_1.expect)((0, prices_1.getShouldUseMaxPrice)(false, false)).toBe(true);
31
+ });
32
+ });
33
+ (0, vitest_1.describe)("getTriggerThresholdType", () => {
34
+ (0, vitest_1.it)("returns Below for LimitIncrease when isLong=true", () => {
35
+ const result = (0, prices_1.getOrderThresholdType)(orders_1.OrderType.LimitIncrease, true);
36
+ (0, vitest_1.expect)(result).toBe(trade_1.TriggerThresholdType.Below);
37
+ });
38
+ (0, vitest_1.it)("returns Above for LimitIncrease when isLong=false", () => {
39
+ const result = (0, prices_1.getOrderThresholdType)(orders_1.OrderType.LimitIncrease, false);
40
+ (0, vitest_1.expect)(result).toBe(trade_1.TriggerThresholdType.Above);
41
+ });
42
+ (0, vitest_1.it)("returns Above for LimitDecrease when isLong=true", () => {
43
+ const result = (0, prices_1.getOrderThresholdType)(orders_1.OrderType.LimitDecrease, true);
44
+ (0, vitest_1.expect)(result).toBe(trade_1.TriggerThresholdType.Above);
45
+ });
46
+ (0, vitest_1.it)("returns Below for LimitDecrease when isLong=false", () => {
47
+ const result = (0, prices_1.getOrderThresholdType)(orders_1.OrderType.LimitDecrease, false);
48
+ (0, vitest_1.expect)(result).toBe(trade_1.TriggerThresholdType.Below);
49
+ });
50
+ (0, vitest_1.it)("returns Below for StopLossDecrease when isLong=true", () => {
51
+ const result = (0, prices_1.getOrderThresholdType)(orders_1.OrderType.StopLossDecrease, true);
52
+ (0, vitest_1.expect)(result).toBe(trade_1.TriggerThresholdType.Below);
53
+ });
54
+ (0, vitest_1.it)("returns Above for StopLossDecrease when isLong=false", () => {
55
+ const result = (0, prices_1.getOrderThresholdType)(orders_1.OrderType.StopLossDecrease, false);
56
+ (0, vitest_1.expect)(result).toBe(trade_1.TriggerThresholdType.Above);
57
+ });
58
+ (0, vitest_1.it)("returns Above for StopMarketIncrease when isLong=true", () => {
59
+ const result = (0, prices_1.getOrderThresholdType)(orders_1.OrderType.StopIncrease, true);
60
+ (0, vitest_1.expect)(result).toBe(trade_1.TriggerThresholdType.Above);
61
+ });
62
+ (0, vitest_1.it)("returns Below for StopMarketIncrease when isLong=false", () => {
63
+ const result = (0, prices_1.getOrderThresholdType)(orders_1.OrderType.StopIncrease, false);
64
+ (0, vitest_1.expect)(result).toBe(trade_1.TriggerThresholdType.Below);
65
+ });
66
+ (0, vitest_1.it)("throws error for invalid order type", () => {
67
+ (0, vitest_1.expect)(() => (0, prices_1.getOrderThresholdType)("SomeInvalidType", true)).toThrow("Invalid trigger order type");
68
+ });
69
+ });
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const viem_1 = require("viem");
4
+ const vitest_1 = require("vitest");
5
+ const referrals_1 = require("../referrals");
6
+ (0, vitest_1.describe)("utils/referrals", () => {
7
+ (0, vitest_1.it)("decode(encode(x)) === x", () => {
8
+ const code = "test";
9
+ const encoded = (0, referrals_1.encodeReferralCode)(code);
10
+ const decoded = (0, referrals_1.decodeReferralCode)(encoded);
11
+ (0, vitest_1.expect)(decoded).toEqual(code);
12
+ });
13
+ (0, vitest_1.it)("decodeReferralCode defaults", () => {
14
+ (0, vitest_1.expect)((0, referrals_1.decodeReferralCode)()).toEqual("");
15
+ (0, vitest_1.expect)((0, referrals_1.decodeReferralCode)(viem_1.zeroHash)).toEqual("");
16
+ });
17
+ (0, vitest_1.it)("encodeReferralCode defaults", () => {
18
+ (0, vitest_1.expect)((0, referrals_1.encodeReferralCode)(new Array(referrals_1.MAX_REFERRAL_CODE_LENGTH + 1).fill("0").join(""))).toEqual(viem_1.zeroHash);
19
+ });
20
+ });
@@ -0,0 +1,165 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const vitest_1 = require("vitest");
4
+ const subgraph_1 = require("../subgraph");
5
+ (0, vitest_1.describe)("buildFiltersBody", () => {
6
+ (0, vitest_1.it)("should return empty object if no filters", () => {
7
+ const input = {};
8
+ const result = (0, subgraph_1.buildFiltersBody)(input);
9
+ (0, vitest_1.expect)(result).toEqual("{}");
10
+ });
11
+ (0, vitest_1.it)("should return correct filter string with single filter", () => {
12
+ const input = {
13
+ foo: "bar",
14
+ };
15
+ const result = (0, subgraph_1.buildFiltersBody)(input);
16
+ (0, vitest_1.expect)(result).toEqual('{foo:"bar"}');
17
+ });
18
+ (0, vitest_1.it)("should return correct filter string with multiple filters", () => {
19
+ const input = {
20
+ foo: "bar",
21
+ baz: "qux",
22
+ };
23
+ const result = (0, subgraph_1.buildFiltersBody)(input);
24
+ (0, vitest_1.expect)(result).toEqual('{foo:"bar",baz:"qux"}');
25
+ });
26
+ (0, vitest_1.it)("should return correct filter string with nested filter", () => {
27
+ const input = {
28
+ foo: {
29
+ bar: {
30
+ baz: "qux",
31
+ },
32
+ },
33
+ };
34
+ const result = (0, subgraph_1.buildFiltersBody)(input);
35
+ (0, vitest_1.expect)(result).toEqual('{foo_:{bar_:{baz:"qux"}}}');
36
+ });
37
+ (0, vitest_1.it)("should return correct filter string with or filter", () => {
38
+ const input = {
39
+ or: [
40
+ {
41
+ foo: "bar",
42
+ },
43
+ {
44
+ baz: "qux",
45
+ },
46
+ ],
47
+ };
48
+ const result = (0, subgraph_1.buildFiltersBody)(input);
49
+ (0, vitest_1.expect)(result).toEqual('{or:[{foo:"bar"},{baz:"qux"}]}');
50
+ });
51
+ (0, vitest_1.it)("should return correct filter string with and filter", () => {
52
+ const input = {
53
+ and: [
54
+ {
55
+ foo: "bar",
56
+ },
57
+ {
58
+ baz: "qux",
59
+ },
60
+ ],
61
+ };
62
+ const result = (0, subgraph_1.buildFiltersBody)(input);
63
+ (0, vitest_1.expect)(result).toEqual('{and:[{foo:"bar"},{baz:"qux"}]}');
64
+ });
65
+ (0, vitest_1.it)("should strip out undefined filters", () => {
66
+ const input = {
67
+ foo: "bar",
68
+ baz: undefined,
69
+ };
70
+ const result = (0, subgraph_1.buildFiltersBody)(input);
71
+ (0, vitest_1.expect)(result).toEqual('{foo:"bar"}');
72
+ });
73
+ (0, vitest_1.it)("should strip out empty or", () => {
74
+ const input = {
75
+ or: [],
76
+ };
77
+ const result = (0, subgraph_1.buildFiltersBody)(input);
78
+ (0, vitest_1.expect)(result).toEqual("{}");
79
+ });
80
+ (0, vitest_1.it)("should strip out or with empty", () => {
81
+ const input = {
82
+ or: [
83
+ {},
84
+ {
85
+ foo: undefined,
86
+ },
87
+ ],
88
+ };
89
+ const result = (0, subgraph_1.buildFiltersBody)(input);
90
+ (0, vitest_1.expect)(result).toEqual("{}");
91
+ });
92
+ (0, vitest_1.it)("should throw error if or is mixed with other filters", () => {
93
+ const input = {
94
+ or: [
95
+ {
96
+ foo: "bar",
97
+ },
98
+ ],
99
+ // @ts-expect-error
100
+ baz: "qux",
101
+ };
102
+ const getResult = () => (0, subgraph_1.buildFiltersBody)(input);
103
+ (0, vitest_1.expect)(getResult).toThrowError();
104
+ });
105
+ (0, vitest_1.it)("should throw error if and is mixed with other filters", () => {
106
+ const input = {
107
+ and: [
108
+ {
109
+ foo: "bar",
110
+ },
111
+ ],
112
+ // @ts-expect-error
113
+ baz: "qux",
114
+ };
115
+ const getResult = () => (0, subgraph_1.buildFiltersBody)(input);
116
+ (0, vitest_1.expect)(getResult).toThrowError();
117
+ });
118
+ (0, vitest_1.it)("should throw not error if empty or is mixed with other filters", () => {
119
+ const input = {
120
+ or: [],
121
+ // @ts-expect-error
122
+ baz: "qux",
123
+ };
124
+ const getResult = () => (0, subgraph_1.buildFiltersBody)(input);
125
+ (0, vitest_1.expect)(getResult).not.toThrowError();
126
+ });
127
+ (0, vitest_1.it)("should throw not error if empty and is mixed with other filters", () => {
128
+ const input = {
129
+ and: [],
130
+ // @ts-expect-error
131
+ baz: "qux",
132
+ };
133
+ const getResult = () => (0, subgraph_1.buildFiltersBody)(input);
134
+ (0, vitest_1.expect)(getResult).not.toThrowError();
135
+ });
136
+ (0, vitest_1.it)("should format string values correctly", () => {
137
+ const input = {
138
+ foo: "bar",
139
+ };
140
+ const result = (0, subgraph_1.buildFiltersBody)(input);
141
+ (0, vitest_1.expect)(result).toEqual('{foo:"bar"}');
142
+ });
143
+ (0, vitest_1.it)("should format number values correctly", () => {
144
+ const input = {
145
+ foo: 123,
146
+ };
147
+ const result = (0, subgraph_1.buildFiltersBody)(input);
148
+ (0, vitest_1.expect)(result).toEqual("{foo:123}");
149
+ });
150
+ (0, vitest_1.it)("should format boolean values correctly", () => {
151
+ const input = {
152
+ foo: true,
153
+ bar: false,
154
+ };
155
+ const result = (0, subgraph_1.buildFiltersBody)(input);
156
+ (0, vitest_1.expect)(result).toEqual("{foo:true,bar:false}");
157
+ });
158
+ (0, vitest_1.it)("should format null values correctly", () => {
159
+ const input = {
160
+ foo: null,
161
+ };
162
+ const result = (0, subgraph_1.buildFiltersBody)(input);
163
+ (0, vitest_1.expect)(result).toEqual("{foo:null}");
164
+ });
165
+ });
@@ -0,0 +1,115 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const vitest_1 = require("vitest");
4
+ const chains_1 = require("../../configs/chains");
5
+ const tokens_1 = require("../../configs/tokens");
6
+ const numbers_1 = require("../numbers");
7
+ const tokens_2 = require("../tokens");
8
+ function getToken(symbol) {
9
+ return tokens_1.TOKENS[chains_1.ARBITRUM].find((token) => token.symbol === symbol);
10
+ }
11
+ (0, vitest_1.describe)("parseContractPrice", () => {
12
+ (0, vitest_1.it)("multiplies price by 10^decimals", () => {
13
+ (0, vitest_1.expect)((0, tokens_2.parseContractPrice)(100n, 2)).toBe(100n * (0, numbers_1.expandDecimals)(1, 2));
14
+ });
15
+ });
16
+ (0, vitest_1.describe)("convertToContractPrice", () => {
17
+ (0, vitest_1.it)("divides price by 10^decimals", () => {
18
+ (0, vitest_1.expect)((0, tokens_2.convertToContractPrice)(10000n, 2)).toBe(10000n / (0, numbers_1.expandDecimals)(1, 2));
19
+ });
20
+ });
21
+ (0, vitest_1.describe)("convertToContractTokenPrices", () => {
22
+ (0, vitest_1.it)("returns min and max contract prices", () => {
23
+ const result = (0, tokens_2.convertToContractTokenPrices)({ minPrice: 1000n, maxPrice: 2000n }, 2);
24
+ (0, vitest_1.expect)(result.min).toBe(1000n / (0, numbers_1.expandDecimals)(1, 2));
25
+ (0, vitest_1.expect)(result.max).toBe(2000n / (0, numbers_1.expandDecimals)(1, 2));
26
+ });
27
+ });
28
+ (0, vitest_1.describe)("convertToTokenAmount", () => {
29
+ (0, vitest_1.it)("returns undefined if inputs are invalid", () => {
30
+ (0, vitest_1.expect)((0, tokens_2.convertToTokenAmount)(undefined, 18, 100n)).toBeUndefined();
31
+ (0, vitest_1.expect)((0, tokens_2.convertToTokenAmount)(1000n, undefined, 100n)).toBeUndefined();
32
+ (0, vitest_1.expect)((0, tokens_2.convertToTokenAmount)(1000n, 18, 0n)).toBeUndefined();
33
+ });
34
+ (0, vitest_1.it)("converts usd to token amount", () => {
35
+ (0, vitest_1.expect)((0, tokens_2.convertToTokenAmount)(1000n, 2, 100n)).toBe((1000n * (0, numbers_1.expandDecimals)(1, 2)) / 100n);
36
+ });
37
+ });
38
+ (0, vitest_1.describe)("convertToUsd", () => {
39
+ (0, vitest_1.it)("returns undefined if inputs are invalid", () => {
40
+ (0, vitest_1.expect)((0, tokens_2.convertToUsd)(undefined, 18, 100n)).toBeUndefined();
41
+ (0, vitest_1.expect)((0, tokens_2.convertToUsd)(1000n, undefined, 100n)).toBeUndefined();
42
+ });
43
+ (0, vitest_1.it)("converts token amount to usd", () => {
44
+ (0, vitest_1.expect)((0, tokens_2.convertToUsd)(1000n, 2, 100n)).toBe((1000n * 100n) / (0, numbers_1.expandDecimals)(1, 2));
45
+ });
46
+ });
47
+ (0, vitest_1.describe)("getMidPrice", () => {
48
+ (0, vitest_1.it)("returns the average of min and max price", () => {
49
+ (0, vitest_1.expect)((0, tokens_2.getMidPrice)({ minPrice: 10n, maxPrice: 20n })).toBe(15n);
50
+ });
51
+ });
52
+ (0, vitest_1.describe)("getIsEquivalentTokens", () => {
53
+ (0, vitest_1.it)("checks address, wrappedAddress, synthetic, and symbol", () => {
54
+ (0, vitest_1.expect)((0, tokens_2.getIsEquivalentTokens)(getToken("ETH"), getToken("WETH"))).toBe(true);
55
+ (0, vitest_1.expect)((0, tokens_2.getIsEquivalentTokens)(getToken("ETH"), getToken("ETH"))).toBe(true);
56
+ (0, vitest_1.expect)((0, tokens_2.getIsEquivalentTokens)({ address: "0xA", isSynthetic: true, symbol: "SYN" }, { address: "0xB", isSynthetic: true, symbol: "SYN" })).toBe(true);
57
+ (0, vitest_1.expect)((0, tokens_2.getIsEquivalentTokens)(getToken("ETH"), getToken("BTC"))).toBe(false);
58
+ });
59
+ });
60
+ (0, vitest_1.describe)("getTokenData", () => {
61
+ (0, vitest_1.it)("returns undefined if no token data", () => {
62
+ (0, vitest_1.expect)((0, tokens_2.getTokenData)()).toBeUndefined();
63
+ });
64
+ (0, vitest_1.it)("returns wrapped if convertTo=wrapped and token isNative", () => {
65
+ const tokensData = {
66
+ "0xnative": { address: "0xnative", isNative: true, wrappedAddress: "0xwrap" },
67
+ "0xwrap": { address: "0xwrap", isWrapped: true },
68
+ };
69
+ (0, vitest_1.expect)((0, tokens_2.getTokenData)(tokensData, "0xnative", "wrapped")).toEqual(tokensData["0xwrap"]);
70
+ });
71
+ });
72
+ (0, vitest_1.describe)("getTokensRatioByAmounts", () => {
73
+ (0, vitest_1.it)("returns ratio of two token amounts", () => {
74
+ const fromToken = { decimals: 2 };
75
+ const toToken = { decimals: 2 };
76
+ const result = (0, tokens_2.getTokensRatioByAmounts)({
77
+ fromToken,
78
+ toToken,
79
+ fromTokenAmount: 1000n,
80
+ toTokenAmount: 500n,
81
+ });
82
+ (0, vitest_1.expect)(result.largestToken).toEqual(fromToken);
83
+ (0, vitest_1.expect)(result.ratio).toBe((((1000n * numbers_1.PRECISION) / (0, numbers_1.expandDecimals)(1, 2)) * numbers_1.PRECISION) / ((500n * numbers_1.PRECISION) / (0, numbers_1.expandDecimals)(1, 2)));
84
+ });
85
+ });
86
+ (0, vitest_1.describe)("getTokensRatioByMinOutputAmountAndTriggerPrice", () => {
87
+ (0, vitest_1.it)("returns ratio of two token amounts in case if triggerPrice is 0n", () => {
88
+ const fromToken = { decimals: 2 };
89
+ const toToken = { decimals: 2 };
90
+ const result = (0, tokens_2.getTokensRatioByMinOutputAmountAndTriggerPrice)({
91
+ fromToken,
92
+ toToken,
93
+ fromTokenAmount: 1000n,
94
+ toTokenAmount: 500n,
95
+ triggerPrice: 0n,
96
+ minOutputAmount: 100n,
97
+ });
98
+ (0, vitest_1.expect)(result.ratio).toBe(10000000000000000000000000000000n);
99
+ (0, vitest_1.expect)(result.allowedSwapSlippageBps).toBe(100n);
100
+ });
101
+ (0, vitest_1.it)("returns ratio of two token amounts in case if triggerPrice is not 0n", () => {
102
+ const fromToken = { decimals: 2 };
103
+ const toToken = { decimals: 2 };
104
+ const result = (0, tokens_2.getTokensRatioByMinOutputAmountAndTriggerPrice)({
105
+ fromToken,
106
+ toToken,
107
+ fromTokenAmount: 1000n,
108
+ toTokenAmount: 500n,
109
+ triggerPrice: 100n,
110
+ minOutputAmount: 100n,
111
+ });
112
+ (0, vitest_1.expect)(result.ratio).toBe(100n);
113
+ (0, vitest_1.expect)(result.allowedSwapSlippageBps).toBe(9999n);
114
+ });
115
+ });
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const vitest_1 = require("vitest");
4
+ const trade_1 = require("../trade");
5
+ (0, vitest_1.describe)("applySlippageToPrice", () => {
6
+ (0, vitest_1.it)("applies positive slippage if getShouldUseMaxPrice is true", () => {
7
+ // isIncrease=true and isLong=true => getShouldUseMaxPrice => true
8
+ const allowedSlippage = 100; // 1%
9
+ const price = 1000n;
10
+ const result = (0, trade_1.applySlippageToPrice)(allowedSlippage, price, true, true);
11
+ // expected: price * (10000+100)/10000 = 1000n * 10100n / 10000n = 1010n
12
+ (0, vitest_1.expect)(result).toBe(1010n);
13
+ });
14
+ (0, vitest_1.it)("applies negative slippage if getShouldUseMaxPrice is false", () => {
15
+ // isIncrease=true or isLong=false => getShouldUseMaxPrice => false
16
+ const allowedSlippage = 100; // 1%
17
+ const price = 1000n;
18
+ const result = (0, trade_1.applySlippageToPrice)(allowedSlippage, price, true, false);
19
+ // expected: price * (10000-100)/10000 = 1000n * 9900n / 10000n = 990n
20
+ (0, vitest_1.expect)(result).toBe(990n);
21
+ });
22
+ });
23
+ (0, vitest_1.describe)("applySlippageToMinOut", () => {
24
+ (0, vitest_1.it)("reduces minOutputAmount by allowed slippage", () => {
25
+ const allowedSlippage = 100; // 1%
26
+ const minOutputAmount = 10000n;
27
+ const result = (0, trade_1.applySlippageToMinOut)(allowedSlippage, minOutputAmount);
28
+ // expected: minOut * (10000 - 100) / 10000 = 10_000n * 9900n / 10000n = 9900n
29
+ (0, vitest_1.expect)(result).toBe(9900n);
30
+ });
31
+ (0, vitest_1.it)("does nothing if slippage is zero", () => {
32
+ const allowedSlippage = 0;
33
+ const minOutputAmount = 10000n;
34
+ const result = (0, trade_1.applySlippageToMinOut)(allowedSlippage, minOutputAmount);
35
+ // expected: 10_000n * (10000 - 0) / 10000n = 10_000n
36
+ (0, vitest_1.expect)(result).toBe(10000n);
37
+ });
38
+ });
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.bigMath = void 0;
4
+ exports.bigMath = {
5
+ abs(x) {
6
+ return x < 0n ? -x : x;
7
+ },
8
+ mulDiv(x, y, z) {
9
+ return (x * y) / z;
10
+ },
11
+ max(max, ...rest) {
12
+ return rest.reduce((currentMax, val) => (currentMax < val ? val : currentMax), max);
13
+ },
14
+ min(min, ...rest) {
15
+ return rest.reduce((currentMin, val) => (currentMin > val ? val : currentMin), min);
16
+ },
17
+ avg(...values) {
18
+ let sum = 0n;
19
+ let count = 0n;
20
+ for (const value of values) {
21
+ if (value !== undefined) {
22
+ sum += value;
23
+ count += 1n;
24
+ }
25
+ }
26
+ if (count === 0n) {
27
+ return undefined;
28
+ }
29
+ return sum / count;
30
+ },
31
+ divRound(x, y) {
32
+ return x / y + ((x % y) * 2n > y ? 1n : 0n);
33
+ },
34
+ };
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.buildUrl = void 0;
7
+ const query_string_1 = __importDefault(require("query-string"));
8
+ function buildUrl(baseUrl, path, query) {
9
+ const qs = query ? `?${query_string_1.default.stringify(query)}` : "";
10
+ return `${baseUrl}${path}${qs}`;
11
+ }
12
+ exports.buildUrl = buildUrl;
@@ -0,0 +1,143 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.callContract = exports.getGasLimit = exports.getGasPrice = void 0;
4
+ const viem_1 = require("viem");
5
+ const chains_1 = require("../configs/chains");
6
+ const factors_1 = require("../configs/factors");
7
+ const bigmath_1 = require("./bigmath");
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;
11
+ const feeData = await (0, viem_1.withRetry)(() => client.estimateFeesPerGas({
12
+ type: "legacy",
13
+ chain: (0, chains_1.getChain)(chainId),
14
+ }), {
15
+ delay: 200,
16
+ retryCount: 2,
17
+ shouldRetry: ({ error }) => {
18
+ const isInvalidBlockError = error?.message?.includes("invalid value for value.hash");
19
+ return isInvalidBlockError;
20
+ },
21
+ });
22
+ const gasPrice = feeData.gasPrice;
23
+ if (maxFeePerGas) {
24
+ if (gasPrice !== undefined && gasPrice !== null) {
25
+ maxFeePerGas = bigmath_1.bigMath.max(gasPrice, maxFeePerGas);
26
+ }
27
+ // Fetch the latest block to get baseFeePerGas for EIP-1559 fee data
28
+ const block = await client.getBlock({ blockTag: "pending" });
29
+ if (block.baseFeePerGas !== undefined && block.baseFeePerGas !== null) {
30
+ const baseFeePerGas = block.baseFeePerGas;
31
+ const maxPriorityFeePerGas = bigmath_1.bigMath.max(chains_1.MAX_PRIORITY_FEE_PER_GAS_MAP[chainId] ?? 0n, premium);
32
+ // Calculate maxFeePerGas
33
+ const calculatedMaxFeePerGas = baseFeePerGas + maxPriorityFeePerGas + premium;
34
+ return {
35
+ maxFeePerGas: bigmath_1.bigMath.max(maxFeePerGas, calculatedMaxFeePerGas),
36
+ maxPriorityFeePerGas: maxPriorityFeePerGas + premium,
37
+ };
38
+ }
39
+ }
40
+ if (gasPrice === null || gasPrice === undefined) {
41
+ throw new Error("Can't fetch gas price");
42
+ }
43
+ const bufferBps = chains_1.GAS_PRICE_BUFFER_MAP[chainId] || 0n;
44
+ const buffer = bigmath_1.bigMath.mulDiv(gasPrice, bufferBps, factors_1.BASIS_POINTS_DIVISOR_BIGINT);
45
+ return {
46
+ gasPrice: gasPrice + buffer + premium,
47
+ };
48
+ }
49
+ exports.getGasPrice = getGasPrice;
50
+ async function getGasLimit(client, account, contractAddress, abi, method, params = [], value) {
51
+ const defaultValue = 0n;
52
+ if (value === undefined || value === null) {
53
+ value = defaultValue;
54
+ }
55
+ let gasLimit = 0n;
56
+ const data = (0, viem_1.encodeFunctionData)({
57
+ abi,
58
+ functionName: method,
59
+ args: params,
60
+ });
61
+ try {
62
+ const estimateGasParams = {
63
+ to: contractAddress,
64
+ data,
65
+ value: BigInt(value),
66
+ account,
67
+ };
68
+ gasLimit = await client.estimateGas(estimateGasParams);
69
+ }
70
+ catch (error) {
71
+ // This call should throw another error instead of the `error`
72
+ const callParams = {
73
+ to: contractAddress,
74
+ data,
75
+ value: BigInt(value),
76
+ };
77
+ if (client.account) {
78
+ callParams.account = client.account;
79
+ }
80
+ await client.call(callParams);
81
+ // If not, we throw the original estimateGas error
82
+ throw error;
83
+ }
84
+ if (gasLimit < 22000n) {
85
+ gasLimit = 22000n;
86
+ }
87
+ // Add a 10% buffer to the gas limit
88
+ return (gasLimit * 11n) / 10n;
89
+ }
90
+ exports.getGasLimit = getGasLimit;
91
+ async function callContract(sdk, contractAddress, abi, method, params, opts = {}) {
92
+ const txnOpts = {};
93
+ const chain = (0, chains_1.getChain)(sdk.chainId);
94
+ if (opts.value) {
95
+ txnOpts.value = BigInt(opts.value);
96
+ }
97
+ const clients = [sdk.publicClient];
98
+ const data = (0, viem_1.encodeFunctionData)({
99
+ abi,
100
+ functionName: method,
101
+ args: params,
102
+ });
103
+ const txnCalls = clients.map(async (client) => {
104
+ const txnInstance = { ...txnOpts };
105
+ async function retrieveGasLimit() {
106
+ return opts.gasLimit
107
+ ? BigInt(opts.gasLimit)
108
+ : await getGasLimit(client, sdk.config.account, contractAddress, abi, method, params, opts.value !== undefined ? BigInt(opts.value) : undefined);
109
+ }
110
+ const gasLimitPromise = retrieveGasLimit().then((gasLimit) => {
111
+ txnInstance.gas = gasLimit;
112
+ });
113
+ const gasPriceDataPromise = getGasPrice(sdk.publicClient, sdk.chainId).then((gasPriceData) => {
114
+ if (gasPriceData.gasPrice !== undefined) {
115
+ txnInstance.gasPrice = gasPriceData.gasPrice;
116
+ }
117
+ else {
118
+ txnInstance.maxFeePerGas = gasPriceData.maxFeePerGas;
119
+ txnInstance.maxPriorityFeePerGas = gasPriceData.maxPriorityFeePerGas;
120
+ }
121
+ });
122
+ await Promise.all([gasLimitPromise, gasPriceDataPromise]);
123
+ return sdk.walletClient.sendTransaction({
124
+ to: contractAddress,
125
+ data,
126
+ chain,
127
+ ...txnInstance,
128
+ });
129
+ });
130
+ const res = await Promise.any(txnCalls)
131
+ .catch((error) => {
132
+ if (error.errors && error.errors.length > 1) {
133
+ // eslint-disable-next-line no-console
134
+ console.error("All transactions failed", ...error.errors);
135
+ }
136
+ throw error.errors ? error.errors[0] : error;
137
+ })
138
+ .catch((error) => {
139
+ throw error;
140
+ });
141
+ return res;
142
+ }
143
+ exports.callContract = callContract;
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TIMEZONE_OFFSET_SEC = exports.sleep = void 0;
4
+ const sleep = (ms, abortSignal) => new Promise((resolve) => {
5
+ const timeout = setTimeout(resolve, ms);
6
+ if (abortSignal) {
7
+ abortSignal.addEventListener("abort", () => {
8
+ clearTimeout(timeout);
9
+ resolve(undefined);
10
+ });
11
+ }
12
+ });
13
+ exports.sleep = sleep;
14
+ exports.TIMEZONE_OFFSET_SEC = -new Date().getTimezoneOffset() * 60;