Smart Contracts & Solidity Fundamentals

By the end of this post, readers will be able to:

  • Explain what a smart contract is.
  • Explain how Solidity works and how it differs from Python as a programming language.
  • Explain how the Ethereum Virtual Machine (EVM) is an isolated environment and how Solidity code can access only on-chain data.
  • Explain how Remix supports blockchain development.
  • Create functions along with getters and setters in Solidity.

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 and Solidity

Smart contracts allow application development and credible transactions that involve assets without a third-party overseer. Smart contracts rely on the following building blocks:

  • A robust, reliable and industry-supported blockchain technology
  • A robust and reliable programming language
  • An isolated execution environment
  • Robust encryption mechanisms
  • A suitable set of development tools for creating and deploying the smart contracts

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:

Python vs. Solidity data types
Python vs. Solidity data types

The main data types in Solidity are string, positive number, negative number, wallet address, and boolean value.

  • A variable of type string stores a text value.
  • A variable of type uint stores a positive number. The keyword uint stands for “unsigned integer.”
  • A variable of type int stores a number. This type of variable can store a positive or a negative integer.
  • A variable of type 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.
  • A variable of type 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:

  • Setter functions, which can set or update the value of any variable.
  • Getter functions, which can get the current value of a variable.

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:

  • The program first gets compiled. This means that it’s converted from human-readable syntax into a computer-friendly version of the code, called bytecode. Once compiled, the code runs in the EVM.
    • The EVM is a software platform that’s embedded in each node of the Ethereum blockchain network. The EVM runs the bytecode that results from running a Solidity smart contract.

Solidity and Remix

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:

Remix File Explorers
Remix File Explorers

Just like .py is the file extension for our Python scripts, .sol is the file extension for our Solidity smart contracts.

Solidity script editor
Solidity script editor

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:

Subscribe to jackofcrypto
Receive the latest updates directly to your inbox.
Verification
This entry has been permanently stored onchain and signed by its creator.