@paraswap/dex-lib 4.8.37-native-dex-math.1 → 4.8.37

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 (305) hide show
  1. package/build/abi/pancakeswap-infinity/cl-pool-manager.json +80 -0
  2. package/build/dex/aave-gsm/config.js +2 -2
  3. package/build/dex/fluid-dex/scripts/measure-calc-time.js +178 -0
  4. package/build/dex/fluid-dex/scripts/measure-calc-time.js.map +1 -0
  5. package/build/dex/idex.d.ts +1 -1
  6. package/build/dex/index.js +2 -0
  7. package/build/dex/index.js.map +1 -1
  8. package/build/dex/{infusion → pancakeswap-infinity}/config.d.ts +2 -2
  9. package/build/dex/pancakeswap-infinity/config.js +14 -0
  10. package/build/dex/pancakeswap-infinity/config.js.map +1 -0
  11. package/build/dex/pancakeswap-infinity/encoder.d.ts +4 -0
  12. package/build/dex/pancakeswap-infinity/encoder.js +218 -0
  13. package/build/dex/pancakeswap-infinity/encoder.js.map +1 -0
  14. package/build/dex/pancakeswap-infinity/pancakeswap-infinity.d.ts +21 -0
  15. package/build/dex/pancakeswap-infinity/pancakeswap-infinity.js +102 -0
  16. package/build/dex/pancakeswap-infinity/pancakeswap-infinity.js.map +1 -0
  17. package/build/dex/pancakeswap-infinity/subgraph.d.ts +7 -0
  18. package/build/dex/pancakeswap-infinity/subgraph.js +64 -0
  19. package/build/dex/pancakeswap-infinity/subgraph.js.map +1 -0
  20. package/build/dex/pancakeswap-infinity/types.d.ts +39 -0
  21. package/build/dex/pancakeswap-infinity/types.js.map +1 -0
  22. package/build/dex/pancakeswap-v3/pancakeswap-v3-pool.d.ts +0 -2
  23. package/build/dex/pancakeswap-v3/pancakeswap-v3-pool.js +0 -5
  24. package/build/dex/pancakeswap-v3/pancakeswap-v3-pool.js.map +1 -1
  25. package/build/dex/pancakeswap-v3/pancakeswap-v3.d.ts +3 -6
  26. package/build/dex/pancakeswap-v3/pancakeswap-v3.js +9 -85
  27. package/build/dex/pancakeswap-v3/pancakeswap-v3.js.map +1 -1
  28. package/build/dex/solidly-v3/solidly-v3-pool.d.ts +0 -3
  29. package/build/dex/solidly-v3/solidly-v3-pool.js +0 -8
  30. package/build/dex/solidly-v3/solidly-v3-pool.js.map +1 -1
  31. package/build/dex/solidly-v3/solidly-v3.d.ts +2 -5
  32. package/build/dex/solidly-v3/solidly-v3.js +8 -84
  33. package/build/dex/solidly-v3/solidly-v3.js.map +1 -1
  34. package/build/dex/uniswap-v3/scripts/measure-calc-time.js +110 -222
  35. package/build/dex/uniswap-v3/scripts/measure-calc-time.js.map +1 -1
  36. package/build/dex/uniswap-v3/types.d.ts +0 -1
  37. package/build/dex/uniswap-v3/types.js.map +1 -1
  38. package/build/dex/uniswap-v3/uniswap-v3-pool.d.ts +0 -2
  39. package/build/dex/uniswap-v3/uniswap-v3-pool.js +0 -5
  40. package/build/dex/uniswap-v3/uniswap-v3-pool.js.map +1 -1
  41. package/build/dex/uniswap-v3/uniswap-v3.d.ts +3 -11
  42. package/build/dex/uniswap-v3/uniswap-v3.js +9 -86
  43. package/build/dex/uniswap-v3/uniswap-v3.js.map +1 -1
  44. package/build/dex/uniswap-v4/api-go/compare-pricing.d.ts +1 -0
  45. package/build/dex/uniswap-v4/api-go/compare-pricing.js +187 -0
  46. package/build/dex/uniswap-v4/api-go/compare-pricing.js.map +1 -0
  47. package/build/dex/uniswap-v4/api-go/compare-states.d.ts +1 -0
  48. package/build/dex/uniswap-v4/api-go/compare-states.js +149 -0
  49. package/build/dex/uniswap-v4/api-go/compare-states.js.map +1 -0
  50. package/build/dex/uniswap-v4/api-go/fetch-pool-key.d.ts +0 -0
  51. package/build/dex/uniswap-v4/api-go/fetch-pool-key.js +140 -0
  52. package/build/dex/uniswap-v4/api-go/fetch-pool-key.js.map +1 -0
  53. package/build/dex/uniswap-v4/types.d.ts +0 -1
  54. package/build/dex/uniswap-v4/uniswap-v4-pool-manager.d.ts +0 -2
  55. package/build/dex/uniswap-v4/uniswap-v4-pool-manager.js +0 -3
  56. package/build/dex/uniswap-v4/uniswap-v4-pool-manager.js.map +1 -1
  57. package/build/dex/uniswap-v4/uniswap-v4-pool.d.ts +0 -3
  58. package/build/dex/uniswap-v4/uniswap-v4-pool.js +0 -18
  59. package/build/dex/uniswap-v4/uniswap-v4-pool.js.map +1 -1
  60. package/build/dex/uniswap-v4/uniswap-v4.d.ts +1 -3
  61. package/build/dex/uniswap-v4/uniswap-v4.js +14 -74
  62. package/build/dex/uniswap-v4/uniswap-v4.js.map +1 -1
  63. package/build/pricing-helper.d.ts +1 -1
  64. package/build/pricing-helper.js +2 -2
  65. package/build/pricing-helper.js.map +1 -1
  66. package/package.json +3 -3
  67. package/build/abi/BProtocol.json +0 -1155
  68. package/build/abi/Jarvis.json +0 -1172
  69. package/build/abi/MStableAsset.json +0 -1545
  70. package/build/abi/OneInchLp.json +0 -1304
  71. package/build/abi/Onebit.json +0 -736
  72. package/build/abi/Shell.json +0 -1294
  73. package/build/abi/TraderJoeV2Router.json +0 -50
  74. package/build/abi/idle-dao/idle-cdo-factory.json +0 -38
  75. package/build/abi/idle-dao/idle-cdo.json +0 -1245
  76. package/build/abi/infusion/InfusionFactory.json +0 -147
  77. package/build/abi/infusion/InfusionPair.json +0 -658
  78. package/build/abi/infusion/InfusionRouter.json +0 -442
  79. package/build/abi/maker-psm/pot.json +0 -322
  80. package/build/abi/maker-psm/psm.json +0 -243
  81. package/build/abi/maker-psm/vat.json +0 -363
  82. package/build/abi/nomiswap-v2/nomiswap-v2-pool.json +0 -773
  83. package/build/abi/quick-perps/fast-price-events.json +0 -70
  84. package/build/abi/quick-perps/fast-price-feed.json +0 -741
  85. package/build/abi/quick-perps/reader.json +0 -313
  86. package/build/abi/quick-perps/vault-price-feed.json +0 -323
  87. package/build/abi/quick-perps/vault.json +0 -1953
  88. package/build/abi/uniswap-v2/excalibur-pool.json +0 -881
  89. package/build/abi/uniswap-v2/mdex-factory.json +0 -759
  90. package/build/abi/wUSDM.json +0 -757
  91. package/build/abi/zrx.v2.json +0 -1967
  92. package/build/abi/zrx.v3.json +0 -3454
  93. package/build/abi/zrx.v4.json +0 -2193
  94. package/build/dex/OneInchLp.d.ts +0 -24
  95. package/build/dex/OneInchLp.js +0 -43
  96. package/build/dex/OneInchLp.js.map +0 -1
  97. package/build/dex/bProtocol/bProtocol.d.ts +0 -15
  98. package/build/dex/bProtocol/bProtocol.js +0 -56
  99. package/build/dex/bProtocol/bProtocol.js.map +0 -1
  100. package/build/dex/bProtocol/types.d.ts +0 -11
  101. package/build/dex/bProtocol/types.js +0 -8
  102. package/build/dex/bProtocol/types.js.map +0 -1
  103. package/build/dex/ekubo/pools/base-pool.d.ts +0 -47
  104. package/build/dex/ekubo/pools/base-pool.js +0 -184
  105. package/build/dex/ekubo/pools/base-pool.js.map +0 -1
  106. package/build/dex/ekubo/pools/iface.d.ts +0 -46
  107. package/build/dex/ekubo/pools/iface.js +0 -75
  108. package/build/dex/ekubo/pools/iface.js.map +0 -1
  109. package/build/dex/ekubo/pools/math/price.d.ts +0 -7
  110. package/build/dex/ekubo/pools/math/price.js +0 -112
  111. package/build/dex/ekubo/pools/math/price.js.map +0 -1
  112. package/build/dex/ekubo/pools/oracle-pool.d.ts +0 -10
  113. package/build/dex/ekubo/pools/oracle-pool.js +0 -19
  114. package/build/dex/ekubo/pools/oracle-pool.js.map +0 -1
  115. package/build/dex/ekubo/pools/pool-utils.d.ts +0 -44
  116. package/build/dex/ekubo/pools/pool-utils.js +0 -240
  117. package/build/dex/ekubo/pools/pool-utils.js.map +0 -1
  118. package/build/dex/idle-dao/config.d.ts +0 -5
  119. package/build/dex/idle-dao/config.js +0 -24
  120. package/build/dex/idle-dao/config.js.map +0 -1
  121. package/build/dex/idle-dao/idle-dao.d.ts +0 -42
  122. package/build/dex/idle-dao/idle-dao.js +0 -279
  123. package/build/dex/idle-dao/idle-dao.js.map +0 -1
  124. package/build/dex/idle-dao/token_list.d.ts +0 -4
  125. package/build/dex/idle-dao/token_list.js +0 -289
  126. package/build/dex/idle-dao/token_list.js.map +0 -1
  127. package/build/dex/idle-dao/tokens.d.ts +0 -9
  128. package/build/dex/idle-dao/tokens.js +0 -68
  129. package/build/dex/idle-dao/tokens.js.map +0 -1
  130. package/build/dex/idle-dao/types.d.ts +0 -35
  131. package/build/dex/idle-dao/types.js +0 -11
  132. package/build/dex/idle-dao/types.js.map +0 -1
  133. package/build/dex/idle-dao/utils.d.ts +0 -8
  134. package/build/dex/idle-dao/utils.js +0 -149
  135. package/build/dex/idle-dao/utils.js.map +0 -1
  136. package/build/dex/infusion/config.js +0 -20
  137. package/build/dex/infusion/config.js.map +0 -1
  138. package/build/dex/infusion/infusion-stable-pool.d.ts +0 -4
  139. package/build/dex/infusion/infusion-stable-pool.js +0 -74
  140. package/build/dex/infusion/infusion-stable-pool.js.map +0 -1
  141. package/build/dex/infusion/infusion.d.ts +0 -51
  142. package/build/dex/infusion/infusion.js +0 -500
  143. package/build/dex/infusion/infusion.js.map +0 -1
  144. package/build/dex/infusion/types.d.ts +0 -45
  145. package/build/dex/infusion/types.js.map +0 -1
  146. package/build/dex/infusion/utils/isStablePair.d.ts +0 -2
  147. package/build/dex/infusion/utils/isStablePair.js +0 -18
  148. package/build/dex/infusion/utils/isStablePair.js.map +0 -1
  149. package/build/dex/jarvis.d.ts +0 -56
  150. package/build/dex/jarvis.js +0 -163
  151. package/build/dex/jarvis.js.map +0 -1
  152. package/build/dex/mStable.d.ts +0 -44
  153. package/build/dex/mStable.js +0 -75
  154. package/build/dex/mStable.js.map +0 -1
  155. package/build/dex/maker-psm/config.d.ts +0 -11
  156. package/build/dex/maker-psm/config.js +0 -53
  157. package/build/dex/maker-psm/config.js.map +0 -1
  158. package/build/dex/maker-psm/maker-psm.d.ts +0 -103
  159. package/build/dex/maker-psm/maker-psm.js +0 -493
  160. package/build/dex/maker-psm/maker-psm.js.map +0 -1
  161. package/build/dex/maker-psm/types.d.ts +0 -39
  162. package/build/dex/maker-psm/types.js +0 -3
  163. package/build/dex/maker-psm/types.js.map +0 -1
  164. package/build/dex/onebit/onebit.d.ts +0 -16
  165. package/build/dex/onebit/onebit.js +0 -61
  166. package/build/dex/onebit/onebit.js.map +0 -1
  167. package/build/dex/onebit/types.d.ts +0 -14
  168. package/build/dex/onebit/types.js +0 -8
  169. package/build/dex/onebit/types.js.map +0 -1
  170. package/build/dex/quick-perps/config.d.ts +0 -11
  171. package/build/dex/quick-perps/config.js +0 -27
  172. package/build/dex/quick-perps/config.js.map +0 -1
  173. package/build/dex/quick-perps/fast-price-feed.d.ts +0 -26
  174. package/build/dex/quick-perps/fast-price-feed.js +0 -184
  175. package/build/dex/quick-perps/fast-price-feed.js.map +0 -1
  176. package/build/dex/quick-perps/pool.d.ts +0 -21
  177. package/build/dex/quick-perps/pool.js +0 -229
  178. package/build/dex/quick-perps/pool.js.map +0 -1
  179. package/build/dex/quick-perps/quick-perps.d.ts +0 -53
  180. package/build/dex/quick-perps/quick-perps.js +0 -247
  181. package/build/dex/quick-perps/quick-perps.js.map +0 -1
  182. package/build/dex/quick-perps/types.d.ts +0 -98
  183. package/build/dex/quick-perps/types.js +0 -3
  184. package/build/dex/quick-perps/types.js.map +0 -1
  185. package/build/dex/quick-perps/usdq.d.ts +0 -15
  186. package/build/dex/quick-perps/usdq.js +0 -62
  187. package/build/dex/quick-perps/usdq.js.map +0 -1
  188. package/build/dex/quick-perps/vault-price-feed.d.ts +0 -43
  189. package/build/dex/quick-perps/vault-price-feed.js +0 -203
  190. package/build/dex/quick-perps/vault-price-feed.js.map +0 -1
  191. package/build/dex/quick-perps/vault-utils.d.ts +0 -8
  192. package/build/dex/quick-perps/vault-utils.js +0 -42
  193. package/build/dex/quick-perps/vault-utils.js.map +0 -1
  194. package/build/dex/quick-perps/vault.d.ts +0 -46
  195. package/build/dex/quick-perps/vault.js +0 -182
  196. package/build/dex/quick-perps/vault.js.map +0 -1
  197. package/build/dex/se-vlr/config.d.ts +0 -3
  198. package/build/dex/se-vlr/config.js +0 -24
  199. package/build/dex/se-vlr/config.js.map +0 -1
  200. package/build/dex/se-vlr/se-vlr-pool.d.ts +0 -39
  201. package/build/dex/se-vlr/se-vlr-pool.js +0 -70
  202. package/build/dex/se-vlr/se-vlr-pool.js.map +0 -1
  203. package/build/dex/se-vlr/se-vlr.d.ts +0 -35
  204. package/build/dex/se-vlr/se-vlr.js +0 -131
  205. package/build/dex/se-vlr/se-vlr.js.map +0 -1
  206. package/build/dex/se-vlr/types.d.ts +0 -6
  207. package/build/dex/se-vlr/types.js +0 -3
  208. package/build/dex/se-vlr/types.js.map +0 -1
  209. package/build/dex/shell.d.ts +0 -25
  210. package/build/dex/shell.js +0 -41
  211. package/build/dex/shell.js.map +0 -1
  212. package/build/dex/solidly/forks-override/aerodrome.d.ts +0 -14
  213. package/build/dex/solidly/forks-override/aerodrome.js +0 -46
  214. package/build/dex/solidly/forks-override/aerodrome.js.map +0 -1
  215. package/build/dex/solidly/forks-override/chronos.d.ts +0 -23
  216. package/build/dex/solidly/forks-override/chronos.js +0 -141
  217. package/build/dex/solidly/forks-override/chronos.js.map +0 -1
  218. package/build/dex/solidly/forks-override/usdfi.d.ts +0 -8
  219. package/build/dex/solidly/forks-override/usdfi.js +0 -15
  220. package/build/dex/solidly/forks-override/usdfi.js.map +0 -1
  221. package/build/dex/solidly/forks-override/velocimeter.d.ts +0 -23
  222. package/build/dex/solidly/forks-override/velocimeter.js +0 -77
  223. package/build/dex/solidly/forks-override/velocimeter.js.map +0 -1
  224. package/build/dex/trader-joe-v2.d.ts +0 -40
  225. package/build/dex/trader-joe-v2.js +0 -74
  226. package/build/dex/trader-joe-v2.js.map +0 -1
  227. package/build/dex/uniswap-v2/dfyn.d.ts +0 -19
  228. package/build/dex/uniswap-v2/dfyn.js +0 -61
  229. package/build/dex/uniswap-v2/dfyn.js.map +0 -1
  230. package/build/dex/uniswap-v2/excalibur.d.ts +0 -24
  231. package/build/dex/uniswap-v2/excalibur.js +0 -47
  232. package/build/dex/uniswap-v2/excalibur.js.map +0 -1
  233. package/build/dex/uniswap-v2/mdex.d.ts +0 -24
  234. package/build/dex/uniswap-v2/mdex.js +0 -50
  235. package/build/dex/uniswap-v2/mdex.js.map +0 -1
  236. package/build/dex/uniswap-v2/nomiswap-v2.d.ts +0 -24
  237. package/build/dex/uniswap-v2/nomiswap-v2.js +0 -57
  238. package/build/dex/uniswap-v2/nomiswap-v2.js.map +0 -1
  239. package/build/dex/uniswap-v3/scripts/bench-all-dexes.js +0 -160
  240. package/build/dex/uniswap-v3/scripts/bench-all-dexes.js.map +0 -1
  241. package/build/dex/uniswap-v3/uniswap-v3-new.d.ts +0 -94
  242. package/build/dex/uniswap-v3/uniswap-v3-new.js +0 -923
  243. package/build/dex/uniswap-v3/uniswap-v3-new.js.map +0 -1
  244. package/build/dex/uniswap-v4/contract-math/Position.d.ts +0 -3
  245. package/build/dex/uniswap-v4/contract-math/Position.js +0 -10
  246. package/build/dex/uniswap-v4/contract-math/Position.js.map +0 -1
  247. package/build/dex/usual-bond/config.d.ts +0 -3
  248. package/build/dex/usual-bond/config.js +0 -13
  249. package/build/dex/usual-bond/config.js.map +0 -1
  250. package/build/dex/usual-bond/types.d.ts +0 -7
  251. package/build/dex/usual-bond/types.js +0 -3
  252. package/build/dex/usual-bond/types.js.map +0 -1
  253. package/build/dex/usual-bond/usual-bond.d.ts +0 -35
  254. package/build/dex/usual-bond/usual-bond.js +0 -176
  255. package/build/dex/usual-bond/usual-bond.js.map +0 -1
  256. package/build/dex/wusdm/config.d.ts +0 -3
  257. package/build/dex/wusdm/config.js +0 -43
  258. package/build/dex/wusdm/config.js.map +0 -1
  259. package/build/dex/wusdm/constants.d.ts +0 -2
  260. package/build/dex/wusdm/constants.js +0 -6
  261. package/build/dex/wusdm/constants.js.map +0 -1
  262. package/build/dex/wusdm/types.d.ts +0 -18
  263. package/build/dex/wusdm/types.js +0 -11
  264. package/build/dex/wusdm/types.js.map +0 -1
  265. package/build/dex/wusdm/wusdm-pool.d.ts +0 -21
  266. package/build/dex/wusdm/wusdm-pool.js +0 -74
  267. package/build/dex/wusdm/wusdm-pool.js.map +0 -1
  268. package/build/dex/wusdm/wusdm.d.ts +0 -46
  269. package/build/dex/wusdm/wusdm.js +0 -243
  270. package/build/dex/wusdm/wusdm.js.map +0 -1
  271. package/build/dex/zerox/config.d.ts +0 -2
  272. package/build/dex/zerox/config.js +0 -33
  273. package/build/dex/zerox/config.js.map +0 -1
  274. package/build/dex/zerox/index.d.ts +0 -22
  275. package/build/dex/zerox/index.js +0 -225
  276. package/build/dex/zerox/index.js.map +0 -1
  277. package/build/dex/zerox/order.d.ts +0 -88
  278. package/build/dex/zerox/order.js +0 -53
  279. package/build/dex/zerox/order.js.map +0 -1
  280. package/build/dex/zerox/types.d.ts +0 -73
  281. package/build/dex/zerox/types.js +0 -21
  282. package/build/dex/zerox/types.js.map +0 -1
  283. package/native/Cargo.lock +0 -331
  284. package/native/Cargo.toml +0 -22
  285. package/native/build.rs +0 -5
  286. package/native/package-lock.json +0 -32
  287. package/native/package.json +0 -20
  288. package/native/src/config.rs +0 -73
  289. package/native/src/lib.rs +0 -528
  290. package/native/src/math/bit_math.rs +0 -177
  291. package/native/src/math/full_math.rs +0 -217
  292. package/native/src/math/liquidity_math.rs +0 -72
  293. package/native/src/math/mod.rs +0 -10
  294. package/native/src/math/oracle.rs +0 -493
  295. package/native/src/math/sqrt_price_math.rs +0 -272
  296. package/native/src/math/swap_math.rs +0 -306
  297. package/native/src/math/tick.rs +0 -239
  298. package/native/src/math/tick_bitmap.rs +0 -312
  299. package/native/src/math/tick_math.rs +0 -321
  300. package/native/src/math/unsafe_math.rs +0 -67
  301. package/native/src/pool_state.rs +0 -41
  302. package/native/src/query_outputs.rs +0 -379
  303. package/native/src/v4_query_outputs.rs +0 -255
  304. /package/build/dex/{uniswap-v3/scripts/bench-all-dexes.d.ts → fluid-dex/scripts/measure-calc-time.d.ts} +0 -0
  305. /package/build/dex/{infusion → pancakeswap-infinity}/types.js +0 -0
