Skip to content

Commit

Permalink
test: add tc for add liquidity without swap
Browse files Browse the repository at this point in the history
  • Loading branch information
graykode committed Sep 25, 2024
1 parent 4336d60 commit 5926c75
Show file tree
Hide file tree
Showing 2 changed files with 390 additions and 0 deletions.
350 changes: 350 additions & 0 deletions test/pool-user-action.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,350 @@
import { expect, beforeEach, test } from 'vitest'
import { addLiquidity, getPool } from '@clober/v2-sdk'
import { formatUnits, zeroHash } from 'viem'

import { cloberTestChain } from '../src/constants/test-chain'

import { account, FORK_URL } from './utils/constants'
import { createProxyClients } from './utils/utils'
import { fetchLPBalance, fetchTokenBalance } from './utils/currency'

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

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

test('Add liquidity without swap - 1', async () => {
const { publicClient, walletClient } = clients[0] as any

const pool = await getPool({
chainId: cloberTestChain.id,
token0: '0x00bfd44e79fb7f6dd5887a9426c8ef85a0cd23e0',
token1: '0xF2e615A933825De4B39b497f6e6991418Fb31b78',
salt: zeroHash,
options: {
rpcUrl: publicClient.transport.url!,
useSubgraph: false,
},
})

let [beforeUSDCBalance, beforeWETHBalance, beforeLPBalance] =
await Promise.all([
fetchTokenBalance(
publicClient,
cloberTestChain.id,
'0x00bfd44e79fb7f6dd5887a9426c8ef85a0cd23e0',
account.address,
),
fetchTokenBalance(
publicClient,
cloberTestChain.id,
'0xF2e615A933825De4B39b497f6e6991418Fb31b78',
account.address,
),
fetchLPBalance(
publicClient,
cloberTestChain.id,
BigInt(pool.key),
account.address,
),
])

const { transaction: tx1, result: result1 } = await addLiquidity({
chainId: cloberTestChain.id,
userAddress: account.address,
token0: '0x00bfd44e79fb7f6dd5887a9426c8ef85a0cd23e0',
token1: '0xF2e615A933825De4B39b497f6e6991418Fb31b78',
salt: zeroHash,
amount0: '2000',
amount1: '1.0',
options: {
rpcUrl: publicClient.transport.url!,
useSubgraph: false,
},
})

const hash1 = await walletClient.sendTransaction({
...tx1!,
account,
gasPrice: tx1!.gasPrice! * 2n,
})
await publicClient.waitForTransactionReceipt({ hash: hash1 })
let [afterUSDCBalance, afterWETHBalance, afterLPBalance] = await Promise.all([
fetchTokenBalance(
publicClient,
cloberTestChain.id,
'0x00bfd44e79fb7f6dd5887a9426c8ef85a0cd23e0',
account.address,
),
fetchTokenBalance(
publicClient,
cloberTestChain.id,
'0xF2e615A933825De4B39b497f6e6991418Fb31b78',
account.address,
),
fetchLPBalance(
publicClient,
cloberTestChain.id,
BigInt(pool.key),
account.address,
),
])
expect(formatUnits(beforeUSDCBalance - afterUSDCBalance, 6)).toBe(
result1.currencyA.amount,
)
expect(formatUnits(beforeWETHBalance - afterWETHBalance, 18)).toBe(
result1.currencyB.amount,
)
expect(formatUnits(afterLPBalance - beforeLPBalance, 18)).toBe(
result1.lpCurrency.amount,
)

// add liquidity more
;[beforeUSDCBalance, beforeWETHBalance, beforeLPBalance] = await Promise.all([
fetchTokenBalance(
publicClient,
cloberTestChain.id,
'0x00bfd44e79fb7f6dd5887a9426c8ef85a0cd23e0',
account.address,
),
fetchTokenBalance(
publicClient,
cloberTestChain.id,
'0xF2e615A933825De4B39b497f6e6991418Fb31b78',
account.address,
),
fetchLPBalance(
publicClient,
cloberTestChain.id,
BigInt(pool.key),
account.address,
),
])

const { transaction: tx2, result: result2 } = await addLiquidity({
chainId: cloberTestChain.id,
userAddress: account.address,
token0: '0x00bfd44e79fb7f6dd5887a9426c8ef85a0cd23e0',
token1: '0xF2e615A933825De4B39b497f6e6991418Fb31b78',
salt: zeroHash,
amount0: '2000',
amount1: '0.7',
options: {
rpcUrl: publicClient.transport.url!,
useSubgraph: false,
},
})

const hash2 = await walletClient.sendTransaction({
...tx2!,
account,
gasPrice: tx2!.gasPrice! * 2n,
})
await publicClient.waitForTransactionReceipt({ hash: hash2 })
;[afterUSDCBalance, afterWETHBalance, afterLPBalance] = await Promise.all([
fetchTokenBalance(
publicClient,
cloberTestChain.id,
'0x00bfd44e79fb7f6dd5887a9426c8ef85a0cd23e0',
account.address,
),
fetchTokenBalance(
publicClient,
cloberTestChain.id,
'0xF2e615A933825De4B39b497f6e6991418Fb31b78',
account.address,
),
fetchLPBalance(
publicClient,
cloberTestChain.id,
BigInt(pool.key),
account.address,
),
])

expect(formatUnits(beforeUSDCBalance - afterUSDCBalance, 6)).toBe(
result2.currencyA.amount,
)
expect(formatUnits(beforeWETHBalance - afterWETHBalance, 18)).toBe(
result2.currencyB.amount,
)
expect(formatUnits(afterLPBalance - beforeLPBalance, 18)).toBe(
result2.lpCurrency.amount,
)
})

