2

Solved For now, you will always need the ABI to execute a call against the ethereum network with web3. A simple way is to reconstruct the function you wish to execute is to, either create a separate contract to call or use the trick here and add in the ABI or at least a part of it from the contract you wish to call. In case, I used a generic ABI of an approval call.

await this.state.currentAddress.methods.approve("0x8b4a5682189361ce56702260051900f74d76e08b", amount).send({from: this.state.accounts[0]});

I have the following line in react. It is how I think I could execute the approve function, but it doesn't work. I can certainly get the contract ABI from an API but I want to avoid this. Is there any way to call the Approve function of standard ERC20 tokens via directly through contract address through web3 without having to import the ABI?

2

You will need the ABI. The .methods is an attribute of the contract instance, which you get by using the ABI.

If you are using ethers.js, you can use a shorthand ABI to achieve the same goal:

let abi = ["function approve(address _spender, uint256 _value) public returns (bool success)"]
let provider = ethers.getDefaultProvider('ropsten')
let contract = new ethers.Contract(tokenAddress, abi, provider)
await contract.approve(accountAddress, amount)

Note: if you want to pass in a from, you will have to use ethers opts.

| improve this answer | |
  • 1
    An absolutely brilliant answer. But I opted for an even easier solution; I simply copied the ABI of a simple ERC20 token approval function and as backup added the call to etherscan's api for the ABI if needed. – Nik Loladze Jan 23 at 8:33

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.