0
0
Blockchain / Solidityprogramming~30 mins

Require, assert, and revert in Blockchain / Solidity - Mini Project: Build & Apply

Choose your learning style9 modes available
Require, assert, and revert in Solidity
📖 Scenario: You are building a simple smart contract for a token sale. You want to make sure that certain conditions are met before allowing users to buy tokens. This helps keep your contract safe and predictable.
🎯 Goal: Learn how to use require, assert, and revert statements in Solidity to check conditions and handle errors properly.
📋 What You'll Learn
Create a contract called TokenSale
Add a public variable tokensSold to track how many tokens are sold
Add a public variable maxTokens to set the maximum tokens available
Use require to check if the buyer sends enough Ether
Use assert to check internal state consistency
Use revert to cancel the transaction with a message if conditions fail
💡 Why This Matters
🌍 Real World
Smart contracts on blockchains must check conditions carefully to avoid errors and protect funds. Using require, assert, and revert helps enforce rules and handle mistakes safely.
💼 Career
Blockchain developers use these statements daily to write secure contracts that handle money and data correctly.
Progress0 / 4 steps
1
Create the TokenSale contract with initial variables
Create a Solidity contract called TokenSale. Inside it, declare a public unsigned integer variable tokensSold and set it to 0. Also declare a public unsigned integer variable maxTokens and set it to 100.
Blockchain / Solidity
Need a hint?

Use contract TokenSale { ... } to start. Declare tokensSold and maxTokens as uint public variables.

2
Add a price variable and a buyTokens function with require
Inside the TokenSale contract, add a public unsigned integer variable tokenPrice and set it to 1 ether. Then create a public function called buyTokens that takes a parameter amount of type uint. Inside the function, use require to check that the message value (msg.value) is at least amount * tokenPrice.
Blockchain / Solidity
Need a hint?

Declare tokenPrice as uint public and set to 1 ether. The buyTokens function should be public payable. Use require to check msg.value.

3
Add checks with require, assert, and revert inside buyTokens
Inside the buyTokens function, after the require you wrote, add another require to check that tokensSold + amount is less than or equal to maxTokens. Then increase tokensSold by amount. Use assert to check that tokensSold is not greater than maxTokens. Finally, add an if statement that checks if amount is zero, and if so, use revert with the message "Cannot buy zero tokens".
Blockchain / Solidity
Need a hint?

Use require to check token limits, increase tokensSold, then assert the limit is not exceeded. Use if and revert to block zero token buys.

4
Test the buyTokens function by simulating a purchase
Add a public view function called tokensLeft that returns the number of tokens still available (which is maxTokens - tokensSold). Then add a public function called testPurchase that calls buyTokens with amount 10 and sends 10 ether as value. Finally, add a public view function called getTokensSold that returns tokensSold. Print the value of getTokensSold() after calling testPurchase() to show tokens sold.
Blockchain / Solidity
Need a hint?

Create tokensLeft to return remaining tokens. testPurchase calls buyTokens with 10 tokens and 10 ether. getTokensSold returns tokens sold.