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);
    }
}
相关推荐
Lester_11012 小时前
嵌入式学习笔记 - 自举电路
笔记·嵌入式硬件·学习
驱动探索者2 小时前
linux genpool 学习
java·linux·学习
yukai080082 小时前
【最后203篇系列】036 MDP学习思考
人工智能·学习
2501_901147832 小时前
PyTorch DDP官方文档学习笔记(核心干货版)
pytorch·笔记·学习·算法·面试
rannn_1112 小时前
【苍穹外卖|Day3】公共字段自动填充、新增菜品功能、菜品分页查询功能、删除菜品功能、修改菜品功能、起售停售菜品
java·spring boot·后端·学习·项目
wdfk_prog2 小时前
[Linux]学习笔记系列 -- [drivers][mmc]mmc_sd
linux·笔记·学习
整点薯条7782 小时前
2026 智能体技术解析:核心架构、能力边界与学习价值评估
学习·架构
怪谈爱好者CMZ3 小时前
【C++学习笔记】图论-最短路径Dijkstra算法
c++·笔记·学习
试试勇气3 小时前
Linux学习笔记(十四)--进程间通信
linux·笔记·学习