@clober/v2-sdk 0.0.11 → 0.0.12-8.dev.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (347) hide show
  1. package/README.md +12 -4
  2. package/dist/cjs/abis/core/book-manager-abi.js +112 -0
  3. package/dist/cjs/abis/core/book-manager-abi.js.map +1 -0
  4. package/dist/cjs/abis/core/book-viewer-abi.js +137 -5
  5. package/dist/cjs/abis/core/book-viewer-abi.js.map +1 -1
  6. package/dist/cjs/abis/core/controller-abi.js +1 -6
  7. package/dist/cjs/abis/core/controller-abi.js.map +1 -1
  8. package/dist/cjs/abis/rebalancer/minter-abi.js +262 -0
  9. package/dist/cjs/abis/rebalancer/minter-abi.js.map +1 -0
  10. package/dist/cjs/abis/rebalancer/mock-swap-abi.js +95 -0
  11. package/dist/cjs/abis/rebalancer/mock-swap-abi.js.map +1 -0
  12. package/dist/cjs/abis/rebalancer/operator-abi.js +401 -0
  13. package/dist/cjs/abis/rebalancer/operator-abi.js.map +1 -0
  14. package/dist/cjs/abis/rebalancer/rebalancer-abi.js +1326 -0
  15. package/dist/cjs/abis/rebalancer/rebalancer-abi.js.map +1 -0
  16. package/dist/cjs/abis/rebalancer/strategy-abi.js +895 -0
  17. package/dist/cjs/abis/rebalancer/strategy-abi.js.map +1 -0
  18. package/dist/cjs/apis/chart-logs.js +30 -13
  19. package/dist/cjs/apis/chart-logs.js.map +1 -1
  20. package/dist/cjs/apis/market.js +45 -22
  21. package/dist/cjs/apis/market.js.map +1 -1
  22. package/dist/cjs/apis/odos.js +100 -0
  23. package/dist/cjs/apis/odos.js.map +1 -0
  24. package/dist/cjs/apis/open-order.js +75 -44
  25. package/dist/cjs/apis/open-order.js.map +1 -1
  26. package/dist/cjs/apis/pool.js +95 -0
  27. package/dist/cjs/apis/pool.js.map +1 -0
  28. package/dist/cjs/apis/strategy.js +53 -0
  29. package/dist/cjs/apis/strategy.js.map +1 -0
  30. package/dist/cjs/approval.js +83 -26
  31. package/dist/cjs/approval.js.map +1 -1
  32. package/dist/cjs/call.js +739 -107
  33. package/dist/cjs/call.js.map +1 -1
  34. package/dist/cjs/constants/addresses.js +68 -1
  35. package/dist/cjs/constants/addresses.js.map +1 -1
  36. package/dist/cjs/constants/chain.js +23 -2
  37. package/dist/cjs/constants/chain.js.map +1 -1
  38. package/dist/cjs/constants/currency.js +86 -3
  39. package/dist/cjs/constants/currency.js.map +1 -1
  40. package/dist/cjs/constants/fee.js +21 -2
  41. package/dist/cjs/constants/fee.js.map +1 -1
  42. package/dist/cjs/constants/permit.js +14 -0
  43. package/dist/cjs/constants/permit.js.map +1 -0
  44. package/dist/cjs/constants/price.js +2 -1
  45. package/dist/cjs/constants/price.js.map +1 -1
  46. package/dist/cjs/constants/rise-sepolia.js +29 -0
  47. package/dist/cjs/constants/rise-sepolia.js.map +1 -0
  48. package/dist/cjs/constants/subgraph.js +37 -0
  49. package/dist/cjs/constants/subgraph.js.map +1 -0
  50. package/dist/cjs/constants/test-chain.js +50 -0
  51. package/dist/cjs/constants/test-chain.js.map +1 -0
  52. package/dist/cjs/constants/tick.js +6 -0
  53. package/dist/cjs/constants/tick.js.map +1 -0
  54. package/dist/cjs/index.js +1 -1
  55. package/dist/cjs/index.js.map +1 -1
  56. package/dist/cjs/model/book.js +62 -35
  57. package/dist/cjs/model/book.js.map +1 -1
  58. package/dist/cjs/model/market.js +61 -26
  59. package/dist/cjs/model/market.js.map +1 -1
  60. package/dist/cjs/model/pool.js +239 -0
  61. package/dist/cjs/model/pool.js.map +1 -0
  62. package/dist/cjs/type.js.map +1 -1
  63. package/dist/cjs/utils/allowance.js +39 -0
  64. package/dist/cjs/utils/allowance.js.map +1 -0
  65. package/dist/cjs/utils/approval.js +2 -3
  66. package/dist/cjs/utils/approval.js.map +1 -1
  67. package/dist/cjs/utils/bigint.js +5 -1
  68. package/dist/cjs/utils/bigint.js.map +1 -1
  69. package/dist/cjs/utils/book-id.js +7 -7
  70. package/dist/cjs/utils/book-id.js.map +1 -1
  71. package/dist/cjs/utils/build-transaction.js +3 -4
  72. package/dist/cjs/utils/build-transaction.js.map +1 -1
  73. package/dist/cjs/utils/currency.js +61 -27
  74. package/dist/cjs/utils/currency.js.map +1 -1
  75. package/dist/cjs/utils/market.js +1 -1
  76. package/dist/cjs/utils/market.js.map +1 -1
  77. package/dist/cjs/utils/math.js +3 -1
  78. package/dist/cjs/utils/math.js.map +1 -1
  79. package/dist/cjs/utils/open.js +35 -25
  80. package/dist/cjs/utils/open.js.map +1 -1
  81. package/dist/cjs/utils/order.js +50 -126
  82. package/dist/cjs/utils/order.js.map +1 -1
  83. package/dist/cjs/utils/pool-key.js +17 -0
  84. package/dist/cjs/utils/pool-key.js.map +1 -0
  85. package/dist/cjs/utils/pool.js +88 -0
  86. package/dist/cjs/utils/pool.js.map +1 -0
  87. package/dist/cjs/utils/prices.js +40 -5
  88. package/dist/cjs/utils/prices.js.map +1 -1
  89. package/dist/cjs/utils/quotes.js +16 -0
  90. package/dist/cjs/utils/quotes.js.map +1 -0
  91. package/dist/cjs/utils/tick.js +12 -1
  92. package/dist/cjs/utils/tick.js.map +1 -1
  93. package/dist/cjs/utils/time-series.js +31 -0
  94. package/dist/cjs/utils/time-series.js.map +1 -0
  95. package/dist/cjs/utils/unit-size.js +14 -0
  96. package/dist/cjs/utils/unit-size.js.map +1 -0
  97. package/dist/cjs/utils.js +18 -0
  98. package/dist/cjs/utils.js.map +1 -0
  99. package/dist/cjs/view.js +308 -44
  100. package/dist/cjs/view.js.map +1 -1
  101. package/dist/esm/abis/core/book-manager-abi.js +109 -0
  102. package/dist/esm/abis/core/book-manager-abi.js.map +1 -0
  103. package/dist/esm/abis/core/book-viewer-abi.js +137 -5
  104. package/dist/esm/abis/core/book-viewer-abi.js.map +1 -1
  105. package/dist/esm/abis/core/controller-abi.js +1 -6
  106. package/dist/esm/abis/core/controller-abi.js.map +1 -1
  107. package/dist/esm/abis/rebalancer/minter-abi.js +259 -0
  108. package/dist/esm/abis/rebalancer/minter-abi.js.map +1 -0
  109. package/dist/esm/abis/rebalancer/mock-swap-abi.js +92 -0
  110. package/dist/esm/abis/rebalancer/mock-swap-abi.js.map +1 -0
  111. package/dist/esm/abis/rebalancer/operator-abi.js +398 -0
  112. package/dist/esm/abis/rebalancer/operator-abi.js.map +1 -0
  113. package/dist/esm/abis/rebalancer/rebalancer-abi.js +1323 -0
  114. package/dist/esm/abis/rebalancer/rebalancer-abi.js.map +1 -0
  115. package/dist/esm/abis/rebalancer/strategy-abi.js +892 -0
  116. package/dist/esm/abis/rebalancer/strategy-abi.js.map +1 -0
  117. package/dist/esm/apis/chart-logs.js +29 -12
  118. package/dist/esm/apis/chart-logs.js.map +1 -1
  119. package/dist/esm/apis/market.js +47 -24
  120. package/dist/esm/apis/market.js.map +1 -1
  121. package/dist/esm/apis/odos.js +94 -0
  122. package/dist/esm/apis/odos.js.map +1 -0
  123. package/dist/esm/apis/open-order.js +78 -44
  124. package/dist/esm/apis/open-order.js.map +1 -1
  125. package/dist/esm/apis/pool.js +96 -0
  126. package/dist/esm/apis/pool.js.map +1 -0
  127. package/dist/esm/apis/strategy.js +48 -0
  128. package/dist/esm/apis/strategy.js.map +1 -0
  129. package/dist/esm/approval.js +107 -25
  130. package/dist/esm/approval.js.map +1 -1
  131. package/dist/esm/call.js +772 -145
  132. package/dist/esm/call.js.map +1 -1
  133. package/dist/esm/constants/addresses.js +69 -2
  134. package/dist/esm/constants/addresses.js.map +1 -1
  135. package/dist/esm/constants/chain.js +23 -2
  136. package/dist/esm/constants/chain.js.map +1 -1
  137. package/dist/esm/constants/currency.js +87 -2
  138. package/dist/esm/constants/currency.js.map +1 -1
  139. package/dist/esm/constants/fee.js +21 -2
  140. package/dist/esm/constants/fee.js.map +1 -1
  141. package/dist/esm/constants/permit.js +11 -0
  142. package/dist/esm/constants/permit.js.map +1 -0
  143. package/dist/esm/constants/price.js +1 -0
  144. package/dist/esm/constants/price.js.map +1 -1
  145. package/dist/esm/constants/rise-sepolia.js +26 -0
  146. package/dist/esm/constants/rise-sepolia.js.map +1 -0
  147. package/dist/esm/constants/subgraph.js +33 -0
  148. package/dist/esm/constants/subgraph.js.map +1 -0
  149. package/dist/esm/constants/test-chain.js +47 -0
  150. package/dist/esm/constants/test-chain.js.map +1 -0
  151. package/dist/esm/constants/tick.js +3 -0
  152. package/dist/esm/constants/tick.js.map +1 -0
  153. package/dist/esm/index.js +1 -1
  154. package/dist/esm/index.js.map +1 -1
  155. package/dist/esm/model/book.js +62 -35
  156. package/dist/esm/model/book.js.map +1 -1
  157. package/dist/esm/model/market.js +63 -28
  158. package/dist/esm/model/market.js.map +1 -1
  159. package/dist/esm/model/pool.js +236 -0
  160. package/dist/esm/model/pool.js.map +1 -0
  161. package/dist/esm/type.js.map +1 -1
  162. package/dist/esm/utils/allowance.js +35 -0
  163. package/dist/esm/utils/allowance.js.map +1 -0
  164. package/dist/esm/utils/approval.js +2 -3
  165. package/dist/esm/utils/approval.js.map +1 -1
  166. package/dist/esm/utils/bigint.js +2 -0
  167. package/dist/esm/utils/bigint.js.map +1 -1
  168. package/dist/esm/utils/book-id.js +7 -7
  169. package/dist/esm/utils/book-id.js.map +1 -1
  170. package/dist/esm/utils/build-transaction.js +3 -4
  171. package/dist/esm/utils/build-transaction.js.map +1 -1
  172. package/dist/esm/utils/currency.js +59 -25
  173. package/dist/esm/utils/currency.js.map +1 -1
  174. package/dist/esm/utils/market.js +1 -1
  175. package/dist/esm/utils/market.js.map +1 -1
  176. package/dist/esm/utils/math.js +1 -0
  177. package/dist/esm/utils/math.js.map +1 -1
  178. package/dist/esm/utils/open.js +33 -23
  179. package/dist/esm/utils/open.js.map +1 -1
  180. package/dist/esm/utils/order.js +49 -125
  181. package/dist/esm/utils/order.js.map +1 -1
  182. package/dist/esm/utils/pool-key.js +12 -0
  183. package/dist/esm/utils/pool-key.js.map +1 -0
  184. package/dist/esm/utils/pool.js +84 -0
  185. package/dist/esm/utils/pool.js.map +1 -0
  186. package/dist/esm/utils/prices.js +39 -5
  187. package/dist/esm/utils/prices.js.map +1 -1
  188. package/dist/esm/utils/quotes.js +12 -0
  189. package/dist/esm/utils/quotes.js.map +1 -0
  190. package/dist/esm/utils/tick.js +11 -1
  191. package/dist/esm/utils/tick.js.map +1 -1
  192. package/dist/esm/utils/time-series.js +27 -0
  193. package/dist/esm/utils/time-series.js.map +1 -0
  194. package/dist/esm/utils/unit-size.js +10 -0
  195. package/dist/esm/utils/unit-size.js.map +1 -0
  196. package/dist/esm/utils.js +5 -0
  197. package/dist/esm/utils.js.map +1 -0
  198. package/dist/esm/view.js +379 -61
  199. package/dist/esm/view.js.map +1 -1
  200. package/dist/tsconfig.build.tsbuildinfo +1 -1
  201. package/dist/types/abis/core/book-manager-abi.d.ts +82 -0
  202. package/dist/types/abis/core/book-manager-abi.d.ts.map +1 -0
  203. package/dist/types/abis/core/book-viewer-abi.d.ts +107 -5
  204. package/dist/types/abis/core/book-viewer-abi.d.ts.map +1 -1
  205. package/dist/types/abis/core/controller-abi.d.ts +1 -5
  206. package/dist/types/abis/core/controller-abi.d.ts.map +1 -1
  207. package/dist/types/abis/rebalancer/minter-abi.d.ts +198 -0
  208. package/dist/types/abis/rebalancer/minter-abi.d.ts.map +1 -0
  209. package/dist/types/abis/rebalancer/mock-swap-abi.d.ts +70 -0
  210. package/dist/types/abis/rebalancer/mock-swap-abi.d.ts.map +1 -0
  211. package/dist/types/abis/rebalancer/operator-abi.d.ts +308 -0
  212. package/dist/types/abis/rebalancer/operator-abi.d.ts.map +1 -0
  213. package/dist/types/abis/rebalancer/rebalancer-abi.d.ts +1023 -0
  214. package/dist/types/abis/rebalancer/rebalancer-abi.d.ts.map +1 -0
  215. package/dist/types/abis/rebalancer/strategy-abi.d.ts +690 -0
  216. package/dist/types/abis/rebalancer/strategy-abi.d.ts.map +1 -0
  217. package/dist/types/apis/chart-logs.d.ts +3 -0
  218. package/dist/types/apis/chart-logs.d.ts.map +1 -1
  219. package/dist/types/apis/market.d.ts +2 -1
  220. package/dist/types/apis/market.d.ts.map +1 -1
  221. package/dist/types/apis/odos.d.ts +28 -0
  222. package/dist/types/apis/odos.d.ts.map +1 -0
  223. package/dist/types/apis/open-order.d.ts +3 -2
  224. package/dist/types/apis/open-order.d.ts.map +1 -1
  225. package/dist/types/apis/pool.d.ts +11 -0
  226. package/dist/types/apis/pool.d.ts.map +1 -0
  227. package/dist/types/apis/strategy.d.ts +6 -0
  228. package/dist/types/apis/strategy.d.ts.map +1 -0
  229. package/dist/types/approval.d.ts +37 -3
  230. package/dist/types/approval.d.ts.map +1 -1
  231. package/dist/types/call.d.ts +233 -54
  232. package/dist/types/call.d.ts.map +1 -1
  233. package/dist/types/constants/addresses.d.ts +4 -0
  234. package/dist/types/constants/addresses.d.ts.map +1 -1
  235. package/dist/types/constants/chain.d.ts +9 -2
  236. package/dist/types/constants/chain.d.ts.map +1 -1
  237. package/dist/types/constants/currency.d.ts +8 -0
  238. package/dist/types/constants/currency.d.ts.map +1 -1
  239. package/dist/types/constants/fee.d.ts +7 -2
  240. package/dist/types/constants/fee.d.ts.map +1 -1
  241. package/dist/types/constants/permit.d.ts +10 -0
  242. package/dist/types/constants/permit.d.ts.map +1 -0
  243. package/dist/types/constants/price.d.ts +1 -0
  244. package/dist/types/constants/price.d.ts.map +1 -1
  245. package/dist/types/constants/rise-sepolia.d.ts +32 -0
  246. package/dist/types/constants/rise-sepolia.d.ts.map +1 -0
  247. package/dist/types/constants/subgraph.d.ts +8 -0
  248. package/dist/types/constants/subgraph.d.ts.map +1 -0
  249. package/dist/types/constants/test-chain.d.ts +4 -0
  250. package/dist/types/constants/test-chain.d.ts.map +1 -0
  251. package/dist/types/constants/tick.d.ts +3 -0
  252. package/dist/types/constants/tick.d.ts.map +1 -0
  253. package/dist/types/index.d.ts +1 -1
  254. package/dist/types/index.d.ts.map +1 -1
  255. package/dist/types/model/book.d.ts +25 -33
  256. package/dist/types/model/book.d.ts.map +1 -1
  257. package/dist/types/model/currency.d.ts +7 -0
  258. package/dist/types/model/currency.d.ts.map +1 -1
  259. package/dist/types/model/depth.d.ts +4 -3
  260. package/dist/types/model/depth.d.ts.map +1 -1
  261. package/dist/types/model/market.d.ts +24 -13
  262. package/dist/types/model/market.d.ts.map +1 -1
  263. package/dist/types/model/open-order.d.ts +20 -27
  264. package/dist/types/model/open-order.d.ts.map +1 -1
  265. package/dist/types/model/pool.d.ts +76 -0
  266. package/dist/types/model/pool.d.ts.map +1 -0
  267. package/dist/types/type.d.ts +95 -6
  268. package/dist/types/type.d.ts.map +1 -1
  269. package/dist/types/utils/allowance.d.ts +3 -0
  270. package/dist/types/utils/allowance.d.ts.map +1 -0
  271. package/dist/types/utils/approval.d.ts +2 -1
  272. package/dist/types/utils/approval.d.ts.map +1 -1
  273. package/dist/types/utils/bigint.d.ts +2 -0
  274. package/dist/types/utils/bigint.d.ts.map +1 -1
  275. package/dist/types/utils/book-id.d.ts +2 -1
  276. package/dist/types/utils/book-id.d.ts.map +1 -1
  277. package/dist/types/utils/build-transaction.d.ts +3 -3
  278. package/dist/types/utils/build-transaction.d.ts.map +1 -1
  279. package/dist/types/utils/currency.d.ts +3 -2
  280. package/dist/types/utils/currency.d.ts.map +1 -1
  281. package/dist/types/utils/math.d.ts +1 -0
  282. package/dist/types/utils/math.d.ts.map +1 -1
  283. package/dist/types/utils/open.d.ts +2 -1
  284. package/dist/types/utils/open.d.ts.map +1 -1
  285. package/dist/types/utils/order.d.ts +4 -11
  286. package/dist/types/utils/order.d.ts.map +1 -1
  287. package/dist/types/utils/pool-key.d.ts +3 -0
  288. package/dist/types/utils/pool-key.d.ts.map +1 -0
  289. package/dist/types/utils/pool.d.ts +11 -0
  290. package/dist/types/utils/pool.d.ts.map +1 -0
  291. package/dist/types/utils/prices.d.ts +13 -2
  292. package/dist/types/utils/prices.d.ts.map +1 -1
  293. package/dist/types/utils/quotes.d.ts +2 -0
  294. package/dist/types/utils/quotes.d.ts.map +1 -0
  295. package/dist/types/utils/tick.d.ts +1 -0
  296. package/dist/types/utils/tick.d.ts.map +1 -1
  297. package/dist/types/utils/time-series.d.ts +4 -0
  298. package/dist/types/utils/time-series.d.ts.map +1 -0
  299. package/dist/types/utils/unit-size.d.ts +4 -0
  300. package/dist/types/utils/unit-size.d.ts.map +1 -0
  301. package/dist/types/utils.d.ts +5 -0
  302. package/dist/types/utils.d.ts.map +1 -0
  303. package/dist/types/view.d.ts +236 -30
  304. package/dist/types/view.d.ts.map +1 -1
  305. package/package.json +4 -3
  306. package/dist/cjs/abis/core/params-abi.js +0 -62
  307. package/dist/cjs/abis/core/params-abi.js.map +0 -1
  308. package/dist/cjs/apis/subgraph.js +0 -26
  309. package/dist/cjs/apis/subgraph.js.map +0 -1
  310. package/dist/cjs/constants/client.js +0 -14
  311. package/dist/cjs/constants/client.js.map +0 -1
  312. package/dist/cjs/constants/subgraph-url.js +0 -8
  313. package/dist/cjs/constants/subgraph-url.js.map +0 -1
  314. package/dist/cjs/signature.js +0 -145
  315. package/dist/cjs/signature.js.map +0 -1
  316. package/dist/cjs/utils/decorator.js +0 -13
  317. package/dist/cjs/utils/decorator.js.map +0 -1
  318. package/dist/cjs/utils/unit.js +0 -34
  319. package/dist/cjs/utils/unit.js.map +0 -1
  320. package/dist/esm/abis/core/params-abi.js +0 -59
  321. package/dist/esm/abis/core/params-abi.js.map +0 -1
  322. package/dist/esm/apis/subgraph.js +0 -22
  323. package/dist/esm/apis/subgraph.js.map +0 -1
  324. package/dist/esm/constants/client.js +0 -10
  325. package/dist/esm/constants/client.js.map +0 -1
  326. package/dist/esm/constants/subgraph-url.js +0 -5
  327. package/dist/esm/constants/subgraph-url.js.map +0 -1
  328. package/dist/esm/signature.js +0 -169
  329. package/dist/esm/signature.js.map +0 -1
  330. package/dist/esm/utils/decorator.js +0 -9
  331. package/dist/esm/utils/decorator.js.map +0 -1
  332. package/dist/esm/utils/unit.js +0 -30
  333. package/dist/esm/utils/unit.js.map +0 -1
  334. package/dist/types/abis/core/params-abi.d.ts +0 -21
  335. package/dist/types/abis/core/params-abi.d.ts.map +0 -1
  336. package/dist/types/apis/subgraph.d.ts +0 -3
  337. package/dist/types/apis/subgraph.d.ts.map +0 -1
  338. package/dist/types/constants/client.d.ts +0 -5
  339. package/dist/types/constants/client.d.ts.map +0 -1
  340. package/dist/types/constants/subgraph-url.d.ts +0 -5
  341. package/dist/types/constants/subgraph-url.d.ts.map +0 -1
  342. package/dist/types/signature.d.ts +0 -38
  343. package/dist/types/signature.d.ts.map +0 -1
  344. package/dist/types/utils/decorator.d.ts +0 -6
  345. package/dist/types/utils/decorator.d.ts.map +0 -1
  346. package/dist/types/utils/unit.d.ts +0 -4
  347. package/dist/types/utils/unit.d.ts.map +0 -1