@@ -1,272 +0,0 @@
1
- use ethnum::{I256, U256};
2
- use super::full_math;
3
- use super::unsafe_math;
4
-
5
- const RESOLUTION: u32 = 96;
6
- const Q96: U256 = U256::from_words(0, 1u128 << 96);
7
- // 2^160 - 1: hi_128 = 0xFFFFFFFF, lo_128 = u128::MAX
8
- const MAX_UINT160: U256 = U256::from_words(0xFFFFFFFF, u128::MAX);
9
-
10
- /// Truncate to uint160 range.
11
- fn as_uint160(val: U256) -> U256 {
12
- val & MAX_UINT160
13
- }
14
-
15
- pub fn get_next_sqrt_price_from_amount0_rounding_up(
16
- sqrt_p_x96: U256,
17
- liquidity: U256,
18
- amount: U256,
19
- add: bool,
20
- ) -> U256 {
21
- if amount == U256::ZERO {
22
- return sqrt_p_x96;
23
- }
24
- let numerator1 = liquidity << RESOLUTION;
25
-
26
- let product = amount * sqrt_p_x96;
27
- if add {
28
- if product / amount == sqrt_p_x96 {
29
- let denominator = numerator1 + product;
30
- if denominator >= numerator1 {
31
- return as_uint160(full_math::mul_div_rounding_up(
32
- numerator1,
33
- sqrt_p_x96,
34
- denominator,
35
- ));
36
- }
37
- }
38
- as_uint160(unsafe_math::div_rounding_up(
39
- numerator1,
40
- numerator1 / sqrt_p_x96 + amount,
41
- ))
42
- } else {
43
- assert!(
44
- product / amount == sqrt_p_x96 && numerator1 > product,
45
- "product / amount == sqrt_p_x96 && numerator1 > product"
46
- );
47
- let denominator = numerator1 - product;
48
- as_uint160(full_math::mul_div_rounding_up(
49
- numerator1,
50
- sqrt_p_x96,
51
- denominator,
52
- ))
53
- }
54
- }
55
-
56
- pub fn get_next_sqrt_price_from_amount1_rounding_down(
57
- sqrt_p_x96: U256,
58
- liquidity: U256,
59
- amount: U256,
60
- add: bool,
61
- ) -> U256 {
62
- if add {
63
- let quotient = if amount <= MAX_UINT160 {
64
- (amount << RESOLUTION) / liquidity
65
- } else {
66
- full_math::mul_div(amount, Q96, liquidity)
67
- };
68
- as_uint160(sqrt_p_x96 + quotient)
69
- } else {
70
- let quotient = if amount <= MAX_UINT160 {
71
- unsafe_math::div_rounding_up(amount << RESOLUTION, liquidity)
72
- } else {
73
- full_math::mul_div_rounding_up(amount, Q96, liquidity)
74
- };
75
- assert!(sqrt_p_x96 > quotient, "sqrt_p_x96 > quotient");
76
- as_uint160(sqrt_p_x96 - quotient)
77
- }
78
- }
79
-
80
- pub fn get_next_sqrt_price_from_input(
81
- sqrt_p_x96: U256,
82
- liquidity: U256,
83
- amount_in: U256,
84
- zero_for_one: bool,
85
- ) -> U256 {
86
- assert!(sqrt_p_x96 > U256::ZERO, "sqrt_p_x96 > 0");
87
- assert!(liquidity > U256::ZERO, "liquidity > 0");
88
-
89
- if zero_for_one {
90
- get_next_sqrt_price_from_amount0_rounding_up(sqrt_p_x96, liquidity, amount_in, true)
91
- } else {
92
- get_next_sqrt_price_from_amount1_rounding_down(sqrt_p_x96, liquidity, amount_in, true)
93
- }
94
- }
95
-
96
- pub fn get_next_sqrt_price_from_output(
97
- sqrt_p_x96: U256,
98
- liquidity: U256,
99
- amount_out: U256,
100
- zero_for_one: bool,
101
- ) -> U256 {
102
- assert!(sqrt_p_x96 > U256::ZERO, "sqrt_p_x96 > 0");
103
- assert!(liquidity > U256::ZERO, "liquidity > 0");
104
-
105
- if zero_for_one {
106
- get_next_sqrt_price_from_amount1_rounding_down(sqrt_p_x96, liquidity, amount_out, false)
107
- } else {
108
- get_next_sqrt_price_from_amount0_rounding_up(sqrt_p_x96, liquidity, amount_out, false)
109
- }
110
- }
111
-
112
- pub fn get_amount0_delta(
113
- sqrt_ratio_a_x96: U256,
114
- sqrt_ratio_b_x96: U256,
115
- liquidity: U256,
116
- round_up: bool,
117
- ) -> U256 {
118
- let (sqrt_ratio_a_x96, sqrt_ratio_b_x96) = if sqrt_ratio_a_x96 > sqrt_ratio_b_x96 {
119
- (sqrt_ratio_b_x96, sqrt_ratio_a_x96)
120
- } else {
121
- (sqrt_ratio_a_x96, sqrt_ratio_b_x96)
122
- };
123
-
124
- let numerator1 = liquidity << RESOLUTION;
125
- let numerator2 = sqrt_ratio_b_x96 - sqrt_ratio_a_x96;
126
-
127
- assert!(sqrt_ratio_a_x96 > U256::ZERO, "sqrt_ratio_a_x96 > 0");
128
-
129
- if round_up {
130
- unsafe_math::div_rounding_up(
131
- full_math::mul_div_rounding_up(numerator1, numerator2, sqrt_ratio_b_x96),
132
- sqrt_ratio_a_x96,
133
- )
134
- } else {
135
- full_math::mul_div(numerator1, numerator2, sqrt_ratio_b_x96) / sqrt_ratio_a_x96
136
- }
137
- }
138
-
139
- pub fn get_amount1_delta(
140
- sqrt_ratio_a_x96: U256,
141
- sqrt_ratio_b_x96: U256,
142
- liquidity: U256,
143
- round_up: bool,
144
- ) -> U256 {
145
- let (sqrt_ratio_a_x96, sqrt_ratio_b_x96) = if sqrt_ratio_a_x96 > sqrt_ratio_b_x96 {
146
- (sqrt_ratio_b_x96, sqrt_ratio_a_x96)
147
- } else {
148
- (sqrt_ratio_a_x96, sqrt_ratio_b_x96)
149
- };
150
-
151
- if round_up {
152
- full_math::mul_div_rounding_up(
153
- liquidity,
154
- sqrt_ratio_b_x96 - sqrt_ratio_a_x96,
155
- Q96,
156
- )
157
- } else {
158
- full_math::mul_div(liquidity, sqrt_ratio_b_x96 - sqrt_ratio_a_x96, Q96)
159
- }
160
- }
161
-
162
- /// Signed version: _getAmount0DeltaO with signed liquidity.
163
- /// Equivalent to TS SqrtPriceMath._getAmount0DeltaO.
164
- pub fn get_amount0_delta_signed(
165
- sqrt_ratio_a_x96: U256,
166
- sqrt_ratio_b_x96: U256,
167
- liquidity: I256,
168
- ) -> I256 {
169
- let mask_128 = (U256::ONE << 128) - U256::ONE;
170
- if liquidity < I256::ZERO {
171
- // BigInt.asUintN(128, -liquidity)
172
- let abs_liq = (-liquidity).as_u256() & mask_128;
173
- let delta = get_amount0_delta(sqrt_ratio_a_x96, sqrt_ratio_b_x96, abs_liq, false);
174
- // -BigInt.asIntN(256, delta)
175
- -(delta.as_i256())
176
- } else {
177
- // BigInt.asUintN(128, liquidity)
178
- let liq_u = liquidity.as_u256() & mask_128;
179
- let delta = get_amount0_delta(sqrt_ratio_a_x96, sqrt_ratio_b_x96, liq_u, true);
180
- // BigInt.asIntN(256, delta)
181
- delta.as_i256()
182
- }
183
- }
184
-
185
- /// Signed version: _getAmount1DeltaO with signed liquidity.
186
- /// Equivalent to TS SqrtPriceMath._getAmount1DeltaO.
187
- pub fn get_amount1_delta_signed(
188
- sqrt_ratio_a_x96: U256,
189
- sqrt_ratio_b_x96: U256,
190
- liquidity: I256,
191
- ) -> I256 {
192
- let mask_128 = (U256::ONE << 128) - U256::ONE;
193
- if liquidity < I256::ZERO {
194
- let abs_liq = (-liquidity).as_u256() & mask_128;
195
- let delta = get_amount1_delta(sqrt_ratio_a_x96, sqrt_ratio_b_x96, abs_liq, false);
196
- -(delta.as_i256())
197
- } else {
198
- let liq_u = liquidity.as_u256() & mask_128;
199
- let delta = get_amount1_delta(sqrt_ratio_a_x96, sqrt_ratio_b_x96, liq_u, true);
200
- delta.as_i256()
201
- }
202
- }
203
-
204
- #[cfg(test)]
205
- mod tests {
206
- use super::*;
207
-
208
- #[test]
209
- fn test_get_amount0_delta_basic() {
210
- let sqrt_a = U256::from(79228162514264337593543950336u128); // Q96 * 1
211
- let sqrt_b = U256::from(158456325028528675187087900672u128); // Q96 * 2
212
- let liquidity = U256::from(1_000_000u64);
213
- let result = get_amount0_delta(sqrt_a, sqrt_b, liquidity, true);
214
- assert!(result > U256::ZERO);
215
- }
216
-
217
- #[test]
218
- fn test_get_amount1_delta_basic() {
219
- let sqrt_a = U256::from(79228162514264337593543950336u128); // Q96 * 1
220
- let sqrt_b = U256::from(158456325028528675187087900672u128); // Q96 * 2
221
- let liquidity = U256::from(1_000_000u64);
222
- let result = get_amount1_delta(sqrt_a, sqrt_b, liquidity, true);
223
- assert!(result > U256::ZERO);
224
- }
225
-
226
- #[test]
227
- fn test_get_next_sqrt_price_from_input_zero_for_one() {
228
- let sqrt_p = U256::from(79228162514264337593543950336u128);
229
- let liquidity = U256::from(1_000_000_000_000u64);
230
- let amount = U256::from(1_000_000u64);
231
- let result = get_next_sqrt_price_from_input(sqrt_p, liquidity, amount, true);
232
- assert!(result > U256::ZERO);
233
- assert!(result <= sqrt_p);
234
- }
235
-
236
- #[test]
237
- fn test_get_next_sqrt_price_from_input_one_for_zero() {
238
- let sqrt_p = U256::from(79228162514264337593543950336u128);
239
- let liquidity = U256::from(1_000_000_000_000u64);
240
- let amount = U256::from(1_000_000u64);
241
- let result = get_next_sqrt_price_from_input(sqrt_p, liquidity, amount, false);
242
- assert!(result >= sqrt_p);
243
- }
244
-
245
- #[test]
246
- fn test_get_amount0_delta_symmetric() {
247
- let sqrt_a = U256::from(79228162514264337593543950336u128);
248
- let sqrt_b = U256::from(158456325028528675187087900672u128);
249
- let liquidity = U256::from(1_000_000u64);
250
- let r1 = get_amount0_delta(sqrt_a, sqrt_b, liquidity, true);
251
- let r2 = get_amount0_delta(sqrt_b, sqrt_a, liquidity, true);
252
- assert_eq!(r1, r2);
253
- }
254
-
255
- #[test]
256
- fn test_get_amount0_delta_signed_positive() {
257
- let sqrt_a = U256::from(79228162514264337593543950336u128);
258
- let sqrt_b = U256::from(158456325028528675187087900672u128);
259
- let liquidity = I256::from(1_000_000i64);
260
- let result = get_amount0_delta_signed(sqrt_a, sqrt_b, liquidity);
261
- assert!(result > I256::ZERO);
262
- }
263
-
264
- #[test]
265
- fn test_get_amount0_delta_signed_negative() {
266
- let sqrt_a = U256::from(79228162514264337593543950336u128);
267
- let sqrt_b = U256::from(158456325028528675187087900672u128);
268
- let liquidity = I256::from(-1_000_000i64);
269
- let result = get_amount0_delta_signed(sqrt_a, sqrt_b, liquidity);
270
- assert!(result < I256::ZERO);
271
- }
272
- }
@@ -1,306 +0,0 @@
1
- use ethnum::{I256, U256};
2
- use super::full_math;
3
- use super::sqrt_price_math;
4
-
5
- /// Result of a single swap step computation.
6
- #[derive(Debug, Clone, Copy, PartialEq, Eq)]
7
- pub struct SwapStepResult {
8
- pub sqrt_ratio_next_x96: U256,
9
- pub amount_in: U256,
10
- pub amount_out: U256,
11
- pub fee_amount: U256,
12
- }
13
-
14
- /// BI_POWS[6] = 1_000_000
15
- const ONE_MILLION: U256 = U256::new(1_000_000u128);
16
-
17
- /// Computes the result of swapping some amount in, or amount out, given the parameters of the swap.
18
- ///
19
- /// The fee, `fee_pips`, is in hundredths of a bip (i.e. 1e-6).
20
- ///
21
- /// `amount_remaining` is treated as an I256: positive means exact-input, negative means exact-output.
22
- pub fn compute_swap_step(
23
- sqrt_ratio_current_x96: U256,
24
- sqrt_ratio_target_x96: U256,
25
- liquidity: U256,
26
- amount_remaining: I256,
27
- fee_pips: U256,
28
- ) -> SwapStepResult {
29
- let zero_for_one = sqrt_ratio_current_x96 >= sqrt_ratio_target_x96;
30
- let exact_in = amount_remaining >= I256::ZERO;
31
-
32
- let sqrt_ratio_next_x96;
33
- let mut amount_in;
34
- let mut amount_out;
35
- let fee_amount;
36
-
37
- if exact_in {
38
- // BigInt.asUintN(256, amountRemaining) -- amountRemaining is non-negative here so it's identity
39
- let amount_remaining_u = amount_remaining.as_u256();
40
- let amount_remaining_less_fee =
41
- full_math::mul_div(amount_remaining_u, ONE_MILLION - fee_pips, ONE_MILLION);
42
-
43
- amount_in = if zero_for_one {
44
- sqrt_price_math::get_amount0_delta(
45
- sqrt_ratio_target_x96,
46
- sqrt_ratio_current_x96,
47
- liquidity,
48
- true,
49
- )
50
- } else {
51
- sqrt_price_math::get_amount1_delta(
52
- sqrt_ratio_current_x96,
53
- sqrt_ratio_target_x96,
54
- liquidity,
55
- true,
56
- )
57
- };
58
-
59
- if amount_remaining_less_fee >= amount_in {
60
- sqrt_ratio_next_x96 = sqrt_ratio_target_x96;
61
- } else {
62
- sqrt_ratio_next_x96 = sqrt_price_math::get_next_sqrt_price_from_input(
63
- sqrt_ratio_current_x96,
64
- liquidity,
65
- amount_remaining_less_fee,
66
- zero_for_one,
67
- );
68
- }
69
- } else {
70
- // BigInt.asUintN(256, -amountRemaining) -- negate signed, interpret as unsigned
71
- let neg_amount = (-amount_remaining).as_u256();
72
-
73
- amount_out = if zero_for_one {
74
- sqrt_price_math::get_amount1_delta(
75
- sqrt_ratio_target_x96,
76
- sqrt_ratio_current_x96,
77
- liquidity,
78
- false,
79
- )
80
- } else {
81
- sqrt_price_math::get_amount0_delta(
82
- sqrt_ratio_current_x96,
83
- sqrt_ratio_target_x96,
84
- liquidity,
85
- false,
86
- )
87
- };
88
-
89
- if neg_amount >= amount_out {
90
- sqrt_ratio_next_x96 = sqrt_ratio_target_x96;
91
- } else {
92
- sqrt_ratio_next_x96 = sqrt_price_math::get_next_sqrt_price_from_output(
93
- sqrt_ratio_current_x96,
94
- liquidity,
95
- neg_amount,
96
- zero_for_one,
97
- );
98
- }
99
-
100
- // Initialize amount_in to 0; it will be set below
101
- amount_in = U256::ZERO;
102
- }
103
-
104
- // Re-initialize for the second half of the function
105
- // We need to track amount_out properly for the !exact_in case
106
- // The TS code re-computes both amount_in and amount_out based on `max` flag
107
- let max = sqrt_ratio_target_x96 == sqrt_ratio_next_x96;
108
-
109
- if exact_in {
110
- // amount_out was not set in exact_in path above, initialize to 0
111
- amount_out = U256::ZERO;
112
- } else {
113
- amount_out = if zero_for_one {
114
- sqrt_price_math::get_amount1_delta(
115
- sqrt_ratio_target_x96,
116
- sqrt_ratio_current_x96,
117
- liquidity,
118
- false,
119
- )
120
- } else {
121
- sqrt_price_math::get_amount0_delta(
122
- sqrt_ratio_current_x96,
123
- sqrt_ratio_target_x96,
124
- liquidity,
125
- false,
126
- )
127
- };
128
- }
129
-
130
- if zero_for_one {
131
- if !(max && exact_in) {
132
- amount_in = sqrt_price_math::get_amount0_delta(
133
- sqrt_ratio_next_x96,
134
- sqrt_ratio_current_x96,
135
- liquidity,
136
- true,
137
- );
138
- }
139
- if !(max && !exact_in) {
140
- amount_out = sqrt_price_math::get_amount1_delta(
141
- sqrt_ratio_next_x96,
142
- sqrt_ratio_current_x96,
143
- liquidity,
144
- false,
145
- );
146
- }
147
- } else {
148
- if !(max && exact_in) {
149
- amount_in = sqrt_price_math::get_amount1_delta(
150
- sqrt_ratio_current_x96,
151
- sqrt_ratio_next_x96,
152
- liquidity,
153
- true,
154
- );
155
- }
156
- if !(max && !exact_in) {
157
- amount_out = sqrt_price_math::get_amount0_delta(
158
- sqrt_ratio_current_x96,
159
- sqrt_ratio_next_x96,
160
- liquidity,
161
- false,
162
- );
163
- }
164
- }
165
-
166
- // Cap the output amount to not exceed the remaining output amount
167
- if !exact_in {
168
- let neg_amount = (-amount_remaining).as_u256();
169
- if amount_out > neg_amount {
170
- amount_out = neg_amount;
171
- }
172
- }
173
-
174
- if exact_in && sqrt_ratio_next_x96 != sqrt_ratio_target_x96 {
175
- // We didn't reach the target, so take the remainder of the maximum input as fee
176
- fee_amount = amount_remaining.as_u256() - amount_in;
177
- } else {
178
- fee_amount =
179
- full_math::mul_div_rounding_up(amount_in, fee_pips, ONE_MILLION - fee_pips);
180
- }
181
-
182
- SwapStepResult {
183
- sqrt_ratio_next_x96,
184
- amount_in,
185
- amount_out,
186
- fee_amount,
187
- }
188
- }
189
-
190
- #[cfg(test)]
191
- mod tests {
192
- use super::*;
193
-
194
- // Q96 = 2^96
195
- const Q96: U256 = U256::from_words(0, 1u128 << 96);
196
-
197
- #[test]
198
- fn test_exact_in_zero_for_one() {
199
- // price moves from 2.0 toward 1.0
200
- let sqrt_current = Q96 * U256::from(2u64); // sqrt(4) * Q96
201
- let sqrt_target = Q96; // sqrt(1) * Q96
202
- let liquidity = U256::from(1_000_000_000_000u128);
203
- let amount_remaining = I256::from(1_000_000i64);
204
- let fee_pips = U256::from(3000u64); // 0.3%
205
-
206
- let result = compute_swap_step(
207
- sqrt_current,
208
- sqrt_target,
209
- liquidity,
210
- amount_remaining,
211
- fee_pips,
212
- );
213
-
214
- assert!(result.sqrt_ratio_next_x96 > U256::ZERO);
215
- assert!(result.sqrt_ratio_next_x96 <= sqrt_current);
216
- assert!(result.amount_in > U256::ZERO);
217
- assert!(result.amount_out > U256::ZERO);
218
- // amount_in + fee_amount should not exceed amount_remaining
219
- assert!(result.amount_in + result.fee_amount <= amount_remaining.as_u256());
220
- }
221
-
222
- #[test]
223
- fn test_exact_out_zero_for_one() {
224
- let sqrt_current = Q96 * U256::from(2u64);
225
- let sqrt_target = Q96;
226
- let liquidity = U256::from(1_000_000_000_000u128);
227
- let amount_remaining = I256::from(-500_000i64); // exact output
228
- let fee_pips = U256::from(3000u64);
229
-
230
- let result = compute_swap_step(
231
- sqrt_current,
232
- sqrt_target,
233
- liquidity,
234
- amount_remaining,
235
- fee_pips,
236
- );
237
-
238
- assert!(result.sqrt_ratio_next_x96 > U256::ZERO);
239
- assert!(result.amount_in > U256::ZERO);
240
- assert!(result.amount_out > U256::ZERO);
241
- // amount_out should not exceed requested
242
- assert!(result.amount_out <= U256::from(500_000u64));
243
- }
244
-
245
- #[test]
246
- fn test_exact_in_one_for_zero() {
247
- let sqrt_current = Q96;
248
- let sqrt_target = Q96 * U256::from(2u64);
249
- let liquidity = U256::from(1_000_000_000_000u128);
250
- let amount_remaining = I256::from(1_000_000i64);
251
- let fee_pips = U256::from(3000u64);
252
-
253
- let result = compute_swap_step(
254
- sqrt_current,
255
- sqrt_target,
256
- liquidity,
257
- amount_remaining,
258
- fee_pips,
259
- );
260
-
261
- assert!(result.sqrt_ratio_next_x96 >= sqrt_current);
262
- assert!(result.amount_in > U256::ZERO);
263
- assert!(result.amount_out > U256::ZERO);
264
- }
265
-
266
- #[test]
267
- fn test_fee_amount_when_target_not_reached() {
268
- // Very small liquidity so we definitely reach the target
269
- let sqrt_current = Q96 * U256::from(2u64);
270
- let sqrt_target = Q96;
271
- let liquidity = U256::from(100u64); // very small liquidity
272
- let amount_remaining = I256::from(1_000_000_000i64); // large amount
273
- let fee_pips = U256::from(3000u64);
274
-
275
- let result = compute_swap_step(
276
- sqrt_current,
277
- sqrt_target,
278
- liquidity,
279
- amount_remaining,
280
- fee_pips,
281
- );
282
-
283
- // Should reach the target price
284
- assert_eq!(result.sqrt_ratio_next_x96, sqrt_target);
285
- }
286
-
287
- #[test]
288
- fn test_zero_fee() {
289
- let sqrt_current = Q96 * U256::from(2u64);
290
- let sqrt_target = Q96;
291
- let liquidity = U256::from(1_000_000_000_000u128);
292
- let amount_remaining = I256::from(1_000_000i64);
293
- let fee_pips = U256::ZERO;
294
-
295
- let result = compute_swap_step(
296
- sqrt_current,
297
- sqrt_target,
298
- liquidity,
299
- amount_remaining,
300
- fee_pips,
301
- );
302
-
303
- // With zero fee, fee_amount should be 0
304
- assert_eq!(result.fee_amount, U256::ZERO);
305
- }
306
- }