A smart contract is a small program that runs on a blockchain. It helps people make agreements automatically without needing a middleman.
0
0
Smart contract concept in Blockchain / Solidity
Introduction
When you want to automatically send money after a condition is met, like paying rent on time.
When you want to create a transparent voting system where votes are counted fairly.
When you want to track ownership of digital items, like art or game items.
When you want to make sure a deal happens only if both sides agree and follow the rules.
When you want to reduce paperwork and speed up business processes.
Syntax
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.
Examples
This contract stores a number and lets you set or get it.
Blockchain / Solidity
contract SimpleContract {
uint number;
function setNumber(uint _num) public {
number = _num;
}
function getNumber() public view returns (uint) {
return number;
}
}This contract lets anyone send money to the owner automatically.
Blockchain / Solidity
contract Payment {
address payable owner;
constructor() {
owner = payable(msg.sender);
}
function pay() public payable {
owner.transfer(msg.value);
}
}Sample Program
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.
Blockchain / Solidity
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; } }
OutputSuccess
Important Notes
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.
Summary
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.