package/dist/cjs/call.js CHANGED
@@ -1,9 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.cancelOrders = exports.cancelOrder = exports.claimOrders = exports.claimOrder = exports.marketOrder = exports.limitOrder = exports.openMarket = void 0;
3
+ exports.resumePool = exports.pausePool = exports.setStrategyConfig = exports.adjustOrderPrice = exports.refillOrder = exports.removeLiquidity = exports.addLiquidity = exports.openPool = exports.cancelOrders = exports.cancelOrder = exports.claimOrders = exports.claimOrder = exports.marketOrder = exports.limitOrder = exports.openMarket = void 0;
4
4
  const viem_1 = require("viem");
5
5
  const chain_1 = require("./constants/chain");
6
- const unit_1 = require("./utils/unit");
6
+ const unit_size_1 = require("./utils/unit-size");
7
7
  const controller_abi_1 = require("./abis/core/controller-abi");
8
8
  const time_1 = require("./utils/time");
9
9
  const build_transaction_1 = require("./utils/build-transaction");
@@ -15,17 +15,34 @@ const tick_1 = require("./utils/tick");
15
15
  const view_1 = require("./view");
16
16
  const book_id_1 = require("./utils/book-id");
17
17
  const approval_1 = require("./utils/approval");
18
- const decorator_1 = require("./utils/decorator");
19
18
  const order_1 = require("./utils/order");
