Skip to content

Commit

Permalink
Merge pull request #9 from clober-dex/fix/limit
Browse files Browse the repository at this point in the history
Fix/limit
  • Loading branch information
graykode authored Apr 15, 2024
2 parents d2a517d + c47959c commit d9244af
Show file tree
Hide file tree
Showing 4 changed files with 241 additions and 43 deletions.
4 changes: 3 additions & 1 deletion src/call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ export const limitOrder = decorator(
amountIn: amount,
options: {
...options,
limitPrice: price,
},
}),
])
Expand Down Expand Up @@ -268,7 +269,7 @@ export const limitOrder = decorator(
{
takeBookId: bookId,
makeBookId: makeParam.id,
limitPrice: rawPrice,
limitPrice: isBid ? invertPrice(rawPrice) : rawPrice,
tick: makeParam.tick,
quoteAmount,
takeHookData: zeroHash,
Expand Down Expand Up @@ -399,6 +400,7 @@ export const marketOrder = decorator(
amountIn: amount,
options: {
...options,
// todo: pass limit price
},
})
const isETH = isAddressEqual(inputToken, zeroAddress)
Expand Down
28 changes: 18 additions & 10 deletions test/limit-order.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ test('make ask order', async () => {
userAddress: account.address,
inputToken: '0x0000000000000000000000000000000000000000',
outputToken: '0x00bfd44e79fb7f6dd5887a9426c8ef85a0cd23e0',
amount: '0.0015',
amount: '0.15',
price: '8000.01',
options: {
rpcUrl: publicClient.transport.url!,
Expand Down Expand Up @@ -193,12 +193,12 @@ test('make ask order', async () => {
}),
])

expect(beforeBalance - afterBalance).toEqual(2048655440677668n)
expect(Number(beforeBalance - afterBalance)).greaterThan(150000000000000000)
expect(
getSize(afterMarket.asks, 8000, 8001) -
getSize(beforeMarket.asks, 8000, 8001),
).toEqual(1500000000000000)
expect(make.amount).toEqual('0.0015')
).toEqual(150045000000000000)
expect(make.amount).toEqual('0.15')
expect(make.currency.address).toEqual(
'0x0000000000000000000000000000000000000000',
)
Expand Down Expand Up @@ -284,17 +284,21 @@ test('limit bid order', async () => {
}),
])

expect(beforeUSDCBalance - afterUSDCBalance).toEqual(100000000000n) // todo: check result
expect(afterETHBalance - beforeETHBalance).toEqual(1327704033001898034n)
expect(beforeUSDCBalance - afterUSDCBalance).toEqual(100000000000n)
expect(Number(afterETHBalance - beforeETHBalance)).lessThan(
100000000000000000,
)
expect(
getSize(afterMarket.bids, 3504, 3505) -
getSize(beforeMarket.bids, 3504, 3505),
).toEqual(27061899601333555000)
).toEqual(28440764694968336000)
expect(afterMarket.asks.length).toEqual(beforeMarket.asks.length - 1)
expect(afterMarket.bids.length).toEqual(beforeMarket.bids.length + 1)
expect(make.amount).toEqual('100000')
expect(make.currency.address).toEqual(
getAddress('0x00bfd44e79fb7f6dd5887a9426c8ef85a0cd23e0'),
)
expect(take.amount).toEqual('5181.356442')
expect(take.amount).toEqual('350.13174')
expect(take.currency.address).toEqual(
'0x0000000000000000000000000000000000000000',
)
Expand Down Expand Up @@ -375,17 +379,21 @@ test('limit ask order', async () => {
}),
])

