A smart contract is a small program that runs on a blockchain. It helps people make agreements automatically without needing a middleman.
Smart contract concept in Blockchain / Solidity
contract ContractName {
// State variables
// Functions
}This is a basic structure of a smart contract in Solidity, a popular blockchain language.
Contracts have variables to store data and functions to do actions.
contract SimpleContract {
uint number;
function setNumber(uint _num) public {
number = _num;
}
function getNumber() public view returns (uint) {
return number;
}
}contract Payment {
address payable owner;
constructor() {
owner = payable(msg.sender);
}
function pay() public payable {
owner.transfer(msg.value);
}
}This smart contract lets two people approve an agreement. It stores their addresses and tracks if both have approved. The agreement is complete only when both say yes.
pragma solidity ^0.8.0; contract SimpleAgreement { address public partyA; address public partyB; bool public partyAApproved = false; bool public partyBApproved = false; constructor(address _partyB) { partyA = msg.sender; partyB = _partyB; } function approve() public { require(msg.sender == partyA || msg.sender == partyB, "Not authorized"); if (msg.sender == partyA) { partyAApproved = true; } else { partyBApproved = true; } } function isAgreementComplete() public view returns (bool) { return partyAApproved && partyBApproved; } }
Smart contracts run exactly as programmed without downtime or interference.
Once deployed, smart contracts cannot be changed easily, so test carefully.
They use blockchain's security to keep data safe and transparent.
Smart contracts automate agreements using code on a blockchain.
They help remove middlemen and speed up processes.
They are useful for payments, ownership, voting, and more.
Practice
smart contract in blockchain technology?Solution
Step 1: Understand the definition of smart contracts
Smart contracts are computer programs that run on blockchains to automate and enforce agreements without middlemen.Step 2: Compare options with the definition
Only A program that runs on a blockchain to automate agreements describes a program on blockchain automating agreements. Other options describe unrelated concepts.Final Answer:
A program that runs on a blockchain to automate agreements -> Option BQuick Check:
Smart contract = automated blockchain program [OK]
- Confusing smart contracts with physical contracts
- Thinking smart contracts are wallets or mining tools
- Assuming smart contracts require middlemen
Solution
Step 1: Recall Solidity syntax for defining contracts
In Solidity, smart contracts start with the keywordcontractfollowed by the contract name and braces.Step 2: Check each option's syntax
contract MyContract { } usescontractkeyword correctly. Options A, B, and C use keywords from other languages or incorrect syntax.Final Answer:
contract MyContract { } -> Option AQuick Check:
Solidity contracts start with 'contract' keyword [OK]
- Using 'class' instead of 'contract' keyword
- Confusing functions with contract declarations
- Using module or other unrelated keywords
function getValue() public pure returns (uint) { return 5 + 3; }Solution
Step 1: Analyze the function logic
The function returns the sum of 5 and 3, which is 8.Step 2: Understand Solidity return behavior
The function is markedpureand returns a uint, so it will output the calculated value 8 without errors.Final Answer:
8 -> Option CQuick Check:
5 + 3 = 8 [OK]
- Thinking '5 + 3' concatenates as '53'
- Expecting runtime error due to missing state
- Assuming default return is zero
contract Test {
uint value;
function setValue(uint _value) public {
value = _value
}
}Solution
Step 1: Check syntax line by line
The linevalue = _valueis missing a semicolon at the end, which is required in Solidity.Step 2: Verify other parts
Function visibility 'public' is correct, variable can be private, and function naming is valid. So only missing semicolon is error.Final Answer:
Missing semicolon after assignment -> Option AQuick Check:
Semicolons end statements in Solidity [OK]
- Ignoring missing semicolon errors
- Thinking function visibility is wrong
- Assuming variable must be public
Solution
Step 1: Check input validation for positive amount
function deposit(uint amount) public { require(amount > 0, "Amount must be positive"); balances[msg.sender] += amount; } usesrequire(amount > 0)to ensure only positive deposits are accepted.Step 2: Compare other options for correctness
function deposit(uint amount) public { if(amount >= 0) { balances[msg.sender] = amount; } } allows zero and overwrites balance instead of adding. function deposit(int amount) public { balances[msg.sender] += amount; } uses signed int allowing negative deposits. function deposit(uint amount) public { balances[msg.sender] += amount; } lacks any check.Final Answer:
function deposit(uint amount) public { require(amount > 0, "Amount must be positive"); balances[msg.sender] += amount; } -> Option DQuick Check:
Use require() to enforce positive deposits [OK]
- Not validating input amounts
- Using signed integers for balances
- Overwriting balances instead of adding
Ready to prove it?
Pass the test and this pattern moves unknown
