[Ethereum] Execute transaction Approve directly against contract address (without ABI)


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?

Best Answer

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.

Related Topic