solidityweb3jsdecentralized-applications

passing IERC20 to a function argument using web3js


i have a solidity function

function check(IERC20 _token) internal view {...}

and i want to call the function using web3.js

contractInst = new web3.eth.Contract(contractABI);
await contractInst.methods.check(_token ).send({from: await connectedAdd, gas: "2000000"},function(error, transactionHash) {} );
        

but i dont know how to create the (IERC20 _token) variable.


Solution

  • For ABI-encoding purposes, all interface and contract arguments (including IERC20) translate to the address type. Docs: https://docs.soliditylang.org/en/v0.8.26/abi-spec.html#mapping-solidity-to-abi-types

    With web3js, you can pass the Solidity address as a JS string. Example:

    const _token = "0xdAC17F958D2ee523a2206206994597C13D831ec7";
    await contractInst.methods.check(_token ).send(...);