@clober/v2-sdk 0.0.1-b → 0.0.1-d
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/dist/cjs/.graphclient/index.js +243 -0
- package/dist/cjs/.graphclient/index.js.map +1 -0
- package/dist/cjs/.graphclient/sources/clober-v2/introspectionSchema.js +14482 -0
- package/dist/cjs/.graphclient/sources/clober-v2/introspectionSchema.js.map +1 -0
- package/dist/cjs/.graphclient/sources/clober-v2/types.js +4 -0
- package/dist/cjs/.graphclient/sources/clober-v2/types.js.map +1 -0
- package/dist/cjs/abis/core/controller-abi.js +989 -0
- package/dist/cjs/abis/core/controller-abi.js.map +1 -0
- package/dist/cjs/abis/core/params-abi.js +62 -0
- package/dist/cjs/abis/core/params-abi.js.map +1 -0
- package/dist/cjs/apis/currency.js +87 -0
- package/dist/cjs/apis/currency.js.map +1 -0
- package/dist/cjs/apis/market.js +63 -0
- package/dist/cjs/apis/market.js.map +1 -0
- package/dist/cjs/apis/open-order.js +90 -0
- package/dist/cjs/apis/open-order.js.map +1 -0
- package/dist/cjs/approval.js +74 -0
- package/dist/cjs/approval.js.map +1 -0
- package/dist/cjs/call.js +545 -0
- package/dist/cjs/call.js.map +1 -0
- package/dist/cjs/constants/action.js +14 -0
- package/dist/cjs/constants/action.js.map +1 -0
- package/dist/cjs/constants/addresses.js +13 -0
- package/dist/cjs/constants/addresses.js.map +1 -0
- package/dist/cjs/constants/chain.js +13 -0
- package/dist/cjs/constants/chain.js.map +1 -0
- package/dist/cjs/constants/currency.js +12 -0
- package/dist/cjs/constants/currency.js.map +1 -0
- package/dist/cjs/constants/fee.js +7 -0
- package/dist/cjs/constants/fee.js.map +1 -0
- package/dist/cjs/constants/price.js +6 -0
- package/dist/cjs/constants/price.js.map +1 -0
- package/dist/cjs/constants/subgraph-url.js +8 -0
- package/dist/cjs/constants/subgraph-url.js.map +1 -0
- package/dist/cjs/index.js +22 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/model/book.js +130 -0
- package/dist/cjs/model/book.js.map +1 -0
- package/dist/cjs/model/currency.js +3 -0
- package/dist/cjs/model/currency.js.map +1 -0
- package/dist/cjs/model/depth.js +3 -0
- package/dist/cjs/model/depth.js.map +1 -0
- package/dist/cjs/model/fee-policy.js +42 -0
- package/dist/cjs/model/fee-policy.js.map +1 -0
- package/dist/cjs/model/market.js +215 -0
- package/dist/cjs/model/market.js.map +1 -0
- package/dist/cjs/model/open-order.js +3 -0
- package/dist/cjs/model/open-order.js.map +1 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/signature.js +178 -0
- package/dist/cjs/signature.js.map +1 -0
- package/dist/cjs/tsconfig.build.tsbuildinfo +1 -0
- package/dist/cjs/type.js +6 -0
- package/dist/cjs/type.js.map +1 -0
- package/dist/cjs/utils/approval.js +46 -0
- package/dist/cjs/utils/approval.js.map +1 -0
- package/dist/cjs/utils/book-id.js +25 -0
- package/dist/cjs/utils/book-id.js.map +1 -0
- package/dist/cjs/utils/build-transaction.js +31 -0
- package/dist/cjs/utils/build-transaction.js.map +1 -0
- package/dist/cjs/utils/decimals.js +17 -0
- package/dist/cjs/utils/decimals.js.map +1 -0
- package/dist/cjs/utils/market.js +53 -0
- package/dist/cjs/utils/market.js.map +1 -0
- package/dist/cjs/utils/math.js +83 -0
- package/dist/cjs/utils/math.js.map +1 -0
- package/dist/cjs/utils/prices.js +22 -0
- package/dist/cjs/utils/prices.js.map +1 -0
- package/dist/cjs/utils/tick.js +106 -0
- package/dist/cjs/utils/tick.js.map +1 -0
- package/dist/cjs/utils/time.js +9 -0
- package/dist/cjs/utils/time.js.map +1 -0
- package/dist/cjs/utils/unit.js +38 -0
- package/dist/cjs/utils/unit.js.map +1 -0
- package/dist/cjs/view.js +185 -0
- package/dist/cjs/view.js.map +1 -0
- package/dist/esm/.graphclient/index.js +233 -0
- package/dist/esm/.graphclient/index.js.map +1 -0
- package/dist/esm/.graphclient/sources/clober-v2/introspectionSchema.js +14480 -0
- package/dist/esm/.graphclient/sources/clober-v2/introspectionSchema.js.map +1 -0
- package/dist/esm/.graphclient/sources/clober-v2/types.js +3 -0
- package/dist/esm/.graphclient/sources/clober-v2/types.js.map +1 -0
- package/dist/esm/abis/core/controller-abi.js +986 -0
- package/dist/esm/abis/core/controller-abi.js.map +1 -0
- package/dist/esm/abis/core/params-abi.js +59 -0
- package/dist/esm/abis/core/params-abi.js.map +1 -0
- package/dist/esm/apis/currency.js +83 -0
- package/dist/esm/apis/currency.js.map +1 -0
- package/dist/esm/apis/market.js +59 -0
- package/dist/esm/apis/market.js.map +1 -0
- package/dist/esm/apis/open-order.js +85 -0
- package/dist/esm/apis/open-order.js.map +1 -0
- package/dist/esm/approval.js +70 -0
- package/dist/esm/approval.js.map +1 -0
- package/dist/esm/call.js +535 -0
- package/dist/esm/call.js.map +1 -0
- package/dist/esm/constants/action.js +11 -0
- package/dist/esm/constants/action.js.map +1 -0
- package/dist/esm/constants/addresses.js +10 -0
- package/dist/esm/constants/addresses.js.map +1 -0
- package/dist/esm/constants/chain.js +10 -0
- package/dist/esm/constants/chain.js.map +1 -0
- package/dist/esm/constants/currency.js +9 -0
- package/dist/esm/constants/currency.js.map +1 -0
- package/dist/esm/constants/fee.js +4 -0
- package/dist/esm/constants/fee.js.map +1 -0
- package/dist/esm/constants/price.js +3 -0
- package/dist/esm/constants/price.js.map +1 -0
- package/dist/esm/constants/subgraph-url.js +5 -0
- package/dist/esm/constants/subgraph-url.js.map +1 -0
- package/dist/esm/index.js +6 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/model/book.js +126 -0
- package/dist/esm/model/book.js.map +1 -0
- package/dist/esm/model/currency.js +2 -0
- package/dist/esm/model/currency.js.map +1 -0
- package/dist/esm/model/depth.js +2 -0
- package/dist/esm/model/depth.js.map +1 -0
- package/dist/esm/model/fee-policy.js +38 -0
- package/dist/esm/model/fee-policy.js.map +1 -0
- package/dist/esm/model/market.js +211 -0
- package/dist/esm/model/market.js.map +1 -0
- package/dist/esm/model/open-order.js +2 -0
- package/dist/esm/model/open-order.js.map +1 -0
- package/dist/esm/package.json +1 -0
- package/dist/esm/signature.js +174 -0
- package/dist/esm/signature.js.map +1 -0
- package/dist/esm/tsconfig.build.tsbuildinfo +1 -0
- package/dist/esm/type.js +2 -0
- package/dist/esm/type.js.map +1 -0
- package/dist/esm/utils/approval.js +42 -0
- package/dist/esm/utils/approval.js.map +1 -0
- package/{src/utils/book-id.ts → dist/esm/utils/book-id.js} +9 -19
- package/dist/esm/utils/book-id.js.map +1 -0
- package/dist/esm/utils/build-transaction.js +27 -0
- package/dist/esm/utils/build-transaction.js.map +1 -0
- package/dist/esm/utils/decimals.js +12 -0
- package/dist/esm/utils/decimals.js.map +1 -0
- package/dist/esm/utils/market.js +49 -0
- package/dist/esm/utils/market.js.map +1 -0
- package/dist/esm/utils/math.js +78 -0
- package/dist/esm/utils/math.js.map +1 -0
- package/dist/esm/utils/prices.js +17 -0
- package/dist/esm/utils/prices.js.map +1 -0
- package/dist/esm/utils/tick.js +100 -0
- package/dist/esm/utils/tick.js.map +1 -0
- package/dist/esm/utils/time.js +5 -0
- package/dist/esm/utils/time.js.map +1 -0
- package/dist/esm/utils/unit.js +34 -0
- package/dist/esm/utils/unit.js.map +1 -0
- package/dist/esm/view.js +177 -0
- package/dist/esm/view.js.map +1 -0
- package/dist/types/.graphclient/index.d.ts +1281 -0
- package/dist/types/.graphclient/index.d.ts.map +1 -0
- package/dist/types/.graphclient/sources/clober-v2/introspectionSchema.d.ts +3 -0
- package/dist/types/.graphclient/sources/clober-v2/introspectionSchema.d.ts.map +1 -0
- package/dist/types/.graphclient/sources/clober-v2/types.d.ts +984 -0
- package/dist/types/.graphclient/sources/clober-v2/types.d.ts.map +1 -0
- package/dist/types/abis/core/controller-abi.d.ts +757 -0
- package/dist/types/abis/core/controller-abi.d.ts.map +1 -0
- package/dist/types/abis/core/params-abi.d.ts +21 -0
- package/dist/types/abis/core/params-abi.d.ts.map +1 -0
- package/dist/types/apis/currency.d.ts +4 -0
- package/dist/types/apis/currency.d.ts.map +1 -0
- package/dist/types/apis/market.d.ts +4 -0
- package/dist/types/apis/market.d.ts.map +1 -0
- package/dist/types/apis/open-order.d.ts +5 -0
- package/dist/types/apis/open-order.d.ts.map +1 -0
- package/dist/types/approval.d.ts +32 -0
- package/dist/types/approval.d.ts.map +1 -0
- package/dist/types/call.d.ts +234 -0
- package/dist/types/call.d.ts.map +1 -0
- package/dist/types/constants/action.d.ts +10 -0
- package/dist/types/constants/action.d.ts.map +1 -0
- package/dist/types/constants/addresses.d.ts +9 -0
- package/dist/types/constants/addresses.d.ts.map +1 -0
- package/dist/types/constants/chain.d.ts +9 -0
- package/dist/types/constants/chain.d.ts.map +1 -0
- package/dist/types/constants/currency.d.ts +8 -0
- package/dist/types/constants/currency.d.ts.map +1 -0
- package/dist/types/constants/fee.d.ts +4 -0
- package/dist/types/constants/fee.d.ts.map +1 -0
- package/dist/types/constants/price.d.ts +3 -0
- package/dist/types/constants/price.d.ts.map +1 -0
- package/dist/types/constants/subgraph-url.d.ts +5 -0
- package/dist/types/constants/subgraph-url.d.ts.map +1 -0
- package/dist/types/index.d.ts +6 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/model/book.d.ts +31 -0
- package/dist/types/model/book.d.ts.map +1 -0
- package/dist/types/model/currency.d.ts +7 -0
- package/dist/types/model/currency.d.ts.map +1 -0
- package/dist/types/model/depth.d.ts +11 -0
- package/dist/types/model/depth.d.ts.map +1 -0
- package/dist/types/model/fee-policy.d.ts +15 -0
- package/dist/types/model/fee-policy.d.ts.map +1 -0
- package/dist/types/model/market.d.ts +44 -0
- package/dist/types/model/market.d.ts.map +1 -0
- package/dist/types/model/open-order.d.ts +28 -0
- package/dist/types/model/open-order.d.ts.map +1 -0
- package/dist/types/signature.d.ts +40 -0
- package/dist/types/signature.d.ts.map +1 -0
- package/dist/types/type.d.ts +34 -0
- package/dist/types/type.d.ts.map +1 -0
- package/dist/types/utils/approval.d.ts +3 -0
- package/dist/types/utils/approval.d.ts.map +1 -0
- package/dist/types/utils/book-id.d.ts +2 -0
- package/dist/types/utils/book-id.d.ts.map +1 -0
- package/dist/types/utils/build-transaction.d.ts +5 -0
- package/dist/types/utils/build-transaction.d.ts.map +1 -0
- package/dist/types/utils/decimals.d.ts +3 -0
- package/dist/types/utils/decimals.d.ts.map +1 -0
- package/dist/types/utils/market.d.ts +7 -0
- package/dist/types/utils/market.d.ts.map +1 -0
- package/dist/types/utils/math.d.ts +3 -0
- package/dist/types/utils/math.d.ts.map +1 -0
- package/dist/types/utils/prices.d.ts +3 -0
- package/dist/types/utils/prices.d.ts.map +1 -0
- package/dist/types/utils/tick.d.ts +4 -0
- package/dist/types/utils/tick.d.ts.map +1 -0
- package/dist/types/utils/time.d.ts +2 -0
- package/dist/types/utils/time.d.ts.map +1 -0
- package/dist/types/utils/unit.d.ts +4 -0
- package/dist/types/utils/unit.d.ts.map +1 -0
- package/dist/types/view.d.ts +129 -0
- package/dist/types/view.d.ts.map +1 -0
- package/package.json +14 -6
- package/.eslintignore +0 -1
- package/.eslintrc.yaml +0 -34
- package/.github/workflows/ci.yaml +0 -88
- package/.github/workflows/deployer.yaml +0 -19
- package/.nvmrc +0 -1
- package/.prettierignore +0 -6
- package/.prettierrc +0 -7
- package/src/.graphclient/index.ts +0 -1695
- package/src/.graphclient/schema.graphql +0 -1116
- package/src/.graphclient/sources/clober-v2/introspectionSchema.ts +0 -14481
- package/src/.graphclient/sources/clober-v2/schema.graphql +0 -1116
- package/src/.graphclient/sources/clober-v2/types.ts +0 -1133
- package/src/.graphclientrc.yml +0 -12
- package/src/abis/core/controller-abi.ts +0 -985
- package/src/abis/core/params-abi.ts +0 -59
- package/src/apis/currency.ts +0 -92
- package/src/apis/graphql/books.graphql +0 -25
- package/src/apis/graphql/open-order.graphql +0 -29
- package/src/apis/graphql/open-orders.graphql +0 -29
- package/src/apis/market.ts +0 -85
- package/src/apis/open-order.ts +0 -145
- package/src/approval.ts +0 -82
- package/src/call.ts +0 -705
- package/src/constants/action.ts +0 -9
- package/src/constants/addresses.ts +0 -17
- package/src/constants/chain.ts +0 -12
- package/src/constants/currency.ts +0 -15
- package/src/constants/fee.ts +0 -4
- package/src/constants/price.ts +0 -3
- package/src/constants/subgraph-url.ts +0 -8
- package/src/index.ts +0 -5
- package/src/model/book.ts +0 -166
- package/src/model/currency.ts +0 -6
- package/src/model/depth.ts +0 -11
- package/src/model/fee-policy.ts +0 -51
- package/src/model/market.ts +0 -320
- package/src/model/open-order.ts +0 -16
- package/src/signature.ts +0 -196
- package/src/type.ts +0 -38
- package/src/utils/approval.ts +0 -48
- package/src/utils/build-transaction.ts +0 -39
- package/src/utils/decimals.ts +0 -22
- package/src/utils/market.ts +0 -74
- package/src/utils/math.ts +0 -117
- package/src/utils/prices.ts +0 -29
- package/src/utils/tick.ts +0 -104
- package/src/utils/time.ts +0 -6
- package/src/utils/unit.ts +0 -43
- package/src/view.ts +0 -258
- package/test/book-id.test.ts +0 -101
- package/test/fee-policy.test.ts +0 -228
- package/test/get-expected-input.test.ts +0 -206
- package/test/get-expected-output.test.ts +0 -206
- package/test/limit-order.test.ts +0 -279
- package/test/market-order.test.ts +0 -245
- package/test/market.test.ts +0 -68
- package/test/math.test.ts +0 -91
- package/test/open-order.test.ts +0 -112
- package/test/open.test.ts +0 -15
- package/test/tick.test.ts +0 -230
- package/test/tsconfig.json +0 -12
- package/test/utils/chain.ts +0 -12
- package/test/utils/constants.ts +0 -25
- package/test/utils/currency.ts +0 -44
- package/test/utils/depth.ts +0 -148
- package/test/utils/test-chain.ts +0 -26
- package/test/vitest.config.ts +0 -15
- package/tsconfig.base.json +0 -37
- package/tsconfig.build.json +0 -9
- package/tsconfig.json +0 -9
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { getAddress } from 'viem'
|
|
2
|
-
|
|
3
|
-
import { CHAIN_IDS } from './chain'
|
|
4
|
-
|
|
5
|
-
export const CONTRACT_ADDRESSES: {
|
|
6
|
-
[chain in CHAIN_IDS]: {
|
|
7
|
-
Controller: `0x${string}`
|
|
8
|
-
BookManager: `0x${string}`
|
|
9
|
-
BookViewer: `0x${string}`
|
|
10
|
-
}
|
|
11
|
-
} = {
|
|
12
|
-
[CHAIN_IDS.ARBITRUM_SEPOLIA]: {
|
|
13
|
-
Controller: getAddress('0xfAe4A04fa491DC21F77796394532a1B62d8331BF'),
|
|
14
|
-
BookManager: getAddress('0x4a4eaF7382821da4Fb85e8A8d515f5555383d58A'),
|
|
15
|
-
BookViewer: getAddress('0xA7603C4c895a533E66c30EA76cC6F6A6A0c5cbFe'),
|
|
16
|
-
},
|
|
17
|
-
}
|
package/src/constants/chain.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { arbitrumSepolia, type Chain, mainnet } from 'viem/chains'
|
|
2
|
-
|
|
3
|
-
export enum CHAIN_IDS {
|
|
4
|
-
MAINNET = mainnet.id,
|
|
5
|
-
ARBITRUM_SEPOLIA = arbitrumSepolia.id,
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
export const CHAIN_MAP: {
|
|
9
|
-
[chain in CHAIN_IDS]: Chain
|
|
10
|
-
} = {
|
|
11
|
-
[CHAIN_IDS.ARBITRUM_SEPOLIA]: arbitrumSepolia,
|
|
12
|
-
}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { zeroAddress } from 'viem'
|
|
2
|
-
|
|
3
|
-
import { CHAIN_IDS } from './chain'
|
|
4
|
-
|
|
5
|
-
export const WETH_ADDRESSES: {
|
|
6
|
-
[chain in CHAIN_IDS]: `0x${string}`[]
|
|
7
|
-
} = {
|
|
8
|
-
[CHAIN_IDS.ARBITRUM_SEPOLIA]: [zeroAddress],
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export const STABLE_COIN_ADDRESSES: {
|
|
12
|
-
[chain in CHAIN_IDS]: `0x${string}`[]
|
|
13
|
-
} = {
|
|
14
|
-
[CHAIN_IDS.ARBITRUM_SEPOLIA]: ['0x00BFD44e79FB7f6dd5887A9426c8EF85A0CD23e0'],
|
|
15
|
-
}
|
package/src/constants/fee.ts
DELETED
package/src/constants/price.ts
DELETED
package/src/index.ts
DELETED
package/src/model/book.ts
DELETED
|
@@ -1,166 +0,0 @@
|
|
|
1
|
-
import { toPrice } from '../utils/tick'
|
|
2
|
-
import { TAKER_DEFAULT_POLICY } from '../constants/fee'
|
|
3
|
-
import { divide } from '../utils/math'
|
|
4
|
-
import { baseToQuote, quoteToBase } from '../utils/decimals'
|
|
5
|
-
|
|
6
|
-
import type { Currency } from './currency'
|
|
7
|
-
import type { RawDepth } from './depth'
|
|
8
|
-
|
|
9
|
-
export class Book {
|
|
10
|
-
id: bigint
|
|
11
|
-
base: Currency
|
|
12
|
-
unit: bigint
|
|
13
|
-
quote: Currency
|
|
14
|
-
depths: RawDepth[]
|
|
15
|
-
|
|
16
|
-
constructor({
|
|
17
|
-
id,
|
|
18
|
-
base,
|
|
19
|
-
quote,
|
|
20
|
-
unit,
|
|
21
|
-
depths,
|
|
22
|
-
}: {
|
|
23
|
-
id: bigint
|
|
24
|
-
base: Currency
|
|
25
|
-
quote: Currency
|
|
26
|
-
unit: bigint
|
|
27
|
-
depths: RawDepth[]
|
|
28
|
-
}) {
|
|
29
|
-
this.id = id
|
|
30
|
-
this.base = base
|
|
31
|
-
this.unit = unit
|
|
32
|
-
this.quote = quote
|
|
33
|
-
this.depths = depths
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
take = ({
|
|
37
|
-
limitPrice,
|
|
38
|
-
amountOut, // quote
|
|
39
|
-
}: {
|
|
40
|
-
limitPrice: bigint
|
|
41
|
-
amountOut: bigint
|
|
42
|
-
}) => {
|
|
43
|
-
let takenQuoteAmount = 0n
|
|
44
|
-
let spendBaseAmount = 0n
|
|
45
|
-
if (this.depths.length === 0) {
|
|
46
|
-
return {
|
|
47
|
-
takenQuoteAmount,
|
|
48
|
-
spendBaseAmount,
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
const ticks = this.depths
|
|
53
|
-
.sort((a, b) => Number(b.tick) - Number(a.tick))
|
|
54
|
-
.map((depth) => depth.tick)
|
|
55
|
-
let index = 0
|
|
56
|
-
let tick = ticks[index]!
|
|
57
|
-
while (tick > -8388608n) {
|
|
58
|
-
if (limitPrice > toPrice(tick)) {
|
|
59
|
-
break
|
|
60
|
-
}
|
|
61
|
-
let maxAmount = TAKER_DEFAULT_POLICY.usesQuote
|
|
62
|
-
? TAKER_DEFAULT_POLICY.calculateOriginalAmount(
|
|
63
|
-
amountOut - takenQuoteAmount,
|
|
64
|
-
true,
|
|
65
|
-
)
|
|
66
|
-
: amountOut - takenQuoteAmount
|
|
67
|
-
maxAmount = divide(maxAmount, this.unit, true)
|
|
68
|
-
|
|
69
|
-
if (maxAmount === 0n) {
|
|
70
|
-
break
|
|
71
|
-
}
|
|
72
|
-
const currentDepth = this.depths.find((depth) => depth.tick === tick)!
|
|
73
|
-
let quoteAmount =
|
|
74
|
-
(currentDepth.rawAmount > maxAmount
|
|
75
|
-
? maxAmount
|
|
76
|
-
: currentDepth.rawAmount) * this.unit
|
|
77
|
-
let baseAmount = quoteToBase(tick, quoteAmount, true)
|
|
78
|
-
if (TAKER_DEFAULT_POLICY.usesQuote) {
|
|
79
|
-
quoteAmount =
|
|
80
|
-
quoteAmount - TAKER_DEFAULT_POLICY.calculateFee(quoteAmount, false)
|
|
81
|
-
} else {
|
|
82
|
-
baseAmount =
|
|
83
|
-
baseAmount + TAKER_DEFAULT_POLICY.calculateFee(baseAmount, false)
|
|
84
|
-
}
|
|
85
|
-
if (quoteAmount === 0n) {
|
|
86
|
-
break
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
takenQuoteAmount += quoteAmount
|
|
90
|
-
spendBaseAmount += baseAmount
|
|
91
|
-
if (amountOut <= takenQuoteAmount) {
|
|
92
|
-
break
|
|
93
|
-
}
|
|
94
|
-
index++
|
|
95
|
-
tick = ticks[index]!
|
|
96
|
-
}
|
|
97
|
-
return {
|
|
98
|
-
takenQuoteAmount,
|
|
99
|
-
spendBaseAmount,
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
spend = ({
|
|
104
|
-
limitPrice,
|
|
105
|
-
amountIn, // base
|
|
106
|
-
}: {
|
|
107
|
-
limitPrice: bigint
|
|
108
|
-
amountIn: bigint
|
|
109
|
-
}) => {
|
|
110
|
-
let takenQuoteAmount = 0n
|
|
111
|
-
let spendBaseAmount = 0n
|
|
112
|
-
if (this.depths.length === 0) {
|
|
113
|
-
return {
|
|
114
|
-
takenQuoteAmount,
|
|
115
|
-
spendBaseAmount,
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
const ticks = this.depths
|
|
120
|
-
.sort((a, b) => Number(b.tick) - Number(a.tick))
|
|
121
|
-
.map((depth) => depth.tick)
|
|
122
|
-
let index = 0
|
|
123
|
-
let tick = ticks[index]!
|
|
124
|
-
while (spendBaseAmount <= amountIn && tick > -8388608n) {
|
|
125
|
-
if (limitPrice > toPrice(tick)) {
|
|
126
|
-
break
|
|
127
|
-
}
|
|
128
|
-
let maxAmount = TAKER_DEFAULT_POLICY.usesQuote
|
|
129
|
-
? amountIn - spendBaseAmount
|
|
130
|
-
: TAKER_DEFAULT_POLICY.calculateOriginalAmount(
|
|
131
|
-
amountIn - spendBaseAmount,
|
|
132
|
-
false,
|
|
133
|
-
)
|
|
134
|
-
maxAmount = baseToQuote(tick, maxAmount, false) / this.unit
|
|
135
|
-
|
|
136
|
-
if (maxAmount === 0n) {
|
|
137
|
-
break
|
|
138
|
-
}
|
|
139
|
-
const currentDepth = this.depths.find((depth) => depth.tick === tick)!
|
|
140
|
-
let quoteAmount =
|
|
141
|
-
(currentDepth.rawAmount > maxAmount
|
|
142
|
-
? maxAmount
|
|
143
|
-
: currentDepth.rawAmount) * this.unit
|
|
144
|
-
let baseAmount = quoteToBase(tick, quoteAmount, true)
|
|
145
|
-
if (TAKER_DEFAULT_POLICY.usesQuote) {
|
|
146
|
-
quoteAmount =
|
|
147
|
-
quoteAmount - TAKER_DEFAULT_POLICY.calculateFee(quoteAmount, false)
|
|
148
|
-
} else {
|
|
149
|
-
baseAmount =
|
|
150
|
-
baseAmount + TAKER_DEFAULT_POLICY.calculateFee(baseAmount, false)
|
|
151
|
-
}
|
|
152
|
-
if (baseAmount === 0n) {
|
|
153
|
-
break
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
takenQuoteAmount += quoteAmount
|
|
157
|
-
spendBaseAmount += baseAmount
|
|
158
|
-
index++
|
|
159
|
-
tick = ticks[index]!
|
|
160
|
-
}
|
|
161
|
-
return {
|
|
162
|
-
takenQuoteAmount,
|
|
163
|
-
spendBaseAmount,
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
}
|
package/src/model/currency.ts
DELETED
package/src/model/depth.ts
DELETED
package/src/model/fee-policy.ts
DELETED
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import { divide } from '../utils/math'
|
|
2
|
-
|
|
3
|
-
export class FeePolicy {
|
|
4
|
-
static readonly MAX_FEE_RATE = 500000n
|
|
5
|
-
static readonly MIN_FEE_RATE = -500000n
|
|
6
|
-
static readonly RATE_MASK = 0x7fffffn
|
|
7
|
-
readonly RATE_PRECISION = 10n ** 6n
|
|
8
|
-
public usesQuote: boolean
|
|
9
|
-
public value: bigint
|
|
10
|
-
public rate: bigint
|
|
11
|
-
|
|
12
|
-
constructor(usesQuote: boolean, rate: bigint) {
|
|
13
|
-
this.usesQuote = usesQuote
|
|
14
|
-
this.rate = rate
|
|
15
|
-
|
|
16
|
-
if (rate > FeePolicy.MAX_FEE_RATE || rate < FeePolicy.MIN_FEE_RATE) {
|
|
17
|
-
throw new Error('InvalidFeePolicy')
|
|
18
|
-
}
|
|
19
|
-
const mask = usesQuote ? 1n << 23n : 0n
|
|
20
|
-
this.value = (rate + FeePolicy.MAX_FEE_RATE) | mask
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
public static from(value: bigint): FeePolicy {
|
|
24
|
-
const usesQuote = value >> 23n > 0n
|
|
25
|
-
return new FeePolicy(usesQuote, FeePolicy.getRateFromValue(value))
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
private static getRateFromValue = (value: bigint): bigint => {
|
|
29
|
-
return (value & FeePolicy.RATE_MASK) - FeePolicy.MAX_FEE_RATE
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
public calculateFee = (amount: bigint, reverseRounding: boolean): bigint => {
|
|
33
|
-
const positive = this.rate > 0n
|
|
34
|
-
const absRate = positive ? this.rate : -this.rate
|
|
35
|
-
const absFee = divide(
|
|
36
|
-
amount * absRate,
|
|
37
|
-
this.RATE_PRECISION,
|
|
38
|
-
reverseRounding ? !positive : positive,
|
|
39
|
-
)
|
|
40
|
-
return positive ? absFee : -absFee
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
public calculateOriginalAmount = (
|
|
44
|
-
amount: bigint,
|
|
45
|
-
reverseFee: boolean,
|
|
46
|
-
): bigint => {
|
|
47
|
-
const positive = this.rate > 0
|
|
48
|
-
const divider = this.RATE_PRECISION + (reverseFee ? -this.rate : this.rate)
|
|
49
|
-
return divide(amount * this.RATE_PRECISION, divider, positive)
|
|
50
|
-
}
|
|
51
|
-
}
|
package/src/model/market.ts
DELETED
|
@@ -1,320 +0,0 @@
|
|
|
1
|
-
import { isAddressEqual } from 'viem'
|
|
2
|
-
|
|
3
|
-
import { getMarketId } from '../utils/market'
|
|
4
|
-
import { CHAIN_IDS } from '../constants/chain'
|
|
5
|
-
import { invertPrice, toPrice } from '../utils/tick'
|
|
6
|
-
import { formatPrice } from '../utils/prices'
|
|
7
|
-
import { MAKER_DEFAULT_POLICY, TAKER_DEFAULT_POLICY } from '../constants/fee'
|
|
8
|
-
import { divide } from '../utils/math'
|
|
9
|
-
import { baseToQuote, quoteToBase } from '../utils/decimals'
|
|
10
|
-
|
|
11
|
-
import { Book } from './book'
|
|
12
|
-
import type { Currency } from './currency'
|
|
13
|
-
import type { Depth, RawDepth } from './depth'
|
|
14
|
-
|
|
15
|
-
export class Market {
|
|
16
|
-
readonly makerFee = (Number(MAKER_DEFAULT_POLICY.rate) * 100) / 1e6
|
|
17
|
-
readonly takerFee = (Number(TAKER_DEFAULT_POLICY.rate) * 100) / 1e6
|
|
18
|
-
|
|
19
|
-
id: string
|
|
20
|
-
quote: Currency
|
|
21
|
-
base: Currency
|
|
22
|
-
bids: Depth[]
|
|
23
|
-
bidBookOpen: boolean
|
|
24
|
-
asks: Depth[]
|
|
25
|
-
askBookOpen: boolean
|
|
26
|
-
private books: Book[]
|
|
27
|
-
|
|
28
|
-
constructor({
|
|
29
|
-
chainId,
|
|
30
|
-
tokens,
|
|
31
|
-
books,
|
|
32
|
-
}: {
|
|
33
|
-
chainId: CHAIN_IDS
|
|
34
|
-
tokens: [Currency, Currency]
|
|
35
|
-
books: Book[]
|
|
36
|
-
}) {
|
|
37
|
-
const { marketId, quoteTokenAddress, baseTokenAddress } = getMarketId(
|
|
38
|
-
chainId,
|
|
39
|
-
tokens.map((token) => token.address),
|
|
40
|
-
)
|
|
41
|
-
this.id = marketId
|
|
42
|
-
this.quote = tokens.find((token) =>
|
|
43
|
-
isAddressEqual(token.address, quoteTokenAddress!),
|
|
44
|
-
)!
|
|
45
|
-
this.base = tokens.find((token) =>
|
|
46
|
-
isAddressEqual(token.address, baseTokenAddress!),
|
|
47
|
-
)!
|
|
48
|
-
|
|
49
|
-
this.bids = books
|
|
50
|
-
.filter((book) => isAddressEqual(book.quote.address, this.quote.address))
|
|
51
|
-
.flatMap((book) => book.depths)
|
|
52
|
-
.map(
|
|
53
|
-
(depth) =>
|
|
54
|
-
({
|
|
55
|
-
price: formatPrice(
|
|
56
|
-
toPrice(depth.tick),
|
|
57
|
-
this.quote.decimals,
|
|
58
|
-
this.base.decimals,
|
|
59
|
-
),
|
|
60
|
-
baseAmount: quoteToBase(
|
|
61
|
-
depth.tick,
|
|
62
|
-
depth.rawAmount * depth.unit,
|
|
63
|
-
false,
|
|
64
|
-
),
|
|
65
|
-
}) as Depth,
|
|
66
|
-
)
|
|
67
|
-
this.bidBookOpen =
|
|
68
|
-
books.filter((book) =>
|
|
69
|
-
isAddressEqual(book.quote.address, this.quote.address),
|
|
70
|
-
).length > 0
|
|
71
|
-
this.asks = books
|
|
72
|
-
.filter((book) => isAddressEqual(book.quote.address, this.base.address))
|
|
73
|
-
.flatMap((book) => book.depths)
|
|
74
|
-
.map((depth) => {
|
|
75
|
-
const price = invertPrice(toPrice(depth.tick))
|
|
76
|
-
const readablePrice = formatPrice(
|
|
77
|
-
price,
|
|
78
|
-
this.quote.decimals,
|
|
79
|
-
this.base.decimals,
|
|
80
|
-
)
|
|
81
|
-
const baseAmount = depth.rawAmount * depth.unit
|
|
82
|
-
return {
|
|
83
|
-
price: readablePrice,
|
|
84
|
-
baseAmount,
|
|
85
|
-
} as Depth
|
|
86
|
-
})
|
|
87
|
-
this.askBookOpen =
|
|
88
|
-
books.filter((book) =>
|
|
89
|
-
isAddressEqual(book.quote.address, this.base.address),
|
|
90
|
-
).length > 0
|
|
91
|
-
this.books = books
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
take = ({
|
|
95
|
-
takeQuote,
|
|
96
|
-
limitPrice,
|
|
97
|
-
amountOut, // quote if takeQuote, base otherwise
|
|
98
|
-
}: {
|
|
99
|
-
takeQuote: boolean
|
|
100
|
-
limitPrice: bigint
|
|
101
|
-
amountOut: bigint
|
|
102
|
-
}) => {
|
|
103
|
-
if (takeQuote) {
|
|
104
|
-
const bidDepths = this.books
|
|
105
|
-
.filter((book) =>
|
|
106
|
-
isAddressEqual(book.quote.address, this.quote.address),
|
|
107
|
-
)
|
|
108
|
-
.flatMap((book) => book.depths)
|
|
109
|
-
return this.takeInner({ depths: bidDepths, limitPrice, amountOut })
|
|
110
|
-
} else {
|
|
111
|
-
const askDepths = this.books
|
|
112
|
-
.filter((book) => isAddressEqual(book.quote.address, this.base.address))
|
|
113
|
-
.flatMap((book) => book.depths)
|
|
114
|
-
return this.takeInner({
|
|
115
|
-
depths: askDepths,
|
|
116
|
-
limitPrice: invertPrice(limitPrice),
|
|
117
|
-
amountOut,
|
|
118
|
-
})
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
spend = ({
|
|
123
|
-
spendBase,
|
|
124
|
-
limitPrice,
|
|
125
|
-
amountIn, // base if spendBase, quote otherwise
|
|
126
|
-
}: {
|
|
127
|
-
spendBase: boolean
|
|
128
|
-
limitPrice: bigint
|
|
129
|
-
amountIn: bigint
|
|
130
|
-
}) => {
|
|
131
|
-
if (spendBase) {
|
|
132
|
-
const bidDepths = this.books
|
|
133
|
-
.filter((book) =>
|
|
134
|
-
isAddressEqual(book.quote.address, this.quote.address),
|
|
135
|
-
)
|
|
136
|
-
.flatMap((book) => book.depths)
|
|
137
|
-
return this.spendInner({ depths: bidDepths, limitPrice, amountIn })
|
|
138
|
-
} else {
|
|
139
|
-
const askDepths = this.books
|
|
140
|
-
.filter((book) => isAddressEqual(book.quote.address, this.base.address))
|
|
141
|
-
.flatMap((book) => book.depths)
|
|
142
|
-
return this.spendInner({
|
|
143
|
-
depths: askDepths,
|
|
144
|
-
limitPrice: invertPrice(limitPrice),
|
|
145
|
-
amountIn,
|
|
146
|
-
})
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
private takeInner = ({
|
|
151
|
-
depths, // only bid orders
|
|
152
|
-
limitPrice,
|
|
153
|
-
amountOut, // quote
|
|
154
|
-
}: {
|
|
155
|
-
depths: RawDepth[]
|
|
156
|
-
limitPrice: bigint
|
|
157
|
-
amountOut: bigint
|
|
158
|
-
}) => {
|
|
159
|
-
if (depths.length === 0) {
|
|
160
|
-
return {}
|
|
161
|
-
}
|
|
162
|
-
const takeResult: {
|
|
163
|
-
[key in string]: {
|
|
164
|
-
takenAmount: bigint
|
|
165
|
-
spendAmount: bigint
|
|
166
|
-
}
|
|
167
|
-
} = {}
|
|
168
|
-
for (const depth of depths) {
|
|
169
|
-
if (!takeResult[depth.bookId]) {
|
|
170
|
-
takeResult[depth.bookId] = {
|
|
171
|
-
takenAmount: 0n,
|
|
172
|
-
spendAmount: 0n,
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
let totalTakenQuoteAmount = 0n
|
|
177
|
-
|
|
178
|
-
const ticks = depths
|
|
179
|
-
.sort((a, b) => Number(b.tick) - Number(a.tick))
|
|
180
|
-
.map((depth) => depth.tick)
|
|
181
|
-
let index = 0
|
|
182
|
-
let tick = ticks[index]!
|
|
183
|
-
while (tick > -8388608n) {
|
|
184
|
-
if (limitPrice > toPrice(tick)) {
|
|
185
|
-
break
|
|
186
|
-
}
|
|
187
|
-
const currentDepth = depths.find((depth) => depth.tick === tick)!
|
|
188
|
-
const currentBook = this.books.find(
|
|
189
|
-
(book) => book.id === BigInt(currentDepth.bookId),
|
|
190
|
-
)!
|
|
191
|
-
let maxAmount = TAKER_DEFAULT_POLICY.usesQuote
|
|
192
|
-
? TAKER_DEFAULT_POLICY.calculateOriginalAmount(
|
|
193
|
-
amountOut - totalTakenQuoteAmount,
|
|
194
|
-
true,
|
|
195
|
-
)
|
|
196
|
-
: amountOut - totalTakenQuoteAmount
|
|
197
|
-
maxAmount = divide(maxAmount, currentBook.unit, true)
|
|
198
|
-
|
|
199
|
-
if (maxAmount === 0n) {
|
|
200
|
-
break
|
|
201
|
-
}
|
|
202
|
-
let quoteAmount =
|
|
203
|
-
(currentDepth.rawAmount > maxAmount
|
|
204
|
-
? maxAmount
|
|
205
|
-
: currentDepth.rawAmount) * currentBook.unit
|
|
206
|
-
let baseAmount = quoteToBase(tick, quoteAmount, true)
|
|
207
|
-
if (TAKER_DEFAULT_POLICY.usesQuote) {
|
|
208
|
-
quoteAmount =
|
|
209
|
-
quoteAmount - TAKER_DEFAULT_POLICY.calculateFee(quoteAmount, false)
|
|
210
|
-
} else {
|
|
211
|
-
baseAmount =
|
|
212
|
-
baseAmount + TAKER_DEFAULT_POLICY.calculateFee(baseAmount, false)
|
|
213
|
-
}
|
|
214
|
-
if (quoteAmount === 0n) {
|
|
215
|
-
break
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
takeResult[currentDepth.bookId]!.takenAmount += quoteAmount
|
|
219
|
-
takeResult[currentDepth.bookId]!.spendAmount += baseAmount
|
|
220
|
-
totalTakenQuoteAmount += quoteAmount
|
|
221
|
-
if (amountOut <= totalTakenQuoteAmount) {
|
|
222
|
-
break
|
|
223
|
-
}
|
|
224
|
-
if (ticks.length === index + 1) {
|
|
225
|
-
break
|
|
226
|
-
}
|
|
227
|
-
index++
|
|
228
|
-
tick = ticks[index]!
|
|
229
|
-
}
|
|
230
|
-
return Object.fromEntries(
|
|
231
|
-
Object.entries(takeResult).filter(
|
|
232
|
-
([, value]) => value.spendAmount > 0 && value.takenAmount > 0,
|
|
233
|
-
),
|
|
234
|
-
)
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
private spendInner = ({
|
|
238
|
-
depths, // only bid orders
|
|
239
|
-
limitPrice,
|
|
240
|
-
amountIn, // base
|
|
241
|
-
}: {
|
|
242
|
-
depths: RawDepth[]
|
|
243
|
-
limitPrice: bigint
|
|
244
|
-
amountIn: bigint
|
|
245
|
-
}) => {
|
|
246
|
-
if (depths.length === 0) {
|
|
247
|
-
return {}
|
|
248
|
-
}
|
|
249
|
-
const spendResult: {
|
|
250
|
-
[key in string]: {
|
|
251
|
-
takenAmount: bigint
|
|
252
|
-
spendAmount: bigint
|
|
253
|
-
}
|
|
254
|
-
} = {}
|
|
255
|
-
for (const depth of depths) {
|
|
256
|
-
if (!spendResult[depth.bookId]) {
|
|
257
|
-
spendResult[depth.bookId] = {
|
|
258
|
-
takenAmount: 0n,
|
|
259
|
-
spendAmount: 0n,
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
let totalSpendBaseAmount = 0n
|
|
264
|
-
|
|
265
|
-
const ticks = depths
|
|
266
|
-
.sort((a, b) => Number(b.tick) - Number(a.tick))
|
|
267
|
-
.map((depth) => depth.tick)
|
|
268
|
-
let index = 0
|
|
269
|
-
let tick = ticks[index]!
|
|
270
|
-
while (totalSpendBaseAmount <= amountIn && tick > -8388608n) {
|
|
271
|
-
if (limitPrice > toPrice(tick)) {
|
|
272
|
-
break
|
|
273
|
-
}
|
|
274
|
-
const currentDepth = depths.find((depth) => depth.tick === tick)!
|
|
275
|
-
const currentBook = this.books.find(
|
|
276
|
-
(book) => book.id === BigInt(currentDepth.bookId),
|
|
277
|
-
)!
|
|
278
|
-
let maxAmount = TAKER_DEFAULT_POLICY.usesQuote
|
|
279
|
-
? amountIn - totalSpendBaseAmount
|
|
280
|
-
: TAKER_DEFAULT_POLICY.calculateOriginalAmount(
|
|
281
|
-
amountIn - totalSpendBaseAmount,
|
|
282
|
-
false,
|
|
283
|
-
)
|
|
284
|
-
maxAmount = baseToQuote(tick, maxAmount, false) / currentBook.unit
|
|
285
|
-
|
|
286
|
-
if (maxAmount === 0n) {
|
|
287
|
-
break
|
|
288
|
-
}
|
|
289
|
-
let quoteAmount =
|
|
290
|
-
(currentDepth.rawAmount > maxAmount
|
|
291
|
-
? maxAmount
|
|
292
|
-
: currentDepth.rawAmount) * currentBook.unit
|
|
293
|
-
let baseAmount = quoteToBase(tick, quoteAmount, true)
|
|
294
|
-
if (TAKER_DEFAULT_POLICY.usesQuote) {
|
|
295
|
-
quoteAmount =
|
|
296
|
-
quoteAmount - TAKER_DEFAULT_POLICY.calculateFee(quoteAmount, false)
|
|
297
|
-
} else {
|
|
298
|
-
baseAmount =
|
|
299
|
-
baseAmount + TAKER_DEFAULT_POLICY.calculateFee(baseAmount, false)
|
|
300
|
-
}
|
|
301
|
-
if (baseAmount === 0n) {
|
|
302
|
-
break
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
spendResult[currentDepth.bookId]!.takenAmount += quoteAmount
|
|
306
|
-
spendResult[currentDepth.bookId]!.spendAmount += baseAmount
|
|
307
|
-
totalSpendBaseAmount += baseAmount
|
|
308
|
-
if (ticks.length === index + 1) {
|
|
309
|
-
break
|
|
310
|
-
}
|
|
311
|
-
index++
|
|
312
|
-
tick = ticks[index]!
|
|
313
|
-
}
|
|
314
|
-
return Object.fromEntries(
|
|
315
|
-
Object.entries(spendResult).filter(
|
|
316
|
-
([, value]) => value.spendAmount > 0 && value.takenAmount > 0,
|
|
317
|
-
),
|
|
318
|
-
)
|
|
319
|
-
}
|
|
320
|
-
}
|
package/src/model/open-order.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { Currency } from './currency'
|
|
2
|
-
|
|
3
|
-
export type OpenOrder = {
|
|
4
|
-
id: string
|
|
5
|
-
isBid: boolean
|
|
6
|
-
inputCurrency: Currency
|
|
7
|
-
outputCurrency: Currency
|
|
8
|
-
txHash: `0x${string}`
|
|
9
|
-
createdAt: number
|
|
10
|
-
price: number
|
|
11
|
-
amount: { currency: Currency; value: string }
|
|
12
|
-
filled: { currency: Currency; value: string }
|
|
13
|
-
claimed: { currency: Currency; value: string }
|
|
14
|
-
claimable: { currency: Currency; value: string }
|
|
15
|
-
cancelable: boolean
|
|
16
|
-
}
|