Ownable
import "@thirdweb-dev/contracts/extension/Ownable.sol";
The Ownable smart contract extension is usable with any base smart contract. It lets you set an owner for your smart contract.
Usage
The Ownable extension is an abstract contract, and expects you to implement the following functions by yourself:
| Name | Type | Returns | Description |
|---|---|---|---|
_canSetOwner | internal view virtual | bool | Runs on every attempt to set the owner of the contract. Returns whether the owner can be set in the given execution context. |
This is an example smart contract demonstrating how to inherit from this extension and override the functions to add (optional) custom functionality.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@thirdweb-dev/contracts/extension/Ownable.sol";
contract MyContract is Ownable {
/**
* This function returns who is authorized to set the owner of your contract.
*
* As an EXAMPLE, we'll only allow the current owner to set the contract's new owner.
*
* You MUST complete the body of this function to use the `Ownable` extension.
*/
function _canSetOwner() internal virtual view override returns (bool) {
return msg.sender == owner();
}
}
SDK Usage
Base Contracts Implementing This Extension
All of the base contracts implement this extension.
Full API reference
owner
function owner() public view override returns (address);
- Returns the owner of the contract.
- Set this value using the
setOwnerfunction.
setOwner
function setOwner(address _newOwner) external;
- Lets an authorized wallet set a new owner for the contract. This value can be read from the
ownerfunction. - Parameter
_newOwner: The address to make the owner of the contract. - The
_canSetOwnerfunction is run on every call to this function. If the return value of_canSetOwnerisfalse, thesetOwnercall will revert.
_setupOwner
function _setupOwner(address _newOwner) internal;
- Sets the owner of the smart contract. This value can be read from the
ownerfunction. - Parameter
_newOwner: The address to make the owner of the contract. - The
_canSetOwnerfunction is not run on a call to this function.
_canSetOwner
function _canSetOwner() internal view virtual returns (bool);
- Runs on every
setOwnerfunction call. - Returns whether a new owner can be set in the given execution context.
- For example, this function can check whether the wallet calling
setOwneris the contract owner, and enforce that only the current owner should be able to successfully callsetOwner.

