📖 Scenario: You are creating a simple smart contract for a shared piggy bank. Only the owner can add money or withdraw it. To keep things safe, you will use function modifiers to check who is calling the functions.
🎯 Goal: Build a Solidity contract that uses a modifier to restrict access to certain functions so only the owner can call them.
📋 What You'll Learn
Create a contract named PiggyBank
Add a state variable owner to store the address of the contract creator
Create a modifier named onlyOwner that allows function execution only if the caller is the owner
Use the onlyOwner modifier on deposit and withdraw functions
Add a balance state variable to track the piggy bank balance
Implement deposit and withdraw functions that update the balance
💡 Why This Matters
🌍 Real World
Function modifiers are used in smart contracts to control who can call certain functions, protecting assets and enforcing rules automatically.
💼 Career
Understanding function modifiers is essential for blockchain developers to write secure and maintainable smart contracts.
Progress0 / 4 steps
1
Create the PiggyBank contract and owner variable
Create a contract named PiggyBank and inside it, declare a public address variable called owner. Set owner to msg.sender inside the constructor.
Blockchain / Solidity
Hint
The constructor runs once when the contract is created. Use msg.sender to get the creator's address.
2
Add the onlyOwner modifier
Inside the PiggyBank contract, create a modifier named onlyOwner that uses require(msg.sender == owner) to allow function execution only if the caller is the owner. Use _; to continue function execution.
Blockchain / Solidity
Hint
A modifier lets you add checks before running a function. Use require to stop if the caller is not the owner.
3
Add balance variable and deposit function with onlyOwner modifier
Add a public unsigned integer variable called balance to track the piggy bank balance. Then create a public function deposit that takes a uint amount parameter. Use the onlyOwner modifier on deposit. Inside the function, add amount to balance.
Blockchain / Solidity
Hint
Use uint for numbers without decimals. The onlyOwner modifier goes after the function visibility.
4
Add withdraw function with onlyOwner modifier and print balance
Add a public function withdraw that takes a uint amount parameter. Use the onlyOwner modifier on withdraw. Inside the function, subtract amount from balance only if balance is greater or equal to amount using require. Then add a public view function getBalance that returns the current balance.
Blockchain / Solidity
Hint
Use require to check if there is enough balance before withdrawing. The view keyword means the function does not change state.
Practice
(1/5)
1. What is the main purpose of a function modifier in blockchain smart contracts?
easy
A. To change the return type of a function
B. To add extra rules or checks around a function
C. To rename a function
D. To delete a function from the contract
Solution
Step 1: Understand function modifiers
Function modifiers are used to add extra rules or steps before or after a function runs.
Step 2: Compare options
Only To add extra rules or checks around a function correctly describes adding extra rules or checks around a function.
Final Answer:
To add extra rules or checks around a function -> Option B
Quick Check:
Function modifiers add rules [OK]
Hint: Modifiers add rules or checks around functions [OK]
Common Mistakes:
Thinking modifiers rename or delete functions
Confusing modifiers with return type changes
Believing modifiers execute functions themselves
2. Which of the following is the correct syntax to define a function modifier in Solidity?
easy
A. modifier onlyOwner() => { require(msg.sender == owner); _; }
B. modifier onlyOwner() : { require(msg.sender == owner); _; }
C. modifier onlyOwner() -> { require(msg.sender == owner); _; }
D. modifier onlyOwner() { require(msg.sender == owner); _; }
Solution
Step 1: Recall Solidity modifier syntax
Modifiers use the keyword 'modifier', name, parentheses, and curly braces with '_;' inside.
Step 2: Identify correct syntax
modifier onlyOwner() { require(msg.sender == owner); _; } matches the correct Solidity syntax with braces and '_;'. Others use invalid symbols like '=>', '->', or ':'.
D. Modifier name should be 'OnlyOwner' with capital O
Solution
Step 1: Check modifier syntax
The modifier has '_', but it misses the semicolon '_;' which is required to mark function execution point.
Step 2: Verify other parts
Function visibility 'public' is correct, owner can be uninitialized but not a syntax error, and modifier names are case-sensitive but not required to be capitalized.
Final Answer:
Missing semicolon after '_' in modifier -> Option A
Quick Check:
Modifier must have '_;' not just '_' [OK]
Hint: Modifier must end '_' with semicolon ';' [OK]
5. You want to create a modifier that allows a function to run only if the contract is not paused. The contract has a boolean paused. Which modifier code correctly implements this logic?
hard
A. modifier whenNotPaused() { require(!paused, "Paused"); _; }
B. modifier whenNotPaused() { if(paused) { revert("Paused"); } }
C. modifier whenNotPaused() { require(paused, "Paused"); _; }
D. modifier whenNotPaused() { if(!paused) { _; } }
Solution
Step 1: Understand the paused logic
The function should run only if paused is false, so require(!paused) ensures this.
Step 2: Check modifier completeness
modifier whenNotPaused() { require(!paused, "Paused"); _; } uses require with negation and includes '_;' to run the function. modifier whenNotPaused() { if(paused) { revert("Paused"); } } lacks '_;', so function never runs. modifier whenNotPaused() { require(paused, "Paused"); _; } requires paused to be true, which is opposite. modifier whenNotPaused() { if(!paused) { _; } } runs function only if not paused but lacks else handling and is less clear.
Final Answer:
modifier whenNotPaused() { require(!paused, "Paused"); _; } -> Option A
Quick Check:
Require !paused with '_;' runs function [OK]
Hint: Use require(!paused) and include '_;' to run function [OK]
Common Mistakes:
Reversing paused condition in require
Omitting '_;' causing function not to run
Using if without else or revert
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.