// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0; // 版本号,使用大于0.8.0的版本编译器编译
import "@openzeppelin/contracts/token/ERC20/ERC20.sol"; // 使用openzeppelin库的ERC20
contract TestToken is ERC20{
address private factory; // 记录合约创建者地址
// 精度
function decimals() public view virtual override returns (uint8) {
return 18;
}
// 总发行量
function totalSupply() public view virtual override returns (uint) {
return 100000000000;
}
// 构造函数,发布合约时传入Token的name和symbol
constructor(string memory name, string memory symbol) ERC20 (name, symbol) {
factory = msg.sender; // 将合约创建者地址记录到factory参数
uint256 _totalSupply = totalSupply() * 10 ** uint256(decimals()); // 计算初始发行量
_mint(msg.sender, _totalSupply); // 将全部token铸给合约发布者
}
}
Solidity学习之路 - 一个简单的Token合约
于 2022-04-24 01:16:21 首次发布