0

I have a IERC20.sol file which is the interface of ERC-20 standard.

In my Dapp, I don't want to implement any ERC-20 tokens. I just want to use the existing tokens so all I need is a interface and the existing token address to interact with them right?

when I want to load the IERC20.json using web3.eth.contract, it will throw an error, because there is no address for the IERC20.json

const networkId = await web3.eth.net.getId()
const erc20Data = IERC20.networks[networkId]
if(erc20Data) {
  const erc20 = new web3.eth.Contract(ERC20.abi, erc20Data.address)
  this.setState({ erc20 })
} else {
  window.alert('erc20 contract not deployed to detected network.')
}

note that I have imported the related address and abis. I have loaded multiple contracts in my web page using this method, but the IERC20 can't be loaded using this method.

2
  • What exactly is the question? You need a valid address in order to create the local contract object in your dapp code. If erc20Data.address is undefined, then you should obviously replace it with a valid Ethereum address of a valid ERC20 token which is deployed on the network that you're connected to. Also, note that we're not supposed to guess what the contents of your erc20Data variable is (though it doesn't really matter in this case, you just need to use a valid address, as I explained). Jan 13, 2021 at 13:25
  • I want to use interface of ERC20 in my dapp and users will be able to enter the valid addresses of existing ERC20 tokens to approve and transfer tokens in my contract. Is it possible to access approve function in react app using IERC20.sol instead of ERC20.sol?
    – Amin
    Jan 13, 2021 at 20:13

1 Answer 1

2

I don't see your imports, but you should use the ABI from IERC20 instead of ERC20 when you instantiate the new contract.

Therefore, your code should look something like this:

import IERC20 from '../IERC20.json';
import web3 from 'web3';

const erc20 = new web3.eth.Contract(
   IERC20.abi,     // ERC20 interface you imported
   contractAddress // ERC20 token address sent by your users (e.g.: '0xaD6D458402F60fD3Bd25163575031ACDce07538D' if using DAI)
);
2
  • Yes, I didn't write the imports here.
    – Amin
    Jan 14, 2021 at 19:48
  • I just realized I have to load each existing token manually using IERC20 abi and token address. it worked fine.
    – Amin
    Jan 14, 2021 at 19:48

Your Answer

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

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