contract MyToken is ERC20 {} 实现ERC20构造函数
时间: 2024-09-07 07:03:47 浏览: 36
在Solidity中,`ERC20` 是一个代币的标准接口,它继承自`ERC20Basic`接口,并添加了一些额外的函数。要实现一个遵循ERC20标准的代币合约,你需要定义合约并实现接口中声明的所有必要函数和事件。构造函数是在部署合约时初始化合约状态的地方。
以下是使用Solidity语言编写的一个简单的`ERC20`代币合约示例,包括构造函数的实现:
```solidity
pragma solidity ^0.8.0;
interface ERC20 {
function totalSupply() external view returns (uint256);
function balanceOf(address account) external view returns (uint256);
function transfer(address recipient, uint256 amount) external returns (bool);
function allowance(address owner, address spender) external view returns (uint256);
function approve(address spender, uint256 amount) external returns (bool);
function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
}
contract MyToken is ERC20 {
string public constant name = "MyToken";
string public constant symbol = "MTK";
uint8 public constant decimals = 18;
uint256 private _totalSupply;
mapping(address => uint256) private _balances;
mapping(address => mapping(address => uint256)) private _allowances;
constructor(uint256 totalSupply) {
_totalSupply = totalSupply;
_balances[msg.sender] = totalSupply;
emit Transfer(address(0), msg.sender, totalSupply);
}
function totalSupply() public view override returns (uint256) {
return _totalSupply;
}
function balanceOf(address account) public view override returns (uint256) {
return _balances[account];
}
function transfer(address recipient, uint256 amount) public override returns (bool) {
require(recipient != address(0), "Transfer to the zero address");
require(_balances[msg.sender] >= amount, "Transfer amount exceeds balance");
_balances[msg.sender] -= amount;
_balances[recipient] += amount;
emit Transfer(msg.sender, recipient, amount);
return true;
}
function allowance(address owner, address spender) public view override returns (uint256) {
return _allowances[owner][spender];
}
function approve(address spender, uint256 amount) public override returns (bool) {
require(spender != address(0), "Approve to the zero address");
_allowances[msg.sender][spender] = amount;
emit Approval(msg.sender, spender, amount);
return true;
}
function transferFrom(address sender, address recipient, uint256 amount) public override returns (bool) {
require(recipient != address(0), "Transfer to the zero address");
require(_balances[sender] >= amount, "Transfer amount exceeds balance");
require(_allowances[sender][msg.sender] >= amount, "Transfer amount exceeds allowance");
_balances[sender] -= amount;
_balances[recipient] += amount;
_allowances[sender][msg.sender] -= amount;
emit Transfer(sender, recipient, amount);
return true;
}
}
```
在这个示例中,我们首先声明了一个符合`ERC20`接口的合约`MyToken`。然后我们定义了几个重要的变量:代币名称、代币符号、小数位数以及总供应量。在构造函数中,我们初始化了代币的总供应量,并将所有代币分配给合约部署者的地址。同时,我们触发了一个`Transfer`事件来记录从零地址到部署者地址的代币转移。
这个示例合约展示了最基本的ERC20代币实现,包括代币的创建、分配和转移。在实际应用中,你可能需要添加更多功能和安全措施,比如限制供应量的增发、实施更复杂的授权机制、增加代币的销毁功能等。
阅读全文