Skip to content

Commit

Permalink
Test(balancer): update test boilerplate
Browse files Browse the repository at this point in the history
  • Loading branch information
Quazia committed Nov 21, 2023
1 parent 18eb8eb commit 4c362d8
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 3 deletions.
45 changes: 42 additions & 3 deletions packages/balancer/src/Balancer.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { GreaterThanOrEqual, apply } from '@rabbitholegg/questdk/filter'
import { apply } from '@rabbitholegg/questdk/filter'
import { describe, expect, test } from 'vitest'
import { DEFAULT_TOKEN_LIST } from './contract-addresses'
import { failingTestCases, passingTestCases } from './test-setup'
import { getAddress } from 'viem'

describe('Given the balancer plugin', () => {
describe('When handling the bridge', () => {
Expand All @@ -12,8 +15,44 @@ describe('Given the balancer plugin', () => {

})

test('should not pass filter with invalid transactions', () => {

describe('should pass filter when all parameters are valid', () => {
passingTestCases.forEach((testCase) => {
const { transaction, params, description } = testCase
test(description, async () => {
const filter = await swap({ ...params })
expect(apply(transaction, filter)).to.be.true
})
})
})
describe('should not pass filter when parameters are invalid', () => {
failingTestCases.forEach((testCase) => {
const { transaction, params, description } = testCase
test(description, async () => {
const filter = await swap({ ...params })
expect(apply(transaction, filter)).to.be.false
})
})
test('should throw error when contract address is incorrect', async () => {
try {
const { transaction, params } = passingTestCases[0]
params.contractAddress = '0xE592427A0AEce92De3Edee1F18E0157C05861564'
const filter = await swap({ ...params })
apply(transaction, filter)
throw new Error('Expected bridge function to throw, but it did not.')
} catch (err) {
if (err instanceof Error) {
expect(err.message).toBe('Invalid Contract Address')
}
}
})
})
describe('all supported tokens addresses are properly checksummed', () => {
test('should have all addresses properly checksummed', () => {
const notChecksummed = DEFAULT_TOKEN_LIST.filter(
(tokenAddress) => tokenAddress !== getAddress(tokenAddress),
)
expect(notChecksummed).to.be.empty
})
})

})
Expand Down
30 changes: 30 additions & 0 deletions packages/balancer/src/test-setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { GreaterThanOrEqual } from '@rabbitholegg/questdk'
import { parseEther, parseUnits } from 'viem'
import { Tokens, createTestCase } from '../../camelot/src/utils'
import {
V2_SWAP_ETH,

} from './test-transactions'

export const passingTestCases = [
createTestCase(V2_SWAP_ETH, 'when swapping ETH for tokens'),
]

export const failingTestCases = [
createTestCase(V2_SWAP_ETH, 'when chainId is incorrect', { chainId: 10 }),
createTestCase(V2_SWAP_ETH, 'when tokenIn is incorrect', {
tokenIn: Tokens.WETH,
}),
createTestCase(V2_SWAP_ETH, 'when tokenOut is incorrect', {
tokenOut: Tokens.WETH,
}),
createTestCase(V2_SWAP_ETH, 'when amountIn is insufficient', {
amountIn: GreaterThanOrEqual(parseEther('0.1')),
}),
createTestCase(V2_SWAP_ETH, 'when amountOut is insufficient', {
amountOut: GreaterThanOrEqual(parseUnits('20', 6)),
}),
createTestCase(V2_SWAP_ETH, 'when recipient in incorrect', {
recipient: '0x12e80D4b52023eDd8cB2294C6948D4c5d5D5D266',
}),
]
27 changes: 27 additions & 0 deletions packages/balancer/src/test-transactions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { ARBITRUM_CHAIN_ID } from '../../camelot/src/chain-ids'
import { parseEther, parseUnits } from 'viem'
import { Tokens, type TestParams } from '../../camelot/src/utils'
import {
GreaterThanOrEqual,
type SwapActionParams,
} from '@rabbitholegg/questdk'

export const V2_SWAP_ETH: TestParams<SwapActionParams> = {
transaction: {
chainId: 42161,
from: '0x0',
hash: '0x0',
input:
'0x0',
to: '00',
value: '0',
},
params: {
chainId: ARBITRUM_CHAIN_ID,
contractAddress: 0x0,
tokenIn: Tokens.ETH,
tokenOut: '0xBfbCFe8873fE28Dfa25f1099282b088D52bbAD9C', // EQB Token
amountIn: GreaterThanOrEqual(parseEther('0.00055')),
amountOut: GreaterThanOrEqual(parseUnits('9.25', 18)),
},
}

0 comments on commit 4c362d8

Please sign in to comment.