Contract Source Code:
File 1 of 1 : Coin
// Copyright (C) 2017, 2018, 2019 dbrock, rain, mrchico
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
pragma solidity 0.6.7;
contract Coin {
// --- Auth ---
mapping (address => uint256) public authorizedAccounts;
function addAuthorization(address account) external isAuthorized {
authorizedAccounts[account] = 1;
emit AddAuthorization(account);
}
function removeAuthorization(address account) external isAuthorized {
authorizedAccounts[account] = 0;
emit RemoveAuthorization(account);
}
modifier isAuthorized {
require(authorizedAccounts[msg.sender] == 1, "Coin/account-not-authorized");
_;
}
// --- ERC20 Data ---
string public name;
string public symbol;
string public version = "1";
uint8 public constant decimals = 18;
uint256 public chainId;
uint256 public totalSupply;
mapping (address => uint256) public balanceOf;
mapping (address => mapping (address => uint256)) public allowance;
mapping (address => uint256) public nonces;
// --- Events ---
event AddAuthorization(address account);
event RemoveAuthorization(address account);
event Approval(address indexed src, address indexed guy, uint256 amount);
event Transfer(address indexed src, address indexed dst, uint256 amount);
// --- Math ---
function addition(uint256 x, uint256 y) internal pure returns (uint256 z) {
require((z = x + y) >= x, "Coin/add-overflow");
}
function subtract(uint256 x, uint256 y) internal pure returns (uint256 z) {
require((z = x - y) <= x, "Coin/sub-underflow");
}
// --- EIP712 niceties ---
bytes32 public DOMAIN_SEPARATOR;
// bytes32 public constant PERMIT_TYPEHASH = keccak256("Permit(address holder,address spender,uint256 nonce,uint256 expiry,bool allowed)");
bytes32 public constant PERMIT_TYPEHASH = 0xea2aa0a1be11a07ed86d755c93467f4f82362b452371d1ba94d1715123511acb;
constructor(
string memory name_,
string memory symbol_,
uint256 chainId_
) public {
authorizedAccounts[msg.sender] = 1;
name = name_;
symbol = symbol_;
chainId = chainId_;
DOMAIN_SEPARATOR = keccak256(abi.encode(
keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)"),
keccak256(bytes(name)),
keccak256(bytes(version)),
chainId_,
address(this)
));
emit AddAuthorization(msg.sender);
}
// --- Token ---
function transfer(address dst, uint256 amount) external returns (bool) {
return transferFrom(msg.sender, dst, amount);
}
function transferFrom(address src, address dst, uint256 amount)
public returns (bool)
{
require(dst != address(0), "Coin/null-dst");
require(dst != address(this), "Coin/dst-cannot-be-this-contract");
require(balanceOf[src] >= amount, "Coin/insufficient-balance");
if (src != msg.sender && allowance[src][msg.sender] != uint256(-1)) {
require(allowance[src][msg.sender] >= amount, "Coin/insufficient-allowance");
allowance[src][msg.sender] = subtract(allowance[src][msg.sender], amount);
}
balanceOf[src] = subtract(balanceOf[src], amount);
balanceOf[dst] = addition(balanceOf[dst], amount);
emit Transfer(src, dst, amount);
return true;
}
function mint(address usr, uint256 amount) external isAuthorized {
balanceOf[usr] = addition(balanceOf[usr], amount);
totalSupply = addition(totalSupply, amount);
emit Transfer(address(0), usr, amount);
}
function burn(address usr, uint256 amount) external {
require(balanceOf[usr] >= amount, "Coin/insufficient-balance");
if (usr != msg.sender && allowance[usr][msg.sender] != uint256(-1)) {
require(allowance[usr][msg.sender] >= amount, "Coin/insufficient-allowance");
allowance[usr][msg.sender] = subtract(allowance[usr][msg.sender], amount);
}
balanceOf[usr] = subtract(balanceOf[usr], amount);
totalSupply = subtract(totalSupply, amount);
emit Transfer(usr, address(0), amount);
}
function approve(address usr, uint256 amount) external returns (bool) {
allowance[msg.sender][usr] = amount;
emit Approval(msg.sender, usr, amount);
return true;
}
// --- Alias ---
function push(address usr, uint256 amount) external {
transferFrom(msg.sender, usr, amount);
}
function pull(address usr, uint256 amount) external {
transferFrom(usr, msg.sender, amount);
}
function move(address src, address dst, uint256 amount) external {
transferFrom(src, dst, amount);
}
// --- Approve by signature ---
function permit(
address holder,
address spender,
uint256 nonce,
uint256 expiry,
bool allowed,
uint8 v,
bytes32 r,
bytes32 s
) external
{
bytes32 digest =
keccak256(abi.encodePacked(
"\x19\x01",
DOMAIN_SEPARATOR,
keccak256(abi.encode(PERMIT_TYPEHASH,
holder,
spender,
nonce,
expiry,
allowed))
));
require(holder != address(0), "Coin/invalid-address-0");
require(holder == ecrecover(digest, v, r, s), "Coin/invalid-permit");
require(expiry == 0 || now <= expiry, "Coin/permit-expired");
require(nonce == nonces[holder]++, "Coin/invalid-nonce");
uint256 wad = allowed ? uint256(-1) : 0;
allowance[holder][spender] = wad;
emit Approval(holder, spender, wad);
}
}