SimpleERC20

Description:

ERC20 token contract with Mintable, Burnable capabilities. Standard implementation for fungible tokens on Ethereum.

Blockchain: Ethereum

Source Code: View Code On The Blockchain

Solidity Source Code:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

/// @title SimpleERC20 - 一个简单且标准兼容的 ERC20 代币合约模板
/// @notice 包含 owner 权限、mint、burn、标准 ERC20 接口
contract SimpleERC20 {
    // --- ERC20 基础数据 ---
    string public name;
    string public symbol;
    uint8 public immutable decimals = 18;
    uint256 public totalSupply;

    mapping(address => uint256) private _balances;
    mapping(address => mapping(address => uint256)) private _allowances;

    // --- 权限(简单 Ownable) ---
    address public owner;
    event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);

    // --- 标准事件 ---
    event Transfer(address indexed from, address indexed to, uint256 value);
    event Approval(address indexed owner, address indexed spender, uint256 value);

    // --- 代币相关事件(可选) ---
    event Mint(address indexed to, uint256 amount);
    event Burn(address indexed from, uint256 amount);

    // --- 修饰器 ---
    modifier onlyOwner() {
        require(msg.sender == owner, "SimpleERC20: caller is not the owner");
        _;
    }

    // --- 构造函数,部署时设定代币名、符号和初始发行量(单位为最小单位,考虑 decimals) ---
    /// @param _name 代币名,例如 "MyToken"
    /// @param _symbol 代币符号,例如 "MTK"
    /// @param _initialSupply 初始发行总量(按最小单位,通常乘以 10**decimals)
    constructor(string memory _name, string memory _symbol, uint256 _initialSupply) {
        name = _name;
        symbol = _symbol;
        owner = msg.sender;
        _mint(msg.sender, _initialSupply * (10 ** decimals));
    }

    // --- 所有权管理函数 ---
    function transferOwnership(address newOwner) external onlyOwner {
        require(newOwner != address(0), "SimpleERC20: new owner is the zero address");
        emit OwnershipTransferred(owner, newOwner);
        owner = newOwner;
    }

    // --- 读取余额/授权 ---
    function balanceOf(address account) external view returns (uint256) {
        return _balances[account];
    }

    function allowance(address tokenOwner, address spender) external view returns (uint256) {
        return _allowances[tokenOwner][spender];
    }

    // --- 转账函数 ---
    function transfer(address to, uint256 amount) external returns (bool) {
        _transfer(msg.sender, to, amount);
        return true;
    }

    function approve(address spender, uint256 amount) external returns (bool) {
        _approve(msg.sender, spender, amount);
        return true;
    }

    function transferFrom(address from, address to, uint256 amount) external returns (bool) {
        uint256 currentAllowance = _allowances[from][msg.sender];
        require(currentAllowance >= amount, "SimpleERC20: transfer amount exceeds allowance");
        unchecked { _approve(from, msg.sender, currentAllowance - amount); }
        _transfer(from, to, amount);
        return true;
    }

    // --- 增减 allowance 便捷函数 ---
    function increaseAllowance(address spender, uint256 addedValue) external returns (bool) {
        _approve(msg.sender, spender, _allowances[msg.sender][spender] + addedValue);
        return true;
    }

    function decreaseAllowance(address spender, uint256 subtractedValue) external returns (bool) {
        uint256 current = _allowances[msg.sender][spender];
        require(current >= subtractedValue, "SimpleERC20: decreased allowance below zero");
        unchecked { _approve(msg.sender, spender, current - subtractedValue); }
        return true;
    }

    // --- 内部转账实现 ---
    function _transfer(address from, address to, uint256 amount) internal {
        require(from != address(0), "SimpleERC20: transfer from the zero address");
        require(to != address(0), "SimpleERC20: transfer to the zero address");
        uint256 fromBalance = _balances[from];
        require(fromBalance >= amount, "SimpleERC20: transfer amount exceeds balance");
        unchecked {
            _balances[from] = fromBalance - amount;
            _balances[to] += amount;
        }
        emit Transfer(from, to, amount);
    }

    // --- Mint / Burn(mint 仅限 owner) ---
    /// @notice 创建新代币到指定地址(仅合约 owner)
    function mint(address to, uint256 amount) external onlyOwner returns (bool) {
        _mint(to, amount);
        return true;
    }

    /// @notice 销毁指定地址的代币(只有持币人能销毁自己的代币)
    function burn(uint256 amount) external returns (bool) {
        _burn(msg.sender, amount);
        return true;
    }

    /// @notice 销毁指定地址的代币(需要 allowance 或 owner 权限)
    function burnFrom(address account, uint256 amount) external returns (bool) {
        if (msg.sender != owner) {
            uint256 currentAllowance = _allowances[account][msg.sender];
            require(currentAllowance >= amount, "SimpleERC20: burn amount exceeds allowance");
            unchecked { _approve(account, msg.sender, currentAllowance - amount); }
        }
        _burn(account, amount);
        return true;
    }

    // --- 内部 mint/burn 实现 ---
    function _mint(address to, uint256 amount) internal {
        require(to != address(0), "SimpleERC20: mint to the zero address");
        totalSupply += amount;
        _balances[to] += amount;
        emit Mint(to, amount);
        emit Transfer(address(0), to, amount);
    }

    function _burn(address from, uint256 amount) internal {
        require(from != address(0), "SimpleERC20: burn from the zero address");
        uint256 accountBalance = _balances[from];
        require(accountBalance >= amount, "SimpleERC20: burn amount exceeds balance");
        unchecked {
            _balances[from] = accountBalance - amount;
            totalSupply -= amount;
        }
        emit Burn(from, amount);
        emit Transfer(from, address(0), amount);
    }

    // --- 内部 approve 实现 ---
    function _approve(address tokenOwner, address spender, uint256 amount) internal {
        require(tokenOwner != address(0), "SimpleERC20: approve from the zero address");
        require(spender != address(0), "SimpleERC20: approve to the zero address");
        _allowances[tokenOwner][spender] = amount;
        emit Approval(tokenOwner, spender, amount);
    }
}

Tags:
ERC20, Token, Mintable, Burnable|addr:0x262641d5eed813fa21f6dac1d1348f2678ad9d12|verified:true|block:23610576|tx:0x80ff55d3aa136b1dbb8f6da11bd311c51d7228a3d605fb9d0bf2c15595c27ffe|first_check:1760872127

Submitted on: 2025-10-19 13:08:49

Comments

Log in to comment.

No comments yet.