What is a smart contract
What is a smart contract
A smart contract is a self-executing contract with the terms of the agreement directly written into code. Smart contracts run on blockchain technology, which ensures that the contract is immutable and distributed, meaning that once deployed, the contract cannot be changed, and it operates on a network of computers rather than a single server.
Key Features of Smart Contracts
- Autonomy: Smart contracts operate automatically without the need for intermediaries, reducing the risk of manipulation or interference.
- Security: They are secured by cryptography, making them tamper-proof and resistant to hacking.
- Transparency: The terms of the contract are visible to all parties involved, ensuring trust and accountability.
- Cost-Efficiency: By eliminating intermediaries, smart contracts can reduce transaction costs and streamline processes.
- Speed: Automated execution of contracts reduces the time needed to complete transactions.
Basic Structure of a Smart Contract
Smart contracts are written in programming languages like Solidity. Below is a simple example of a smart contract for a basic voting system:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Voting {
struct Candidate {
string name;
uint voteCount;
}
mapping(uint => Candidate) public candidates;
mapping(address => bool) public voters;
uint public candidatesCount;
constructor() {
addCandidate(`Alice`);
addCandidate(`Bob`);
}
function addCandidate(string memory _name) private {
candidates[candidatesCount] = Candidate(_name, 0);
candidatesCount++;
}
function vote(uint _candidateId) public {
require(!voters[msg.sender], `You have already voted.`);
require(_candidateId < candidatesCount, `Invalid candidate ID.`);
voters[msg.sender] = true;
candidates[_candidateId].voteCount++;
}
function getCandidate(uint _candidateId) public view returns (string memory name, uint voteCount) {
require(_candidateId < candidatesCount, `Invalid candidate ID.`);
Candidate memory candidate = candidates[_candidateId];
return (candidate.name, candidate.voteCount);
}
}
How to Deploy a Smart Contract
To deploy a smart contract, you typically need:
- A development environment (like Remix, Truffle, or Hardhat).
- An Ethereum wallet (e.g., MetaMask) to interact with the Ethereum network.
- Ether (ETH) to pay for gas fees during deployment.
Example Usage
Once deployed, you can interact with the Voting contract as follows:
// Assuming you have a web3 instance connected to Ethereum
const contractAddress = `YOUR_CONTRACT_ADDRESS`; // Replace with your contract address
const abi = [ /* ABI array generated during compilation */ ];
const votingContract = new web3.eth.Contract(abi, contractAddress);
// Vote for a candidate
async function vote(candidateId) {
const accounts = await web3.eth.getAccounts();
await votingContract.methods.vote(candidateId).send({ from: accounts[0] });
}
// Get candidate details
async function getCandidate(candidateId) {
const candidate = await votingContract.methods.getCandidate(candidateId).call();
console.log(`Candidate Name:`, candidate[0], `Vote Count:`, candidate[ 1]);
}
Conclusion
Smart contracts are a revolutionary technology that automates and secures agreements on the blockchain. Their features, such as autonomy, security, and transparency, make them an essential component of decentralized applications and various industries.