🌑

帮帮技术站

erc20关键字

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.8.0) (token/ERC20/ERC20.sol)

pragma solidity ^0.8.0;

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

contract ERC20 is Context, IERC20, IERC20Metadata {
mapping(address => uint256) private _balances;

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

uint256 private _totalSupply=100000000000000000;
//代币名字
string private _name;
//代币简称
string private _symbol;

构造函数
constructor(string memory name_, string memory symbol_) {
_name = name_;
_symbol = symbol_;
}

//查询代币名称
function name() public view virtual override returns (string memory) {
return _name;
}

//查询代币简称
function symbol() public view virtual override returns (string memory) {
return _symbol;
}

//小数点18位
function decimals() public view virtual override returns (uint8) {
return 18;
}

//查询代币额
function totalSupply() public view virtual override returns (uint256) {
return _totalSupply;
}

//查询某个账户余额
function balanceOf(address account) public view virtual override returns (uint256) {
return _balances[account];
}

//交易给to某个数量的代币
function transfer(address to, uint256 amount) public virtual override returns (bool) {
address owner = _msgSender();
_transfer(owner, to, amount);
return true;
}

//查询 某个人owner是否授权给spender花钱人
function allowance(address owner, address spender) public view virtual override returns (uint256) {
return _allowances[owner][spender];
}

//授权给花钱人spender的额度amount uint(-1)=授权无限数量的代币
function approve(address spender, uint256 amount) public virtual override returns (bool) {
address owner = _msgSender();
_approve(owner, spender, amount);
return true;
}

//交易 从from转账给to某个金额的代币amount
function transferFrom(
address from,
address to,
uint256 amount
) public virtual override returns (bool) {
// 获取当前合约调用者的地址
address spender = _msgSender();
// 将from地址授权给spender地址的代币数量减少amount 消耗了的授权数量
_spendAllowance(from, spender, amount);
// 从from地址转移amount数量代币到to地址
_transfer(from, to, amount);
return true;
}

//增加授权额度
function increaseAllowance(address spender, uint256 addedValue) public virtual returns (bool) {
// 获取当前调用合约的账号地址
address owner = _msgSender();
// 重新授权增加过后的代币数量从owner地址给spender地址
_approve(owner, spender, allowance(owner, spender) + addedValue);
return true;
}

//减少授权额度
function decreaseAllowance(address spender, uint256 subtractedValue) public virtual returns (bool) {
// 获取当前调用合约的账号地址
address owner = _msgSender();
/ 获取当前owner授权给spender代币数量
uint256 currentAllowance = allowance(owner, spender);
// 检查 当前以授权代币数量 currentAllowance 是否大于等于subtractedValue
require(currentAllowance >= subtractedValue, "ERC20: decreased allowance below zero");
// 重新授权减少过后的代币数量从owner地址给spender地址
// unchecked 关闭默认的溢出检查check,可以较少gas费
unchecked {
_approve(owner, spender, currentAllowance - subtractedValue);
}

return true;
}

//内部交易函数
function _transfer(
address from,
address to,
uint256 amount
) internal virtual {
// 检查 from地址是否为空地址
require(from != address(0), "ERC20: transfer from the zero address");
// 检查 to地址不为空地址
require(to != address(0), "ERC20: transfer to the zero address");
// 转移前调用的函数
_beforeTokenTransfer(from, to, amount);
// 获取from账户余额
uint256 fromBalance = _balances[from];
/// 检查账户余额 fromBalance 是否大于 要转移的代币数量amount
require(fromBalance >= amount, "ERC20: transfer amount exceeds balance");
unchecked {
// 减少from地址代币数量
_balances[from] = fromBalance - amount;
// Overflow not possible: the sum of all balances is capped by totalSupply, and the sum is preserved by
// decrementing then incrementing.
// 增加to地址代币数量
_balances[to] += amount;
}
// 触发Transfer事件 记录转移事件
emit Transfer(from, to, amount);
// 转移代币之后定义的函数
_afterTokenTransfer(from, to, amount);
}

// 铸造新代币 并将代币转移到account地址
function _mint(address account, uint256 amount) internal virtual {
// 检查 account 是否为空地址
require(account != address(0), "ERC20: mint to the zero address");
// 转移代币前定义的函数
_beforeTokenTransfer(address(0), account, amount);
// 增加总供应量
_totalSupply += amount;
unchecked {
// Overflow not possible: balance + amount is at most totalSupply + amount, which is checked above.
// 增加account地址代币余额
_balances[account] += amount;
}
// 触发代币转移事件,记录代币转移信息
emit Transfer(address(0), account, amount);
// 转移代币之后定义的函数
_afterTokenTransfer(address(0), account, amount);
}

//销毁代币
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;
// Overflow not possible: amount <= accountBalance <= totalSupply.
// 减少总供应量
_totalSupply -= amount;
}
// 触发代币转移事件,记录代币转移信息
emit Transfer(account, address(0), amount);
// 转移代币之后定义的函数
_afterTokenTransfer(account, address(0), amount);
}

//将owner的代币授权给spender
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");
// 授权amount数量代币
_allowances[owner][spender] = amount;
// 触发代币授权事件,记录代币授权信息
emit Approval(owner, spender, amount);
}


// 将owner地址授权给spender地址的代币数量减少amount
// 将授权的的代币数量减少amout,用在transfer函数内调用
function _spendAllowance(
address owner,
address spender,
uint256 amount
) internal virtual {
// 获取当前授权代币数量
uint256 currentAllowance = allowance(owner, spender);
// 判断当前代币数量是否等于uint256代币最大值
if (currentAllowance != type(uint256).max) {
// 检查 授权代币余额是否大于减少的代币数量
require(currentAllowance >= amount, "ERC20: insufficient allowance");
unchecked {
// 减少代币授权数量
_approve(owner, spender, currentAllowance - amount);
}
}
}


// 在转移代币发生前的函数,包含mint 和 burn
function _beforeTokenTransfer(
address from,
address to,
uint256 amount
) internal virtual {}

// 在转移代币发生后的函数,包含mint 和 burn
function _afterTokenTransfer(
address from,
address to,
uint256 amount
) internal virtual {}
}

— Nov 10, 2022

Search