1. 程式人生 > 其它 >solidity學習(五)---部署合約發行測試幣

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 {} }