1

Hello all I've recently starting diving into solidity development and I'm having trouble with ERC20 token interactions.

What I want to accomplish is sending ERC20 tokens to a smart contract that I created.

pragma solidity ^0.5.0;

contract ERC20 {
    function totalSupply() public view returns (uint supply);
    function balanceOf(address _owner) public view returns (uint balance);
    function transfer(address _to, uint _value) public returns (bool success);
    function transferFrom(address _from, address _to, uint _value) public returns (bool success);
    function approve(address _spender, uint _value) public returns (bool success);
    function allowance(address _owner, address _spender) public view returns (uint remaining);
    function decimals() public view returns(uint digits);
    event Approval(address indexed _owner, address indexed _spender, uint _value);
}

contract MySmartContract {
    function approveDeposit(address tokenAddress) public {
        ERC20 token = ERC20(tokenAddress);
        token.approve(address(this), token.balanceOf(msg.sender));
    }

    function depositToken(address tokenAddress) public {
        ERC20 token = ERC20(tokenAddress);
        uint tokenAmount = token.balanceOf(msg.sender);
        token.transferFrom(msg.sender, address(this), tokenAmount);
    }
}

In my wallet I have BAT tokens. After calling the approveDeposit function with the BAT token address, I try calling depositToken but it returns an error.

What am I doing wrong?

2 Answers 2

2

In order for this to complete successfully when executed from a function in your contract:

token.transferFrom(msg.sender, address(this), tokenAmount);

You need to execute approve(yourContractAddress, tokenAmount) using the msg.sender account.


But when you do this from a function in your contract:

token.approve(address(this), token.balanceOf(msg.sender));

You are executing approve using your contract account, which means that you allow some other account to transfer tokens from your contract. Moreover, by passing this as the first parameter, that other account is your own contract. In other words, you are approving your contract to transfer tokens from itself, which is of course useless.

0

I've been working on this as well. I havent found a way for the contract to call/execute the approve function in the token contract on behalf of the msg.sender to enable the contract to give itself the allowance. It seems like the only option is the msg.sender has to manually go to the erc20 token contract and approve the contract address and amount they want to deposit to the contract then go back to the contract and call the deposit function. Maybe as a user were just not seeing this interaction as the task to call the erc20 contract approve is programmed on the frontend.

1

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.