Skip to main content

Understanding Openzeppelin Ownable

openzeppelin

Introduction

In this article, we will learn how to integrate OpenZeppelin's Ownable contract into our smart contract. Ownable contracts provide developers with an easy way to manage ownership of the contract and create access control in the contract. Integrating Ownable contracts will give us a basic understanding of how access control can be implemented in a decentralized application. So, let's start with a step-by-step guide to integrating OpenZeppelin's Ownable contract into our project.

Prerequisite

Instead of building the entire smart contract from scratch, we will integrate Ownable into the CRUD contract that we’ve built in the previous tutorial. So, if you haven’t read it yet, you can find it here.

Setting Up Project

If you have followed the previous tutorial on CRUD smart contracts, then open that project.

Before we start building, we first need to install @openzeppelin/contracts package. To install the package, run the following command in the terminal:

npm install @openzeppelin/contracts

Understanding Contract Ownership

Let's first understand how the Ownable contract works. Integrating the Ownable contract into any dApp is a straightforward way to manage ownership and access control. At its core, the Ownable contract allows developers to designate the address that deploys the contract as the contract's owner, granting permission to access smart contract functions that are restricted to be used only by the owner.

Now, let's proceed with integrating OpenZeppelin's Ownable contracts into our CRUD contract.

Integrating Ownable to Our Contract

In our CRUD smart contract, we have four functions: createData, readData, updateData, and deleteData. We will restrict all functions, except readData, to be accessible only by the owner. This means only the contract owner will have permission to create, update, and delete data.

Let's start by importing the Ownable contract in CRUD.sol:

import "@openzeppelin/contracts/access/Ownable.sol";

Next, we'll make our CRUD contract inherit the Ownable contract to access its ownership logic:

contract CRUD is ownable {
// our contract logic
}

By inheriting Ownable, we gain access to the ownership control logic it provides.

Restricting Function to Owner only

With Ownable inherited, we can use the onlyOwner modifier provided by Ownable to restrict functions for the contract owner:

function createData(uint _key, string memory _value) public onlyOwner {
//createData logic
}

function updateData(uint _key, string memory _value) public onlyOwner {
//updateData logic
}

function deleteData(uint _key) public onlyOwner {
//deleteData logic
}

By integrating Ownable into our contract and using the onlyOwner modifier, we've restricted these functions to be accessible only by the contract owner.

You might wonder how the owner of the contract is set. With Ownable, we don't have to set the owner manually. The address that deploys the contract is automatically set as the contract owner.

Additionally, Ownable also provides access to the transferOwnership and renounceOwnership functions, allowing users to transfer ownership to a different address or renounce ownership altogether, respectively.

Deployment

Now, that you have our modified contract, let’s deploy it on the Velas testnet. Run the following command in the terminal to deploy that contract:

npx hardhat run scripts/deploy.js --network velas_testnet

After executing this command, you’ll see the address of the deployed contract logged in the terminal.

Conclusion

In conclusion, we've successfully integrated OpenZeppelin's Ownable contract into our CRUD smart contract for ownership management and access control. The Ownable contract enables us to set the contract owner, restricting certain functions to be accessed only by the owner.

By using the Ownable contract, we've learned a simple yet effective access control mechanism that can be implemented in any smart contract. In future tutorials, we'll delve deeper into access control to further enhance our understanding. Stay tuned for more exciting developments!