更新合约文档,翻译为中文以增强可读性,包括 AccessControl、Ownable、ERC20 和 IERC20 的相关说明。修正了一些注释以确保准确性和一致性。
Some checks failed
transpile upgradeable / transpile (push) Has been cancelled

This commit is contained in:
2025-07-12 23:27:57 +08:00
parent bc8f775df2
commit 264f00fe39
8 changed files with 149 additions and 197 deletions

View File

@ -8,22 +8,18 @@ import {Context} from "../utils/Context.sol";
import {IERC165, ERC165} from "../utils/introspection/ERC165.sol";
/**
* @dev Contract module that allows children to implement role-based access
* control mechanisms. This is a lightweight version that doesn't allow enumerating role
* members except through off-chain means by accessing the contract event logs. Some
* applications may benefit from on-chain enumerability, for those cases see
* {AccessControlEnumerable}.
* @dev 合约模块,允许子合约实现基于角色的访问控制机制。这是一个轻量级版本,
* 不允许枚举角色成员,除非通过链下方式访问合约事件日志。某些应用可能会受益于链上可枚举性,
* 对于这些情况,请参见 {AccessControlEnumerable}。
*
* Roles are referred to by their `bytes32` identifier. These should be exposed
* in the external API and be unique. The best way to achieve this is by
* using `public constant` hash digests:
* 角色通过其 `bytes32` 标识符来引用。这些应该在外部 API 中公开并且是唯一的。
* 实现这一点的最佳方法是使用 `public constant` 哈希摘要:
*
* ```solidity
* bytes32 public constant MY_ROLE = keccak256("MY_ROLE");
* ```
*
* Roles can be used to represent a set of permissions. To restrict access to a
* function call, use {hasRole}:
* 角色可以用来表示一组权限。要限制对函数调用的访问,请使用 {hasRole}
*
* ```solidity
* function foo() public {
@ -32,19 +28,15 @@ import {IERC165, ERC165} from "../utils/introspection/ERC165.sol";
* }
* ```
*
* Roles can be granted and revoked dynamically via the {grantRole} and
* {revokeRole} functions. Each role has an associated admin role, and only
* accounts that have a role's admin role can call {grantRole} and {revokeRole}.
* 角色可以通过 {grantRole} 和 {revokeRole} 函数动态授予和撤销。每个角色都有一个关联的管理员角色,
* 只有具有角色管理员角色的账户才能调用 {grantRole} 和 {revokeRole}。
*
* By default, the admin role for all roles is `DEFAULT_ADMIN_ROLE`, which means
* that only accounts with this role will be able to grant or revoke other
* roles. More complex role relationships can be created by using
* {_setRoleAdmin}.
* 默认情况下,所有角色的管理员角色是 `DEFAULT_ADMIN_ROLE`,这意味着只有具有此角色的账户
* 才能授予或撤销其他角色。可以使用 {_setRoleAdmin} 创建更复杂的角色关系。
*
* WARNING: The `DEFAULT_ADMIN_ROLE` is also its own admin: it has permission to
* grant and revoke this role. Extra precautions should be taken to secure
* accounts that have been granted it. We recommend using {AccessControlDefaultAdminRules}
* to enforce additional security measures for this role.
* 警告:`DEFAULT_ADMIN_ROLE` 也是其自身的管理员:它有权限授予和撤销这个角色。
* 应该采取额外的预防措施来保护已被授予此角色的账户。我们建议使用 {AccessControlDefaultAdminRules}
* 来为此角色强制执行额外的安全措施。
*/
abstract contract AccessControl is Context, IAccessControl, ERC165 {
struct RoleData {
@ -57,8 +49,8 @@ abstract contract AccessControl is Context, IAccessControl, ERC165 {
bytes32 public constant DEFAULT_ADMIN_ROLE = 0x00;
/**
* @dev Modifier that checks that an account has a specific role. Reverts
* with an {AccessControlUnauthorizedAccount} error including the required role.
* @dev 检查账户是否具有特定角色的修饰符。如果没有,则使用 {AccessControlUnauthorizedAccount} 错误
* 回退,包括所需的角色。
*/
modifier onlyRole(bytes32 role) {
_checkRole(role);
@ -71,23 +63,22 @@ abstract contract AccessControl is Context, IAccessControl, ERC165 {
}
/**
* @dev Returns `true` if `account` has been granted `role`.
* @dev 如果 `account` 已被授予 `role`,则返回 `true`
*/
function hasRole(bytes32 role, address account) public view virtual returns (bool) {
return _roles[role].hasRole[account];
}
/**
* @dev Reverts with an {AccessControlUnauthorizedAccount} error if `_msgSender()`
* is missing `role`. Overriding this function changes the behavior of the {onlyRole} modifier.
* @dev 如果 `_msgSender()` 缺少 `role`,则使用 {AccessControlUnauthorizedAccount} 错误回退。
* 重写此函数会改变 {onlyRole} 修饰符的行为。
*/
function _checkRole(bytes32 role) internal view virtual {
_checkRole(role, _msgSender());
}
/**
* @dev Reverts with an {AccessControlUnauthorizedAccount} error if `account`
* is missing `role`.
* @dev 如果 `account` 缺少 `role`,则使用 {AccessControlUnauthorizedAccount} 错误回退。
*/
function _checkRole(bytes32 role, address account) internal view virtual {
if (!hasRole(role, account)) {
@ -96,61 +87,57 @@ abstract contract AccessControl is Context, IAccessControl, ERC165 {
}
/**
* @dev Returns the admin role that controls `role`. See {grantRole} and
* {revokeRole}.
* @dev 返回控制 `role` 的管理员角色。请参见 {grantRole} 和 {revokeRole}。
*
* To change a role's admin, use {_setRoleAdmin}.
* 要更改角色的管理员,请使用 {_setRoleAdmin}
*/
function getRoleAdmin(bytes32 role) public view virtual returns (bytes32) {
return _roles[role].adminRole;
}
/**
* @dev Grants `role` to `account`.
* @dev `role` 授予给 `account`
*
* If `account` had not been already granted `role`, emits a {RoleGranted}
* event.
* 如果 `account` 尚未被授予 `role`,则发出 {RoleGranted} 事件。
*
* Requirements:
* 要求:
*
* - the caller must have ``role``'s admin role.
* - 调用者必须具有 `role` 的管理员角色。
*
* May emit a {RoleGranted} event.
* 可能发出 {RoleGranted} 事件。
*/
function grantRole(bytes32 role, address account) public virtual onlyRole(getRoleAdmin(role)) {
_grantRole(role, account);
}
/**
* @dev Revokes `role` from `account`.
* @dev 从 `account` 撤销 `role`。
*
* If `account` had been granted `role`, emits a {RoleRevoked} event.
* 如果 `account` 已被授予 `role`,则发出 {RoleRevoked} 事件。
*
* Requirements:
* 要求:
*
* - the caller must have ``role``'s admin role.
* - 调用者必须具有 `role` 的管理员角色。
*
* May emit a {RoleRevoked} event.
* 可能发出 {RoleRevoked} 事件。
*/
function revokeRole(bytes32 role, address account) public virtual onlyRole(getRoleAdmin(role)) {
_revokeRole(role, account);
}
/**
* @dev Revokes `role` from the calling account.
* @dev 从调用账户撤销 `role`。
*
* Roles are often managed via {grantRole} and {revokeRole}: this function's
* purpose is to provide a mechanism for accounts to lose their privileges
* if they are compromised (such as when a trusted device is misplaced).
* 角色通常通过 {grantRole} {revokeRole} 进行管理:此函数的目的是为账户提供一种机制,
* 在它们受到损害时(例如当受信任的设备丢失时)失去其特权。
*
* If the calling account had been revoked `role`, emits a {RoleRevoked}
* event.
* 如果调用账户的 `role` 已被撤销,则发出 {RoleRevoked} 事件。
*
* Requirements:
* 要求:
*
* - the caller must be `callerConfirmation`.
* - 调用者必须是 `callerConfirmation`
*
* May emit a {RoleRevoked} event.
* 可能发出 {RoleRevoked} 事件。
*/
function renounceRole(bytes32 role, address callerConfirmation) public virtual {
if (callerConfirmation != _msgSender()) {
@ -161,9 +148,9 @@ abstract contract AccessControl is Context, IAccessControl, ERC165 {
}
/**
* @dev Sets `adminRole` as ``role``'s admin role.
* @dev `adminRole` 设置为 `role` 的管理员角色。
*
* Emits a {RoleAdminChanged} event.
* 发出 {RoleAdminChanged} 事件。
*/
function _setRoleAdmin(bytes32 role, bytes32 adminRole) internal virtual {
bytes32 previousAdminRole = getRoleAdmin(role);
@ -172,11 +159,11 @@ abstract contract AccessControl is Context, IAccessControl, ERC165 {
}
/**
* @dev Attempts to grant `role` to `account` and returns a boolean indicating if `role` was granted.
* @dev 尝试将 `role` 授予给 `account` 并返回一个布尔值,指示是否授予了 `role`。
*
* Internal function without access restriction.
* 内部函数,无访问限制。
*
* May emit a {RoleGranted} event.
* 可能发出 {RoleGranted} 事件。
*/
function _grantRole(bytes32 role, address account) internal virtual returns (bool) {
if (!hasRole(role, account)) {
@ -189,11 +176,11 @@ abstract contract AccessControl is Context, IAccessControl, ERC165 {
}
/**
* @dev Attempts to revoke `role` from `account` and returns a boolean indicating if `role` was revoked.
* @dev 尝试从 `account` 撤销 `role` 并返回一个布尔值,指示是否撤销了 `role`。
*
* Internal function without access restriction.
* 内部函数,无访问限制。
*
* May emit a {RoleRevoked} event.
* 可能发出 {RoleRevoked} 事件。
*/
function _revokeRole(bytes32 role, address account) internal virtual returns (bool) {
if (hasRole(role, account)) {

View File

@ -6,34 +6,29 @@ pragma solidity ^0.8.20;
import {Context} from "../utils/Context.sol";
/**
* @dev Contract module which provides a basic access control mechanism, where
* there is an account (an owner) that can be granted exclusive access to
* specific functions.
* @dev 合约模块,提供基本的访问控制机制,其中有一个账户(所有者)可以被授予对特定函数的独占访问权限。
*
* The initial owner is set to the address provided by the deployer. This can
* later be changed with {transferOwnership}.
* 初始所有者设置为部署者提供的地址。这可以稍后通过 {transferOwnership} 更改。
*
* This module is used through inheritance. It will make available the modifier
* `onlyOwner`, which can be applied to your functions to restrict their use to
* the owner.
* 此模块通过继承使用。它将提供修饰符 `onlyOwner`,可以应用于您的函数以限制它们仅供所有者使用。
*/
abstract contract Ownable is Context {
address private _owner;
/**
* @dev The caller account is not authorized to perform an operation.
* @dev 调用者账户未被授权执行操作。
*/
error OwnableUnauthorizedAccount(address account);
/**
* @dev The owner is not a valid owner account. (eg. `address(0)`)
* @dev 所有者不是有效的所有者账户。(例如 `address(0)`)
*/
error OwnableInvalidOwner(address owner);
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
/**
* @dev Initializes the contract setting the address provided by the deployer as the initial owner.
* @dev 初始化合约,将部署者提供的地址设置为初始所有者。
*/
constructor(address initialOwner) {
if (initialOwner == address(0)) {
@ -43,7 +38,7 @@ abstract contract Ownable is Context {
}
/**
* @dev Throws if called by any account other than the owner.
* @dev 如果由所有者以外的任何账户调用则抛出异常。
*/
modifier onlyOwner() {
_checkOwner();
@ -51,14 +46,14 @@ abstract contract Ownable is Context {
}
/**
* @dev Returns the address of the current owner.
* @dev 返回当前所有者的地址。
*/
function owner() public view virtual returns (address) {
return _owner;
}
/**
* @dev Throws if the sender is not the owner.
* @dev 如果发送者不是所有者则抛出异常。
*/
function _checkOwner() internal view virtual {
if (owner() != _msgSender()) {
@ -67,19 +62,16 @@ abstract contract Ownable is Context {
}
/**
* @dev Leaves the contract without owner. It will not be possible to call
* `onlyOwner` functions. Can only be called by the current owner.
* @dev 使合约没有所有者。将无法调用 `onlyOwner` 函数。只能由当前所有者调用。
*
* NOTE: Renouncing ownership will leave the contract without an owner,
* thereby disabling any functionality that is only available to the owner.
* 注意:放弃所有权将使合约没有所有者,从而禁用仅对所有者可用的任何功能。
*/
function renounceOwnership() public virtual onlyOwner {
_transferOwnership(address(0));
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Can only be called by the current owner.
* @dev 将合约的所有权转移给新账户 (`newOwner`)。只能由当前所有者调用。
*/
function transferOwnership(address newOwner) public virtual onlyOwner {
if (newOwner == address(0)) {
@ -89,8 +81,7 @@ abstract contract Ownable is Context {
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Internal function without access restriction.
* @dev 将合约的所有权转移给新账户 (`newOwner`)。内部函数,无访问限制。
*/
function _transferOwnership(address newOwner) internal virtual {
address oldOwner = _owner;