Deploy your first private Ethereum smart contract using Geth and Web3.js

So what is an Ethereum contract?

Michael Whittle
Sep 22 · 9 min read

An Ethereum smart contract is basically a program that runs using the Ethereum Virtual Machine (EVM) in the Blockchain. An Ethereum address is assigned when it is created and each interaction uses a single transaction. The contract/application will have a state within the Blockchain and the state will change when you interact with it. It’s important to note that once the contract is sent to the Blockchain it can never be changed (it is immutable). You are able to upload the same contract again with changes but the previous contract will remain in place and they will run in parallel with no knowledge of each other. Quality assurance and testing smart contracts is essential before they go into the Mainnet. I did my Certified Ethereum Developer and Ethereum QA Engineer qualifications (amongst others) with B9lab Academy. The courses are really amazing and trainers are superb. If this is a topic of interest I highly recommend them.

Take this blockchain course and become a blockchain developer

How can I create an Ethereum smart contract?

Solidity is one of the main compilers for Ethereum smart contracts. In Ubuntu you can install the latest solc compiler like this but you really don’t want to do that as it will cause you problems.

You should rather use the exact version you want to use. In our case I’m going to use 0.6.6 to match the version we’ll use in Remix next.

Replace “YOUR_CONTRACT.sol” with the contract you want to compile and “TARGET_LOCATION” for the ABI location.

There is this really powerful online Ethereum IDE for developing smart contracts called Remix. You able to create, run, test, troubleshoot, and optimise your contracts there. Another great framework for Ethereum smart contracts is called Truffle. This tutorial is not going into the detail of these two tools but I just wanted to let you know they are there. If you are interested in learning more about this I highly recommend B9lab Academy and their trainers.

I’m going to use one of the example contracts (Owner.sol) from Remix for this tutorial. At a high level this contract will set the owner of the contract as the person who deploys it. There is a “getter” to retrieve who the current owner is and a “setter” to allow the current owner to set another owner. So pretty simple but will serve the purpose.

Quick Remix introduction

Remix loads with a few example contracts. The one we are using is called Owner.sol above. I removed the comments included for this tutorial. I’m going to explain how to interact with it.

We will be working our way down the menu on the left starting with the File Explorers. All you need to do here is select Owner.sol.

You then want to move one menu option down to the Solidity Compiler. To keep things simple just click on the Compile Owner.sol button in blue.

Now move one menu option down to the Deploy & Run Transactions menu. Just click the orange Deploy button.

Please notice that Remix provided several funded accounts to work with. Each account is funded with 100 ether. Test ether obviously :) I’m going to deploy the contract using the first account starting with 0x5B3.

After you click the orange Deploy you will see the contract deployed under Deployed Contracts. You will also notice it has its own Ethereum address.

I expanded the contract and you can see the two methods from the contract there. The “gettergetOwner and the “setterchangeOwner with an input field. I click on the getOwner button and true enough you see the address that deployed the contract: 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4

I want to change the owner of the contract. Up at the top I move to the second account to be able to copy it and then move back to the first account which is the owner.

Now from the 0x5B3 account I run the changeOwner method to set the new other to the 0xAb8 account.

Now I run the getOwner method again and true enough the owner as changed to 0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2

So that’s just a 1 minute intro into Remix. I do recommend you trying this all out yourself. Try changeOwner to an invalid address or maybe try and changeOwner not as the owner and see what happens.

Compiling Owner.sol using the Docker version of solc 0.66 in Ubuntu

First copy Owner.sol from Remix into a directory for compiling.

This will create Owner.abi, Owner.bin and combined.json in the current directory.

Important note: for some reason compiling with the Docker solc causes the contract name to be incorrectly. Make sure the contract says “Owner.sol:Owner” and not “/root/Owner.sol:Owner” as below.

In order to read this JSON output easily I recommend installing “jq”.

Now for this next part you will need an Ethereum Geth node to install the smart contract into. If you have not read and deployed an Ethereum private blockchain with network ID 2020 from my other article “https://medium.com/coinmonks/creating-and-exploring-a-private-ethereum-blockchain-using-geth-d71afc5cdcf9” please do that now.

I’m going to attach to my working node now…

To deploy the contract please do as follows.

Please notice that the contract was deployed and our two methods are there…

There is the transaction receipt in the Blockchain.

The contract is deployed and working. You will not want to keep deploying the contract over and over to integrate with it. This is how you would interact with an existing contract.

Now you can supply the contract address here. We are using a variable but you can supply the actual address as well. Please notice the “.at” instead of the “.new” previously used. This means you want to retrieve an existing contract and not create a new one.

Anyone can retrieve the contract binary.

And this is how we access our contract methods.

Interacting with your smart contract using the React.js or Node.js library (Web3.js)

So let’s start with creating a very basic React.js application. I’m going to use the “create-react-app” tool to create the default application.

As per the instructions if you change directory to “mediumtutorial” and “yarn start” it should open up your default browser and you should see this.

You want to install Web3.js next.

This library is available for both Node.js and React.js. You are able to interact with your Geth node RPC like this.

If you enjoyed reading this article and would like me to write on any other topics please let me know in the comments or email me directly.

I’m the Head of the Networks Practice at Net Reply. My team specialises in networks, security and process automation including self-service dashboards. If you would like more information on this please contact me on m.whittle@reply.com. Alternatively you can learn more about us on LinkedIn and Twitter.

Also, Read

Get Best Software Deals Directly In Your Inbox

Image for post
Image for post

Coinmonks

Coinmonks is a non-profit Crypto educational publication.

Sign up for Coinmonks

By Coinmonks

A newsletter that brings you week's best crypto and blockchain stories and trending news directly in your inbox, by CoinCodeCap.com Take a look

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Michael Whittle

Written by

CCIE R&S #24223 | Network Architect | Security Specialist | Software Developer | Blockchain Developer | Studying Machine Learning & AI

Coinmonks

Coinmonks

Coinmonks is a non-profit Crypto educational publication. Follow us on Twitter @coinmonks Our other project — https://coincodecap.com

Michael Whittle

Written by

CCIE R&S #24223 | Network Architect | Security Specialist | Software Developer | Blockchain Developer | Studying Machine Learning & AI

Coinmonks

Coinmonks

Coinmonks is a non-profit Crypto educational publication. Follow us on Twitter @coinmonks Our other project — https://coincodecap.com

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store