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);
    }
}
相关推荐
你怎么知道我是队长19 小时前
前端学习---HTML---块元素和行内元素
前端·学习·html
saoys20 小时前
Opencv 学习笔记:腐蚀操作 + 轮廓标记 + 分水岭分割
笔记·opencv·学习
saoys20 小时前
Opencv 学习笔记:距离变换(DIST_L1 算法实战 + 归一化)
笔记·opencv·学习
在这habit之下20 小时前
Tomcat学习总结
学习
尘似鹤20 小时前
linux驱动学习---竞争与并发(原子操作与各种锁)
linux·学习
在这habit之下20 小时前
HAProxy学习总结
学习
来两个炸鸡腿20 小时前
【Datawhale组队学习202602】Hello-Agents task06 框架应用开发实战
人工智能·学习·大模型·智能体
盐焗西兰花20 小时前
鸿蒙学习实战之路-STG系列(4/11)-应用选择页功能详解
服务器·学习·harmonyos
qq_4162764221 小时前
通用音频表征的对比学习
学习·音视频