ControlledERC20.sol 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity 0.8.28;
  3. import { ERC20 } from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
  4. import { ERC20Burnable } from "@openzeppelin/contracts/token/ERC20/extensions/ERC20Burnable.sol";
  5. import { ERC20Pausable } from "@openzeppelin/contracts/token/ERC20/extensions/ERC20Pausable.sol";
  6. import { AccessControl } from "@openzeppelin/contracts/access/AccessControl.sol";
  7. /**
  8. * @title ControlledERC20
  9. * @dev 带有转账开关、mint功能和权限管理的ERC20代币合约
  10. * 包含以下功能:
  11. * - 转账开关控制
  12. * - 权限管理(管理员、铸造者、暂停者)
  13. * - 铸造功能
  14. * - 暂停/恢复功能
  15. */
  16. contract ControlledERC20 is ERC20, ERC20Burnable, ERC20Pausable, AccessControl {
  17. // 角色定义
  18. bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE");
  19. bytes32 public constant PAUSER_ROLE = keccak256("PAUSER_ROLE");
  20. bytes32 public constant TRANSFER_CONTROLLER_ROLE = keccak256("TRANSFER_CONTROLLER_ROLE");
  21. // 转账开关状态
  22. bool public transferEnabled = true;
  23. // 白名单地址(不受转账开关限制)
  24. mapping(address => bool) public whitelist;
  25. // 事件
  26. event TransferEnabled(bool enabled);
  27. event WhitelistUpdated(address indexed account, bool isWhitelisted);
  28. constructor(
  29. string memory name,
  30. string memory symbol,
  31. address initialOwner
  32. ) ERC20(name, symbol) {
  33. // 设置初始角色
  34. _grantRole(DEFAULT_ADMIN_ROLE, initialOwner);
  35. _grantRole(MINTER_ROLE, initialOwner);
  36. _grantRole(PAUSER_ROLE, initialOwner);
  37. _grantRole(TRANSFER_CONTROLLER_ROLE, initialOwner);
  38. // 将初始所有者加入白名单
  39. whitelist[initialOwner] = true;
  40. emit WhitelistUpdated(initialOwner, true);
  41. }
  42. /**
  43. * @dev 重写transfer函数,添加转账开关检查
  44. */
  45. function transfer(address to, uint256 amount) public virtual override returns (bool) {
  46. require(transferEnabled || whitelist[msg.sender] || whitelist[to], "Transfer is disabled");
  47. return super.transfer(to, amount);
  48. }
  49. /**
  50. * @dev 重写transferFrom函数,添加转账开关检查
  51. */
  52. function transferFrom(address from, address to, uint256 amount) public virtual override returns (bool) {
  53. require(transferEnabled || whitelist[from] || whitelist[to], "Transfer is disabled");
  54. return super.transferFrom(from, to, amount);
  55. }
  56. /**
  57. * @dev 铸造代币(仅限MINTER_ROLE)
  58. */
  59. function mint(address to, uint256 amount) external onlyRole(MINTER_ROLE) {
  60. _mint(to, amount);
  61. }
  62. /**
  63. * @dev 批量铸造代币(仅限MINTER_ROLE)
  64. */
  65. function mintBatch(address[] calldata recipients, uint256[] calldata amounts) external onlyRole(MINTER_ROLE) {
  66. require(recipients.length == amounts.length, "Arrays length mismatch");
  67. for (uint256 i = 0; i < recipients.length; i++) {
  68. _mint(recipients[i], amounts[i]);
  69. }
  70. }
  71. /**
  72. * @dev 暂停合约(仅限PAUSER_ROLE)
  73. */
  74. function pause() external onlyRole(PAUSER_ROLE) {
  75. _pause();
  76. }
  77. /**
  78. * @dev 恢复合约(仅限PAUSER_ROLE)
  79. */
  80. function unpause() external onlyRole(PAUSER_ROLE) {
  81. _unpause();
  82. }
  83. /**
  84. * @dev 启用/禁用转账(仅限TRANSFER_CONTROLLER_ROLE)
  85. */
  86. function setTransferEnabled(bool enabled) external onlyRole(TRANSFER_CONTROLLER_ROLE) {
  87. transferEnabled = enabled;
  88. emit TransferEnabled(enabled);
  89. }
  90. /**
  91. * @dev 更新白名单(仅限TRANSFER_CONTROLLER_ROLE)
  92. */
  93. function updateWhitelist(address account, bool whitelisted) external onlyRole(TRANSFER_CONTROLLER_ROLE) {
  94. whitelist[account] = whitelisted;
  95. emit WhitelistUpdated(account, whitelisted);
  96. }
  97. /**
  98. * @dev 批量更新白名单(仅限TRANSFER_CONTROLLER_ROLE)
  99. */
  100. function updateWhitelistBatch(address[] calldata accounts, bool[] calldata whitelisted) external onlyRole(TRANSFER_CONTROLLER_ROLE) {
  101. require(accounts.length == whitelisted.length, "Arrays length mismatch");
  102. for (uint256 i = 0; i < accounts.length; i++) {
  103. whitelist[accounts[i]] = whitelisted[i];
  104. emit WhitelistUpdated(accounts[i], whitelisted[i]);
  105. }
  106. }
  107. /**
  108. * @dev 检查地址是否在白名单中
  109. */
  110. function isWhitelisted(address account) external view returns (bool) {
  111. return whitelist[account];
  112. }
  113. /**
  114. * @dev 重写_update函数以支持暂停功能
  115. */
  116. function _update(address from, address to, uint256 value) internal override(ERC20, ERC20Pausable) {
  117. super._update(from, to, value);
  118. }
  119. /**
  120. * @dev 重写supportsInterface函数以支持AccessControl
  121. */
  122. function supportsInterface(bytes4 interfaceId) public view override(AccessControl) returns (bool) {
  123. return super.supportsInterface(interfaceId);
  124. }
  125. }