4

I'm trying to implement something like this (which works)

function withdraw() public {
    msg.sender.transfer(address(this).balance)
}

but I need to be able to withdraw ERC20 tokens other than Ethereum from my contract, is this possible? I can't seem to figure it out.

Thanks!

1
  • A problem with the code snippet is that it is not permissioned so anyone will be able to extract ethers from the contract.
    – Ismael
    Commented Jul 8, 2020 at 1:41

1 Answer 1

6
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";

function withdrawErc20(IERC20 token) public {
  require(token.transfer(msg.sender, token.balanceOf(address(this))), "Transfer failed");
}

See IERC20 for the interface code. Copy & paste it or install it via npm.

0

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.