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