@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,206 +0,0 @@
1
- import { expect, test } from 'vitest'
2
- import { getExpectedInput } from '@clober/v2-sdk'
3
- import { arbitrumSepolia } from 'viem/chains'
4
- import { formatUnits, isAddressEqual, parseUnits, zeroHash } from 'viem'
5
-
6
- import { fetchMarket } from '../src/apis/market'
7
- import { parsePrice } from '../src/utils/prices'
8
- import { invertPrice } from '../src/utils/tick'
9
- import { CONTRACT_ADDRESSES } from '../src/constants/addresses'
10
-
11
- import { publicClient } from './utils/constants'
12
-
13
- const _ABI = [
14
- {
15
- inputs: [
16
- {
17
- components: [
18
- {
19
- internalType: 'BookId',
20
- name: 'id',
21
- type: 'uint192',
22
- },
23
- {
24
- internalType: 'uint256',
25
- name: 'limitPrice',
26
- type: 'uint256',
27
- },
28
- {
29
- internalType: 'uint256',
30
- name: 'quoteAmount',
31
- type: 'uint256',
32
- },
33
- {
34
- internalType: 'uint256',
35
- name: 'maxBaseAmount',
36
- type: 'uint256',
37
- },
38
- {
39
- internalType: 'bytes',
40
- name: 'hookData',
41
- type: 'bytes',
42
- },
43
- ],
44
- internalType: 'struct IController.TakeOrderParams',
45
- name: 'params',
46
- type: 'tuple',
47
- },
48
- ],
49
- name: 'getExpectedInput',
50
- outputs: [
51
- {
52
- internalType: 'uint256',
53
- name: 'takenQuoteAmount',
54
- type: 'uint256',
55
- },
56
- {
57
- internalType: 'uint256',
58
- name: 'spendBaseAmount',
59
- type: 'uint256',
60
- },
61
- ],
62
- stateMutability: 'view',
63
- type: 'function',
64
- },
65
- ] as const
66
-
67
- const isTakeResultEqual = async (
68
- inputToken: `0x${string}`,
69
- outputToken: `0x${string}`,
70
- amountOut: string,
71
- limitPrice: string,
72
- ) => {
73
- const market = await fetchMarket(arbitrumSepolia.id, [
74
- inputToken,
75
- outputToken,
76
- ])
77
-
78
- const isBid = isAddressEqual(market.quote.address, inputToken)
79
- const outputCurrency = isBid ? market.base : market.quote
80
- const [takenQuoteAmount, spendBaseAmount] = await publicClient.readContract({
81
- address: CONTRACT_ADDRESSES[arbitrumSepolia.id]!.BookViewer,
82
- abi: _ABI,
83
- functionName: 'getExpectedInput',
84
- args: [
85
- {
86
- id: isBid
87
- ? 2753017174304248252793812478093441832431186343406437115611n
88
- : 2505799676027433010421416925405481572661563164234992034276n,
89
- limitPrice: isBid
90
- ? invertPrice(
91
- parsePrice(
92
- Number(limitPrice),
93
- market.quote.decimals,
94
- market.base.decimals,
95
- ),
96
- )
97
- : parsePrice(
98
- Number(limitPrice),
99
- market.quote.decimals,
100
- market.base.decimals,
101
- ),
102
- quoteAmount: parseUnits(amountOut, outputCurrency.decimals),
103
- maxBaseAmount: 0n,
104
- hookData: zeroHash,
105
- },
106
- ],
107
- })
108
-
109
- const { takenAmount, spendAmount } = await getExpectedInput(
110
- arbitrumSepolia.id,
111
- inputToken,
112
- outputToken,
113
- amountOut,
114
- { limitPrice, rpcUrl: publicClient.transport.url! },
115
- )
116
-
117
- expect(takenAmount).toBe(
118
- formatUnits(
119
- takenQuoteAmount,
120
- isBid ? market.base.decimals : market.quote.decimals,
121
- ),
122
- )
123
- expect(spendAmount).toBe(
124
- formatUnits(
125
- spendBaseAmount,
126
- isBid ? market.quote.decimals : market.base.decimals,
127
- ),
128
- )
129
- return {
130
- takenAmount,
131
- spendAmount,
132
- }
133
- }
134
-
135
- test('get expected input ask', async () => {
136
- await isTakeResultEqual(
137
- '0x00bfd44e79fb7f6dd5887a9426c8ef85a0cd23e0',
138
- '0x0000000000000000000000000000000000000000',
139
- '0.1',
140
- '100.01',
141
- )
142
-
143
- await isTakeResultEqual(
144
- '0x00bfd44e79fb7f6dd5887a9426c8ef85a0cd23e0',
145
- '0x0000000000000000000000000000000000000000',
146
- '0.2',
147
- '4005.01',
148
- )
149
-
150
- await isTakeResultEqual(
151
- '0x00bfd44e79fb7f6dd5887a9426c8ef85a0cd23e0',
152
- '0x0000000000000000000000000000000000000000',
153
- '0.23',
154
- '10000.01',
155
- )
156
-
157
- await isTakeResultEqual(
158
- '0x00bfd44e79fb7f6dd5887a9426c8ef85a0cd23e0',
159
- '0x0000000000000000000000000000000000000000',
160
- '10',
161
- (Math.pow(2, 256) - 1).toFixed(0),
162
- )
163
- })
164
-
165
- test('get expected input bid', async () => {
166
- await isTakeResultEqual(
167
- '0x0000000000000000000000000000000000000000',
168
- '0x00bfd44e79fb7f6dd5887a9426c8ef85a0cd23e0',
169
- '100000',
170
- '4010.01',
171
- )
172
-
173
- await isTakeResultEqual(
174
- '0x0000000000000000000000000000000000000000',
175
- '0x00bfd44e79fb7f6dd5887a9426c8ef85a0cd23e0',
176
- '100000',
177
- '4005.16',
178
- )
179
-
180
- await isTakeResultEqual(
181
- '0x0000000000000000000000000000000000000000',
182
- '0x00bfd44e79fb7f6dd5887a9426c8ef85a0cd23e0',
183
- '100000',
184
- '200.12',
185
- )
186
-
187
- await isTakeResultEqual(
188
- '0x0000000000000000000000000000000000000000',
189
- '0x00bfd44e79fb7f6dd5887a9426c8ef85a0cd23e0',
190
- '200000',
191
- '0',
192
- )
193
- })
194
-
195
- // @dev: this test will be fail when the market is open
196
- test('get expected input in not open book', async () => {
197
- const { takenAmount, spendAmount } = await getExpectedInput(
198
- arbitrumSepolia.id,
199
- '0xf18Be2a91cF31Fc3f8D828b6c714e1806a75e0AA',
200
- '0x0000000000000000000000000000000000000000',
201
- '0.1',
202
- { rpcUrl: publicClient.transport.url! },
203
- )
204
- expect(takenAmount).toBe('0')
205
- expect(spendAmount).toBe('0')
206
- })
@@ -1,206 +0,0 @@
1
- import { expect, test } from 'vitest'
2
- import { getExpectedOutput } from '@clober/v2-sdk'
3
- import { arbitrumSepolia } from 'viem/chains'
4
- import { formatUnits, isAddressEqual, parseUnits, zeroHash } from 'viem'
5
-
6
- import { fetchMarket } from '../src/apis/market'
7
- import { parsePrice } from '../src/utils/prices'
8
- import { invertPrice } from '../src/utils/tick'
9
- import { CONTRACT_ADDRESSES } from '../src/constants/addresses'
10
-
11
- import { publicClient } from './utils/constants'
12
-
13
- const _ABI = [
14
- {
15
- inputs: [
16
- {
17
- components: [
18
- {
19
- internalType: 'BookId',
20
- name: 'id',
21
- type: 'uint192',
22
- },
23
- {
24
- internalType: 'uint256',
25
- name: 'limitPrice',
26
- type: 'uint256',
27
- },
28
- {
29
- internalType: 'uint256',
30
- name: 'baseAmount',
31
- type: 'uint256',
32
- },
33
- {
34
- internalType: 'uint256',
35
- name: 'minQuoteAmount',
36
- type: 'uint256',
37
- },
38
- {
39
- internalType: 'bytes',
40
- name: 'hookData',
41
- type: 'bytes',
42
- },
43
- ],
44
- internalType: 'struct IController.SpendOrderParams',
45
- name: 'params',
46
- type: 'tuple',
47
- },
48
- ],
49
- name: 'getExpectedOutput',
50
- outputs: [
51
- {
52
- internalType: 'uint256',
53
- name: 'takenQuoteAmount',
54
- type: 'uint256',
55
- },
56
- {
57
- internalType: 'uint256',
58
- name: 'spendBaseAmount',
59
- type: 'uint256',
60
- },
61
- ],
62
- stateMutability: 'view',
63
- type: 'function',
64
- },
65
- ] as const
66
-
67
- const isSpendResultEqual = async (
68
- inputToken: `0x${string}`,
69
- outputToken: `0x${string}`,
70
- amountIn: string,
71
- limitPrice: string,
72
- ) => {
73
- const market = await fetchMarket(arbitrumSepolia.id, [
74
- inputToken,
75
- outputToken,
76
- ])
77
-
78
- const isBid = isAddressEqual(market.quote.address, inputToken)
79
- const inputCurrency = isBid ? market.quote : market.base
80
- const [takenQuoteAmount, spendBaseAmount] = await publicClient.readContract({
81
- address: CONTRACT_ADDRESSES[arbitrumSepolia.id]!.BookViewer,
82
- abi: _ABI,
83
- functionName: 'getExpectedOutput',
84
- args: [
85
- {
86
- id: isBid
87
- ? 2753017174304248252793812478093441832431186343406437115611n
88
- : 2505799676027433010421416925405481572661563164234992034276n,
89
- limitPrice: isBid
90
- ? invertPrice(
91
- parsePrice(
92
- Number(limitPrice),
93
- market.quote.decimals,
94
- market.base.decimals,
95
- ),
96
- )
97
- : parsePrice(
98
- Number(limitPrice),
99
- market.quote.decimals,
100
- market.base.decimals,
101
- ),
102
- baseAmount: parseUnits(amountIn, inputCurrency.decimals),
103
- minQuoteAmount: 0n,
104
- hookData: zeroHash,
105
- },
106
- ],
107
- })
108
-
109
- const { takenAmount, spendAmount } = await getExpectedOutput(
110
- arbitrumSepolia.id,
111
- inputToken,
112
- outputToken,
113
- amountIn,
114
- { limitPrice, rpcUrl: publicClient.transport.url! },
115
- )
116
-
117
- expect(takenAmount).toBe(
118
- formatUnits(
119
- takenQuoteAmount,
120
- isBid ? market.base.decimals : market.quote.decimals,
121
- ),
122
- )
123
- expect(spendAmount).toBe(
124
- formatUnits(
125
- spendBaseAmount,
126
- isBid ? market.quote.decimals : market.base.decimals,
127
- ),
128
- )
129
- return {
130
- takenAmount,
131
- spendAmount,
132
- }
133
- }
134
-
135
- test('get expected output ask', async () => {
136
- await isSpendResultEqual(
137
- '0x00bfd44e79fb7f6dd5887a9426c8ef85a0cd23e0',
138
- '0x0000000000000000000000000000000000000000',
139
- '1000.123',
140
- '100.01',
141
- )
142
-
143
- await isSpendResultEqual(
144
- '0x00bfd44e79fb7f6dd5887a9426c8ef85a0cd23e0',
145
- '0x0000000000000000000000000000000000000000',
146
- '1000000',
147
- '4005.01',
148
- )
149
-
150
- await isSpendResultEqual(
151
- '0x00bfd44e79fb7f6dd5887a9426c8ef85a0cd23e0',
152
- '0x0000000000000000000000000000000000000000',
153
- '1000.123',
154
- '10000.01',
155
- )
156
-
157
- await isSpendResultEqual(
158
- '0x00bfd44e79fb7f6dd5887a9426c8ef85a0cd23e0',
159
- '0x0000000000000000000000000000000000000000',
160
- '1000000',
161
- (Math.pow(2, 256) - 1).toFixed(0),
162
- )
163
- })
164
-
165
- test('get expected output bid', async () => {
166
- await isSpendResultEqual(
167
- '0x0000000000000000000000000000000000000000',
168
- '0x00bfd44e79fb7f6dd5887a9426c8ef85a0cd23e0',
169
- '0.01',
170
- '4010.01',
171
- )
172
-
173
- await isSpendResultEqual(
174
- '0x0000000000000000000000000000000000000000',
175
- '0x00bfd44e79fb7f6dd5887a9426c8ef85a0cd23e0',
176
- '0.1',
177
- '4005.01',
178
- )
179
-
180
- await isSpendResultEqual(
181
- '0x0000000000000000000000000000000000000000',
182
- '0x00bfd44e79fb7f6dd5887a9426c8ef85a0cd23e0',
183
- '1.1234',
184
- '200.01',
185
- )
186
-
187
- await isSpendResultEqual(
188
- '0x0000000000000000000000000000000000000000',
189
- '0x00bfd44e79fb7f6dd5887a9426c8ef85a0cd23e0',
190
- '3.14',
191
- '0',
192
- )
193
- })
194
-
195
- // @dev: this test will be fail when the market is open
196
- test('get expected output in not open book', async () => {
197
- const { takenAmount, spendAmount } = await getExpectedOutput(
198
- arbitrumSepolia.id,
199
- '0xf18Be2a91cF31Fc3f8D828b6c714e1806a75e0AA',
200
- '0x0000000000000000000000000000000000000000',
201
- '10000',
202
- { rpcUrl: publicClient.transport.url! },
203
- )
204
- expect(takenAmount).toBe('0')
205
- expect(spendAmount).toBe('0')
206
- })
@@ -1,279 +0,0 @@
1
- import { expect, test } from 'vitest'
2
- import { limitOrder, signERC20Permit } from '@clober/v2-sdk'
3
- import { formatUnits } from 'viem'
4
- import { arbitrumSepolia } from 'viem/chains'
5
-
6
- import { fetchTokenBalance } from './utils/currency'
7
- import { fetchAskDepth, fetchBidDepth, getSize } from './utils/depth'
8
- import { account, publicClient, walletClient } from './utils/constants'
9
- import { cloberTestChain } from './utils/test-chain'
10
-
11
- const IS_LOCAL = process.env.IS_LOCAL === 'true'
12
-
13
- test.runIf(IS_LOCAL)('limit order in not open market', async () => {
14
- expect(
15
- await limitOrder(
16
- arbitrumSepolia.id,
17
- '0x447ad4a108b5540c220f9f7e83723ac87c0f8fd8',
18
- '0x447ad4a108b5540c220f9f7e83723ac87c0f8fd8',
19
- '0x0000000000000000000000000000000000000000',
20
- '10',
21
- '1000',
22
- { rpcUrl: publicClient.transport.url! },
23
- ).catch((e) => e.message),
24
- ).toEqual(`
25
- import { openMarket } from '@clober/v2-sdk'
26
-
27
- const transaction = await openMarket(
28
- ${cloberTestChain.id},
29
- '0x447ad4a108b5540c220f9f7e83723ac87c0f8fd8',
30
- '0x0000000000000000000000000000000000000000',
31
- )
32
- `)
33
- })
34
-
35
- test.runIf(IS_LOCAL)('make bid order', async () => {
36
- const signature = await signERC20Permit(
37
- cloberTestChain.id,
38
- account,
39
- '0x00bfd44e79fb7f6dd5887a9426c8ef85a0cd23e0',
40
- '1000',
41
- { rpcUrl: publicClient.transport.url! },
42
- )
43
- const transaction = await limitOrder(
44
- cloberTestChain.id,
45
- account.address,
46
- '0x00bfd44e79fb7f6dd5887a9426c8ef85a0cd23e0',
47
- '0x0000000000000000000000000000000000000000',
48
- '1000',
49
- '0.01',
50
- { signature, rpcUrl: publicClient.transport.url!, postOnly: true },
51
- )
52
-
53
- const [beforeBalance, beforeDepth] = await Promise.all([
54
- fetchTokenBalance(
55
- cloberTestChain.id,
56
- '0x00bfd44e79fb7f6dd5887a9426c8ef85a0cd23e0',
57
- account.address,
58
- publicClient.transport.url!,
59
- ),
60
- fetchBidDepth(publicClient.transport.url!),
61
- ])
62
-
63
- const hash = await walletClient.sendTransaction({
64
- ...transaction!,
65
- account,
66
- gasPrice: transaction!.gasPrice! * 2n,
67
- })
68
- const receipt = await publicClient.waitForTransactionReceipt({ hash })
69
- expect(receipt.status).toBe('success')
70
-
71
- const [afterBalance, afterDepth] = await Promise.all([
72
- fetchTokenBalance(
73
- cloberTestChain.id,
74
- '0x00bfd44e79fb7f6dd5887a9426c8ef85a0cd23e0',
75
- account.address,
76
- publicClient.transport.url!,
77
- ),
78
- fetchBidDepth(publicClient.transport.url!),
79
- ])
80
-
81
- expect(beforeBalance - afterBalance).toEqual(1000000000n)
82
- expect(getSize(afterDepth, 0, 0.01)).greaterThan(
83
- getSize(beforeDepth, 0, 0.01),
84
- )
85
- })
86
-
87
- test.runIf(IS_LOCAL)('make ask order', async () => {
88
- const transaction = await limitOrder(
89
- cloberTestChain.id,
90
- account.address,
91
- '0x0000000000000000000000000000000000000000',
92
- '0x00bfd44e79fb7f6dd5887a9426c8ef85a0cd23e0',
93
- '0.001',
94
- '8000.01',
95
- { rpcUrl: publicClient.transport.url!, postOnly: true },
96
- )
97
-
98
- const [beforeBalance, beforeDepth] = await Promise.all([
99
- publicClient.getBalance({
100
- address: account.address,
101
- }),
102
- fetchAskDepth(publicClient.transport.url!),
103
- ])
104
-
105
- const hash = await walletClient.sendTransaction({
106
- ...transaction!,
107
- account,
108
- gasPrice: transaction!.gasPrice! * 2n,
109
- })
110
- const receipt = await publicClient.waitForTransactionReceipt({ hash })
111
- expect(receipt.status).toBe('success')
112
-
113
- const [afterBalance, afterDepth] = await Promise.all([
114
- publicClient.getBalance({
115
- address: account.address,
116
- }),
117
- fetchAskDepth(publicClient.transport.url!),
118
- ])
119
-
120
- expect(Number(beforeBalance - afterBalance)).greaterThan(Number(10n ** 15n))
121
- expect(getSize(afterDepth, 8000, 8001)).greaterThan(
122
- getSize(beforeDepth, 8000, 8001),
123
- )
124
- })
125
-
126
- test.runIf(IS_LOCAL)('limit bid order', async () => {
127
- const beforeBidDepth = await fetchBidDepth(publicClient.transport.url!)
128
- const makeTx = await limitOrder(
129
- cloberTestChain.id,
130
- account.address,
131
- '0x0000000000000000000000000000000000000000',
132
- '0x00bfd44e79fb7f6dd5887a9426c8ef85a0cd23e0',
133
- '0.001',
134
- beforeBidDepth.length === 0
135
- ? '5001'
136
- : (beforeBidDepth[0]!.price + 1).toString(),
137
- { rpcUrl: publicClient.transport.url!, postOnly: true },
138
- )
139
- await publicClient.waitForTransactionReceipt({
140
- hash: await walletClient.sendTransaction({
141
- ...makeTx!,
142
- account,
143
- gasPrice: makeTx!.gasPrice! * 2n,
144
- }),
145
- })
146
-
147
- const [beforeUSDCBalance, beforeETHBalance] = await Promise.all([
148
- fetchTokenBalance(
149
- cloberTestChain.id,
150
- '0x00bfd44e79fb7f6dd5887a9426c8ef85a0cd23e0',
151
- account.address,
152
- publicClient.transport.url!,
153
- ),
154
- publicClient.getBalance({
155
- address: account.address,
156
- }),
157
- ])
158
- const signature = await signERC20Permit(
159
- cloberTestChain.id,
160
- account,
161
- '0x00bfd44e79fb7f6dd5887a9426c8ef85a0cd23e0',
162
- '100000',
163
- { rpcUrl: publicClient.transport.url! },
164
- )
165
- const transaction = await limitOrder(
166
- cloberTestChain.id,
167
- account.address,
168
- '0x00bfd44e79fb7f6dd5887a9426c8ef85a0cd23e0',
169
- '0x0000000000000000000000000000000000000000',
170
- '100000',
171
- beforeBidDepth.length === 0
172
- ? '5002'
173
- : (beforeBidDepth[0]!.price + 2).toString(),
174
- { signature, rpcUrl: publicClient.transport.url! },
175
- )
176
-
177
- const hash = await walletClient.sendTransaction({
178
- ...transaction!,
179
- account,
180
- gasPrice: transaction!.gasPrice! * 2n,
181
- })
182
- const receipt = await publicClient.waitForTransactionReceipt({ hash })
183
- expect(receipt.status).toBe('success')
184
-
185
- const [afterUSDCBalance, afterETHBalance] = await Promise.all([
186
- fetchTokenBalance(
187
- cloberTestChain.id,
188
- '0x00bfd44e79fb7f6dd5887a9426c8ef85a0cd23e0',
189
- account.address,
190
- publicClient.transport.url!,
191
- ),
192
- publicClient.getBalance({
193
- address: account.address,
194
- }),
195
- ])
196
-
197
- expect(beforeUSDCBalance - afterUSDCBalance).toEqual(100000000000n)
198
- expect(Number(formatUnits(afterETHBalance, 18))).greaterThan(
199
- Number(formatUnits(beforeETHBalance, 18)),
200
- )
201
- })
202
-
203
- test.runIf(IS_LOCAL)('limit ask order', async () => {
204
- const beforeAskDepth = await fetchAskDepth(publicClient.transport.url!)
205
- const signature = await signERC20Permit(
206
- cloberTestChain.id,
207
- account,
208
- '0x00bfd44e79fb7f6dd5887a9426c8ef85a0cd23e0',
209
- '100',
210
- { rpcUrl: publicClient.transport.url! },
211
- )
212
- const makeTx = await limitOrder(
213
- cloberTestChain.id,
214
- account.address,
215
- '0x00bfd44e79fb7f6dd5887a9426c8ef85a0cd23e0',
216
- '0x0000000000000000000000000000000000000000',
217
- '100',
218
- beforeAskDepth.length === 0
219
- ? '4999'
220
- : (beforeAskDepth[0]!.price - 1).toString(),
221
- { signature, rpcUrl: publicClient.transport.url!, postOnly: true },
222
- )
223
- await publicClient.waitForTransactionReceipt({
224
- hash: await walletClient.sendTransaction({
225
- ...makeTx!,
226
- account,
227
- gasPrice: makeTx!.gasPrice! * 2n,
228
- }),
229
- })
230
-
231
- const transaction = await limitOrder(
232
- cloberTestChain.id,
233
- account.address,
234
- '0x0000000000000000000000000000000000000000',
235
- '0x00bfd44e79fb7f6dd5887a9426c8ef85a0cd23e0',
236
- '0.001',
237
- beforeAskDepth.length === 0
238
- ? '4998'
239
- : (beforeAskDepth[0]!.price - 2).toString(),
240
- { rpcUrl: publicClient.transport.url! },
241
- )
242
-
243
- const [beforeUSDCBalance, beforeETHBalance] = await Promise.all([
244
- fetchTokenBalance(
245
- cloberTestChain.id,
246
- '0x00bfd44e79fb7f6dd5887a9426c8ef85a0cd23e0',
247
- account.address,
248
- publicClient.transport.url!,
249
- ),
250
- publicClient.getBalance({
251
- address: account.address,
252
- }),
253
- ])
254
-
255
- const hash = await walletClient.sendTransaction({
256
- ...transaction!,
257
- account,
258
- gasPrice: transaction!.gasPrice! * 2n,
259
- })
260
- const receipt = await publicClient.waitForTransactionReceipt({ hash })
261
- expect(receipt.status).toBe('success')
262
-
263
- const [afterUSDCBalance, afterETHBalance] = await Promise.all([
264
- fetchTokenBalance(
265
- cloberTestChain.id,
266
- '0x00bfd44e79fb7f6dd5887a9426c8ef85a0cd23e0',
267
- account.address,
268
- publicClient.transport.url!,
269
- ),
270
- publicClient.getBalance({
271
- address: account.address,
272
- }),
273
- ])
274
-
275
- expect(beforeETHBalance - afterETHBalance).toBeGreaterThan(1000000000000000)
276
- expect(Number(formatUnits(afterUSDCBalance, 18))).greaterThan(
277
- Number(formatUnits(beforeUSDCBalance, 18)),
278
- )
279
- })