Openzeppelin学习记录三:token模块(ERC标准)
Posted 一袋芋头
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Openzeppelin学习记录三:token模块(ERC标准)相关的知识,希望对你有一定的参考价值。
Openzeppelin学习记录三:token模块(ERC标准:ERC20、ERC721)
Openzeppelin学习记录二:utils模块(SafeMath.sol+SafeCast.sol)
时间关系,明确了自己的任务,去做应用层的东西,所以就不怎么看合约了(起码不能如此细致的看了)今天上午先写了个ERC20的笔记,ERC721在看但是没写完还,后续有机会补上吧。
3.ERC标准
3.1 ERC20
先来看一下重要的函数
function transfer(address recipient, uint256 amount) public virtual override returns (bool);
function approve(address spender, uint256 amount) public virtual override returns (bool);
function transferFrom(
address sender,
address recipient,
uint256 amount
) public virtual override returns (bool);
注意:这里其实主要有两个操作,一个是transfer,即真正的转账,要扣除双方余额的。
另一个是approve,相当于津贴授权操作,此时只维护一个涉及到所有者owner,用钱方spender和金额的mapping(owner=>mapping(spender=>amount));即进行approve操作后,spender可以对owner的allowance的钱进行操作(钱并不转到自己名下,但可以用)。比如A approve B 50块,则B拥有了属于A的50块钱的操作权。
此时转账(比如A->C 30块)就有两种情况实现,一个就是亲自转账A调用transfer(A->C 30块)
另一个就是由B代A转账,B调用 transferFrom(A,C,30)
或者说B要转给C30块就有两种情况了,一个是B转自己余额的钱,另一个就是花A授予B的那部分津贴,扣除的其实是A的账户。(这种场景其实更贴合实际!)
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "./IERC20.sol";
import "./extensions/IERC20Metadata.sol";
import "../../utils/Context.sol";
/**
* @dev Implementation of the IERC20 interface.
*
* This implementation is agnostic to the way tokens are created. This means
* that a supply mechanism has to be added in a derived contract using _mint.
* For a generic mechanism see ERC20PresetMinterPauser.
*
* TIP: For a detailed writeup see our guide
* https://forum.zeppelin.solutions/t/how-to-implement-erc20-supply-mechanisms/226[How
* to implement supply mechanisms].
*
* We have followed general OpenZeppelin guidelines: functions revert instead
* of returning `false` on failure. This behavior is nonetheless conventional
* and does not conflict with the expectations of ERC20 applications.
*
* Additionally, an Approval event is emitted on calls to transferFrom.
* This allows applications to reconstruct the allowance for all accounts just
* by listening to said events. Other implementations of the EIP may not emit
* these events, as it isn't required by the specification.
*
* Finally, the non-standard decreaseAllowance and increaseAllowance
* functions have been added to mitigate the well-known issues around setting
* allowances. See IERC20-approve.
*/
contract ERC20 is Context, IERC20, IERC20Metadata
//个人余额
mapping(address => uint256) private _balances;
//转赠津贴1转给2
mapping(address => mapping(address => uint256)) private _allowances;
//代币总额
uint256 private _totalSupply;
string private _name;
string private _symbol;
/**
* @dev Sets the values for name and symbol.
*
* The default value of decimals is 18. To select a different value for
* decimals you should overload it.
*
* All two of these values are immutable: they can only be set once during
* construction.
*/
constructor(string memory name_, string memory symbol_)
_name = name_;
_symbol = symbol_;
/**
* @dev Returns the name of the token.
*/
function name() public view virtual override returns (string memory)
return _name;
/**
* @dev Returns the symbol of the token, usually a shorter version of the
* name.
*/
function symbol() public view virtual override returns (string memory)
return _symbol;
/**
* @dev Returns the number of decimals used to get its user representation.
* For example, if `decimals` equals `2`, a balance of `505` tokens should
* be displayed to a user as `5,05` (`505 / 10 ** 2`).
*
* Tokens usually opt for a value of 18, imitating the relationship between
* Ether and Wei. This is the value ERC20 uses, unless this function is
* overridden;
*
* NOTE: This information is only used for _display_ purposes: it in
* no way affects any of the arithmetic of the contract, including
* IERC20-balanceOf and IERC20-transfer.
*/
function decimals() public view virtual override returns (uint8)
return 18;
/**
* @dev See IERC20-totalSupply.
*/
function totalSupply() public view virtual override returns (uint256)
return _totalSupply;
/**
* @dev See IERC20-balanceOf.
*/
function balanceOf(address account) public view virtual override returns (uint256)
return _balances[account];
/**
* @dev See IERC20-transfer.
*
* Requirements:
*
* - `recipient` cannot be the zero address.
* - the caller must have a balance of at least `amount`.
*/
// msg.sender将amount转到recipient
function transfer(address recipient, uint256 amount) public virtual override returns (bool)
_transfer(_msgSender(), recipient, amount);
return true;
/**
* @dev See IERC20-allowance.
*/
// owner 授予 spender的津贴
function allowance(address owner, address spender) public view virtual override returns (uint256)
return _allowances[owner][spender];
/**
* @dev See IERC20-approve.
*
* Requirements:
*
* - `spender` cannot be the zero address.
*/
//msg.sender授予spender amount的津贴
function approve(address spender, uint256 amount) public virtual override returns (bool)
_approve(_msgSender(), spender, amount);
return true;
/**
* @dev See IERC20-transferFrom.
*
* Emits an Approval event indicating the updated allowance. This is not
* required by the EIP. See the note at the beginning of ERC20.
*
* Requirements:
*
* - `sender` and `recipient` cannot be the zero address.
* - `sender` must have a balance of at least `amount`.
* - the caller must have allowance for ``sender``'s tokens of at least
* `amount`.
*/
//应该是当已经有部分代笔已经转移所有权,然后再进行转账的时候的操作,此时还要更新转增的mapping,即调用_approve()
//例如:A已经approve了B 50块(即B拥有A 50块钱的操作权),现在A需要对C转账30块,B就可以代A转账,然后在A授予B的津贴中扣除30块。
function transferFrom(
address sender,
address recipient,
uint256 amount
) public virtual override returns (bool)
_transfer(sender, recipient, amount);
uint256 currentAllowance = _allowances[sender][_msgSender()];
require(currentAllowance >= amount, "ERC20: transfer amount exceeds allowance");
unchecked
_approve(sender, _msgSender(), currentAllowance - amount);
return true;
/**
* @dev Atomically increases the allowance granted to `spender` by the caller.
*
* This is an alternative to approve that can be used as a mitigation for
* problems described in IERC20-approve.
*
* Emits an Approval event indicating the updated allowance.
*
* Requirements:
*
* - `spender` cannot be the zero address.
*/
//msg.sender多授予一些addValue给spender
function increaseAllowance(address spender, uint256 addedValue) public virtual returns (bool)
_approve(_msgSender(), spender, _allowances[_msgSender()][spender] + addedValue);
return true;
/**
* @dev Atomically decreases the allowance granted to `spender` by the caller.
*
* This is an alternative to approve that can be used as a mitigation for
* problems described in IERC20-approve.
*
* Emits an Approval event indicating the updated allowance.
*
* Requirements:
*
* - `spender` cannot be the zero address.
* - `spender` must have allowance for the caller of at least
* `subtractedValue`.
*/
msg.sender少授予一些addValue给spender
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 tokens `amount` from `sender` to `recipient`.
*
* This is 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;
//0.8版本新特性:unchecked用来停用safemath检查
_balances[recipient] += amount;
emit Transfer(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);
/**
* @dev Destroys `amount` tokens from `account`, reducing the
* total supply.
*
* Emits a Transfer event with `to` set to the zero address.
*
* Requirements:
*
* - `account` cannot be the zero address.
* - `account` must have at least `amount` tokens.
*/
function _burn(address account, uint256 amount) internal virtual
require(account != address(0), "ERC20: burn from the zero address");
_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);
/**
* @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.
*/
//_approve():授权过程只是简单的改mapping的值,并没有改变双方余额
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 to 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
以上是关于Openzeppelin学习记录三:token模块(ERC标准)的主要内容,如果未能解决你的问题,请参考以下文章
Openzeppelin学习记录三:token模块(ERC标准)
Openzeppelin学习记录一:access模块(AccessControl.sol+Ownable.sol)
Openzeppelin学习记录一:access模块(AccessControl.sol+Ownable.sol)
Openzeppelin学习记录一:access模块(AccessControl.sol+Ownable.sol)