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

Implement a compliant ERC20 without a name / symbol / decimal functions #27

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -290,3 +290,9 @@ Some tokens ([DAI, RAI, GLM, STAKE, CHAI, HAKKA, USDFL, HNY](https://github.com/
Some tokens (e.g., `cUSDCv3`) contain a special case for `amount == type(uint256).max` in their transfer functions that results in only the user's balance being transferred.

This may cause issues with systems that transfer a user-supplied `amount` to their contract and then credit the user with the same value in storage (e.g., Vault-type systems) without checking the amount that has actually been transferred.

## Tokens without a `name` / `symbol` / `decimal` functions

ERC20 compliant contracts do no need to implement the `name`, `symbol` nor `decimal` functions as specified by the [EIP20](https://github.com/ethereum/ercs/blob/master/ERCS/erc-20.md) since they are optional.

*example*: [ERC20WithoutOptional.sol](./src/ERC20WithoutOptional.sol)
66 changes: 66 additions & 0 deletions src/ERC20WithoutOptional.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright (C) 2017, 2018, 2019, 2020, 2024 dbrock, rain, mrchico, d-xo, sqrlfirst

Choose a reason for hiding this comment

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

hey @sqrlfirst I believe that copyright should always go to the owner of the repository!

Choose a reason for hiding this comment

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

Nop, it shall go depending on the CONTRIBUTING.md (which is absent from this repository) and who's doing the code writing... you know the author :)

Choose a reason for hiding this comment

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

hey @Magicking well! I leave it up to the repository owner :)

// SPDX-License-Identifier: AGPL-3.0-only

pragma solidity >=0.6.12;

contract Math {
// --- Math ---
function add(uint x, uint y) internal pure returns (uint z) {
require((z = x + y) >= x);
}

function sub(uint x, uint y) internal pure returns (uint z) {
require((z = x - y) <= x);
}
}

contract ERC20WithoutOptional is Math {
// --- ERC20 Data ---
string private constant name = "Token";
string private constant symbol = "TKN";
uint8 private decimals = 18;
uint256 public totalSupply;

mapping(address => uint) public balanceOf;
mapping(address => mapping(address => uint)) public allowance;

event Approval(address indexed src, address indexed guy, uint wad);
event Transfer(address indexed src, address indexed dst, uint wad);

// --- Init ---
constructor(uint _totalSupply) public {

Choose a reason for hiding this comment

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

remove public visibility in the constructor!

Choose a reason for hiding this comment

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

Thanks @luislucena16 but if you look line 4, (3 below the copyright), it mentions Solidity 0.6.12 which according to the documentation still mention « public »
See https://docs.soliditylang.org/en/v0.6.12/style-guide.html?#order-of-functions

Choose a reason for hiding this comment

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

yes, of course, but will the old version still be used even for the new features?..

Choose a reason for hiding this comment

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

It's accurate for the solidity compiler version specified, not sure what your words means.

totalSupply = _totalSupply;
balanceOf[msg.sender] = _totalSupply;
emit Transfer(address(0), msg.sender, _totalSupply);
}

// --- Token ---
function transfer(address dst, uint wad) public virtual returns (bool) {
return transferFrom(msg.sender, dst, wad);
}

function transferFrom(
address src,
address dst,
uint wad
) public virtual returns (bool) {
require(balanceOf[src] >= wad, "insufficient-balance");
if (src != msg.sender && allowance[src][msg.sender] != type(uint).max) {
require(
allowance[src][msg.sender] >= wad,
"insufficient-allowance"
);
allowance[src][msg.sender] = sub(allowance[src][msg.sender], wad);
}
balanceOf[src] = sub(balanceOf[src], wad);
balanceOf[dst] = add(balanceOf[dst], wad);
emit Transfer(src, dst, wad);
return true;
}

function approve(address usr, uint wad) public virtual returns (bool) {
allowance[msg.sender][usr] = wad;
emit Approval(msg.sender, usr, wad);
return true;
}
}