@clober/v2-sdk 0.0.1-b → 0.0.1-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 (297) hide show
  1. package/dist/cjs/.graphclient/index.js +243 -0
  2. package/dist/cjs/.graphclient/index.js.map +1 -0
  3. package/dist/cjs/.graphclient/sources/clober-v2/introspectionSchema.js +14482 -0
  4. package/dist/cjs/.graphclient/sources/clober-v2/introspectionSchema.js.map +1 -0
  5. package/dist/cjs/.graphclient/sources/clober-v2/types.js +4 -0
  6. package/dist/cjs/.graphclient/sources/clober-v2/types.js.map +1 -0
  7. package/dist/cjs/abis/core/controller-abi.js +989 -0
  8. package/dist/cjs/abis/core/controller-abi.js.map +1 -0
  9. package/dist/cjs/abis/core/params-abi.js +62 -0
  10. package/dist/cjs/abis/core/params-abi.js.map +1 -0
  11. package/dist/cjs/apis/currency.js +87 -0
  12. package/dist/cjs/apis/currency.js.map +1 -0
  13. package/dist/cjs/apis/market.js +63 -0
  14. package/dist/cjs/apis/market.js.map +1 -0
  15. package/dist/cjs/apis/open-order.js +90 -0
  16. package/dist/cjs/apis/open-order.js.map +1 -0
  17. package/dist/cjs/approval.js +74 -0
  18. package/dist/cjs/approval.js.map +1 -0
  19. package/dist/cjs/call.js +545 -0
  20. package/dist/cjs/call.js.map +1 -0
  21. package/dist/cjs/constants/action.js +14 -0
  22. package/dist/cjs/constants/action.js.map +1 -0
  23. package/dist/cjs/constants/addresses.js +13 -0
  24. package/dist/cjs/constants/addresses.js.map +1 -0
  25. package/dist/cjs/constants/chain.js +13 -0
  26. package/dist/cjs/constants/chain.js.map +1 -0
  27. package/dist/cjs/constants/currency.js +12 -0
  28. package/dist/cjs/constants/currency.js.map +1 -0
  29. package/dist/cjs/constants/fee.js +7 -0
  30. package/dist/cjs/constants/fee.js.map +1 -0
  31. package/dist/cjs/constants/price.js +6 -0
  32. package/dist/cjs/constants/price.js.map +1 -0
  33. package/dist/cjs/constants/subgraph-url.js +8 -0
  34. package/dist/cjs/constants/subgraph-url.js.map +1 -0
  35. package/dist/cjs/index.js +22 -0
  36. package/dist/cjs/index.js.map +1 -0
  37. package/dist/cjs/model/book.js +130 -0
  38. package/dist/cjs/model/book.js.map +1 -0
  39. package/dist/cjs/model/currency.js +3 -0
  40. package/dist/cjs/model/currency.js.map +1 -0
  41. package/dist/cjs/model/depth.js +3 -0
  42. package/dist/cjs/model/depth.js.map +1 -0
  43. package/dist/cjs/model/fee-policy.js +42 -0
  44. package/dist/cjs/model/fee-policy.js.map +1 -0
  45. package/dist/cjs/model/market.js +215 -0
  46. package/dist/cjs/model/market.js.map +1 -0
  47. package/dist/cjs/model/open-order.js +3 -0
  48. package/dist/cjs/model/open-order.js.map +1 -0
  49. package/dist/cjs/package.json +1 -0
  50. package/dist/cjs/signature.js +178 -0
  51. package/dist/cjs/signature.js.map +1 -0
  52. package/dist/cjs/tsconfig.build.tsbuildinfo +1 -0
  53. package/dist/cjs/type.js +6 -0
  54. package/dist/cjs/type.js.map +1 -0
  55. package/dist/cjs/utils/approval.js +46 -0
  56. package/dist/cjs/utils/approval.js.map +1 -0
  57. package/dist/cjs/utils/book-id.js +25 -0
  58. package/dist/cjs/utils/book-id.js.map +1 -0
  59. package/dist/cjs/utils/build-transaction.js +31 -0
  60. package/dist/cjs/utils/build-transaction.js.map +1 -0
  61. package/dist/cjs/utils/decimals.js +17 -0
  62. package/dist/cjs/utils/decimals.js.map +1 -0
  63. package/dist/cjs/utils/market.js +53 -0
  64. package/dist/cjs/utils/market.js.map +1 -0
  65. package/dist/cjs/utils/math.js +83 -0
  66. package/dist/cjs/utils/math.js.map +1 -0
  67. package/dist/cjs/utils/prices.js +22 -0
  68. package/dist/cjs/utils/prices.js.map +1 -0
  69. package/dist/cjs/utils/tick.js +106 -0
  70. package/dist/cjs/utils/tick.js.map +1 -0
  71. package/dist/cjs/utils/time.js +9 -0
  72. package/dist/cjs/utils/time.js.map +1 -0
  73. package/dist/cjs/utils/unit.js +38 -0
  74. package/dist/cjs/utils/unit.js.map +1 -0
  75. package/dist/cjs/view.js +185 -0
  76. package/dist/cjs/view.js.map +1 -0
  77. package/dist/esm/.graphclient/index.js +233 -0
  78. package/dist/esm/.graphclient/index.js.map +1 -0
  79. package/dist/esm/.graphclient/sources/clober-v2/introspectionSchema.js +14480 -0
  80. package/dist/esm/.graphclient/sources/clober-v2/introspectionSchema.js.map +1 -0
  81. package/dist/esm/.graphclient/sources/clober-v2/types.js +3 -0
  82. package/dist/esm/.graphclient/sources/clober-v2/types.js.map +1 -0
  83. package/dist/esm/abis/core/controller-abi.js +986 -0
  84. package/dist/esm/abis/core/controller-abi.js.map +1 -0
  85. package/dist/esm/abis/core/params-abi.js +59 -0
  86. package/dist/esm/abis/core/params-abi.js.map +1 -0
  87. package/dist/esm/apis/currency.js +83 -0
  88. package/dist/esm/apis/currency.js.map +1 -0
  89. package/dist/esm/apis/market.js +59 -0
  90. package/dist/esm/apis/market.js.map +1 -0
  91. package/dist/esm/apis/open-order.js +85 -0
  92. package/dist/esm/apis/open-order.js.map +1 -0
  93. package/dist/esm/approval.js +70 -0
  94. package/dist/esm/approval.js.map +1 -0
  95. package/dist/esm/call.js +535 -0
  96. package/dist/esm/call.js.map +1 -0
  97. package/dist/esm/constants/action.js +11 -0
  98. package/dist/esm/constants/action.js.map +1 -0
  99. package/dist/esm/constants/addresses.js +10 -0
  100. package/dist/esm/constants/addresses.js.map +1 -0
  101. package/dist/esm/constants/chain.js +10 -0
  102. package/dist/esm/constants/chain.js.map +1 -0
  103. package/dist/esm/constants/currency.js +9 -0
  104. package/dist/esm/constants/currency.js.map +1 -0
  105. package/dist/esm/constants/fee.js +4 -0
  106. package/dist/esm/constants/fee.js.map +1 -0
  107. package/dist/esm/constants/price.js +3 -0
  108. package/dist/esm/constants/price.js.map +1 -0
  109. package/dist/esm/constants/subgraph-url.js +5 -0
  110. package/dist/esm/constants/subgraph-url.js.map +1 -0
  111. package/dist/esm/index.js +6 -0
  112. package/dist/esm/index.js.map +1 -0
  113. package/dist/esm/model/book.js +126 -0
  114. package/dist/esm/model/book.js.map +1 -0
  115. package/dist/esm/model/currency.js +2 -0
  116. package/dist/esm/model/currency.js.map +1 -0
  117. package/dist/esm/model/depth.js +2 -0
  118. package/dist/esm/model/depth.js.map +1 -0
  119. package/dist/esm/model/fee-policy.js +38 -0
  120. package/dist/esm/model/fee-policy.js.map +1 -0
  121. package/dist/esm/model/market.js +211 -0
  122. package/dist/esm/model/market.js.map +1 -0
  123. package/dist/esm/model/open-order.js +2 -0
  124. package/dist/esm/model/open-order.js.map +1 -0
  125. package/dist/esm/package.json +1 -0
  126. package/dist/esm/signature.js +174 -0
  127. package/dist/esm/signature.js.map +1 -0
  128. package/dist/esm/tsconfig.build.tsbuildinfo +1 -0
  129. package/dist/esm/type.js +2 -0
  130. package/dist/esm/type.js.map +1 -0
  131. package/dist/esm/utils/approval.js +42 -0
  132. package/dist/esm/utils/approval.js.map +1 -0
  133. package/{src/utils/book-id.ts → dist/esm/utils/book-id.js} +9 -19
  134. package/dist/esm/utils/book-id.js.map +1 -0
  135. package/dist/esm/utils/build-transaction.js +27 -0
  136. package/dist/esm/utils/build-transaction.js.map +1 -0
  137. package/dist/esm/utils/decimals.js +12 -0
  138. package/dist/esm/utils/decimals.js.map +1 -0
  139. package/dist/esm/utils/market.js +49 -0
  140. package/dist/esm/utils/market.js.map +1 -0
  141. package/dist/esm/utils/math.js +78 -0
  142. package/dist/esm/utils/math.js.map +1 -0
  143. package/dist/esm/utils/prices.js +17 -0
  144. package/dist/esm/utils/prices.js.map +1 -0
  145. package/dist/esm/utils/tick.js +100 -0
  146. package/dist/esm/utils/tick.js.map +1 -0
  147. package/dist/esm/utils/time.js +5 -0
  148. package/dist/esm/utils/time.js.map +1 -0
  149. package/dist/esm/utils/unit.js +34 -0
  150. package/dist/esm/utils/unit.js.map +1 -0
  151. package/dist/esm/view.js +177 -0
  152. package/dist/esm/view.js.map +1 -0
  153. package/dist/types/.graphclient/index.d.ts +1281 -0
  154. package/dist/types/.graphclient/index.d.ts.map +1 -0
  155. package/dist/types/.graphclient/sources/clober-v2/introspectionSchema.d.ts +3 -0
  156. package/dist/types/.graphclient/sources/clober-v2/introspectionSchema.d.ts.map +1 -0
  157. package/dist/types/.graphclient/sources/clober-v2/types.d.ts +984 -0
  158. package/dist/types/.graphclient/sources/clober-v2/types.d.ts.map +1 -0
  159. package/dist/types/abis/core/controller-abi.d.ts +757 -0
  160. package/dist/types/abis/core/controller-abi.d.ts.map +1 -0
  161. package/dist/types/abis/core/params-abi.d.ts +21 -0
  162. package/dist/types/abis/core/params-abi.d.ts.map +1 -0
  163. package/dist/types/apis/currency.d.ts +4 -0
  164. package/dist/types/apis/currency.d.ts.map +1 -0
  165. package/dist/types/apis/market.d.ts +4 -0
  166. package/dist/types/apis/market.d.ts.map +1 -0
  167. package/dist/types/apis/open-order.d.ts +5 -0
  168. package/dist/types/apis/open-order.d.ts.map +1 -0
  169. package/dist/types/approval.d.ts +32 -0
  170. package/dist/types/approval.d.ts.map +1 -0
  171. package/dist/types/call.d.ts +234 -0
  172. package/dist/types/call.d.ts.map +1 -0
  173. package/dist/types/constants/action.d.ts +10 -0
  174. package/dist/types/constants/action.d.ts.map +1 -0
  175. package/dist/types/constants/addresses.d.ts +9 -0
  176. package/dist/types/constants/addresses.d.ts.map +1 -0
  177. package/dist/types/constants/chain.d.ts +9 -0
  178. package/dist/types/constants/chain.d.ts.map +1 -0
  179. package/dist/types/constants/currency.d.ts +8 -0
  180. package/dist/types/constants/currency.d.ts.map +1 -0
  181. package/dist/types/constants/fee.d.ts +4 -0
  182. package/dist/types/constants/fee.d.ts.map +1 -0
  183. package/dist/types/constants/price.d.ts +3 -0
  184. package/dist/types/constants/price.d.ts.map +1 -0
  185. package/dist/types/constants/subgraph-url.d.ts +5 -0
  186. package/dist/types/constants/subgraph-url.d.ts.map +1 -0
  187. package/dist/types/index.d.ts +6 -0
  188. package/dist/types/index.d.ts.map +1 -0
  189. package/dist/types/model/book.d.ts +31 -0
  190. package/dist/types/model/book.d.ts.map +1 -0
  191. package/dist/types/model/currency.d.ts +7 -0
  192. package/dist/types/model/currency.d.ts.map +1 -0
  193. package/dist/types/model/depth.d.ts +11 -0
  194. package/dist/types/model/depth.d.ts.map +1 -0
  195. package/dist/types/model/fee-policy.d.ts +15 -0
  196. package/dist/types/model/fee-policy.d.ts.map +1 -0
  197. package/dist/types/model/market.d.ts +44 -0
  198. package/dist/types/model/market.d.ts.map +1 -0
  199. package/dist/types/model/open-order.d.ts +28 -0
  200. package/dist/types/model/open-order.d.ts.map +1 -0
  201. package/dist/types/signature.d.ts +40 -0
  202. package/dist/types/signature.d.ts.map +1 -0
  203. package/dist/types/type.d.ts +34 -0
  204. package/dist/types/type.d.ts.map +1 -0
  205. package/dist/types/utils/approval.d.ts +3 -0
  206. package/dist/types/utils/approval.d.ts.map +1 -0
  207. package/dist/types/utils/book-id.d.ts +2 -0
  208. package/dist/types/utils/book-id.d.ts.map +1 -0
  209. package/dist/types/utils/build-transaction.d.ts +5 -0
  210. package/dist/types/utils/build-transaction.d.ts.map +1 -0
  211. package/dist/types/utils/decimals.d.ts +3 -0
  212. package/dist/types/utils/decimals.d.ts.map +1 -0
  213. package/dist/types/utils/market.d.ts +7 -0
  214. package/dist/types/utils/market.d.ts.map +1 -0
  215. package/dist/types/utils/math.d.ts +3 -0
  216. package/dist/types/utils/math.d.ts.map +1 -0
  217. package/dist/types/utils/prices.d.ts +3 -0
  218. package/dist/types/utils/prices.d.ts.map +1 -0
  219. package/dist/types/utils/tick.d.ts +4 -0
  220. package/dist/types/utils/tick.d.ts.map +1 -0
  221. package/dist/types/utils/time.d.ts +2 -0
  222. package/dist/types/utils/time.d.ts.map +1 -0
  223. package/dist/types/utils/unit.d.ts +4 -0
  224. package/dist/types/utils/unit.d.ts.map +1 -0
  225. package/dist/types/view.d.ts +129 -0
  226. package/dist/types/view.d.ts.map +1 -0
  227. package/package.json +14 -6
  228. package/.eslintignore +0 -1
  229. package/.eslintrc.yaml +0 -34
  230. package/.github/workflows/ci.yaml +0 -88
  231. package/.github/workflows/deployer.yaml +0 -19
  232. package/.nvmrc +0 -1
  233. package/.prettierignore +0 -6
  234. package/.prettierrc +0 -7
  235. package/src/.graphclient/index.ts +0 -1695
  236. package/src/.graphclient/schema.graphql +0 -1116
  237. package/src/.graphclient/sources/clober-v2/introspectionSchema.ts +0 -14481
  238. package/src/.graphclient/sources/clober-v2/schema.graphql +0 -1116
  239. package/src/.graphclient/sources/clober-v2/types.ts +0 -1133
  240. package/src/.graphclientrc.yml +0 -12
  241. package/src/abis/core/controller-abi.ts +0 -985
  242. package/src/abis/core/params-abi.ts +0 -59
  243. package/src/apis/currency.ts +0 -92
  244. package/src/apis/graphql/books.graphql +0 -25
  245. package/src/apis/graphql/open-order.graphql +0 -29
  246. package/src/apis/graphql/open-orders.graphql +0 -29
  247. package/src/apis/market.ts +0 -85
  248. package/src/apis/open-order.ts +0 -145
  249. package/src/approval.ts +0 -82
  250. package/src/call.ts +0 -705
  251. package/src/constants/action.ts +0 -9
  252. package/src/constants/addresses.ts +0 -17
  253. package/src/constants/chain.ts +0 -12
  254. package/src/constants/currency.ts +0 -15
  255. package/src/constants/fee.ts +0 -4
  256. package/src/constants/price.ts +0 -3
  257. package/src/constants/subgraph-url.ts +0 -8
  258. package/src/index.ts +0 -5
  259. package/src/model/book.ts +0 -166
  260. package/src/model/currency.ts +0 -6
  261. package/src/model/depth.ts +0 -11
  262. package/src/model/fee-policy.ts +0 -51
  263. package/src/model/market.ts +0 -320
  264. package/src/model/open-order.ts +0 -16
  265. package/src/signature.ts +0 -196
  266. package/src/type.ts +0 -38
  267. package/src/utils/approval.ts +0 -48
  268. package/src/utils/build-transaction.ts +0 -39
  269. package/src/utils/decimals.ts +0 -22
  270. package/src/utils/market.ts +0 -74
  271. package/src/utils/math.ts +0 -117
  272. package/src/utils/prices.ts +0 -29
  273. package/src/utils/tick.ts +0 -104
  274. package/src/utils/time.ts +0 -6
  275. package/src/utils/unit.ts +0 -43
  276. package/src/view.ts +0 -258
  277. package/test/book-id.test.ts +0 -101
  278. package/test/fee-policy.test.ts +0 -228
  279. package/test/get-expected-input.test.ts +0 -206
  280. package/test/get-expected-output.test.ts +0 -206
  281. package/test/limit-order.test.ts +0 -279
  282. package/test/market-order.test.ts +0 -245
  283. package/test/market.test.ts +0 -68
  284. package/test/math.test.ts +0 -91
  285. package/test/open-order.test.ts +0 -112
  286. package/test/open.test.ts +0 -15
  287. package/test/tick.test.ts +0 -230
  288. package/test/tsconfig.json +0 -12
  289. package/test/utils/chain.ts +0 -12
  290. package/test/utils/constants.ts +0 -25
  291. package/test/utils/currency.ts +0 -44
  292. package/test/utils/depth.ts +0 -148
  293. package/test/utils/test-chain.ts +0 -26
  294. package/test/vitest.config.ts +0 -15
  295. package/tsconfig.base.json +0 -37
  296. package/tsconfig.build.json +0 -9
  297. package/tsconfig.json +0 -9
@@ -1,59 +0,0 @@
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
- ]
@@ -1,92 +0,0 @@
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
- }
@@ -1,25 +0,0 @@
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
- }
@@ -1,29 +0,0 @@
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
- }
@@ -1,29 +0,0 @@
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
- }
@@ -1,85 +0,0 @@
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
- }
@@ -1,145 +0,0 @@
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
- }
package/src/approval.ts DELETED
@@ -1,82 +0,0 @@
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
- }