So basically, I want a transfer function of ERC20 tokens on 0.4.16, but I want to make it so that you can specify which token you want to transfer to another address, but everytime I try to send tokens, there's an error, I think it's because the "msg.sender" has to approve the tokens, but idk, this is the code, if you want to review it:

pragma solidity 0.4.16;

interface ERC20 {
    function transfer(address _to, uint256 _value) public returns (bool);

contract TokenTransfer {
    function transferToken(address _tokenAddress, address _to, uint256 _value) public returns (bool) {
        // Create an instance of the ERC20 token contract
        ERC20 token = ERC20(_tokenAddress);

        // Transfer the specified amount of tokens to the specified address
        return token.transfer(_to, _value);
  • Things that would help: 1. put the code in code-blocks so that it is easier to read 2. include the error you have (compile error / runtime error) Jul 12, 2023 at 14:15
  • With that code, you would be sending tokens from the balance of the contract to the _to address, so there would be no problems with approvals. Maybe it does not have balance to send.
    – ceseshi
    Jul 12, 2023 at 14:50

1 Answer 1


transfer() would transfer tokens that are already in your contract to _to, and is gonna fail if your contract doesn't have the required balance. If you want to take tokens from the user calling transferToken(), you'll need to use transferFrom(), and in that case you're going to need approval, yes.

  • Thanks, it worked, I just needed to add some tokens to the contract, btw, can you help me with my other question about BatchTransfer?
    – 19216811
    Jul 15, 2023 at 19:40

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.