@clober/v2-sdk 0.0.4 → 0.0.5-0.dev

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 (238) 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 +321 -0
  5. package/dist/cjs/abis/core/book-viewer-abi.js.map +1 -0
  6. package/dist/cjs/abis/core/controller-abi.js +29 -24
  7. package/dist/cjs/abis/core/controller-abi.js.map +1 -1
  8. package/dist/cjs/apis/chart-logs.js +154 -0
  9. package/dist/cjs/apis/chart-logs.js.map +1 -0
  10. package/dist/cjs/apis/market.js +61 -32
  11. package/dist/cjs/apis/market.js.map +1 -1
  12. package/dist/cjs/apis/open-order.js +56 -21
  13. package/dist/cjs/apis/open-order.js.map +1 -1
  14. package/dist/cjs/approval.js +77 -30
  15. package/dist/cjs/approval.js.map +1 -1
  16. package/dist/cjs/call.js +313 -206
  17. package/dist/cjs/call.js.map +1 -1
  18. package/dist/cjs/constants/addresses.js +26 -1
  19. package/dist/cjs/constants/addresses.js.map +1 -1
  20. package/dist/cjs/constants/chain.js +19 -1
  21. package/dist/cjs/constants/chain.js.map +1 -1
  22. package/dist/cjs/constants/client.js +4 -0
  23. package/dist/cjs/constants/client.js.map +1 -1
  24. package/dist/cjs/constants/currency.js +28 -2
  25. package/dist/cjs/constants/currency.js.map +1 -1
  26. package/dist/cjs/constants/fee.js +17 -2
  27. package/dist/cjs/constants/fee.js.map +1 -1
  28. package/dist/cjs/constants/subgraph.js +57 -0
  29. package/dist/cjs/constants/subgraph.js.map +1 -0
  30. package/dist/cjs/constants/test-chain.js +27 -0
  31. package/dist/cjs/constants/test-chain.js.map +1 -0
  32. package/dist/cjs/index.js +20 -6
  33. package/dist/cjs/index.js.map +1 -1
  34. package/dist/cjs/model/book.js +47 -29
  35. package/dist/cjs/model/book.js.map +1 -1
  36. package/dist/cjs/model/chart-log.js +3 -0
  37. package/dist/cjs/model/chart-log.js.map +1 -0
  38. package/dist/cjs/model/market.js +45 -182
  39. package/dist/cjs/model/market.js.map +1 -1
  40. package/dist/cjs/signature.js +20 -16
  41. package/dist/cjs/signature.js.map +1 -1
  42. package/dist/cjs/type.js +16 -1
  43. package/dist/cjs/type.js.map +1 -1
  44. package/dist/cjs/utils/allowance.js +40 -0
  45. package/dist/cjs/utils/allowance.js.map +1 -0
  46. package/dist/cjs/utils/bigint.js +9 -0
  47. package/dist/cjs/utils/bigint.js.map +1 -0
  48. package/dist/cjs/utils/bignumber.js +9 -0
  49. package/dist/cjs/utils/bignumber.js.map +1 -0
  50. package/dist/cjs/utils/book-id.js +7 -7
  51. package/dist/cjs/utils/book-id.js.map +1 -1
  52. package/dist/cjs/utils/build-transaction.js +8 -7
  53. package/dist/cjs/utils/build-transaction.js.map +1 -1
  54. package/dist/cjs/utils/currency.js +166 -0
  55. package/dist/cjs/utils/currency.js.map +1 -0
  56. package/dist/cjs/utils/decorator.js +3 -1
  57. package/dist/cjs/utils/decorator.js.map +1 -1
  58. package/dist/cjs/utils/open.js +36 -0
  59. package/dist/cjs/utils/open.js.map +1 -0
  60. package/dist/cjs/utils/order.js +102 -0
  61. package/dist/cjs/utils/order.js.map +1 -0
  62. package/dist/cjs/utils/prices.js +25 -3
  63. package/dist/cjs/utils/prices.js.map +1 -1
  64. package/dist/cjs/utils/unit-size.js +49 -0
  65. package/dist/cjs/utils/unit-size.js.map +1 -0
  66. package/dist/cjs/utils.js +15 -0
  67. package/dist/cjs/utils.js.map +1 -0
  68. package/dist/cjs/view.js +102 -33
  69. package/dist/cjs/view.js.map +1 -1
  70. package/dist/esm/abis/core/book-manager-abi.js +109 -0
  71. package/dist/esm/abis/core/book-manager-abi.js.map +1 -0
  72. package/dist/esm/abis/core/book-viewer-abi.js +318 -0
  73. package/dist/esm/abis/core/book-viewer-abi.js.map +1 -0
  74. package/dist/esm/abis/core/controller-abi.js +29 -24
  75. package/dist/esm/abis/core/controller-abi.js.map +1 -1
  76. package/dist/esm/apis/chart-logs.js +150 -0
  77. package/dist/esm/apis/chart-logs.js.map +1 -0
  78. package/dist/esm/apis/market.js +61 -32
  79. package/dist/esm/apis/market.js.map +1 -1
  80. package/dist/esm/apis/open-order.js +57 -21
  81. package/dist/esm/apis/open-order.js.map +1 -1
  82. package/dist/esm/approval.js +116 -44
  83. package/dist/esm/approval.js.map +1 -1
  84. package/dist/esm/call.js +430 -309
  85. package/dist/esm/call.js.map +1 -1
  86. package/dist/esm/constants/addresses.js +26 -1
  87. package/dist/esm/constants/addresses.js.map +1 -1
  88. package/dist/esm/constants/chain.js +20 -2
  89. package/dist/esm/constants/chain.js.map +1 -1
  90. package/dist/esm/constants/client.js +5 -1
  91. package/dist/esm/constants/client.js.map +1 -1
  92. package/dist/esm/constants/currency.js +28 -2
  93. package/dist/esm/constants/currency.js.map +1 -1
  94. package/dist/esm/constants/fee.js +17 -2
  95. package/dist/esm/constants/fee.js.map +1 -1
  96. package/dist/esm/constants/subgraph.js +53 -0
  97. package/dist/esm/constants/subgraph.js.map +1 -0
  98. package/dist/esm/constants/test-chain.js +24 -0
  99. package/dist/esm/constants/test-chain.js.map +1 -0
  100. package/dist/esm/index.js +1 -0
  101. package/dist/esm/index.js.map +1 -1
  102. package/dist/esm/model/book.js +47 -29
  103. package/dist/esm/model/book.js.map +1 -1
  104. package/dist/esm/model/chart-log.js +2 -0
  105. package/dist/esm/model/chart-log.js.map +1 -0
  106. package/dist/esm/model/market.js +46 -187
  107. package/dist/esm/model/market.js.map +1 -1
  108. package/dist/esm/signature.js +35 -37
  109. package/dist/esm/signature.js.map +1 -1
  110. package/dist/esm/type.js +15 -0
  111. package/dist/esm/type.js.map +1 -1
  112. package/dist/esm/utils/allowance.js +36 -0
  113. package/dist/esm/utils/allowance.js.map +1 -0
  114. package/dist/esm/utils/bigint.js +5 -0
  115. package/dist/esm/utils/bigint.js.map +1 -0
  116. package/dist/esm/utils/bignumber.js +6 -0
  117. package/dist/esm/utils/bignumber.js.map +1 -0
  118. package/dist/esm/utils/book-id.js +7 -7
  119. package/dist/esm/utils/book-id.js.map +1 -1
  120. package/dist/esm/utils/build-transaction.js +8 -7
  121. package/dist/esm/utils/build-transaction.js.map +1 -1
  122. package/dist/esm/utils/currency.js +161 -0
  123. package/dist/esm/utils/currency.js.map +1 -0
  124. package/dist/esm/utils/decorator.js +3 -1
  125. package/dist/esm/utils/decorator.js.map +1 -1
  126. package/dist/esm/utils/open.js +32 -0
  127. package/dist/esm/utils/open.js.map +1 -0
  128. package/dist/esm/utils/order.js +98 -0
  129. package/dist/esm/utils/order.js.map +1 -0
  130. package/dist/esm/utils/prices.js +24 -2
  131. package/dist/esm/utils/prices.js.map +1 -1
  132. package/dist/esm/utils/unit-size.js +45 -0
  133. package/dist/esm/utils/unit-size.js.map +1 -0
  134. package/dist/esm/utils.js +4 -0
  135. package/dist/esm/utils.js.map +1 -0
  136. package/dist/esm/view.js +215 -61
  137. package/dist/esm/view.js.map +1 -1
  138. package/dist/tsconfig.build.tsbuildinfo +1 -1
  139. package/dist/types/abis/core/book-manager-abi.d.ts +82 -0
  140. package/dist/types/abis/core/book-manager-abi.d.ts.map +1 -0
  141. package/dist/types/abis/core/book-viewer-abi.d.ts +243 -0
  142. package/dist/types/abis/core/book-viewer-abi.d.ts.map +1 -0
  143. package/dist/types/abis/core/controller-abi.d.ts +23 -19
  144. package/dist/types/abis/core/controller-abi.d.ts.map +1 -1
  145. package/dist/types/apis/chart-logs.d.ts +5 -0
  146. package/dist/types/apis/chart-logs.d.ts.map +1 -0
  147. package/dist/types/apis/market.d.ts +1 -1
  148. package/dist/types/apis/market.d.ts.map +1 -1
  149. package/dist/types/apis/open-order.d.ts +3 -2
  150. package/dist/types/apis/open-order.d.ts.map +1 -1
  151. package/dist/types/approval.d.ts +48 -16
  152. package/dist/types/approval.d.ts.map +1 -1
  153. package/dist/types/call.d.ts +151 -109
  154. package/dist/types/call.d.ts.map +1 -1
  155. package/dist/types/constants/addresses.d.ts.map +1 -1
  156. package/dist/types/constants/chain.d.ts +6 -2
  157. package/dist/types/constants/chain.d.ts.map +1 -1
  158. package/dist/types/constants/client.d.ts +1 -1
  159. package/dist/types/constants/client.d.ts.map +1 -1
  160. package/dist/types/constants/currency.d.ts.map +1 -1
  161. package/dist/types/constants/fee.d.ts +7 -2
  162. package/dist/types/constants/fee.d.ts.map +1 -1
  163. package/dist/types/constants/subgraph.d.ts +10 -0
  164. package/dist/types/constants/subgraph.d.ts.map +1 -0
  165. package/dist/types/constants/test-chain.d.ts +3 -0
  166. package/dist/types/constants/test-chain.d.ts.map +1 -0
  167. package/dist/types/index.d.ts +1 -0
  168. package/dist/types/index.d.ts.map +1 -1
  169. package/dist/types/model/book.d.ts +13 -29
  170. package/dist/types/model/book.d.ts.map +1 -1
  171. package/dist/types/model/chart-log.d.ts +9 -0
  172. package/dist/types/model/chart-log.d.ts.map +1 -0
  173. package/dist/types/model/depth.d.ts +4 -5
  174. package/dist/types/model/depth.d.ts.map +1 -1
  175. package/dist/types/model/market.d.ts +15 -19
  176. package/dist/types/model/market.d.ts.map +1 -1
  177. package/dist/types/model/open-order.d.ts +16 -8
  178. package/dist/types/model/open-order.d.ts.map +1 -1
  179. package/dist/types/signature.d.ts +18 -24
  180. package/dist/types/signature.d.ts.map +1 -1
  181. package/dist/types/type.d.ts +52 -8
  182. package/dist/types/type.d.ts.map +1 -1
  183. package/dist/types/utils/allowance.d.ts +3 -0
  184. package/dist/types/utils/allowance.d.ts.map +1 -0
  185. package/dist/types/utils/bigint.d.ts +2 -0
  186. package/dist/types/utils/bigint.d.ts.map +1 -0
  187. package/dist/types/utils/bignumber.d.ts +3 -0
  188. package/dist/types/utils/bignumber.d.ts.map +1 -0
  189. package/dist/types/utils/book-id.d.ts +2 -1
  190. package/dist/types/utils/book-id.d.ts.map +1 -1
  191. package/dist/types/utils/build-transaction.d.ts +3 -3
  192. package/dist/types/utils/build-transaction.d.ts.map +1 -1
  193. package/dist/types/{apis → utils}/currency.d.ts +3 -0
  194. package/dist/types/utils/currency.d.ts.map +1 -0
  195. package/dist/types/utils/decorator.d.ts +1 -1
  196. package/dist/types/utils/decorator.d.ts.map +1 -1
  197. package/dist/types/utils/open.d.ts +3 -0
  198. package/dist/types/utils/open.d.ts.map +1 -0
  199. package/dist/types/utils/order.d.ts +8 -0
  200. package/dist/types/utils/order.d.ts.map +1 -0
  201. package/dist/types/utils/prices.d.ts +8 -1
  202. package/dist/types/utils/prices.d.ts.map +1 -1
  203. package/dist/types/utils/unit-size.d.ts +4 -0
  204. package/dist/types/utils/unit-size.d.ts.map +1 -0
  205. package/dist/types/utils.d.ts +4 -0
  206. package/dist/types/utils.d.ts.map +1 -0
  207. package/dist/types/view.d.ts +184 -41
  208. package/dist/types/view.d.ts.map +1 -1
  209. package/package.json +1 -1
  210. package/dist/cjs/abis/core/params-abi.js +0 -62
  211. package/dist/cjs/abis/core/params-abi.js.map +0 -1
  212. package/dist/cjs/apis/currency.js +0 -83
  213. package/dist/cjs/apis/currency.js.map +0 -1
  214. package/dist/cjs/apis/subgraph.js +0 -26
  215. package/dist/cjs/apis/subgraph.js.map +0 -1
  216. package/dist/cjs/constants/subgraph-url.js +0 -8
  217. package/dist/cjs/constants/subgraph-url.js.map +0 -1
  218. package/dist/cjs/utils/unit.js +0 -34
  219. package/dist/cjs/utils/unit.js.map +0 -1
  220. package/dist/esm/abis/core/params-abi.js +0 -59
  221. package/dist/esm/abis/core/params-abi.js.map +0 -1
  222. package/dist/esm/apis/currency.js +0 -79
  223. package/dist/esm/apis/currency.js.map +0 -1
  224. package/dist/esm/apis/subgraph.js +0 -22
  225. package/dist/esm/apis/subgraph.js.map +0 -1
  226. package/dist/esm/constants/subgraph-url.js +0 -5
  227. package/dist/esm/constants/subgraph-url.js.map +0 -1
  228. package/dist/esm/utils/unit.js +0 -30
  229. package/dist/esm/utils/unit.js.map +0 -1
  230. package/dist/types/abis/core/params-abi.d.ts +0 -21
  231. package/dist/types/abis/core/params-abi.d.ts.map +0 -1
  232. package/dist/types/apis/currency.d.ts.map +0 -1
  233. package/dist/types/apis/subgraph.d.ts +0 -3
  234. package/dist/types/apis/subgraph.d.ts.map +0 -1
  235. package/dist/types/constants/subgraph-url.d.ts +0 -5
  236. package/dist/types/constants/subgraph-url.d.ts.map +0 -1
  237. package/dist/types/utils/unit.d.ts +0 -4
  238. package/dist/types/utils/unit.d.ts.map +0 -1
