By the end of this post, readers will be able to:
In essence, smart contracts are computer programs that allow credible transactions of digital assets under certain conditions without third parties. Solidity is an object-oriented language for implementing smart contracts in Ethereum.
Smart contracts allow application development and credible transactions that involve assets without a third-party overseer. Smart contracts rely on the following building blocks:
Solidity, which is a human-readable programming language is like Python in the sense that its syntax comes close to human language (as opposed to the bits and bytes of a more computer-friendly language). This eases our ability to read, write, and understand Solidity code.
Solidity is a programming language that’s statically typed. This means that we need to specify a data type for each Solidity variable. By contrast, we don’t need to do this for Python variables.
Solidity includes several data types. The following image lists the main ones, which you can use for smart contracts, and includes Python and Solidity examples of using them:
The main data types in Solidity are string, positive number, negative number, wallet address, and boolean value.
string
stores a text value.uint
stores a positive number. The keyword uint
stands for “unsigned integer.”int
stores a number. This type of variable can store a positive or a negative integer.address
stores an Ethereum address. This is a special Solidity data type for storing an Ethereum address in a way that’s computationally more efficient than storing a string.bool
stores a Boolean value—that is, true
or false
.In Solidity, we have the function
keyword followed by the function name. Then, the following syntax can specify many function arguments or none. You can define two special types of functions:
By default, Solidity functions are public. This means that the function can be called from outside the contract—by either users or other contracts. This is known as access control in the object-oriented programming paradigm.
The EVM can store items in three areas: in the Storage, Memory and the Stack.
Here’s a summary of what happens when a Solidity smart contract runs:
We use an IDE for Solidity, just like we use an IDE for Python. But instead of using Visual Studio Code or JupyterLab, we’ll use the Remix IDE.
The Remix IDE is an open-source application for developing, deploying, and administering smart contracts that run on Ethereum-based blockchains. We can use this IDE for the entire development cycle of smart contracts and as a playground for teaching and learning Ethereum.
To create a new Solidity script in the Remix IDE, first click the File Explorers button (which appears in the upper-left area of the window). This causes the Solidity Compiler pane to change to the File Explorers pane, as the following image shows:
Just like .py
is the file extension for our Python scripts, .sol
is the file extension for our Solidity smart contracts.
Pragma refers to the version of Solidity that we’ll use. As an example, use version 0.5.0, which we’ll define in the first line of our code.
We specify a smart contract by using the contract
keyword and a set of braces ({ }) that defines the start and the end of the contract’s code.
Let’s wrap all of this together to show a contract script example:
pragma solidity ^0.5.0;
contract CustomerAccount {
address owner;
bool isNewAccount;
uint accountBalance;
string customerName;
string customerLastName;
function getInfo() view public returns(address, bool, uint, string memory, string memory) {
return (owner, isNewAccount, accountBalance, customerName, customerLastName);
}
function setInfo(address newOwner, bool newAccountStatus, uint
newAccountBalance, string memory newCustomerName, string memory newCustomerLastName) public {
owner = newOwner;
isNewAccount = newAccountStatus;
accountBalance = newAccountBalance;
customerName = newCustomerName;
customerLastName = newCustomerLastName;
}
}
Finally, we click the Compile message_contract.sol button to pass our Solidity script through the compiler. If the program successfully compiles, a green check mark indicating “compilation successful” will appear next to the Solidity compiler button.
Until next time, here’s a twitter thread summary of this post: