Skip to content

ERC20 totalSupply changed from public property to a function#666

Merged
frangio merged 2 commits into
OpenZeppelin:masterfrom
spalladino:feature/434-totalSupply_as_function
Jan 19, 2018
Merged

ERC20 totalSupply changed from public property to a function#666
frangio merged 2 commits into
OpenZeppelin:masterfrom
spalladino:feature/434-totalSupply_as_function

Conversation

@spalladino

Copy link
Copy Markdown
Contributor
  • 📘 I've reviewed the OpenZeppelin Contributor Guidelines
  • ✅ I've added tests where applicable to test my new functionality.
  • 📖 I've made sure that my contracts are well-documented.
  • 🎨 I've run the JavaScript linter (npm run lint:fix) and fixed all issues.

Fixes #434


🚀 Description

Having ERC20#totalSupply defined as a public property made it impossible to override it in any child class, due to a solidity limitation. As such, it was impossible to change the getter behaviour while retaining the ERC20 interface, at least without modifying the OpenZeppelin base contract (which is something we definitely want to avoid).

This PR removes the public property definition from the ERC20Basic interface, and replaces it with just the signature for the getter. The implementation is provided in BasicToken, the first contract that implements that interface, backed by a totalSupply_ internal field.

Regarding the naming convention for the backing field: since we are using leading underscores for the parameters, I unilaterally decided to use trailing underscore for the backing fields. Other options I considered were using a dollar sign ($totalSupply or totalSupply$), or some variant of Hungarian notation (u256TotalSupply).

@spalladino

Copy link
Copy Markdown
Contributor Author

Heh, PR 666 😈

*/
function SimpleToken() public {
totalSupply = INITIAL_SUPPLY;
totalSupply_ = INITIAL_SUPPLY;

@facuspagnuolo facuspagnuolo Jan 12, 2018

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I thought we had said that we were going to use an underscore as a prefix, instead of a suffix, am I right?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah, hence the last comment on the PR:

Regarding the naming convention for the backing field: since we are using leading underscores for the parameters, I unilaterally decided to use trailing underscore for the backing fields. Other options I considered were using a dollar sign ($totalSupply or totalSupply$), or some variant of Hungarian notation (u256TotalSupply).

frangio
frangio previously approved these changes Jan 18, 2018

@frangio frangio left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Not happy with the trailing underscore notation but cannot think of a better alternative.

@spalladino

Copy link
Copy Markdown
Contributor Author

@frangio please re-review :-)

@frangio frangio merged commit 370e6a8 into OpenZeppelin:master Jan 19, 2018
@frangio frangio removed the review label Jan 19, 2018
*/
contract ERC20Basic {
uint256 public totalSupply;
function totalSupply() public view returns (uint256);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I got a compilation error because of this. It tells the following:

zeppelin-solidity/contracts/token/ERC20/ERC20Basic.sol:10:3: TypeError: Redeclaring an already implemented function as abstract
  function totalSupply() public view returns (uint256);
  ^---------------------------------------------------^
,zeppelin-solidity/contracts/token/ERC20/ERC20Basic.sol:10:3: TypeError: Redeclaring an already implemented function as abstract
  function totalSupply() public view returns (uint256);
  ^---------------------------------------------------^

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Where did that happen @hlogeon? If you can reproduce would you mind opening a new issue please?

ProphetDaniel pushed a commit to classicdelta/Smart-Contracts that referenced this pull request Mar 9, 2018
hbdgr added a commit to JoyPlatform/joy-contracts that referenced this pull request Jul 17, 2018
JoyGaming pushed a commit to JoyPlatform/joy-contracts that referenced this pull request Jul 27, 2018
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.

totalSupply should be a function, not state variable

4 participants