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);
    }
}
相关推荐
Z21-X17 小时前
云原生运维学习笔记:Kubernetes Volume 与 Secret 实验
运维·笔记·学习·云原生·kubernetes
console.log('npc')20 小时前
Python 学习教程 · 第4课:数据结构
数据结构·python·学习
星落梦生1 天前
Docker学习(11)
学习·docker·容器
重生之我是萧炎2 天前
PR安装教程,Adobe Premiere Pro安装教程(含安装包),仅供学习使用
学习·adobe
蒸蒸yyyyzwd2 天前
深搜广搜学习笔记
笔记·学习
叼烟扛炮2 天前
[LangChain] 学习笔记(13):并发不是越大越好——batch / gather / 信号量 / 配额治理
笔记·学习·langchain
渣渣盟2 天前
狂命爆肝99天,共52K字的JAVA学习笔记奉上,JAVA从入门到精通一文搞定,一文在手JAVA无忧
java·笔记·学习
叼烟扛炮2 天前
[LangChain] 学习笔记(09):工具(Tool)定义与调用闭环 · 理论+实战完整版
笔记·学习·langchain
个 人 练 习 生2 天前
C++:从零基础到深入了解vector的使用
开发语言·c++·经验分享·学习·程序人生·stl