1

Here is a function in a smart contract to call transferFrom in ERC20 token contract.

pragma ^0.7.0

interface ERC20Token {
   function transfer(address recipient, uint256 amount) external returns (bool);
   function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
 }

contract MyContract {
  ...
  function depositEscrow(uint256 _value) payable external  returns (bool) {
        ...
        ERC20Token _fex = ERC20Token(contractFEX);  //<<==create instance of the ERC20 token contract with its address
        _fex.transferFrom(msg.sender, address(this), value);  //Who is msg.sender in transferFrom? is it the MyContract address?
        ... 
        return true;
    }

}

When executing _fex.transferFrom(), is msg.sender the address of MyContract?

7
  • 1
    No, it's the address of whoever called function depositEscrow. Dec 29 '20 at 6:11
  • 1
    The msg sender is the one who called the function. If u did directly with a transaction then it is u. If u called a contract and that contract called this method in MyContract then it is the address of the "between" contract.
    – Majd TL
    Dec 29 '20 at 17:33
  • 1
    Take care how the transferFrom method is implemented, because "inside" the code of transferFrom(in the Token Contract) the msg.sender is your contract
    – Majd TL
    Dec 29 '20 at 17:37
  • Majd TL, I thought inside ERC20Token.transferFrom(), the msg.sender is the address(this) which is the address of the MyContract instance. Is it true?. I understand that in function depositEscrow(), the msg.sender is the address of the caller who initializes the tx. Thanks.
    – user938363
    Dec 30 '20 at 20:17
  • just added the definition of ERC10Token interface to the post.
    – user938363
    Dec 30 '20 at 20:23
2

msg.sender is the direct sender of the message. BUT it could also be a contract address so for example a contract that passed it along.

3
  • Hi Tony, just added definition of token interface to the post. I thought msg.sender is the contract address when executing ERC20Token.transferFrom(), Please correct if I am wrong.
    – user938363
    Dec 30 '20 at 20:26
  • Not always. As mentioned it COULD be a contract but could also be a user of a dapp for example. It is the senders address of what triggered the event.
    – Tony
    Dec 31 '20 at 21:04
  • 1
    In @openZeppelin ERC20 contract, when calling transferFrom from a function inside a contract, the msg.sender is the address of the contract. .
    – user938363
    Jan 4 at 5:37
1

I am doing something related to that here, and I found out that msg.sender will always return the address of the user who gave start to the transaction.

So if the valued is passed along to a hundred (or more) intermediary contracts, and you inspect the variable "msg.sender" on the last contract, it will still contain the address of the user who started the transaction rather than the address of the contracts.

By the way, right now i am looking for a solution to show me the address of the intermediary contact. I need it for a project.

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.