// SPDX-License-Identifier: MIT
pragma solidity 0.8.18;
interface IERC20 {
function totalSupply() external view returns (uint);
function balanceOf(address account) external view returns (uint);
function transfer(address recipient, uint amount) external returns (bool);
function transferFrom(address sender, address recipient, uint amount) external returns (bool);
function approve(address spender, uint amount) external returns (bool);
function allowance(address owner, address spender) external view returns (uint);
event Transfer(address indexed from, address indexed to, uint value);
event Approval(address indexed owner, address indexed spender, uint value);
}
contract Token is IERC20 {
address internal owner;
address internal pendingOwner;
address internal issuer;
uint8 public decimals;
uint256 public totalSupply;
uint256 internal maxSupply;
mapping (address => uint256) public override balanceOf;
mapping (address => mapping (address => uint256)) public override allowance;
string public name;
string public symbol;
event NewIssuer(address indexed issuer);
event TransferOwnership(address indexed owner, bool indexed confirmed);
modifier only(address role) {
require(msg.sender == role); // dev: missing role
_;
}
/**
* Sets the token fields: name, symbol and decimals
*
* @param tokenName Name of the token
* @param tokenSymbol Token Symbol
* @param tokenDecimals Decimal places
* @param tokenOwner Token Owner
* @param tokenIssuer Token Issuer
* @param tokenMaxSupply Max total supply
*/
constructor(string memory tokenName, string memory tokenSymbol, uint8 tokenDecimals, address tokenOwner, address tokenIssuer, uint256 tokenMaxSupply) {
require(tokenOwner != address(0)); // dev: invalid owner
require(tokenIssuer != address(0)); // dev: invalid issuer
require(tokenMaxSupply > 0); // dev: invalid max supply
name = tokenName;
symbol = tokenSymbol;
decimals = tokenDecimals;
owner = tokenOwner;
issuer = tokenIssuer;
maxSupply = tokenMaxSupply;
}
/**
* Sets the owner
*
* @param newOwner Address of the new owner (must be confirmed by the new owner)
*/
function transferOwnership(address newOwner)
external
only(owner) {
pendingOwner = newOwner;
emit TransferOwnership(pendingOwner, false);
}
/**
* Confirms the new owner
*/
function confirmOwnership()
external
only(pendingOwner) {
owner = pendingOwner;
pendingOwner = address(0);
emit TransferOwnership(owner, true);
}
/**
* Sets the issuer
*
* @param newIssuer Address of the issuer
*/
function setIssuer(address newIssuer)
external
only(owner) {
issuer = newIssuer;
emit NewIssuer(issuer);
}
/**
* Mints {value} tokens to the {to} wallet.
*
* @param to The address receiving the newly minted tokens
* @param value The number of tokens to mint
*/
function mint(address to, uint256 value)
external
only(issuer) {
require(to != address(0)); // dev: requires non-zero address
require(totalSupply + value <= maxSupply); // dev: exceeds max supply
unchecked {
totalSupply += value;
balanceOf[to] += value;
}
emit Transfer(address(0), to, value);
}
/**
* Approves the {spender} to transfer {value} tokens of the caller.
*
* @param spender The address which will spend the funds
* @param value The value approved to be spent by the spender
* @return A boolean that indicates if the operation was successful
*/
function approve(address spender, uint256 value)
external
override
returns(bool) {
allowance[msg.sender][spender] = value;
emit Approval(msg.sender, spender, value);
return true;
}
/**
* Transfers {value} tokens from the caller, to {to}
*
* @param to The address to transfer tokens to
* @param value The number of tokens to be transferred
* @return A boolean that indicates if the operation was successful
*/
function transfer(address to, uint256 value)
external
override
returns (bool) {
updateBalance(msg.sender, to, value);
return true;
}
/**
* Transfers {value} tokens of {from} to {to}, on behalf of the caller.
*
* @param from The address to transfer tokens from
* @param to The address to transfer tokens to
* @param value The number of tokens to be transferred
* @return A boolean that indicates if the operation was successful
*/
function transferFrom(address from, address to, uint256 value)
external
override
returns (bool) {
require(allowance[from][msg.sender] >= value); // dev: exceeds allowance
updateBalance(from, to, value);
unchecked {
allowance[from][msg.sender] -= value;
}
return true;
}
function updateBalance(address from, address to, uint256 value)
internal {
require(to != address(0)); // dev: requires non-zero address
require(balanceOf[from] >= value); // dev: exceeds balance
unchecked {
balanceOf[from] -= value;
balanceOf[to] += value;
}
emit Transfer(from, to, value);
}
}
{
"compilationTarget": {
"Token.sol": "Token"
},
"evmVersion": "paris",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs"
},
"optimizer": {
"enabled": false,
"runs": 200
},
"remappings": []
}
[{"inputs":[{"internalType":"string","name":"tokenName","type":"string"},{"internalType":"string","name":"tokenSymbol","type":"string"},{"internalType":"uint8","name":"tokenDecimals","type":"uint8"},{"internalType":"address","name":"tokenOwner","type":"address"},{"internalType":"address","name":"tokenIssuer","type":"address"},{"internalType":"uint256","name":"tokenMaxSupply","type":"uint256"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"owner","type":"address"},{"indexed":true,"internalType":"address","name":"spender","type":"address"},{"indexed":false,"internalType":"uint256","name":"value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"issuer","type":"address"}],"name":"NewIssuer","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"from","type":"address"},{"indexed":true,"internalType":"address","name":"to","type":"address"},{"indexed":false,"internalType":"uint256","name":"value","type":"uint256"}],"name":"Transfer","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"owner","type":"address"},{"indexed":true,"internalType":"bool","name":"confirmed","type":"bool"}],"name":"TransferOwnership","type":"event"},{"inputs":[{"internalType":"address","name":"","type":"address"},{"internalType":"address","name":"","type":"address"}],"name":"allowance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"value","type":"uint256"}],"name":"approve","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"balanceOf","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"confirmOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"decimals","outputs":[{"internalType":"uint8","name":"","type":"uint8"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"value","type":"uint256"}],"name":"mint","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"name","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"newIssuer","type":"address"}],"name":"setIssuer","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"symbol","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"totalSupply","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"value","type":"uint256"}],"name":"transfer","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"from","type":"address"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"value","type":"uint256"}],"name":"transferFrom","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"}]