@clober/v2-sdk 0.0.1-a → 0.0.1-b

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 (297) hide show
  1. package/.eslintignore +1 -0
  2. package/.eslintrc.yaml +34 -0
  3. package/.github/workflows/ci.yaml +88 -0
  4. package/.github/workflows/deployer.yaml +19 -0
  5. package/.nvmrc +1 -0
  6. package/.prettierignore +6 -0
  7. package/.prettierrc +7 -0
  8. package/package.json +8 -4
  9. package/src/.graphclient/index.ts +1695 -0
  10. package/src/.graphclient/schema.graphql +1116 -0
  11. package/src/.graphclient/sources/clober-v2/introspectionSchema.ts +14481 -0
  12. package/src/.graphclient/sources/clober-v2/schema.graphql +1116 -0
  13. package/src/.graphclient/sources/clober-v2/types.ts +1133 -0
  14. package/src/.graphclientrc.yml +12 -0
  15. package/src/abis/core/controller-abi.ts +985 -0
  16. package/src/abis/core/params-abi.ts +59 -0
  17. package/src/apis/currency.ts +92 -0
  18. package/src/apis/graphql/books.graphql +25 -0
  19. package/src/apis/graphql/open-order.graphql +29 -0
  20. package/src/apis/graphql/open-orders.graphql +29 -0
  21. package/src/apis/market.ts +85 -0
  22. package/src/apis/open-order.ts +145 -0
  23. package/src/approval.ts +82 -0
  24. package/src/call.ts +705 -0
  25. package/src/constants/action.ts +9 -0
  26. package/src/constants/addresses.ts +17 -0
  27. package/src/constants/chain.ts +12 -0
  28. package/src/constants/currency.ts +15 -0
  29. package/src/constants/fee.ts +4 -0
  30. package/src/constants/price.ts +3 -0
  31. package/src/constants/subgraph-url.ts +8 -0
  32. package/src/index.ts +5 -0
  33. package/src/model/book.ts +166 -0
  34. package/src/model/currency.ts +6 -0
  35. package/src/model/depth.ts +11 -0
  36. package/src/model/fee-policy.ts +51 -0
  37. package/src/model/market.ts +320 -0
  38. package/src/model/open-order.ts +16 -0
  39. package/src/signature.ts +196 -0
  40. package/src/type.ts +38 -0
  41. package/src/utils/approval.ts +48 -0
  42. package/{dist/esm/utils/book-id.js → src/utils/book-id.ts} +19 -9
  43. package/src/utils/build-transaction.ts +39 -0
  44. package/src/utils/decimals.ts +22 -0
  45. package/src/utils/market.ts +74 -0
  46. package/src/utils/math.ts +117 -0
  47. package/src/utils/prices.ts +29 -0
  48. package/src/utils/tick.ts +104 -0
  49. package/src/utils/time.ts +6 -0
  50. package/src/utils/unit.ts +43 -0
  51. package/src/view.ts +258 -0
  52. package/test/book-id.test.ts +101 -0
  53. package/test/fee-policy.test.ts +228 -0
  54. package/test/get-expected-input.test.ts +206 -0
  55. package/test/get-expected-output.test.ts +206 -0
  56. package/test/limit-order.test.ts +279 -0
  57. package/test/market-order.test.ts +245 -0
  58. package/test/market.test.ts +68 -0
  59. package/test/math.test.ts +91 -0
  60. package/test/open-order.test.ts +112 -0
  61. package/test/open.test.ts +15 -0
  62. package/test/tick.test.ts +230 -0
  63. package/test/tsconfig.json +12 -0
  64. package/test/utils/chain.ts +12 -0
  65. package/test/utils/constants.ts +25 -0
  66. package/test/utils/currency.ts +44 -0
  67. package/test/utils/depth.ts +148 -0
  68. package/test/utils/test-chain.ts +26 -0
  69. package/test/vitest.config.ts +15 -0
  70. package/tsconfig.base.json +37 -0
  71. package/tsconfig.build.json +9 -0
  72. package/tsconfig.json +9 -0
  73. package/dist/cjs/.graphclient/index.js +0 -243
  74. package/dist/cjs/.graphclient/index.js.map +0 -1
  75. package/dist/cjs/.graphclient/sources/clober-v2/introspectionSchema.js +0 -14482
  76. package/dist/cjs/.graphclient/sources/clober-v2/introspectionSchema.js.map +0 -1
  77. package/dist/cjs/.graphclient/sources/clober-v2/types.js +0 -4
  78. package/dist/cjs/.graphclient/sources/clober-v2/types.js.map +0 -1
  79. package/dist/cjs/abis/core/controller-abi.js +0 -989
  80. package/dist/cjs/abis/core/controller-abi.js.map +0 -1
  81. package/dist/cjs/abis/core/params-abi.js +0 -62
  82. package/dist/cjs/abis/core/params-abi.js.map +0 -1
  83. package/dist/cjs/apis/currency.js +0 -87
  84. package/dist/cjs/apis/currency.js.map +0 -1
  85. package/dist/cjs/apis/market.js +0 -63
  86. package/dist/cjs/apis/market.js.map +0 -1
  87. package/dist/cjs/apis/open-order.js +0 -90
  88. package/dist/cjs/apis/open-order.js.map +0 -1
  89. package/dist/cjs/approval.js +0 -74
  90. package/dist/cjs/approval.js.map +0 -1
  91. package/dist/cjs/call.js +0 -545
  92. package/dist/cjs/call.js.map +0 -1
  93. package/dist/cjs/constants/action.js +0 -14
  94. package/dist/cjs/constants/action.js.map +0 -1
  95. package/dist/cjs/constants/addresses.js +0 -13
  96. package/dist/cjs/constants/addresses.js.map +0 -1
  97. package/dist/cjs/constants/chain.js +0 -13
  98. package/dist/cjs/constants/chain.js.map +0 -1
  99. package/dist/cjs/constants/currency.js +0 -12
  100. package/dist/cjs/constants/currency.js.map +0 -1
  101. package/dist/cjs/constants/fee.js +0 -7
  102. package/dist/cjs/constants/fee.js.map +0 -1
  103. package/dist/cjs/constants/price.js +0 -6
  104. package/dist/cjs/constants/price.js.map +0 -1
  105. package/dist/cjs/constants/subgraph-url.js +0 -8
  106. package/dist/cjs/constants/subgraph-url.js.map +0 -1
  107. package/dist/cjs/index.js +0 -22
  108. package/dist/cjs/index.js.map +0 -1
  109. package/dist/cjs/model/book.js +0 -130
  110. package/dist/cjs/model/book.js.map +0 -1
  111. package/dist/cjs/model/currency.js +0 -3
  112. package/dist/cjs/model/currency.js.map +0 -1
  113. package/dist/cjs/model/depth.js +0 -3
  114. package/dist/cjs/model/depth.js.map +0 -1
  115. package/dist/cjs/model/fee-policy.js +0 -42
  116. package/dist/cjs/model/fee-policy.js.map +0 -1
  117. package/dist/cjs/model/market.js +0 -215
  118. package/dist/cjs/model/market.js.map +0 -1
  119. package/dist/cjs/model/open-order.js +0 -3
  120. package/dist/cjs/model/open-order.js.map +0 -1
  121. package/dist/cjs/package.json +0 -1
  122. package/dist/cjs/signature.js +0 -178
  123. package/dist/cjs/signature.js.map +0 -1
  124. package/dist/cjs/tsconfig.build.tsbuildinfo +0 -1
  125. package/dist/cjs/type.js +0 -6
  126. package/dist/cjs/type.js.map +0 -1
  127. package/dist/cjs/utils/approval.js +0 -46
  128. package/dist/cjs/utils/approval.js.map +0 -1
  129. package/dist/cjs/utils/book-id.js +0 -25
  130. package/dist/cjs/utils/book-id.js.map +0 -1
  131. package/dist/cjs/utils/build-transaction.js +0 -31
  132. package/dist/cjs/utils/build-transaction.js.map +0 -1
  133. package/dist/cjs/utils/decimals.js +0 -17
  134. package/dist/cjs/utils/decimals.js.map +0 -1
  135. package/dist/cjs/utils/market.js +0 -53
  136. package/dist/cjs/utils/market.js.map +0 -1
  137. package/dist/cjs/utils/math.js +0 -83
  138. package/dist/cjs/utils/math.js.map +0 -1
  139. package/dist/cjs/utils/prices.js +0 -22
  140. package/dist/cjs/utils/prices.js.map +0 -1
  141. package/dist/cjs/utils/tick.js +0 -106
  142. package/dist/cjs/utils/tick.js.map +0 -1
  143. package/dist/cjs/utils/time.js +0 -9
  144. package/dist/cjs/utils/time.js.map +0 -1
  145. package/dist/cjs/utils/unit.js +0 -38
  146. package/dist/cjs/utils/unit.js.map +0 -1
  147. package/dist/cjs/view.js +0 -185
  148. package/dist/cjs/view.js.map +0 -1
  149. package/dist/esm/.graphclient/index.js +0 -233
  150. package/dist/esm/.graphclient/index.js.map +0 -1
  151. package/dist/esm/.graphclient/sources/clober-v2/introspectionSchema.js +0 -14480
  152. package/dist/esm/.graphclient/sources/clober-v2/introspectionSchema.js.map +0 -1
  153. package/dist/esm/.graphclient/sources/clober-v2/types.js +0 -3
  154. package/dist/esm/.graphclient/sources/clober-v2/types.js.map +0 -1
  155. package/dist/esm/abis/core/controller-abi.js +0 -986
  156. package/dist/esm/abis/core/controller-abi.js.map +0 -1
  157. package/dist/esm/abis/core/params-abi.js +0 -59
  158. package/dist/esm/abis/core/params-abi.js.map +0 -1
  159. package/dist/esm/apis/currency.js +0 -83
  160. package/dist/esm/apis/currency.js.map +0 -1
  161. package/dist/esm/apis/market.js +0 -59
  162. package/dist/esm/apis/market.js.map +0 -1
  163. package/dist/esm/apis/open-order.js +0 -85
  164. package/dist/esm/apis/open-order.js.map +0 -1
  165. package/dist/esm/approval.js +0 -70
  166. package/dist/esm/approval.js.map +0 -1
  167. package/dist/esm/call.js +0 -535
  168. package/dist/esm/call.js.map +0 -1
  169. package/dist/esm/constants/action.js +0 -11
  170. package/dist/esm/constants/action.js.map +0 -1
  171. package/dist/esm/constants/addresses.js +0 -10
  172. package/dist/esm/constants/addresses.js.map +0 -1
  173. package/dist/esm/constants/chain.js +0 -10
  174. package/dist/esm/constants/chain.js.map +0 -1
  175. package/dist/esm/constants/currency.js +0 -9
  176. package/dist/esm/constants/currency.js.map +0 -1
  177. package/dist/esm/constants/fee.js +0 -4
  178. package/dist/esm/constants/fee.js.map +0 -1
  179. package/dist/esm/constants/price.js +0 -3
  180. package/dist/esm/constants/price.js.map +0 -1
  181. package/dist/esm/constants/subgraph-url.js +0 -5
  182. package/dist/esm/constants/subgraph-url.js.map +0 -1
  183. package/dist/esm/index.js +0 -6
  184. package/dist/esm/index.js.map +0 -1
  185. package/dist/esm/model/book.js +0 -126
  186. package/dist/esm/model/book.js.map +0 -1
  187. package/dist/esm/model/currency.js +0 -2
  188. package/dist/esm/model/currency.js.map +0 -1
  189. package/dist/esm/model/depth.js +0 -2
  190. package/dist/esm/model/depth.js.map +0 -1
  191. package/dist/esm/model/fee-policy.js +0 -38
  192. package/dist/esm/model/fee-policy.js.map +0 -1
  193. package/dist/esm/model/market.js +0 -211
  194. package/dist/esm/model/market.js.map +0 -1
  195. package/dist/esm/model/open-order.js +0 -2
  196. package/dist/esm/model/open-order.js.map +0 -1
  197. package/dist/esm/package.json +0 -1
  198. package/dist/esm/signature.js +0 -174
  199. package/dist/esm/signature.js.map +0 -1
  200. package/dist/esm/tsconfig.build.tsbuildinfo +0 -1
  201. package/dist/esm/type.js +0 -2
  202. package/dist/esm/type.js.map +0 -1
  203. package/dist/esm/utils/approval.js +0 -42
  204. package/dist/esm/utils/approval.js.map +0 -1
  205. package/dist/esm/utils/book-id.js.map +0 -1
  206. package/dist/esm/utils/build-transaction.js +0 -27
  207. package/dist/esm/utils/build-transaction.js.map +0 -1
  208. package/dist/esm/utils/decimals.js +0 -12
  209. package/dist/esm/utils/decimals.js.map +0 -1
  210. package/dist/esm/utils/market.js +0 -49
  211. package/dist/esm/utils/market.js.map +0 -1
  212. package/dist/esm/utils/math.js +0 -78
  213. package/dist/esm/utils/math.js.map +0 -1
  214. package/dist/esm/utils/prices.js +0 -17
  215. package/dist/esm/utils/prices.js.map +0 -1
  216. package/dist/esm/utils/tick.js +0 -100
  217. package/dist/esm/utils/tick.js.map +0 -1
  218. package/dist/esm/utils/time.js +0 -5
  219. package/dist/esm/utils/time.js.map +0 -1
  220. package/dist/esm/utils/unit.js +0 -34
  221. package/dist/esm/utils/unit.js.map +0 -1
  222. package/dist/esm/view.js +0 -177
  223. package/dist/esm/view.js.map +0 -1
  224. package/dist/types/.graphclient/index.d.ts +0 -1281
  225. package/dist/types/.graphclient/index.d.ts.map +0 -1
  226. package/dist/types/.graphclient/sources/clober-v2/introspectionSchema.d.ts +0 -3
  227. package/dist/types/.graphclient/sources/clober-v2/introspectionSchema.d.ts.map +0 -1
  228. package/dist/types/.graphclient/sources/clober-v2/types.d.ts +0 -984
  229. package/dist/types/.graphclient/sources/clober-v2/types.d.ts.map +0 -1
  230. package/dist/types/abis/core/controller-abi.d.ts +0 -757
  231. package/dist/types/abis/core/controller-abi.d.ts.map +0 -1
  232. package/dist/types/abis/core/params-abi.d.ts +0 -21
  233. package/dist/types/abis/core/params-abi.d.ts.map +0 -1
  234. package/dist/types/apis/currency.d.ts +0 -4
  235. package/dist/types/apis/currency.d.ts.map +0 -1
  236. package/dist/types/apis/market.d.ts +0 -4
  237. package/dist/types/apis/market.d.ts.map +0 -1
  238. package/dist/types/apis/open-order.d.ts +0 -5
  239. package/dist/types/apis/open-order.d.ts.map +0 -1
  240. package/dist/types/approval.d.ts +0 -32
  241. package/dist/types/approval.d.ts.map +0 -1
  242. package/dist/types/call.d.ts +0 -234
  243. package/dist/types/call.d.ts.map +0 -1
  244. package/dist/types/constants/action.d.ts +0 -10
  245. package/dist/types/constants/action.d.ts.map +0 -1
  246. package/dist/types/constants/addresses.d.ts +0 -9
  247. package/dist/types/constants/addresses.d.ts.map +0 -1
  248. package/dist/types/constants/chain.d.ts +0 -9
  249. package/dist/types/constants/chain.d.ts.map +0 -1
  250. package/dist/types/constants/currency.d.ts +0 -8
  251. package/dist/types/constants/currency.d.ts.map +0 -1
  252. package/dist/types/constants/fee.d.ts +0 -4
  253. package/dist/types/constants/fee.d.ts.map +0 -1
  254. package/dist/types/constants/price.d.ts +0 -3
  255. package/dist/types/constants/price.d.ts.map +0 -1
  256. package/dist/types/constants/subgraph-url.d.ts +0 -5
  257. package/dist/types/constants/subgraph-url.d.ts.map +0 -1
  258. package/dist/types/index.d.ts +0 -6
  259. package/dist/types/index.d.ts.map +0 -1
  260. package/dist/types/model/book.d.ts +0 -31
  261. package/dist/types/model/book.d.ts.map +0 -1
  262. package/dist/types/model/currency.d.ts +0 -7
  263. package/dist/types/model/currency.d.ts.map +0 -1
  264. package/dist/types/model/depth.d.ts +0 -11
  265. package/dist/types/model/depth.d.ts.map +0 -1
  266. package/dist/types/model/fee-policy.d.ts +0 -15
  267. package/dist/types/model/fee-policy.d.ts.map +0 -1
  268. package/dist/types/model/market.d.ts +0 -44
  269. package/dist/types/model/market.d.ts.map +0 -1
  270. package/dist/types/model/open-order.d.ts +0 -28
  271. package/dist/types/model/open-order.d.ts.map +0 -1
  272. package/dist/types/signature.d.ts +0 -40
  273. package/dist/types/signature.d.ts.map +0 -1
  274. package/dist/types/type.d.ts +0 -34
  275. package/dist/types/type.d.ts.map +0 -1
  276. package/dist/types/utils/approval.d.ts +0 -3
  277. package/dist/types/utils/approval.d.ts.map +0 -1
  278. package/dist/types/utils/book-id.d.ts +0 -2
  279. package/dist/types/utils/book-id.d.ts.map +0 -1
  280. package/dist/types/utils/build-transaction.d.ts +0 -5
  281. package/dist/types/utils/build-transaction.d.ts.map +0 -1
  282. package/dist/types/utils/decimals.d.ts +0 -3
  283. package/dist/types/utils/decimals.d.ts.map +0 -1
  284. package/dist/types/utils/market.d.ts +0 -7
  285. package/dist/types/utils/market.d.ts.map +0 -1
  286. package/dist/types/utils/math.d.ts +0 -3
  287. package/dist/types/utils/math.d.ts.map +0 -1
  288. package/dist/types/utils/prices.d.ts +0 -3
  289. package/dist/types/utils/prices.d.ts.map +0 -1
  290. package/dist/types/utils/tick.d.ts +0 -4
  291. package/dist/types/utils/tick.d.ts.map +0 -1
  292. package/dist/types/utils/time.d.ts +0 -2
  293. package/dist/types/utils/time.d.ts.map +0 -1
  294. package/dist/types/utils/unit.d.ts +0 -4
  295. package/dist/types/utils/unit.d.ts.map +0 -1
  296. package/dist/types/view.d.ts +0 -129
  297. package/dist/types/view.d.ts.map +0 -1
