@gmx-io/sdk 1.4.1 → 1.5.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 (418) hide show
  1. package/build/cjs/scripts/prebuild/index.js +6 -1
  2. package/build/cjs/scripts/prebuild/prebuildKinkModelMarketRatesKeys.js +2 -28
  3. package/build/cjs/scripts/prebuild/prebuildMarketConfigKeys.js +2 -2
  4. package/build/cjs/scripts/prebuild/prebuildMarketValuesKeys.js +2 -2
  5. package/build/cjs/scripts/subsquid-codegen.js +26 -0
  6. package/build/cjs/src/{utils → clients/v1}/callContract.js +4 -6
  7. package/build/cjs/src/{index.js → clients/v1/index.js} +4 -4
  8. package/build/cjs/src/{modules → clients/v1/modules}/accounts/accounts.js +1 -1
  9. package/build/cjs/src/{modules → clients/v1/modules}/accounts/accounts.spec.js +1 -1
  10. package/build/cjs/src/clients/v1/modules/markets/index.js +227 -0
  11. package/build/cjs/src/{modules → clients/v1/modules}/markets/markets.spec.js +1 -1
  12. package/build/cjs/src/{modules → clients/v1/modules}/oracle.js +1 -1
  13. package/build/cjs/src/{modules → clients/v1/modules}/orders/helpers.js +11 -9
  14. package/build/cjs/src/{modules → clients/v1/modules}/orders/helpers.spec.js +5 -5
  15. package/build/cjs/src/{modules → clients/v1/modules}/orders/orders.js +11 -11
  16. package/build/cjs/src/{modules → clients/v1/modules}/orders/orders.spec.js +1 -1
  17. package/build/cjs/src/{modules → clients/v1/modules}/orders/transactions/cancelOrdersTxn.js +2 -2
  18. package/build/cjs/src/{modules → clients/v1/modules}/orders/transactions/createDecreaseOrderTxn.js +7 -7
  19. package/build/cjs/src/{modules → clients/v1/modules}/orders/transactions/createIncreaseOrderTxn.js +11 -11
  20. package/build/cjs/src/{modules → clients/v1/modules}/orders/transactions/createSwapOrderTxn.js +10 -10
  21. package/build/cjs/src/{modules → clients/v1/modules}/orders/transactions/createWrapOrUnwrapTxn.js +2 -2
  22. package/build/cjs/src/{modules → clients/v1/modules}/orders/transactions/updateOrderTxn.js +3 -3
  23. package/build/cjs/src/{modules → clients/v1/modules}/orders/utils.js +7 -7
  24. package/build/cjs/src/{modules → clients/v1/modules}/positions/positions.js +11 -11
  25. package/build/cjs/src/{modules → clients/v1/modules}/positions/positions.spec.js +1 -1
  26. package/build/cjs/src/{modules → clients/v1/modules}/tokens/tokens.js +3 -3
  27. package/build/cjs/src/{modules → clients/v1/modules}/tokens/tokens.spec.js +1 -1
  28. package/build/cjs/src/{modules → clients/v1/modules}/trades/trades.js +19 -19
  29. package/build/cjs/src/{modules → clients/v1/modules}/trades/trades.spec.js +1 -1
  30. package/build/cjs/src/{modules → clients/v1/modules}/utils/utils.js +8 -8
  31. package/build/cjs/src/{utils → clients/v1}/multicall.js +4 -4
  32. package/build/cjs/src/{utils → clients/v1}/simulateExecuteOrder.js +5 -5
  33. package/build/cjs/src/{utils → clients/v1}/testUtil.js +2 -2
  34. package/build/cjs/src/clients/v2/index.js +21 -0
  35. package/build/cjs/src/{prebuilt → codegen/prebuilt}/hashedKinkModelMarketRatesKeys.json +40 -0
  36. package/build/cjs/src/{prebuilt → codegen/prebuilt}/hashedMarketConfigKeys.json +285 -0
  37. package/build/cjs/src/{prebuilt → codegen/prebuilt}/hashedMarketValuesKeys.json +75 -0
  38. package/build/cjs/src/codegen/prebuilt/index.js +16 -0
  39. package/build/cjs/src/configs/api.js +15 -0
  40. package/build/cjs/src/configs/chains.js +29 -1
  41. package/build/cjs/src/configs/dataStore.js +6 -2
  42. package/build/cjs/src/configs/markets.js +35 -0
  43. package/build/cjs/src/configs/multichain.js +29 -19
  44. package/build/cjs/src/configs/tokens.js +49 -169
  45. package/build/cjs/src/test/mock.js +5 -122
  46. package/build/cjs/src/utils/24h/index.js +17 -0
  47. package/build/cjs/src/utils/LruCache/index.js +17 -0
  48. package/build/cjs/src/utils/__tests__/fees.spec.js +1 -1
  49. package/build/cjs/src/utils/__tests__/orderTransactions/batchUtils.spec.js +2 -2
  50. package/build/cjs/src/utils/__tests__/orderTransactions/decreaseOrders.spec.js +33 -33
  51. package/build/cjs/src/utils/__tests__/orderTransactions/exchangeRouterMulticall.spec.js +4 -4
  52. package/build/cjs/src/utils/__tests__/orderTransactions/increaseOrders.spec.js +28 -28
  53. package/build/cjs/src/utils/__tests__/orderTransactions/swapOrders.spec.js +20 -20
  54. package/build/cjs/src/utils/__tests__/orderTransactions/updateOrder.spec.js +3 -3
  55. package/build/cjs/src/utils/__tests__/prices.spec.js +18 -18
  56. package/build/cjs/src/utils/abort/index.js +17 -0
  57. package/build/cjs/src/utils/bigmath/index.js +17 -0
  58. package/build/cjs/src/utils/buildUrl/index.js +17 -0
  59. package/build/cjs/src/utils/chains/index.js +17 -0
  60. package/build/cjs/src/utils/common/index.js +17 -0
  61. package/build/cjs/src/utils/{common.js → common/utils.js} +1 -2
  62. package/build/cjs/src/utils/fees/executionFee.js +2 -2
  63. package/build/cjs/src/utils/fees/index.js +9 -9
  64. package/build/cjs/src/utils/gelatoRelay/index.js +17 -0
  65. package/build/cjs/src/utils/graphqlFetcher/index.js +8 -0
  66. package/build/cjs/src/utils/hash/index.js +17 -0
  67. package/build/cjs/src/utils/{hash.js → hash/utils.js} +1 -1
  68. package/build/cjs/src/utils/http/http.js +33 -0
  69. package/build/cjs/src/utils/indexers/index.js +17 -0
  70. package/build/cjs/src/utils/{indexers.js → indexers/utils.js} +1 -1
  71. package/build/cjs/src/utils/markets/__tests__/mockMarkets.js +129 -0
  72. package/build/cjs/src/utils/markets/api.js +14 -0
  73. package/build/cjs/src/utils/{marketKeysAndConfigs.js → markets/hashKeys.js} +32 -3
  74. package/build/cjs/src/utils/markets/index.js +21 -0
  75. package/build/cjs/src/{modules/markets/query-builders.js → utils/markets/multicall.js} +149 -42
  76. package/build/cjs/src/utils/markets/utils.js +398 -0
  77. package/build/cjs/src/utils/numbers/index.js +17 -0
  78. package/build/cjs/src/utils/{numbers.js → numbers/utils.js} +8 -3
  79. package/build/cjs/src/utils/objects/index.js +17 -0
  80. package/build/cjs/src/utils/{objects.js → objects/utils.js} +9 -1
  81. package/build/cjs/src/utils/orderTransactions/index.js +17 -0
  82. package/build/cjs/src/utils/{orderTransactions.js → orderTransactions/utils.js} +22 -22
  83. package/build/cjs/src/utils/orders/index.js +18 -0
  84. package/build/cjs/src/utils/{orders.js → orders/utils.js} +22 -22
  85. package/build/cjs/src/utils/positions/index.js +18 -0
  86. package/build/cjs/src/utils/{positions.js → positions/utils.js} +6 -6
  87. package/build/cjs/src/utils/prices/index.js +17 -0
  88. package/build/cjs/src/utils/{prices.js → prices/utils.js} +15 -15
  89. package/build/cjs/src/utils/referrals/index.js +18 -0
  90. package/build/cjs/src/utils/sidecarOrders/index.js +17 -0
  91. package/build/cjs/src/utils/swap/__tests__/findSwapPathsBetweenTokens.spec.js +22 -0
  92. package/build/cjs/src/utils/swap/__tests__/mockRouting.spec.js +264 -7
  93. package/build/cjs/src/utils/swap/__tests__/swapRouting.spec.js +5 -5
  94. package/build/cjs/src/utils/swap/__tests__/swapStats.spec.js +10 -10
  95. package/build/cjs/src/utils/swap/botanixStaking.js +3 -3
  96. package/build/cjs/src/utils/swap/buildSwapStrategy.js +13 -5
  97. package/build/cjs/src/utils/swap/externalSwapPath.js +2 -2
  98. package/build/cjs/src/utils/swap/externalSwapQuoteByPath.js +2 -2
  99. package/build/cjs/src/utils/swap/findSwapPathsBetweenTokens.js +8 -4
  100. package/build/cjs/src/utils/swap/index.js +12 -0
  101. package/build/cjs/src/utils/swap/swapPath.js +3 -3
  102. package/build/cjs/src/utils/swap/swapValues.js +17 -10
  103. package/build/cjs/src/utils/time/index.js +17 -0
  104. package/build/cjs/src/utils/tokens/index.js +18 -0
  105. package/build/cjs/src/utils/{tokens.js → tokens/utils.js} +4 -4
  106. package/build/cjs/src/utils/trade/decrease.js +8 -8
  107. package/build/cjs/src/utils/trade/increase.js +5 -2
  108. package/build/cjs/src/utils/trade/index.js +3 -0
  109. package/build/cjs/src/utils/trade/trade.js +10 -10
  110. package/build/cjs/src/utils/tradeHistory/index.js +18 -0
  111. package/build/cjs/src/utils/{tradeHistory.js → tradeHistory/utils.js} +4 -4
  112. package/build/cjs/src/utils/twap/index.js +15 -0
  113. package/build/cjs/src/utils/types/index.js +7 -0
  114. package/build/esm/scripts/prebuild/index.js +3 -1
  115. package/build/esm/scripts/prebuild/prebuildKinkModelMarketRatesKeys.js +2 -28
  116. package/build/esm/scripts/prebuild/prebuildMarketConfigKeys.js +1 -1
  117. package/build/esm/scripts/prebuild/prebuildMarketValuesKeys.js +1 -1
  118. package/build/esm/scripts/subsquid-codegen.js +24 -0
  119. package/build/esm/src/{utils → clients/v1}/callContract.js +5 -5
  120. package/build/esm/src/{index.js → clients/v1/index.js} +4 -4
  121. package/build/esm/src/{modules → clients/v1/modules}/accounts/accounts.js +1 -1
  122. package/build/esm/src/{modules → clients/v1/modules}/accounts/accounts.spec.js +1 -1
  123. package/build/esm/src/clients/v1/modules/markets/index.js +220 -0
  124. package/build/esm/src/{modules → clients/v1/modules}/markets/markets.spec.js +1 -1
  125. package/build/esm/src/{modules → clients/v1/modules}/oracle.js +1 -1
  126. package/build/esm/src/{modules → clients/v1/modules}/orders/helpers.js +8 -6
  127. package/build/esm/src/{modules → clients/v1/modules}/orders/helpers.spec.js +5 -5
  128. package/build/esm/src/{modules → clients/v1/modules}/orders/orders.js +4 -4
  129. package/build/esm/src/{modules → clients/v1/modules}/orders/orders.spec.js +1 -1
  130. package/build/esm/src/{modules → clients/v1/modules}/orders/transactions/cancelOrdersTxn.js +2 -2
  131. package/build/esm/src/{modules → clients/v1/modules}/orders/transactions/createDecreaseOrderTxn.js +7 -7
  132. package/build/esm/src/{modules → clients/v1/modules}/orders/transactions/createIncreaseOrderTxn.js +8 -8
  133. package/build/esm/src/{modules → clients/v1/modules}/orders/transactions/createSwapOrderTxn.js +7 -7
  134. package/build/esm/src/{modules → clients/v1/modules}/orders/transactions/createWrapOrUnwrapTxn.js +2 -2
  135. package/build/esm/src/{modules → clients/v1/modules}/orders/transactions/updateOrderTxn.js +3 -3
  136. package/build/esm/src/{modules → clients/v1/modules}/orders/utils.js +7 -7
  137. package/build/esm/src/{modules → clients/v1/modules}/positions/positions.js +11 -11
  138. package/build/esm/src/{modules → clients/v1/modules}/positions/positions.spec.js +1 -1
  139. package/build/esm/src/{modules → clients/v1/modules}/tokens/tokens.js +3 -3
  140. package/build/esm/src/{modules → clients/v1/modules}/tokens/tokens.spec.js +1 -1
  141. package/build/esm/src/{modules → clients/v1/modules}/trades/trades.js +8 -8
  142. package/build/esm/src/{modules → clients/v1/modules}/trades/trades.spec.js +1 -1
  143. package/build/esm/src/{modules → clients/v1/modules}/utils/utils.js +8 -8
  144. package/build/esm/src/{utils → clients/v1}/multicall.js +4 -4
  145. package/build/esm/src/{utils → clients/v1}/simulateExecuteOrder.js +5 -5
  146. package/build/esm/src/{utils → clients/v1}/testUtil.js +2 -2
  147. package/build/esm/src/clients/v2/index.js +17 -0
  148. package/build/esm/src/{prebuilt → codegen/prebuilt}/hashedKinkModelMarketRatesKeys.json +40 -0
  149. package/build/esm/src/{prebuilt → codegen/prebuilt}/hashedMarketConfigKeys.json +285 -0
  150. package/build/esm/src/{prebuilt → codegen/prebuilt}/hashedMarketValuesKeys.json +75 -0
  151. package/build/esm/src/codegen/prebuilt/index.js +10 -0
  152. package/build/esm/src/configs/api.js +11 -0
  153. package/build/esm/src/configs/chains.js +29 -1
  154. package/build/esm/src/configs/dataStore.js +3 -0
  155. package/build/esm/src/configs/markets.js +35 -0
  156. package/build/esm/src/configs/multichain.js +28 -19
  157. package/build/esm/src/configs/tokens.js +49 -169
  158. package/build/esm/src/test/mock.js +3 -120
  159. package/build/esm/src/utils/24h/index.js +1 -0
  160. package/build/esm/src/utils/LruCache/index.js +1 -0
  161. package/build/esm/src/utils/__tests__/fees.spec.js +1 -1
  162. package/build/esm/src/utils/__tests__/orderTransactions/batchUtils.spec.js +1 -1
  163. package/build/esm/src/utils/__tests__/orderTransactions/decreaseOrders.spec.js +1 -1
  164. package/build/esm/src/utils/__tests__/orderTransactions/exchangeRouterMulticall.spec.js +1 -1
  165. package/build/esm/src/utils/__tests__/orderTransactions/increaseOrders.spec.js +1 -1
  166. package/build/esm/src/utils/__tests__/orderTransactions/swapOrders.spec.js +1 -1
  167. package/build/esm/src/utils/__tests__/orderTransactions/updateOrder.spec.js +1 -1
  168. package/build/esm/src/utils/__tests__/prices.spec.js +2 -2
  169. package/build/esm/src/utils/abort/index.js +1 -0
  170. package/build/esm/src/utils/bigmath/index.js +1 -0
  171. package/build/esm/src/utils/buildUrl/index.js +1 -0
  172. package/build/esm/src/utils/chains/index.js +1 -0
  173. package/build/esm/src/utils/common/index.js +1 -0
  174. package/build/esm/src/utils/{common.js → common/utils.js} +0 -1
  175. package/build/esm/src/utils/fees/executionFee.js +1 -1
  176. package/build/esm/src/utils/fees/index.js +7 -7
  177. package/build/esm/src/utils/gelatoRelay/index.js +1 -0
  178. package/build/esm/src/utils/graphqlFetcher/index.js +1 -0
  179. package/build/esm/src/utils/hash/index.js +1 -0
  180. package/build/esm/src/utils/{hash.js → hash/utils.js} +1 -1
  181. package/build/esm/src/utils/http/http.js +26 -0
  182. package/build/esm/src/utils/indexers/index.js +1 -0
  183. package/build/esm/src/utils/{indexers.js → indexers/utils.js} +1 -1
  184. package/build/esm/src/utils/markets/__tests__/mockMarkets.js +124 -0
  185. package/build/esm/src/utils/markets/api.js +9 -0
  186. package/build/esm/src/utils/{marketKeysAndConfigs.js → markets/hashKeys.js} +30 -2
  187. package/build/esm/src/utils/markets/index.js +5 -0
  188. package/build/esm/src/{modules/markets/query-builders.js → utils/markets/multicall.js} +146 -41
  189. package/build/esm/src/utils/markets/utils.js +364 -0
  190. package/build/esm/src/utils/numbers/index.js +1 -0
  191. package/build/esm/src/utils/{numbers.js → numbers/utils.js} +8 -3
  192. package/build/esm/src/utils/objects/index.js +1 -0
  193. package/build/esm/src/utils/{objects.js → objects/utils.js} +7 -0
  194. package/build/esm/src/utils/orderTransactions/index.js +1 -0
  195. package/build/esm/src/utils/{orderTransactions.js → orderTransactions/utils.js} +14 -14
  196. package/build/esm/src/utils/orders/index.js +2 -0
  197. package/build/esm/src/utils/{orders.js → orders/utils.js} +8 -8
  198. package/build/esm/src/utils/positions/index.js +2 -0
  199. package/build/esm/src/utils/{positions.js → positions/utils.js} +6 -6
  200. package/build/esm/src/utils/prices/index.js +1 -0
  201. package/build/esm/src/utils/{prices.js → prices/utils.js} +7 -7
  202. package/build/esm/src/utils/referrals/index.js +2 -0
  203. package/build/esm/src/utils/sidecarOrders/index.js +1 -0
  204. package/build/esm/src/utils/swap/__tests__/findSwapPathsBetweenTokens.spec.js +22 -0
  205. package/build/esm/src/utils/swap/__tests__/mockRouting.spec.js +258 -1
  206. package/build/esm/src/utils/swap/__tests__/swapRouting.spec.js +1 -1
  207. package/build/esm/src/utils/swap/__tests__/swapStats.spec.js +1 -1
  208. package/build/esm/src/utils/swap/botanixStaking.js +1 -1
  209. package/build/esm/src/utils/swap/buildSwapStrategy.js +14 -6
  210. package/build/esm/src/utils/swap/externalSwapPath.js +1 -1
  211. package/build/esm/src/utils/swap/externalSwapQuoteByPath.js +1 -1
  212. package/build/esm/src/utils/swap/findSwapPathsBetweenTokens.js +8 -4
  213. package/build/esm/src/utils/swap/index.js +12 -0
  214. package/build/esm/src/utils/swap/swapPath.js +1 -1
  215. package/build/esm/src/utils/swap/swapValues.js +15 -8
  216. package/build/esm/src/utils/time/index.js +1 -0
  217. package/build/esm/src/utils/tokens/index.js +2 -0
  218. package/build/esm/src/utils/{tokens.js → tokens/utils.js} +4 -4
  219. package/build/esm/src/utils/trade/decrease.js +1 -1
  220. package/build/esm/src/utils/trade/increase.js +4 -1
  221. package/build/esm/src/utils/trade/index.js +3 -0
  222. package/build/esm/src/utils/trade/trade.js +2 -2
  223. package/build/esm/src/utils/tradeHistory/index.js +2 -0
  224. package/build/esm/src/utils/{tradeHistory.js → tradeHistory/utils.js} +4 -4
  225. package/build/esm/src/utils/twap/index.js +1 -0
  226. package/build/esm/src/utils/types/index.js +3 -0
  227. package/build/types/scripts/subsquid-codegen.d.ts +3 -0
  228. package/build/types/src/clients/v1/callContract.d.ts +7 -0
  229. package/build/types/src/{index.d.ts → clients/v1/index.d.ts} +3 -3
  230. package/build/types/src/{modules → clients/v1/modules}/markets/index.d.ts +12 -1
  231. package/build/types/src/{modules → clients/v1/modules}/oracle.d.ts +1 -1
  232. package/build/types/src/{modules → clients/v1/modules}/orders/helpers.d.ts +3 -3
  233. package/build/types/src/{modules → clients/v1/modules}/orders/orders.d.ts +5 -5
  234. package/build/types/src/{modules → clients/v1/modules}/orders/transactions/createDecreaseOrderTxn.d.ts +2 -2
  235. package/build/types/src/{modules → clients/v1/modules}/orders/transactions/createIncreaseOrderTxn.d.ts +4 -4
  236. package/build/types/src/{modules → clients/v1/modules}/orders/transactions/createSwapOrderTxn.d.ts +2 -2
  237. package/build/types/src/{modules → clients/v1/modules}/orders/transactions/updateOrderTxn.d.ts +1 -1
  238. package/build/types/src/{modules → clients/v1/modules}/orders/utils.d.ts +8 -8
  239. package/build/types/src/{modules → clients/v1/modules}/positions/positions.d.ts +4 -4
  240. package/build/types/src/{modules → clients/v1/modules}/tokens/tokens.d.ts +1 -1
  241. package/build/types/src/{modules → clients/v1/modules}/trades/trades.d.ts +4 -4
  242. package/build/types/src/{modules → clients/v1/modules}/utils/utils.d.ts +4 -4
  243. package/build/types/src/{utils → clients/v1}/multicall.d.ts +4 -4
  244. package/build/types/src/{utils → clients/v1}/simulateExecuteOrder.d.ts +3 -3
  245. package/build/types/src/{utils → clients/v1}/testUtil.d.ts +2 -2
  246. package/build/types/src/{types/sdk.d.ts → clients/v1/types.d.ts} +3 -3
  247. package/build/types/src/clients/v2/index.d.ts +13 -0
  248. package/build/types/src/codegen/prebuilt/index.d.ts +13636 -0
  249. package/build/types/src/{types → codegen}/subsquid.d.ts +13 -0
  250. package/build/types/src/configs/api.d.ts +1 -0
  251. package/build/types/src/configs/chains.d.ts +26 -2
  252. package/build/types/src/configs/dataStore.d.ts +1 -0
  253. package/build/types/src/configs/express.d.ts +1 -1
  254. package/build/types/src/configs/gasLimits.d.ts +1 -1
  255. package/build/types/src/configs/markets.d.ts +1 -1
  256. package/build/types/src/configs/multichain.d.ts +6 -3
  257. package/build/types/src/configs/tokens.d.ts +1 -1
  258. package/build/types/src/test/mock.d.ts +3 -7
  259. package/build/types/src/utils/24h/index.d.ts +1 -0
  260. package/build/types/src/utils/24h/types.d.ts +7 -0
  261. package/build/types/src/utils/LruCache/index.d.ts +1 -0
  262. package/build/types/src/utils/abort/index.d.ts +1 -0
  263. package/build/types/src/utils/bigmath/index.d.ts +1 -0
  264. package/build/types/src/utils/buildUrl/index.d.ts +1 -0
  265. package/build/types/src/utils/chains/index.d.ts +1 -0
  266. package/build/types/src/utils/common/index.d.ts +1 -0
  267. package/build/types/src/utils/{common.d.ts → common/utils.d.ts} +0 -1
  268. package/build/types/src/utils/fees/executionFee.d.ts +3 -3
  269. package/build/types/src/utils/fees/getNaiveEstimatedGasBySwapCount.d.ts +1 -1
  270. package/build/types/src/utils/fees/index.d.ts +10 -13
  271. package/build/types/src/utils/fees/priceImpact.d.ts +3 -3
  272. package/build/types/src/{types/fees.d.ts → utils/fees/types.d.ts} +2 -2
  273. package/build/types/src/utils/gelatoRelay/index.d.ts +1 -0
  274. package/build/types/src/utils/graphqlFetcher/index.d.ts +1 -0
  275. package/build/types/src/utils/hash/index.d.ts +1 -0
  276. package/build/types/src/utils/http/http.d.ts +9 -0
  277. package/build/types/src/utils/http/types.d.ts +7 -0
  278. package/build/types/src/utils/indexers/index.d.ts +1 -0
  279. package/build/types/src/utils/markets/__tests__/mockMarkets.d.ts +9 -0
  280. package/build/types/src/utils/markets/api.d.ts +9 -0
  281. package/build/types/src/utils/markets/hashKeys.d.ts +23 -0
  282. package/build/types/src/utils/markets/index.d.ts +5 -0
  283. package/build/types/src/utils/markets/multicall.d.ts +25 -0
  284. package/build/types/src/{types/markets.d.ts → utils/markets/types.d.ts} +64 -1
  285. package/build/types/src/utils/markets/utils.d.ts +122 -0
  286. package/build/types/src/utils/numbers/index.d.ts +1 -0
  287. package/build/types/src/utils/{numbers.d.ts → numbers/utils.d.ts} +3 -1
  288. package/build/types/src/utils/objects/index.d.ts +1 -0
  289. package/build/types/src/utils/{objects.d.ts → objects/utils.d.ts} +1 -0
  290. package/build/types/src/utils/orderTransactions/index.d.ts +1 -0
  291. package/build/types/src/utils/{orderTransactions.d.ts → orderTransactions/utils.d.ts} +6 -6
  292. package/build/types/src/utils/orders/index.d.ts +2 -0
  293. package/build/types/src/{types/orders.d.ts → utils/orders/types.d.ts} +3 -3
  294. package/build/types/src/utils/{orders.d.ts → orders/utils.d.ts} +3 -3
  295. package/build/types/src/utils/positions/index.d.ts +2 -0
  296. package/build/types/src/{types/positions.d.ts → utils/positions/types.d.ts} +2 -2
  297. package/build/types/src/utils/{positions.d.ts → positions/utils.d.ts} +4 -4
  298. package/build/types/src/utils/prices/index.d.ts +1 -0
  299. package/build/types/src/utils/{prices.d.ts → prices/utils.d.ts} +4 -4
  300. package/build/types/src/utils/referrals/index.d.ts +2 -0
  301. package/build/types/src/utils/sidecarOrders/index.d.ts +1 -0
  302. package/build/types/src/{types/sidecarOrders.d.ts → utils/sidecarOrders/types.d.ts} +2 -2
  303. package/build/types/src/utils/swap/botanixStaking.d.ts +2 -2
  304. package/build/types/src/utils/swap/buildSwapStrategy.d.ts +9 -7
  305. package/build/types/src/utils/swap/externalSwapPath.d.ts +1 -1
  306. package/build/types/src/utils/swap/externalSwapQuoteByPath.d.ts +1 -1
  307. package/build/types/src/utils/swap/findSwapPathsBetweenTokens.d.ts +1 -1
  308. package/build/types/src/utils/swap/index.d.ts +12 -0
  309. package/build/types/src/utils/swap/preparedSwapData.d.ts +1 -1
  310. package/build/types/src/utils/swap/swapPath.d.ts +6 -6
  311. package/build/types/src/utils/swap/swapRouting.d.ts +5 -5
  312. package/build/types/src/utils/swap/swapStats.d.ts +3 -3
  313. package/build/types/src/utils/swap/swapValues.d.ts +6 -4
  314. package/build/types/src/{types/swapStrategy.d.ts → utils/swap/types.d.ts} +1 -1
  315. package/build/types/src/utils/time/index.d.ts +1 -0
  316. package/build/types/src/utils/tokens/index.d.ts +2 -0
  317. package/build/types/src/{types/tokens.d.ts → utils/tokens/types.d.ts} +0 -3
  318. package/build/types/src/utils/{tokens.d.ts → tokens/utils.d.ts} +2 -2
  319. package/build/types/src/utils/trade/decrease.d.ts +5 -5
  320. package/build/types/src/utils/trade/increase.d.ts +5 -5
  321. package/build/types/src/utils/trade/index.d.ts +3 -0
  322. package/build/types/src/utils/trade/trade.d.ts +1 -1
  323. package/build/types/src/{types/trade.d.ts → utils/trade/types.d.ts} +4 -4
  324. package/build/types/src/utils/tradeHistory/index.d.ts +2 -0
  325. package/build/types/src/{types/tradeHistory.d.ts → utils/tradeHistory/types.d.ts} +3 -3
  326. package/build/types/src/utils/{tradeHistory.d.ts → tradeHistory/utils.d.ts} +4 -4
  327. package/build/types/src/utils/twap/index.d.ts +3 -2
  328. package/build/types/src/utils/{types.d.ts → types/index.d.ts} +0 -1
  329. package/package.json +286 -32
  330. package/build/cjs/src/modules/markets/index.js +0 -366
  331. package/build/cjs/src/prebuilt/index.js +0 -15
  332. package/build/cjs/src/utils/markets.js +0 -195
  333. package/build/cjs/src/utils/types.js +0 -13
  334. package/build/esm/src/modules/markets/index.js +0 -359
  335. package/build/esm/src/prebuilt/index.js +0 -7
  336. package/build/esm/src/utils/markets.js +0 -171
  337. package/build/esm/src/utils/types.js +0 -8
  338. package/build/types/src/modules/markets/query-builders.d.ts +0 -42
  339. package/build/types/src/modules/markets/types.d.ts +0 -51
  340. package/build/types/src/prebuilt/index.d.ts +0 -24
  341. package/build/types/src/types/common.d.ts +0 -1
  342. package/build/types/src/utils/callContract.d.ts +0 -17
  343. package/build/types/src/utils/marketKeysAndConfigs.d.ts +0 -73
  344. package/build/types/src/utils/markets.d.ts +0 -58
  345. /package/build/cjs/src/{modules → clients/v1/modules}/base.js +0 -0
  346. /package/build/cjs/src/{modules/markets → clients/v1}/types.js +0 -0
  347. /package/build/cjs/src/{types → codegen}/subsquid.js +0 -0
  348. /package/build/cjs/src/{types/chains.js → utils/24h/types.js} +0 -0
  349. /package/build/cjs/src/utils/{LruCache.js → LruCache/utils.js} +0 -0
  350. /package/build/cjs/src/utils/{abort.js → abort/utils.js} +0 -0
  351. /package/build/cjs/src/utils/{bigmath.js → bigmath/utils.js} +0 -0
  352. /package/build/cjs/src/utils/{buildUrl.js → buildUrl/utils.js} +0 -0
  353. /package/build/cjs/src/{types/common.js → utils/chains/types.js} +0 -0
  354. /package/build/cjs/src/{types/fees.js → utils/fees/types.js} +0 -0
  355. /package/build/cjs/src/utils/{gelatoRelay.js → gelatoRelay/utils.js} +0 -0
  356. /package/build/cjs/src/utils/{graphqlFetcher.js → graphqlFetcher/utils.js} +0 -0
  357. /package/build/cjs/src/{types/markets.js → utils/http/types.js} +0 -0
  358. /package/build/cjs/src/{types/positions.js → utils/markets/types.js} +0 -0
  359. /package/build/cjs/src/{types/orders.js → utils/orders/types.js} +0 -0
  360. /package/build/cjs/src/{types/sdk.js → utils/positions/types.js} +0 -0
  361. /package/build/cjs/src/{types/referrals.js → utils/referrals/types.js} +0 -0
  362. /package/build/cjs/src/utils/{referrals.js → referrals/utils.js} +0 -0
  363. /package/build/cjs/src/{types/sidecarOrders.js → utils/sidecarOrders/types.js} +0 -0
  364. /package/build/cjs/src/{types/swapStrategy.js → utils/swap/types.js} +0 -0
  365. /package/build/cjs/src/utils/{time.js → time/utils.js} +0 -0
  366. /package/build/cjs/src/{types/tokens.js → utils/tokens/types.js} +0 -0
  367. /package/build/cjs/src/{types/trade.js → utils/trade/types.js} +0 -0
  368. /package/build/cjs/src/{types/tradeHistory.js → utils/tradeHistory/types.js} +0 -0
  369. /package/build/cjs/src/{types/twap.js → utils/twap/types.js} +0 -0
  370. /package/build/esm/src/{modules → clients/v1/modules}/base.js +0 -0
  371. /package/build/esm/src/{modules/markets → clients/v1}/types.js +0 -0
  372. /package/build/esm/src/{types → codegen}/subsquid.js +0 -0
  373. /package/build/esm/src/{types/chains.js → utils/24h/types.js} +0 -0
  374. /package/build/esm/src/utils/{LruCache.js → LruCache/utils.js} +0 -0
  375. /package/build/esm/src/utils/{abort.js → abort/utils.js} +0 -0
  376. /package/build/esm/src/utils/{bigmath.js → bigmath/utils.js} +0 -0
  377. /package/build/esm/src/utils/{buildUrl.js → buildUrl/utils.js} +0 -0
  378. /package/build/esm/src/{types/common.js → utils/chains/types.js} +0 -0
  379. /package/build/esm/src/{types/fees.js → utils/fees/types.js} +0 -0
  380. /package/build/esm/src/utils/{gelatoRelay.js → gelatoRelay/utils.js} +0 -0
  381. /package/build/esm/src/utils/{graphqlFetcher.js → graphqlFetcher/utils.js} +0 -0
  382. /package/build/esm/src/{types/markets.js → utils/http/types.js} +0 -0
  383. /package/build/esm/src/{types/positions.js → utils/markets/types.js} +0 -0
  384. /package/build/esm/src/{types/orders.js → utils/orders/types.js} +0 -0
  385. /package/build/esm/src/{types/sdk.js → utils/positions/types.js} +0 -0
  386. /package/build/esm/src/{types/referrals.js → utils/referrals/types.js} +0 -0
  387. /package/build/esm/src/utils/{referrals.js → referrals/utils.js} +0 -0
  388. /package/build/esm/src/{types/sidecarOrders.js → utils/sidecarOrders/types.js} +0 -0
  389. /package/build/esm/src/{types/swapStrategy.js → utils/swap/types.js} +0 -0
  390. /package/build/esm/src/utils/{time.js → time/utils.js} +0 -0
  391. /package/build/esm/src/{types/tokens.js → utils/tokens/types.js} +0 -0
  392. /package/build/esm/src/{types/trade.js → utils/trade/types.js} +0 -0
  393. /package/build/esm/src/{types/tradeHistory.js → utils/tradeHistory/types.js} +0 -0
  394. /package/build/esm/src/{types/twap.js → utils/twap/types.js} +0 -0
  395. /package/build/types/src/{modules → clients/v1/modules}/accounts/accounts.d.ts +0 -0
  396. /package/build/types/src/{modules → clients/v1/modules}/accounts/accounts.spec.d.ts +0 -0
  397. /package/build/types/src/{modules → clients/v1/modules}/base.d.ts +0 -0
  398. /package/build/types/src/{modules → clients/v1/modules}/markets/markets.spec.d.ts +0 -0
  399. /package/build/types/src/{modules → clients/v1/modules}/orders/helpers.spec.d.ts +0 -0
  400. /package/build/types/src/{modules → clients/v1/modules}/orders/orders.spec.d.ts +0 -0
  401. /package/build/types/src/{modules → clients/v1/modules}/orders/transactions/cancelOrdersTxn.d.ts +0 -0
  402. /package/build/types/src/{modules → clients/v1/modules}/orders/transactions/createWrapOrUnwrapTxn.d.ts +0 -0
  403. /package/build/types/src/{modules → clients/v1/modules}/positions/positions.spec.d.ts +0 -0
  404. /package/build/types/src/{modules → clients/v1/modules}/tokens/tokens.spec.d.ts +0 -0
  405. /package/build/types/src/{modules → clients/v1/modules}/trades/trades.spec.d.ts +0 -0
  406. /package/build/types/src/utils/{LruCache.d.ts → LruCache/utils.d.ts} +0 -0
  407. /package/build/types/src/utils/{abort.d.ts → abort/utils.d.ts} +0 -0
  408. /package/build/types/src/utils/{bigmath.d.ts → bigmath/utils.d.ts} +0 -0
  409. /package/build/types/src/utils/{buildUrl.d.ts → buildUrl/utils.d.ts} +0 -0
  410. /package/build/types/src/{types/chains.d.ts → utils/chains/types.d.ts} +0 -0
  411. /package/build/types/src/utils/{gelatoRelay.d.ts → gelatoRelay/utils.d.ts} +0 -0
  412. /package/build/types/src/utils/{graphqlFetcher.d.ts → graphqlFetcher/utils.d.ts} +0 -0
  413. /package/build/types/src/utils/{hash.d.ts → hash/utils.d.ts} +0 -0
  414. /package/build/types/src/utils/{indexers.d.ts → indexers/utils.d.ts} +0 -0
  415. /package/build/types/src/{types/referrals.d.ts → utils/referrals/types.d.ts} +0 -0
  416. /package/build/types/src/utils/{referrals.d.ts → referrals/utils.d.ts} +0 -0
  417. /package/build/types/src/utils/{time.d.ts → time/utils.d.ts} +0 -0
  418. /package/build/types/src/{types/twap.d.ts → utils/twap/types.d.ts} +0 -0
