• Discover
  • Partner with us
  • Chapters
  • Blog

Learn everything about Solidity

Getting Started
✨Connect Your Profile to Open Campus
Introduction to Remix IDE
Counter Contract and Solidity Basics
Submit Your First Homework
Solidity Cheat Sheet
Creating Proposal Structure
Submit Your Second Homework

Creating a Counter
Creating a Proposal
Submit Your Third Homework
The Constructor
OnlyOwner Modifier
Voting Functionality

Correcting the Voting Functionality
Correcting the Voting Functionality 2
Calculating the Current State of a Proposal
Submit Your Fourth Task
Finalizing Proposal Contract
Final Version of the Proposal Contract

Submit Your Final Homework
Next Steps

In the last lesson, you have created a new variable named owner to represent the owner of the contract and we set the owner to the msg.sender in our contructor. Now, we want to use this owner in our create function so that only the owner of the contract can use this function.

You can achieve this with a modifier.

  • Modifiers are defined using the modifier keyword followed by the modifier name and parameters if any.
  • Within the modifier, custom logic can be added before and/or after the _; statement.
  • By default, modifiers will run the logic before executing the function body. The _; tells it to then execute the function.
  • To use a modifier on a function, include it after the function name followed by parameters.

Let's create our onlyOwner modifier.

modifier onlyOwner() {
    require(msg.sender == owner);
    _;
}
  • modifier onlyOwner() {...} - This starts the definition of the modifier called onlyOwner.
  • require(msg.sender == owner) - This line checks that the msg.sender, the account calling the function, is equal to the stored owner address.
  • _; - This tells Solidity to execute the rest of the function code after completing the modifier logic.

To use the onlyOwner modifier, you just need to add the onlyOwner keyword at the end of the function definition.

Here is the modified version of our function:

function create(string calldata _description, uint256 _total_vote_to_end) external onlyOwner {
    counter += 1;
    proposal_history[counter] = Proposal(_description, 0, 0, 0, _total_vote_to_end, false, true);
}

Now your function can only be called by the owner of this contract.

Finally, let's add a function to set the owner of the contract since you may need to transfer ownership of the contract in the future.

function setOwner(address new_owner) external onlyOwner {
    owner = new_owner;
}

This function will transfer the ownership of the contract to the given address as the parameter and it can only be called by the owner of the contract.

Lesson discussion

Swap insights and ask questions about “Learn everything about Solidity”.

Enroll to participate
Start the course to unlock the discussion. Enrolling helps us keep conversations relevant to learners.
WebsiteDiscoverPartner with UsBlogEvents
Discover
CoursesCircleRustSoliditySolanaWeb3 FundamentalsBlockchain Basics
CompanyAbout UsBrand GuidelineFAQsTerms of UsePrivacy PolicyGDPR NoticeCookies
Don't miss any update!

Disclaimer: The information, programs, and events provided on https://risein.com is strictly for upskilling and networking purposes related to the technical infrastructure of blockchain platforms. We do not provide financial or investment advice, nor do we make any representations regarding the value, profitability, or future price of any blockchain or cryptocurrency. Users are encouraged to conduct their own research and consult with licensed financial professionals before engaging in any investment activities. https://risein.com disclaims any responsibility for financial decisions made by users based on the information provided here.

© 2026 Rise In, All rights reserved