写一份ARB链上的代币合约
pragma solidity ^0.8.0;
// 定义代币合约 contract MyToken { // 代币的名称 string public name; // 代币的符号 string public symbol; // 代币的小数位数 uint8 public decimals; // 代币的总发行量 uint256 public totalSupply;
// 存储每个账户的余额
mapping (address => uint256) public balanceOf;
// 存储每个账户被冻结的余额
mapping (address => uint256) public frozenBalanceOf;
// 定义转账事件
event Transfer(address indexed from, address indexed to, uint256 value);
// 定义冻结事件
event Freeze(address indexed from, uint256 value);
// 定义解冻事件
event Unfreeze(address indexed from, uint256 value);
// 构造函数,初始化代币
constructor(uint256 initialSupply, string memory tokenName, string memory tokenSymbol, uint8 decimalUnits) {
name = tokenName;
symbol = tokenSymbol;
decimals = decimalUnits;
totalSupply = initialSupply * 10 ** uint256(decimals);
balanceOf[msg.sender] = totalSupply;
}
// 实现转账
function transfer(address to, uint256 value) public {
// 检查发送者的余额是否足够转账
require(balanceOf[msg.sender] >= value, "Not enough balance");
// 更新余额
balanceOf[msg.sender] -= value;
balanceOf[to] += value;
// 触发转账事件
emit Transfer(msg.sender, to, value);
}
// 冻结账户余额
function freeze(uint256 value) public {
// 检查发送者的余额是否足够冻结
require(balanceOf[msg.sender] >= value, "Not enough balance to freeze");
// 更新余额和冻结余额
balanceOf[msg.sender] -= value;
frozenBalanceOf[msg.sender] += value;
// 触发冻结事件
emit Freeze(msg.sender, value);
}
// 解冻账户余额
function unfreeze(uint256 value) public {
// 检查发送者的冻结余额是否足够解冻
require(frozenBalanceOf[msg.sender] >= value, "Not enough frozen balance to unfreeze");
// 更新余额和冻结余额
balanceOf[msg.sender] += value;
frozenBalanceOf[msg.sender] -= value;
// 触发解冻事件
emit Unfreeze(msg.sender, value);
}
原文地址: https://www.cveoy.top/t/topic/eFRt 著作权归作者所有。请勿转载和采集!