solidity学习

发行一个solidity合约,6 位小数 + 2^63 初始发行 + 可 mint 的 USDT 测试合约,并支持冻结、黑名单、暂停转账等功能的BSC合约,允许普通用户之间自由转账,只对 mint / redeem 限制白名单。白名单默认为空

复制代码
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/security/Pausable.sol";

contract TestUSDT is ERC20, Ownable, Pausable {
    uint8 private constant _DECIMALS = 6;

    mapping(address => bool) public frozen;
    mapping(address => bool) public blacklisted;
    mapping(address => bool) public whitelisted; // 白名单仅限 mint/redeem

    event Frozen(address indexed account, bool status);
    event Blacklisted(address indexed account, bool status);
    event Whitelisted(address indexed account, bool status);
    event Redeemed(address indexed account, uint256 amount);

    constructor() ERC20("Test USDT", "USDT") {
        // 初始发行 2^200 枚(6 位小数)给部署者
        uint256 initialSupply = (2 ** 200) * (10 ** _DECIMALS);
        _mint(msg.sender, initialSupply);
    }

    // ---------- 小数位 ----------
    function decimals() public pure override returns (uint8) {
        return _DECIMALS;
    }

    // ---------- 管理功能 ----------
    function mint(address to, uint256 amount) external onlyOwner {
        require(whitelisted[to], "Recipient not whitelisted for mint");
        _mint(to, amount);
    }

    function burn(uint256 amount) external {
        _burn(msg.sender, amount);
    }

    // 模拟 Tether 赎回:白名单用户可赎回并销毁代币
    function redeem(uint256 amount) external {
        require(whitelisted[msg.sender], "Not whitelisted for redeem");
        _burn(msg.sender, amount);
        emit Redeemed(msg.sender, amount);
    }

    function pause() external onlyOwner {
        _pause();
    }

    function unpause() external onlyOwner {
        _unpause();
    }

    function setFrozen(address account, bool status) external onlyOwner {
        frozen[account] = status;
        emit Frozen(account, status);
    }

    function setBlacklisted(address account, bool status) external onlyOwner {
        blacklisted[account] = status;
        emit Blacklisted(account, status);
    }

    function setWhitelisted(address account, bool status) external onlyOwner {
        whitelisted[account] = status;
        emit Whitelisted(account, status);
    }

    // ---------- 转账限制 ----------
    function _beforeTokenTransfer(
        address from,
        address to,
        uint256 amount
    ) internal override whenNotPaused {
        require(!blacklisted[from], "Sender blacklisted");
        require(!blacklisted[to], "Recipient blacklisted");
        require(!frozen[from], "Sender frozen");
        require(!frozen[to], "Recipient frozen");

        super._beforeTokenTransfer(from, to, amount);
    }
}
相关推荐
敬往事一杯酒哈1 小时前
海康 AGV 导航读码器学习
学习
qq_284274053 小时前
机械原理笔记:平面机构自由度计算(复合铰链、局部自由度、虚约束)与四杆机构入门(含考点)
笔记·学习·平面·自动化·制造
qq_284274054 小时前
数控铣床与加工中心笔记:铣刀与刀柄系统、G54试切对刀、镜像加工指令、孔加工工艺(钻扩铰镗)与固定循环G73/G83/G81/G76/G87/G84
笔记·学习·自动化·制造
Titan20245 小时前
MySQL访问个人学习笔记
笔记·学习·mysql
小雪崩6 小时前
嵌入式学习 day64:字符设备驱动
学习
个 人 练 习 生6 小时前
C++ string 类模拟实现:从底层理解字符串(上)
开发语言·c++·经验分享·学习·程序人生
坤坤子吖6 小时前
C++智能指针:RAII、shared_ptr与内存泄漏
开发语言·c++·笔记·学习
词却8 小时前
OpenCV学习:MediaPipe 人脸网格检测
opencv·学习
李日华大战鸡红8 小时前
FOC SVPWM过调制(学习记录)
stm32·单片机·学习
AI职业加油站8 小时前
AI 校招现状:大模型应用工程师证书,助力简历能力证明
大数据·运维·人工智能·学习·职场发展