What is a smart contract?
A smart contract is a self-executing digital contract that enables the automation and enforcement of agreements between two or more parties. Smart contracts are typically stored on a blockchain, which is a decentralized and distributed ledger that allows for secure and transparent transactions without the need for intermediaries such as banks or other financial institutions.
How do smart contracts work?
Smart contracts work by automating the process of executing an agreement. Once the terms of the contract are met, the contract is automatically executed, and the agreed-upon transaction is completed. Smart contracts are designed to be self-executing, which means that they do not require human intervention to execute.
Let's start with the basics first:
pragma solidity ^0.8.0;
contract SimpleContract {
uint256 public value;
function setValue(uint256 newValue) public {
value = newValue;
}
function getValue() public view returns (uint256) {
return value;
}
}
In this smart contract, we have a single variable value
of type uint256
, which is publicly accessible. There are two functions defined: setValue
and getValue
. The setValue
function allows the caller to set a new value for the value
variable, and the getValue
function allows the caller to retrieve the current value of the value
variable.
Let's say we deploy this smart contract on the Ethereum blockchain. Once it is deployed, anyone can interact with the contract by calling the setValue
or getValue
functions. For example, a user could call the setValue
function and pass in a new value for the value
variable:
contractInstance.setValue(100);
This would set the value
variable to 100.
Another user could then call the getValue
function to retrieve the current value of the value
variable:
uint256 currentValue = contractInstance.getValue();
// This would retrieve the current value of the value variable, which in this case would be 100.
Now Let's decode the Complex one:
pragma solidity ^0.8.0;
contract Voting {
struct Candidate {
string name;
uint256 voteCount;
}
mapping(address => bool) public voters;
mapping(uint256 => Candidate) public candidates;
uint256 public candidatesCount;
constructor(string[] memory names) {
for (uint256 i = 0; i < names.length; i++) {
candidates[candidatesCount] = Candidate(names[i], 0);
candidatesCount++;
}
}
function vote(uint256 candidateIndex) public {
require(!voters[msg.sender], "You have already voted");
require(candidateIndex < candidatesCount, "Invalid candidate index");
candidates[candidateIndex].voteCount++;
voters[msg.sender] = true;
}
}
Let's break it down step by step:
struct Candidate
: This defines a new data type calledCandidate
that has two properties - aname
and avoteCount
. This is used to store information about each candidate.mapping(address => bool) public voters
: This is a mapping that maps an Ethereum address to a boolean value. It is used to keep track of which addresses have voted.mapping(uint256 => Candidate) public candidates
: This is a mapping that maps an index to aCandidate
struct. It is used to keep track of all the candidates.uint256 public candidatesCount
: This is a public variable that keeps track of the number of candidates.constructor(string[] memory names)
: This is a constructor function that is called when the contract is deployed. It takes an array of candidate names as input and creates a newCandidate
struct for each name, with a vote count of 0.function vote(uint256 candidateIndex) public
: This is a function that allows a user to vote for a candidate. It takes the index of the candidate as input and checks if the user has already voted and if the candidate index is valid. If both conditions are met, it increments the vote count for the chosen candidate and marks the user as having voted.
Conclusion:
Overall, this smart contract is a simple voting system that allows users to vote for their preferred candidate. The Candidate
struct stores information about each candidate, the voters
mapping keeps track of who has voted, and the candidate
mapping keeps track of all the candidates. The vote
function allows users to cast their vote and updates the vote count for the chosen candidate