You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am new to web3 and struggling a bit to find the right way to write tests for contract transactions that are done through viem.
I want to test the following code.
classClient{constructor(privatewalletClient: WalletClient){}publicdeposit(walletAddress: Address){constcontract=getContract({address: '0x...',abi: ...,client: {// Test client passed through the constructor.wallet: this.walletClient,},});consttransact=async(value: bigint)=>awaitcontract.write.deposit([walletAddress],{account: walletAddress,chain: this.walletClient.chain,
value,});constestimate=async()=>awaitcontract.estimateGas.deposit([walletAddress],{account: walletAddress,});return{ transact, estimate };}}
This is the test I am trying to write.
exportconstsetupTestClient=(requestMock: EIP1193RequestFn=jest.fn())=>{returncreateTestClient({mode: 'anvil',chain: anvil,transport: custom({request: requestMock}),});};exportconstsetupTestWalletClient=(requestMock: EIP1193RequestFn=jest.fn(),)=>{constclient=setupTestClient(requestMock);returnclient.extend(walletActions)asWalletClient;};it('should deposit',async()=>{constmockAddress='0xEB77D02f8122B32273444a1b544C147Fb559CB41';constmockGas=BigInt(1);// `transact` aka `deposit` function of the contract returns an address.constwalletRequest=jest.fn().mockReturnValue(mockAddress);constwalletClient=setupTestWalletClient(walletRequest);constclient=newClient(walletClient);const{ transact, estimate }=client.deposit(mockAddress);expect(awaittransact(BigInt(1))).toBe(mockAddress);expect(awaitestimate()).toBe(mockGas);});
However, I get the following error.
ChainMismatchError: The current chain of the wallet (id: 1.344284755342623e+48) does not match the target chain for the transaction (id: 31337 – Anvil).
Any idea what I might be doing wrong? I have been trying to look around what's the best way to write tests for viem-based code like this but haven't been able to find any useful examples to use as reference.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hi all,
I am new to web3 and struggling a bit to find the right way to write tests for contract transactions that are done through viem.
I want to test the following code.
This is the test I am trying to write.
However, I get the following error.
Any idea what I might be doing wrong? I have been trying to look around what's the best way to write tests for viem-based code like this but haven't been able to find any useful examples to use as reference.
Beta Was this translation helpful? Give feedback.
All reactions