0

Solidity noob but have looked around and cannot seem to find an answer to my specific issue. I am sure I am overlooking something really obvious but need help.

I am working in Remix using the Injected Web3 environment. I have successfully deployed a test contract from the injected wallet (using https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/ERC20.sol) and I can see the 1 trillion tokens in that wallet. So far so good.

I have a second contract and am just trying to set up some functions so I can:

  • Approve an allowance for the contract to spend this token on my behalf
  • Send the token on my behalf (to itself, in this case)

The code I have is:

    
    // grant allowance to contract to spend 'amount' on behalf of caller
    function approveToken(uint amount) public payable {
        token.increaseAllowance(address(this), amount);
    }
    
    // send 'amount' from caller to contract
    function sendToken(uint amount) public payable {
        token.transferFrom(msg.sender, address(this), amount);
    }
    
    // return balance of 'token' owned by caller
    function getTokenBalance() public view returns(uint256) {
        return token.balanceOf(msg.sender);
    } 
    
    // return 'token' allowance contract can spend on behalf of caller
    function getTokenAllowance() public view returns(uint256) {
        return token.allowance(msg.sender, address(this));
    }

approveToken executes without any error

getTokenBalance works fine (shows the tokens)

getTokenAllowance returns 0

What I think is happening is when I run the sendToken function from remix my wallet is the caller, not the contract. So I am essentially checking whether I have given myself an allowance to send this token, and perhaps that is what is tripping things up. So it seems to me I need to make sure the contract is what is calling the sendToken function as that is the 3rd party given an allowance.

But I am lost, any help would be appreciated.

1 Answer 1

1

Sorted, was being stupid and calling the tokens approve() function via the transfer contract and not on on the token contract directly.

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.