Access Control
Ownable and Roles.
Why Access Control
Many contract functions should only be callable by certain accounts — minting tokens, pausing the system, withdrawing funds. Access control enforces who can do what.
OpenZeppelin offers two main patterns: Ownable and AccessControl.
The Ownable Pattern
Ownable gives a contract a single privileged owner. Import and inherit it:
import "@openzeppelin/contracts/access/Ownable.sol";
contract Vault is Ownable {
constructor() Ownable(msg.sender) {}
}The deployer becomes the initial owner.
import "@openzeppelin/contracts/access/Ownable.sol";
contract Vault is Ownable {
constructor() Ownable(msg.sender) {}
}All lessons in this course
- Why OpenZeppelin
- Access Control
- Token Extensions
- Upgradeable Contracts