Skip to content

Commit 3ded57b

Browse files
committed
Merge branch 'master' into cross-chain
1 parent 60da221 commit 3ded57b

File tree

9,811 files changed

+1582933
-37
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

9,811 files changed

+1582933
-37
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# <img src="logo.svg" alt="OpenZeppelin" height="40px">
2+
3+
[![NPM Package](https://img.shields.io/npm/v/@openzeppelin/contracts.svg)](https://www.npmjs.org/package/@openzeppelin/contracts)
4+
[![Coverage Status](https://codecov.io/gh/OpenZeppelin/openzeppelin-contracts/graph/badge.svg)](https://codecov.io/gh/OpenZeppelin/openzeppelin-contracts)
5+
[![GitPOAPs](https://public-api.gitpoap.io/v1/repo/OpenZeppelin/openzeppelin-contracts/badge)](https://www.gitpoap.io/gh/OpenZeppelin/openzeppelin-contracts)
6+
[![Docs](https://img.shields.io/badge/docs-%F0%9F%93%84-yellow)](https://docs.openzeppelin.com/contracts)
7+
[![Forum](https://img.shields.io/badge/forum-%F0%9F%92%AC-yellow)](https://docs.openzeppelin.com/contracts)
8+
9+
**A library for secure smart contract development.** Build on a solid foundation of community-vetted code.
10+
11+
* Implementations of standards like [ERC20](https://docs.openzeppelin.com/contracts/erc20) and [ERC721](https://docs.openzeppelin.com/contracts/erc721).
12+
* Flexible [role-based permissioning](https://docs.openzeppelin.com/contracts/access-control) scheme.
13+
* Reusable [Solidity components](https://docs.openzeppelin.com/contracts/utilities) to build custom contracts and complex decentralized systems.
14+
15+
:mage: **Not sure how to get started?** Check out [Contracts Wizard](https://wizard.openzeppelin.com/) — an interactive smart contract generator.
16+
17+
:building_construction: **Want to scale your decentralized application?** Check out [OpenZeppelin Defender](https://openzeppelin.com/defender) — a mission-critical developer security platform to code, audit, deploy, monitor, and operate with confidence.
18+
19+
> [!IMPORTANT]
20+
> OpenZeppelin Contracts uses semantic versioning to communicate backwards compatibility of its API and storage layout. For upgradeable contracts, the storage layout of different major versions should be assumed incompatible, for example, it is unsafe to upgrade from 4.9.3 to 5.0.0. Learn more at [Backwards Compatibility](https://docs.openzeppelin.com/contracts/backwards-compatibility).
21+
22+
+> [!NOTE]
23+
+> You are looking at the upgradeable variant of OpenZeppelin Contracts. Be sure to review the documentation on [Using OpenZeppelin Contracts with Upgrades](https://docs.openzeppelin.com/contracts/upgradeable).
24+
+
25+
## Overview
26+
27+
### Installation
28+
29+
#### Hardhat (npm)
30+
31+
```
32+
$ npm install @openzeppelin/contracts-upgradeable
33+
```
34+
35+
#### Foundry (git)
36+
37+
> [!WARNING]
38+
> When installing via git, it is a common error to use the `master` branch. This is a development branch that should be avoided in favor of tagged releases. The release process involves security measures that the `master` branch does not guarantee.
39+
40+
> [!WARNING]
41+
> Foundry installs the latest version initially, but subsequent `forge update` commands will use the `master` branch.
42+
43+
```
44+
$ forge install OpenZeppelin/openzeppelin-contracts-upgradeable
45+
```
46+
47+
Add `@openzeppelin/contracts-upgradeable/=lib/openzeppelin-contracts-upgradeable/contracts/` in `remappings.txt.`
48+
49+
### Usage
50+
51+
Once installed, you can use the contracts in the library by importing them:
52+
53+
```solidity
54+
pragma solidity ^0.8.20;
55+
56+
import {ERC721Upgradeable} from "@openzeppelin/contracts-upgradeable/token/ERC721/ERC721Upgradeable.sol";
57+
58+
contract MyCollectible is ERC721Upgradeable {
59+
function initialize() initializer public {
60+
__ERC721_init("MyCollectible", "MCO");
61+
}
62+
}
63+
```
64+
65+
_If you're new to smart contract development, head to [Developing Smart Contracts](https://docs.openzeppelin.com/learn/developing-smart-contracts) to learn about creating a new project and compiling your contracts._
66+
67+
To keep your system secure, you should **always** use the installed code as-is, and neither copy-paste it from online sources nor modify it yourself. The library is designed so that only the contracts and functions you use are deployed, so you don't need to worry about it needlessly increasing gas costs.
68+
69+
## Learn More
70+
71+
The guides in the [documentation site](https://docs.openzeppelin.com/contracts) will teach about different concepts, and how to use the related contracts that OpenZeppelin Contracts provides:
72+
73+
* [Access Control](https://docs.openzeppelin.com/contracts/access-control): decide who can perform each of the actions on your system.
74+
* [Tokens](https://docs.openzeppelin.com/contracts/tokens): create tradeable assets or collectives, and distribute them via [Crowdsales](https://docs.openzeppelin.com/contracts/crowdsales).
75+
* [Utilities](https://docs.openzeppelin.com/contracts/utilities): generic useful tools including non-overflowing math, signature verification, and trustless paying systems.
76+
77+
The [full API](https://docs.openzeppelin.com/contracts/api/token/ERC20) is also thoroughly documented, and serves as a great reference when developing your smart contract application. You can also ask for help or follow Contracts's development in the [community forum](https://forum.openzeppelin.com).
78+
79+
Finally, you may want to take a look at the [guides on our blog](https://blog.openzeppelin.com/), which cover several common use cases and good practices. The following articles provide great background reading, though please note that some of the referenced tools have changed, as the tooling in the ecosystem continues to rapidly evolve.
80+
81+
* [The Hitchhiker’s Guide to Smart Contracts in Ethereum](https://blog.openzeppelin.com/the-hitchhikers-guide-to-smart-contracts-in-ethereum-848f08001f05) will help you get an overview of the various tools available for smart contract development, and help you set up your environment.
82+
* [A Gentle Introduction to Ethereum Programming, Part 1](https://blog.openzeppelin.com/a-gentle-introduction-to-ethereum-programming-part-1-783cc7796094) provides very useful information on an introductory level, including many basic concepts from the Ethereum platform.
83+
* For a more in-depth dive, you may read the guide [Designing the Architecture for Your Ethereum Application](https://blog.openzeppelin.com/designing-the-architecture-for-your-ethereum-application-9cec086f8317), which discusses how to better structure your application and its relationship to the real world.
84+
85+
## Security
86+
87+
This project is maintained by [OpenZeppelin](https://openzeppelin.com) with the goal of providing a secure and reliable library of smart contract components for the ecosystem. We address security through risk management in various areas such as engineering and open source best practices, scoping and API design, multi-layered review processes, and incident response preparedness.
88+
89+
The [OpenZeppelin Contracts Security Center](https://contracts.openzeppelin.com/security) contains more details about the secure development process.
90+
91+
The security policy is detailed in [`SECURITY.md`](./SECURITY.md) as well, and specifies how you can report security vulnerabilities, which versions will receive security patches, and how to stay informed about them. We run a [bug bounty program on Immunefi](https://immunefi.com/bounty/openzeppelin) to reward the responsible disclosure of vulnerabilities.
92+
93+
The engineering guidelines we follow to promote project quality can be found in [`GUIDELINES.md`](./GUIDELINES.md).
94+
95+
Past audits can be found in [`audits/`](./audits).
96+
97+
Smart contracts are a nascent technology and carry a high level of technical risk and uncertainty. Although OpenZeppelin is well known for its security audits, using OpenZeppelin Contracts is not a substitute for a security audit.
98+
99+
OpenZeppelin Contracts is made available under the MIT License, which disclaims all warranties in relation to the project and which limits the liability of those that contribute and maintain the project, including OpenZeppelin. As set out further in the Terms, you acknowledge that you are solely responsible for any use of OpenZeppelin Contracts and you assume all risks associated with any such use.
100+
101+
## Contribute
102+
103+
OpenZeppelin Contracts exists thanks to its contributors. There are many ways you can participate and help build high quality software. Check out the [contribution guide](CONTRIBUTING.md)!
104+
105+
## License
106+
107+
OpenZeppelin Contracts is released under the [MIT License](LICENSE).
108+
109+
## Legal
110+
111+
Your use of this Project is governed by the terms found at www.openzeppelin.com/tos (the "Terms").
Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
1+
// SPDX-License-Identifier: MIT
2+
// OpenZeppelin Contracts (last updated v5.0.0) (access/AccessControl.sol)
3+
4+
pragma solidity ^0.8.20;
5+
6+
import {IAccessControl} from "@openzeppelin/contracts/access/IAccessControl.sol";
7+
import {ContextUpgradeable} from "../utils/ContextUpgradeable.sol";
8+
import {ERC165Upgradeable} from "../utils/introspection/ERC165Upgradeable.sol";
9+
import {Initializable} from "../proxy/utils/Initializable.sol";
10+
11+
/**
12+
* @dev Contract module that allows children to implement role-based access
13+
* control mechanisms. This is a lightweight version that doesn't allow enumerating role
14+
* members except through off-chain means by accessing the contract event logs. Some
15+
* applications may benefit from on-chain enumerability, for those cases see
16+
* {AccessControlEnumerable}.
17+
*
18+
* Roles are referred to by their `bytes32` identifier. These should be exposed
19+
* in the external API and be unique. The best way to achieve this is by
20+
* using `public constant` hash digests:
21+
*
22+
* ```solidity
23+
* bytes32 public constant MY_ROLE = keccak256("MY_ROLE");
24+
* ```
25+
*
26+
* Roles can be used to represent a set of permissions. To restrict access to a
27+
* function call, use {hasRole}:
28+
*
29+
* ```solidity
30+
* function foo() public {
31+
* require(hasRole(MY_ROLE, msg.sender));
32+
* ...
33+
* }
34+
* ```
35+
*
36+
* Roles can be granted and revoked dynamically via the {grantRole} and
37+
* {revokeRole} functions. Each role has an associated admin role, and only
38+
* accounts that have a role's admin role can call {grantRole} and {revokeRole}.
39+
*
40+
* By default, the admin role for all roles is `DEFAULT_ADMIN_ROLE`, which means
41+
* that only accounts with this role will be able to grant or revoke other
42+
* roles. More complex role relationships can be created by using
43+
* {_setRoleAdmin}.
44+
*
45+
* WARNING: The `DEFAULT_ADMIN_ROLE` is also its own admin: it has permission to
46+
* grant and revoke this role. Extra precautions should be taken to secure
47+
* accounts that have been granted it. We recommend using {AccessControlDefaultAdminRules}
48+
* to enforce additional security measures for this role.
49+
*/
50+
abstract contract AccessControlUpgradeable is Initializable, ContextUpgradeable, IAccessControl, ERC165Upgradeable {
51+
struct RoleData {
52+
mapping(address account => bool) hasRole;
53+
bytes32 adminRole;
54+
}
55+
56+
bytes32 public constant DEFAULT_ADMIN_ROLE = 0x00;
57+
58+
59+
/// @custom:storage-location erc7201:openzeppelin.storage.AccessControl
60+
struct AccessControlStorage {
61+
mapping(bytes32 role => RoleData) _roles;
62+
}
63+
64+
// keccak256(abi.encode(uint256(keccak256("openzeppelin.storage.AccessControl")) - 1)) & ~bytes32(uint256(0xff))
65+
bytes32 private constant AccessControlStorageLocation = 0x02dd7bc7dec4dceedda775e58dd541e08a116c6c53815c0bd028192f7b626800;
66+
67+
function _getAccessControlStorage() private pure returns (AccessControlStorage storage $) {
68+
assembly {
69+
$.slot := AccessControlStorageLocation
70+
}
71+
}
72+
73+
/**
74+
* @dev Modifier that checks that an account has a specific role. Reverts
75+
* with an {AccessControlUnauthorizedAccount} error including the required role.
76+
*/
77+
modifier onlyRole(bytes32 role) {
78+
_checkRole(role);
79+
_;
80+
}
81+
82+
function __AccessControl_init() internal onlyInitializing {
83+
}
84+
85+
function __AccessControl_init_unchained() internal onlyInitializing {
86+
}
87+
/**
88+
* @dev See {IERC165-supportsInterface}.
89+
*/
90+
function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) {
91+
return interfaceId == type(IAccessControl).interfaceId || super.supportsInterface(interfaceId);
92+
}
93+
94+
/**
95+
* @dev Returns `true` if `account` has been granted `role`.
96+
*/
97+
function hasRole(bytes32 role, address account) public view virtual returns (bool) {
98+
AccessControlStorage storage $ = _getAccessControlStorage();
99+
return $._roles[role].hasRole[account];
100+
}
101+
102+
/**
103+
* @dev Reverts with an {AccessControlUnauthorizedAccount} error if `_msgSender()`
104+
* is missing `role`. Overriding this function changes the behavior of the {onlyRole} modifier.
105+
*/
106+
function _checkRole(bytes32 role) internal view virtual {
107+
_checkRole(role, _msgSender());
108+
}
109+
110+
/**
111+
* @dev Reverts with an {AccessControlUnauthorizedAccount} error if `account`
112+
* is missing `role`.
113+
*/
114+
function _checkRole(bytes32 role, address account) internal view virtual {
115+
if (!hasRole(role, account)) {
116+
revert AccessControlUnauthorizedAccount(account, role);
117+
}
118+
}
119+
120+
/**
121+
* @dev Returns the admin role that controls `role`. See {grantRole} and
122+
* {revokeRole}.
123+
*
124+
* To change a role's admin, use {_setRoleAdmin}.
125+
*/
126+
function getRoleAdmin(bytes32 role) public view virtual returns (bytes32) {
127+
AccessControlStorage storage $ = _getAccessControlStorage();
128+
return $._roles[role].adminRole;
129+
}
130+
131+
/**
132+
* @dev Grants `role` to `account`.
133+
*
134+
* If `account` had not been already granted `role`, emits a {RoleGranted}
135+
* event.
136+
*
137+
* Requirements:
138+
*
139+
* - the caller must have ``role``'s admin role.
140+
*
141+
* May emit a {RoleGranted} event.
142+
*/
143+
function grantRole(bytes32 role, address account) public virtual onlyRole(getRoleAdmin(role)) {
144+
_grantRole(role, account);
145+
}
146+
147+
/**
148+
* @dev Revokes `role` from `account`.
149+
*
150+
* If `account` had been granted `role`, emits a {RoleRevoked} event.
151+
*
152+
* Requirements:
153+
*
154+
* - the caller must have ``role``'s admin role.
155+
*
156+
* May emit a {RoleRevoked} event.
157+
*/
158+
function revokeRole(bytes32 role, address account) public virtual onlyRole(getRoleAdmin(role)) {
159+
_revokeRole(role, account);
160+
}
161+
162+
/**
163+
* @dev Revokes `role` from the calling account.
164+
*
165+
* Roles are often managed via {grantRole} and {revokeRole}: this function's
166+
* purpose is to provide a mechanism for accounts to lose their privileges
167+
* if they are compromised (such as when a trusted device is misplaced).
168+
*
169+
* If the calling account had been revoked `role`, emits a {RoleRevoked}
170+
* event.
171+
*
172+
* Requirements:
173+
*
174+
* - the caller must be `callerConfirmation`.
175+
*
176+
* May emit a {RoleRevoked} event.
177+
*/
178+
function renounceRole(bytes32 role, address callerConfirmation) public virtual {
179+
if (callerConfirmation != _msgSender()) {
180+
revert AccessControlBadConfirmation();
181+
}
182+
183+
_revokeRole(role, callerConfirmation);
184+
}
185+
186+
/**
187+
* @dev Sets `adminRole` as ``role``'s admin role.
188+
*
189+
* Emits a {RoleAdminChanged} event.
190+
*/
191+
function _setRoleAdmin(bytes32 role, bytes32 adminRole) internal virtual {
192+
AccessControlStorage storage $ = _getAccessControlStorage();
193+
bytes32 previousAdminRole = getRoleAdmin(role);
194+
$._roles[role].adminRole = adminRole;
195+
emit RoleAdminChanged(role, previousAdminRole, adminRole);
196+
}
197+
198+
/**
199+
* @dev Attempts to grant `role` to `account` and returns a boolean indicating if `role` was granted.
200+
*
201+
* Internal function without access restriction.
202+
*
203+
* May emit a {RoleGranted} event.
204+
*/
205+
function _grantRole(bytes32 role, address account) internal virtual returns (bool) {
206+
AccessControlStorage storage $ = _getAccessControlStorage();
207+
if (!hasRole(role, account)) {
208+
$._roles[role].hasRole[account] = true;
209+
emit RoleGranted(role, account, _msgSender());
210+
return true;
211+
} else {
212+
return false;
213+
}
214+
}
215+
216+
/**
217+
* @dev Attempts to revoke `role` to `account` and returns a boolean indicating if `role` was revoked.
218+
*
219+
* Internal function without access restriction.
220+
*
221+
* May emit a {RoleRevoked} event.
222+
*/
223+
function _revokeRole(bytes32 role, address account) internal virtual returns (bool) {
224+
AccessControlStorage storage $ = _getAccessControlStorage();
225+
if (hasRole(role, account)) {
226+
$._roles[role].hasRole[account] = false;
227+
emit RoleRevoked(role, account, _msgSender());
228+
return true;
229+
} else {
230+
return false;
231+
}
232+
}
233+
}

0 commit comments

Comments
 (0)