In this tutorial, our primary goal is to guide you through the process of creating a smart contract using Solidity. By the end of this tutorial, you will be able to create and prepare your own smart contract for deployment.
You will learn:
Prerequisites:
Smart Contracts
Smart contracts are self-executing contracts with the terms of the agreement directly written into code. They exist across a decentralized, blockchain network.
Solidity
Solidity is a high-level language for implementing smart contracts on the Ethereum blockchain.
Creating a Smart Contract
Start your contract with a line to define the Solidity version. This is done to ensure that the contract is compiled with the correct compiler versions.
javascript
pragma solidity ^0.5.16;
This is the beginning of a smart contract. It is similar to classes in object-oriented programming.
javascript
contract MyFirstContract {
}
State variables are values which are permanently stored in contract storage.
javascript
contract MyFirstContract {
uint storedData; // State variable
}
Functions are the executable units of code within a contract.
```javascript
contract MyFirstContract {
uint storedData; // State variable
function set(uint x) public {
storedData = x;
}
function get() public view returns (uint) {
return storedData;
}
}
```
Let's create a simple contract for a voting system. This contract will allow you to vote for an option, tally the votes, and retrieve the total votes for a particular option.
pragma solidity ^0.5.16;
contract VotingSystem {
// A mapping to hold the votes for each option
mapping (bytes32 => uint256) public votesReceived;
// An array containing the list of options for voting
bytes32[] public optionsList;
// The constructor function to initialize the options
constructor(bytes32[] memory optionsNames) public {
optionsList = optionsNames;
}
// A function to vote for an option
function voteForOption(bytes32 option) public {
require(validOption(option), "Option does not exist.");
votesReceived[option] += 1;
}
// A function to check whether an option is valid or not
function validOption(bytes32 option) view public returns (bool) {
for(uint i = 0; i < optionsList.length; i++) {
if (optionsList[i] == option) {
return true;
}
}
return false;
}
// A function to retrieve the total votes a particular option got
function totalVotesFor(bytes32 option) view public returns (uint256) {
require(validOption(option), "Option does not exist.");
return votesReceived[option];
}
}
In the above example:
votesReceived
is a mapping that associates each option with the number of votes it has received.optionsList
is an array of all the available options.voteForOption(bytes32 option)
is a function that increments the vote count of the specified option.validOption(bytes32 option)
is a function that checks whether the provided option exists.totalVotesFor(bytes32 option)
is a function that returns the total number of votes received by the specified option.In this tutorial, we covered:
Next, I recommend diving deeper into Solidity to learn more about its capabilities, such as handling different types of data, creating more complex functions, and understanding gas costs. You can find more resources on the Solidity documentation.
Exercise: Create a contract named Counter
that has a state variable count
initialized to 0. Add functions to increment and decrement the count
variable and to get its value.
Exercise: Create a contract named SimpleBank
that allows users to deposit and withdraw Ether. Keep track of each user's balance and do not allow them to withdraw more than their deposited amount.
Remember, practice is key to becoming proficient in Solidity and smart contract development. Happy coding!