@clober/v2-sdk 0.0.3-b → 0.0.4-6.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.
- package/README.md +12 -4
- package/dist/cjs/abis/core/book-manager-abi.js +112 -0
- package/dist/cjs/abis/core/book-manager-abi.js.map +1 -0
- package/dist/cjs/abis/core/book-viewer-abi.js +321 -0
- package/dist/cjs/abis/core/book-viewer-abi.js.map +1 -0
- package/dist/cjs/abis/core/controller-abi.js +29 -24
- package/dist/cjs/abis/core/controller-abi.js.map +1 -1
- package/dist/cjs/apis/chart-logs.js +154 -0
- package/dist/cjs/apis/chart-logs.js.map +1 -0
- package/dist/cjs/apis/market.js +63 -34
- package/dist/cjs/apis/market.js.map +1 -1
- package/dist/cjs/apis/open-order.js +60 -25
- package/dist/cjs/apis/open-order.js.map +1 -1
- package/dist/cjs/approval.js +79 -32
- package/dist/cjs/approval.js.map +1 -1
- package/dist/cjs/call.js +346 -245
- package/dist/cjs/call.js.map +1 -1
- package/dist/cjs/constants/addresses.js +26 -1
- package/dist/cjs/constants/addresses.js.map +1 -1
- package/dist/cjs/constants/chain.js +19 -1
- package/dist/cjs/constants/chain.js.map +1 -1
- package/dist/cjs/constants/client.js +18 -0
- package/dist/cjs/constants/client.js.map +1 -0
- package/dist/cjs/constants/currency.js +28 -2
- package/dist/cjs/constants/currency.js.map +1 -1
- package/dist/cjs/constants/fee.js +17 -2
- package/dist/cjs/constants/fee.js.map +1 -1
- package/dist/cjs/constants/subgraph.js +57 -0
- package/dist/cjs/constants/subgraph.js.map +1 -0
- package/dist/cjs/constants/test-chain.js +27 -0
- package/dist/cjs/constants/test-chain.js.map +1 -0
- package/dist/cjs/index.js +20 -6
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/model/book.js +47 -29
- package/dist/cjs/model/book.js.map +1 -1
- package/dist/cjs/model/chart-log.js +3 -0
- package/dist/cjs/model/chart-log.js.map +1 -0
- package/dist/cjs/model/market.js +43 -182
- package/dist/cjs/model/market.js.map +1 -1
- package/dist/cjs/signature.js +25 -25
- package/dist/cjs/signature.js.map +1 -1
- package/dist/cjs/type.js +16 -1
- package/dist/cjs/type.js.map +1 -1
- package/dist/cjs/utils/allowance.js +40 -0
- package/dist/cjs/utils/allowance.js.map +1 -0
- package/dist/cjs/utils/approval.js +3 -8
- package/dist/cjs/utils/approval.js.map +1 -1
- package/dist/cjs/utils/bigint.js +9 -0
- package/dist/cjs/utils/bigint.js.map +1 -0
- package/dist/cjs/utils/book-id.js +7 -7
- package/dist/cjs/utils/book-id.js.map +1 -1
- package/dist/cjs/utils/build-transaction.js +10 -13
- package/dist/cjs/utils/build-transaction.js.map +1 -1
- package/dist/cjs/utils/currency.js +166 -0
- package/dist/cjs/utils/currency.js.map +1 -0
- package/dist/cjs/utils/decorator.js +15 -0
- package/dist/cjs/utils/decorator.js.map +1 -0
- package/dist/cjs/utils/open.js +36 -0
- package/dist/cjs/utils/open.js.map +1 -0
- package/dist/cjs/utils/order.js +102 -0
- package/dist/cjs/utils/order.js.map +1 -0
- package/dist/cjs/utils/prices.js +1 -1
- package/dist/cjs/utils/unit-size.js +49 -0
- package/dist/cjs/utils/unit-size.js.map +1 -0
- package/dist/cjs/utils.js +14 -0
- package/dist/cjs/utils.js.map +1 -0
- package/dist/cjs/view.js +64 -49
- package/dist/cjs/view.js.map +1 -1
- package/dist/esm/abis/core/book-manager-abi.js +109 -0
- package/dist/esm/abis/core/book-manager-abi.js.map +1 -0
- package/dist/esm/abis/core/book-viewer-abi.js +318 -0
- package/dist/esm/abis/core/book-viewer-abi.js.map +1 -0
- package/dist/esm/abis/core/controller-abi.js +29 -24
- package/dist/esm/abis/core/controller-abi.js.map +1 -1
- package/dist/esm/apis/chart-logs.js +150 -0
- package/dist/esm/apis/chart-logs.js.map +1 -0
- package/dist/esm/apis/market.js +63 -34
- package/dist/esm/apis/market.js.map +1 -1
- package/dist/esm/apis/open-order.js +61 -25
- package/dist/esm/apis/open-order.js.map +1 -1
- package/dist/esm/approval.js +118 -45
- package/dist/esm/approval.js.map +1 -1
- package/dist/esm/call.js +464 -342
- package/dist/esm/call.js.map +1 -1
- package/dist/esm/constants/addresses.js +26 -1
- package/dist/esm/constants/addresses.js.map +1 -1
- package/dist/esm/constants/chain.js +20 -2
- package/dist/esm/constants/chain.js.map +1 -1
- package/dist/esm/constants/client.js +14 -0
- package/dist/esm/constants/client.js.map +1 -0
- package/dist/esm/constants/currency.js +28 -2
- package/dist/esm/constants/currency.js.map +1 -1
- package/dist/esm/constants/fee.js +17 -2
- package/dist/esm/constants/fee.js.map +1 -1
- package/dist/esm/constants/subgraph.js +53 -0
- package/dist/esm/constants/subgraph.js.map +1 -0
- package/dist/esm/constants/test-chain.js +24 -0
- package/dist/esm/constants/test-chain.js.map +1 -0
- package/dist/esm/index.js +1 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/model/book.js +47 -29
- package/dist/esm/model/book.js.map +1 -1
- package/dist/esm/model/chart-log.js +2 -0
- package/dist/esm/model/chart-log.js.map +1 -0
- package/dist/esm/model/market.js +44 -187
- package/dist/esm/model/market.js.map +1 -1
- package/dist/esm/signature.js +40 -45
- package/dist/esm/signature.js.map +1 -1
- package/dist/esm/type.js +15 -0
- package/dist/esm/type.js.map +1 -1
- package/dist/esm/utils/allowance.js +36 -0
- package/dist/esm/utils/allowance.js.map +1 -0
- package/dist/esm/utils/approval.js +3 -8
- package/dist/esm/utils/approval.js.map +1 -1
- package/dist/esm/utils/bigint.js +5 -0
- package/dist/esm/utils/bigint.js.map +1 -0
- package/dist/esm/utils/book-id.js +7 -7
- package/dist/esm/utils/book-id.js.map +1 -1
- package/dist/esm/utils/build-transaction.js +11 -14
- package/dist/esm/utils/build-transaction.js.map +1 -1
- package/dist/esm/utils/currency.js +161 -0
- package/dist/esm/utils/currency.js.map +1 -0
- package/dist/esm/utils/decorator.js +11 -0
- package/dist/esm/utils/decorator.js.map +1 -0
- package/dist/esm/utils/open.js +32 -0
- package/dist/esm/utils/open.js.map +1 -0
- package/dist/esm/utils/order.js +98 -0
- package/dist/esm/utils/order.js.map +1 -0
- package/dist/esm/utils/prices.js +1 -1
- package/dist/esm/utils/unit-size.js +45 -0
- package/dist/esm/utils/unit-size.js.map +1 -0
- package/dist/esm/utils.js +4 -0
- package/dist/esm/utils.js.map +1 -0
- package/dist/esm/view.js +164 -71
- package/dist/esm/view.js.map +1 -1
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/dist/types/abis/core/book-manager-abi.d.ts +82 -0
- package/dist/types/abis/core/book-manager-abi.d.ts.map +1 -0
- package/dist/types/abis/core/book-viewer-abi.d.ts +243 -0
- package/dist/types/abis/core/book-viewer-abi.d.ts.map +1 -0
- package/dist/types/abis/core/controller-abi.d.ts +23 -19
- package/dist/types/abis/core/controller-abi.d.ts.map +1 -1
- package/dist/types/apis/chart-logs.d.ts +5 -0
- package/dist/types/apis/chart-logs.d.ts.map +1 -0
- package/dist/types/apis/market.d.ts +1 -1
- package/dist/types/apis/market.d.ts.map +1 -1
- package/dist/types/apis/open-order.d.ts +4 -3
- package/dist/types/apis/open-order.d.ts.map +1 -1
- package/dist/types/approval.d.ts +50 -15
- package/dist/types/approval.d.ts.map +1 -1
- package/dist/types/call.d.ts +191 -123
- package/dist/types/call.d.ts.map +1 -1
- package/dist/types/constants/addresses.d.ts.map +1 -1
- package/dist/types/constants/chain.d.ts +6 -2
- package/dist/types/constants/chain.d.ts.map +1 -1
- package/dist/types/constants/client.d.ts +5 -0
- package/dist/types/constants/client.d.ts.map +1 -0
- package/dist/types/constants/currency.d.ts.map +1 -1
- package/dist/types/constants/fee.d.ts +7 -2
- package/dist/types/constants/fee.d.ts.map +1 -1
- package/dist/types/constants/subgraph.d.ts +10 -0
- package/dist/types/constants/subgraph.d.ts.map +1 -0
- package/dist/types/constants/test-chain.d.ts +3 -0
- package/dist/types/constants/test-chain.d.ts.map +1 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/model/book.d.ts +13 -29
- package/dist/types/model/book.d.ts.map +1 -1
- package/dist/types/model/chart-log.d.ts +9 -0
- package/dist/types/model/chart-log.d.ts.map +1 -0
- package/dist/types/model/depth.d.ts +3 -5
- package/dist/types/model/depth.d.ts.map +1 -1
- package/dist/types/model/market.d.ts +15 -19
- package/dist/types/model/market.d.ts.map +1 -1
- package/dist/types/model/open-order.d.ts +16 -8
- package/dist/types/model/open-order.d.ts.map +1 -1
- package/dist/types/signature.d.ts +23 -25
- package/dist/types/signature.d.ts.map +1 -1
- package/dist/types/type.d.ts +54 -8
- package/dist/types/type.d.ts.map +1 -1
- package/dist/types/utils/allowance.d.ts +3 -0
- package/dist/types/utils/allowance.d.ts.map +1 -0
- package/dist/types/utils/approval.d.ts +1 -1
- package/dist/types/utils/approval.d.ts.map +1 -1
- package/dist/types/utils/bigint.d.ts +2 -0
- package/dist/types/utils/bigint.d.ts.map +1 -0
- package/dist/types/utils/book-id.d.ts +2 -1
- package/dist/types/utils/book-id.d.ts.map +1 -1
- package/dist/types/utils/build-transaction.d.ts +3 -4
- package/dist/types/utils/build-transaction.d.ts.map +1 -1
- package/dist/types/utils/currency.d.ts +7 -0
- package/dist/types/utils/currency.d.ts.map +1 -0
- package/dist/types/utils/decorator.d.ts +6 -0
- package/dist/types/utils/decorator.d.ts.map +1 -0
- package/dist/types/utils/open.d.ts +3 -0
- package/dist/types/utils/open.d.ts.map +1 -0
- package/dist/types/utils/order.d.ts +8 -0
- package/dist/types/utils/order.d.ts.map +1 -0
- package/dist/types/utils/prices.d.ts +1 -1
- package/dist/types/utils/unit-size.d.ts +4 -0
- package/dist/types/utils/unit-size.d.ts.map +1 -0
- package/dist/types/utils.d.ts +4 -0
- package/dist/types/utils.d.ts.map +1 -0
- package/dist/types/view.d.ts +164 -52
- package/dist/types/view.d.ts.map +1 -1
- package/package.json +1 -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/subgraph.js +0 -26
- package/dist/cjs/apis/subgraph.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/utils/unit.js +0 -38
- package/dist/cjs/utils/unit.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/subgraph.js +0 -22
- package/dist/esm/apis/subgraph.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/utils/unit.js +0 -34
- package/dist/esm/utils/unit.js.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/subgraph.d.ts +0 -3
- package/dist/types/apis/subgraph.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/utils/unit.d.ts +0 -4
- package/dist/types/utils/unit.d.ts.map +0 -1
package/dist/esm/view.js
CHANGED
|
@@ -1,43 +1,82 @@
|
|
|
1
|
-
import { formatUnits, isAddressEqual, parseUnits } from 'viem';
|
|
1
|
+
import { formatUnits, getAddress, isAddressEqual, parseUnits } from 'viem';
|
|
2
2
|
import { fetchMarket } from './apis/market';
|
|
3
3
|
import { parsePrice } from './utils/prices';
|
|
4
4
|
import { MAX_PRICE } from './constants/price';
|
|
5
|
-
import { fetchOpenOrder,
|
|
5
|
+
import { fetchOpenOrder, fetchOpenOrdersByUserAddress } from './apis/open-order';
|
|
6
|
+
import { decorator } from './utils/decorator';
|
|
7
|
+
import { fetchChartLogs, fetchLatestChartLog } from './apis/chart-logs';
|
|
8
|
+
import { getMarketId } from './utils/market';
|
|
9
|
+
import { CONTRACT_ADDRESSES } from './constants/addresses';
|
|
10
|
+
/**
|
|
11
|
+
* Get contract addresses by chain id
|
|
12
|
+
* @param chainId - chain id from {@link CHAIN_IDS}
|
|
13
|
+
* @returns Contract addresses
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* import { getContractAddresses } from '@clober/v2-sdk'
|
|
17
|
+
*
|
|
18
|
+
* const addresses = await getContractAddresses({
|
|
19
|
+
* chainId: 421614,
|
|
20
|
+
* })
|
|
21
|
+
*/
|
|
22
|
+
export const getContractAddresses = ({ chainId }) => {
|
|
23
|
+
return CONTRACT_ADDRESSES[chainId];
|
|
24
|
+
};
|
|
6
25
|
/**
|
|
7
26
|
* Get market information by chain id and token addresses
|
|
8
27
|
* @param chainId - chain id from {@link CHAIN_IDS}
|
|
9
28
|
* @param token0 - token0 address
|
|
10
29
|
* @param token1 - token1 address
|
|
11
30
|
* @param options
|
|
31
|
+
* @param options.n - number of depth levels to fetch
|
|
12
32
|
* @param options.rpcUrl - RPC URL of the blockchain
|
|
33
|
+
* @param options.useSubgraph Whether to use the subgraph to fetch the market data.
|
|
13
34
|
* @returns A market {@link Market}
|
|
14
35
|
*
|
|
15
36
|
* @example
|
|
16
37
|
* import { getMarket } from '@clober/v2-sdk'
|
|
17
38
|
*
|
|
18
|
-
* const market = await getMarket(
|
|
19
|
-
* 421614,
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
* )
|
|
39
|
+
* const market = await getMarket({
|
|
40
|
+
* chainId: 421614,
|
|
41
|
+
* token0: '0x00bfd44e79fb7f6dd5887a9426c8ef85a0cd23e0',
|
|
42
|
+
* token1: '0x0000000000000000000000000000000000000000',
|
|
43
|
+
* })
|
|
23
44
|
*/
|
|
24
|
-
export const getMarket = async (chainId, token0, token1, options) => {
|
|
45
|
+
export const getMarket = decorator(async ({ chainId, token0, token1, options, }) => {
|
|
25
46
|
if (isAddressEqual(token0, token1)) {
|
|
26
47
|
throw new Error('Token0 and token1 must be different');
|
|
27
48
|
}
|
|
28
|
-
const market = await fetchMarket(chainId, [token0, token1], options?.
|
|
49
|
+
const market = await fetchMarket(chainId, [token0, token1], options?.n);
|
|
29
50
|
return {
|
|
30
51
|
chainId,
|
|
31
52
|
quote: market.quote,
|
|
32
53
|
base: market.base,
|
|
33
54
|
makerFee: market.makerFee,
|
|
34
55
|
takerFee: market.takerFee,
|
|
35
|
-
bids: market.bids,
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
56
|
+
bids: market.bids.map(({ price, baseAmount }) => ({
|
|
57
|
+
price,
|
|
58
|
+
baseAmount: formatUnits(baseAmount, market.base.decimals),
|
|
59
|
+
})),
|
|
60
|
+
bidBook: {
|
|
61
|
+
id: market.bidBook.id.toString(),
|
|
62
|
+
base: market.bidBook.base,
|
|
63
|
+
unitSize: market.bidBook.unitSize.toString(),
|
|
64
|
+
quote: market.bidBook.quote,
|
|
65
|
+
isOpened: market.bidBook.isOpened,
|
|
66
|
+
},
|
|
67
|
+
asks: market.asks.map(({ price, baseAmount }) => ({
|
|
68
|
+
price,
|
|
69
|
+
baseAmount: formatUnits(baseAmount, market.base.decimals),
|
|
70
|
+
})),
|
|
71
|
+
askBook: {
|
|
72
|
+
id: market.askBook.id.toString(),
|
|
73
|
+
base: market.askBook.base,
|
|
74
|
+
unitSize: market.askBook.unitSize.toString(),
|
|
75
|
+
quote: market.askBook.quote,
|
|
76
|
+
isOpened: market.askBook.isOpened,
|
|
77
|
+
},
|
|
39
78
|
};
|
|
40
|
-
};
|
|
79
|
+
});
|
|
41
80
|
/**
|
|
42
81
|
* Calculates the expected output for a given input amount, based on the provided market data.
|
|
43
82
|
*
|
|
@@ -48,19 +87,20 @@ export const getMarket = async (chainId, token0, token1, options) => {
|
|
|
48
87
|
* @param options
|
|
49
88
|
* @param options.limitPrice The maximum limit price to spend.
|
|
50
89
|
* @param options.rpcUrl The RPC URL of the blockchain.
|
|
90
|
+
* @param options.useSubgraph Whether to use the subgraph to fetch the market data.
|
|
51
91
|
* @returns A Promise resolving to an object containing the taken amount, spend amount and result of the calculation.
|
|
52
92
|
* @example
|
|
53
93
|
* import { getExpectedOutput } from '@clober/v2-sdk'
|
|
54
94
|
*
|
|
55
|
-
* const { takenAmount,
|
|
56
|
-
* 421614,
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
*
|
|
60
|
-
* )
|
|
95
|
+
* const { takenAmount, spentAmount } = await getExpectedOutput({
|
|
96
|
+
* chainId: 421614,
|
|
97
|
+
* inputToken: '0x00bfd44e79fb7f6dd5887a9426c8ef85a0cd23e0',
|
|
98
|
+
* outputToken: '0x0000000000000000000000000000000000000000',
|
|
99
|
+
* amountIn: '1000.123', // spend 1000.123 USDC
|
|
100
|
+
* })
|
|
61
101
|
*/
|
|
62
|
-
export const getExpectedOutput = async (chainId, inputToken, outputToken, amountIn, options) => {
|
|
63
|
-
const market = await fetchMarket(chainId, [inputToken, outputToken]
|
|
102
|
+
export const getExpectedOutput = decorator(async ({ chainId, inputToken, outputToken, amountIn, options, }) => {
|
|
103
|
+
const market = await fetchMarket(chainId, [inputToken, outputToken]);
|
|
64
104
|
const isBid = isAddressEqual(market.quote.address, inputToken);
|
|
65
105
|
const rawLimitPrice = options && options.limitPrice
|
|
66
106
|
? parsePrice(Number(options.limitPrice), market.quote.decimals, market.base.decimals)
|
|
@@ -68,25 +108,17 @@ export const getExpectedOutput = async (chainId, inputToken, outputToken, amount
|
|
|
68
108
|
? MAX_PRICE
|
|
69
109
|
: 0n;
|
|
70
110
|
const inputCurrency = isBid ? market.quote : market.base;
|
|
71
|
-
const
|
|
72
|
-
|
|
111
|
+
const { takenQuoteAmount, spentBaseAmount, bookId } = market.spend({
|
|
112
|
+
spentBase: !isBid,
|
|
73
113
|
limitPrice: rawLimitPrice,
|
|
74
114
|
amountIn: parseUnits(amountIn, inputCurrency.decimals),
|
|
75
115
|
});
|
|
76
|
-
const { takenAmount, spendAmount } = Object.values(result).reduce((acc, { takenAmount, spendAmount }) => ({
|
|
77
|
-
takenAmount: acc.takenAmount + takenAmount,
|
|
78
|
-
spendAmount: acc.spendAmount + spendAmount,
|
|
79
|
-
}), { takenAmount: 0n, spendAmount: 0n });
|
|
80
116
|
return {
|
|
81
|
-
takenAmount: formatUnits(
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
bookId: BigInt(bookId),
|
|
85
|
-
takenAmount,
|
|
86
|
-
spendAmount,
|
|
87
|
-
})),
|
|
117
|
+
takenAmount: formatUnits(takenQuoteAmount, isBid ? market.base.decimals : market.quote.decimals),
|
|
118
|
+
spentAmount: formatUnits(spentBaseAmount, isBid ? market.quote.decimals : market.base.decimals),
|
|
119
|
+
bookId,
|
|
88
120
|
};
|
|
89
|
-
};
|
|
121
|
+
});
|
|
90
122
|
/**
|
|
91
123
|
* Calculates the expected input for a given output amount, based on the provided market data.
|
|
92
124
|
*
|
|
@@ -97,19 +129,20 @@ export const getExpectedOutput = async (chainId, inputToken, outputToken, amount
|
|
|
97
129
|
* @param options
|
|
98
130
|
* @param options.limitPrice The maximum limit price to take.
|
|
99
131
|
* @param options.rpcUrl The RPC URL of the blockchain.
|
|
100
|
-
* @
|
|
132
|
+
* @param options.useSubgraph Whether to use the subgraph to fetch the market data.
|
|
133
|
+
* @returns A Promise resolving to an object containing the taken amount, spent amount and result of the calculation.
|
|
101
134
|
* @example
|
|
102
135
|
* import { getExpectedInput } from '@clober/v2-sdk'
|
|
103
136
|
*
|
|
104
|
-
* const { takenAmount,
|
|
105
|
-
* 421614,
|
|
106
|
-
*
|
|
107
|
-
*
|
|
108
|
-
*
|
|
109
|
-
* )
|
|
137
|
+
* const { takenAmount, spentAmount } = await getExpectedInput({
|
|
138
|
+
* chainId: 421614,
|
|
139
|
+
* inputToken: '0x00bfd44e79fb7f6dd5887a9426c8ef85a0cd23e0',
|
|
140
|
+
* outputToken: '0x0000000000000000000000000000000000000000',
|
|
141
|
+
* amountOut: '0.1', // take 0.1 ETH
|
|
142
|
+
* })
|
|
110
143
|
*/
|
|
111
|
-
export const getExpectedInput = async (chainId, inputToken, outputToken, amountOut, options) => {
|
|
112
|
-
const market = await fetchMarket(chainId, [inputToken, outputToken]
|
|
144
|
+
export const getExpectedInput = decorator(async ({ chainId, inputToken, outputToken, amountOut, options, }) => {
|
|
145
|
+
const market = await fetchMarket(chainId, [inputToken, outputToken]);
|
|
113
146
|
const isBid = isAddressEqual(market.quote.address, inputToken);
|
|
114
147
|
const rawLimitPrice = options && options.limitPrice
|
|
115
148
|
? parsePrice(Number(options.limitPrice), market.quote.decimals, market.base.decimals)
|
|
@@ -117,25 +150,17 @@ export const getExpectedInput = async (chainId, inputToken, outputToken, amountO
|
|
|
117
150
|
? MAX_PRICE
|
|
118
151
|
: 0n;
|
|
119
152
|
const outputCurrency = isBid ? market.base : market.quote;
|
|
120
|
-
const
|
|
153
|
+
const { takenQuoteAmount, spentBaseAmount, bookId } = market.take({
|
|
121
154
|
takeQuote: !isBid,
|
|
122
155
|
limitPrice: rawLimitPrice,
|
|
123
156
|
amountOut: parseUnits(amountOut, outputCurrency.decimals),
|
|
124
157
|
});
|
|
125
|
-
const { takenAmount, spendAmount } = Object.values(result).reduce((acc, { takenAmount, spendAmount }) => ({
|
|
126
|
-
takenAmount: acc.takenAmount + takenAmount,
|
|
127
|
-
spendAmount: acc.spendAmount + spendAmount,
|
|
128
|
-
}), { takenAmount: 0n, spendAmount: 0n });
|
|
129
158
|
return {
|
|
130
|
-
takenAmount: formatUnits(
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
bookId: BigInt(bookId),
|
|
134
|
-
takenAmount,
|
|
135
|
-
spendAmount,
|
|
136
|
-
})),
|
|
159
|
+
takenAmount: formatUnits(takenQuoteAmount, isBid ? market.base.decimals : market.quote.decimals),
|
|
160
|
+
spentAmount: formatUnits(spentBaseAmount, isBid ? market.quote.decimals : market.base.decimals),
|
|
161
|
+
bookId,
|
|
137
162
|
};
|
|
138
|
-
};
|
|
163
|
+
});
|
|
139
164
|
/**
|
|
140
165
|
* Retrieves the open order with the specified ID on the given chain.
|
|
141
166
|
*
|
|
@@ -143,18 +168,19 @@ export const getExpectedInput = async (chainId, inputToken, outputToken, amountO
|
|
|
143
168
|
* @param {string} id The ID of the open order.
|
|
144
169
|
* @param options
|
|
145
170
|
* @param options.rpcUrl The RPC URL of the blockchain.
|
|
171
|
+
* @param options.useSubgraph Whether to use the subgraph to fetch the market data.
|
|
146
172
|
* @returns {Promise<OpenOrder>} Promise resolving to the open order object, or undefined if not found.
|
|
147
173
|
* @example
|
|
148
174
|
* import { getOpenOrder } from '@clober/v2-sdk'
|
|
149
175
|
*
|
|
150
|
-
* const openOrder = await getOpenOrder(
|
|
151
|
-
* 421614,
|
|
152
|
-
*
|
|
153
|
-
* )
|
|
176
|
+
* const openOrder = await getOpenOrder({
|
|
177
|
+
* chainId: 421614,
|
|
178
|
+
* id: '46223845323662364279893361453861711542636620039907198451770258805035840307200'
|
|
179
|
+
* })
|
|
154
180
|
*/
|
|
155
|
-
export const getOpenOrder = async (chainId, id,
|
|
156
|
-
return fetchOpenOrder(chainId, id
|
|
157
|
-
};
|
|
181
|
+
export const getOpenOrder = decorator(async ({ chainId, id, }) => {
|
|
182
|
+
return fetchOpenOrder(chainId, id);
|
|
183
|
+
});
|
|
158
184
|
/**
|
|
159
185
|
* Retrieves open orders for the specified user on the given chain.
|
|
160
186
|
*
|
|
@@ -162,16 +188,83 @@ export const getOpenOrder = async (chainId, id, options) => {
|
|
|
162
188
|
* @param {`0x${string}`} userAddress The Ethereum address of the user.
|
|
163
189
|
* @param options
|
|
164
190
|
* @param options.rpcUrl The RPC URL of the blockchain.
|
|
191
|
+
* @param options.useSubgraph Whether to use the subgraph to fetch the market data.
|
|
165
192
|
* @returns {Promise<OpenOrder[]>} Promise resolving to an array of open orders.
|
|
166
193
|
* @example
|
|
167
194
|
* import { getOpenOrders } from '@clober/v2-sdk'
|
|
168
195
|
*
|
|
169
|
-
* const openOrders = await getOpenOrders(
|
|
170
|
-
* 421614,
|
|
171
|
-
*
|
|
172
|
-
* )
|
|
196
|
+
* const openOrders = await getOpenOrders({
|
|
197
|
+
* chainId: 421614,
|
|
198
|
+
* userAddress: '0x5F79EE8f8fA862E98201120d83c4eC39D9468D49'
|
|
199
|
+
* })
|
|
200
|
+
*/
|
|
201
|
+
export const getOpenOrders = decorator(async ({ chainId, userAddress, }) => {
|
|
202
|
+
return fetchOpenOrdersByUserAddress(chainId, userAddress);
|
|
203
|
+
});
|
|
204
|
+
/**
|
|
205
|
+
* Retrieves the latest chart log for a specific market.
|
|
206
|
+
*
|
|
207
|
+
* @param {CHAIN_IDS} params.chainId - The ID of the blockchain.
|
|
208
|
+
* @param {`0x${string}`} params.quote - The address of the quote token.
|
|
209
|
+
* @param {`0x${string}`} params.base - The address of the base token.
|
|
210
|
+
* @returns {Promise<ChartLog>} A promise that resolves with the latest chart log.
|
|
211
|
+
*
|
|
212
|
+
* @example
|
|
213
|
+
* import { getLatestChartLog } from '@clober/v2-sdk'
|
|
214
|
+
*
|
|
215
|
+
* const logs = await getLatestChartLog({
|
|
216
|
+
* chainId: 421614,
|
|
217
|
+
* quote: '0x00bfd44e79fb7f6dd5887a9426c8ef85a0cd23e0',
|
|
218
|
+
* base: '0x0000000000000000000000000000000000000000',
|
|
219
|
+
* })
|
|
220
|
+
*/
|
|
221
|
+
export const getLatestChartLog = decorator(async ({ chainId, quote, base, }) => {
|
|
222
|
+
return fetchLatestChartLog(chainId, `${base}/${quote}`);
|
|
223
|
+
});
|
|
224
|
+
/**
|
|
225
|
+
* Retrieves chart logs for a specific market within a specified time interval.
|
|
226
|
+
*
|
|
227
|
+
* @param {CHAIN_IDS} params.chainId - The ID of the chain.
|
|
228
|
+
* @param {`0x${string}`} params.quote - The address of the quote token.
|
|
229
|
+
* @param {`0x${string}`} params.base - The address of the base token.
|
|
230
|
+
* @param {CHART_LOG_INTERVALS} params.intervalType - The type of time interval for the chart logs.
|
|
231
|
+
* @param {number} params.from - The start of the time interval (Unix timestamp in seconds).
|
|
232
|
+
* @param {number} params.to - The end of the time interval (Unix timestamp in seconds).
|
|
233
|
+
* @returns {Promise<ChartLog[]>} A promise that resolves with an array of chart logs within the specified interval.
|
|
234
|
+
*
|
|
235
|
+
* @example
|
|
236
|
+
* import { getLatestChartLog, CHART_LOG_INTERVALS } from '@clober/v2-sdk'
|
|
237
|
+
*
|
|
238
|
+
* const logs = await getChartLogs({
|
|
239
|
+
* chainId: 421614,
|
|
240
|
+
* quote: '0x00bfd44e79fb7f6dd5887a9426c8ef85a0cd23e0',
|
|
241
|
+
* base: '0x0000000000000000000000000000000000000000',
|
|
242
|
+
* intervalType: CHART_LOG_INTERVALS.oneDay,
|
|
243
|
+
* from: 1687305600,
|
|
244
|
+
* to: 1713312000,
|
|
245
|
+
* })
|
|
246
|
+
*/
|
|
247
|
+
export const getChartLogs = decorator(async ({ chainId, quote, base, intervalType, from, to, }) => {
|
|
248
|
+
return fetchChartLogs(chainId, `${base}/${quote}`, intervalType, from, to);
|
|
249
|
+
});
|
|
250
|
+
/**
|
|
251
|
+
* Retrieves the quote token address for a given chain and a pair of tokens.
|
|
252
|
+
*
|
|
253
|
+
* @param chainId - chain id from {@link CHAIN_IDS}
|
|
254
|
+
* @param token0 - token0 address
|
|
255
|
+
* @param token1 - token1 address
|
|
256
|
+
* @returns {string} The address of the quote token.
|
|
257
|
+
*
|
|
258
|
+
* @example
|
|
259
|
+
* import { getQuoteToken } from '@clober/v2-sdk'
|
|
260
|
+
*
|
|
261
|
+
* const quote = await getQuoteToken({
|
|
262
|
+
* chainId: 421614,
|
|
263
|
+
* token0: '0x00bfd44e79fb7f6dd5887a9426c8ef85a0cd23e0',
|
|
264
|
+
* token1: '0x0000000000000000000000000000000000000000',
|
|
265
|
+
* })
|
|
173
266
|
*/
|
|
174
|
-
export const
|
|
175
|
-
return
|
|
267
|
+
export const getQuoteToken = ({ chainId, token0, token1, }) => {
|
|
268
|
+
return getAddress(getMarketId(chainId, [token0, token1]).quoteTokenAddress);
|
|
176
269
|
};
|
|
177
270
|
//# sourceMappingURL=view.js.map
|
package/dist/esm/view.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"view.js","sourceRoot":"","sources":["../../src/view.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,cAAc,EAAE,UAAU,EAAE,MAAM,MAAM,CAAA;
|
|
1
|
+
{"version":3,"file":"view.js","sourceRoot":"","sources":["../../src/view.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,UAAU,EAAE,cAAc,EAAE,UAAU,EAAE,MAAM,MAAM,CAAA;AAE1E,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAA;AAI3C,OAAO,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAA;AAC3C,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAA;AAC7C,OAAO,EAAE,cAAc,EAAE,4BAA4B,EAAE,MAAM,mBAAmB,CAAA;AAEhF,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAA;AAC7C,OAAO,EAAE,cAAc,EAAE,mBAAmB,EAAE,MAAM,mBAAmB,CAAA;AACvE,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAC5C,OAAO,EAAE,kBAAkB,EAAE,MAAM,uBAAuB,CAAA;AAE1D;;;;;;;;;;;GAWG;AACH,MAAM,CAAC,MAAM,oBAAoB,GAAG,CAAC,EAAE,OAAO,EAA0B,EAAE,EAAE;IAC1E,OAAO,kBAAkB,CAAC,OAAO,CAAC,CAAA;AACpC,CAAC,CAAA;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,CAAC,MAAM,SAAS,GAAG,SAAS,CAChC,KAAK,EAAE,EACL,OAAO,EACP,MAAM,EACN,MAAM,EACN,OAAO,GAQR,EAAmB,EAAE;IACpB,IA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