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
TokenSaleAdd a public variable
tokensSold to track how many tokens are soldAdd a public variable
maxTokens to set the maximum tokens availableUse
require to check if the buyer sends enough EtherUse
assert to check internal state consistencyUse
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