20
- const decimals_1 = require("./utils/decimals");
21
19
  const bigint_1 = require("./utils/bigint");
22
- exports.openMarket = (0, decorator_1.decorator)(async ({ chainId, inputToken, outputToken, options, }) => {
23
- const market = await (0, market_1.fetchMarket)(chainId, [inputToken, outputToken]);
20
+ const pool_1 = require("./apis/pool");
21
+ const rebalancer_abi_1 = require("./abis/rebalancer/rebalancer-abi");
22
+ const pool_2 = require("./utils/pool");
23
+ const odos_1 = require("./apis/odos");
24
+ const minter_abi_1 = require("./abis/rebalancer/minter-abi");
25
+ const permit_1 = require("./constants/permit");
26
+ const math_1 = require("./utils/math");
27
+ const pool_key_1 = require("./utils/pool-key");
28
+ const operator_abi_1 = require("./abis/rebalancer/operator-abi");
29
+ const strategy_abi_1 = require("./abis/rebalancer/strategy-abi");
30
+ const open_order_1 = require("./apis/open-order");
31
+ const quotes_1 = require("./utils/quotes");
32
+ const openMarket = async ({ chainId, userAddress, inputToken, outputToken, options, }) => {
33
+ const publicClient = (0, viem_1.createPublicClient)({
34
+ chain: chain_1.CHAIN_MAP[chainId],
35
+ transport: options?.rpcUrl ? (0, viem_1.http)(options.rpcUrl) : (0, viem_1.http)(),
36
+ });
37
+ const market = await (0, market_1.fetchMarket)(publicClient, chainId, [inputToken, outputToken], !!(options && options.useSubgraph));
24
38
  const isBid = (0, viem_1.isAddressEqual)(market.quote.address, inputToken);
25
- if ((isBid && !market.bidBookOpen) || (!isBid && !market.askBookOpen)) {
26
- const unit = await (0, unit_1.calculateUnit)(chainId, isBid ? market.quote : market.base);
27
- return (0, build_transaction_1.buildTransaction)(chainId, {
39
+ if ((isBid && !market.bidBook.isOpened) ||
40
+ (!isBid && !market.askBook.isOpened)) {
41
+ const unitSize = (0, unit_size_1.calculateUnitSize)(chainId, isBid ? market.quote : market.base);
42
+ return (0, build_transaction_1.buildTransaction)(publicClient, {
43
+ chain: chain_1.CHAIN_MAP[chainId],
28
44
  address: addresses_1.CONTRACT_ADDRESSES[chainId].Controller,
45
+ account: userAddress,
29
46
  abi: controller_abi_1.CONTROLLER_ABI,
30
47
  functionName: 'open',
31
48
  args: [
@@ -33,11 +50,11 @@ exports.openMarket = (0, decorator_1.decorator)(async ({ chainId, inputToken, ou
33
50
  {
34
51
  key: {
35
52
  base: outputToken,
36
- unit,
53
+ unitSize,
37
54
  quote: inputToken,
38
- makerPolicy: fee_1.MAKER_DEFAULT_POLICY.value,
55
+ makerPolicy: fee_1.MAKER_DEFAULT_POLICY[chainId].value,
39
56
  hooks: viem_1.zeroAddress,
40
- takerPolicy: fee_1.TAKER_DEFAULT_POLICY.value,
57
+ takerPolicy: fee_1.TAKER_DEFAULT_POLICY[chainId].value,
41
58
  },
42
59
  hookData: viem_1.zeroHash,
43
60
  },
@@ -47,14 +64,26 @@ exports.openMarket = (0, decorator_1.decorator)(async ({ chainId, inputToken, ou
47
64
  }, options?.gasLimit);
48
65
  }
49
66
  return undefined;
50
- });
51
- exports.limitOrder = (0, decorator_1.decorator)(async ({ chainId, userAddress, inputToken, outputToken, amount, price, options, }) => {
52
- const market = await (0, market_1.fetchMarket)(chainId, [inputToken, outputToken]);
67
+ };
68
+ exports.openMarket = openMarket;
69
+ const limitOrder = async ({ chainId, userAddress, inputToken, outputToken, amount, price, options, }) => {
70
+ const [roundingUpMakeBid, roundingDownMakeAsk, roundingDownTakenBid, roundingUpTakenAsk,] = [
71
+ options?.roundingUpMakeBid ? options.roundingUpMakeBid : false,
72
+ options?.roundingDownMakeAsk ? options.roundingDownMakeAsk : false,
73
+ options?.roundingDownTakenBid ? options.roundingDownTakenBid : false,
74
+ options?.roundingUpTakenAsk ? options.roundingUpTakenAsk : false,
75
+ ];
76
+ const publicClient = (0, viem_1.createPublicClient)({
77
+ chain: chain_1.CHAIN_MAP[chainId],
78
+ transport: options?.rpcUrl ? (0, viem_1.http)(options.rpcUrl) : (0, viem_1.http)(),
79
+ });
80
+ const market = await (0, market_1.fetchMarket)(publicClient, chainId, [inputToken, outputToken], !!(options && options.useSubgraph));
53
81
  const isBid = (0, viem_1.isAddressEqual)(market.quote.address, inputToken);
54
82
  const [inputCurrency, outputCurrency] = isBid
55
83
  ? [market.quote, market.base]
56
84
  : [market.base, market.quote];
57
- if ((isBid && !market.bidBookOpen) || (!isBid && !market.askBookOpen)) {
85
+ if ((isBid && !market.bidBook.isOpened) ||
86
+ (!isBid && !market.askBook.isOpened)) {
58
87
  throw new Error(`
59
88
  Open the market before placing a limit order.
60
89
  import { openMarket } from '@clober/v2-sdk'
@@ -66,33 +95,36 @@ exports.limitOrder = (0, decorator_1.decorator)(async ({ chainId, userAddress, i
66
95
  })
67
96
  `);
68
97
  }
69
- const rawPrice = (0, prices_1.parsePrice)(Number(price), market.quote.decimals, market.base.decimals);
70
- const tick = isBid ? (0, tick_1.fromPrice)(rawPrice) : (0, tick_1.fromPrice)((0, tick_1.invertPrice)(rawPrice));
98
+ const { roundingDownTick, roundingUpTick } = (0, prices_1.parsePrice)(Number(price), market.quote.decimals, market.base.decimals);
71
99
  const tokensToSettle = [inputToken, outputToken].filter((address) => !(0, viem_1.isAddressEqual)(address, viem_1.zeroAddress));
72
100
  const quoteAmount = (0, viem_1.parseUnits)(amount, inputCurrency.decimals);
73
- const [unit, { spendAmount, bookId }] = await Promise.all([
74
- (0, unit_1.calculateUnit)(chainId, inputCurrency),
75
- (0, view_1.getExpectedOutput)({
76
- chainId,
77
- inputToken,
78
- outputToken,
79
- amountIn: amount,
80
- options: {
81
- ...options,
82
- limitPrice: price,
83
- },
84
- }),
85
- ]);
101
+ const unitSize = (0, unit_size_1.calculateUnitSize)(chainId, inputCurrency);
102
+ const { takenAmount, spentAmount, bookId, events } = await (0, view_1.getExpectedOutput)({
103
+ chainId,
104
+ inputToken,
105
+ outputToken,
106
+ amountIn: amount,
107
+ options: {
108
+ ...options,
109
+ limitPrice: price,
110
+ },
111
+ });
86
112
  const isETH = (0, viem_1.isAddressEqual)(inputToken, viem_1.zeroAddress);
87
113
  const makeParam = {
88
- id: (0, book_id_1.toBookId)(inputToken, outputToken, unit),
89
- tick: Number(tick),
114
+ id: (0, book_id_1.toBookId)(chainId, inputToken, outputToken, unitSize),
115
+ tick: options?.makeTick
116
+ ? Number(options.makeTick)
117
+ : Number(isBid
118
+ ? roundingUpMakeBid
119
+ ? roundingUpTick
120
+ : roundingDownTick
121
+ : (0, tick_1.invertTick)(roundingDownMakeAsk ? roundingDownTick : roundingUpTick)),
90
122
  quoteAmount,
91
123
  hookData: viem_1.zeroHash,
92
124
  };
93
- if (options?.postOnly === true || spendAmount === '0') {
125
+ if (options?.postOnly === true || spentAmount === '0') {
94
126
  return {
95
- transaction: await (0, build_transaction_1.buildTransaction)(chainId, {
127
+ transaction: await (0, build_transaction_1.buildTransaction)(publicClient, {
96
128
  chain: chain_1.CHAIN_MAP[chainId],
97
129
  account: userAddress,
98
130
  address: addresses_1.CONTRACT_ADDRESSES[chainId].Controller,
@@ -111,18 +143,28 @@ exports.limitOrder = (0, decorator_1.decorator)(async ({ chainId, userAddress, i
111
143
  amount: (0, viem_1.formatUnits)(quoteAmount, inputCurrency.decimals),
112
144
  currency: inputCurrency,
113
145
  direction: 'in',
146
+ price: (0, prices_1.formatPrice)(isBid
147
+ ? (0, tick_1.toPrice)(BigInt(makeParam.tick))
148
+ : (0, tick_1.toPrice)((0, tick_1.invertTick)(BigInt(makeParam.tick))), market.quote.decimals, market.base.decimals),
114
149
  },
115
- take: {
150
+ spent: {
151
+ amount: '0',
152
+ currency: inputCurrency,
153
+ direction: 'in',
154
+ events: [],
155
+ },
156
+ taken: {
116
157
  amount: '0',
117
158
  currency: outputCurrency,
118
159
  direction: 'out',
160
+ events: [],
119
161
  },
120
162
  },
121
163
  };
122
164
  }
123
165
  else {
124
166
  return {
125
- transaction: await (0, build_transaction_1.buildTransaction)(chainId, {
167
+ transaction: await (0, build_transaction_1.buildTransaction)(publicClient, {
126
168
  chain: chain_1.CHAIN_MAP[chainId],
127
169
  account: userAddress,
128
170
  address: addresses_1.CONTRACT_ADDRESSES[chainId].Controller,
@@ -133,7 +175,15 @@ exports.limitOrder = (0, decorator_1.decorator)(async ({ chainId, userAddress, i
133
175
  {
134
176
  takeBookId: bookId,
135
177
  makeBookId: makeParam.id,
136
- limitPrice: isBid ? (0, tick_1.invertPrice)(rawPrice) : rawPrice,
178
+ limitPrice: options?.takeLimitTick
179
+ ? (0, tick_1.toPrice)(options.takeLimitTick)
180
+ : (0, tick_1.toPrice)(isBid
181
+ ? (0, tick_1.invertTick)(roundingUpTakenAsk
182
+ ? roundingUpTick
183
+ : roundingDownTick)
184
+ : roundingDownTakenBid
185
+ ? roundingDownTick
186
+ : roundingUpTick),
137
187
  tick: makeParam.tick,
138
188
  quoteAmount,
139
189
  takeHookData: viem_1.zeroHash,
@@ -148,27 +198,54 @@ exports.limitOrder = (0, decorator_1.decorator)(async ({ chainId, userAddress, i
148
198
  }, options?.gasLimit),
149
199
  result: {
150
200
  make: {
151
- amount: (0, viem_1.formatUnits)(quoteAmount, inputCurrency.decimals),
201
+ amount: (0, viem_1.formatUnits)(quoteAmount - (0, viem_1.parseUnits)(spentAmount, inputCurrency.decimals), inputCurrency.decimals),
152
202
  currency: inputCurrency,
153
203
  direction: 'in',
204
+ price: (0, prices_1.formatPrice)(isBid
205
+ ? (0, tick_1.toPrice)(BigInt(makeParam.tick))
206
+ : (0, tick_1.toPrice)((0, tick_1.invertTick)(BigInt(makeParam.tick))), market.quote.decimals, market.base.decimals),
154
207
  },
155
- take: {
156
- amount: spendAmount,
208
+ spent: {
209
+ amount: spentAmount,
210
+ currency: inputCurrency,
211
+ direction: 'in',
212
+ events: events.map(({ price, spentAmount }) => ({
213
+ price,
214
+ amount: spentAmount,
215
+ })),
216
+ },
217
+ taken: {
218
+ amount: takenAmount,
157
219
  currency: outputCurrency,
158
220
  direction: 'out',
221
+ events: events.map(({ price, takenAmount }) => ({
222
+ price,
223
+ amount: takenAmount,
224
+ })),
159
225
  },
160
226
  },
161
227
  };
162
228
  }
163
- });
164
- exports.marketOrder = (0, decorator_1.decorator)(async ({ chainId, userAddress, inputToken, outputToken, amountIn, amountOut, options, }) => {
165
- const market = await (0, market_1.fetchMarket)(chainId, [inputToken, outputToken]);
229
+ };
230
+ exports.limitOrder = limitOrder;
231
+ const marketOrder = async ({ chainId, userAddress, inputToken, outputToken, amountIn, amountOut, options, }) => {
232
+ if (!amountIn && !amountOut) {
233
+ throw new Error('Either amountIn or amountOut must be provided');
234
+ }
235
+ else if (amountIn && amountOut) {
236
+ throw new Error('Only one of amountIn or amountOut can be provided');
237
+ }
238
+ const publicClient = (0, viem_1.createPublicClient)({
239
+ chain: chain_1.CHAIN_MAP[chainId],
240
+ transport: options?.rpcUrl ? (0, viem_1.http)(options.rpcUrl) : (0, viem_1.http)(),
241
+ });
242
+ const market = await (0, market_1.fetchMarket)(publicClient, chainId, [inputToken, outputToken], !!(options && options.useSubgraph));
166
243
  const isTakingBid = (0, viem_1.isAddressEqual)(market.base.address, inputToken);
167
244
  const [inputCurrency, outputCurrency] = isTakingBid
168
245
  ? [market.base, market.quote]
169
246
  : [market.quote, market.base];
170
- if ((isTakingBid && !market.bidBookOpen) ||
171
- (!isTakingBid && !market.askBookOpen)) {
247
+ if ((isTakingBid && !market.bidBook.isOpened) ||
248
+ (!isTakingBid && !market.askBook.isOpened)) {
172
249
  throw new Error(`
173
250
  Open the market before placing a market order.
174
251
  import { openMarket } from '@clober/v2-sdk'
@@ -183,7 +260,7 @@ exports.marketOrder = (0, decorator_1.decorator)(async ({ chainId, userAddress,
183
260
  const tokensToSettle = [inputToken, outputToken].filter((address) => !(0, viem_1.isAddressEqual)(address, viem_1.zeroAddress));
184
261
  const isETH = (0, viem_1.isAddressEqual)(inputToken, viem_1.zeroAddress);
185
262
  if (amountIn && !amountOut) {
186
- const { bookId, takenAmount, spendAmount } = await (0, view_1.getExpectedOutput)({
263
+ const { bookId, takenAmount, spentAmount, events } = await (0, view_1.getExpectedOutput)({
187
264
  chainId,
188
265
  inputToken,
189
266
  outputToken,
@@ -194,7 +271,7 @@ exports.marketOrder = (0, decorator_1.decorator)(async ({ chainId, userAddress,
194
271
  });
195
272
  const baseAmount = (0, viem_1.parseUnits)(amountIn, inputCurrency.decimals);
196
273
  return {
197
- transaction: await (0, build_transaction_1.buildTransaction)(chainId, {
274
+ transaction: await (0, build_transaction_1.buildTransaction)(publicClient, {
198
275
  chain: chain_1.CHAIN_MAP[chainId],
199
276
  account: userAddress,
200
277
  address: addresses_1.CONTRACT_ADDRESSES[chainId].Controller,
@@ -219,21 +296,29 @@ exports.marketOrder = (0, decorator_1.decorator)(async ({ chainId, userAddress,
219
296
  value: isETH ? baseAmount : 0n,
220
297
  }, options?.gasLimit),
221
298
  result: {
222
- spend: {
223
- amount: spendAmount,
299
+ spent: {
300
+ amount: spentAmount,
224
301
  currency: inputCurrency,
225
302
  direction: 'in',
303
+ events: events.map(({ price, spentAmount }) => ({
304
+ price,
305
+ amount: spentAmount,
306
+ })),
226
307
  },
227
- take: {
308
+ taken: {
228
309
  amount: takenAmount,
229
310
  currency: outputCurrency,
230
311
  direction: 'out',
312
+ events: events.map(({ price, takenAmount }) => ({
313
+ price,
314
+ amount: takenAmount,
315
+ })),
231
316
  },
232
317
  },
233
318
  };
234
319
  }
235
- else if (amountIn && amountOut) {
236
- const { bookId, spendAmount, takenAmount } = await (0, view_1.getExpectedInput)({
320
+ else if (!amountIn && amountOut) {
321
+ const { bookId, spentAmount, takenAmount, events } = await (0, view_1.getExpectedInput)({
237
322
  chainId,
238
323
  inputToken,
239
324
  outputToken,
@@ -243,9 +328,15 @@ exports.marketOrder = (0, decorator_1.decorator)(async ({ chainId, userAddress,
243
328
  },
244
329
  });
245
330
  const quoteAmount = (0, viem_1.parseUnits)(amountOut, outputCurrency.decimals);
246
- const baseAmount = (0, viem_1.parseUnits)(amountIn ?? spendAmount, inputCurrency.decimals);
331
+ const baseAmount = (0, viem_1.parseUnits)(spentAmount, inputCurrency.decimals);
332
+ const maxBaseAmount = options?.erc20PermitParam?.permitAmount ??
333
+ (options?.slippage
334
+ ? (0, bigint_1.applyPercent)(baseAmount, 100 + options.slippage)
335
+ : isETH
336
+ ? baseAmount
337
+ : 2n ** 256n - 1n);
247
338
  return {
248
- transaction: await (0, build_transaction_1.buildTransaction)(chainId, {
339
+ transaction: await (0, build_transaction_1.buildTransaction)(publicClient, {
249
340
  chain: chain_1.CHAIN_MAP[chainId],
250
341
  account: userAddress,
251
342
  address: addresses_1.CONTRACT_ADDRESSES[chainId].Controller,
@@ -257,9 +348,7 @@ exports.marketOrder = (0, decorator_1.decorator)(async ({ chainId, userAddress,
257
348
  id: bookId,
258
349
  limitPrice: 0n,
259
350
  quoteAmount,
260
- maxBaseAmount: options?.slippage
261
- ? (0, bigint_1.applyPercent)(baseAmount, 100 + options.slippage)
262
- : 2n ** 256n - 1n,
351
+ maxBaseAmount,
263
352
  hookData: viem_1.zeroHash,
264
353
  },
265
354
  ],
@@ -267,18 +356,26 @@ exports.marketOrder = (0, decorator_1.decorator)(async ({ chainId, userAddress,
267
356
  options?.erc20PermitParam ? [options.erc20PermitParam] : [],
268
357
  (0, time_1.getDeadlineTimestampInSeconds)(),
269
358
  ],
270
- value: isETH ? baseAmount : 0n,
359
+ value: isETH ? maxBaseAmount : 0n,
271
360
  }, options?.gasLimit),
272
361
  result: {
273
- spend: {
274
- amount: spendAmount,
362
+ spent: {
363
+ amount: spentAmount,
275
364
  currency: inputCurrency,
276
365
  direction: 'in',
366
+ events: events.map(({ price, spentAmount }) => ({
367
+ price,
368
+ amount: spentAmount,
369
+ })),
277
370
  },
278
- take: {
371
+ taken: {
279
372
  amount: takenAmount,
280
373
  currency: outputCurrency,
281
374
  direction: 'out',
375
+ events: events.map(({ price, takenAmount }) => ({
376
+ price,
377
+ amount: takenAmount,
378
+ })),
282
379
  },
283
380
  },
284
381
  };
@@ -286,8 +383,9 @@ exports.marketOrder = (0, decorator_1.decorator)(async ({ chainId, userAddress,
286
383
  else {
287
384
  throw new Error('Either amountIn or amountOut must be provided');
288
385
  }
289
- });
290
- exports.claimOrder = (0, decorator_1.decorator)(async ({ chainId, userAddress, id, options, }) => {
386
+ };
387
+ exports.marketOrder = marketOrder;
388
+ const claimOrder = async ({ chainId, userAddress, id, options, }) => {
291
389
  const { transaction, result } = await (0, exports.claimOrders)({
292
390
  chainId,
293
391
  userAddress,
@@ -298,9 +396,14 @@ exports.claimOrder = (0, decorator_1.decorator)(async ({ chainId, userAddress, i
298
396
  transaction,
299
397
  result: result[0],
300
398
  };
301
- });
302
- exports.claimOrders = (0, decorator_1.decorator)(async ({ chainId, userAddress, ids, options, }) => {
303
- const isApprovedForAll = await (0, approval_1.fetchIsApprovedForAll)(chainId, userAddress);
399
+ };
400
+ exports.claimOrder = claimOrder;
401
+ const claimOrders = async ({ chainId, userAddress, ids, options, }) => {
402
+ const publicClient = (0, viem_1.createPublicClient)({
403
+ chain: chain_1.CHAIN_MAP[chainId],
404
+ transport: options?.rpcUrl ? (0, viem_1.http)(options.rpcUrl) : (0, viem_1.http)(),
405
+ });
406
+ const isApprovedForAll = await (0, approval_1.fetchIsApprovedForAll)(publicClient, chainId, userAddress);
304
407
  if (!isApprovedForAll) {
305
408
  throw new Error(`
306
409
  Set ApprovalForAll before calling this function.
@@ -312,22 +415,25 @@ exports.claimOrders = (0, decorator_1.decorator)(async ({ chainId, userAddress,
312
415
  })
313
416
  `);
314
417
  }
315
- const orders = (await (0, order_1.fetchOrders)(chainId, ids.map((id) => BigInt(id)))).filter((order) => (0, viem_1.isAddressEqual)(order.owner, userAddress) && order.claimable > 0n);
418
+ const useSubgraph = !!(options && options.useSubgraph);
419
+ const orders = (useSubgraph
420
+ ? await (0, open_order_1.fetchOpenOrdersByOrderIdsFromSubgraph)(chainId, ids)
421
+ : await (0, order_1.fetchOnChainOrders)(publicClient, chainId, ids.map((id) => BigInt(id)))).filter((order) => (0, viem_1.isAddressEqual)(order.user, userAddress) && order.claimable.value !== '0');
316
422
  const tokensToSettle = orders
317
- .map((order) => [order.baseCurrency.address, order.quoteCurrency.address])
423
+ .map((order) => [order.inputCurrency.address, order.outputCurrency.address])
318
424
  .flat()
319
425
  .filter((address, index, self) => self.findIndex((c) => (0, viem_1.isAddressEqual)(c, address)) === index)
320
426
  .filter((address) => !(0, viem_1.isAddressEqual)(address, viem_1.zeroAddress));
321
427
  return {
322
- transaction: await (0, build_transaction_1.buildTransaction)(chainId, {
428
+ transaction: await (0, build_transaction_1.buildTransaction)(publicClient, {
323
429
  chain: chain_1.CHAIN_MAP[chainId],
324
430
  account: userAddress,
325
431
  address: addresses_1.CONTRACT_ADDRESSES[chainId].Controller,
326
432
  abi: controller_abi_1.CONTROLLER_ABI,
327
433
  functionName: 'claim',
328
434
  args: [
329
- orders.map(({ orderId }) => ({
330
- id: orderId,
435
+ orders.map(({ id }) => ({
436
+ id,
331
437
  hookData: viem_1.zeroHash,
332
438
  })),
333
439
  tokensToSettle,
@@ -335,25 +441,18 @@ exports.claimOrders = (0, decorator_1.decorator)(async ({ chainId, userAddress,
335
441
  (0, time_1.getDeadlineTimestampInSeconds)(),
336
442
  ],
337
443
  }, options?.gasLimit),
338
- result: orders
339
- .map((order) => {
340
- const amount = (0, decimals_1.quoteToBase)(order.tick, order.unit * order.claimable, false);
341
- return {
342
- currency: order.baseCurrency,
343
- amount: (0, viem_1.formatUnits)(amount, order.baseCurrency.decimals),
344
- };
345
- })
346
- .reduce((acc, { currency, amount }) => {
444
+ result: orders.reduce((acc, { claimable: { currency, value } }) => {
347
445
  const index = acc.findIndex((c) => (0, viem_1.isAddressEqual)(c.currency.address, currency.address));
348
446
  if (index === -1) {
349
- return [...acc, { currency, amount, direction: 'out' }];
447
+ return [...acc, { currency, amount: value, direction: 'out' }];
350
448
  }
351
- acc[index].amount = (Number(acc[index].amount) + Number(amount)).toString();
449
+ acc[index].amount = (Number(acc[index].amount) + Number(value)).toString();
352
450
  return acc;
353
451
  }, []),
354
452
  };
355
- });
356
- exports.cancelOrder = (0, decorator_1.decorator)(async ({ chainId, userAddress, id, options, }) => {
453
+ };
454
+ exports.claimOrders = claimOrders;
455
+ const cancelOrder = async ({ chainId, userAddress, id, options, }) => {
357
456
  const { transaction, result } = await (0, exports.cancelOrders)({
358
457
  chainId,
359
458
  userAddress,
@@ -364,9 +463,14 @@ exports.cancelOrder = (0, decorator_1.decorator)(async ({ chainId, userAddress,
364
463
  transaction,
365
464
  result: result[0],
366
465
  };
367
- });
368
- exports.cancelOrders = (0, decorator_1.decorator)(async ({ chainId, userAddress, ids, options, }) => {
369
- const isApprovedForAll = await (0, approval_1.fetchIsApprovedForAll)(chainId, userAddress);
466
+ };
467
+ exports.cancelOrder = cancelOrder;
468
+ const cancelOrders = async ({ chainId, userAddress, ids, options, }) => {
469
+ const publicClient = (0, viem_1.createPublicClient)({
470
+ chain: chain_1.CHAIN_MAP[chainId],
471
+ transport: options?.rpcUrl ? (0, viem_1.http)(options.rpcUrl) : (0, viem_1.http)(),
472
+ });
473
+ const isApprovedForAll = await (0, approval_1.fetchIsApprovedForAll)(publicClient, chainId, userAddress);
370
474
  if (!isApprovedForAll) {
371
475
  throw new Error(`
372
476
  Set ApprovalForAll before calling this function.
@@ -378,22 +482,25 @@ exports.cancelOrders = (0, decorator_1.decorator)(async ({ chainId, userAddress,
378
482
  })
379
483
  `);
380
484
  }
381
- const orders = (await (0, order_1.fetchOrders)(chainId, ids.map((id) => BigInt(id)))).filter((order) => (0, viem_1.isAddressEqual)(order.owner, userAddress) && order.open > 0n);
485
+ const useSubgraph = !!(options && options.useSubgraph);
486
+ const orders = (useSubgraph
487
+ ? await (0, open_order_1.fetchOpenOrdersByOrderIdsFromSubgraph)(chainId, ids)
488
+ : await (0, order_1.fetchOnChainOrders)(publicClient, chainId, ids.map((id) => BigInt(id)))).filter((order) => (0, viem_1.isAddressEqual)(order.user, userAddress) && order.cancelable.value !== '0');
382
489
  const tokensToSettle = orders
383
- .map((order) => [order.baseCurrency.address, order.quoteCurrency.address])
490
+ .map((order) => [order.inputCurrency.address, order.outputCurrency.address])
384
491
  .flat()
385
492
  .filter((address, index, self) => self.findIndex((c) => (0, viem_1.isAddressEqual)(c, address)) === index)
386
493
  .filter((address) => !(0, viem_1.isAddressEqual)(address, viem_1.zeroAddress));
387
494
  return {
388
- transaction: await (0, build_transaction_1.buildTransaction)(chainId, {
495
+ transaction: await (0, build_transaction_1.buildTransaction)(publicClient, {
389
496
  chain: chain_1.CHAIN_MAP[chainId],
390
497
  account: userAddress,
391
498
  address: addresses_1.CONTRACT_ADDRESSES[chainId].Controller,
392
499
  abi: controller_abi_1.CONTROLLER_ABI,
393
500
  functionName: 'cancel',
394
501
  args: [
395
- orders.map(({ orderId }) => ({
396
- id: orderId,
502
+ orders.map(({ id }) => ({
503
+ id,
397
504
  leftQuoteAmount: 0n,
398
505
  hookData: viem_1.zeroHash,
399
506
  })),
@@ -402,24 +509,549 @@ exports.cancelOrders = (0, decorator_1.decorator)(async ({ chainId, userAddress,
402
509
  (0, time_1.getDeadlineTimestampInSeconds)(),
403
510
  ],
404
511
  }, options?.gasLimit),
405
- result: orders
406
- .map((order) => {
407
- const amount = (0, bigint_1.applyPercent)(order.unit * order.open, 100 +
408
- (Number(fee_1.MAKER_DEFAULT_POLICY.rate) * 100) /
409
- Number(fee_1.MAKER_DEFAULT_POLICY.RATE_PRECISION), 6);
410
- return {
411
- currency: order.quoteCurrency,
412
- amount: (0, viem_1.formatUnits)(amount, order.quoteCurrency.decimals),
413
- };
414
- })
415
- .reduce((acc, { currency, amount }) => {
512
+ result: orders.reduce((acc, { cancelable: { currency, value } }) => {
416
513
  const index = acc.findIndex((c) => (0, viem_1.isAddressEqual)(c.currency.address, currency.address));
417
514
  if (index === -1) {
418
- return [...acc, { currency, amount, direction: 'out' }];
515
+ return [...acc, { currency, amount: value, direction: 'out' }];
419
516
  }
420
- acc[index].amount = (Number(acc[index].amount) + Number(amount)).toString();
517
+ acc[index].amount = (Number(acc[index].amount) + Number(value)).toString();
421
518
  return acc;
422
519
  }, []),
423
520
  };
424
- });
521
+ };
522
+ exports.cancelOrders = cancelOrders;
523
+ const openPool = async ({ chainId, userAddress, tokenA, tokenB, salt, options, }) => {
524
+ const publicClient = (0, viem_1.createPublicClient)({
525
+ chain: chain_1.CHAIN_MAP[chainId],
526
+ transport: options?.rpcUrl ? (0, viem_1.http)(options.rpcUrl) : (0, viem_1.http)(),
527
+ });
528
+ const pool = await (0, pool_1.fetchPool)(publicClient, chainId, [tokenA, tokenB], salt, !!(options && options.useSubgraph));
529
+ if (!pool.isOpened) {
530
+ return (0, build_transaction_1.buildTransaction)(publicClient, {
531
+ chain: chain_1.CHAIN_MAP[chainId],
532
+ address: addresses_1.CONTRACT_ADDRESSES[chainId].Rebalancer,
533
+ account: userAddress,
534
+ abi: rebalancer_abi_1.REBALANCER_ABI,
535
+ functionName: 'open',
536
+ args: [
537
+ {
538
+ base: pool.market.bidBook.base.address,
539
+ unitSize: pool.market.bidBook.unitSize,
540
+ quote: pool.market.bidBook.quote.address,
541
+ makerPolicy: fee_1.MAKER_DEFAULT_POLICY[chainId].value,
542
+ hooks: viem_1.zeroAddress,
543
+ takerPolicy: fee_1.TAKER_DEFAULT_POLICY[chainId].value,
544
+ },
545
+ {
546
+ base: pool.market.askBook.base.address,
547
+ unitSize: pool.market.askBook.unitSize,
548
+ quote: pool.market.askBook.quote.address,
549
+ makerPolicy: fee_1.MAKER_DEFAULT_POLICY[chainId].value,
550
+ hooks: viem_1.zeroAddress,
551
+ takerPolicy: fee_1.TAKER_DEFAULT_POLICY[chainId].value,
552
+ },
553
+ (0, pool_key_1.toBytes32)(salt),
554
+ addresses_1.CONTRACT_ADDRESSES[chainId].Strategy,
555
+ ],
556
+ }, options?.gasLimit);
557
+ }
558
+ return undefined;
559
+ };
560
+ exports.openPool = openPool;
561
+ const addLiquidity = async ({ chainId, userAddress, token0, token1, salt, amount0, amount1, options, }) => {
562
+ const publicClient = (0, viem_1.createPublicClient)({
563
+ chain: chain_1.CHAIN_MAP[chainId],
564
+ transport: options?.rpcUrl ? (0, viem_1.http)(options.rpcUrl) : (0, viem_1.http)(),
565
+ });
566
+ const pool = await (0, pool_1.fetchPool)(publicClient, chainId, [token0, token1], salt, !!(options && options.useSubgraph));
567
+ if (!pool.isOpened) {
568
+ throw new Error(`
569
+ Open the pool before adding liquidity.
570
+ import { openPool } from '@clober/v2-sdk'
571
+
572
+ const transaction = await openPool({
573
+ chainId: ${chainId},
574
+ tokenA: '${token0}',
575
+ tokenB: '${token1}',
576
+ salt: '0x0000000000000000000000000000000000000000000000000000000000000000',
577
+ })
578
+ `);
579
+ }
580
+ const [amountAOrigin, amountBOrigin] = (0, viem_1.isAddressEqual)(pool.currencyA.address, (0, viem_1.getAddress)(token0))
581
+ ? [
582
+ (0, viem_1.parseUnits)(amount0 ?? '0', pool.currencyA.decimals),
583
+ (0, viem_1.parseUnits)(amount1 ?? '0', pool.currencyB.decimals),
584
+ ]
585
+ : [
586
+ (0, viem_1.parseUnits)(amount1 ?? '0', pool.currencyA.decimals),
587
+ (0, viem_1.parseUnits)(amount0 ?? '0', pool.currencyB.decimals),
588
+ ];
589
+ let [amountA, amountB] = [amountAOrigin, amountBOrigin];
590
+ const tokenAPermitParams = (0, viem_1.isAddressEqual)(pool.currencyA.address, (0, viem_1.getAddress)(token0))
591
+ ? options?.token0PermitParams ?? permit_1.emptyERC20PermitParams
592
+ : options?.token1PermitParams ?? permit_1.emptyERC20PermitParams;
593
+ const tokenBPermitParams = (0, viem_1.isAddressEqual)(pool.currencyA.address, (0, viem_1.getAddress)(token0))
594
+ ? options?.token1PermitParams ?? permit_1.emptyERC20PermitParams
595
+ : options?.token0PermitParams ?? permit_1.emptyERC20PermitParams;
596
+ let disableSwap = !!(options && options.disableSwap);
597
+ if (pool.totalSupply === 0n ||
598
+ (pool.liquidityA === 0n && pool.liquidityB === 0n)) {
599
+ disableSwap = true;
600
+ }
601
+ const slippageLimitPercent = options?.slippage ?? 1.0;
602
+ const swapParams = {
603
+ inCurrency: viem_1.zeroAddress,
604
+ amount: 0n,
605
+ data: '0x',
606
+ };
607
+ if (!disableSwap) {
608
+ const currencyBPerCurrencyA = options?.testnetPrice
609
+ ? (0, viem_1.isAddressEqual)((0, view_1.getQuoteToken)({
610
+ chainId,
611
+ token0,
612
+ token1,
613
+ }), pool.currencyA.address)
614
+ ? 1 / Number(options.testnetPrice)
615
+ : Number(options.testnetPrice)
616
+ : undefined;
617
+ const swapAmountA = (0, viem_1.parseUnits)('1', pool.currencyA.decimals);
618
+ let swapAmountB = -1n;
619
+ if (options && options.token0Price && options.token1Price) {
620
+ const tokenAPrice = (0, viem_1.isAddressEqual)(pool.currencyA.address, (0, viem_1.getAddress)(token0))
621
+ ? options.token0Price
622
+ : options.token1Price;
623
+ const tokenBPrice = (0, viem_1.isAddressEqual)(pool.currencyA.address, (0, viem_1.getAddress)(token0))
624
+ ? options.token1Price
625
+ : options.token0Price;
626
+ swapAmountB = (0, quotes_1.quotes)(swapAmountA, tokenAPrice, tokenBPrice, pool.currencyA.decimals, pool.currencyB.decimals);
627
+ }
628
+ else {
629
+ ;
630
+ ({ amountOut: swapAmountB } = await (0, odos_1.fetchQuote)({
631
+ chainId,
632
+ amountIn: swapAmountA,
633
+ tokenIn: pool.currencyA,
634
+ tokenOut: pool.currencyB,
635
+ slippageLimitPercent: 1,
636
+ userAddress: addresses_1.CONTRACT_ADDRESSES[chainId].Minter,
637
+ testnetPrice: currencyBPerCurrencyA,
638
+ }));
639
+ }
640
+ if (swapAmountB === -1n) {
641
+ throw new Error('Failed to fetch quote');
642
+ }
643
+ const { deltaA, deltaB } = (0, pool_2.getIdealDelta)(amountA, amountB, pool.liquidityA, pool.liquidityB, swapAmountA, swapAmountB);
644
+ if (deltaA < 0n) {
645
+ swapParams.inCurrency = pool.currencyA.address;
646
+ swapParams.amount = -deltaA;
647
+ const { amountOut: actualDeltaB, data: calldata } = await (0, odos_1.fetchCallData)({
648
+ chainId,
649
+ amountIn: swapParams.amount,
650
+ tokenIn: pool.currencyA,
651
+ tokenOut: pool.currencyB,
652
+ slippageLimitPercent,
653
+ userAddress: addresses_1.CONTRACT_ADDRESSES[chainId].Minter,
654
+ testnetPrice: currencyBPerCurrencyA,
655
+ });
656
+ swapParams.data = calldata;
657
+ amountA += deltaA;
658
+ amountB += actualDeltaB;
659
+ }
660
+ else if (deltaB < 0n) {
661
+ swapParams.inCurrency = pool.currencyB.address;
662
+ swapParams.amount = -deltaB;
663
+ const { amountOut: actualDeltaA, data: calldata } = await (0, odos_1.fetchCallData)({
664
+ chainId,
665
+ amountIn: swapParams.amount,
666
+ tokenIn: pool.currencyB,
667
+ tokenOut: pool.currencyA,
668
+ slippageLimitPercent,
669
+ userAddress: addresses_1.CONTRACT_ADDRESSES[chainId].Minter,
670
+ testnetPrice: currencyBPerCurrencyA
671
+ ? 1 / currencyBPerCurrencyA
672
+ : undefined,
673
+ });
674
+ swapParams.data = calldata;
675
+ amountA += actualDeltaA;
676
+ amountB += deltaB;
677
+ }
678
+ }
679
+ const { mintAmount, inAmountA, inAmountB } = (0, pool_2.getExpectedMintResult)(pool.totalSupply, pool.liquidityA, pool.liquidityB, amountA, amountB, pool.currencyA, pool.currencyB);
680
+ if (mintAmount === 0n) {
681
+ return {
682
+ transaction: undefined,
683
+ result: {
684
+ currencyA: {
685
+ currency: pool.currencyA,
686
+ amount: '0',
687
+ direction: 'in',
688
+ },
689
+ currencyB: {
690
+ currency: pool.currencyB,
691
+ amount: '0',
692
+ direction: 'in',
693
+ },
694
+ lpCurrency: {
695
+ currency: pool.currencyLp,
696
+ amount: '0',
697
+ direction: 'out',
698
+ },
699
+ },
700
+ };
701
+ }
702
+ const minMintAmount = (0, bigint_1.applyPercent)(mintAmount, 100 - slippageLimitPercent);
703
+ const transaction = await (0, build_transaction_1.buildTransaction)(publicClient, {
704
+ chain: chain_1.CHAIN_MAP[chainId],
705
+ account: userAddress,
706
+ address: addresses_1.CONTRACT_ADDRESSES[chainId].Minter,
707
+ abi: minter_abi_1.MINTER_ABI,
708
+ functionName: 'mint',
709
+ args: [
710
+ pool.key,
711
+ amountAOrigin,
712
+ amountBOrigin,
713
+ minMintAmount,
714
+ {
715
+ permitAmount: tokenAPermitParams.permitAmount,
716
+ signature: tokenAPermitParams.signature,
717
+ },
718
+ {
719
+ permitAmount: tokenBPermitParams.permitAmount,
720
+ signature: tokenBPermitParams.signature,
721
+ },
722
+ swapParams,
723
+ ],
724
+ value: (0, viem_1.isAddressEqual)(token0, viem_1.zeroAddress)
725
+ ? amountAOrigin
726
+ : (0, viem_1.isAddressEqual)(token1, viem_1.zeroAddress)
727
+ ? amountBOrigin
728
+ : undefined,
729
+ }, options?.gasLimit);
730
+ const currencyARefund = amountA - inAmountA;
731
+ const currencyBRefund = amountB - inAmountB;
732
+ const currencyAResultAmount = amountAOrigin - currencyARefund;
733
+ const currencyBResultAmount = amountBOrigin - currencyBRefund;
734
+ return {
735
+ transaction,
736
+ result: {
737
+ currencyA: {
738
+ currency: pool.currencyA,
739
+ amount: (0, viem_1.formatUnits)((0, math_1.abs)(currencyAResultAmount), pool.currencyA.decimals),
740
+ direction: currencyAResultAmount >= 0 ? 'in' : 'out',
741
+ },
742
+ currencyB: {
743
+ currency: pool.currencyB,
744
+ amount: (0, viem_1.formatUnits)((0, math_1.abs)(currencyBResultAmount), pool.currencyB.decimals),
745
+ direction: currencyBResultAmount >= 0 ? 'in' : 'out',
746
+ },
747
+ lpCurrency: {
748
+ currency: pool.currencyLp,
749
+ amount: (0, viem_1.formatUnits)(mintAmount, pool.currencyLp.decimals),
750
+ direction: 'out',
751
+ },
752
+ },
753
+ };
754
+ };
755
+ exports.addLiquidity = addLiquidity;
756
+ const removeLiquidity = async ({ chainId, userAddress, token0, token1, salt, amount, options, }) => {
757
+ const publicClient = (0, viem_1.createPublicClient)({
758
+ chain: chain_1.CHAIN_MAP[chainId],
759
+ transport: options?.rpcUrl ? (0, viem_1.http)(options.rpcUrl) : (0, viem_1.http)(),
760
+ });
761
+ const pool = await (0, pool_1.fetchPool)(publicClient, chainId, [token0, token1], salt, !!(options && options.useSubgraph));
762
+ if (!pool.isOpened) {
763
+ throw new Error(`
764
+ Open the pool before removing liquidity.
765
+ import { openPool } from '@clober/v2-sdk'
766
+
767
+ const transaction = await openPool({
768
+ chainId: ${chainId},
769
+ tokenA: '${token0}',
770
+ tokenB: '${token1}',
771
+ salt: '0x0000000000000000000000000000000000000000000000000000000000000000',
772
+ })
773
+ `);
774
+ }
775
+ const burnAmount = (0, viem_1.parseUnits)(amount, pool.currencyLp.decimals);
776
+ const slippageLimitPercent = options?.slippage ?? 2;
777
+ const withdrawAmountA = (burnAmount * pool.liquidityA) / pool.totalSupply;
778
+ const withdrawAmountB = (burnAmount * pool.liquidityB) / pool.totalSupply;
779
+ const minWithdrawAmountA = (0, bigint_1.applyPercent)(withdrawAmountA, 100 - slippageLimitPercent);
780
+ const minWithdrawAmountB = (0, bigint_1.applyPercent)(withdrawAmountB, 100 - slippageLimitPercent);
781
+ if (burnAmount === 0n) {
782
+ return {
783
+ transaction: undefined,
784
+ result: {
785
+ currencyA: {
786
+ currency: pool.currencyA,
787
+ amount: '0',
788
+ direction: 'out',
789
+ },
790
+ currencyB: {
791
+ currency: pool.currencyB,
792
+ amount: '0',
793
+ direction: 'out',
794
+ },
795
+ lpCurrency: {
796
+ currency: pool.currencyLp,
797
+ amount: '0',
798
+ direction: 'in',
799
+ },
800
+ },
801
+ };
802
+ }
803
+ const transaction = await (0, build_transaction_1.buildTransaction)(publicClient, {
804
+ chain: chain_1.CHAIN_MAP[chainId],
805
+ account: userAddress,
806
+ address: addresses_1.CONTRACT_ADDRESSES[chainId].Rebalancer,
807
+ abi: rebalancer_abi_1.REBALANCER_ABI,
808
+ functionName: 'burn',
809
+ args: [pool.key, burnAmount, minWithdrawAmountA, minWithdrawAmountB],
810
+ }, options?.gasLimit);
811
+ return {
812
+ transaction,
813
+ result: {
814
+ currencyA: {
815
+ currency: pool.currencyA,
816
+ amount: (0, viem_1.formatUnits)(withdrawAmountA, pool.currencyA.decimals),
817
+ direction: 'out',
818
+ },
819
+ currencyB: {
820
+ currency: pool.currencyB,
821
+ amount: (0, viem_1.formatUnits)(withdrawAmountB, pool.currencyB.decimals),
822
+ direction: 'out',
823
+ },
824
+ lpCurrency: {
825
+ currency: pool.currencyLp,
826
+ amount: amount,
827
+ direction: 'in',
828
+ },
829
+ },
830
+ };
831
+ };
832
+ exports.removeLiquidity = removeLiquidity;
833
+ const refillOrder = async ({ chainId, userAddress, token0, token1, salt, options, }) => {
834
+ const publicClient = (0, viem_1.createPublicClient)({
835
+ chain: chain_1.CHAIN_MAP[chainId],
836
+ transport: options?.rpcUrl ? (0, viem_1.http)(options.rpcUrl) : (0, viem_1.http)(),
837
+ });
838
+ const pool = options?.pool
839
+ ? options.pool
840
+ : (await (0, pool_1.fetchPool)(publicClient, chainId, [token0, token1], salt, !!(options && options.useSubgraph))).toJson();
841
+ if (!pool.isOpened) {
842
+ throw new Error(`
843
+ Open the pool before rebalancing pool.
844
+ import { openPool } from '@clober/v2-sdk'
845
+
846
+ const transaction = await openPool({
847
+ chainId: ${chainId},
848
+ tokenA: '${token0}',
849
+ tokenB: '${token1}',
850
+ salt: '0x0000000000000000000000000000000000000000000000000000000000000000',
851
+ })
852
+ `);
853
+ }
854
+ return (0, build_transaction_1.buildTransaction)(publicClient, {
855
+ chain: chain_1.CHAIN_MAP[chainId],
856
+ account: userAddress,
857
+ address: addresses_1.CONTRACT_ADDRESSES[chainId].Rebalancer,
858
+ abi: rebalancer_abi_1.REBALANCER_ABI,
859
+ functionName: 'rebalance',
860
+ args: [pool.key],
861
+ }, options?.gasLimit, options?.gasPriceLimit);
862
+ };
863
+ exports.refillOrder = refillOrder;
864
+ const adjustOrderPrice = async ({ chainId, userAddress, token0, token1, salt, oraclePrice, bidPrice, askPrice, alpha, options, }) => {
865
+ if (Number(alpha) <= 0 || Number(alpha) > 1) {
866
+ throw new Error('Alpha value must be in the range (0, 1]');
867
+ }
868
+ if (Number(bidPrice) <= 0 || Number(askPrice) <= 0) {
869
+ throw new Error('Price must be greater than 0');
870
+ }
871
+ if (Number(bidPrice) >= Number(askPrice)) {
872
+ throw new Error('Bid price must be less than ask price');
873
+ }
874
+ const publicClient = (0, viem_1.createPublicClient)({
875
+ chain: chain_1.CHAIN_MAP[chainId],
876
+ transport: options?.rpcUrl ? (0, viem_1.http)(options.rpcUrl) : (0, viem_1.http)(),
877
+ });
878
+ const pool = options?.pool
879
+ ? options.pool
880
+ : (await (0, pool_1.fetchPool)(publicClient, chainId, [token0, token1], salt, !!(options && options.useSubgraph))).toJson();
881
+ if (!pool.isOpened) {
882
+ throw new Error(`
883
+ Open the pool before updating strategy price.
884
+ import { openPool } from '@clober/v2-sdk'
885
+
886
+ const transaction = await openPool({
887
+ chainId: ${chainId},
888
+ tokenA: '${token0}',
889
+ tokenB: '${token1}',
890
+ salt: '0x0000000000000000000000000000000000000000000000000000000000000000',
891
+ })
892
+ `);
893
+ }
894
+ const [roundingUpBidPrice, roundingUpAskPrice] = [
895
+ options?.roundingUpBidPrice ? options.roundingUpBidPrice : false,
896
+ options?.roundingUpAskPrice ? options.roundingUpAskPrice : false,
897
+ ];
898
+ const { roundingDownTick: roundingDownTickA, roundingUpTick: roundingUpTickA, } = (0, prices_1.parsePrice)(Number(bidPrice), pool.currencyA.decimals, pool.currencyB.decimals);
899
+ const { roundingDownTick: roundingDownTickB, roundingUpTick: roundingUpTickB, } = (0, prices_1.parsePrice)(Number(askPrice), pool.currencyA.decimals, pool.currencyB.decimals);
900
+ const oracleRawPrice = (0, prices_1.convertHumanReadablePriceToRawPrice)(Number(oraclePrice), pool.currencyA.decimals, pool.currencyB.decimals);
901
+ const tickA = options?.bidTick
902
+ ? Number(options.bidTick)
903
+ : Number(roundingUpBidPrice ? roundingUpTickA : roundingDownTickA);
904
+ let tickB = options?.askTick
905
+ ? Number(options.askTick)
906
+ : Number((0, tick_1.invertTick)(roundingUpAskPrice ? roundingUpTickB : roundingDownTickB));
907
+ if ((0, tick_1.invertTick)(BigInt(tickB)) <= BigInt(tickA)) {
908
+ tickB = Number((0, tick_1.invertTick)(BigInt(tickA + 1)));
909
+ }
910
+ const rateRaw = (0, viem_1.parseUnits)(alpha, 6);
911
+ return (0, build_transaction_1.buildTransaction)(publicClient, {
912
+ chain: chain_1.CHAIN_MAP[chainId],
913
+ account: userAddress,
914
+ address: addresses_1.CONTRACT_ADDRESSES[chainId].Operator,
915
+ abi: operator_abi_1.OPERATOR_ABI,
916
+ functionName: 'updatePosition',
917
+ args: [pool.key, oracleRawPrice, tickA, tickB, rateRaw],
918
+ }, options?.gasLimit, options?.gasPriceLimit);
919
+ };
920
+ exports.adjustOrderPrice = adjustOrderPrice;
921
+ const setStrategyConfig = async ({ chainId, userAddress, token0, token1, salt, config, options, }) => {
922
+ if (Number(config.referenceThreshold) < 0 ||
923
+ Number(config.referenceThreshold) > 1) {
924
+ throw new Error('Reference threshold must be in the range [0, 1]');
925
+ }
926
+ if (Number(config.rebalanceThreshold) < 0 ||
927
+ Number(config.rebalanceThreshold) > 1) {
928
+ throw new Error('Rebalance threshold must be in the range [0, 1]');
929
+ }
930
+ if (Number(config.priceThresholdA) < 0 ||
931
+ Number(config.priceThresholdA) > 1 ||
932
+ Number(config.priceThresholdB) < 0 ||
933
+ Number(config.priceThresholdB) > 1) {
934
+ throw new Error('Price threshold must be in the range [0, 1]');
935
+ }
936
+ if (Number(config.rateA) < 0 ||
937
+ Number(config.rateA) > 1 ||
938
+ Number(config.rateB) < 0 ||
939
+ Number(config.rateB) > 1) {
940
+ throw new Error('Rate must be in the range [0, 1]');
941
+ }
942
+ if (Number(config.minRateA) < 0 ||
943
+ Number(config.minRateA) > 1 ||
944
+ Number(config.minRateB) < 0 ||
945
+ Number(config.minRateB) > 1) {
946
+ throw new Error('Min rate must be in the range [0, 1]');
947
+ }
948
+ if (Number(config.minRateA) > Number(config.rateA) ||
949
+ Number(config.minRateB) > Number(config.rateB)) {
950
+ throw new Error('Min rate must be less or equal to rate');
951
+ }
952
+ const publicClient = (0, viem_1.createPublicClient)({
953
+ chain: chain_1.CHAIN_MAP[chainId],
954
+ transport: options?.rpcUrl ? (0, viem_1.http)(options.rpcUrl) : (0, viem_1.http)(),
955
+ });
956
+ const pool = await (0, pool_1.fetchPool)(publicClient, chainId, [token0, token1], salt, !!(options && options.useSubgraph));
957
+ if (!pool.isOpened) {
958
+ throw new Error(`
959
+ Open the pool before set strategy config.
960
+ import { openPool } from '@clober/v2-sdk'
961
+
962
+ const transaction = await openPool({
963
+ chainId: ${chainId},
964
+ tokenA: '${token0}',
965
+ tokenB: '${token1}',
966
+ })
967
+ `);
968
+ }
969
+ const configRaw = {
970
+ referenceThreshold: (0, viem_1.parseUnits)(config.referenceThreshold, 6),
971
+ rebalanceThreshold: (0, viem_1.parseUnits)(config.rebalanceThreshold, 6),
972
+ rateA: (0, viem_1.parseUnits)(config.rateA, 6),
973
+ rateB: (0, viem_1.parseUnits)(config.rateB, 6),
974
+ minRateA: (0, viem_1.parseUnits)(config.minRateA, 6),
975
+ minRateB: (0, viem_1.parseUnits)(config.minRateB, 6),
976
+ priceThresholdA: (0, viem_1.parseUnits)(config.priceThresholdA, 6),
977
+ priceThresholdB: (0, viem_1.parseUnits)(config.priceThresholdB, 6),
978
+ };
979
+ return (0, build_transaction_1.buildTransaction)(publicClient, {
980
+ chain: chain_1.CHAIN_MAP[chainId],
981
+ account: userAddress,
982
+ address: addresses_1.CONTRACT_ADDRESSES[chainId].Strategy,
983
+ abi: strategy_abi_1.STRATEGY_ABI,
984
+ functionName: 'setConfig',
985
+ args: [pool.key, configRaw],
986
+ }, options?.gasLimit);
987
+ };
988
+ exports.setStrategyConfig = setStrategyConfig;
989
+ const pausePool = async ({ chainId, userAddress, token0, token1, salt, options, }) => {
990
+ const publicClient = (0, viem_1.createPublicClient)({
991
+ chain: chain_1.CHAIN_MAP[chainId],
992
+ transport: options?.rpcUrl ? (0, viem_1.http)(options.rpcUrl) : (0, viem_1.http)(),
993
+ });
994
+ const pool = options?.pool
995
+ ? options.pool
996
+ : (await (0, pool_1.fetchPool)(publicClient, chainId, [token0, token1], salt, !!(options && options.useSubgraph))).toJson();
997
+ if (!pool.isOpened) {
998
+ throw new Error(`
999
+ Open the pool before trying pause.
1000
+ import { openPool } from '@clober/v2-sdk'
1001
+
1002
+ const transaction = await openPool({
1003
+ chainId: ${chainId},
1004
+ tokenA: '${token0}',
1005
+ tokenB: '${token1}',
1006
+ salt: '0x0000000000000000000000000000000000000000000000000000000000000000',
1007
+ })
1008
+ `);
1009
+ }
1010
+ if (pool.paused) {
1011
+ return undefined;
1012
+ }
1013
+ return (0, build_transaction_1.buildTransaction)(publicClient, {
1014
+ chain: chain_1.CHAIN_MAP[chainId],
1015
+ account: userAddress,
1016
+ address: addresses_1.CONTRACT_ADDRESSES[chainId].Operator,
1017
+ abi: operator_abi_1.OPERATOR_ABI,
1018
+ functionName: 'pause',
1019
+ args: [pool.key],
1020
+ }, options?.gasLimit, options?.gasPriceLimit);
1021
+ };
1022
+ exports.pausePool = pausePool;
1023
+ const resumePool = async ({ chainId, userAddress, token0, token1, salt, options, }) => {
1024
+ const publicClient = (0, viem_1.createPublicClient)({
1025
+ chain: chain_1.CHAIN_MAP[chainId],
1026
+ transport: options?.rpcUrl ? (0, viem_1.http)(options.rpcUrl) : (0, viem_1.http)(),
1027
+ });
1028
+ const pool = options?.pool
1029
+ ? options.pool
1030
+ : (await (0, pool_1.fetchPool)(publicClient, chainId, [token0, token1], salt, !!(options && options.useSubgraph))).toJson();
1031
+ if (!pool.isOpened) {
1032
+ throw new Error(`
1033
+ Open the pool before trying resume.
1034
+ import { openPool } from '@clober/v2-sdk'
1035
+
1036
+ const transaction = await openPool({
1037
+ chainId: ${chainId},
1038
+ tokenA: '${token0}',
1039
+ tokenB: '${token1}',
1040
+ salt: '0x0000000000000000000000000000000000000000000000000000000000000000',
1041
+ })
1042
+ `);
1043
+ }
1044
+ if (!pool.paused) {
1045
+ return undefined;
1046
+ }
1047
+ return (0, build_transaction_1.buildTransaction)(publicClient, {
1048
+ chain: chain_1.CHAIN_MAP[chainId],
1049
+ account: userAddress,
1050
+ address: addresses_1.CONTRACT_ADDRESSES[chainId].Strategy,
1051
+ abi: strategy_abi_1.STRATEGY_ABI,
1052
+ functionName: 'unpause',
1053
+ args: [pool.key],
1054
+ }, options?.gasLimit, options?.gasPriceLimit);
1055
+ };
1056
+ exports.resumePool = resumePool;
425
1057
  //# sourceMappingURL=call.js.map