@@ -34,3 +34,10 @@ export function objectKeysDeep(obj, depth = 1) {
34
34
  }
35
35
  return Array.from(keys);
36
36
  }
37
+ export function toDict(array, by) {
38
+ return array.reduce((acc, item) => {
39
+ const key = item[by];
40
+ acc[key] = item;
41
+ return acc;
42
+ }, {});
43
+ }
@@ -0,0 +1 @@
1
+ export * from "./utils";
@@ -1,19 +1,19 @@
1
1
  import uniq from "lodash/uniq";
2
2
  import { encodeFunctionData, zeroAddress, zeroHash } from "viem";
3
- import ExchangeRouterAbi from "../abis/ExchangeRouter";
4
- import { abis } from "../abis/index";
5
- import ERC20ABI from "../abis/Token";
6
- import { getExcessiveExecutionFee, getHighExecutionFee } from "../configs/chains";
7
- import { getContract } from "../configs/contracts";
8
- import { convertTokenAddress, getToken, getWrappedToken, NATIVE_TOKEN_ADDRESS } from "../configs/tokens";
9
- import { DecreasePositionSwapType, OrderType } from "../types/orders";
10
- import { expandDecimals, MaxUint256, USD_DECIMALS } from "./numbers";
11
- import { getByKey } from "./objects";
12
- import { isIncreaseOrderType, isSwapOrderType } from "./orders";
13
- import { convertToContractPrice, convertToUsd } from "./tokens";
14
- import { applySlippageToMinOut, applySlippageToPrice } from "./trade";
15
- import { getTwapValidFromTime } from "./twap";
16
- import { createTwapUiFeeReceiver } from "./twap/uiFeeReceiver";
3
+ import ExchangeRouterAbi from "../../abis/ExchangeRouter";
4
+ import { abis } from "../../abis/index";
5
+ import ERC20ABI from "../../abis/Token";
6
+ import { getExcessiveExecutionFee, getHighExecutionFee } from "../../configs/chains";
7
+ import { getContract } from "../../configs/contracts";
8
+ import { convertTokenAddress, getToken, getWrappedToken, NATIVE_TOKEN_ADDRESS } from "../../configs/tokens";
9
+ import { expandDecimals, MaxUint256, USD_DECIMALS } from "../numbers";
10
+ import { getByKey } from "../objects";
11
+ import { isIncreaseOrderType, isSwapOrderType } from "../orders";
12
+ import { DecreasePositionSwapType, OrderType } from "../orders/types";
13
+ import { convertToContractPrice, convertToUsd } from "../tokens";
14
+ import { applySlippageToMinOut, applySlippageToPrice } from "../trade";
15
+ import { getTwapValidFromTime } from "../twap";
16
+ import { createTwapUiFeeReceiver } from "../twap/uiFeeReceiver";
17
17
  export function buildSwapOrderPayload(p) {
18
18
  const tokenTransfersParams = buildTokenTransfersParamsForIncreaseOrSwap(p);
19
19
  const orderPayload = {
@@ -0,0 +1,2 @@
1
+ export * from "./types";
2
+ export * from "./utils";
@@ -1,11 +1,11 @@
1
- import { BASIS_POINTS_DIVISOR_BIGINT, DEFAULT_ALLOWED_SWAP_SLIPPAGE_BPS } from "../configs/factors";
2
- import { OrderType, SwapPricingType, } from "../types/orders";
3
- import { getSwapPathOutputAddresses, getSwapPathStats } from "./swap/swapStats";
4
- import { bigMath } from "./bigmath";
5
- import { getByKey } from "./objects";
6
- import { getPositionKey, parsePositionKey } from "./positions";
7
- import { getOrderThresholdType } from "./prices";
8
- import { convertToTokenAmount, convertToUsd, getTokensRatioByAmounts, getTokensRatioByMinOutputAmountAndTriggerPrice, parseContractPrice, } from "./tokens";
1
+ import { BASIS_POINTS_DIVISOR_BIGINT, DEFAULT_ALLOWED_SWAP_SLIPPAGE_BPS } from "../../configs/factors";
2
+ import { getSwapPathOutputAddresses, getSwapPathStats } from "../swap/swapStats";
3
+ import { bigMath } from "../bigmath";
4
+ import { getByKey } from "../objects";
5
+ import { OrderType, SwapPricingType, } from "./types";
6
+ import { getPositionKey, parsePositionKey } from "../positions";
7
+ import { getOrderThresholdType } from "../prices";
8
+ import { convertToTokenAmount, convertToUsd, getTokensRatioByAmounts, getTokensRatioByMinOutputAmountAndTriggerPrice, parseContractPrice, } from "../tokens";
9
9
  export function isMarketOrderType(orderType) {
10
10
  return [OrderType.MarketDecrease, OrderType.MarketIncrease, OrderType.MarketSwap].includes(orderType);
11
11
  }
@@ -0,0 +1,2 @@
1
+ export * from "./types";
2
+ export * from "./utils";
@@ -1,9 +1,9 @@
1
- import { BASIS_POINTS_DIVISOR_BIGINT } from "../configs/factors";
2
- import { bigMath } from "./bigmath";
3
- import { capPositionImpactUsdByMaxImpactPool, capPositionImpactUsdByMaxPriceImpactFactor, getMaxPositionImpactFactors, getPositionFee, getPriceImpactForPosition, getProportionalPendingImpactValues, } from "./fees";
4
- import { getCappedPoolPnl, getMarketPnl, getOpenInterestUsd, getPoolUsdWithoutPnl } from "./markets";
5
- import { applyFactor, expandDecimals, PRECISION } from "./numbers";
6
- import { convertToUsd, getIsEquivalentTokens } from "./tokens";
1
+ import { BASIS_POINTS_DIVISOR_BIGINT } from "../../configs/factors";
2
+ import { bigMath } from "../bigmath";
3
+ import { capPositionImpactUsdByMaxImpactPool, capPositionImpactUsdByMaxPriceImpactFactor, getMaxPositionImpactFactors, getPositionFee, getPriceImpactForPosition, getProportionalPendingImpactValues, } from "../fees";
4
+ import { getCappedPoolPnl, getMarketPnl, getOpenInterestUsd, getPoolUsdWithoutPnl } from "../markets";
5
+ import { applyFactor, expandDecimals, PRECISION } from "../numbers";
6
+ import { convertToUsd, getIsEquivalentTokens } from "../tokens";
7
7
  export function getPositionKey(account, marketAddress, collateralAddress, isLong) {
8
8
  return `${account}:${marketAddress}:${collateralAddress}:${isLong}`;
9
9
  }
@@ -0,0 +1 @@
1
+ export * from "./utils";
@@ -1,10 +1,10 @@
1
- import { BASIS_POINTS_DIVISOR_BIGINT, DEFAULT_ACCEPTABLE_PRICE_IMPACT_BUFFER } from "../configs/factors";
2
- import { OrderType } from "../types/orders";
3
- import { TriggerThresholdType } from "../types/trade";
4
- import { bigMath } from "./bigmath";
5
- import { getCappedPositionImpactUsd, getPriceImpactByAcceptablePrice } from "./fees";
6
- import { expandDecimals, getBasisPoints, roundUpMagnitudeDivision } from "./numbers";
7
- import { convertToTokenAmount } from "./tokens";
1
+ import { BASIS_POINTS_DIVISOR_BIGINT, DEFAULT_ACCEPTABLE_PRICE_IMPACT_BUFFER } from "../../configs/factors";
2
+ import { bigMath } from "../bigmath";
3
+ import { getCappedPositionImpactUsd, getPriceImpactByAcceptablePrice } from "../fees";
4
+ import { expandDecimals, getBasisPoints, roundUpMagnitudeDivision } from "../numbers";
5
+ import { OrderType } from "../orders/types";
6
+ import { convertToTokenAmount } from "../tokens";
7
+ import { TriggerThresholdType } from "../trade/types";
8
8
  export function getMarkPrice(p) {
9
9
  const { prices, isIncrease, isLong } = p;
10
10
  const shouldUseMaxPrice = getShouldUseMaxPrice(isIncrease, isLong);
@@ -0,0 +1,2 @@
1
+ export * from "./types";
2
+ export * from "./utils";
@@ -0,0 +1 @@
1
+ export * from "./types";
@@ -35,12 +35,26 @@ describe("findSwapPathsBetweenTokens", () => {
35
35
  const result = findSwapPathsBetweenTokens(graph);
36
36
  expect(result).toEqual({
37
37
  BTC: {
38
+ BTC: [
39
+ ["ETH", "USDC"],
40
+ ["USDC", "ETH"],
41
+ ],
38
42
  ETH: [[], ["USDC"]],
39
43
  USDC: [[], ["ETH"]],
40
44
  },
41
45
  ETH: {
46
+ ETH: [
47
+ ["USDC", "BTC"],
48
+ ["BTC", "USDC"],
49
+ ],
42
50
  USDC: [[], ["BTC"]],
43
51
  },
52
+ USDC: {
53
+ USDC: [
54
+ ["ETH", "BTC"],
55
+ ["BTC", "ETH"],
56
+ ],
57
+ },
44
58
  });
45
59
  });
46
60
  it("should handle multiple markets between same tokens", () => {
@@ -55,8 +69,12 @@ describe("findSwapPathsBetweenTokens", () => {
55
69
  const result = findSwapPathsBetweenTokens(graph);
56
70
  expect(result).toEqual({
57
71
  ETH: {
72
+ ETH: [["USDC"]],
58
73
  USDC: [[], ["USDC", "ETH"]],
59
74
  },
75
+ USDC: {
76
+ USDC: [["ETH"]],
77
+ },
60
78
  });
61
79
  });
62
80
  it("should not include self-loops", () => {
@@ -137,12 +155,16 @@ describe("findSwapPathsBetweenTokens", () => {
137
155
  const result = findSwapPathsBetweenTokens(graph);
138
156
  expect(result).toEqual({
139
157
  A: {
158
+ A: [["USDC"]],
140
159
  B: [["USDC"]],
141
160
  USDC: [[], ["USDC", "A"]],
142
161
  },
143
162
  B: {
144
163
  USDC: [[], ["USDC", "A"]],
145
164
  },
165
+ USDC: {
166
+ USDC: [["A"]],
167
+ },
146
168
  });
147
169
  });
148
170
  });
@@ -1,8 +1,9 @@
1
1
  import { describe, expect, it } from "vitest";
2
2
  import { USD_DECIMALS } from "../../../configs/factors";
3
3
  import { NATIVE_TOKEN_ADDRESS } from "../../../configs/tokens";
4
- import { SwapPricingType } from "../../../types/orders";
5
4
  import { expandDecimals } from "../../numbers";
5
+ import { SwapPricingType } from "../../orders/types";
6
+ import { getSwapPathStats } from "..";
6
7
  import { buildMarketsAdjacencyGraph } from "../buildMarketsAdjacencyGraph";
7
8
  import { findSwapPathsBetweenTokens } from "../findSwapPathsBetweenTokens";
8
9
  import { createMarketEdgeLiquidityGetter, createNaiveNetworkEstimator, createNaiveSwapEstimator, getMaxLiquidityMarketSwapPathFromTokenSwapPaths, getNaiveBestMarketSwapPathsFromTokenSwapPaths, getTokenSwapPathsForTokenPair, } from "../swapRouting";
@@ -11,6 +12,7 @@ const marketKeys = [
11
12
  "ETH-ETH-USDC",
12
13
  "BTC-BTC-USDC",
13
14
  "BTC-BTC-DAI",
15
+ "BTC-SOL-DAI",
14
16
  "SOL-SOL-USDC",
15
17
  "SOL-ETH-USDC",
16
18
  "SOL-BTC-USDC",
@@ -432,3 +434,258 @@ describe("getMaxLiquidityMarketSwapPathFromTokenSwapPaths", () => {
432
434
  });
433
435
  });
434
436
  });
437
+ describe("same token arbitrage swaps", () => {
438
+ it("should yield profit (usdOut > usdIn) for USDC->USDC swap when markets have opposite imbalances", () => {
439
+ const USD_IN = expandDecimals(10000n, USD_DECIMALS);
440
+ const marketsInfoData = mockMarketsInfoData(tokensData, marketKeys, {
441
+ "ETH-ETH-USDC": {
442
+ longPoolAmount: usdToToken(30000, tokensData.ETH),
443
+ shortPoolAmount: usdToToken(170000, tokensData.USDC),
444
+ maxLongPoolAmount: usdToToken(1000000, tokensData.ETH),
445
+ maxShortPoolAmount: usdToToken(1000000, tokensData.USDC),
446
+ },
447
+ "SOL-ETH-USDC": {
448
+ longPoolAmount: usdToToken(170000, tokensData.ETH),
449
+ shortPoolAmount: usdToToken(30000, tokensData.USDC),
450
+ maxLongPoolAmount: usdToToken(1000000, tokensData.ETH),
451
+ maxShortPoolAmount: usdToToken(1000000, tokensData.USDC),
452
+ },
453
+ });
454
+ const tokenSwapPaths = getTokenSwapPathsForTokenPair(swapPaths, "USDC", "USDC");
455
+ const estimator = createNaiveSwapEstimator(marketsInfoData, SwapPricingType.Swap);
456
+ const networkEstimator = createNaiveNetworkEstimator({
457
+ chainId: 1,
458
+ gasLimits: {
459
+ ...baseGasLimits,
460
+ singleSwap: baseGasLimits.singleSwap * 3n,
461
+ },
462
+ gasPrice: baseGasPrice * 100n,
463
+ tokensData: {
464
+ ...tokensData,
465
+ [NATIVE_TOKEN_ADDRESS]: {
466
+ ...tokensData.ETH,
467
+ address: NATIVE_TOKEN_ADDRESS,
468
+ },
469
+ },
470
+ });
471
+ const paths = getNaiveBestMarketSwapPathsFromTokenSwapPaths({
472
+ graph: marketAdjacencyGraph,
473
+ tokenSwapPaths,
474
+ topPathsCount: 1,
475
+ tokenInAddress: "USDC",
476
+ tokenOutAddress: "USDC",
477
+ estimator,
478
+ usdIn: USD_IN,
479
+ networkEstimator,
480
+ });
481
+ const topPath = paths?.[0];
482
+ expect(topPath).toBeDefined();
483
+ expect(topPath?.length).toBe(2);
484
+ const swapPathStats = getSwapPathStats({
485
+ marketsInfoData,
486
+ swapPath: topPath,
487
+ initialCollateralAddress: "USDC",
488
+ wrappedNativeTokenAddress: "WETH",
489
+ usdIn: USD_IN,
490
+ shouldUnwrapNativeToken: false,
491
+ shouldApplyPriceImpact: true,
492
+ swapPricingType: SwapPricingType.Swap,
493
+ });
494
+ expect(swapPathStats).toBeDefined();
495
+ const { usdOut } = networkEstimator(swapPathStats.usdOut, topPath.length);
496
+ expect(usdOut).toBeGreaterThan(USD_IN);
497
+ });
498
+ it("should yield loss when markets are balanced", () => {
499
+ const USD_IN = expandDecimals(10000n, USD_DECIMALS);
500
+ const marketsInfoData = mockMarketsInfoData(tokensData, marketKeys, {
501
+ "ETH-ETH-USDC": {
502
+ longPoolAmount: usdToToken(100000, tokensData.ETH),
503
+ shortPoolAmount: usdToToken(100000, tokensData.USDC),
504
+ maxLongPoolAmount: usdToToken(1000000, tokensData.ETH),
505
+ maxShortPoolAmount: usdToToken(1000000, tokensData.USDC),
506
+ },
507
+ "SOL-ETH-USDC": {
508
+ longPoolAmount: usdToToken(100000, tokensData.ETH),
509
+ shortPoolAmount: usdToToken(100000, tokensData.USDC),
510
+ maxLongPoolAmount: usdToToken(1000000, tokensData.ETH),
511
+ maxShortPoolAmount: usdToToken(1000000, tokensData.USDC),
512
+ },
513
+ });
514
+ const tokenSwapPaths = getTokenSwapPathsForTokenPair(swapPaths, "USDC", "USDC");
515
+ const estimator = createNaiveSwapEstimator(marketsInfoData, SwapPricingType.Swap);
516
+ const networkEstimator = createNaiveNetworkEstimator({
517
+ chainId: 1,
518
+ gasLimits: baseGasLimits,
519
+ gasPrice: baseGasPrice,
520
+ tokensData: {
521
+ ...tokensData,
522
+ [NATIVE_TOKEN_ADDRESS]: {
523
+ ...tokensData.ETH,
524
+ address: NATIVE_TOKEN_ADDRESS,
525
+ },
526
+ },
527
+ });
528
+ const paths = getNaiveBestMarketSwapPathsFromTokenSwapPaths({
529
+ graph: marketAdjacencyGraph,
530
+ tokenSwapPaths,
531
+ topPathsCount: 1,
532
+ tokenInAddress: "USDC",
533
+ tokenOutAddress: "USDC",
534
+ estimator,
535
+ usdIn: USD_IN,
536
+ networkEstimator,
537
+ });
538
+ const topPath = paths?.[0];
539
+ expect(topPath).toBeDefined();
540
+ expect(topPath?.length).toBeGreaterThan(0);
541
+ const swapPathStats = getSwapPathStats({
542
+ marketsInfoData,
543
+ swapPath: topPath,
544
+ initialCollateralAddress: "USDC",
545
+ wrappedNativeTokenAddress: "WETH",
546
+ usdIn: USD_IN,
547
+ shouldUnwrapNativeToken: false,
548
+ shouldApplyPriceImpact: true,
549
+ swapPricingType: SwapPricingType.Swap,
550
+ });
551
+ expect(swapPathStats).toBeDefined();
552
+ const { usdOut } = networkEstimator(swapPathStats.usdOut, topPath.length);
553
+ expect(usdOut).toBeLessThan(USD_IN);
554
+ });
555
+ it("should find most profitable arbitrage path", () => {
556
+ const USD_IN = expandDecimals(10000n, USD_DECIMALS);
557
+ const marketsInfoData = mockMarketsInfoData(tokensData, marketKeys, {
558
+ // Path 1: ETH-ETH-USDC -> SOL-ETH-USDC (moderate profit)
559
+ "ETH-ETH-USDC": {
560
+ longPoolAmount: usdToToken(50000, tokensData.ETH),
561
+ shortPoolAmount: usdToToken(150000, tokensData.USDC),
562
+ maxLongPoolAmount: usdToToken(1000000, tokensData.ETH),
563
+ maxShortPoolAmount: usdToToken(1000000, tokensData.USDC),
564
+ },
565
+ "SOL-ETH-USDC": {
566
+ longPoolAmount: usdToToken(150000, tokensData.ETH),
567
+ shortPoolAmount: usdToToken(50000, tokensData.USDC),
568
+ maxLongPoolAmount: usdToToken(1000000, tokensData.ETH),
569
+ maxShortPoolAmount: usdToToken(1000000, tokensData.USDC),
570
+ },
571
+ // Path 2: BTC-BTC-USDC -> SOL-BTC-USDC (higher profit)
572
+ "BTC-BTC-USDC": {
573
+ longPoolAmount: usdToToken(30000, tokensData.BTC),
574
+ shortPoolAmount: usdToToken(170000, tokensData.USDC),
575
+ maxLongPoolAmount: usdToToken(1000000, tokensData.BTC),
576
+ maxShortPoolAmount: usdToToken(1000000, tokensData.USDC),
577
+ },
578
+ "SOL-BTC-USDC": {
579
+ longPoolAmount: usdToToken(170000, tokensData.BTC),
580
+ shortPoolAmount: usdToToken(30000, tokensData.USDC),
581
+ maxLongPoolAmount: usdToToken(1000000, tokensData.BTC),
582
+ maxShortPoolAmount: usdToToken(1000000, tokensData.USDC),
583
+ },
584
+ });
585
+ const tokenSwapPaths = getTokenSwapPathsForTokenPair(swapPaths, "USDC", "USDC");
586
+ const estimator = createNaiveSwapEstimator(marketsInfoData, SwapPricingType.Swap);
587
+ const networkEstimator = createNaiveNetworkEstimator({
588
+ chainId: 1,
589
+ gasLimits: baseGasLimits,
590
+ gasPrice: baseGasPrice,
591
+ tokensData: {
592
+ ...tokensData,
593
+ [NATIVE_TOKEN_ADDRESS]: {
594
+ ...tokensData.ETH,
595
+ address: NATIVE_TOKEN_ADDRESS,
596
+ },
597
+ },
598
+ });
599
+ const paths = getNaiveBestMarketSwapPathsFromTokenSwapPaths({
600
+ graph: marketAdjacencyGraph,
601
+ tokenSwapPaths,
602
+ topPathsCount: 1,
603
+ tokenInAddress: "USDC",
604
+ tokenOutAddress: "USDC",
605
+ estimator,
606
+ usdIn: USD_IN,
607
+ networkEstimator,
608
+ });
609
+ const topPath = paths?.[0];
610
+ expect(topPath).toBeDefined();
611
+ expect(topPath?.length).toBe(2);
612
+ const swapPathStats = getSwapPathStats({
613
+ marketsInfoData,
614
+ swapPath: topPath,
615
+ initialCollateralAddress: "USDC",
616
+ wrappedNativeTokenAddress: "WETH",
617
+ usdIn: USD_IN,
618
+ shouldUnwrapNativeToken: false,
619
+ shouldApplyPriceImpact: true,
620
+ swapPricingType: SwapPricingType.Swap,
621
+ });
622
+ expect(swapPathStats).toBeDefined();
623
+ const { usdOut } = networkEstimator(swapPathStats.usdOut, topPath.length);
624
+ expect(usdOut).toBeGreaterThan(USD_IN);
625
+ expect(topPath).toEqual(["SOL-BTC-USDC", "BTC-BTC-USDC"]);
626
+ });
627
+ it("should find longer arbitrage swap path with higher profit", () => {
628
+ const USD_IN = expandDecimals(5000n, USD_DECIMALS);
629
+ const marketsInfoData = mockMarketsInfoData(tokensData, marketKeys, {
630
+ // Path 1: USDC -> ETH -> USDC (2 markets, moderate profit)
631
+ "ETH-ETH-USDC": {
632
+ longPoolAmount: usdToToken(60000, tokensData.ETH),
633
+ shortPoolAmount: usdToToken(140000, tokensData.USDC),
634
+ maxLongPoolAmount: usdToToken(1000000, tokensData.ETH),
635
+ maxShortPoolAmount: usdToToken(1000000, tokensData.USDC),
636
+ },
637
+ "SOL-ETH-USDC": {
638
+ longPoolAmount: usdToToken(140000, tokensData.ETH),
639
+ shortPoolAmount: usdToToken(60000, tokensData.USDC),
640
+ maxLongPoolAmount: usdToToken(1000000, tokensData.ETH),
641
+ maxShortPoolAmount: usdToToken(1000000, tokensData.USDC),
642
+ },
643
+ // Path 2: USDC -> SOL -> DAI -> USDC (3 markets, higher profit)
644
+ "SOL-SOL-USDC": {
645
+ longPoolAmount: usdToToken(150000, tokensData.SOL),
646
+ shortPoolAmount: usdToToken(50000, tokensData.USDC),
647
+ maxLongPoolAmount: usdToToken(1000000, tokensData.SOL),
648
+ maxShortPoolAmount: usdToToken(1000000, tokensData.USDC),
649
+ },
650
+ "BTC-SOL-DAI": {
651
+ longPoolAmount: usdToToken(50000, tokensData.SOL),
652
+ shortPoolAmount: usdToToken(150000, tokensData.DAI),
653
+ maxLongPoolAmount: usdToToken(1000000, tokensData.SOL),
654
+ maxShortPoolAmount: usdToToken(1000000, tokensData.DAI),
655
+ },
656
+ "SPOT-DAI-USDC": {
657
+ longPoolAmount: usdToToken(50000, tokensData.DAI),
658
+ shortPoolAmount: usdToToken(150000, tokensData.USDC),
659
+ maxLongPoolAmount: usdToToken(1000000, tokensData.DAI),
660
+ maxShortPoolAmount: usdToToken(1000000, tokensData.USDC),
661
+ },
662
+ });
663
+ const tokenSwapPaths = getTokenSwapPathsForTokenPair(swapPaths, "USDC", "USDC");
664
+ const estimator = createNaiveSwapEstimator(marketsInfoData, SwapPricingType.Swap);
665
+ const networkEstimator = createNaiveNetworkEstimator({
666
+ chainId: 1,
667
+ gasLimits: baseGasLimits,
668
+ gasPrice: baseGasPrice,
669
+ tokensData: {
670
+ ...tokensData,
671
+ [NATIVE_TOKEN_ADDRESS]: {
672
+ ...tokensData.ETH,
673
+ address: NATIVE_TOKEN_ADDRESS,
674
+ },
675
+ },
676
+ });
677
+ const paths = getNaiveBestMarketSwapPathsFromTokenSwapPaths({
678
+ graph: marketAdjacencyGraph,
679
+ tokenSwapPaths,
680
+ topPathsCount: 1,
681
+ tokenInAddress: "USDC",
682
+ tokenOutAddress: "USDC",
683
+ estimator,
684
+ usdIn: USD_IN,
685
+ networkEstimator,
686
+ });
687
+ const topPath = paths?.[0];
688
+ expect(topPath).toBeDefined();
689
+ expect(topPath.length).toEqual(3);
690
+ });
691
+ });
@@ -1,7 +1,7 @@
1
1
  import { describe, expect, it } from "vitest";
2
2
  import { USD_DECIMALS } from "../../../configs/factors";
3
- import { SwapPricingType } from "../../../types/orders";
4
3
  import { bigMath } from "../../bigmath";
4
+ import { SwapPricingType } from "../../orders/types";
5
5
  import { mockMarketsInfoData, mockTokensData } from "../../../test/mock";
6
6
  import { buildMarketsAdjacencyGraph } from "../buildMarketsAdjacencyGraph";
7
7
  import { createMarketEdgeLiquidityGetter, createNaiveSwapEstimator, createSwapEstimator, getBestMarketForTokenEdge, getBestSwapPath, getMaxLiquidityMarketForTokenEdge, getMaxLiquidityMarketSwapPathFromTokenSwapPaths, getNaiveBestMarketSwapPathsFromTokenSwapPaths, } from "../swapRouting";
@@ -1,7 +1,7 @@
1
1
  import { describe, expect, it } from "vitest";
2
2
  import { USD_DECIMALS } from "../../../configs/factors";
3
3
  import { NATIVE_TOKEN_ADDRESS } from "../../../configs/tokens";
4
- import { SwapPricingType } from "../../../types/orders";
4
+ import { SwapPricingType } from "../../orders/types";
5
5
  import { mockMarketsInfoData as createMockMarketsInfoData, mockTokensData, usdToToken } from "../../../test/mock";
6
6
  import { getSwapPathOutputAddresses, getSwapPathStats } from "../swapStats";
7
7
  const someWrappedToken = "0x0000000000000000000000000000000000000001";
@@ -2,11 +2,11 @@ import { encodeFunctionData } from "viem";
2
2
  import StBTCABI from "../../abis/StBTC";
3
3
  import { BOTANIX } from "../../configs/chains";
4
4
  import { getContract } from "../../configs/contracts";
5
- import { ExternalSwapAggregator } from "../../types/trade";
6
5
  import { bigMath } from "../bigmath";
7
6
  import { BASIS_POINTS_DIVISOR_BIGINT, getBasisPoints } from "../numbers";
8
7
  import { AVAILABLE_BOTANIX_DEPOSIT_PAIRS, AVAILABLE_BOTANIX_WITHDRAW_PAIRS } from "./externalSwapPath";
9
8
  import { convertToUsd, getMidPrice, getTokenData } from "../tokens";
9
+ import { ExternalSwapAggregator } from "../trade/types";
10
10
  const COEF_REDUCER = getBasisPoints(1n, 10000n);
11
11
  export const getBotanixStakingExternalSwapQuote = ({ tokenInAddress, tokenOutAddress, amountIn, gasPrice, receiverAddress, tokensData, assetsPerShare, }) => {
12
12
  const inTokenData = getTokenData(tokensData, tokenInAddress);
@@ -1,9 +1,9 @@
1
- import { SwapPricingType } from "../../types/orders";
2
1
  import { bigMath } from "../bigmath";
3
- import { convertToTokenAmount, convertToUsd, getIsEquivalentTokens, getIsStake, getIsUnstake, getMidPrice, } from "../tokens";
2
+ import { convertToTokenAmount, convertToUsd, getIsEquivalentTokens, getIsStake, getIsUnstake, getIsUnwrap, getIsWrap, getMidPrice, } from "../tokens";
4
3
  import { getAvailableExternalSwapPaths } from "./externalSwapPath";
5
4
  import { getExternalSwapQuoteByPath } from "./externalSwapQuoteByPath";
6
5
  import { createFindSwapPath } from "./swapPath";
6
+ import { SwapPricingType } from "../orders/types";
7
7
  /*
8
8
  Order/Priority of getting swap strategy:
9
9
  1. Check if it needs a swap and return noSwap if tokens are equivalent, stake or unstake [noSwap]
@@ -11,7 +11,7 @@ import { createFindSwapPath } from "./swapPath";
11
11
  3. Check if there is a combined swap strategy and return combinedSwap if there is [combinedSwap]
12
12
  4. Return defaultSwapStrategy (noSwap) if there is no other swap strategy [noSwap]
13
13
  */
14
- export function buildSwapStrategy({ amountIn, tokenIn, tokenOut, marketsInfoData, chainId, swapOptimizationOrder, externalSwapQuoteParams, swapPricingType = SwapPricingType.Swap, }) {
14
+ export function buildSwapStrategy({ amountIn, tokenIn, tokenOut, marketsInfoData, chainId, swapOptimizationOrder, externalSwapQuoteParams, swapPricingType = SwapPricingType.Swap, allowSameTokenSwap, }) {
15
15
  const priceIn = tokenIn.prices.minPrice;
16
16
  const usdIn = convertToUsd(amountIn, tokenIn.decimals, priceIn);
17
17
  if (amountIn < 0n) {
@@ -29,7 +29,11 @@ export function buildSwapStrategy({ amountIn, tokenIn, tokenOut, marketsInfoData
29
29
  priceOut: tokenOut.prices.maxPrice,
30
30
  feesUsd: 0n,
31
31
  };
32
- if (getIsEquivalentTokens(tokenIn, tokenOut) || getIsStake(tokenIn, tokenOut) || getIsUnstake(tokenIn, tokenOut)) {
32
+ if ((!allowSameTokenSwap && getIsEquivalentTokens(tokenIn, tokenOut)) ||
33
+ getIsWrap(tokenIn, tokenOut) ||
34
+ getIsUnwrap(tokenIn, tokenOut) ||
35
+ getIsStake(tokenIn, tokenOut) ||
36
+ getIsUnstake(tokenIn, tokenOut)) {
33
37
  return defaultSwapStrategy;
34
38
  }
35
39
  const findSwapPath = createFindSwapPath({
@@ -100,7 +104,7 @@ export function buildSwapStrategy({ amountIn, tokenIn, tokenOut, marketsInfoData
100
104
  return defaultSwapStrategy;
101
105
  }
102
106
  // Used for getting swap amounts by to value
103
- export function buildReverseSwapStrategy({ amountOut, tokenIn, tokenOut, marketsInfoData, chainId, externalSwapQuoteParams, swapOptimizationOrder, swapPricingType, }) {
107
+ export function buildReverseSwapStrategy({ amountOut, tokenIn, tokenOut, marketsInfoData, chainId, externalSwapQuoteParams, swapOptimizationOrder, swapPricingType, allowSameTokenSwap, }) {
104
108
  const priceIn = getMidPrice(tokenIn.prices);
105
109
  const priceOut = getMidPrice(tokenOut.prices);
106
110
  const preferredUsdOut = convertToUsd(amountOut, tokenOut.decimals, getMidPrice(tokenOut.prices));
@@ -118,7 +122,11 @@ export function buildReverseSwapStrategy({ amountOut, tokenIn, tokenOut, markets
118
122
  priceOut,
119
123
  feesUsd: 0n,
120
124
  };
121
- if (getIsEquivalentTokens(tokenIn, tokenOut) || getIsStake(tokenIn, tokenOut) || getIsUnstake(tokenIn, tokenOut)) {
125
+ if ((!allowSameTokenSwap && getIsEquivalentTokens(tokenIn, tokenOut)) ||
126
+ getIsWrap(tokenIn, tokenOut) ||
127
+ getIsUnwrap(tokenIn, tokenOut) ||
128
+ getIsStake(tokenIn, tokenOut) ||
129
+ getIsUnstake(tokenIn, tokenOut)) {
122
130
  return defaultSwapStrategy;
123
131
  }
124
132
  const findSwapPath = createFindSwapPath({
@@ -1,6 +1,6 @@
1
1
  import { BOTANIX } from "../../configs/chains";
2
2
  import { NATIVE_TOKEN_ADDRESS, getTokenBySymbol } from "../../configs/tokens";
3
- import { ExternalSwapAggregator } from "../../types/trade";
3
+ import { ExternalSwapAggregator } from "../trade/types";
4
4
  const BBTC_ADDRESS = NATIVE_TOKEN_ADDRESS;
5
5
  const PBTC_ADDRESS = getTokenBySymbol(BOTANIX, "PBTC").address;
6
6
  const STBTC_ADDRESS = getTokenBySymbol(BOTANIX, "STBTC").address;
@@ -1,5 +1,5 @@
1
- import { ExternalSwapAggregator } from "../../types/trade";
2
1
  import { getBotanixStakingExternalSwapQuote } from "./botanixStaking";
2
+ import { ExternalSwapAggregator } from "../trade/types";
3
3
  export const getExternalSwapQuoteByPath = ({ amountIn, externalSwapPath, externalSwapQuoteParams, }) => {
4
4
  if (amountIn === undefined ||
5
5
  externalSwapQuoteParams.gasPrice === undefined ||
@@ -7,7 +7,7 @@ export function findSwapPathsBetweenTokens(graph) {
7
7
  swapRoutes[tokenAAddress] = {};
8
8
  let empty = true;
9
9
  for (const tokenBAddress of allTokens) {
10
- if (tokenAAddress === tokenBAddress || swapRoutes[tokenBAddress]?.[tokenAAddress]) {
10
+ if (swapRoutes[tokenBAddress]?.[tokenAAddress]) {
11
11
  continue;
12
12
  }
13
13
  const result = {};
@@ -43,9 +43,13 @@ export function findSwapPathsBetweenTokens(graph) {
43
43
  }
44
44
  if (currentToken === tokenBAddress) {
45
45
  const intermediateTokenPath = tokenPath.slice(1, -1);
46
- const pathKey = intermediateTokenPath.join(",");
47
- if (!result[pathKey]) {
48
- result[pathKey] = intermediateTokenPath;
46
+ const isValidSameTokenSwap = tokenAAddress === tokenBAddress && intermediateTokenPath.length > 0;
47
+ const isValidDifferentTokenSwap = tokenAAddress !== tokenBAddress;
48
+ if (isValidSameTokenSwap || isValidDifferentTokenSwap) {
49
+ const pathKey = intermediateTokenPath.join(",");
50
+ if (!result[pathKey]) {
51
+ result[pathKey] = intermediateTokenPath;
52
+ }
49
53
  }
50
54
  }
51
55
  if (tokenPath.length >= MAX_EDGE_PATH_LENGTH + 1) {
@@ -1,2 +1,14 @@
1
+ export * from "./types";
2
+ export * from "./swapRouting";
1
3
  export * from "./swapStats";
4
+ export * from "./swapPath";
5
+ export * from "./buildSwapStrategy";
6
+ export * from "./findSwapPathsBetweenTokens";
7
+ export * from "./findReachableTokens";
8
+ export * from "./buildMarketsAdjacencyGraph";
2
9
  export * from "./swapValues";
10
+ export * from "./preparedSwapData";
11
+ export * from "./externalSwapPath";
12
+ export * from "./externalSwapQuoteByPath";
13
+ export * from "./botanixStaking";
14
+ export * from "./constants";
@@ -1,11 +1,11 @@
1
1
  import { MARKETS } from "../../configs/markets";
2
2
  import { convertTokenAddress, getWrappedToken, NATIVE_TOKEN_ADDRESS } from "../../configs/tokens";
3
- import { SwapPricingType } from "../../types/orders";
4
3
  import { LRUCache } from "../LruCache";
5
4
  import { getIsMarketAvailableForExpressSwaps } from "../markets";
6
5
  import { buildMarketsAdjacencyGraph } from "./buildMarketsAdjacencyGraph";
7
6
  import { createMarketEdgeLiquidityGetter, createNaiveNetworkEstimator, createNaiveSwapEstimator, createSwapEstimator, getBestSwapPath, getMarketAdjacencyGraph, getMaxLiquidityMarketSwapPathFromTokenSwapPaths, getNaiveBestMarketSwapPathsFromTokenSwapPaths, getTokenSwapPathsForTokenPairPrebuilt, marketRouteToMarketEdges, } from "./swapRouting";
8
7
  import { getSwapPathStats } from "./swapStats";
8
+ import { SwapPricingType } from "../orders/types";
9
9
  export const getWrappedAddress = (chainId, address) => {
10
10
  return address ? convertTokenAddress(chainId, address, "wrapped") : undefined;
11
11
  };