Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename some ERC20 parameters to match the standard document. #3167

Merged
merged 6 commits into from
Feb 7, 2022

Conversation

MicahZoltu
Copy link
Contributor

@MicahZoltu MicahZoltu commented Feb 4, 2022

The variable naming was incorrect. The source of the funds is not necessarily (and in most cases isn't) the sender of the transaction. Also, this code has a msgSender which further adds confusion.

PR Checklist

  • Tests
  • Documentation
  • Changelog entry

The naming variable was incorrect.  The source of the funds is *not* necessarily (and in most cases isn't) the sender of the transaction.  Also, this code has a `msgSender` which further adds confusion.
@Amxx
Copy link
Collaborator

Amxx commented Feb 4, 2022

Hello @MicahZoltu

I'm not sure that has any impact at all, but if we should rename them then I'd suggest we use the from, to, value naming as used in the reference document.

_msgSender() is our binding into the Context pattern (for relayed meta-tx support).

@MicahZoltu
Copy link
Contributor Author

Updated PR to use to/from, I agree that is a better naming scheme. I'm familiar with the _msgSender pattern the OZ contracts use, but that doesn't change the confusion introduced in this function by having two senders, one of which is not actually the "sender" of anything.

@DanielVF
Copy link
Contributor

DanielVF commented Feb 4, 2022

I love the from/to.

Looks like the mini-docs above the function need to be updated. I've sent you a PR to your PR, Micah. :D

Function documentation matches new names
@Amxx
Copy link
Collaborator

Amxx commented Feb 4, 2022

For consistency, it would also make sense to do the same changes to transfer, approve, and all the standard functions.

Also changed `msgSender` to `owner` in the approval related methods.
@MicahZoltu
Copy link
Contributor Author

Updated the PR to improve consistency across methods. This includes both renaming sender/recipient to from/to and ALSO renaming _msgSender() in some places to owner (via local variable). The latter brings consistency across the approval methods and helps make the code easier to digest.

⚠️ Someone who understands _msgSender() better than I should validate that caching the result of _msgSender() in a local for the duration of these functions is not problematic! I do not know if perhaps it is intentional that _msgSender() can change within a function. If not, then I think the local is appropriate because it not only saves gas and improves readability, but it also reduces the chance that some derived class breaks something by having _msgSender() be mutable within a given call context. If it is intentional that it is mutable I will undo those changes.

Amxx
Amxx previously approved these changes Feb 6, 2022
@Amxx
Copy link
Collaborator

Amxx commented Feb 6, 2022

@MicahZoltu I 100% approve caching _msgSender. This function is not expected to write/change state, and in case it would return different results when called from the same context, that would be breaking.

Caching not only fixes that, but it also might save some gas!

@frangio Can you double-check before we merge?

@Amxx Amxx changed the title Renames sender to source. Rename some ERC20 parameters to match the standard document. Feb 7, 2022
uint256 amount
) public virtual override returns (bool) {
uint256 currentAllowance = _allowances[sender][_msgSender()];
address spender = _msgSender();
uint256 currentAllowance = allowance(from, spender);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: This line contains an unrelated change, the allowance is now read through an (overrideable) function instead of directly from storage. We were planning to make this change and it's small enough that I think it's ok to include here.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I included it for that reason

Copy link
Contributor

@frangio frangio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thank you @MicahZoltu for the suggestion.

I'd love to clarify one thing though. I don't believe that argument names in EIPs are normative, even in events, so this was more about improving code clarity than fixing something that was incorrect. Do we disagree on this?

@Amxx Amxx merged commit 63b4669 into OpenZeppelin:master Feb 7, 2022
@DanielVF
Copy link
Contributor

DanielVF commented Feb 8, 2022

@frangio, Yes, code clarity is the reason. The original kick off for this was a project (not using OZ) getting rekt by switching the order of their allowance lookups in one function.

The OZ code for allowances prior to this change was:

uint256 currentAllowance = _allowances[sender][_msgSender()];

Which is a bit confusing to look at (sender vs sender) and could easily lead to errors when someone was copying and modifying it.

The rest of the PR just grew from there. :)

@Amxx
Copy link
Collaborator

Amxx commented Feb 8, 2022

The name of event's arguments are important in terms of backward compatibility. Changing that could break things like the openzeppelin subgraphs

@MicahZoltu
Copy link
Contributor Author

I'd love to clarify one thing though. I don't believe that argument names in EIPs are normative, even in events, so this was more about improving code clarity than fixing something that was incorrect. Do we disagree on this?

Daniel was correct, this PR was about improving clarity only. The choice of to/from wasn't even my original suggestion. 😄

@MicahZoltu MicahZoltu deleted the patch-4 branch February 8, 2022 10:21
frangio pushed a commit that referenced this pull request Feb 9, 2022
* Renames `sender` to `source`.

The naming variable was incorrect.  The source of the funds is *not* necessarily (and in most cases isn't) the sender of the transaction.  Also, this code has a `msgSender` which further adds confusion.

* Changes to `from/to` instead of `source`.

* Function documentation matches new names

* Changed other instances of sender/recipient to from/to.

Also changed `msgSender` to `owner` in the approval related methods.

* apply changes to IERC20.sol + minor renaming in ERC20.sol

Co-authored-by: Daniel Von Fange <daniel@leancoder.com>
Co-authored-by: Hadrien Croubois <hadrien.croubois@gmail.com>
(cherry picked from commit 63b4669)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants