Restrict Smart Contract Functions to a Specified Owner

Ryan Harris
InstructorRyan Harris
Share this video with your friends

Social Share Links

Send Tweet
Published 3 years ago
Updated 3 years ago

Right now our sale can be open and closed by anyone. We want to take ownership of our smart contract to prevent this.

We'll use Ownable from OpenZeppelin, Set the openSale and closeSale public functions to onlyOwner, and then set the owner of the smart contract in the deploy script.

Instructor: [0:00] Currently, anyone can open or close our ticket sale. Open, Close. However, we want to restrict this access so that only we, the owners of the smart contract, can open and close the sale. The first thing we're going to do is import a new OpenZeppelin package -- import "@openzeppelin/contracts/access/ownable.sol."

[0:25] Then we'll use it to update our contract definition. We'll say, contract NFTixBooth is ownable. This gives us another keyword, onlyOwner(), which restricts function access to the address that owns the smart contract.

[0:39] We'll come down below and find openSale() and closeSale(). We want to be the only ones who can change the SaleIsActive state value. Let's add the onlyOwner keyword to both of these functions, onlyOwner.

[0:53] Now, if we redeploy our smart contract to the local blockchain instance, yarn deploy, and we go to our browser. We can now try to open the sale like we did before and we'll see we get an error, ownable caller is not the owner. If we try to close the sale, we'll get the same message. Let's demonstrate that this does work for the owner of the smart contract.

[1:19] Let's go back to VS Code and open our deployment script. Below where we instantiate our smart contract, let's add a new line and we'll say, await NFTixBooth.transfer ownership. I'm going to paste in a wallet address I already have set up in my MetaMask extension.

[1:39] Before we redeploy, we want to add a new restriction to our mint function. Come into our smart contract. Go down to mint. We'll add a new require statement, require. Here we want to check if the sale is active or not. We'll say, sale is active.

[1:57] If not, we'll log out, tickets are not on sale. We can deploy this to the local blockchain by running yarn deploy. We'll jump back to the browser.

[2:13] Instead of using the pre-configured wallet that scaffold Ether set up for us, let's connect my MetaMask extension, which has the wallet address I added to the deploy script. We'll click connect MetaMask. We're logged in as this address.

[2:27] If we look below, you will see that's the address listed as the contract owner. If we click open sale, and confirm the transaction in our MetaMask extension, you'll see we successfully open the sale. That means if we log out, the pre-configured scaffold-eth wallet can now mint a token -- .08 ETH, converted to Gwei, and sent.

[2:55] To review, we made our smart contract ownable, and transferred ownership to a wallet address we control, in our MetaMask extension, and restricted the access to our openSale() and closeSale() methods, so only the owner of the smart contract can change the value of SaleIsActive. Then, in our mint function, we prevented users from minting an NFT if the sale is not active.

egghead
egghead
~ 28 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today