@@ -0,0 +1,59 @@
1
+ export const TAKE_ORDER_PARAMS_ABI = [
2
+ {
3
+ components: [
4
+ {
5
+ internalType: 'BookId',
6
+ name: 'id',
7
+ type: 'uint192',
8
+ },
9
+ {
10
+ internalType: 'uint256',
11
+ name: 'limitPrice',
12
+ type: 'uint256',
13
+ },
14
+ {
15
+ internalType: 'uint256',
16
+ name: 'quoteAmount',
17
+ type: 'uint256',
18
+ },
19
+ {
20
+ internalType: 'bytes',
21
+ name: 'hookData',
22
+ type: 'bytes',
23
+ },
24
+ ],
25
+ internalType: 'struct IController.TakeOrderParams',
26
+ name: 'params',
27
+ type: 'tuple',
28
+ },
29
+ ]
30
+
31
+ export const MAKE_ORDER_PARAMS_ABI = [
32
+ {
33
+ components: [
34
+ {
35
+ internalType: 'BookId',
36
+ name: 'id',
37
+ type: 'uint192',
38
+ },
39
+ {
40
+ internalType: 'Tick',
41
+ name: 'tick',
42
+ type: 'int24',
43
+ },
44
+ {
45
+ internalType: 'uint256',
46
+ name: 'quoteAmount',
47
+ type: 'uint256',
48
+ },
49
+ {
50
+ internalType: 'bytes',
51
+ name: 'hookData',
52
+ type: 'bytes',
53
+ },
54
+ ],
55
+ internalType: 'struct IController.MakeOrderParams',
56
+ name: 'params',
57
+ type: 'tuple',
58
+ },
59
+ ]
@@ -0,0 +1,92 @@
1
+ import { createPublicClient, http, isAddressEqual, zeroAddress } from 'viem'
2
+
3
+ import type { Currency } from '../model/currency'
4
+ import { CHAIN_IDS, CHAIN_MAP } from '../constants/chain'
5
+
6
+ const _abi = [
7
+ {
8
+ inputs: [],
9
+ name: 'name',
10
+ outputs: [
11
+ {
12
+ internalType: 'string',
13
+ name: '',
14
+ type: 'string',
15
+ },
16
+ ],
17
+ stateMutability: 'view',
18
+ type: 'function',
19
+ },
20
+ {
21
+ inputs: [],
22
+ name: 'symbol',
23
+ outputs: [
24
+ {
25
+ internalType: 'string',
26
+ name: '',
27
+ type: 'string',
28
+ },
29
+ ],
30
+ stateMutability: 'view',
31
+ type: 'function',
32
+ },
33
+ {
34
+ inputs: [],
35
+ name: 'decimals',
36
+ outputs: [
37
+ {
38
+ internalType: 'uint8',
39
+ name: '',
40
+ type: 'uint8',
41
+ },
42
+ ],
43
+ stateMutability: 'view',
44
+ type: 'function',
45
+ },
46
+ ] as const
47
+
48
+ export const fetchCurrency = async (
49
+ chainId: CHAIN_IDS,
50
+ address: `0x${string}`,
51
+ rpcUrl?: string,
52
+ ): Promise<Currency> => {
53
+ if (isAddressEqual(address, zeroAddress)) {
54
+ return {
55
+ address: zeroAddress,
56
+ name: 'Ethereum',
57
+ symbol: 'ETH',
58
+ decimals: 18,
59
+ }
60
+ }
61
+
62
+ const publicClient = createPublicClient({
63
+ chain: CHAIN_MAP[chainId],
64
+ transport: rpcUrl ? http(rpcUrl) : http(),
65
+ })
66
+ const [{ result: name }, { result: symbol }, { result: decimals }] =
67
+ await publicClient.multicall({
68
+ contracts: [
69
+ {
70
+ address,
71
+ abi: _abi,
72
+ functionName: 'name',
73
+ },
74
+ {
75
+ address,
76
+ abi: _abi,
77
+ functionName: 'symbol',
78
+ },
79
+ {
80
+ address,
81
+ abi: _abi,
82
+ functionName: 'decimals',
83
+ },
84
+ ],
85
+ })
86
+ return {
87
+ address,
88
+ name: name ?? 'Unknown',
89
+ symbol: symbol ?? 'Unknown',
90
+ decimals: decimals ?? 18,
91
+ }
92
+ }
@@ -0,0 +1,25 @@
1
+ query getBooks($baseTokenAddress: String!, $quoteTokenAddress: String!) {
2
+ books(
3
+ where: {base: $baseTokenAddress, quote: $quoteTokenAddress, makerPolicy: "8888308", takerPolicy: "8889608", hooks: "0x0000000000000000000000000000000000000000"}
4
+ ) {
5
+ id
6
+ base {
7
+ id
8
+ name
9
+ symbol
10
+ decimals
11
+ }
12
+ quote {
13
+ id
14
+ name
15
+ symbol
16
+ decimals
17
+ }
18
+ unit
19
+ depths {
20
+ tick
21
+ price
22
+ rawAmount
23
+ }
24
+ }
25
+ }
@@ -0,0 +1,29 @@
1
+ query getOpenOrder($orderId: ID!) {
2
+ openOrder(id: $orderId) {
3
+ id
4
+ book {
5
+ id
6
+ base {
7
+ id
8
+ name
9
+ symbol
10
+ decimals
11
+ }
12
+ quote {
13
+ id
14
+ name
15
+ symbol
16
+ decimals
17
+ }
18
+ unit
19
+ }
20
+ tick
21
+ txHash
22
+ createdAt
23
+
24
+ rawAmount
25
+ rawFilledAmount
26
+ rawClaimedAmount
27
+ rawClaimableAmount
28
+ }
29
+ }
@@ -0,0 +1,29 @@
1
+ query getOpenOrders($userAddress: String!) {
2
+ openOrders(where: { user: $userAddress }) {
3
+ id
4
+ book {
5
+ id
6
+ base {
7
+ id
8
+ name
9
+ symbol
10
+ decimals
11
+ }
12
+ quote {
13
+ id
14
+ name
15
+ symbol
16
+ decimals
17
+ }
18
+ unit
19
+ }
20
+ tick
21
+ txHash
22
+ createdAt
23
+
24
+ rawAmount
25
+ rawFilledAmount
26
+ rawClaimedAmount
27
+ rawClaimableAmount
28
+ }
29
+ }
@@ -0,0 +1,85 @@
1
+ import { getAddress, isAddressEqual } from 'viem'
2
+
3
+ import { CHAIN_IDS } from '../constants/chain'
4
+ import { Market } from '../model/market'
5
+ import { SUBGRAPH_URL } from '../constants/subgraph-url'
6
+ import { getBuiltGraphSDK } from '../.graphclient'
7
+ import { Book } from '../model/book'
8
+ import type { RawDepth } from '../model/depth'
9
+ import { getMarketId } from '../utils/market'
10
+
11
+ import { fetchCurrency } from './currency'
12
+
13
+ const { getBooks } = getBuiltGraphSDK()
14
+
15
+ export async function fetchMarket(
16
+ chainId: CHAIN_IDS,
17
+ tokenAddresses: `0x${string}`[],
18
+ rpcUrl?: string,
19
+ ): Promise<Market> {
20
+ if (tokenAddresses.length !== 2) {
21
+ throw new Error('Invalid token pair')
22
+ }
23
+
24
+ const { quoteTokenAddress, baseTokenAddress } = getMarketId(chainId, [
25
+ tokenAddresses[0]!,
26
+ tokenAddresses[1]!,
27
+ ])
28
+
29
+ const [
30
+ quoteCurrency,
31
+ baseCurrency,
32
+ { books: bidBooks },
33
+ { books: askBooks },
34
+ ] = await Promise.all([
35
+ fetchCurrency(chainId, quoteTokenAddress, rpcUrl),
36
+ fetchCurrency(chainId, baseTokenAddress, rpcUrl),
37
+ getBooks(
38
+ {
39
+ quoteTokenAddress: quoteTokenAddress.toLowerCase(),
40
+ baseTokenAddress: baseTokenAddress.toLowerCase(),
41
+ },
42
+ {
43
+ url: SUBGRAPH_URL[chainId],
44
+ },
45
+ ),
46
+ getBooks(
47
+ {
48
+ quoteTokenAddress: baseTokenAddress.toLowerCase(),
49
+ baseTokenAddress: quoteTokenAddress.toLowerCase(),
50
+ },
51
+ {
52
+ url: SUBGRAPH_URL[chainId],
53
+ },
54
+ ),
55
+ ])
56
+
57
+ return new Market({
58
+ chainId,
59
+ tokens: [quoteCurrency, baseCurrency],
60
+ books: [...bidBooks, ...askBooks].map((book) => {
61
+ const isBid = isAddressEqual(
62
+ getAddress(book.quote.id),
63
+ quoteCurrency.address,
64
+ )
65
+ return new Book({
66
+ id: BigInt(book.id),
67
+ base: isBid ? baseCurrency : quoteCurrency,
68
+ quote: isBid ? quoteCurrency : baseCurrency,
69
+ unit: BigInt(book.unit),
70
+ depths: book.depths
71
+ .map((depth) => {
72
+ const rawAmount = BigInt(depth.rawAmount)
73
+ const tick = BigInt(depth.tick)
74
+ return {
75
+ bookId: String(book.id),
76
+ unit: BigInt(book.unit),
77
+ tick,
78
+ rawAmount,
79
+ } as RawDepth
80
+ })
81
+ .sort((a, b) => Number(b.tick) - Number(a.tick)),
82
+ })
83
+ }),
84
+ })
85
+ }
@@ -0,0 +1,145 @@
1
+ import { formatUnits, getAddress, isAddressEqual } from 'viem'
2
+
3
+ import {
4
+ Book,
5
+ OpenOrder as SubGraphOpenOrder,
6
+ getBuiltGraphSDK,
7
+ } from '../.graphclient'
8
+ import { CHAIN_IDS } from '../constants/chain'
9
+ import { SUBGRAPH_URL } from '../constants/subgraph-url'
10
+ import { getMarketId } from '../utils/market'
11
+ import { Currency } from '../model/currency'
12
+ import { quoteToBase } from '../utils/decimals'
13
+ import { formatPrice } from '../utils/prices'
14
+ import { invertPrice, toPrice } from '../utils/tick'
15
+ import { OpenOrder } from '../model/open-order'
16
+
17
+ import { fetchCurrency } from './currency'
18
+
19
+ const { getOpenOrders, getOpenOrder } = getBuiltGraphSDK()
20
+
21
+ export async function fetchOpenOrders(
22
+ chainId: CHAIN_IDS,
23
+ userAddress: `0x${string}`,
24
+ rpcUrl?: string,
25
+ ): Promise<OpenOrder[]> {
26
+ const { openOrders } = await getOpenOrders(
27
+ {
28
+ userAddress: userAddress.toLowerCase(),
29
+ },
30
+ {
31
+ url: SUBGRAPH_URL[chainId],
32
+ },
33
+ )
34
+ const currencies = await Promise.all(
35
+ openOrders
36
+ .map((openOrder) => [
37
+ getAddress(openOrder.book.base.id),
38
+ getAddress(openOrder.book.quote.id),
39
+ ])
40
+ .flat()
41
+ .filter(
42
+ (address, index, self) =>
43
+ self.findIndex((c) => isAddressEqual(c, address)) === index,
44
+ )
45
+ .map((address) => fetchCurrency(chainId, address, rpcUrl)),
46
+ )
47
+ return openOrders.map((openOrder) =>
48
+ toOpenOrder(chainId, currencies, openOrder),
49
+ )
50
+ }
51
+
52
+ export async function fetchOpenOrder(
53
+ chainId: CHAIN_IDS,
54
+ id: string,
55
+ rpcUrl?: string,
56
+ ): Promise<OpenOrder> {
57
+ const { openOrder } = await getOpenOrder(
58
+ {
59
+ orderId: id,
60
+ },
61
+ {
62
+ url: SUBGRAPH_URL[chainId],
63
+ },
64
+ )
65
+ if (!openOrder) {
66
+ throw new Error(`Open order not found: ${id}`)
67
+ }
68
+ const currencies = await Promise.all([
69
+ fetchCurrency(chainId, getAddress(openOrder.book.base.id), rpcUrl),
70
+ fetchCurrency(chainId, getAddress(openOrder.book.quote.id), rpcUrl),
71
+ ])
72
+ return toOpenOrder(chainId, currencies, openOrder)
73
+ }
74
+
75
+ const toOpenOrder = (
76
+ chainId: CHAIN_IDS,
77
+ currencies: Currency[],
78
+ openOrder: Pick<
79
+ SubGraphOpenOrder,
80
+ | 'id'
81
+ | 'tick'
82
+ | 'txHash'
83
+ | 'createdAt'
84
+ | 'rawAmount'
85
+ | 'rawFilledAmount'
86
+ | 'rawClaimedAmount'
87
+ | 'rawClaimableAmount'
88
+ > & {
89
+ book: Pick<Book, 'id' | 'unit'> & {
90
+ quote: { id: string }
91
+ base: { id: string }
92
+ }
93
+ },
94
+ ): OpenOrder => {
95
+ const inputCurrency = currencies.find((c: Currency) =>
96
+ isAddressEqual(c.address, getAddress(openOrder.book.quote.id)),
97
+ )!
98
+ const outputCurrency = currencies.find((c: Currency) =>
99
+ isAddressEqual(c.address, getAddress(openOrder.book.base.id)),
100
+ )!
101
+ const { quoteTokenAddress } = getMarketId(chainId, [
102
+ inputCurrency.address,
103
+ outputCurrency.address,
104
+ ])
105
+ const isBid = isAddressEqual(quoteTokenAddress, inputCurrency.address)
106
+ const quote = isBid ? inputCurrency : outputCurrency
107
+ const base = isBid ? outputCurrency : inputCurrency
108
+ const tick = BigInt(openOrder.tick)
109
+ const rawAmount = BigInt(openOrder.rawAmount)
110
+ const rawFilledAmount = BigInt(openOrder.rawFilledAmount)
111
+ const unit = BigInt(openOrder.book.unit)
112
+ const quoteAmount = unit * rawAmount
113
+ const rawClaimedAmount = BigInt(openOrder.rawClaimedAmount)
114
+ const rawClaimableAmount = BigInt(openOrder.rawClaimableAmount)
115
+ const amount = isBid ? quoteToBase(tick, quoteAmount, false) : quoteAmount
116
+ const filled = isBid
117
+ ? quoteToBase(tick, unit * rawFilledAmount, false)
118
+ : unit * rawFilledAmount
119
+ const claimed = quoteToBase(tick, unit * rawClaimedAmount, false)
120
+ const claimable = quoteToBase(tick, unit * rawClaimableAmount, false)
121
+ return {
122
+ id: openOrder.id,
123
+ isBid,
124
+ inputCurrency,
125
+ outputCurrency,
126
+ txHash: openOrder.txHash as `0x${string}`,
127
+ createdAt: Number(openOrder.createdAt),
128
+ price: formatPrice(
129
+ isBid ? toPrice(tick) : invertPrice(toPrice(tick)),
130
+ quote.decimals,
131
+ base.decimals,
132
+ ),
133
+ amount: { currency: base, value: formatUnits(amount, base.decimals) },
134
+ filled: { currency: base, value: formatUnits(filled, base.decimals) },
135
+ claimed: {
136
+ currency: outputCurrency,
137
+ value: formatUnits(claimed, outputCurrency.decimals),
138
+ },
139
+ claimable: {
140
+ currency: outputCurrency,
141
+ value: formatUnits(claimable, outputCurrency.decimals),
142
+ },
143
+ cancelable: rawAmount > rawFilledAmount,
144
+ }
145
+ }
@@ -0,0 +1,82 @@
1
+ import { createWalletClient, HDAccount, http, PrivateKeyAccount } from 'viem'
2
+
3
+ import { CHAIN_IDS, CHAIN_MAP } from './constants/chain'
4
+ import { CONTRACT_ADDRESSES } from './constants/addresses'
5
+ import { fetchIsApprovedForAll } from './utils/approval'
6
+
7
+ const _abi = [
8
+ {
9
+ inputs: [
10
+ {
11
+ internalType: 'address',
12
+ name: 'operator',
13
+ type: 'address',
14
+ },
15
+ {
16
+ internalType: 'bool',
17
+ name: 'approved',
18
+ type: 'bool',
19
+ },
20
+ ],
21
+ name: 'setApprovalForAll',
22
+ outputs: [],
23
+ stateMutability: 'nonpayable',
24
+ type: 'function',
25
+ },
26
+ ] as const
27
+
28
+ /**
29
+ * Sets approval of all open orders for the specified account on the given chain.
30
+ *
31
+ * @param {CHAIN_IDS} chainId The chain ID.
32
+ * @param {HDAccount | PrivateKeyAccount} account The Ethereum account for which approval is to be set.
33
+ * @param {Object} [options] Optional parameters for setting approval.
34
+ * @param {string} options.rpcUrl The RPC URL to use for executing the transaction.
35
+ * @returns {Promise<`0x${string}` | undefined>} Promise resolving to the transaction hash. If the account is already approved for all, the promise resolves to `undefined`.
36
+ * @example
37
+ * import { setApprovalOfOpenOrdersForAll } from '@clober/v2-sdk'
38
+ * import { privateKeyToAccount } from 'viem/accounts'
39
+ *
40
+ * const hash = await setApprovalOfOpenOrdersForAll(
41
+ * 421614,
42
+ * privateKeyToAccount('0x...')
43
+ * )
44
+ *
45
+ * @example
46
+ * import { setApprovalOfOpenOrdersForAll } from '@clober/v2-sdk'
47
+ * import { mnemonicToAccount } from 'viem/accounts'
48
+ *
49
+ * const hash = await setApprovalOfOpenOrdersForAll(
50
+ * 421614,
51
+ * mnemonicToAccount('legal ...')
52
+ * )
53
+ */
54
+ export const setApprovalOfOpenOrdersForAll = async (
55
+ chainId: CHAIN_IDS,
56
+ account: HDAccount | PrivateKeyAccount,
57
+ options?: {
58
+ rpcUrl: string
59
+ },
60
+ ): Promise<`0x${string}` | undefined> => {
61
+ const isApprovedForAll = await fetchIsApprovedForAll(
62
+ chainId,
63
+ account.address,
64
+ options?.rpcUrl,
65
+ )
66
+ if (isApprovedForAll) {
67
+ return undefined
68
+ }
69
+ const walletClient = createWalletClient({
70
+ chain: CHAIN_MAP[chainId],
71
+ account,
72
+ transport: options?.rpcUrl ? http(options.rpcUrl) : http(),
73
+ })
74
+ return walletClient.writeContract({
75
+ account,
76
+ chain: CHAIN_MAP[chainId],
77
+ address: CONTRACT_ADDRESSES[chainId]!.BookManager,
78
+ abi: _abi,
79
+ functionName: 'setApprovalForAll',
80
+ args: [CONTRACT_ADDRESSES[chainId]!.Controller, true],
81
+ })
82
+ }