I read ERC20 solidity code below.


Then, I couldn't understand what the terms of msg.sender, spender, from, to, owner mean.

I guessed msg.sender equals from, spender equals to, and msg.sender equals owner. But if this hypothesis is true, allowed[from][msg.sender] isn't well defined.

I searched deeply but I couldn't get any information mentioned about above. Could anyone describe the definition and usage?

1 Answer 1


msg.sender is always the account that called the function. So any time you're reading code and see msg.sender, think "caller."

The caller is logically different entities depending on what function you're looking at. In the constructor, msg.sender is the account creating the contract (the "owner"). In a call to transfer, the caller is the owner of the coins being transferred, etc.

  • In transfer(to, amount), tokens are transferred from msg.sender to to.
  • In transferFrom(from, to, amount), msg.sender is the "spender" of tokens that are being transferred from from to to.
  • In allow(spender, amount), msg.sender is authorizing spender to be able to call transferFrom later.
  • In allowed[from][msg.sender], we're checking how many tokens from has previously allowed msg.sender to spend.
  • How about factory pattern, like a contract call function in another contract? Commented Aug 20, 2021 at 17:11
  • @HoangMinhDung you should see the difference between tx.origin and msg.sender, which is out of scope for this question
    – caffeinum
    Commented Sep 20, 2021 at 3:13

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.