The first concept we will tackle is contract interaction. Contract interaction refers to how contracts communicate and share information. In Solidity, contracts can call other contracts’ functions, create other contracts, and even send Ether to other contracts or addresses.
We have already seen contract interaction in our voting system. Each time a vote is cast, the voting contract interacts with itself to update the voter’s status and the vote count for the proposal. However, for a more complex system, you might have separate contracts that need to interact with each other.
Here is a simple example of contract interaction:
Solidity
pragma solidity >=0.7.0 <0.9.0;
contract CalledContract {
uint public x;
function setX(uint _x) public {
x = _x;
}
}
contract CallerContract {
function callSetX(address _calledContractAddress, uint _x) public {
CalledContract calledContract = CalledContract(_calledContractAddress);
calledContract.setX(_x);
}
}
In this example, CallerContract
interacts with CalledContract
by calling its setX
function.
Now let’s move onto event logging. Events are a way for your contract to communicate that something has happened to the external world. In our voting system, for instance, we might want to emit an event each time a vote is cast. Here’s how to do it:
Solidity
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract Voter {
// Event definition
event VoteCast(address voter, uint proposal);
// Rest of the contract...
function vote(uint _proposal) public {
Person storage sender = voters[msg.sender];
require(!sender.voted, "Already voted.");
sender.voted = true;
sender.vote = _proposal;
proposals[_proposal].voteCount += 1;
// Emit event
emit VoteCast(msg.sender, _proposal);
}
// Rest of the contract...
}
In the updated vote
function, we emit a VoteCast
event each time a vote is cast. The event logs the address of the voter and the proposal they voted for.
In this lesson, we learned about contract interaction and event logging, two essential concepts in Solidity. With the knowledge you’ve gained from this course, you are now equipped to start developing more complex decentralized applications on Ethereum.
Congratulations! You’ve successfully completed the course, “Building a Decentralized Voting System”.
Throughout this course, you’ve learned the core concepts behind a decentralized voting system and how to implement one using Solidity on the Remix IDE. Starting from understanding what decentralized voting is and why it matters, we delved into coding our own smart contracts for voter registration, vote casting, and vote tallying. We also learned how to interact with our contracts and simulate the entire voting process within the Remix IDE. Lastly, we explored how contracts interact with each other and how to log events for tracking activities on the blockchain.
The skills and knowledge you’ve gained from this course are not only limited to creating voting systems but can also be applied to other types of decentralized applications. Keep practicing, experimenting, and building.
The first concept we will tackle is contract interaction. Contract interaction refers to how contracts communicate and share information. In Solidity, contracts can call other contracts’ functions, create other contracts, and even send Ether to other contracts or addresses.
We have already seen contract interaction in our voting system. Each time a vote is cast, the voting contract interacts with itself to update the voter’s status and the vote count for the proposal. However, for a more complex system, you might have separate contracts that need to interact with each other.
Here is a simple example of contract interaction:
Solidity
pragma solidity >=0.7.0 <0.9.0;
contract CalledContract {
uint public x;
function setX(uint _x) public {
x = _x;
}
}
contract CallerContract {
function callSetX(address _calledContractAddress, uint _x) public {
CalledContract calledContract = CalledContract(_calledContractAddress);
calledContract.setX(_x);
}
}
In this example, CallerContract
interacts with CalledContract
by calling its setX
function.
Now let’s move onto event logging. Events are a way for your contract to communicate that something has happened to the external world. In our voting system, for instance, we might want to emit an event each time a vote is cast. Here’s how to do it:
Solidity
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract Voter {
// Event definition
event VoteCast(address voter, uint proposal);
// Rest of the contract...
function vote(uint _proposal) public {
Person storage sender = voters[msg.sender];
require(!sender.voted, "Already voted.");
sender.voted = true;
sender.vote = _proposal;
proposals[_proposal].voteCount += 1;
// Emit event
emit VoteCast(msg.sender, _proposal);
}
// Rest of the contract...
}
In the updated vote
function, we emit a VoteCast
event each time a vote is cast. The event logs the address of the voter and the proposal they voted for.
In this lesson, we learned about contract interaction and event logging, two essential concepts in Solidity. With the knowledge you’ve gained from this course, you are now equipped to start developing more complex decentralized applications on Ethereum.
Congratulations! You’ve successfully completed the course, “Building a Decentralized Voting System”.
Throughout this course, you’ve learned the core concepts behind a decentralized voting system and how to implement one using Solidity on the Remix IDE. Starting from understanding what decentralized voting is and why it matters, we delved into coding our own smart contracts for voter registration, vote casting, and vote tallying. We also learned how to interact with our contracts and simulate the entire voting process within the Remix IDE. Lastly, we explored how contracts interact with each other and how to log events for tracking activities on the blockchain.
The skills and knowledge you’ve gained from this course are not only limited to creating voting systems but can also be applied to other types of decentralized applications. Keep practicing, experimenting, and building.