@clober/v2-sdk 0.0.3 → 0.0.5
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/abis/core/book-viewer-abi.js +189 -0
- package/dist/cjs/abis/core/book-viewer-abi.js.map +1 -0
- package/dist/cjs/apis/market.js +42 -33
- package/dist/cjs/apis/market.js.map +1 -1
- package/dist/cjs/apis/open-order.js +10 -7
- package/dist/cjs/apis/open-order.js.map +1 -1
- package/dist/cjs/apis/subgraph.js.map +1 -1
- package/dist/cjs/approval.js +4 -4
- package/dist/cjs/approval.js.map +1 -1
- package/dist/cjs/call.js +197 -186
- package/dist/cjs/call.js.map +1 -1
- package/dist/cjs/constants/client.js +14 -0
- package/dist/cjs/constants/client.js.map +1 -0
- package/dist/cjs/model/book.js +8 -1
- package/dist/cjs/model/book.js.map +1 -1
- package/dist/cjs/model/market.js +44 -171
- package/dist/cjs/model/market.js.map +1 -1
- package/dist/cjs/signature.js +7 -11
- package/dist/cjs/signature.js.map +1 -1
- package/dist/cjs/utils/approval.js +3 -8
- package/dist/cjs/utils/approval.js.map +1 -1
- package/dist/cjs/utils/build-transaction.js +4 -8
- package/dist/cjs/utils/build-transaction.js.map +1 -1
- package/dist/cjs/{apis → utils}/currency.js +3 -7
- package/dist/cjs/utils/currency.js.map +1 -0
- package/dist/cjs/utils/decorator.js +13 -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/unit.js +3 -7
- package/dist/cjs/utils/unit.js.map +1 -1
- package/dist/cjs/view.js +24 -44
- package/dist/cjs/view.js.map +1 -1
- package/dist/esm/abis/core/book-viewer-abi.js +186 -0
- package/dist/esm/abis/core/book-viewer-abi.js.map +1 -0
- package/dist/esm/apis/market.js +43 -34
- package/dist/esm/apis/market.js.map +1 -1
- package/dist/esm/apis/open-order.js +10 -7
- package/dist/esm/apis/open-order.js.map +1 -1
- package/dist/esm/apis/subgraph.js.map +1 -1
- package/dist/esm/approval.js +5 -4
- package/dist/esm/approval.js.map +1 -1
- package/dist/esm/call.js +213 -191
- package/dist/esm/call.js.map +1 -1
- package/dist/esm/constants/client.js +10 -0
- package/dist/esm/constants/client.js.map +1 -0
- package/dist/esm/model/book.js +8 -1
- package/dist/esm/model/book.js.map +1 -1
- package/dist/esm/model/market.js +45 -176
- package/dist/esm/model/market.js.map +1 -1
- package/dist/esm/signature.js +8 -11
- package/dist/esm/signature.js.map +1 -1
- package/dist/esm/utils/approval.js +3 -8
- package/dist/esm/utils/approval.js.map +1 -1
- package/dist/esm/utils/build-transaction.js +5 -9
- package/dist/esm/utils/build-transaction.js.map +1 -1
- package/dist/esm/{apis → utils}/currency.js +4 -8
- package/dist/esm/utils/currency.js.map +1 -0
- package/dist/esm/utils/decorator.js +9 -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/unit.js +4 -8
- package/dist/esm/utils/unit.js.map +1 -1
- package/dist/esm/view.js +25 -39
- package/dist/esm/view.js.map +1 -1
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/dist/types/abis/core/book-viewer-abi.d.ts +141 -0
- package/dist/types/abis/core/book-viewer-abi.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 +3 -3
- package/dist/types/apis/open-order.d.ts.map +1 -1
- package/dist/types/approval.d.ts +6 -3
- package/dist/types/approval.d.ts.map +1 -1
- package/dist/types/call.d.ts +82 -31
- package/dist/types/call.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/model/book.d.ts +3 -1
- package/dist/types/model/book.d.ts.map +1 -1
- package/dist/types/model/depth.d.ts +0 -2
- package/dist/types/model/depth.d.ts.map +1 -1
- package/dist/types/model/market.d.ts +11 -13
- package/dist/types/model/market.d.ts.map +1 -1
- package/dist/types/model/open-order.d.ts +5 -2
- package/dist/types/model/open-order.d.ts.map +1 -1
- package/dist/types/signature.d.ts +7 -3
- package/dist/types/signature.d.ts.map +1 -1
- package/dist/types/type.d.ts +14 -6
- package/dist/types/type.d.ts.map +1 -1
- package/dist/types/utils/approval.d.ts +1 -1
- package/dist/types/utils/approval.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/{apis → utils}/currency.d.ts +1 -1
- 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/unit.d.ts +2 -2
- package/dist/types/utils/unit.d.ts.map +1 -1
- package/dist/types/view.d.ts +36 -24
- package/dist/types/view.d.ts.map +1 -1
- package/package.json +1 -8
- package/dist/cjs/apis/currency.js.map +0 -1
- package/dist/esm/apis/currency.js.map +0 -1
- package/dist/types/apis/currency.d.ts.map +0 -1
package/dist/esm/call.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { formatUnits, isAddressEqual, parseUnits, zeroAddress, zeroHash, } from 'viem';
|
|
2
2
|
import { CHAIN_MAP } from './constants/chain';
|
|
3
3
|
import { calculateUnit } from './utils/unit';
|
|
4
4
|
import { CONTROLLER_ABI } from './abis/core/controller-abi';
|
|
@@ -11,9 +11,8 @@ import { parsePrice } from './utils/prices';
|
|
|
11
11
|
import { fromPrice, invertPrice } from './utils/tick';
|
|
12
12
|
import { getExpectedOutput, getOpenOrders } from './view';
|
|
13
13
|
import { toBookId } from './utils/book-id';
|
|
14
|
-
import { MAKE_ORDER_PARAMS_ABI, TAKE_ORDER_PARAMS_ABI, } from './abis/core/params-abi';
|
|
15
|
-
import { Action } from './constants/action';
|
|
16
14
|
import { fetchIsApprovedForAll } from './utils/approval';
|
|
15
|
+
import { decorator } from './utils/decorator';
|
|
17
16
|
/**
|
|
18
17
|
* Build a transaction to open a market.
|
|
19
18
|
*
|
|
@@ -32,11 +31,11 @@ import { fetchIsApprovedForAll } from './utils/approval';
|
|
|
32
31
|
* '0x0000000000000000000000000000000000000000'
|
|
33
32
|
* )
|
|
34
33
|
*/
|
|
35
|
-
export const openMarket = async (chainId, inputToken, outputToken,
|
|
36
|
-
const market = await fetchMarket(chainId, [inputToken, outputToken]
|
|
34
|
+
export const openMarket = decorator(async ({ chainId, inputToken, outputToken, }) => {
|
|
35
|
+
const market = await fetchMarket(chainId, [inputToken, outputToken]);
|
|
37
36
|
const isBid = isAddressEqual(market.quote.address, inputToken);
|
|
38
37
|
if ((isBid && !market.bidBookOpen) || (!isBid && !market.askBookOpen)) {
|
|
39
|
-
const unit = await calculateUnit(chainId, isBid ? market.quote : market.base
|
|
38
|
+
const unit = await calculateUnit(chainId, isBid ? market.quote : market.base);
|
|
40
39
|
return buildTransaction(chainId, {
|
|
41
40
|
address: CONTRACT_ADDRESSES[chainId].Controller,
|
|
42
41
|
abi: CONTROLLER_ABI,
|
|
@@ -57,10 +56,10 @@ export const openMarket = async (chainId, inputToken, outputToken, options) => {
|
|
|
57
56
|
],
|
|
58
57
|
getDeadlineTimestampInSeconds(),
|
|
59
58
|
],
|
|
60
|
-
}
|
|
59
|
+
});
|
|
61
60
|
}
|
|
62
61
|
return undefined;
|
|
63
|
-
};
|
|
62
|
+
});
|
|
64
63
|
/**
|
|
65
64
|
* Places a limit order on the specified chain for trading tokens.
|
|
66
65
|
*
|
|
@@ -74,7 +73,8 @@ export const openMarket = async (chainId, inputToken, outputToken, options) => {
|
|
|
74
73
|
* @param {PermitSignature} [options.signature] The permit signature for token approval.
|
|
75
74
|
* @param {boolean} [options.postOnly] A boolean indicating whether the order is only to be made not taken.
|
|
76
75
|
* @param {string} [options.rpcUrl] The RPC URL of the blockchain.
|
|
77
|
-
* @returns {Promise<
|
|
76
|
+
* @returns {Promise<{ transaction: Transaction, result: { make: CurrencyFlow, take: CurrencyFlow } }>}
|
|
77
|
+
* Promise resolving to the transaction object representing the limit order with the result of the order.
|
|
78
78
|
* @example
|
|
79
79
|
* import { signERC20Permit, limitOrder } from '@clober/v2-sdk'
|
|
80
80
|
* import { privateKeyToAccount } from 'viem/accounts'
|
|
@@ -86,7 +86,7 @@ export const openMarket = async (chainId, inputToken, outputToken, options) => {
|
|
|
86
86
|
* '100.123'
|
|
87
87
|
* )
|
|
88
88
|
*
|
|
89
|
-
* const transaction = await limitOrder(
|
|
89
|
+
* const { transaction } = await limitOrder(
|
|
90
90
|
* 421614,
|
|
91
91
|
* '0xF8c1869Ecd4df136693C45EcE1b67f85B6bDaE69
|
|
92
92
|
* '0x00bfd44e79fb7f6dd5887a9426c8ef85a0cd23e0',
|
|
@@ -99,7 +99,7 @@ export const openMarket = async (chainId, inputToken, outputToken, options) => {
|
|
|
99
99
|
* @example
|
|
100
100
|
* import { limitOrder } from '@clober/v2-sdk'
|
|
101
101
|
*
|
|
102
|
-
* const transaction = await limitOrder(
|
|
102
|
+
* const { transaction } = await limitOrder(
|
|
103
103
|
* 421614,
|
|
104
104
|
* '0xF8c1869Ecd4df136693C45EcE1b67f85B6bDaE69
|
|
105
105
|
* '0x0000000000000000000000000000000000000000',
|
|
@@ -108,16 +108,12 @@ export const openMarket = async (chainId, inputToken, outputToken, options) => {
|
|
|
108
108
|
* '4000.01', // price at 4000.01 (ETH/USDC)
|
|
109
109
|
* )
|
|
110
110
|
*/
|
|
111
|
-
export const limitOrder = async (chainId, userAddress, inputToken, outputToken, amount, price, options) => {
|
|
112
|
-
const
|
|
113
|
-
signature: undefined,
|
|
114
|
-
postOnly: false,
|
|
115
|
-
rpcUrl: undefined,
|
|
116
|
-
};
|
|
117
|
-
const market = await fetchMarket(chainId, [inputToken, outputToken], rpcUrl);
|
|
111
|
+
export const limitOrder = decorator(async ({ chainId, userAddress, inputToken, outputToken, amount, price, options, }) => {
|
|
112
|
+
const market = await fetchMarket(chainId, [inputToken, outputToken]);
|
|
118
113
|
const isBid = isAddressEqual(market.quote.address, inputToken);
|
|
119
114
|
if ((isBid && !market.bidBookOpen) || (!isBid && !market.askBookOpen)) {
|
|
120
115
|
throw new Error(`
|
|
116
|
+
Open the market before placing a limit order.
|
|
121
117
|
import { openMarket } from '@clober/v2-sdk'
|
|
122
118
|
|
|
123
119
|
const transaction = await openMarket(
|
|
@@ -131,24 +127,25 @@ export const limitOrder = async (chainId, userAddress, inputToken, outputToken,
|
|
|
131
127
|
const tick = isBid ? fromPrice(rawPrice) : fromPrice(invertPrice(rawPrice));
|
|
132
128
|
const tokensToSettle = [inputToken, outputToken].filter((address) => !isAddressEqual(address, zeroAddress));
|
|
133
129
|
const quoteAmount = parseUnits(amount, isBid ? market.quote.decimals : market.base.decimals);
|
|
134
|
-
const [unit, {
|
|
135
|
-
calculateUnit(chainId, isBid ? market.quote : market.base
|
|
136
|
-
getExpectedOutput(
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
: {
|
|
142
|
-
|
|
143
|
-
}
|
|
130
|
+
const [unit, { spendAmount, bookId }] = await Promise.all([
|
|
131
|
+
calculateUnit(chainId, isBid ? market.quote : market.base),
|
|
132
|
+
getExpectedOutput({
|
|
133
|
+
chainId,
|
|
134
|
+
inputToken,
|
|
135
|
+
outputToken,
|
|
136
|
+
amountIn: amount,
|
|
137
|
+
options: {
|
|
138
|
+
...options,
|
|
139
|
+
},
|
|
140
|
+
}),
|
|
144
141
|
]);
|
|
145
142
|
const isETH = isAddressEqual(inputToken, zeroAddress);
|
|
146
|
-
const permitParamsList = signature && !isETH
|
|
143
|
+
const permitParamsList = options?.signature && !isETH
|
|
147
144
|
? [
|
|
148
145
|
{
|
|
149
146
|
token: inputToken,
|
|
150
147
|
permitAmount: quoteAmount,
|
|
151
|
-
signature,
|
|
148
|
+
signature: options.signature,
|
|
152
149
|
},
|
|
153
150
|
]
|
|
154
151
|
: [];
|
|
@@ -158,93 +155,78 @@ export const limitOrder = async (chainId, userAddress, inputToken, outputToken,
|
|
|
158
155
|
quoteAmount,
|
|
159
156
|
hookData: zeroHash,
|
|
160
157
|
};
|
|
161
|
-
if (postOnly === true ||
|
|
162
|
-
return
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
[
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
value: isETH ? quoteAmount : 0n,
|
|
175
|
-
}, options?.rpcUrl);
|
|
176
|
-
}
|
|
177
|
-
else if (result.length === 1) {
|
|
178
|
-
// take and make
|
|
179
|
-
return buildTransaction(chainId, {
|
|
180
|
-
chain: CHAIN_MAP[chainId],
|
|
181
|
-
account: userAddress,
|
|
182
|
-
address: CONTRACT_ADDRESSES[chainId].Controller,
|
|
183
|
-
abi: CONTROLLER_ABI,
|
|
184
|
-
functionName: 'limit',
|
|
185
|
-
args: [
|
|
186
|
-
[
|
|
187
|
-
{
|
|
188
|
-
takeBookId: result[0].bookId,
|
|
189
|
-
makeBookId: makeParam.id,
|
|
190
|
-
limitPrice: rawPrice,
|
|
191
|
-
tick: makeParam.tick,
|
|
192
|
-
quoteAmount,
|
|
193
|
-
takeHookData: zeroHash,
|
|
194
|
-
makeHookData: makeParam.hookData,
|
|
195
|
-
},
|
|
158
|
+
if (options?.postOnly === true || spendAmount === '0') {
|
|
159
|
+
return {
|
|
160
|
+
transaction: await buildTransaction(chainId, {
|
|
161
|
+
chain: CHAIN_MAP[chainId],
|
|
162
|
+
account: userAddress,
|
|
163
|
+
address: CONTRACT_ADDRESSES[chainId].Controller,
|
|
164
|
+
abi: CONTROLLER_ABI,
|
|
165
|
+
functionName: 'make',
|
|
166
|
+
args: [
|
|
167
|
+
[makeParam],
|
|
168
|
+
tokensToSettle,
|
|
169
|
+
permitParamsList,
|
|
170
|
+
getDeadlineTimestampInSeconds(),
|
|
196
171
|
],
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
172
|
+
value: isETH ? quoteAmount : 0n,
|
|
173
|
+
}),
|
|
174
|
+
result: {
|
|
175
|
+
make: {
|
|
176
|
+
amount: formatUnits(quoteAmount, isBid ? market.quote.decimals : market.base.decimals),
|
|
177
|
+
currency: isBid ? market.quote : market.base,
|
|
178
|
+
direction: 'in',
|
|
179
|
+
},
|
|
180
|
+
take: {
|
|
181
|
+
amount: '0',
|
|
182
|
+
currency: isBid ? market.base : market.quote,
|
|
183
|
+
direction: 'out',
|
|
184
|
+
},
|
|
185
|
+
},
|
|
186
|
+
};
|
|
203
187
|
}
|
|
204
188
|
else {
|
|
205
|
-
// take
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
[
|
|
216
|
-
...result.map(() => Action.TAKE),
|
|
217
|
-
...(makeAmount > 0n ? [Action.MAKE] : []),
|
|
218
|
-
],
|
|
219
|
-
[
|
|
220
|
-
...result.map(({ bookId, takenAmount }) => encodeAbiParameters(TAKE_ORDER_PARAMS_ABI, [
|
|
189
|
+
// take and make
|
|
190
|
+
return {
|
|
191
|
+
transaction: await buildTransaction(chainId, {
|
|
192
|
+
chain: CHAIN_MAP[chainId],
|
|
193
|
+
account: userAddress,
|
|
194
|
+
address: CONTRACT_ADDRESSES[chainId].Controller,
|
|
195
|
+
abi: CONTROLLER_ABI,
|
|
196
|
+
functionName: 'limit',
|
|
197
|
+
args: [
|
|
198
|
+
[
|
|
221
199
|
{
|
|
222
|
-
|
|
200
|
+
takeBookId: bookId,
|
|
201
|
+
makeBookId: makeParam.id,
|
|
223
202
|
limitPrice: rawPrice,
|
|
224
|
-
|
|
225
|
-
|
|
203
|
+
tick: makeParam.tick,
|
|
204
|
+
quoteAmount,
|
|
205
|
+
takeHookData: zeroHash,
|
|
206
|
+
makeHookData: makeParam.hookData,
|
|
226
207
|
},
|
|
227
|
-
]
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
{
|
|
232
|
-
...makeParam,
|
|
233
|
-
quoteAmount: makeAmount,
|
|
234
|
-
},
|
|
235
|
-
]),
|
|
236
|
-
]
|
|
237
|
-
: []),
|
|
208
|
+
],
|
|
209
|
+
tokensToSettle,
|
|
210
|
+
permitParamsList,
|
|
211
|
+
getDeadlineTimestampInSeconds(),
|
|
238
212
|
],
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
213
|
+
value: isETH ? quoteAmount : 0n,
|
|
214
|
+
}),
|
|
215
|
+
result: {
|
|
216
|
+
make: {
|
|
217
|
+
amount: formatUnits(quoteAmount, isBid ? market.quote.decimals : market.base.decimals),
|
|
218
|
+
currency: isBid ? market.quote : market.base,
|
|
219
|
+
direction: 'in',
|
|
220
|
+
},
|
|
221
|
+
take: {
|
|
222
|
+
amount: spendAmount,
|
|
223
|
+
currency: isBid ? market.base : market.quote,
|
|
224
|
+
direction: 'out',
|
|
225
|
+
},
|
|
226
|
+
},
|
|
227
|
+
};
|
|
246
228
|
}
|
|
247
|
-
};
|
|
229
|
+
});
|
|
248
230
|
/**
|
|
249
231
|
* Executes a market order on the specified chain for trading tokens.
|
|
250
232
|
*
|
|
@@ -290,16 +272,12 @@ export const limitOrder = async (chainId, userAddress, inputToken, outputToken,
|
|
|
290
272
|
* '0.13', // 0.13 ETH
|
|
291
273
|
* )
|
|
292
274
|
*/
|
|
293
|
-
export const marketOrder = async (chainId, userAddress, inputToken, outputToken, amount, options) => {
|
|
294
|
-
const
|
|
295
|
-
signature: undefined,
|
|
296
|
-
rpcUrl: undefined,
|
|
297
|
-
limitPrice: undefined,
|
|
298
|
-
};
|
|
299
|
-
const market = await fetchMarket(chainId, [inputToken, outputToken], rpcUrl);
|
|
275
|
+
export const marketOrder = decorator(async ({ chainId, userAddress, inputToken, outputToken, amount, options, }) => {
|
|
276
|
+
const market = await fetchMarket(chainId, [inputToken, outputToken]);
|
|
300
277
|
const isBid = isAddressEqual(market.quote.address, inputToken);
|
|
301
278
|
if ((isBid && !market.bidBookOpen) || (!isBid && !market.askBookOpen)) {
|
|
302
279
|
throw new Error(`
|
|
280
|
+
Open the market before placing a market order.
|
|
303
281
|
import { openMarket } from '@clober/v2-sdk'
|
|
304
282
|
|
|
305
283
|
const transaction = await openMarket(
|
|
@@ -309,21 +287,25 @@ export const marketOrder = async (chainId, userAddress, inputToken, outputToken,
|
|
|
309
287
|
)
|
|
310
288
|
`);
|
|
311
289
|
}
|
|
312
|
-
const rawLimitPrice = parsePrice(Number(limitPrice ?? '0'), market.quote.decimals, market.base.decimals);
|
|
290
|
+
const rawLimitPrice = parsePrice(Number(options?.limitPrice ?? '0'), market.quote.decimals, market.base.decimals);
|
|
313
291
|
const tokensToSettle = [inputToken, outputToken].filter((address) => !isAddressEqual(address, zeroAddress));
|
|
314
292
|
const quoteAmount = parseUnits(amount, isBid ? market.quote.decimals : market.base.decimals);
|
|
315
|
-
const {
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
:
|
|
293
|
+
const { bookId, takenAmount } = await getExpectedOutput({
|
|
294
|
+
chainId,
|
|
295
|
+
inputToken,
|
|
296
|
+
outputToken,
|
|
297
|
+
amountIn: amount,
|
|
298
|
+
options: {
|
|
299
|
+
...options,
|
|
300
|
+
},
|
|
301
|
+
});
|
|
320
302
|
const isETH = isAddressEqual(inputToken, zeroAddress);
|
|
321
|
-
const permitParamsList = signature && !isETH
|
|
303
|
+
const permitParamsList = options?.signature && !isETH
|
|
322
304
|
? [
|
|
323
305
|
{
|
|
324
306
|
token: inputToken,
|
|
325
307
|
permitAmount: quoteAmount,
|
|
326
|
-
signature,
|
|
308
|
+
signature: options.signature,
|
|
327
309
|
},
|
|
328
310
|
]
|
|
329
311
|
: [];
|
|
@@ -334,19 +316,21 @@ export const marketOrder = async (chainId, userAddress, inputToken, outputToken,
|
|
|
334
316
|
abi: CONTROLLER_ABI,
|
|
335
317
|
functionName: 'take',
|
|
336
318
|
args: [
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
319
|
+
[
|
|
320
|
+
{
|
|
321
|
+
id: bookId,
|
|
322
|
+
limitPrice: isBid ? invertPrice(rawLimitPrice) : rawLimitPrice,
|
|
323
|
+
quoteAmount: takenAmount,
|
|
324
|
+
hookData: zeroHash,
|
|
325
|
+
},
|
|
326
|
+
],
|
|
343
327
|
tokensToSettle,
|
|
344
328
|
permitParamsList,
|
|
345
329
|
getDeadlineTimestampInSeconds(),
|
|
346
330
|
],
|
|
347
331
|
value: isETH ? quoteAmount : 0n,
|
|
348
|
-
}
|
|
349
|
-
};
|
|
332
|
+
});
|
|
333
|
+
});
|
|
350
334
|
/**
|
|
351
335
|
* Claims specified open order for settlement.
|
|
352
336
|
* [IMPORTANT] Set ApprovalForAll before calling this function.
|
|
@@ -356,7 +340,8 @@ export const marketOrder = async (chainId, userAddress, inputToken, outputToken,
|
|
|
356
340
|
* @param {string} id An ID representing the open order to be claimed.
|
|
357
341
|
* @param {Object} [options] Optional parameters for claiming orders.
|
|
358
342
|
* @param {string} [options.rpcUrl] The RPC URL to use for executing the transaction.
|
|
359
|
-
* @returns {Promise<
|
|
343
|
+
* @returns {Promise<{ transaction: Transaction, result: CurrencyFlow }>}
|
|
344
|
+
* Promise resolving to the transaction object representing the claim action with the result of the order.
|
|
360
345
|
* @throws {Error} Throws an error if no open orders are found for the specified user.
|
|
361
346
|
* @example
|
|
362
347
|
* import { getOpenOrders, claimOrders } from '@clober/v2-sdk'
|
|
@@ -371,9 +356,18 @@ export const marketOrder = async (chainId, userAddress, inputToken, outputToken,
|
|
|
371
356
|
* openOrders.map((order) => order.id)
|
|
372
357
|
* )
|
|
373
358
|
*/
|
|
374
|
-
export const claimOrder = async (chainId, userAddress, id, options) => {
|
|
375
|
-
|
|
376
|
-
|
|
359
|
+
export const claimOrder = decorator(async ({ chainId, userAddress, id, options, }) => {
|
|
360
|
+
const { transaction, result } = await claimOrders({
|
|
361
|
+
chainId,
|
|
362
|
+
userAddress,
|
|
363
|
+
ids: [id],
|
|
364
|
+
options: { ...options },
|
|
365
|
+
});
|
|
366
|
+
return {
|
|
367
|
+
transaction,
|
|
368
|
+
result: result[0],
|
|
369
|
+
};
|
|
370
|
+
});
|
|
377
371
|
/**
|
|
378
372
|
* Claims specified open orders for settlement.
|
|
379
373
|
* [IMPORTANT] Set ApprovalForAll before calling this function.
|
|
@@ -383,7 +377,8 @@ export const claimOrder = async (chainId, userAddress, id, options) => {
|
|
|
383
377
|
* @param {string[]} ids An array of IDs representing the open orders to be claimed.
|
|
384
378
|
* @param {Object} [options] Optional parameters for claiming orders.
|
|
385
379
|
* @param {string} [options.rpcUrl] The RPC URL to use for executing the transaction.
|
|
386
|
-
* @returns {Promise<
|
|
380
|
+
* @returns {Promise<{ transaction: Transaction, result: CurrencyFlow[] }>}
|
|
381
|
+
* Promise resolving to the transaction object representing the claim action with the result of the orders.
|
|
387
382
|
* @throws {Error} Throws an error if no open orders are found for the specified user.
|
|
388
383
|
* @example
|
|
389
384
|
* import { getOpenOrders, claimOrders } from '@clober/v2-sdk'
|
|
@@ -398,13 +393,11 @@ export const claimOrder = async (chainId, userAddress, id, options) => {
|
|
|
398
393
|
* openOrders.map((order) => order.id)
|
|
399
394
|
* )
|
|
400
395
|
*/
|
|
401
|
-
export const claimOrders = async (chainId, userAddress, ids, options) => {
|
|
402
|
-
const
|
|
403
|
-
rpcUrl: undefined,
|
|
404
|
-
};
|
|
405
|
-
const isApprovedForAll = await fetchIsApprovedForAll(chainId, userAddress, options?.rpcUrl);
|
|
396
|
+
export const claimOrders = decorator(async ({ chainId, userAddress, ids, options, }) => {
|
|
397
|
+
const isApprovedForAll = await fetchIsApprovedForAll(chainId, userAddress);
|
|
406
398
|
if (!isApprovedForAll) {
|
|
407
399
|
throw new Error(`
|
|
400
|
+
Set ApprovalForAll before calling this function.
|
|
408
401
|
import { setApprovalOfOpenOrdersForAll } from '@clober/v2-sdk'
|
|
409
402
|
|
|
410
403
|
const hash = await setApprovalOfOpenOrdersForAll(
|
|
@@ -413,32 +406,42 @@ export const claimOrders = async (chainId, userAddress, ids, options) => {
|
|
|
413
406
|
)
|
|
414
407
|
`);
|
|
415
408
|
}
|
|
416
|
-
const openOrders = (await getOpenOrders(chainId, userAddress,
|
|
409
|
+
const openOrders = (await getOpenOrders({ chainId, userAddress, options: { ...options } })).filter((order) => ids.includes(order.id));
|
|
417
410
|
if (openOrders.length === 0) {
|
|
418
411
|
throw new Error(`No claimable open orders found for ${userAddress}`);
|
|
419
412
|
}
|
|
420
413
|
const tokensToSettle = openOrders
|
|
421
|
-
.map((order) => [
|
|
414
|
+
.map((order) => [
|
|
415
|
+
order.outputCurrency.address,
|
|
416
|
+
order.inputCurrency.address,
|
|
417
|
+
])
|
|
422
418
|
.flat()
|
|
423
419
|
.filter((address, index, self) => self.findIndex((c) => isAddressEqual(c, address)) === index)
|
|
424
420
|
.filter((address) => !isAddressEqual(address, zeroAddress));
|
|
425
|
-
return
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
}
|
|
421
|
+
return {
|
|
422
|
+
transaction: await buildTransaction(chainId, {
|
|
423
|
+
chain: CHAIN_MAP[chainId],
|
|
424
|
+
account: userAddress,
|
|
425
|
+
address: CONTRACT_ADDRESSES[chainId].Controller,
|
|
426
|
+
abi: CONTROLLER_ABI,
|
|
427
|
+
functionName: 'claim',
|
|
428
|
+
args: [
|
|
429
|
+
openOrders.map((order) => ({
|
|
430
|
+
id: BigInt(order.id),
|
|
431
|
+
hookData: zeroHash,
|
|
432
|
+
})),
|
|
433
|
+
tokensToSettle,
|
|
434
|
+
[],
|
|
435
|
+
getDeadlineTimestampInSeconds(),
|
|
436
|
+
],
|
|
437
|
+
}),
|
|
438
|
+
result: openOrders.map((order) => ({
|
|
439
|
+
currency: order.claimable.currency,
|
|
440
|
+
amount: order.claimable.value,
|
|
441
|
+
direction: 'out',
|
|
442
|
+
})),
|
|
443
|
+
};
|
|
444
|
+
});
|
|
442
445
|
/**
|
|
443
446
|
* Cancels specified open order if the order is not fully filled.
|
|
444
447
|
* [IMPORTANT] Set ApprovalForAll before calling this function.
|
|
@@ -448,7 +451,8 @@ export const claimOrders = async (chainId, userAddress, ids, options) => {
|
|
|
448
451
|
* @param {string} id An ID representing the open order to be canceled
|
|
449
452
|
* @param {Object} [options] Optional parameters for canceling orders.
|
|
450
453
|
* @param {string} [options.rpcUrl] The RPC URL to use for executing the transaction.
|
|
451
|
-
* @returns {Promise<
|
|
454
|
+
* @returns {Promise<{ transaction: Transaction, result: CurrencyFlow }>}
|
|
455
|
+
* Promise resolving to the transaction object representing the cancel action with the result of the order.
|
|
452
456
|
* @throws {Error} Throws an error if no open orders are found for the specified user.
|
|
453
457
|
* @example
|
|
454
458
|
* import { getOpenOrders, cancelOrders } from '@clober/v2-sdk'
|
|
@@ -463,9 +467,18 @@ export const claimOrders = async (chainId, userAddress, ids, options) => {
|
|
|
463
467
|
* openOrders.map((order) => order.id)
|
|
464
468
|
* )
|
|
465
469
|
*/
|
|
466
|
-
export const cancelOrder = async (chainId, userAddress, id, options) => {
|
|
467
|
-
|
|
468
|
-
|
|
470
|
+
export const cancelOrder = decorator(async ({ chainId, userAddress, id, options, }) => {
|
|
471
|
+
const { transaction, result } = await cancelOrders({
|
|
472
|
+
chainId,
|
|
473
|
+
userAddress,
|
|
474
|
+
ids: [id],
|
|
475
|
+
options: { ...options },
|
|
476
|
+
});
|
|
477
|
+
return {
|
|
478
|
+
transaction,
|
|
479
|
+
result: result[0],
|
|
480
|
+
};
|
|
481
|
+
});
|
|
469
482
|
/**
|
|
470
483
|
* Cancels specified open orders if orders are not fully filled.
|
|
471
484
|
* [IMPORTANT] Set ApprovalForAll before calling this function.
|
|
@@ -475,7 +488,8 @@ export const cancelOrder = async (chainId, userAddress, id, options) => {
|
|
|
475
488
|
* @param {string[]} ids An array of IDs representing the open orders to be canceled.
|
|
476
489
|
* @param {Object} [options] Optional parameters for canceling orders.
|
|
477
490
|
* @param {string} [options.rpcUrl] The RPC URL to use for executing the transaction.
|
|
478
|
-
* @returns {Promise<
|
|
491
|
+
* @returns {Promise<{ transaction: Transaction, result: CurrencyFlow[] }>
|
|
492
|
+
* Promise resolving to the transaction object representing the cancel action with the result of the orders.
|
|
479
493
|
* @throws {Error} Throws an error if no open orders are found for the specified user.
|
|
480
494
|
* @example
|
|
481
495
|
* import { getOpenOrders, cancelOrders } from '@clober/v2-sdk'
|
|
@@ -490,13 +504,11 @@ export const cancelOrder = async (chainId, userAddress, id, options) => {
|
|
|
490
504
|
* openOrders.map((order) => order.id)
|
|
491
505
|
* )
|
|
492
506
|
*/
|
|
493
|
-
export const cancelOrders = async (chainId, userAddress, ids, options) => {
|
|
494
|
-
const
|
|
495
|
-
rpcUrl: undefined,
|
|
496
|
-
};
|
|
497
|
-
const isApprovedForAll = await fetchIsApprovedForAll(chainId, userAddress, options?.rpcUrl);
|
|
507
|
+
export const cancelOrders = decorator(async ({ chainId, userAddress, ids, options, }) => {
|
|
508
|
+
const isApprovedForAll = await fetchIsApprovedForAll(chainId, userAddress);
|
|
498
509
|
if (!isApprovedForAll) {
|
|
499
510
|
throw new Error(`
|
|
511
|
+
Set ApprovalForAll before calling this function.
|
|
500
512
|
import { setApprovalOfOpenOrdersForAll } from '@clober/v2-sdk'
|
|
501
513
|
|
|
502
514
|
const hash = await setApprovalOfOpenOrdersForAll(
|
|
@@ -505,31 +517,41 @@ export const cancelOrders = async (chainId, userAddress, ids, options) => {
|
|
|
505
517
|
)
|
|
506
518
|
`);
|
|
507
519
|
}
|
|
508
|
-
const openOrders = (await getOpenOrders(chainId, userAddress,
|
|
520
|
+
const openOrders = (await getOpenOrders({ chainId, userAddress, options: { ...options } })).filter((order) => ids.includes(order.id) && order.claimable.value !== '0');
|
|
509
521
|
if (openOrders.length === 0) {
|
|
510
522
|
throw new Error(`No cancelable open orders found for ${userAddress}`);
|
|
511
523
|
}
|
|
512
524
|
const tokensToSettle = openOrders
|
|
513
|
-
.map((order) => [
|
|
525
|
+
.map((order) => [
|
|
526
|
+
order.outputCurrency.address,
|
|
527
|
+
order.inputCurrency.address,
|
|
528
|
+
])
|
|
514
529
|
.flat()
|
|
515
530
|
.filter((address, index, self) => self.findIndex((c) => isAddressEqual(c, address)) === index)
|
|
516
531
|
.filter((address) => !isAddressEqual(address, zeroAddress));
|
|
517
|
-
return
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
}
|
|
532
|
+
return {
|
|
533
|
+
transaction: await buildTransaction(chainId, {
|
|
534
|
+
chain: CHAIN_MAP[chainId],
|
|
535
|
+
account: userAddress,
|
|
536
|
+
address: CONTRACT_ADDRESSES[chainId].Controller,
|
|
537
|
+
abi: CONTROLLER_ABI,
|
|
538
|
+
functionName: 'cancel',
|
|
539
|
+
args: [
|
|
540
|
+
openOrders.map((order) => ({
|
|
541
|
+
id: BigInt(order.id),
|
|
542
|
+
leftQuoteAmount: 0n,
|
|
543
|
+
hookData: zeroHash,
|
|
544
|
+
})),
|
|
545
|
+
tokensToSettle,
|
|
546
|
+
[],
|
|
547
|
+
getDeadlineTimestampInSeconds(),
|
|
548
|
+
],
|
|
549
|
+
}),
|
|
550
|
+
result: openOrders.map((order) => ({
|
|
551
|
+
currency: order.cancelable.currency,
|
|
552
|
+
amount: order.cancelable.value,
|
|
553
|
+
direction: 'out',
|
|
554
|
+
})),
|
|
555
|
+
};
|
|
556
|
+
});
|
|
535
557
|
//# sourceMappingURL=call.js.map
|