I approved the contract to send my tokens using this code solidity await contract.approve(contract.address, totalBalance) and the transaction passes. After that, I execute the code solidity await contract.transferFrom(player,randomAddress,totalBalance), and the transaction fails. What is the reason?

1 Answer 1


Because that's not how you should use the approve function.

You have all the tokens but can't transfer them with transfer(). So you need to use transferFrom. So you need to approve someone to use all your token, so that someone can call transferFrom and transfer all your tokens out of your wallet.

In this case, you can even approve yourself (i.e. call approve(myAddress, 1000000000000000000000000)), so then you can call transferFrom(someoneElse,1000000000000000000000000).

Or you can use a second address and call, from address 1, approve(address2, 1000000000000000000000000), and then use address 2 to call transferFrom(address1, address2, 1000000000000000000000000).

What you are doing here is approving the contract to use your tokens, but the contract doesn't have the ability to call transferFrom so that's not gonna help.

Hope my answer helps, let me know if you have follow up questions

  • But in the ethernaut challenge DEX, I approved the contract to transfer my tokens, and it worked. I don't quite get the differennce. Oct 17, 2022 at 5:04
  • In DEX, by calling approve(), you are basically saying "I, address1, authorize the DEXaddress, to use X amount of my coins". Then, in DEX, when the function swap() is called, it triggers a call the IERC20(from).transferFrom. In this context, from the IERC20 contract perspective, the msg.sender of transferFrom() is the DEX contract. That's why you need to approve the contract. BUT in naught coin's case, the contract doesn't call transferFrom, so no need to approve the contract. Hope this helps! Oct 17, 2022 at 5:35
  • It did. Thank yo u very much for such a detailed explanation. Oct 17, 2022 at 9:46
  • Happy I could help! Good luck for the rest of the levels! Oct 17, 2022 at 12: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.