Msg.sender problem for calling ERC20 token contract from another contract returns contract address

when i am going to call approve function of erc20 contract.log prints tokenOwner and tokenSpender address same which is contract address msg.sender returns contract address. but i want tokenWner = caller address and tokenSpender = contract address please help.

THis is erc20 method

 function approve(address spender, uint256 tokens) public returns (bool success) {
    allowed[msg.sender][spender] = tokens;
   emit Approval(msg.sender, spender, tokens);

    return true;

And this is my contract method that is calling token method

 function approve(address token, uint256 _value) public returns (bool) {
 return ERC20Interface(token).approve(this, _value);
1 Like

Hi @love_chawda,

Welcome to the community :wave:

Only the holder of the tokens can call approve to set an allowance for a spender.
A contract cannot call approve on behalf of a holder as the ERC20 token uses msg.sender to determine the holder.

To spend an ERC20 token a holder must:

  1. Approve an allowance for the spender by calling approve on the token
  2. Call the contract which will use the token and the contract will call transferFrom (using the allowance for the contract for that holder).

If you are creating a token yourself you could look at creating ERC777 tokens (no need to do approve and transferFrom in two separate transactions). See the documentation for details:

Hi @love_chawda,

I wanted to check that the above answered your question.

yes i have checked. Thankyou for your reply

1 Like