test('Add liquidity without swap - 2', async () => {
const { publicClient, walletClient } = clients[0] as any

const pool = await getPool({
chainId: cloberTestChain.id,
token0: '0x00bfd44e79fb7f6dd5887a9426c8ef85a0cd23e0',
token1: '0xF2e615A933825De4B39b497f6e6991418Fb31b78',
salt: zeroHash,
options: {
rpcUrl: publicClient.transport.url!,
useSubgraph: false,
},
})

let [beforeUSDCBalance, beforeWETHBalance, beforeLPBalance] =
await Promise.all([
fetchTokenBalance(
publicClient,
cloberTestChain.id,
'0x00bfd44e79fb7f6dd5887a9426c8ef85a0cd23e0',
account.address,
),
fetchTokenBalance(
publicClient,
cloberTestChain.id,
'0xF2e615A933825De4B39b497f6e6991418Fb31b78',
account.address,
),
fetchLPBalance(
publicClient,
cloberTestChain.id,
BigInt(pool.key),
account.address,
),
])

const { transaction: tx1, result: result1 } = await addLiquidity({
chainId: cloberTestChain.id,
userAddress: account.address,
token0: '0x00bfd44e79fb7f6dd5887a9426c8ef85a0cd23e0',
token1: '0xF2e615A933825De4B39b497f6e6991418Fb31b78',
salt: zeroHash,
amount0: '2000',
amount1: '0.7',
options: {
rpcUrl: publicClient.transport.url!,
useSubgraph: false,
},
})

const hash1 = await walletClient.sendTransaction({
...tx1!,
account,
gasPrice: tx1!.gasPrice! * 2n,
})
await publicClient.waitForTransactionReceipt({ hash: hash1 })
let [afterUSDCBalance, afterWETHBalance, afterLPBalance] = await Promise.all([
fetchTokenBalance(
publicClient,
cloberTestChain.id,
'0x00bfd44e79fb7f6dd5887a9426c8ef85a0cd23e0',
account.address,
),
fetchTokenBalance(
publicClient,
cloberTestChain.id,
'0xF2e615A933825De4B39b497f6e6991418Fb31b78',
account.address,
),
fetchLPBalance(
publicClient,
cloberTestChain.id,
BigInt(pool.key),
account.address,
),
])
expect(formatUnits(beforeUSDCBalance - afterUSDCBalance, 6)).toBe(
result1.currencyA.amount,
)
expect(formatUnits(beforeWETHBalance - afterWETHBalance, 18)).toBe(
result1.currencyB.amount,
)
expect(formatUnits(afterLPBalance - beforeLPBalance, 18)).toBe(
result1.lpCurrency.amount,
)

// add liquidity more
;[beforeUSDCBalance, beforeWETHBalance, beforeLPBalance] = await Promise.all([
fetchTokenBalance(
publicClient,
cloberTestChain.id,
'0x00bfd44e79fb7f6dd5887a9426c8ef85a0cd23e0',
account.address,
),
fetchTokenBalance(
publicClient,
cloberTestChain.id,
'0xF2e615A933825De4B39b497f6e6991418Fb31b78',
account.address,
),
fetchLPBalance(
publicClient,
cloberTestChain.id,
BigInt(pool.key),
account.address,
),
])

const { transaction: tx2, result: result2 } = await addLiquidity({
chainId: cloberTestChain.id,
userAddress: account.address,
token0: '0x00bfd44e79fb7f6dd5887a9426c8ef85a0cd23e0',
token1: '0xF2e615A933825De4B39b497f6e6991418Fb31b78',
salt: zeroHash,
amount0: '2000',
amount1: '1.0',
options: {
rpcUrl: publicClient.transport.url!,
useSubgraph: false,
},
})

const hash2 = await walletClient.sendTransaction({
...tx2!,
account,
gasPrice: tx2!.gasPrice! * 2n,
})
await publicClient.waitForTransactionReceipt({ hash: hash2 })
;[afterUSDCBalance, afterWETHBalance, afterLPBalance] = await Promise.all([
fetchTokenBalance(
publicClient,
cloberTestChain.id,
'0x00bfd44e79fb7f6dd5887a9426c8ef85a0cd23e0',
account.address,
),
fetchTokenBalance(
publicClient,
cloberTestChain.id,
'0xF2e615A933825De4B39b497f6e6991418Fb31b78',
account.address,
),
fetchLPBalance(
publicClient,
cloberTestChain.id,
BigInt(pool.key),
account.address,
),
])

expect(formatUnits(beforeUSDCBalance - afterUSDCBalance, 6)).toBe(
result2.currencyA.amount,
)
expect(formatUnits(beforeWETHBalance - afterWETHBalance, 18)).toBe(
result2.currencyB.amount,
)
expect(formatUnits(afterLPBalance - beforeLPBalance, 18)).toBe(
result2.lpCurrency.amount,
)
})

// test('Add liquidity with swap', async () => {
// const { publicClient, walletClient } = clients[0] as any
// })
Loading

0 comments on commit 5926c75

Please sign in to comment.