expect(beforeETHBalance - afterETHBalance).toEqual(2000959595585013014n)
expect(Number(beforeETHBalance - afterETHBalance)).greaterThan(
2000000000000000000,
)
expect(afterUSDCBalance - beforeUSDCBalance).toEqual(3467570270n)
expect(
getSize(afterMarket.asks, 3450, 3451) -
getSize(beforeMarket.asks, 3450, 3451),
).toEqual(1008553000000000000)
expect(afterMarket.bids.length).toEqual(beforeMarket.bids.length - 1)
expect(afterMarket.asks.length).toEqual(beforeMarket.asks.length + 1)
expect(make.amount).toEqual('2')
expect(make.currency.address).toEqual(
'0x0000000000000000000000000000000000000000',
)
expect(take.amount).toEqual('1.999999999746300601')
expect(take.amount).toEqual('0.991749515426862714')
expect(take.currency.address).toEqual(
getAddress('0x00bfd44e79fb7f6dd5887a9426c8ef85a0cd23e0'),
)
Expand Down
185 changes: 153 additions & 32 deletions test/open-order.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,42 @@
import { expect, test } from 'vitest'
import { afterEach, expect, test } from 'vitest'
import {
getOpenOrders,
getOpenOrder,
claimOrders,
setApprovalOfOpenOrdersForAll,
cancelOrders,
signERC20Permit,
limitOrder,
claimOrder,
} from '@clober/v2-sdk'

import { buildPublicClient } from '../src/constants/client'

import { cloberTestChain } from './utils/test-chain'
import { account, publicClient, walletClient } from './utils/constants'
import { account, FORK_BLOCK_NUMBER, FORK_URL } from './utils/constants'
import { createProxyClients } from './utils/utils'
import { fetchOpenOrders } from './utils/open-order'

const clients = createProxyClients(
Array.from({ length: 4 }, () => Math.floor(new Date().getTime())).map(
(id) => id,
),
)

afterEach(async () => {
await Promise.all(
clients.map(({ testClient }) => {
return testClient.reset({
jsonRpcUrl: FORK_URL,
blockNumber: FORK_BLOCK_NUMBER,
})
}),
)
})

