This is openzeppelin ERC20 Code


I dont understand why this code uses the following assigment

address spender = _msgSender();

Because in this contract every function use this following assigment

address owner = _msgSender();

Could you clarify this difference ?

enter image description here

2 Answers 2


We start from the premise that ERC20 contract uses the Gas Station Network (GNS). GNS allows you to build apps where you pay for your users transactions, so they do not need to hold Ether to pay for gas, easing their onboarding process. For more information read this.

From the documentation _msgSender() function:

Replacement for msg.sender. Returns the actual sender of a transaction: msg.sender for regular transactions, and the end-user for GSN relayed calls

In code you posted, the variable spender will have the address of who calling the function.

  • I dont understand I m owner and I m msg. sender, I would like to sent tokens the other account, I call the function. Why does receive get "msg. sender"? Commented Feb 26, 2022 at 15:17
  • 1
    Who call the functions is the your contract not your address. When your contract interact with other contract msg.sender will be your contract address because it called the function. Commented Feb 26, 2022 at 17:21

Just an unfortunate naming choice.

Usually, owner is used as the variable name for the contract owner.

In this case, they mean the token owner, alias the sender of the token, alias usually the from variable.

Please note OpenZeppelin contracts change often, so you always need to check the updated documentation, currently v4.x. I.e., GSN has been an external project since OZ v3, so now the _msgSender() function returns the msg.sender, if you don't expressly override with your implementation, as you can see in the code (currently v4.4.1) and in the updated documentation.

Personally, I find OZ contracts often over-designed and unnecessarily complicated. I suggest studying those contracts because there can be good and tested code, but then creating your contract with the actual code needed by your application.

Writing a more straightforward contract that you understand and is exactly what your application needs is far better than relying on someone else's catch-them-all code.

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.