Skip to content

Commit 4a0bb47

Browse files
authored
Merge pull request #28 from clober-dex/fix/limit-result
Fix/limit result
2 parents daee7ff + 0a957ba commit 4a0bb47

File tree

3 files changed

+37
-13
lines changed

3 files changed

+37
-13
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@clober/v2-sdk",
3-
"version": "0.0.18",
3+
"version": "0.0.19",
44
"description": "🛠 An SDK for building applications on top of Clober V2",
55
"files": [
66
"dist"

src/call.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ export const openMarket = decorator(
111111
* @param {string} [options.rpcUrl] The RPC URL of the blockchain.
112112
* @param {number} [options.gasLimit] The gas limit to use for the transaction.
113113
* @param {boolean} [options.useSubgraph] A boolean indicating whether to use the subgraph for fetching orders.
114-
* @returns {Promise<{ transaction: Transaction, result: { make: CurrencyFlow, take: CurrencyFlow } }>}
114+
* @returns {Promise<{ transaction: Transaction, result: { make: CurrencyFlow, take: CurrencyFlow, spent: CurrencyFlow }>}
115115
* Promise resolving to the transaction object representing the limit order with the result of the order.
116116
* @example
117117
* import { signERC20Permit, limitOrder } from '@clober/v2-sdk'
@@ -171,6 +171,7 @@ export const limitOrder = decorator(
171171
result: {
172172
make: CurrencyFlow
173173
taken: CurrencyFlow
174+
spent: CurrencyFlow
174175
}
175176
}> => {
176177
const market = await fetchMarket(chainId, [inputToken, outputToken])
@@ -201,7 +202,7 @@ export const limitOrder = decorator(
201202
(address) => !isAddressEqual(address, zeroAddress),
202203
)
203204
const quoteAmount = parseUnits(amount, inputCurrency.decimals)
204-
const [unit, { spentAmount, bookId }] = await Promise.all([
205+
const [unit, { takenAmount, spentAmount, bookId }] = await Promise.all([
205206
calculateUnit(chainId, inputCurrency),
206207
getExpectedOutput({
207208
chainId,
@@ -247,6 +248,11 @@ export const limitOrder = decorator(
247248
currency: inputCurrency,
248249
direction: 'in',
249250
},
251+
spent: {
252+
amount: '0',
253+
currency: inputCurrency,
254+
direction: 'out',
255+
},
250256
taken: {
251257
amount: '0',
252258
currency: outputCurrency,
@@ -287,12 +293,20 @@ export const limitOrder = decorator(
287293
),
288294
result: {
289295
make: {
290-
amount: formatUnits(quoteAmount, inputCurrency.decimals),
296+
amount: formatUnits(
297+
quoteAmount - parseUnits(spentAmount, inputCurrency.decimals),
298+
inputCurrency.decimals,
299+
),
291300
currency: inputCurrency,
292301
direction: 'in',
293302
},
294-
taken: {
303+
spent: {
295304
amount: spentAmount,
305+
currency: inputCurrency,
306+
direction: 'out',
307+
},
308+
taken: {
309+
amount: takenAmount,
296310
currency: outputCurrency,
297311
direction: 'out',
298312
},

test/limit-order.test.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ test('make bid order', async () => {
6262
})
6363
const {
6464
transaction,
65-
result: { make, taken },
65+
result: { make, taken, spent },
6666
} = await limitOrder({
6767
chainId: cloberTestChain.id,
6868
userAddress: account.address,
@@ -128,6 +128,10 @@ test('make bid order', async () => {
128128
expect(make.currency.address).toEqual(
129129
getAddress('0x00bfd44e79fb7f6dd5887a9426c8ef85a0cd23e0'),
130130
)
131+
expect(spent.amount).toEqual('0')
132+
expect(spent.currency.address).toEqual(
133+
getAddress('0x00bfd44e79fb7f6dd5887a9426c8ef85a0cd23e0'),
134+
)
131135
expect(taken.amount).toEqual('0')
132136
expect(taken.currency.address).toEqual(
133137
'0x0000000000000000000000000000000000000000',
@@ -140,7 +144,7 @@ test('make ask order', async () => {
140144

141145
const {
142146
transaction,
143-
result: { make, taken },
147+
result: { make, taken, spent },
144148
} = await limitOrder({
145149
chainId: cloberTestChain.id,
146150
userAddress: account.address,
@@ -202,6 +206,10 @@ test('make ask order', async () => {
202206
expect(make.currency.address).toEqual(
203207
'0x0000000000000000000000000000000000000000',
204208
)
209+
expect(spent.amount).toEqual('0')
210+
expect(spent.currency.address).toEqual(
211+
'0x0000000000000000000000000000000000000000',
212+
)
205213
expect(taken.amount).toEqual('0')
206214
expect(taken.currency.address).toEqual(
207215
getAddress('0x00bfd44e79fb7f6dd5887a9426c8ef85a0cd23e0'),
@@ -245,7 +253,7 @@ test('limit bid order', async () => {
245253
})
246254
const {
247255
transaction,
248-
result: { make, taken },
256+
result: { make, taken, spent },
249257
} = await limitOrder({
250258
chainId: cloberTestChain.id,
251259
userAddress: account.address,
@@ -298,14 +306,15 @@ test('limit bid order', async () => {
298306
).toEqual(28440764694968336000)
299307
expect(afterMarket.asks.length).toEqual(beforeMarket.asks.length - 1)
300308
expect(afterMarket.bids.length).toEqual(beforeMarket.bids.length + 1)
301-
expect(make.amount).toEqual('100000')
309+
expect(make.amount).toEqual('99649.86826')
302310
expect(make.currency.address).toEqual(
303311
getAddress('0x00bfd44e79fb7f6dd5887a9426c8ef85a0cd23e0'),
304312
)
305-
expect(taken.amount).toEqual('350.13174')
313+
expect(taken.amount).toEqual('0.09992997')
306314
expect(taken.currency.address).toEqual(
307315
'0x0000000000000000000000000000000000000000',
308316
)
317+
expect(Number(make.amount) + Number(spent.amount)).toEqual(100000)
309318
})
310319

311320
test('limit ask order', async () => {
@@ -322,7 +331,7 @@ test('limit ask order', async () => {
322331

323332
const {
324333
transaction,
325-
result: { make, taken },
334+
result: { make, taken, spent },
326335
} = await limitOrder({
327336
chainId: cloberTestChain.id,
328337
userAddress: account.address,
@@ -396,12 +405,13 @@ test('limit ask order', async () => {
396405
).toEqual(1008553000000000000)
397406
expect(afterMarket.bids.length).toEqual(beforeMarket.bids.length - 1)
398407
expect(afterMarket.asks.length).toEqual(beforeMarket.asks.length + 1)
399-
expect(make.amount).toEqual('2')
408+
expect(make.amount).toEqual('1.008250484573137286')
400409
expect(make.currency.address).toEqual(
401410
'0x0000000000000000000000000000000000000000',
402411
)
403-
expect(taken.amount).toEqual('0.991749515426862714')
412+
expect(taken.amount).toEqual('3467.57027')
404413
expect(taken.currency.address).toEqual(
405414
getAddress('0x00bfd44e79fb7f6dd5887a9426c8ef85a0cd23e0'),
406415
)
416+
expect(Number(make.amount) + Number(spent.amount)).toEqual(2)
407417
})

0 commit comments

Comments
 (0)