From 4c362d8a9ac72511bacd6a345d363d40db8b513b Mon Sep 17 00:00:00 2001 From: Quazia Date: Tue, 21 Nov 2023 11:51:55 -0500 Subject: [PATCH] Test(balancer): update test boilerplate --- packages/balancer/src/Balancer.test.ts | 45 ++++++++++++++++++++-- packages/balancer/src/test-setup.ts | 30 +++++++++++++++ packages/balancer/src/test-transactions.ts | 27 +++++++++++++ 3 files changed, 99 insertions(+), 3 deletions(-) create mode 100644 packages/balancer/src/test-setup.ts create mode 100644 packages/balancer/src/test-transactions.ts diff --git a/packages/balancer/src/Balancer.test.ts b/packages/balancer/src/Balancer.test.ts index 9d45c1733..ca4970ed0 100644 --- a/packages/balancer/src/Balancer.test.ts +++ b/packages/balancer/src/Balancer.test.ts @@ -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', () => { @@ -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 + }) }) }) diff --git a/packages/balancer/src/test-setup.ts b/packages/balancer/src/test-setup.ts new file mode 100644 index 000000000..4e095d8dc --- /dev/null +++ b/packages/balancer/src/test-setup.ts @@ -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', + }), +] diff --git a/packages/balancer/src/test-transactions.ts b/packages/balancer/src/test-transactions.ts new file mode 100644 index 000000000..7b08cbd92 --- /dev/null +++ b/packages/balancer/src/test-transactions.ts @@ -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 = { + 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)), + }, +}