solidity学习(五)---部署合约发行测试币

注:本教程为技术教程,不谈论且不涉及炒作任何数字货币

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import "./IERC20.sol";
import "./extensions/IERC20Metadata.sol";
import "../../utils/Context.sol";

/**
* https://forum.zeppelin.solutions/t/how-to-implement-erc20-supply-mechanisms/226
*allowance:消费限额   
*
 */

contract ERC20 is Context, IERC20, IERC20Metadata {
    //定义了一个合约叫ERC20,is“继承”
    //address是地址类型
    mapping(address => uint256) private _balances;
    //address在_balance在字典里映射256位一个值,仅当前合约可见;
    //【这里要修改】 字典是不是balance

    mapping(address => mapping(address => uint256)) private _allowances;
    //这里是一个嵌套,从外到里看,首先address映射到另外一个小mapping中,其次小mapping运算后得出一个256位的值,
    //所以,大mapping得出一个256位的值

    uint256 private _totalSupply;
    string private _name;
    string private _symbol;

    /**
     * 设置name symbol
     *decimals翻译为小数,这里理解为精度,默认是18位,只设置一次
     *constructor构造函数,部署运行且只运行一次,构造了2个临时的变量;(初始化)
     *storage变量是永久的,memory是临时的、当外部函数对某合约调用完成时,内存型变量即被移除。
     */
    constructor(string memory name_, string memory symbol_) {

        _name = name_;
        _symbol = symbol_;
    }

    /**
     * 返回标记的名称
     *
     */
    function name() public view virtual override returns (string memory) {
        return _name;
    }

    /**
     * @dev返回代币的名字,一般是缩写???
     * 
     */
    function symbol() public view virtual override returns (string memory) {
        return _symbol;
    }

    /**
     *显示这个数的“精度”,例如如果是2,505则表示5.05
     */
    function decimals() public view virtual override returns (uint8) {
        return 18;
    }

    /**
     *_totalSupply是代币总量。
     */
    function totalSupply() public view virtual override returns (uint256) {
        return _totalSupply;
    }

    /**
     * 返回账户余额
     */
    function balanceOf(address account) public view virtual override returns (uint256) {
        return _balances[account];//这个_balances是参数
    }

    /**
     * Requirements:
     *接收者的地址不为空,调用者要有额度
     */
    function transfer(address recipient, uint256 amount) public virtual override returns (bool) {
        _transfer(_msgSender(), recipient, amount);
        return true;
    }

    /**
     * @dev See {IERC20-allowance}.
     */
    function allowance(address owner, address spender) public virtual override returns (uint256) {
        // allowance理解为“转移”;view是修饰符的函数,查看某种状态,在接下来的调用函数不会消耗gas;
        // virtual继承    override函数重写  
        //父合约标记为 virtual 函数可以在继承合约里重写(overridden)以更改他们的行为。
        //重写的函数需要使用关键字 override 修饰。
        return _allowances[owner][spender];
    }

    /**
     * Requirements:
     * - `spender` cannot be the zero address.
     *把approve理解为授权,用户1授权spender有amount的额度。
     */
    function approve(address spender, uint256 amount) public virtual override returns (bool) {
        _approve(_msgSender(), spender, amount);
        return true;
    }

    /**
     * Requirements:
     * 发送者和接收者地址不为空
     * 发送者要有钱
     * 调用者要有额度(有发送者授权的额度)
     */
    function transferFrom(
        address sender,
        address recipient,
        uint256 amount
    ) public virtual override returns (bool) {
        _transfer(sender, recipient, amount);

        uint256 currentAllowance = _allowances[sender][_msgSender()];
        //看上面的allowance,是两次mapping得出的是一个数,而这个数就是两者的关系。
        require(currentAllowance >= amount, "ERC20: transfer amount exceeds allowance");
        //进行一个比较,比较 当前额度>=待转出的数值,如果成立则输出:。。。。
        unchecked {
            _approve(sender, _msgSender(), currentAllowance - amount);
        }
        //????这是不检查吗?【忽略】

        return true;
    }


    /**
     * increase和decrease allowance
     */
    function increaseAllowance(address spender, uint256 addedValue) public virtual returns (bool) {
        //增加权限
        _approve(_msgSender(), spender, _allowances[_msgSender()][spender] + addedValue);
        //因为是映射,所以得出两者相关的值,比如已经授权给B多少了,这个值就是初始值。加上刚刚输入的权限值,就为最终结果。
        return true;
    }

    function decreaseAllowance(address spender, uint256 subtractedValue) public virtual returns (bool) {
        uint256 currentAllowance = _allowances[_msgSender()][spender];
        require(currentAllowance >= subtractedValue, "ERC20: decreased allowance below zero");
        //当前额度>=减去额度,则显示:减少授权额度到零???
        unchecked {
            _approve(_msgSender(), spender, currentAllowance - subtractedValue);
        }

        return true;
    }

    /**
     * @dev Moves `amount` of tokens from `sender` to `recipient`.
     *
     * This internal function is equivalent to {transfer}, and can be used to
     * e.g. implement automatic token fees, slashing mechanisms, etc.
     *
     * Emits a {Transfer} event.
     *
     * Requirements:
     *
     * - `sender` cannot be the zero address.
     * - `recipient` cannot be the zero address.
     * - `sender` must have a balance of at least `amount`.
     */
    function _transfer(
        address sender,
        address recipient,
        uint256 amount
    ) internal virtual {
        require(sender != address(0), "ERC20: transfer from the zero address");
        //发送者地址不为空,显示传递者为空
        require(recipient != address(0), "ERC20: transfer to the zero address");
        //接收者不为空
        _beforeTokenTransfer(sender, recipient, amount);

        uint256 senderBalance = _balances[sender];
        //什么意思啊
        require(senderBalance >= amount, "ERC20: transfer amount exceeds balance");
        //超过平衡
        unchecked {
            _balances[sender] = senderBalance - amount;
        }
        _balances[recipient] += amount;

        emit Transfer(sender, recipient, amount);
        //触发一个事件用emit说明,这里触发Transfer事件 。。。。不显示,为了让前端找结果
    

        _afterTokenTransfer(sender, recipient, amount);
        //这句话的语法不太懂
    }

    /** @dev Creates `amount` tokens and assigns them to `account`, increasing
     * the total supply.
     *
     * Emits a {Transfer} event with `from` set to the zero address.
     *
     * Requirements:
     *
     * - `account` cannot be the zero address.
     */
    function _mint(address account, uint256 amount) internal virtual {
        require(account != address(0), "ERC20: mint to the zero address");

        _beforeTokenTransfer(address(0), account, amount);

        _totalSupply += amount;
        _balances[account] += amount;
        emit Transfer(address(0), account, amount);//0--x是挖矿

        _afterTokenTransfer(address(0), account, amount);
    }

    /**
     * @dev Destroys `amount` tokens from `account`, reducing the
     * total supply.
     *销毁
     */
    function _burn(address account, uint256 amount) internal virtual {
        require(account != address(0), "ERC20: burn from the zero address");//x--0是铸币

        _beforeTokenTransfer(account, address(0), amount);

        uint256 accountBalance = _balances[account];
        require(accountBalance >= amount, "ERC20: burn amount exceeds balance(超过平衡)");
        unchecked {
            _balances[account] = accountBalance - amount;
        }
        _totalSupply -= amount;

        emit Transfer(account, address(0), amount);

        _afterTokenTransfer(account, address(0), amount);
    }

    /**
     * @dev Sets `amount` as the allowance of `spender` over the `owner` s tokens.
     *
     * This internal function is equivalent to `approve`, and can be used to
     * e.g. set automatic allowances for certain subsystems, etc.
     *
     * Emits an {Approval} event.
     *
     * Requirements:
     *
     * - `owner` cannot be the zero address.
     * - `spender` cannot be the zero address.
     */
    function _approve(address owner,address spender,uint256 amount) internal virtual {
        require(owner != address(0), "ERC20: approve from the zero address");
        require(spender != address(0), "ERC20: approve to the zero address");

        _allowances[owner][spender] = amount;
        emit Approval(owner, spender, amount);
    }

    /**
     * @dev Hook that is called before any transfer of tokens. This includes
     * minting and burning.
     *
     * Calling conditions:
     *
     * - when `from` and `to` are both non-zero, `amount` of ``from``'s tokens
     * will be transferred to `to`.
     * - when `from` is zero, `amount` tokens will be minted for `to`.
     * - when `to` is zero, `amount` of ``from``'s tokens will be burned.
     * - `from` and `to` are never both zero.
     *
     * To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
     */
    function _beforeTokenTransfer(address from,address to,uint256 amount) internal virtual {}

    function _afterTokenTransfer(
        address from,
        address to,
        uint256 amount
    ) internal virtual {}
}
珍惜时间,只错一次
原文地址:https://www.cnblogs.com/TEAM0N/p/14964581.html