Writing your first Ethereum Smart Contract

We are going to go through the steps of creating your very first smart contract on the Ethereum blockchain. We will be using a testnet so you wont need to fork over any money to deploy the contact. Solidity, blockchain and smart contracts can at first seem overwhelming with all the new words and jargon around creating these new services on a decentralised chain ( a public database ).

There are a few things we are going to need first

Remix

Remix is the IDE we are going to use, setting up a decent development environment to work with blockchains and deploying smart contracts to local/test/mainnets requires way more effort that I want you to go through to get that sweet feeling of success with writing your first smart contract.

MetaMask

This is the wallet we will be using to push our contracts to the testnet. So if you are already a developer you likely are familiar with dev / test/ production systems. The same exists with in blockchain, with the testnets we can get free tokens from faucets which will allow us to perform transactions. if you use a dev chain on your local or Javascript VM within Remix then you wont need to get tokens from a faucet.

Important -- Do not use your wallet with real funds to do any development work, have a separate browser profile for this.

Faucet

A faucet is where we can get tokens for free on a network, when we want to make transactions on a blockchain we need to pay GAS for it. The way I like to think about it is the cost of renting processing power from a global computer, a simple transaction of sending X amount of tokens to another wallet will have a low cost, Interacting with a smart contract that does lots of calculations, reading, writing can cost a significant amount more. Lucky for us we will be on a test net so cost will only be your time.

How to get ETH - Kovan tokens from a faucet

We have all that we need now.

Lets GetCoding!

Creating the Smart contract

When we first get into Remix you will notice there are a few folders and files already populated for us, Lets get rid of them 🙂

Adding our Contract

Right click on the contracts folder and select new file, enter the filename SimpleContract.sol

This SimpleContract.sol file is what we will compile and deploy onto the blockchain! Get ready to level up your awesomeness!

First things we need to do are add a license and define what compiler version to use, with out either of these the compiler will complain.

// License in the line below is commented out, but is required. This type is the most appropriate for public works
// SPDX-License-Identifier: MIT

// 1. Pragma -- Compiler version
pragma solidity ^0.8.0;

Once you have this code in your SimpleContract.sol you can git CRTL+S and it will save & compile the contract. You will see a green tick on the side if this has worked.

Green tick visible on the left most column

Now it is time to get our smart contract to do something, What we are going to do is have a variable we can store a number that you will pass to it, you will also be update and retrieve it

// License in the line below is commented out, but is required. This type is the most appropriate for public works
// SPDX-License-Identifier: MIT

// 1. Pragma -- Compiler version
pragma solidity ^0.8.0;

contract SimpleContract{
    uint256 private simpleNumber ; // Declaration of our storage variable

    function setSimpleNumber(uint256 _simpleNumber) public {
        simpleNumber =_simpleNumber;
    }
    function getSimpleNumber() public view returns(uint256){
        return simpleNumber;
    }
}

The above is the complete code we will be working with here, we have our contract declaration, defining our storage variable and then get/set functions for it. Hit CRTL+S and compile the contract.

Deploying the contract

For us to compile our contract we will do the following steps

  1. Navigate to the Deploy and Run transaction item in the left most column ( the one at the bottom )
  2. Select our Environment ( Javascript VM London )
    • Initially we will do this on a local/dev network and after we have success there we will do this on a Ethereum testnet.
  3. Select the Contract SimpleContract to be deployoed
  4. Click Deploy
Arrows point to all the items of interest you need to deploy.

After you have clicked deploy you will see a item underneath the Deployed Contracts area, expand this and you will see two options one is for our setSimpleNumber function and the other is for the getSimpleNumber Function.

Click on getSimpleNumber ( the blue one ) and you will see a value of 0, this is the default value assigned.

Lets update this to a different number, enter 50 into the text box next to setSimpleStorage and then click the orange button. Once this has completed you can click the getSimpleNumber button and you will see the updated value

Congratulations !! your smart contract has worked!

you can stop here if you are satisfied with that. If you want to deploy this to an Ethereum Test net follow on below.

I'm Assuming you already have test net ETH in your wallet. If not go here and follow these instructions -- TOCOME --

Set metamask to the Kovan Network , and then change the Environment in remix to Injected Web 3, you wll be prompted to connect your metamask to remix

Ready to deploy to Ethereum Kovan Testnet

Click on Deploy, you will have meta mask popup OR flash a 1 on the extension icon to indicate you have a transaction to review & approve.

Approve the transaction and wait it to be processed on chain

Assuming everything went well you will see a newly deployed contract, as well as an information in the debug console indicating the transaction has completed

If you copy the transaction hash and navigate to kovans etherscan enter the transaction hash ( tx hash ) and you will see the transaction on the kovan testnet blockchain!

Repeat the steps before where you setSimpleNumber & getSimpleNumber, you will need to sign a transaction for setSimpleNumber as we are writing data to the smartcontract and altering the state of the chain. But to perform a getSimpleNumber you will not need to.

If you got to the end of this well done! You have learned a lot here and are on your path to learn smart contracts and block chain development! Tweet your success and take be @_Matt_Heff to let me know how you went.

Comments are closed!