Skip to content

Instantly share code, notes, and snippets.

@kn1g
Created October 10, 2022 15:14
Show Gist options
  • Save kn1g/18b0dbc99137149c9234f007a0d84bfe to your computer and use it in GitHub Desktop.
Save kn1g/18b0dbc99137149c9234f007a0d84bfe to your computer and use it in GitHub Desktop.
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity 0.8.11;
interface IRegistry {
function register(uint8 _decimals) external;
}
// https://raw.githubusercontent.com/Rari-Capital/solmate/main/src/tokens/ERC20.sol
contract ERC20 {
/*///////////////////////////////////////////////////////////////
EVENTS
//////////////////////////////////////////////////////////////*/
event Transfer(address indexed from, address indexed to, uint256 amount);
event Approval(address indexed owner, address indexed spender, uint256 amount);
/*///////////////////////////////////////////////////////////////
METADATA STORAGE
//////////////////////////////////////////////////////////////*/
string public name;
string public symbol;
uint8 public immutable decimals;
/*///////////////////////////////////////////////////////////////
ERC20 STORAGE
//////////////////////////////////////////////////////////////*/
uint256 public totalSupply;
mapping(address => uint256) public balanceOf;
mapping(address => mapping(address => uint256)) public allowance;
/*///////////////////////////////////////////////////////////////
CONSTRUCTOR
//////////////////////////////////////////////////////////////*/
constructor(
string memory _name,
string memory _symbol,
uint8 _decimals
) {
name = _name;
symbol = _symbol;
decimals = _decimals;
}
/*///////////////////////////////////////////////////////////////
ERC20 LOGIC
//////////////////////////////////////////////////////////////*/
function approve(address spender, uint256 amount) public virtual returns (bool) {
allowance[msg.sender][spender] = amount;
emit Approval(msg.sender, spender, amount);
return true;
}
function transfer(address to, uint256 amount) public virtual returns (bool) {
// Cannot overflow because the sum of all user
// balances can't exceed the max uint256 value.
unchecked {
balanceOf[to] += amount;
}
emit Transfer(msg.sender, to, amount);
return true;
}
function transferFrom(
address from,
address to,
uint256 amount
) public virtual returns (bool) {
uint256 allowed = allowance[from][msg.sender]; // Saves gas for limited approvals.
if (allowed != type(uint256).max) allowance[from][msg.sender] = allowed - amount;
// Cannot overflow because the sum of all user
// balances can't exceed the max uint256 value.
unchecked {
balanceOf[to] += amount;
}
emit Transfer(from, to, amount);
return true;
}
/*///////////////////////////////////////////////////////////////
INTERNAL MINT/BURN LOGIC
//////////////////////////////////////////////////////////////*/
function _mint(address to, uint256 amount) internal virtual {
totalSupply += amount;
// Cannot overflow because the sum of all user
// balances can't exceed the max uint256 value.
unchecked {
balanceOf[to] += amount;
}
emit Transfer(address(0), to, amount);
}
function _burn(address from, uint256 amount) internal virtual {
balanceOf[from] -= amount;
// Cannot underflow because a user's balance
// will never be larger than the total supply.
unchecked {
totalSupply -= amount;
}
emit Transfer(from, address(0), amount);
}
}
contract MyToken is ERC20{
// Please change those as you like
string constant MY_NAME = "NicoRichCoin";
string constant MY_SYMBOL = "NITO";
uint8 constant MY_DECIMALS = 18;
uint constant INITIAL_SUPPLY = 10**7;
// Please leave unchanged
address constant reg_addr = 0x5B174940a5C631A9B88F2e1A8c6439Ff88eF9F05;
constructor() ERC20(MY_NAME, MY_SYMBOL, MY_DECIMALS){
// Number of tokens that will exist
_mint(msg.sender, INITIAL_SUPPLY * 10 ** MY_DECIMALS);
// Please leave this untouched
IRegistry registry = IRegistry(reg_addr);
registry.register(MY_DECIMALS);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment