Mistral logo

Starting Ethereum - Your First Smart Contract

23.11.2018 - READING TIME: 7 minutes

Starting Ethereum - Your First Smart Contract

Author: Adnan Karisic

 

When I realized all the possibilities blockchain provides, I immediately wanted to create something. So I decided to work on Solidity smart contracts. Even though the technical documentation I had was great, making the first code steps were surprisingly hard.

This blog aims to help you break the “this is impossible” barrier that magically appears in the beginning (been there) and sets you up for a great start.

If you want to learn more about Ethereum before you start coding, I would suggest you read this article.

What exactly are we making?

If you follow the steps right, by the end of this blog you will have created a (not so smart) smart contract that will contain some data stored on a blockchain.

In the process, you get familiar with Remix, in-browser IDE (integrated development environment) for developing smart contracts, and Solidityprogramming language.

First things first: What is a Smart Contract?

Smart contracts are just accounts on Ethereum blockchain that can be programmed to self-execute. They contain functions and can interact with other contracts, make decisions, store data, and send ether to others. Contracts are defined by their creators (developers), but their execution is provided by the Ethereum network itself.

They will exist and be executable as long as the whole network exists, and will only disappear if they were programmed to self-destruct. So basically the contracts can be executed by your application’s users as long as Ethereum exists.

Smart contracts can:

  • Function as ‘multi-signature’ accounts, so that funds are spent only when a required percentage of people agree
  • Manage agreements between users, say, if one buys insurance from the other
  • Provide utility to other contracts (similar to how a software library works)
  • Store information about an application, such as domain registration information or membership records.

Why Solidity?

“Solidity is statically typed, supports inheritance, libraries and complex user-defined types among other features.” — Solidity docs

It is the best documented and the most common language Ethereum developers use for developing smart contracts. Solidity is not perfect and it is sometimes hard to write secure code with it.

When creating production applications, you should try and make your smart contracts as simple as possible. That will make them more readable and auditable which will make it less likely to have critical bugs.

Solidity allows you to write programs that enable voting, representing physical goods via tokens, decentralized exchanges, organizations and more. You can read more about Solidity in docs.

In the future, there may be support for other commonly used languages like Rust, Go or C++ via upgrades to EVM through ewasm. Another option is Vyper. While it’s currently unstable and in development, it that may one day replace Solidity.

Let’s dive into the code

We’ll run our code in an online IDE for Solidity, Remix. After you open it, you will see an example of a Ballot smart contract. While it is a great code to analyze and understand many features of Solidity, we’ll start with something simpler.

First time opening Remix IDE

Create a new file by clicking a + button in the upper left corner. Let’s give it a name: SimpleStorage.sol. Copy/paste the code below into the newly created file.

// This code example is obtained from Solidity docs
pragma solidity >=0.4.0 <0.6.0;

contract SimpleStorage {
uint storedData;

function set(uint x) public {
storedData = x;
}

function get() public view returns (uint) {
return storedData;
}
}


Dissecting the code line by line

// This code example is obtained from Solidity docs — inline comments start with // like in many programming languages.

pragma solidity >=0.4.0 <0.6.0;the first line specifies the version of solidity compiler we want our code to be compiled with. In this example, we want to use the latest available compiler between 0.4.0 and 0.60 versions.

contract SimpleStorage { defining a contract is similar to defining a class in object-oriented languages. Like classes, contracts can utilize some form of inheritance. Because of this, Solidity is considered a Contract Oriented Programming language.

uint storedData; —Solidity supports different number types. You choose which one to use based on how much memory you need to allocate. While it’s fairly similar to JavaScript, Solidity has many variable types.

We used uint, an unsigned integer. That means it can only represent positive numbers. The range of numbers it represents is inside the scope of 256 bits. You could also use uint256 instead of uint to make it more declarative since uint is just an alias for uint256.

function set(uint x) public {
storedData = x;
}

This function stores the acquired value to previously defined storedData. An important note here is that this function mutates the data, which means it will change the state on the Ethereum blockchain when it’s called.

Every state change costs and the person who invokes this function will need to pay a fee to execute it. The unit that measures the amount of computational effort that it will take to execute certain operations is called gas. The final fee is calculated by multiplying the amount of gas required to execute a function, and ether the user is willing to pay per one unit of gas.

function get() public view returns (uint) {
return storedData;
}
}

Unlike the previous function, this one reads the data that is already stored on the blockchain. When calling it, you are only interacting with your local copy of a ledger.

The keyword view promises that the function will not mutate any data. To specify which type of data the function should return, there is another keyword returns followed by (uint) which is the same type as storedDatathat is being returned.

Run it in Remix

Let’s compile and run the contract now.

Remix has a few tabs in the top right corner. In the Compile tab, click ‘Start to compile’ and you should see the green box with SimpleStorage written inside it.

Under Run tab, in the Environment section, there are three options. We will use the “Javascript VM”


This option will let you run your smart contract in your browser locally, and simulate accounts and transactions.

The first thing we have to do is deploy our smart contract; make it available on blockchain so that we can interact with it. Clicking on the red Deploy buttondoes the trick. After you deploy the contract, look at the accounts at the top right. You will notice that a simulated account was triggered, and a small fee paid by the selected account. Instead of 100, it now holds 99.99 ether.

In the bottom on the right side, you can now see your Deployed Contracts. If you click on the SimpleStorage it will expand, and you will notice the two functions we previously defined.


Let’s switch from the account we used to another, intact one. Just click on the accounts and pick one that still has 100 ether. Now go down and click on the ‘get’ button. You will see it returns 0 since it’s a default value of the storedData variable.

Now let’s type a number next to the ‘set’ field. Place any positive number, but wrap it around in quotation marks (e.g. “420”). Click on the set button and then get again. You will see an output of the stored number.

Where to go now

We have created the simplest possible smart contract and used Remix to compile, deploy and interact with it in our browser. If you are still curious, the next step should be to try and create something more complex.

Now when you know how to use basic functionalities of Remix, you could explore the Ballot contract that comes when you first open the integrated development environment (IDE).

When you’re ready to step up the game, or when you have some ideas of what you’d want to create, I suggest you check out Truffle and OpenZeppelin smart contract frameworks that’ll give you a nice set of development tools and let you develop easily in any code editor.

All that is left to be said at this point is good luck and have fun with your blockchain journey, and if you need any assistance or advice, feel free to write in the comment section below.