0

How would one mock this call to a ethereum smart contract using the web3 js library.

export const getERC20Balance = async (
  web3Client: Web3,
  tokenAddress: string,
  walletAddress: string
) => {
  const balanceOfAbi = getERC20ABIOfInterest('balanceOf');
  const contract = new web3Client.eth.Contract(balanceOfAbi, tokenAddress);
  const result = await contract.methods.balanceOf(walletAddress).call();
  if (!result) return;

  const resultInEther = web3Client.utils.fromWei(Number(result), 'ether');

  return resultInEther
};

more specifically, const contract = new web3Client.eth.Contract(balanceOfAbi, tokenAddress); const result = await contract.methods.balanceOf(walletAddress).call();

I've tried mocking the library using mockDeep but get TypeError: Cannot read properties of undefined (reading 'balanceOf')

2
  • Show the code you have tried. Commented Apr 17, 2024 at 7:03
  • jest.mock('web3', () => { const web3 = jest.requireActual('web3'); jest.spyOn(web3, 'Contract').mockReturnValue({ Contract: () => ({ methods: jest.fn().mockImplementation(() => ({ // eslint-disable-next-line @typescript-eslint/no-unused-vars balanceOf: (_walletAddress: string) => ({ call: jest.fn(), }), })), }), }); Commented Apr 18, 2024 at 9:28

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.