0

I try to implement functionality of burning ERC20 token.Transaction is failed during calling "transferFrom" function. Who knows what's the problem? I guess there is an error in allowance.

function burnERC20(uint256 _amount, address _sender, address _tokenAddress) internal returns(bool){
       IERC20 tokenContract = IERC20(_tokenAddress);
       uint8 tokenDecimals = tokenContract.decimals();
       tokenContract.allowance(msg.sender, address(this));
       require(tokenContract.balanceOf(_sender) >= _amount * 10^tokenDecimals, 'you do not have enough funds');
       bool sent = tokenContract.transferFrom(_sender, address(0), _amount);
       return sent;
     }

1 Answer 1

1

There are two ways to transfer tokens:

  1. Direct transfer with transfer. The owner (party A) issues a transaction to transfer tokens from himself to some other address (party B). So tokens are transferred from A to B.

  2. Indirect transfer with approve and transferFrom. This is what you are trying to use. But this requires both of the parties to do something: A has to call approve and give B's address as the address to be allowed to withdraw tokens. After that, B calls transferFrom which utilizes the allowance.

But, since you are burning tokens by sending them to the zero address 0x0, you have to use the first method, since the receiver (0x0) can't call approve. So you have to use direct transfer: the owner of the tokens has to call the token contract's burn function (which sends the tokens to 0x0).

The allowance function you are using is simply a view function which doesn't perform any state changes - it simply gives you the current allowance status.

1
  • Thank you for your answer! I tried to pass TUSD 0xd92e713d051c37ebb2561803a3b5fbabc4962431 as ERC20 token and transfer it to zero address but it still fails. Here is code function transferERC20Token(address _tokenAddress, uint256 _amount)external { IERC20 tokenContract = IERC20(_tokenAddress); uint8 tokenDecimals = tokenContract.decimals(); require(tokenContract.balanceOf(msg.sender) >= _amount * 10^tokenDecimals, 'you do not have enougth funds'); tokenContract.transfer(address(0), _amount * 10**tokenDecimals); } Jan 30, 2022 at 17:46

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.