package/dist/cjs/call.js CHANGED
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  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");
@@ -14,59 +14,66 @@ const prices_1 = require("./utils/prices");
14
14
  const tick_1 = require("./utils/tick");
15
15
  const view_1 = require("./view");
16
16
  const book_id_1 = require("./utils/book-id");
17
- const params_abi_1 = require("./abis/core/params-abi");
18
- const action_1 = require("./constants/action");
19
17
  const approval_1 = require("./utils/approval");
20
18
  const decorator_1 = require("./utils/decorator");
21
- exports.openMarket = (0, decorator_1.decorator)(async ({ chainId, inputToken, outputToken, }) => {
19
+ const order_1 = require("./utils/order");
20
+ const bigint_1 = require("./utils/bigint");
21
+ exports.openMarket = (0, decorator_1.decorator)(async ({ chainId, userAddress, inputToken, outputToken, options, }) => {
22
22
  const market = await (0, market_1.fetchMarket)(chainId, [inputToken, outputToken]);
23
23
  const isBid = (0, viem_1.isAddressEqual)(market.quote.address, inputToken);
24
- if ((isBid && !market.bidBookOpen) || (!isBid && !market.askBookOpen)) {
25
- const unit = await (0, unit_1.calculateUnit)(chainId, isBid ? market.quote : market.base);
24
+ if ((isBid && !market.bidBook.isOpened) ||
25
+ (!isBid && !market.askBook.isOpened)) {
26
+ const unitSize = await (0, unit_size_1.calculateUnitSize)(chainId, isBid ? market.quote : market.base);
26
27
  return (0, build_transaction_1.buildTransaction)(chainId, {
28
+ chain: chain_1.CHAIN_MAP[chainId],
27
29
  address: addresses_1.CONTRACT_ADDRESSES[chainId].Controller,
30
+ account: userAddress,
28
31
  abi: controller_abi_1.CONTROLLER_ABI,
29
32
  functionName: 'open',
30
33
  args: [
31
34
  [
32
35
  {
33
36
  key: {
34
- base: inputToken,
35
- unit,
36
- quote: outputToken,
37
- makerPolicy: fee_1.MAKER_DEFAULT_POLICY.value,
37
+ base: outputToken,
38
+ unitSize,
39
+ quote: inputToken,
40
+ makerPolicy: fee_1.MAKER_DEFAULT_POLICY[chainId].value,
38
41
  hooks: viem_1.zeroAddress,
39
- takerPolicy: fee_1.TAKER_DEFAULT_POLICY.value,
42
+ takerPolicy: fee_1.TAKER_DEFAULT_POLICY[chainId].value,
40
43
  },
41
44
  hookData: viem_1.zeroHash,
42
45
  },
43
46
  ],
44
47
  (0, time_1.getDeadlineTimestampInSeconds)(),
45
48
  ],
46
- });
49
+ }, options?.gasLimit);
47
50
  }
48
51
  return undefined;
49
52
  });
50
53
  exports.limitOrder = (0, decorator_1.decorator)(async ({ chainId, userAddress, inputToken, outputToken, amount, price, options, }) => {
51
54
  const market = await (0, market_1.fetchMarket)(chainId, [inputToken, outputToken]);
52
55
  const isBid = (0, viem_1.isAddressEqual)(market.quote.address, inputToken);
53
- if ((isBid && !market.bidBookOpen) || (!isBid && !market.askBookOpen)) {
56
+ const [inputCurrency, outputCurrency] = isBid
57
+ ? [market.quote, market.base]
58
+ : [market.base, market.quote];
59
+ if ((isBid && !market.bidBook.isOpened) ||
60
+ (!isBid && !market.askBook.isOpened)) {
54
61
  throw new Error(`
62
+ Open the market before placing a limit order.
55
63
  import { openMarket } from '@clober/v2-sdk'
56
64
 
57
- const transaction = await openMarket(
58
- ${chainId},
59
- '${inputToken}',
60
- '${outputToken}',
61
- )
65
+ const transaction = await openMarket({
66
+ chainId: ${chainId},
67
+ inputToken: '${inputToken}',
68
+ outputToken: '${outputToken}',
69
+ })
62
70
  `);
63
71
  }
64
72
  const rawPrice = (0, prices_1.parsePrice)(Number(price), market.quote.decimals, market.base.decimals);
65
- const tick = isBid ? (0, tick_1.fromPrice)(rawPrice) : (0, tick_1.fromPrice)((0, tick_1.invertPrice)(rawPrice));
66
73
  const tokensToSettle = [inputToken, outputToken].filter((address) => !(0, viem_1.isAddressEqual)(address, viem_1.zeroAddress));
67
- const quoteAmount = (0, viem_1.parseUnits)(amount, isBid ? market.quote.decimals : market.base.decimals);
68
- const [unit, { result }] = await Promise.all([
69
- (0, unit_1.calculateUnit)(chainId, isBid ? market.quote : market.base),
74
+ const quoteAmount = (0, viem_1.parseUnits)(amount, inputCurrency.decimals);
75
+ const [unitSize, { takenAmount, spentAmount, bookId }] = await Promise.all([
76
+ (0, unit_size_1.calculateUnitSize)(chainId, inputCurrency),
70
77
  (0, view_1.getExpectedOutput)({
71
78
  chainId,
72
79
  inputToken,
@@ -74,115 +81,120 @@ exports.limitOrder = (0, decorator_1.decorator)(async ({ chainId, userAddress, i
74
81
  amountIn: amount,
75
82
  options: {
76
83
  ...options,
84
+ limitPrice: price,
77
85
  },
78
86
  }),
79
87
  ]);
80
88
  const isETH = (0, viem_1.isAddressEqual)(inputToken, viem_1.zeroAddress);
81
- const permitParamsList = options?.signature && !isETH
82
- ? [
83
- {
84
- token: inputToken,
85
- permitAmount: quoteAmount,
86
- signature: options.signature,
87
- },
88
- ]
89
- : [];
90
89
  const makeParam = {
91
- id: (0, book_id_1.toBookId)(inputToken, outputToken, unit),
92
- tick: Number(tick),
90
+ id: (0, book_id_1.toBookId)(chainId, inputToken, outputToken, unitSize),
91
+ tick: options?.makeTick
92
+ ? Number(options.makeTick)
93
+ : Number(isBid ? (0, tick_1.fromPrice)(rawPrice) : (0, tick_1.fromPrice)((0, tick_1.invertPrice)(rawPrice))),
93
94
  quoteAmount,
94
95
  hookData: viem_1.zeroHash,
95
96
  };
96
- if (options?.postOnly === true || result.length === 0) {
97
- return (0, build_transaction_1.buildTransaction)(chainId, {
98
- chain: chain_1.CHAIN_MAP[chainId],
99
- account: userAddress,
100
- address: addresses_1.CONTRACT_ADDRESSES[chainId].Controller,
101
- abi: controller_abi_1.CONTROLLER_ABI,
102
- functionName: 'make',
103
- args: [
104
- [makeParam],
105
- tokensToSettle,
106
- permitParamsList,
107
- (0, time_1.getDeadlineTimestampInSeconds)(),
108
- ],
109
- value: isETH ? quoteAmount : 0n,
110
- });
111
- }
112
- else if (result.length === 1) {
113
- return (0, build_transaction_1.buildTransaction)(chainId, {
114
- chain: chain_1.CHAIN_MAP[chainId],
115
- account: userAddress,
116
- address: addresses_1.CONTRACT_ADDRESSES[chainId].Controller,
117
- abi: controller_abi_1.CONTROLLER_ABI,
118
- functionName: 'limit',
119
- args: [
120
- [
121
- {
122
- takeBookId: result[0].bookId,
123
- makeBookId: makeParam.id,
124
- limitPrice: rawPrice,
125
- tick: makeParam.tick,
126
- quoteAmount,
127
- takeHookData: viem_1.zeroHash,
128
- makeHookData: makeParam.hookData,
129
- },
97
+ if (options?.postOnly === true || spentAmount === '0') {
98
+ return {
99
+ transaction: await (0, build_transaction_1.buildTransaction)(chainId, {
100
+ chain: chain_1.CHAIN_MAP[chainId],
101
+ account: userAddress,
102
+ address: addresses_1.CONTRACT_ADDRESSES[chainId].Controller,
103
+ abi: controller_abi_1.CONTROLLER_ABI,
104
+ functionName: 'make',
105
+ args: [
106
+ [makeParam],
107
+ tokensToSettle,
108
+ options?.erc20PermitParam ? [options.erc20PermitParam] : [],
109
+ (0, time_1.getDeadlineTimestampInSeconds)(),
130
110
  ],
131
- tokensToSettle,
132
- permitParamsList,
133
- (0, time_1.getDeadlineTimestampInSeconds)(),
134
- ],
135
- value: isETH ? quoteAmount : 0n,
136
- });
111
+ value: isETH ? quoteAmount : 0n,
112
+ }, options?.gasLimit),
113
+ result: {
114
+ make: {
115
+ amount: (0, viem_1.formatUnits)(quoteAmount, inputCurrency.decimals),
116
+ currency: inputCurrency,
117
+ direction: 'in',
118
+ },
119
+ spent: {
120
+ amount: '0',
121
+ currency: inputCurrency,
122
+ direction: 'in',
123
+ },
124
+ taken: {
125
+ amount: '0',
126
+ currency: outputCurrency,
127
+ direction: 'out',
128
+ },
129
+ },
130
+ };
137
131
  }
138
132
  else {
139
- const makeAmount = quoteAmount -
140
- result.reduce((acc, { spendAmount }) => acc + spendAmount, 0n);
141
- return (0, build_transaction_1.buildTransaction)(chainId, {
142
- chain: chain_1.CHAIN_MAP[chainId],
143
- account: userAddress,
144
- address: addresses_1.CONTRACT_ADDRESSES[chainId].Controller,
145
- abi: controller_abi_1.CONTROLLER_ABI,
146
- functionName: 'execute',
147
- args: [
148
- [
149
- ...result.map(() => action_1.Action.TAKE),
150
- ...(makeAmount > 0n ? [action_1.Action.MAKE] : []),
151
- ],
152
- [
153
- ...result.map(({ bookId, takenAmount }) => (0, viem_1.encodeAbiParameters)(params_abi_1.TAKE_ORDER_PARAMS_ABI, [
133
+ return {
134
+ transaction: await (0, build_transaction_1.buildTransaction)(chainId, {
135
+ chain: chain_1.CHAIN_MAP[chainId],
136
+ account: userAddress,
137
+ address: addresses_1.CONTRACT_ADDRESSES[chainId].Controller,
138
+ abi: controller_abi_1.CONTROLLER_ABI,
139
+ functionName: 'limit',
140
+ args: [
141
+ [
154
142
  {
155
- id: bookId,
156
- limitPrice: rawPrice,
157
- quoteAmount: takenAmount,
158
- hookData: viem_1.zeroHash,
143
+ takeBookId: bookId,
144
+ makeBookId: makeParam.id,
145
+ limitPrice: options?.takeLimitTick
146
+ ? (0, tick_1.toPrice)(options.takeLimitTick)
147
+ : isBid
148
+ ? (0, tick_1.invertPrice)(rawPrice)
149
+ : rawPrice,
150
+ tick: makeParam.tick,
151
+ quoteAmount,
152
+ takeHookData: viem_1.zeroHash,
153
+ makeHookData: makeParam.hookData,
159
154
  },
160
- ])),
161
- ...(makeAmount > 0n
162
- ? [
163
- (0, viem_1.encodeAbiParameters)(params_abi_1.MAKE_ORDER_PARAMS_ABI, [
164
- {
165
- ...makeParam,
166
- quoteAmount: makeAmount,
167
- },
168
- ]),
169
- ]
170
- : []),
155
+ ],
156
+ tokensToSettle,
157
+ options?.erc20PermitParam ? [options.erc20PermitParam] : [],
158
+ (0, time_1.getDeadlineTimestampInSeconds)(),
171
159
  ],
172
- tokensToSettle,
173
- permitParamsList,
174
- [],
175
- (0, time_1.getDeadlineTimestampInSeconds)(),
176
- ],
177
- value: isETH ? quoteAmount : 0n,
178
- });
160
+ value: isETH ? quoteAmount : 0n,
161
+ }, options?.gasLimit),
162
+ result: {
163
+ make: {
164
+ amount: (0, viem_1.formatUnits)(quoteAmount - (0, viem_1.parseUnits)(spentAmount, inputCurrency.decimals), inputCurrency.decimals),
165
+ currency: inputCurrency,
166
+ direction: 'in',
167
+ },
168
+ spent: {
169
+ amount: spentAmount,
170
+ currency: inputCurrency,
171
+ direction: 'in',
172
+ },
173
+ taken: {
174
+ amount: takenAmount,
175
+ currency: outputCurrency,
176
+ direction: 'out',
177
+ },
178
+ },
179
+ };
179
180
  }
180
181
  });
181
- exports.marketOrder = (0, decorator_1.decorator)(async ({ chainId, userAddress, inputToken, outputToken, amount, options, }) => {
182
+ exports.marketOrder = (0, decorator_1.decorator)(async ({ chainId, userAddress, inputToken, outputToken, amountIn, amountOut, options, }) => {
183
+ if (!amountIn && !amountOut) {
184
+ throw new Error('Either amountIn or amountOut must be provided');
185
+ }
186
+ else if (amountIn && amountOut) {
187
+ throw new Error('Only one of amountIn or amountOut can be provided');
188
+ }
182
189
  const market = await (0, market_1.fetchMarket)(chainId, [inputToken, outputToken]);
183
- const isBid = (0, viem_1.isAddressEqual)(market.quote.address, inputToken);
184
- if ((isBid && !market.bidBookOpen) || (!isBid && !market.askBookOpen)) {
190
+ const isTakingBid = (0, viem_1.isAddressEqual)(market.base.address, inputToken);
191
+ const [inputCurrency, outputCurrency] = isTakingBid
192
+ ? [market.base, market.quote]
193
+ : [market.quote, market.base];
194
+ if ((isTakingBid && !market.bidBook.isOpened) ||
195
+ (!isTakingBid && !market.askBook.isOpened)) {
185
196
  throw new Error(`
197
+ Open the market before placing a market order.
186
198
  import { openMarket } from '@clober/v2-sdk'
187
199
 
188
200
  const transaction = await openMarket(
@@ -192,145 +204,240 @@ exports.marketOrder = (0, decorator_1.decorator)(async ({ chainId, userAddress,
192
204
  )
193
205
  `);
194
206
  }
195
- const rawLimitPrice = (0, prices_1.parsePrice)(Number(options?.limitPrice ?? '0'), market.quote.decimals, market.base.decimals);
196
207
  const tokensToSettle = [inputToken, outputToken].filter((address) => !(0, viem_1.isAddressEqual)(address, viem_1.zeroAddress));
197
- const quoteAmount = (0, viem_1.parseUnits)(amount, isBid ? market.quote.decimals : market.base.decimals);
198
- const { result } = await (0, view_1.getExpectedOutput)({
199
- chainId,
200
- inputToken,
201
- outputToken,
202
- amountIn: amount,
203
- options: {
204
- ...options,
205
- },
206
- });
207
208
  const isETH = (0, viem_1.isAddressEqual)(inputToken, viem_1.zeroAddress);
208
- const permitParamsList = options?.signature && !isETH
209
- ? [
210
- {
211
- token: inputToken,
212
- permitAmount: quoteAmount,
213
- signature: options.signature,
209
+ if (amountIn && !amountOut) {
210
+ const { bookId, takenAmount, spentAmount } = await (0, view_1.getExpectedOutput)({
211
+ chainId,
212
+ inputToken,
213
+ outputToken,
214
+ amountIn,
215
+ options: {
216
+ ...options,
214
217
  },
215
- ]
216
- : [];
217
- return (0, build_transaction_1.buildTransaction)(chainId, {
218
- chain: chain_1.CHAIN_MAP[chainId],
219
- account: userAddress,
220
- address: addresses_1.CONTRACT_ADDRESSES[chainId].Controller,
221
- abi: controller_abi_1.CONTROLLER_ABI,
222
- functionName: 'take',
223
- args: [
224
- result.map(({ bookId, takenAmount }) => ({
225
- id: bookId,
226
- limitPrice: isBid ? (0, tick_1.invertPrice)(rawLimitPrice) : rawLimitPrice,
227
- quoteAmount: takenAmount,
228
- hookData: viem_1.zeroHash,
229
- })),
230
- tokensToSettle,
231
- permitParamsList,
232
- (0, time_1.getDeadlineTimestampInSeconds)(),
233
- ],
234
- value: isETH ? quoteAmount : 0n,
235
- });
218
+ });
219
+ const baseAmount = (0, viem_1.parseUnits)(amountIn, inputCurrency.decimals);
220
+ return {
221
+ transaction: await (0, build_transaction_1.buildTransaction)(chainId, {
222
+ chain: chain_1.CHAIN_MAP[chainId],
223
+ account: userAddress,
224
+ address: addresses_1.CONTRACT_ADDRESSES[chainId].Controller,
225
+ abi: controller_abi_1.CONTROLLER_ABI,
226
+ functionName: 'spend',
227
+ args: [
228
+ [
229
+ {
230
+ id: bookId,
231
+ limitPrice: 0n,
232
+ baseAmount,
233
+ minQuoteAmount: options?.slippage
234
+ ? (0, bigint_1.applyPercent)((0, viem_1.parseUnits)(takenAmount, outputCurrency.decimals), 100 - options.slippage)
235
+ : 0n,
236
+ hookData: viem_1.zeroHash,
237
+ },
238
+ ],
239
+ tokensToSettle,
240
+ options?.erc20PermitParam ? [options.erc20PermitParam] : [],
241
+ (0, time_1.getDeadlineTimestampInSeconds)(),
242
+ ],
243
+ value: isETH ? baseAmount : 0n,
244
+ }, options?.gasLimit),
245
+ result: {
246
+ spent: {
247
+ amount: spentAmount,
248
+ currency: inputCurrency,
249
+ direction: 'in',
250
+ },
251
+ taken: {
252
+ amount: takenAmount,
253
+ currency: outputCurrency,
254
+ direction: 'out',
255
+ },
256
+ },
257
+ };
258
+ }
259
+ else if (!amountIn && amountOut) {
260
+ const { bookId, spentAmount, takenAmount } = await (0, view_1.getExpectedInput)({
261
+ chainId,
262
+ inputToken,
263
+ outputToken,
264
+ amountOut,
265
+ options: {
266
+ ...options,
267
+ },
268
+ });
269
+ const quoteAmount = (0, viem_1.parseUnits)(amountOut, outputCurrency.decimals);
270
+ const baseAmount = (0, viem_1.parseUnits)(spentAmount, inputCurrency.decimals);
271
+ const maxBaseAmount = options?.erc20PermitParam?.permitAmount ??
272
+ (options?.slippage
273
+ ? (0, bigint_1.applyPercent)(baseAmount, 100 + options.slippage)
274
+ : isETH
275
+ ? baseAmount
276
+ : 2n ** 256n - 1n);
277
+ return {
278
+ transaction: await (0, build_transaction_1.buildTransaction)(chainId, {
279
+ chain: chain_1.CHAIN_MAP[chainId],
280
+ account: userAddress,
281
+ address: addresses_1.CONTRACT_ADDRESSES[chainId].Controller,
282
+ abi: controller_abi_1.CONTROLLER_ABI,
283
+ functionName: 'take',
284
+ args: [
285
+ [
286
+ {
287
+ id: bookId,
288
+ limitPrice: 0n,
289
+ quoteAmount,
290
+ maxBaseAmount,
291
+ hookData: viem_1.zeroHash,
292
+ },
293
+ ],
294
+ tokensToSettle,
295
+ options?.erc20PermitParam ? [options.erc20PermitParam] : [],
296
+ (0, time_1.getDeadlineTimestampInSeconds)(),
297
+ ],
298
+ value: isETH ? maxBaseAmount : 0n,
299
+ }, options?.gasLimit),
300
+ result: {
301
+ spent: {
302
+ amount: spentAmount,
303
+ currency: inputCurrency,
304
+ direction: 'in',
305
+ },
306
+ taken: {
307
+ amount: takenAmount,
308
+ currency: outputCurrency,
309
+ direction: 'out',
310
+ },
311
+ },
312
+ };
313
+ }
314
+ else {
315
+ throw new Error('Either amountIn or amountOut must be provided');
316
+ }
236
317
  });
237
318
  exports.claimOrder = (0, decorator_1.decorator)(async ({ chainId, userAddress, id, options, }) => {
238
- return (0, exports.claimOrders)({
319
+ const { transaction, result } = await (0, exports.claimOrders)({
239
320
  chainId,
240
321
  userAddress,
241
322
  ids: [id],
242
323
  options: { ...options },
243
324
  });
325
+ return {
326
+ transaction,
327
+ result: result[0],
328
+ };
244
329
  });
245
330
  exports.claimOrders = (0, decorator_1.decorator)(async ({ chainId, userAddress, ids, options, }) => {
246
331
  const isApprovedForAll = await (0, approval_1.fetchIsApprovedForAll)(chainId, userAddress);
247
332
  if (!isApprovedForAll) {
248
333
  throw new Error(`
334
+ Set ApprovalForAll before calling this function.
249
335
  import { setApprovalOfOpenOrdersForAll } from '@clober/v2-sdk'
250
336
 
251
- const hash = await setApprovalOfOpenOrdersForAll(
252
- ${chainId},
253
- privateKeyToAccount('0x...')
254
- )
337
+ const hash = await setApprovalOfOpenOrdersForAll({
338
+ chainId: ${chainId},
339
+ walletClient, // use viem
340
+ })
255
341
  `);
256
342
  }
257
- const openOrders = (await (0, view_1.getOpenOrders)({ chainId, userAddress, options: { ...options } })).filter((order) => ids.includes(order.id));
258
- if (openOrders.length === 0) {
259
- throw new Error(`No claimable open orders found for ${userAddress}`);
260
- }
261
- const tokensToSettle = openOrders
343
+ const orders = (await (0, order_1.fetchOrders)(chainId, ids.map((id) => BigInt(id)))).filter((order) => (0, viem_1.isAddressEqual)(order.user, userAddress) &&
344
+ order.claimable.value !== '0');
345
+ const tokensToSettle = orders
262
346
  .map((order) => [
263
- order.outputCurrency.address,
264
347
  order.inputCurrency.address,
348
+ order.outputCurrency.address,
265
349
  ])
266
350
  .flat()
267
351
  .filter((address, index, self) => self.findIndex((c) => (0, viem_1.isAddressEqual)(c, address)) === index)
268
352
  .filter((address) => !(0, viem_1.isAddressEqual)(address, viem_1.zeroAddress));
269
- return (0, build_transaction_1.buildTransaction)(chainId, {
270
- chain: chain_1.CHAIN_MAP[chainId],
271
- account: userAddress,
272
- address: addresses_1.CONTRACT_ADDRESSES[chainId].Controller,
273
- abi: controller_abi_1.CONTROLLER_ABI,
274
- functionName: 'claim',
275
- args: [
276
- openOrders.map((order) => ({
277
- id: BigInt(order.id),
278
- hookData: viem_1.zeroHash,
279
- })),
280
- tokensToSettle,
281
- [],
282
- (0, time_1.getDeadlineTimestampInSeconds)(),
283
- ],
284
- });
353
+ return {
354
+ transaction: await (0, build_transaction_1.buildTransaction)(chainId, {
355
+ chain: chain_1.CHAIN_MAP[chainId],
356
+ account: userAddress,
357
+ address: addresses_1.CONTRACT_ADDRESSES[chainId].Controller,
358
+ abi: controller_abi_1.CONTROLLER_ABI,
359
+ functionName: 'claim',
360
+ args: [
361
+ orders.map(({ id }) => ({
362
+ id,
363
+ hookData: viem_1.zeroHash,
364
+ })),
365
+ tokensToSettle,
366
+ [],
367
+ (0, time_1.getDeadlineTimestampInSeconds)(),
368
+ ],
369
+ }, options?.gasLimit),
370
+ result: orders.reduce((acc, { claimable: { currency, value } }) => {
371
+ const index = acc.findIndex((c) => (0, viem_1.isAddressEqual)(c.currency.address, currency.address));
372
+ if (index === -1) {
373
+ return [...acc, { currency, amount: value, direction: 'out' }];
374
+ }
375
+ acc[index].amount = (Number(acc[index].amount) + Number(value)).toString();
376
+ return acc;
377
+ }, []),
378
+ };
285
379
  });
286
380
  exports.cancelOrder = (0, decorator_1.decorator)(async ({ chainId, userAddress, id, options, }) => {
287
- return (0, exports.cancelOrders)({
381
+ const { transaction, result } = await (0, exports.cancelOrders)({
288
382
  chainId,
289
383
  userAddress,
290
384
  ids: [id],
291
385
  options: { ...options },
292
386
  });
387
+ return {
388
+ transaction,
389
+ result: result[0],
390
+ };
293
391
  });
294
392
  exports.cancelOrders = (0, decorator_1.decorator)(async ({ chainId, userAddress, ids, options, }) => {
295
393
  const isApprovedForAll = await (0, approval_1.fetchIsApprovedForAll)(chainId, userAddress);
296
394
  if (!isApprovedForAll) {
297
395
  throw new Error(`
396
+ Set ApprovalForAll before calling this function.
298
397
  import { setApprovalOfOpenOrdersForAll } from '@clober/v2-sdk'
299
398
 
300
- const hash = await setApprovalOfOpenOrdersForAll(
301
- ${chainId},
302
- privateKeyToAccount('0x...')
303
- )
399
+ const hash = await setApprovalOfOpenOrdersForAll({
400
+ chainId: ${chainId},
401
+ walletClient, // use viem
402
+ })
304
403
  `);
305
404
  }
306
- const openOrders = (await (0, view_1.getOpenOrders)({ chainId, userAddress, options: { ...options } })).filter((order) => ids.includes(order.id) && order.cancelable);
307
- if (openOrders.length === 0) {
308
- throw new Error(`No cancelable open orders found for ${userAddress}`);
309
- }
310
- const tokensToSettle = openOrders
405
+ const orders = (await (0, order_1.fetchOrders)(chainId, ids.map((id) => BigInt(id)))).filter((order) => (0, viem_1.isAddressEqual)(order.user, userAddress) &&
406
+ order.cancelable.value !== '0');
407
+ const tokensToSettle = orders
311
408
  .map((order) => [
312
- order.outputCurrency.address,
313
409
  order.inputCurrency.address,
410
+ order.outputCurrency.address,
314
411
  ])
315
412
  .flat()
316
413
  .filter((address, index, self) => self.findIndex((c) => (0, viem_1.isAddressEqual)(c, address)) === index)
317
414
  .filter((address) => !(0, viem_1.isAddressEqual)(address, viem_1.zeroAddress));
318
- return (0, build_transaction_1.buildTransaction)(chainId, {
319
- chain: chain_1.CHAIN_MAP[chainId],
320
- account: userAddress,
321
- address: addresses_1.CONTRACT_ADDRESSES[chainId].Controller,
322
- abi: controller_abi_1.CONTROLLER_ABI,
323
- functionName: 'cancel',
324
- args: [
325
- openOrders.map((order) => ({
326
- id: BigInt(order.id),
327
- leftQuoteAmount: 0n,
328
- hookData: viem_1.zeroHash,
329
- })),
330
- tokensToSettle,
331
- [],
332
- (0, time_1.getDeadlineTimestampInSeconds)(),
333
- ],
334
- });
415
+ return {
416
+ transaction: await (0, build_transaction_1.buildTransaction)(chainId, {
417
+ chain: chain_1.CHAIN_MAP[chainId],
418
+ account: userAddress,
419
+ address: addresses_1.CONTRACT_ADDRESSES[chainId].Controller,
420
+ abi: controller_abi_1.CONTROLLER_ABI,
421
+ functionName: 'cancel',
422
+ args: [
423
+ orders.map(({ id }) => ({
424
+ id,
425
+ leftQuoteAmount: 0n,
426
+ hookData: viem_1.zeroHash,
427
+ })),
428
+ tokensToSettle,
429
+ [],
430
+ (0, time_1.getDeadlineTimestampInSeconds)(),
431
+ ],
432
+ }, options?.gasLimit),
433
+ result: orders.reduce((acc, { cancelable: { currency, value } }) => {
434
+ const index = acc.findIndex((c) => (0, viem_1.isAddressEqual)(c.currency.address, currency.address));
435
+ if (index === -1) {
436
+ return [...acc, { currency, amount: value, direction: 'out' }];
437
+ }
438
+ acc[index].amount = (Number(acc[index].amount) + Number(value)).toString();
439
+ return acc;
440
+ }, []),
441
+ };
335
442
  });
336
443
  //# sourceMappingURL=call.js.map