Skip to content

Commit

Permalink
move test token out to its own helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
douglance committed Jan 14, 2025
1 parent 43e2c6c commit 8ca8616
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 34 deletions.
36 changes: 3 additions & 33 deletions packages/sdk-viem/tests/deposit-erc20.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,15 @@ import {
fundChildSigner,
fundParentSigner,
} from '@arbitrum/sdk/tests/integration/testHelpers'
import TestERC20 from '@arbitrum/token-bridge-contracts/build/contracts/contracts/tokenbridge/test/TestERC20.sol/TestERC20.json'
import { expect } from 'chai'
import { Address, Hex } from 'viem'
import { setupTestToken } from './helpers'
import { testSetup } from './testSetup'

describe('deposit erc20', function () {
this.timeout(300000)

let setup: Awaited<ReturnType<typeof testSetup>>
let testToken: {
address: Address
abi: typeof TestERC20.abi
}
let testToken: Awaited<ReturnType<typeof setupTestToken>>

before('init', async () => {
setup = await testSetup()
Expand All @@ -29,33 +25,7 @@ describe('deposit erc20', function () {
await fundParentSigner(setup.parentSigner)
await fundChildSigner(setup.childSigner)

const hash = await setup.parentWalletClient.deployContract({
abi: TestERC20.abi,
bytecode: TestERC20.bytecode as Hex,
chain: setup.localEthChain,
account: setup.parentAccount,
})

const receipt = await setup.parentPublicClient.waitForTransactionReceipt({
hash,
})

testToken = {
address: receipt.contractAddress! as Address,
abi: TestERC20.abi,
} as const

const mintHash = await setup.parentWalletClient.writeContract({
address: testToken.address,
abi: TestERC20.abi,
functionName: 'mint',
chain: setup.localEthChain,
account: setup.parentAccount,
})

await setup.parentPublicClient.waitForTransactionReceipt({
hash: mintHash,
})
testToken = await setupTestToken(setup)
})

beforeEach(async function () {
Expand Down
46 changes: 45 additions & 1 deletion packages/sdk-viem/tests/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ import {
ChildTransactionReceipt,
} from '@arbitrum/sdk'
import { config } from '@arbitrum/sdk/tests/testSetup'
import TestERC20 from '@arbitrum/token-bridge-contracts/build/contracts/contracts/tokenbridge/test/TestERC20.sol/TestERC20.json'
import {
publicClientToProvider,
viemTransactionReceiptToEthersTransactionReceipt,
} from '@offchainlabs/ethers-viem-compat'
import { Wallet } from 'ethers'
import { Hash, PublicClient, TransactionReceipt } from 'viem'
import { Address, Hash, Hex, PublicClient, TransactionReceipt } from 'viem'
import { testSetup } from './testSetup'


/**
* Test utility to execute a withdrawal after it's been confirmed.
Expand Down Expand Up @@ -63,3 +66,44 @@ export async function executeConfirmedWithdrawal(
hash: execHash as Hash,
}
}

/**
* Deploys and initializes a test ERC20 token contract for testing purposes.
* The function deploys the TestERC20 contract on the parent chain and mints initial tokens.
*
* @param setup - Test setup object containing wallet clients and chain configuration
* @returns An object containing the deployed token's address and ABI
*/
export const setupTestToken = async (
setup: Awaited<ReturnType<typeof testSetup>>
) => {
const hash = await setup.parentWalletClient.deployContract({
abi: TestERC20.abi,
bytecode: TestERC20.bytecode as Hex,
chain: setup.localEthChain,
account: setup.parentAccount,
args: [],
})

const receipt = await setup.parentPublicClient.waitForTransactionReceipt({
hash,
})

const mintHash = await setup.parentWalletClient.writeContract({
address: receipt.contractAddress! as Address,
abi: TestERC20.abi,
functionName: 'mint',
chain: setup.localEthChain,
account: setup.parentAccount,
args: [],
})

await setup.parentPublicClient.waitForTransactionReceipt({
hash: mintHash,
})

return {
address: receipt.contractAddress! as Address,
abi: TestERC20.abi,
} as const
}

0 comments on commit 8ca8616

Please sign in to comment.