const IS_LOCAL = process.env.IS_LOCAL === 'true'
test('get open orders by user address', async () => {
const { publicClient } = clients[0] as any
buildPublicClient(cloberTestChain.id, publicClient.transport.url!)

test.runIf(IS_LOCAL)('get open orders by user address', async () => {
const openOrders = await getOpenOrders({
chainId: cloberTestChain.id,
userAddress: '0xf18Be2a91cF31Fc3f8D828b6c714e1806a75e0AA',
Expand All @@ -23,7 +47,10 @@ test.runIf(IS_LOCAL)('get open orders by user address', async () => {
expect(openOrders.length).toBeGreaterThan(0)
})

test.runIf(IS_LOCAL)('get undefined open orders', async () => {
test('get undefined open orders', async () => {
const { publicClient } = clients[1] as any
buildPublicClient(cloberTestChain.id, publicClient.transport.url!)

expect(
await getOpenOrder({
chainId: cloberTestChain.id,
Expand All @@ -35,16 +62,53 @@ test.runIf(IS_LOCAL)('get undefined open orders', async () => {
).toEqual('Open order not found: 200')
})

test.runIf(IS_LOCAL)('claim all orders', async () => {
const openOrders = (
await getOpenOrders({
chainId: cloberTestChain.id,
userAddress: account.address,
options: {
rpcUrl: publicClient.transport.url!,
},
})
).slice(0, 20)
test('claim order', async () => {
const { publicClient, walletClient } = clients[2] as any
buildPublicClient(cloberTestChain.id, publicClient.transport.url!)

const signature = await signERC20Permit({
chainId: cloberTestChain.id,
account,
token: '0x00bfd44e79fb7f6dd5887a9426c8ef85a0cd23e0',
amount: '100000',
options: {
rpcUrl: publicClient.transport.url!,
},
})
const { transaction: takeTx } = await limitOrder({
chainId: cloberTestChain.id,
userAddress: account.address,
inputToken: '0x00bfd44e79fb7f6dd5887a9426c8ef85a0cd23e0',
outputToken: '0x0000000000000000000000000000000000000000',
amount: '100000',
price: '3505.01',
options: {
signature,
rpcUrl: publicClient.transport.url!,
},
})

expect(
(
await fetchOpenOrders(cloberTestChain.id, [
50784203244917507140848199044778666621202412111794785971205812514094254653440n,
])
).map((order) => order.claimable),
).toEqual([0n])

await walletClient.sendTransaction({
...takeTx!,
account,
gasPrice: takeTx!.gasPrice! * 2n,
})

expect(
(
await fetchOpenOrders(cloberTestChain.id, [
50784203244917507140848199044778666621202412111794785971205812514094254653440n,
])
).map((order) => order.claimable),
).toEqual([100030n])

// be sure to approve before claim
const hash = await setApprovalOfOpenOrdersForAll({
Expand All @@ -58,30 +122,75 @@ test.runIf(IS_LOCAL)('claim all orders', async () => {
await publicClient.waitForTransactionReceipt({ hash })
}

const transaction = await claimOrders({
const { transaction } = await claimOrder({
chainId: cloberTestChain.id,
userAddress: account.address,
ids: openOrders.map((order) => order.id),
id: '50784203244917507140848199044778666621202412111794785971205812514094254653440',
options: {
rpcUrl: publicClient.transport.url!,
},
})

await walletClient.sendTransaction({ ...transaction, account })

expect(
(
await fetchOpenOrders(cloberTestChain.id, [
50784203244917507140848199044778666621202412111794785971205812514094254653440n,
])
).map((order) => order.claimable),
).toEqual([0n])
})

test.runIf(IS_LOCAL)('cancel all orders', async () => {
const openOrders = (
await getOpenOrders({
chainId: cloberTestChain.id,
userAddress: account.address,
options: {
rpcUrl: publicClient.transport.url!,
},
})
)
.filter((order) => order.isCancelable)
.slice(0, 20)
test('claim orders', async () => {
const { publicClient, walletClient } = clients[2] as any
buildPublicClient(cloberTestChain.id, publicClient.transport.url!)

const signature = await signERC20Permit({
chainId: cloberTestChain.id,
account,
token: '0x00bfd44e79fb7f6dd5887a9426c8ef85a0cd23e0',
amount: '100000',
options: {
rpcUrl: publicClient.transport.url!,
},
})
const { transaction: takeTx } = await limitOrder({
chainId: cloberTestChain.id,
userAddress: account.address,
inputToken: '0x00bfd44e79fb7f6dd5887a9426c8ef85a0cd23e0',
outputToken: '0x0000000000000000000000000000000000000000',
amount: '100000',
price: '3505.01',
options: {
signature,
rpcUrl: publicClient.transport.url!,
},
})

expect(
(
await fetchOpenOrders(cloberTestChain.id, [
50784203244917507140848199044778666621202412111794785971205812514094254653440n,
50784203244917507140848199044778666621202412111794785971205812483307929075712n,
])
).map((order) => order.claimable),
).toEqual([0n, 0n])

await walletClient.sendTransaction({
...takeTx!,
account,
gasPrice: takeTx!.gasPrice! * 2n,
})

expect(
(
await fetchOpenOrders(cloberTestChain.id, [
50784203244917507140848199044778666621202412111794785971205812514094254653440n,
50784203244917507140848199044778666621202412111794785971205812483307929075712n,
])
).map((order) => order.claimable),
).toEqual([100030n, 60018n])

Check failure on line 193 in test/open-order.test.ts

View workflow job for this annotation

GitHub Actions / NPM Test (18.10)

open-order.test.ts > claim orders

AssertionError: expected [ 100030n, 0n ] to deeply equal [ 100030n, 60018n ] - Expected + Received Array [ 100030n, - 60018n, + 0n, ] ❯ open-order.test.ts:193:5

// be sure to approve before claim
const hash = await setApprovalOfOpenOrdersForAll({
Expand All @@ -95,14 +204,26 @@ test.runIf(IS_LOCAL)('cancel all orders', async () => {
await publicClient.waitForTransactionReceipt({ hash })
}

const transaction = await cancelOrders({
const { transaction } = await claimOrders({
chainId: cloberTestChain.id,
userAddress: account.address,
ids: openOrders.map((order) => order.id),
ids: [
'50784203244917507140848199044778666621202412111794785971205812514094254653440',
'50784203244917507140848199044778666621202412111794785971205812483307929075712',
],
options: {
rpcUrl: publicClient.transport.url!,
},
})

await walletClient.sendTransaction({ ...transaction, account })

expect(
(
await fetchOpenOrders(cloberTestChain.id, [
50784203244917507140848199044778666621202412111794785971205812514094254653440n,
50784203244917507140848199044778666621202412111794785971205812483307929075712n,
])
).map((order) => order.claimable),
).toEqual([0n, 0n])
})
Loading

0 comments on commit d9244af

Please sign in to comment.