Understanding Solidity by example

Lakshay Maini
Coinmonks
6 min readFeb 25, 2022

--

In this short article, I will walk you through some Solidity code examples written in the Remix IDE to give you an overall idea about how it works and some common use cases.

Source: ethereum.org

Introduction

This article is for coders that already have some knowledge of some programming languages and Object-Oriented Programming.

The examples that we will cover are currently available in the Remix introduction examples, I will copy/paste some chunks of code in this post just in case they update or delete it.

There are 3 use cases covered ordered by increasing complexity:

  • Storage: Very simple smart contract that stores a value in an integer variable.
  • Owner: Similar to storage but in this case the contract stores an address type variable called “Owner”.
  • Ballot: In this last example we have a much more useful and “Real-world” use case, the contract implements the voting process along with vote delegation.
Source: ethereum.org

Example 1: Storage

Solidity By Example — Storage.sol

Meaning

The store function will update the value of the number variable with the given argument. The retrieve function will return the current value of the number function.

Concepts

  • pragma: This keyword is used to enable certain compiler features or checks. A pragma directive is always local to a source file, so you have to add the pragma to all your files if you want enable it in all of your projects.
  • contract: The usual analogy with OOP programming languages is that contracts are like classes, that contain attributes and methods among others.
  • public view returns : They are modifiers and essentially they can provide some restrictions or validations to the function that contains them. This case public means that the function can be called from outside the contract, view which means that the function can’t change the state of the contract and returns simply means the return type of the function, note that you can set arguments to the modifiers like in the returns example.

Example 2: Owner

Solidity By Example — Owner.sol

Meaning

This code does something similar to the first example, stores and updates a variable inside the smart contract. But in this case, instead of a simple integer, we have an address data type.

This address (account identifier) represents an owner, that has some specific privilege over the contract, for example, to call some function or to access some value.

Concepts

  • private: Just like in contract functions, attributes have access modifiers, these modifiers restrict from “where” the code can read or change the state of the attribute. In this case is private, which means that the variable only can be accessed from the contract functions. If you want to keep the variable as read-only, then you can declare it as private and then create a getter function with a view modifier that only returns the value.
  • event: is an inheritable member of a contract. An event is emitted, it stores the arguments passed in transaction logs. These logs are stored on the blockchain and are accessible using the address of the contract. In the example, we create an event emitted each time the owner is changed in the changeOwner function.
  • constructor & msg.sender: The constructor function is called when the smart contract is deployed, since the smart contract is immutable, it only can be deployed once, therefore the constructor function will be called just once as well. msg.sender is the address where the current (external) function call came from. In this case, we initialize the owner variable with the address of the deployer.
  • modifier: Solidity provides this keyword to create your own modifiers with custom logic. If the first argument of require evaluated to false then the operations inside the functions are reverted, this is also useful to check that the functions are called correctly. As a second argument, you can also explain what went wrong.

Example 3: Ballot

The code is quite long, is not necessary to read it all, I will explain it in a few sentences in the meaning block and then if you want to read and analyze some specific fragment is up to you.

Solidity By Example — Ballot.sol

Meaning

Implements voting process along with vote delegation, this means that there are some proposals to vote (can be persons, plans, ideas, etc.) and there is one vote per voter.

Each voter can also “delegate” it’s a vote to another voter, this increases the vote “weight” of the receiver (initially the weight is 1 per voter). There can’t be a delegation loop, this means that if Jon delegates his vote to Rose, then Rose delegates her vote to Tom, Tom can’t delegate his vote to Jon again, this loop is prevented from 2 to K people, where K is the total amount of voters.

Finally, there is a chairman that can assign the right to vote to a voter that already delegated its vote.

Concepts

  • struct: They are types used to represent a record, just like in C. Since the concept of a class doesn’t exist (a contract is an analogy of a class), they are widely used in Solidity. In this example, we have 2 different structs: the Voter and the Proposal.
  • bytes32: Is a string with 32 bytes of memory, it’s useful in cases where the value never surpasses the size, it uses less gas because it fits in a single word of the EVM. On the other hand, the string is a dynamically sized type that has current limitations in Solidity (such as can't be returned from a function to a contract).
  • mapping: they can be seen as hash tables which are virtually initialized such that every possible key exists and is mapped to a value whose byte-representation is all zeros. They don’t have a length, nor do they have a concept of a key or a value being set.
  • memory: The memory variables in Solidity can only be declared within the methods and they are short term variables that cannot be saved in the blockchain. It holds the value only during the execution of a function and its value destroys after execution.
Source: ethereum.org

Conclusion

The introductory examples on the Remix page are tremendously good for people that want to understand the basic and not-so-basic concepts of Solidity.

I’m sure that the examples were very well thought out and I believe they are worth a thorough explanation, even more since people like me tend to just delete the code and start writing console.log(“Hello world!”) in the editor without thinking too much.

If you want to dive deeper into this programming language before starting writing smart contracts, I kindly invite you to read the documentation in the Ethereum official docs. 🤓.

--

--

Lakshay Maini
Coinmonks

Full-stack Blockchain Developer | Driven to Deliver Excellence