Skip to main content

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.

Availability in base contracts

The Ownable extension is already available in the following base contracts:

  • All ERC20, ERC721 and ERC1155 base contracts.

Unlocked Features

Once deployed, you can use the features made available by these contracts on the SDK and dashboard:

Implementing the Contract

The Ownable extension is an abstract contract, and expects you to implement the following functions by yourself:

NameTypeParametersReturnsDescription
_canSetOwnerinternal view virtualn/aboolRuns on every attempt to set the owner of the contract. Returns whether the owner can be set in the given execution context.
// 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();
}
}

Full API reference

owner

function owner() public view override returns (address);
  • Returns the owner of the contract.
  • Set this value using the setOwner function.

setOwner

function setOwner(address _newOwner) external;
  • Lets an authorized wallet set a new owner for the contract. This value can be read from the owner function.
  • Parameter _newOwner: The address to make the owner of the contract.
  • The _canSetOwner function is run on every call to this function. If the return value of _canSetOwner is false, the setOwner call will revert.

_setupOwner

function _setupOwner(address _newOwner) internal;
  • Sets the owner of the smart contract. This value can be read from the owner function.
  • Parameter _newOwner: The address to make the owner of the contract.
  • The _canSetOwner function is not run on a call to this function.

_canSetOwner

function _canSetOwner() internal view virtual returns (bool);
  • Runs on every setOwner function call.
  • Returns whether a new owner can be set in the given execution context.
  • For example, this function can check whether the wallet calling setOwner is the contract owner, and enforce that only the current owner should be able to successfully call setOwner.