@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.
- package/.eslintignore +1 -0
- package/.eslintrc.yaml +34 -0
- package/.github/workflows/ci.yaml +88 -0
- package/.github/workflows/deployer.yaml +19 -0
- package/.nvmrc +1 -0
- package/.prettierignore +6 -0
- package/.prettierrc +7 -0
- package/package.json +8 -4
- package/src/.graphclient/index.ts +1695 -0
- package/src/.graphclient/schema.graphql +1116 -0
- package/src/.graphclient/sources/clober-v2/introspectionSchema.ts +14481 -0
- package/src/.graphclient/sources/clober-v2/schema.graphql +1116 -0
- package/src/.graphclient/sources/clober-v2/types.ts +1133 -0
- package/src/.graphclientrc.yml +12 -0
- package/src/abis/core/controller-abi.ts +985 -0
- package/src/abis/core/params-abi.ts +59 -0
- package/src/apis/currency.ts +92 -0
- package/src/apis/graphql/books.graphql +25 -0
- package/src/apis/graphql/open-order.graphql +29 -0
- package/src/apis/graphql/open-orders.graphql +29 -0
- package/src/apis/market.ts +85 -0
- package/src/apis/open-order.ts +145 -0
- package/src/approval.ts +82 -0
- package/src/call.ts +705 -0
- package/src/constants/action.ts +9 -0
- package/src/constants/addresses.ts +17 -0
- package/src/constants/chain.ts +12 -0
- package/src/constants/currency.ts +15 -0
- package/src/constants/fee.ts +4 -0
- package/src/constants/price.ts +3 -0
- package/src/constants/subgraph-url.ts +8 -0
- package/src/index.ts +5 -0
- package/src/model/book.ts +166 -0
- package/src/model/currency.ts +6 -0
- package/src/model/depth.ts +11 -0
- package/src/model/fee-policy.ts +51 -0
- package/src/model/market.ts +320 -0
- package/src/model/open-order.ts +16 -0
- package/src/signature.ts +196 -0
- package/src/type.ts +38 -0
- package/src/utils/approval.ts +48 -0
- package/{dist/esm/utils/book-id.js → src/utils/book-id.ts} +19 -9
- package/src/utils/build-transaction.ts +39 -0
- package/src/utils/decimals.ts +22 -0
- package/src/utils/market.ts +74 -0
- package/src/utils/math.ts +117 -0
- package/src/utils/prices.ts +29 -0
- package/src/utils/tick.ts +104 -0
- package/src/utils/time.ts +6 -0
- package/src/utils/unit.ts +43 -0
- package/src/view.ts +258 -0
- package/test/book-id.test.ts +101 -0
- package/test/fee-policy.test.ts +228 -0
- package/test/get-expected-input.test.ts +206 -0
- package/test/get-expected-output.test.ts +206 -0
- package/test/limit-order.test.ts +279 -0
- package/test/market-order.test.ts +245 -0
- package/test/market.test.ts +68 -0
- package/test/math.test.ts +91 -0
- package/test/open-order.test.ts +112 -0
- package/test/open.test.ts +15 -0
- package/test/tick.test.ts +230 -0
- package/test/tsconfig.json +12 -0
- package/test/utils/chain.ts +12 -0
- package/test/utils/constants.ts +25 -0
- package/test/utils/currency.ts +44 -0
- package/test/utils/depth.ts +148 -0
- package/test/utils/test-chain.ts +26 -0
- package/test/vitest.config.ts +15 -0
- package/tsconfig.base.json +37 -0
- package/tsconfig.build.json +9 -0
- package/tsconfig.json +9 -0
- package/dist/cjs/.graphclient/index.js +0 -243
- package/dist/cjs/.graphclient/index.js.map +0 -1
- package/dist/cjs/.graphclient/sources/clober-v2/introspectionSchema.js +0 -14482
- package/dist/cjs/.graphclient/sources/clober-v2/introspectionSchema.js.map +0 -1
- package/dist/cjs/.graphclient/sources/clober-v2/types.js +0 -4
- package/dist/cjs/.graphclient/sources/clober-v2/types.js.map +0 -1
- package/dist/cjs/abis/core/controller-abi.js +0 -989
- package/dist/cjs/abis/core/controller-abi.js.map +0 -1
- package/dist/cjs/abis/core/params-abi.js +0 -62
- package/dist/cjs/abis/core/params-abi.js.map +0 -1
- package/dist/cjs/apis/currency.js +0 -87
- package/dist/cjs/apis/currency.js.map +0 -1
- package/dist/cjs/apis/market.js +0 -63
- package/dist/cjs/apis/market.js.map +0 -1
- package/dist/cjs/apis/open-order.js +0 -90
- package/dist/cjs/apis/open-order.js.map +0 -1
- package/dist/cjs/approval.js +0 -74
- package/dist/cjs/approval.js.map +0 -1
- package/dist/cjs/call.js +0 -545
- package/dist/cjs/call.js.map +0 -1
- package/dist/cjs/constants/action.js +0 -14
- package/dist/cjs/constants/action.js.map +0 -1
- package/dist/cjs/constants/addresses.js +0 -13
- package/dist/cjs/constants/addresses.js.map +0 -1
- package/dist/cjs/constants/chain.js +0 -13
- package/dist/cjs/constants/chain.js.map +0 -1
- package/dist/cjs/constants/currency.js +0 -12
- package/dist/cjs/constants/currency.js.map +0 -1
- package/dist/cjs/constants/fee.js +0 -7
- package/dist/cjs/constants/fee.js.map +0 -1
- package/dist/cjs/constants/price.js +0 -6
- package/dist/cjs/constants/price.js.map +0 -1
- package/dist/cjs/constants/subgraph-url.js +0 -8
- package/dist/cjs/constants/subgraph-url.js.map +0 -1
- package/dist/cjs/index.js +0 -22
- package/dist/cjs/index.js.map +0 -1
- package/dist/cjs/model/book.js +0 -130
- package/dist/cjs/model/book.js.map +0 -1
- package/dist/cjs/model/currency.js +0 -3
- package/dist/cjs/model/currency.js.map +0 -1
- package/dist/cjs/model/depth.js +0 -3
- package/dist/cjs/model/depth.js.map +0 -1
- package/dist/cjs/model/fee-policy.js +0 -42
- package/dist/cjs/model/fee-policy.js.map +0 -1
- package/dist/cjs/model/market.js +0 -215
- package/dist/cjs/model/market.js.map +0 -1
- package/dist/cjs/model/open-order.js +0 -3
- package/dist/cjs/model/open-order.js.map +0 -1
- package/dist/cjs/package.json +0 -1
- package/dist/cjs/signature.js +0 -178
- package/dist/cjs/signature.js.map +0 -1
- package/dist/cjs/tsconfig.build.tsbuildinfo +0 -1
- package/dist/cjs/type.js +0 -6
- package/dist/cjs/type.js.map +0 -1
- package/dist/cjs/utils/approval.js +0 -46
- package/dist/cjs/utils/approval.js.map +0 -1
- package/dist/cjs/utils/book-id.js +0 -25
- package/dist/cjs/utils/book-id.js.map +0 -1
- package/dist/cjs/utils/build-transaction.js +0 -31
- package/dist/cjs/utils/build-transaction.js.map +0 -1
- package/dist/cjs/utils/decimals.js +0 -17
- package/dist/cjs/utils/decimals.js.map +0 -1
- package/dist/cjs/utils/market.js +0 -53
- package/dist/cjs/utils/market.js.map +0 -1
- package/dist/cjs/utils/math.js +0 -83
- package/dist/cjs/utils/math.js.map +0 -1
- package/dist/cjs/utils/prices.js +0 -22
- package/dist/cjs/utils/prices.js.map +0 -1
- package/dist/cjs/utils/tick.js +0 -106
- package/dist/cjs/utils/tick.js.map +0 -1
- package/dist/cjs/utils/time.js +0 -9
- package/dist/cjs/utils/time.js.map +0 -1
- package/dist/cjs/utils/unit.js +0 -38
- package/dist/cjs/utils/unit.js.map +0 -1
- package/dist/cjs/view.js +0 -185
- package/dist/cjs/view.js.map +0 -1
- package/dist/esm/.graphclient/index.js +0 -233
- package/dist/esm/.graphclient/index.js.map +0 -1
- package/dist/esm/.graphclient/sources/clober-v2/introspectionSchema.js +0 -14480
- package/dist/esm/.graphclient/sources/clober-v2/introspectionSchema.js.map +0 -1
- package/dist/esm/.graphclient/sources/clober-v2/types.js +0 -3
- package/dist/esm/.graphclient/sources/clober-v2/types.js.map +0 -1
- package/dist/esm/abis/core/controller-abi.js +0 -986
- package/dist/esm/abis/core/controller-abi.js.map +0 -1
- package/dist/esm/abis/core/params-abi.js +0 -59
- package/dist/esm/abis/core/params-abi.js.map +0 -1
- package/dist/esm/apis/currency.js +0 -83
- package/dist/esm/apis/currency.js.map +0 -1
- package/dist/esm/apis/market.js +0 -59
- package/dist/esm/apis/market.js.map +0 -1
- package/dist/esm/apis/open-order.js +0 -85
- package/dist/esm/apis/open-order.js.map +0 -1
- package/dist/esm/approval.js +0 -70
- package/dist/esm/approval.js.map +0 -1
- package/dist/esm/call.js +0 -535
- package/dist/esm/call.js.map +0 -1
- package/dist/esm/constants/action.js +0 -11
- package/dist/esm/constants/action.js.map +0 -1
- package/dist/esm/constants/addresses.js +0 -10
- package/dist/esm/constants/addresses.js.map +0 -1
- package/dist/esm/constants/chain.js +0 -10
- package/dist/esm/constants/chain.js.map +0 -1
- package/dist/esm/constants/currency.js +0 -9
- package/dist/esm/constants/currency.js.map +0 -1
- package/dist/esm/constants/fee.js +0 -4
- package/dist/esm/constants/fee.js.map +0 -1
- package/dist/esm/constants/price.js +0 -3
- package/dist/esm/constants/price.js.map +0 -1
- package/dist/esm/constants/subgraph-url.js +0 -5
- package/dist/esm/constants/subgraph-url.js.map +0 -1
- package/dist/esm/index.js +0 -6
- package/dist/esm/index.js.map +0 -1
- package/dist/esm/model/book.js +0 -126
- package/dist/esm/model/book.js.map +0 -1
- package/dist/esm/model/currency.js +0 -2
- package/dist/esm/model/currency.js.map +0 -1
- package/dist/esm/model/depth.js +0 -2
- package/dist/esm/model/depth.js.map +0 -1
- package/dist/esm/model/fee-policy.js +0 -38
- package/dist/esm/model/fee-policy.js.map +0 -1
- package/dist/esm/model/market.js +0 -211
- package/dist/esm/model/market.js.map +0 -1
- package/dist/esm/model/open-order.js +0 -2
- package/dist/esm/model/open-order.js.map +0 -1
- package/dist/esm/package.json +0 -1
- package/dist/esm/signature.js +0 -174
- package/dist/esm/signature.js.map +0 -1
- package/dist/esm/tsconfig.build.tsbuildinfo +0 -1
- package/dist/esm/type.js +0 -2
- package/dist/esm/type.js.map +0 -1
- package/dist/esm/utils/approval.js +0 -42
- package/dist/esm/utils/approval.js.map +0 -1
- package/dist/esm/utils/book-id.js.map +0 -1
- package/dist/esm/utils/build-transaction.js +0 -27
- package/dist/esm/utils/build-transaction.js.map +0 -1
- package/dist/esm/utils/decimals.js +0 -12
- package/dist/esm/utils/decimals.js.map +0 -1
- package/dist/esm/utils/market.js +0 -49
- package/dist/esm/utils/market.js.map +0 -1
- package/dist/esm/utils/math.js +0 -78
- package/dist/esm/utils/math.js.map +0 -1
- package/dist/esm/utils/prices.js +0 -17
- package/dist/esm/utils/prices.js.map +0 -1
- package/dist/esm/utils/tick.js +0 -100
- package/dist/esm/utils/tick.js.map +0 -1
- package/dist/esm/utils/time.js +0 -5
- package/dist/esm/utils/time.js.map +0 -1
- package/dist/esm/utils/unit.js +0 -34
- package/dist/esm/utils/unit.js.map +0 -1
- package/dist/esm/view.js +0 -177
- package/dist/esm/view.js.map +0 -1
- package/dist/types/.graphclient/index.d.ts +0 -1281
- package/dist/types/.graphclient/index.d.ts.map +0 -1
- package/dist/types/.graphclient/sources/clober-v2/introspectionSchema.d.ts +0 -3
- package/dist/types/.graphclient/sources/clober-v2/introspectionSchema.d.ts.map +0 -1
- package/dist/types/.graphclient/sources/clober-v2/types.d.ts +0 -984
- package/dist/types/.graphclient/sources/clober-v2/types.d.ts.map +0 -1
- package/dist/types/abis/core/controller-abi.d.ts +0 -757
- package/dist/types/abis/core/controller-abi.d.ts.map +0 -1
- package/dist/types/abis/core/params-abi.d.ts +0 -21
- package/dist/types/abis/core/params-abi.d.ts.map +0 -1
- package/dist/types/apis/currency.d.ts +0 -4
- package/dist/types/apis/currency.d.ts.map +0 -1
- package/dist/types/apis/market.d.ts +0 -4
- package/dist/types/apis/market.d.ts.map +0 -1
- package/dist/types/apis/open-order.d.ts +0 -5
- package/dist/types/apis/open-order.d.ts.map +0 -1
- package/dist/types/approval.d.ts +0 -32
- package/dist/types/approval.d.ts.map +0 -1
- package/dist/types/call.d.ts +0 -234
- package/dist/types/call.d.ts.map +0 -1
- package/dist/types/constants/action.d.ts +0 -10
- package/dist/types/constants/action.d.ts.map +0 -1
- package/dist/types/constants/addresses.d.ts +0 -9
- package/dist/types/constants/addresses.d.ts.map +0 -1
- package/dist/types/constants/chain.d.ts +0 -9
- package/dist/types/constants/chain.d.ts.map +0 -1
- package/dist/types/constants/currency.d.ts +0 -8
- package/dist/types/constants/currency.d.ts.map +0 -1
- package/dist/types/constants/fee.d.ts +0 -4
- package/dist/types/constants/fee.d.ts.map +0 -1
- package/dist/types/constants/price.d.ts +0 -3
- package/dist/types/constants/price.d.ts.map +0 -1
- package/dist/types/constants/subgraph-url.d.ts +0 -5
- package/dist/types/constants/subgraph-url.d.ts.map +0 -1
- package/dist/types/index.d.ts +0 -6
- package/dist/types/index.d.ts.map +0 -1
- package/dist/types/model/book.d.ts +0 -31
- package/dist/types/model/book.d.ts.map +0 -1
- package/dist/types/model/currency.d.ts +0 -7
- package/dist/types/model/currency.d.ts.map +0 -1
- package/dist/types/model/depth.d.ts +0 -11
- package/dist/types/model/depth.d.ts.map +0 -1
- package/dist/types/model/fee-policy.d.ts +0 -15
- package/dist/types/model/fee-policy.d.ts.map +0 -1
- package/dist/types/model/market.d.ts +0 -44
- package/dist/types/model/market.d.ts.map +0 -1
- package/dist/types/model/open-order.d.ts +0 -28
- package/dist/types/model/open-order.d.ts.map +0 -1
- package/dist/types/signature.d.ts +0 -40
- package/dist/types/signature.d.ts.map +0 -1
- package/dist/types/type.d.ts +0 -34
- package/dist/types/type.d.ts.map +0 -1
- package/dist/types/utils/approval.d.ts +0 -3
- package/dist/types/utils/approval.d.ts.map +0 -1
- package/dist/types/utils/book-id.d.ts +0 -2
- package/dist/types/utils/book-id.d.ts.map +0 -1
- package/dist/types/utils/build-transaction.d.ts +0 -5
- package/dist/types/utils/build-transaction.d.ts.map +0 -1
- package/dist/types/utils/decimals.d.ts +0 -3
- package/dist/types/utils/decimals.d.ts.map +0 -1
- package/dist/types/utils/market.d.ts +0 -7
- package/dist/types/utils/market.d.ts.map +0 -1
- package/dist/types/utils/math.d.ts +0 -3
- package/dist/types/utils/math.d.ts.map +0 -1
- package/dist/types/utils/prices.d.ts +0 -3
- package/dist/types/utils/prices.d.ts.map +0 -1
- package/dist/types/utils/tick.d.ts +0 -4
- package/dist/types/utils/tick.d.ts.map +0 -1
- package/dist/types/utils/time.d.ts +0 -2
- package/dist/types/utils/time.d.ts.map +0 -1
- package/dist/types/utils/unit.d.ts +0 -4
- package/dist/types/utils/unit.d.ts.map +0 -1
- package/dist/types/view.d.ts +0 -129
- package/dist/types/view.d.ts.map +0 -1
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { PRICE_PRECISION } from '../constants/price'
|
|
2
|
+
|
|
3
|
+
import { lnWad } from './math'
|
|
4
|
+
|
|
5
|
+
const _R0 = 0xfff97272373d413259a46990n
|
|
6
|
+
const _R1 = 0xfff2e50f5f656932ef12357cn
|
|
7
|
+
const _R2 = 0xffe5caca7e10e4e61c3624ean
|
|
8
|
+
const _R3 = 0xffcb9843d60f6159c9db5883n
|
|
9
|
+
const _R4 = 0xff973b41fa98c081472e6896n
|
|
10
|
+
const _R5 = 0xff2ea16466c96a3843ec78b3n
|
|
11
|
+
const _R6 = 0xfe5dee046a99a2a811c461f1n
|
|
12
|
+
const _R7 = 0xfcbe86c7900a88aedcffc83bn
|
|
13
|
+
const _R8 = 0xf987a7253ac413176f2b074cn
|
|
14
|
+
const _R9 = 0xf3392b0822b70005940c7a39n
|
|
15
|
+
const _R10 = 0xe7159475a2c29b7443b29c7fn
|
|
16
|
+
const _R11 = 0xd097f3bdfd2022b8845ad8f7n
|
|
17
|
+
const _R12 = 0xa9f746462d870fdf8a65dc1fn
|
|
18
|
+
const _R13 = 0x70d869a156d2a1b890bb3df6n
|
|
19
|
+
const _R14 = 0x31be135f97d08fd981231505n
|
|
20
|
+
const _R15 = 0x9aa508b5b7a84e1c677de54n
|
|
21
|
+
const _R16 = 0x5d6af8dedb81196699c329n
|
|
22
|
+
const _R17 = 0x2216e584f5fa1ea92604n
|
|
23
|
+
const _R18 = 0x48a170391f7dc42n
|
|
24
|
+
|
|
25
|
+
export const invertPrice = (price: bigint): bigint => {
|
|
26
|
+
return price ? (1n << (PRICE_PRECISION * 2n)) / price : 0n
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export const fromPrice = (price: bigint): bigint => {
|
|
30
|
+
const tick = (lnWad(price) * 42951820407860n) / 2n ** 128n
|
|
31
|
+
if (toPrice(tick) > price) {
|
|
32
|
+
return tick - 1n
|
|
33
|
+
}
|
|
34
|
+
return tick
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export const toPrice = (tick: bigint): bigint => {
|
|
38
|
+
const absTick = tick < 0n ? -tick : tick
|
|
39
|
+
let price = 0n
|
|
40
|
+
if ((absTick & 0x1n) !== 0n) {
|
|
41
|
+
price = _R0
|
|
42
|
+
} else {
|
|
43
|
+
price = 1n << PRICE_PRECISION
|
|
44
|
+
}
|
|
45
|
+
if ((absTick & 0x2n) != 0n) {
|
|
46
|
+
price = (price * _R1) >> PRICE_PRECISION
|
|
47
|
+
}
|
|
48
|
+
if ((absTick & 0x4n) != 0n) {
|
|
49
|
+
price = (price * _R2) >> PRICE_PRECISION
|
|
50
|
+
}
|
|
51
|
+
if ((absTick & 0x8n) != 0n) {
|
|
52
|
+
price = (price * _R3) >> PRICE_PRECISION
|
|
53
|
+
}
|
|
54
|
+
if ((absTick & 0x10n) != 0n) {
|
|
55
|
+
price = (price * _R4) >> PRICE_PRECISION
|
|
56
|
+
}
|
|
57
|
+
if ((absTick & 0x20n) != 0n) {
|
|
58
|
+
price = (price * _R5) >> PRICE_PRECISION
|
|
59
|
+
}
|
|
60
|
+
if ((absTick & 0x40n) != 0n) {
|
|
61
|
+
price = (price * _R6) >> PRICE_PRECISION
|
|
62
|
+
}
|
|
63
|
+
if ((absTick & 0x80n) != 0n) {
|
|
64
|
+
price = (price * _R7) >> PRICE_PRECISION
|
|
65
|
+
}
|
|
66
|
+
if ((absTick & 0x100n) != 0n) {
|
|
67
|
+
price = (price * _R8) >> PRICE_PRECISION
|
|
68
|
+
}
|
|
69
|
+
if ((absTick & 0x200n) != 0n) {
|
|
70
|
+
price = (price * _R9) >> PRICE_PRECISION
|
|
71
|
+
}
|
|
72
|
+
if ((absTick & 0x400n) != 0n) {
|
|
73
|
+
price = (price * _R10) >> PRICE_PRECISION
|
|
74
|
+
}
|
|
75
|
+
if ((absTick & 0x800n) != 0n) {
|
|
76
|
+
price = (price * _R11) >> PRICE_PRECISION
|
|
77
|
+
}
|
|
78
|
+
if ((absTick & 0x1000n) != 0n) {
|
|
79
|
+
price = (price * _R12) >> PRICE_PRECISION
|
|
80
|
+
}
|
|
81
|
+
if ((absTick & 0x2000n) != 0n) {
|
|
82
|
+
price = (price * _R13) >> PRICE_PRECISION
|
|
83
|
+
}
|
|
84
|
+
if ((absTick & 0x4000n) != 0n) {
|
|
85
|
+
price = (price * _R14) >> PRICE_PRECISION
|
|
86
|
+
}
|
|
87
|
+
if ((absTick & 0x8000n) != 0n) {
|
|
88
|
+
price = (price * _R15) >> PRICE_PRECISION
|
|
89
|
+
}
|
|
90
|
+
if ((absTick & 0x10000n) != 0n) {
|
|
91
|
+
price = (price * _R16) >> PRICE_PRECISION
|
|
92
|
+
}
|
|
93
|
+
if ((absTick & 0x20000n) != 0n) {
|
|
94
|
+
price = (price * _R17) >> PRICE_PRECISION
|
|
95
|
+
}
|
|
96
|
+
if ((absTick & 0x40000n) != 0n) {
|
|
97
|
+
price = (price * _R18) >> PRICE_PRECISION
|
|
98
|
+
}
|
|
99
|
+
if (tick > 0n) {
|
|
100
|
+
price = 0x1000000000000000000000000000000000000000000000000n / price
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return price
|
|
104
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { createPublicClient, http, isAddressEqual, zeroAddress } from 'viem'
|
|
2
|
+
|
|
3
|
+
import { Currency } from '../model/currency'
|
|
4
|
+
import { CHAIN_IDS, CHAIN_MAP } from '../constants/chain'
|
|
5
|
+
|
|
6
|
+
const _abi = [
|
|
7
|
+
{
|
|
8
|
+
inputs: [],
|
|
9
|
+
name: 'totalSupply',
|
|
10
|
+
outputs: [
|
|
11
|
+
{
|
|
12
|
+
internalType: 'uint256',
|
|
13
|
+
name: '',
|
|
14
|
+
type: 'uint256',
|
|
15
|
+
},
|
|
16
|
+
],
|
|
17
|
+
stateMutability: 'view',
|
|
18
|
+
type: 'function',
|
|
19
|
+
},
|
|
20
|
+
] as const
|
|
21
|
+
|
|
22
|
+
export const calculateUnit = async (
|
|
23
|
+
chainId: CHAIN_IDS,
|
|
24
|
+
quote: Currency,
|
|
25
|
+
rpcUrl?: string,
|
|
26
|
+
) => {
|
|
27
|
+
if (isAddressEqual(quote.address, zeroAddress)) {
|
|
28
|
+
return 10n ** 12n
|
|
29
|
+
}
|
|
30
|
+
const publicClient = createPublicClient({
|
|
31
|
+
chain: CHAIN_MAP[chainId],
|
|
32
|
+
transport: rpcUrl ? http(rpcUrl) : http(),
|
|
33
|
+
})
|
|
34
|
+
const totalSupply = await publicClient.readContract({
|
|
35
|
+
address: quote.address,
|
|
36
|
+
abi: _abi,
|
|
37
|
+
functionName: 'totalSupply',
|
|
38
|
+
})
|
|
39
|
+
return (
|
|
40
|
+
10n **
|
|
41
|
+
BigInt(totalSupply <= 2n ** 64n ? 0n : Math.max(quote.decimals - 6, 0))
|
|
42
|
+
)
|
|
43
|
+
}
|
package/src/view.ts
ADDED
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
import { formatUnits, isAddressEqual, parseUnits } from 'viem'
|
|
2
|
+
|
|
3
|
+
import { fetchMarket } from './apis/market'
|
|
4
|
+
import { CHAIN_IDS } from './constants/chain'
|
|
5
|
+
import { Market } from './type'
|
|
6
|
+
import { parsePrice } from './utils/prices'
|
|
7
|
+
import { MAX_PRICE } from './constants/price'
|
|
8
|
+
import { fetchOpenOrder, fetchOpenOrders } from './apis/open-order'
|
|
9
|
+
import { OpenOrder } from './model/open-order'
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Get market information by chain id and token addresses
|
|
13
|
+
* @param chainId - chain id from {@link CHAIN_IDS}
|
|
14
|
+
* @param token0 - token0 address
|
|
15
|
+
* @param token1 - token1 address
|
|
16
|
+
* @param options
|
|
17
|
+
* @param options.rpcUrl - RPC URL of the blockchain
|
|
18
|
+
* @returns A market {@link Market}
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* import { getMarket } from '@clober/v2-sdk'
|
|
22
|
+
*
|
|
23
|
+
* const market = await getMarket(
|
|
24
|
+
* 421614,
|
|
25
|
+
* '0x00bfd44e79fb7f6dd5887a9426c8ef85a0cd23e0',
|
|
26
|
+
* '0x0000000000000000000000000000000000000000',
|
|
27
|
+
* )
|
|
28
|
+
*/
|
|
29
|
+
export const getMarket = async (
|
|
30
|
+
chainId: CHAIN_IDS,
|
|
31
|
+
token0: `0x${string}`,
|
|
32
|
+
token1: `0x${string}`,
|
|
33
|
+
options?: { rpcUrl?: string },
|
|
34
|
+
): Promise<Market> => {
|
|
35
|
+
if (isAddressEqual(token0, token1)) {
|
|
36
|
+
throw new Error('Token0 and token1 must be different')
|
|
37
|
+
}
|
|
38
|
+
const market = await fetchMarket(chainId, [token0, token1], options?.rpcUrl)
|
|
39
|
+
return {
|
|
40
|
+
chainId,
|
|
41
|
+
quote: market.quote,
|
|
42
|
+
base: market.base,
|
|
43
|
+
makerFee: market.makerFee,
|
|
44
|
+
takerFee: market.takerFee,
|
|
45
|
+
bids: market.bids,
|
|
46
|
+
bidBookOpen: market.bidBookOpen,
|
|
47
|
+
asks: market.asks,
|
|
48
|
+
askBookOpen: market.askBookOpen,
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Calculates the expected output for a given input amount, based on the provided market data.
|
|
54
|
+
*
|
|
55
|
+
* @param chainId The chain ID of the blockchain.
|
|
56
|
+
* @param inputToken The address of the input token.
|
|
57
|
+
* @param outputToken The address of the output token.
|
|
58
|
+
* @param amountIn The amount of expected input amount. (ex 1.2 ETH -> 1.2)
|
|
59
|
+
* @param options
|
|
60
|
+
* @param options.limitPrice The maximum limit price to spend.
|
|
61
|
+
* @param options.rpcUrl The RPC URL of the blockchain.
|
|
62
|
+
* @returns A Promise resolving to an object containing the taken amount, spend amount and result of the calculation.
|
|
63
|
+
* @example
|
|
64
|
+
* import { getExpectedOutput } from '@clober/v2-sdk'
|
|
65
|
+
*
|
|
66
|
+
* const { takenAmount, spendAmount } = await getExpectedOutput(
|
|
67
|
+
* 421614,
|
|
68
|
+
* '0x00bfd44e79fb7f6dd5887a9426c8ef85a0cd23e0',
|
|
69
|
+
* '0x0000000000000000000000000000000000000000',
|
|
70
|
+
* '1000.123', // spend 1000.123 USDC
|
|
71
|
+
* )
|
|
72
|
+
*/
|
|
73
|
+
export const getExpectedOutput = async (
|
|
74
|
+
chainId: CHAIN_IDS,
|
|
75
|
+
inputToken: `0x${string}`,
|
|
76
|
+
outputToken: `0x${string}`,
|
|
77
|
+
amountIn: string,
|
|
78
|
+
options?: { limitPrice?: string; rpcUrl?: string },
|
|
79
|
+
): Promise<{
|
|
80
|
+
takenAmount: string
|
|
81
|
+
spendAmount: string
|
|
82
|
+
result: { bookId: bigint; takenAmount: bigint; spendAmount: bigint }[]
|
|
83
|
+
}> => {
|
|
84
|
+
const market = await fetchMarket(
|
|
85
|
+
chainId,
|
|
86
|
+
[inputToken, outputToken],
|
|
87
|
+
options?.rpcUrl,
|
|
88
|
+
)
|
|
89
|
+
const isBid = isAddressEqual(market.quote.address, inputToken)
|
|
90
|
+
const rawLimitPrice =
|
|
91
|
+
options && options.limitPrice
|
|
92
|
+
? parsePrice(
|
|
93
|
+
Number(options.limitPrice),
|
|
94
|
+
market.quote.decimals,
|
|
95
|
+
market.base.decimals,
|
|
96
|
+
)
|
|
97
|
+
: isBid
|
|
98
|
+
? MAX_PRICE
|
|
99
|
+
: 0n
|
|
100
|
+
const inputCurrency = isBid ? market.quote : market.base
|
|
101
|
+
const result = market.spend({
|
|
102
|
+
spendBase: !isBid,
|
|
103
|
+
limitPrice: rawLimitPrice,
|
|
104
|
+
amountIn: parseUnits(amountIn, inputCurrency.decimals),
|
|
105
|
+
})
|
|
106
|
+
const { takenAmount, spendAmount } = Object.values(result).reduce(
|
|
107
|
+
(acc, { takenAmount, spendAmount }) => ({
|
|
108
|
+
takenAmount: acc.takenAmount + takenAmount,
|
|
109
|
+
spendAmount: acc.spendAmount + spendAmount,
|
|
110
|
+
}),
|
|
111
|
+
{ takenAmount: 0n, spendAmount: 0n },
|
|
112
|
+
)
|
|
113
|
+
return {
|
|
114
|
+
takenAmount: formatUnits(
|
|
115
|
+
takenAmount,
|
|
116
|
+
isBid ? market.base.decimals : market.quote.decimals,
|
|
117
|
+
),
|
|
118
|
+
spendAmount: formatUnits(
|
|
119
|
+
spendAmount,
|
|
120
|
+
isBid ? market.quote.decimals : market.base.decimals,
|
|
121
|
+
),
|
|
122
|
+
result: Object.entries(result).map(
|
|
123
|
+
([bookId, { takenAmount, spendAmount }]) => ({
|
|
124
|
+
bookId: BigInt(bookId),
|
|
125
|
+
takenAmount,
|
|
126
|
+
spendAmount,
|
|
127
|
+
}),
|
|
128
|
+
),
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Calculates the expected input for a given output amount, based on the provided market data.
|
|
134
|
+
*
|
|
135
|
+
* @param chainId The chain ID of the blockchain.
|
|
136
|
+
* @param inputToken The address of the input token.
|
|
137
|
+
* @param outputToken The address of the output token.
|
|
138
|
+
* @param amountOut The amount of expected output amount. (ex 1.2 ETH -> 1.2)
|
|
139
|
+
* @param options
|
|
140
|
+
* @param options.limitPrice The maximum limit price to take.
|
|
141
|
+
* @param options.rpcUrl The RPC URL of the blockchain.
|
|
142
|
+
* @returns A Promise resolving to an object containing the taken amount, spend amount and result of the calculation.
|
|
143
|
+
* @example
|
|
144
|
+
* import { getExpectedInput } from '@clober/v2-sdk'
|
|
145
|
+
*
|
|
146
|
+
* const { takenAmount, spendAmount } = await getExpectedInput(
|
|
147
|
+
* 421614,
|
|
148
|
+
* '0x00bfd44e79fb7f6dd5887a9426c8ef85a0cd23e0',
|
|
149
|
+
* '0x0000000000000000000000000000000000000000',
|
|
150
|
+
* '0.1', // take 0.1 ETH
|
|
151
|
+
* )
|
|
152
|
+
*/
|
|
153
|
+
export const getExpectedInput = async (
|
|
154
|
+
chainId: CHAIN_IDS,
|
|
155
|
+
inputToken: `0x${string}`,
|
|
156
|
+
outputToken: `0x${string}`,
|
|
157
|
+
amountOut: string,
|
|
158
|
+
options?: { limitPrice?: string; rpcUrl?: string },
|
|
159
|
+
): Promise<{
|
|
160
|
+
takenAmount: string
|
|
161
|
+
spendAmount: string
|
|
162
|
+
result: { bookId: bigint; takenAmount: bigint; spendAmount: bigint }[]
|
|
163
|
+
}> => {
|
|
164
|
+
const market = await fetchMarket(
|
|
165
|
+
chainId,
|
|
166
|
+
[inputToken, outputToken],
|
|
167
|
+
options?.rpcUrl,
|
|
168
|
+
)
|
|
169
|
+
const isBid = isAddressEqual(market.quote.address, inputToken)
|
|
170
|
+
const rawLimitPrice =
|
|
171
|
+
options && options.limitPrice
|
|
172
|
+
? parsePrice(
|
|
173
|
+
Number(options.limitPrice),
|
|
174
|
+
market.quote.decimals,
|
|
175
|
+
market.base.decimals,
|
|
176
|
+
)
|
|
177
|
+
: isBid
|
|
178
|
+
? MAX_PRICE
|
|
179
|
+
: 0n
|
|
180
|
+
const outputCurrency = isBid ? market.base : market.quote
|
|
181
|
+
const result = market.take({
|
|
182
|
+
takeQuote: !isBid,
|
|
183
|
+
limitPrice: rawLimitPrice,
|
|
184
|
+
amountOut: parseUnits(amountOut, outputCurrency.decimals),
|
|
185
|
+
})
|
|
186
|
+
const { takenAmount, spendAmount } = Object.values(result).reduce(
|
|
187
|
+
(acc, { takenAmount, spendAmount }) => ({
|
|
188
|
+
takenAmount: acc.takenAmount + takenAmount,
|
|
189
|
+
spendAmount: acc.spendAmount + spendAmount,
|
|
190
|
+
}),
|
|
191
|
+
{ takenAmount: 0n, spendAmount: 0n },
|
|
192
|
+
)
|
|
193
|
+
return {
|
|
194
|
+
takenAmount: formatUnits(
|
|
195
|
+
takenAmount,
|
|
196
|
+
isBid ? market.base.decimals : market.quote.decimals,
|
|
197
|
+
),
|
|
198
|
+
spendAmount: formatUnits(
|
|
199
|
+
spendAmount,
|
|
200
|
+
isBid ? market.quote.decimals : market.base.decimals,
|
|
201
|
+
),
|
|
202
|
+
result: Object.entries(result).map(
|
|
203
|
+
([bookId, { takenAmount, spendAmount }]) => ({
|
|
204
|
+
bookId: BigInt(bookId),
|
|
205
|
+
takenAmount,
|
|
206
|
+
spendAmount,
|
|
207
|
+
}),
|
|
208
|
+
),
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Retrieves the open order with the specified ID on the given chain.
|
|
214
|
+
*
|
|
215
|
+
* @param {CHAIN_IDS} chainId The chain ID.
|
|
216
|
+
* @param {string} id The ID of the open order.
|
|
217
|
+
* @param options
|
|
218
|
+
* @param options.rpcUrl The RPC URL of the blockchain.
|
|
219
|
+
* @returns {Promise<OpenOrder>} Promise resolving to the open order object, or undefined if not found.
|
|
220
|
+
* @example
|
|
221
|
+
* import { getOpenOrder } from '@clober/v2-sdk'
|
|
222
|
+
*
|
|
223
|
+
* const openOrder = await getOpenOrder(
|
|
224
|
+
* 421614,
|
|
225
|
+
* '46223845323662364279893361453861711542636620039907198451770258805035840307200'
|
|
226
|
+
* )
|
|
227
|
+
*/
|
|
228
|
+
export const getOpenOrder = async (
|
|
229
|
+
chainId: CHAIN_IDS,
|
|
230
|
+
id: string,
|
|
231
|
+
options?: { rpcUrl?: string },
|
|
232
|
+
): Promise<OpenOrder> => {
|
|
233
|
+
return fetchOpenOrder(chainId, id, options?.rpcUrl)
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Retrieves open orders for the specified user on the given chain.
|
|
238
|
+
*
|
|
239
|
+
* @param {CHAIN_IDS} chainId The chain ID.
|
|
240
|
+
* @param {`0x${string}`} userAddress The Ethereum address of the user.
|
|
241
|
+
* @param options
|
|
242
|
+
* @param options.rpcUrl The RPC URL of the blockchain.
|
|
243
|
+
* @returns {Promise<OpenOrder[]>} Promise resolving to an array of open orders.
|
|
244
|
+
* @example
|
|
245
|
+
* import { getOpenOrders } from '@clober/v2-sdk'
|
|
246
|
+
*
|
|
247
|
+
* const openOrders = await getOpenOrders(
|
|
248
|
+
* 421614,
|
|
249
|
+
* '0x5F79EE8f8fA862E98201120d83c4eC39D9468D49'
|
|
250
|
+
* )
|
|
251
|
+
*/
|
|
252
|
+
export const getOpenOrders = async (
|
|
253
|
+
chainId: CHAIN_IDS,
|
|
254
|
+
userAddress: `0x${string}`,
|
|
255
|
+
options?: { rpcUrl?: string },
|
|
256
|
+
): Promise<OpenOrder[]> => {
|
|
257
|
+
return fetchOpenOrders(chainId, userAddress, options?.rpcUrl)
|
|
258
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { expect, test } from 'vitest'
|
|
2
|
+
|
|
3
|
+
import { toBookId } from '../src/utils/book-id'
|
|
4
|
+
import {
|
|
5
|
+
MAKER_DEFAULT_POLICY,
|
|
6
|
+
TAKER_DEFAULT_POLICY,
|
|
7
|
+
} from '../src/constants/fee'
|
|
8
|
+
|
|
9
|
+
import { publicClient } from './utils/constants'
|
|
10
|
+
|
|
11
|
+
const _abi = [
|
|
12
|
+
{
|
|
13
|
+
inputs: [
|
|
14
|
+
{
|
|
15
|
+
components: [
|
|
16
|
+
{
|
|
17
|
+
internalType: 'Currency',
|
|
18
|
+
name: 'base',
|
|
19
|
+
type: 'address',
|
|
20
|
+
},
|
|
21
|
+
{
|
|
22
|
+
internalType: 'uint64',
|
|
23
|
+
name: 'unit',
|
|
24
|
+
type: 'uint64',
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
internalType: 'Currency',
|
|
28
|
+
name: 'quote',
|
|
29
|
+
type: 'address',
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
internalType: 'FeePolicy',
|
|
33
|
+
name: 'makerPolicy',
|
|
34
|
+
type: 'uint24',
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
internalType: 'contract IHooks',
|
|
38
|
+
name: 'hooks',
|
|
39
|
+
type: 'address',
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
internalType: 'FeePolicy',
|
|
43
|
+
name: 'takerPolicy',
|
|
44
|
+
type: 'uint24',
|
|
45
|
+
},
|
|
46
|
+
],
|
|
47
|
+
internalType: 'struct IBookManager.BookKey',
|
|
48
|
+
name: 'bookKey',
|
|
49
|
+
type: 'tuple',
|
|
50
|
+
},
|
|
51
|
+
],
|
|
52
|
+
name: 'toId',
|
|
53
|
+
outputs: [
|
|
54
|
+
{
|
|
55
|
+
internalType: 'BookId',
|
|
56
|
+
name: 'id',
|
|
57
|
+
type: 'uint192',
|
|
58
|
+
},
|
|
59
|
+
],
|
|
60
|
+
stateMutability: 'pure',
|
|
61
|
+
type: 'function',
|
|
62
|
+
},
|
|
63
|
+
] as const
|
|
64
|
+
|
|
65
|
+
const BOOK_ID_WRAPPER_ADDRESS = '0xbBa11dC70D31578fA426FB0EaCed79EB844F93A7'
|
|
66
|
+
|
|
67
|
+
test('check toBookId function', async () => {
|
|
68
|
+
const bidBookKey = {
|
|
69
|
+
base: '0x0000000000000000000000000000000000000000' as `0x${string}`,
|
|
70
|
+
unit: 10n ** 12n,
|
|
71
|
+
quote: '0x447ad4a108b5540c220f9f7e83723ac87c0f8fd8' as `0x${string}`,
|
|
72
|
+
makerPolicy: Number(MAKER_DEFAULT_POLICY.value),
|
|
73
|
+
hooks: '0x0000000000000000000000000000000000000000' as `0x${string}`,
|
|
74
|
+
takerPolicy: Number(TAKER_DEFAULT_POLICY.value),
|
|
75
|
+
}
|
|
76
|
+
expect(
|
|
77
|
+
await publicClient.readContract({
|
|
78
|
+
address: BOOK_ID_WRAPPER_ADDRESS,
|
|
79
|
+
abi: _abi,
|
|
80
|
+
functionName: 'toId',
|
|
81
|
+
args: [bidBookKey],
|
|
82
|
+
}),
|
|
83
|
+
).toBe(toBookId(bidBookKey.quote, bidBookKey.base, bidBookKey.unit))
|
|
84
|
+
|
|
85
|
+
const askBookKey = {
|
|
86
|
+
base: '0x447ad4a108b5540c220f9f7e83723ac87c0f8fd8' as `0x${string}`,
|
|
87
|
+
unit: 10n ** 1n,
|
|
88
|
+
quote: '0x0000000000000000000000000000000000000000' as `0x${string}`,
|
|
89
|
+
makerPolicy: Number(MAKER_DEFAULT_POLICY.value),
|
|
90
|
+
hooks: '0x0000000000000000000000000000000000000000' as `0x${string}`,
|
|
91
|
+
takerPolicy: Number(TAKER_DEFAULT_POLICY.value),
|
|
92
|
+
}
|
|
93
|
+
expect(
|
|
94
|
+
await publicClient.readContract({
|
|
95
|
+
address: BOOK_ID_WRAPPER_ADDRESS,
|
|
96
|
+
abi: _abi,
|
|
97
|
+
functionName: 'toId',
|
|
98
|
+
args: [askBookKey],
|
|
99
|
+
}),
|
|
100
|
+
).toBe(toBookId(askBookKey.quote, askBookKey.base, askBookKey.unit))
|
|
101
|
+
})
|