I am trying to send ERC20 token into an escrow contract so that it could transfer them to relevant stake holders. The part that I am stuck in is properly using the approve function.

From my escrow contract I created the following approve function:

function approveDeposit(uint amount) public returns(uint) { 
  return _tokenAddress.approve(address(this), amount);

For example when submitting approveDeposit(100), I was expecting that the it would first call the tokenContract and then approve the contract to transferFrom the owner 100 tokens in the form:

_allowed[owner][contract] = 100;

But when I debug in remix, it seems like _allowed[contract][contract] is executed.

How exactly can I execute _allowed[owner][contract] = amount instead of _allowed[contract][contract] = amount?


I think possibly there is a conceptual misunderstanding.

When you call _tokenAddress.approve() like that, the Escrow contract is msg.sender from the perspective of the ERC20. It's approving itself to spend tokens. The spirit of approve() is to give the privilege to someone/something else.

The flow will make more sense if the user who is staking ERC20 first calls approve(Escrow then calls a function in the Escrow to transferFrom(user. The user would be required to own sufficient unencumbered tokens for the authorization to succeed.

In the first stage, the user authorizes the Escrow contract to take from the user's own account (approve). In the second stage, the user instructs the Escrow contract to exercise the privilege using transferFrom(). The Escrow contract would be expected to update its internal states to keep track of obligations.

Hope it helps.

  • 1
    Thank you for the quick response. Yes, I think I was misunderstanding how approve() works. I do understand now that the _tokenAddress.approve() should be called by the owner, then calls transferFrom() from the Escrow. But how do I call _tokenAddress.approve() from the owner? – doexperiment Sep 10 '18 at 21:50
  • In remix, I tried launched an ERC20 token(say Fcoin) from address A. Then deployed the Escrow contract with address B. Now, I called approve(B, 100) from the Fcoin. Then I'm supposed to called transferFrom(msg.sender, address(this), 100). But even before the transferFrom, when I call allowance function to check allowed[A][B], I get 0. So I'm confused how to call approve() from an external address to allow contract address X amounts of tokens. – doexperiment Sep 10 '18 at 21:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.