2

This might be a basic question, but I was walking through how to interact with the DAI (an ERC20 token) smart contract in Web3.js which led to this:

const daiTokenAddress = "0x6b175474e89094c44da98b954eedeac495271d0f"; // Replace DAI Address Here (can be the actual smart contract)
    const daiTokenMock = new web3.eth.Contract(DaiTokenMock.abi, daiTokenAddress);

But then I switched the daiTokenAddress with the smart contract of the USDC token (a different ERC20 token) and grabbed that contract instead and it works fine.

So my question is, can I switch the contract address in the above function to any smart contract? And then what's the point of passing in an ABI?

1
  • The abi tell what methods the contract has and the signature of those methods (method name and parameter..). Because both contracts are ERC20 and it is a standard interface then they has the same method signature and you can use the abi of DAI or a generic ERC20 abi.
    – Majd TL
    Feb 26 '20 at 9:43
1

It worked because both Smart Contracts inherited from the ERC20 standard Smart Contract. This Smart Contracts defines default functions, variables and events. The ABI contains information about the functions (name, inputs, outputs, mutability, payable), variables and events. Using this information a web3 library can craft transactions that execute a specific function, create calls to the EVM (read-only functions, variables) and handle events. Since the default ERC20 functions, events and variables are the same in DAI and USDC tokens, one ABI is sufficient for both cases. If somebody would create an ERC20 token by those standards and add another function or events for example, your Contract instance would not be able to handle those.

1

Yes, you're right, it's working in this case, because they're both an implementation of the same standard ERC-20 contract, you can use the same ABI as it implements the same methods.

You may consider DaiTokenMock.abi as ERC20.abi to make it more clear. :)

So my question is, can I switch the contract address in the above function to any smart contract? And then what's the point of passing in an ABI?

Yes, you can switch to any other ERC-20 contracts, and not if you're trying to instantiate a Contract, which is not an ERC-20.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.