1

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?

2
  • @Rob Hitchens, yes, thats the concept. but HOW do you actaully make the USER who is staking ERC20 call the approve(Escrow, amount) function FROM interacting with the ESCROW contract? i have not yet figured this out. Commented Jul 1, 2021 at 15:40
  • It's a user-interface concern. Suppose the user is to push a button to sign "the transaction." It's a two-stepper. Step one, they "authorize" by sending a transaction to approve(). Step 2, they "execute" by sending a transaction to the receiving contract, which will rely on transferFrom() ... hopefully the receiver has a sufficient allowance grab what they need from the sender. Commented Jul 1, 2021 at 19:44

1 Answer 1

2

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.

3
  • 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? Commented Sep 10, 2018 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. Commented Sep 10, 2018 at 21:54
  • @doexperiment did you solve this? I am also a bit confused how to use this.
    – Suisse
    Commented Feb 17, 2021 at 22:15

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.