Description:
ERC20 token contract with Mintable, Burnable capabilities. Standard implementation for fungible tokens on Ethereum.
Blockchain: Ethereum
Source Code: View Code On The Blockchain
Solidity Source Code:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
// PEPSI代币合约
contract PEPSI {
// 代币基本信息
string private _name; // 代币名称
string private _symbol; // 代币符号
uint8 private _decimals; // 小数位数
// 代币供应量相关变量
uint256 private _totalSupply; // 总供应量
uint256 constant INITIAL_SUPPLY = 1000000000 * (10 ** 6); // 初始供应量10亿,考虑6位小数
// 余额映射
mapping(address => uint256) private _balances;
// 授权额度映射
mapping(address => mapping(address => uint256)) private _allowances;
// 合约所有者地址
address private _contractOwner;
// 转账税费比例(1%)
uint256 constant TAX_RATE = 100; // 1% = 100/10000
// 事件定义
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
// 构造函数
constructor() {
_name = "Pepsi";
_symbol = "PEPSI";
_decimals = 6;
_contractOwner = msg.sender;
// 将全部初始供应量分配给合约创建者
_totalSupply = INITIAL_SUPPLY;
_balances[msg.sender] = INITIAL_SUPPLY;
// 触发转账事件
emit Transfer(address(0), msg.sender, INITIAL_SUPPLY);
}
// 返回代币名称
function name() public view returns (string memory) {
return _name;
}
// 返回代币符号
function symbol() public view returns (string memory) {
return _symbol;
}
// 返回小数位数
function decimals() public view returns (uint8) {
return _decimals;
}
// 返回总供应量
function totalSupply() public view returns (uint256) {
return _totalSupply;
}
// 返回指定地址的余额
function balanceOf(address account) public view returns (uint256) {
return _balances[account];
}
// 转账函数
function transfer(address to, uint256 amount) public returns (bool) {
_transfer(msg.sender, to, amount);
return true;
}
// 查询授权额度
function allowance(address tokenOwner, address spender) public view returns (uint256) {
return _allowances[tokenOwner][spender];
}
// 授权函数
function approve(address spender, uint256 amount) public returns (bool) {
_approve(msg.sender, spender, amount);
return true;
}
// 授权转账函数
function transferFrom(address from, address to, uint256 amount) public returns (bool) {
_spendAllowance(from, msg.sender, amount);
_transfer(from, to, amount);
return true;
}
// 内部转账实现
function _transfer(address from, address to, uint256 amount) internal {
// 检查发送方地址是否有效
require(from != address(0), "ERC20: transfer from the zero address");
// 检查接收方地址是否有效
require(to != address(0), "ERC20: transfer to the zero address");
// 检查发送方余额是否足够
require(_balances[from] >= amount, "ERC20: transfer amount exceeds balance");
// 计算税费(1%)
uint256 tax = (amount * TAX_RATE) / 10000;
// 计算实际转账金额(扣除税费)
uint256 transferAmount = amount - tax;
// 更新发送方余额
unchecked {
_balances[from] -= amount;
}
// 更新接收方余额
unchecked {
_balances[to] += transferAmount;
}
// 将税费转入合约所有者账户
unchecked {
_balances[_contractOwner] += tax;
}
// 触发转账事件
emit Transfer(from, to, transferAmount);
// 触发税费转账事件
if (tax > 0) {
emit Transfer(from, _contractOwner, tax);
}
}
// 内部授权函数
function _approve(address tokenOwner, address spender, uint256 amount) internal {
// 检查所有者地址是否有效
require(tokenOwner != address(0), "ERC20: approve from the zero address");
// 检查授权方地址是否有效
require(spender != address(0), "ERC20: approve to the zero address");
// 设置授权额度
_allowances[tokenOwner][spender] = amount;
// 触发授权事件
emit Approval(tokenOwner, spender, amount);
}
// 内部授权额度消费函数
function _spendAllowance(address tokenOwner, address spender, uint256 amount) internal {
// 获取当前授权额度
uint256 currentAllowance = _allowances[tokenOwner][spender];
// 检查授权额度是否足够
if (currentAllowance != type(uint256).max) {
require(currentAllowance >= amount, "ERC20: insufficient allowance");
// 更新授权额度
unchecked {
_approve(tokenOwner, spender, currentAllowance - amount);
}
}
}
// 返回合约所有者地址
function owner() public view returns (address) {
return _contractOwner;
}
// 增加铸币功能(仅所有者可调用)
function mint(address to, uint256 amount) public {
require(msg.sender == _contractOwner, "ERC20: only owner can mint");
require(to != address(0), "ERC20: mint to the zero address");
_totalSupply += amount;
unchecked {
_balances[to] += amount;
}
emit Transfer(address(0), to, amount);
}
// 增加销毁功能
function burn(uint256 amount) public {
require(_balances[msg.sender] >= amount, "ERC20: burn amount exceeds balance");
unchecked {
_balances[msg.sender] -= amount;
_totalSupply -= amount;
}
emit Transfer(msg.sender, address(0), amount);
}
}
Submitted on: 2025-10-14 09:56:34
Comments
Log in to comment.
No comments yet.