How to Code Ethereum — A Three Part Series (Part 2)

In the first part of this tutorial, we talked about Smart Contracts. We defined what it is and how it works, and we also listed the 4 tools that are to be used when deploying it when testing Smart Contracts on two types of networks.

Set Up

If you want to understand this second part of the tutorial, you must be familiar with what a command line is and how it can be used. You should also know a bit about NPM.


Open a command line and then type this:

npm install -g truffle

If everything doesn’t go as intended, read more about Truffle.


The second step is to set up Ganache command line

npm install -g ganache-cli

You can read more about Ganache.

Get Started

To start, set up a folder and then type out the following line

truffle init

This will start a truffle project that’s empty.

Then find the wrestling.sol file that you made in the first tutorial and then copy it into a file named ‘contracts.’

Then open the “migrations” folder and set up a new file known as “2_deploy_contracts.js”. The function of the migrations is to assist in setting up contracts in a blockchain.

Paste this code into it and then save:

const Wrestling = artifacts.require(“./Wrestling.sol”)

module.exports = function(deployer) {



The “Wrestling.sol” file is imported, and in the fourth line, it gets set up in the blockchain.

In the root folder you will find the files “truffle.js” and “truffle-config.js” if you are using a Windows Operating system, then remove “truffle.js,” but if you are working using a different system, it won’t matter whether you retain it or remove it.

Since I am using windows, I will delete the “truffle.js” file and then input this code into the “truffle-config.js.”

module.exports = {

// See

// for more about customizing your Truffle configuration!

networks: {

development: {

host: “”,

port: 7545,

network_id: “*” // Match any network id




In case you are using a development network, use “” to connect to the host. Use code 745 for the port.

The next step is to test the blockchain.

Open Ganache and then input the command line below

ganache-cli -p 7545

Ganache will then set up several test accounts. Since 100 ethers is inbuilt into it, you can send the coins. You will then carry out two commands.

truffle compile

truffle migrate — network development

The code will then be installed using Migrate. You can find this in the “truffle-config.js” file that we set up before in the in the “development” network.

To activate the Truffle console, type this command for it to interact with ganache

truffle console — network development

but first execute the commands below

account0 = web3.eth.accounts[0]

account1 = web3.eth.accounts[1]

Doing this allocates the first account’s address to the variable 0, and the second account to account1.

Then write the following

Wrestling.deployed().then(inst => { WrestlingInstance = inst })

This allocates reference in the part of the contract installed by truffle into the variable “WrestlingInstance”.

Then execute this line

This returns the address of wrestler1.

Then set up the next account

WrestlingInstance.registerAsAnOpponent({from: account1})

To get the second wrestler’s address, use this line

At this point, the wrestlers can start the game.

WrestlingInstance.wrestle({from: account0, value: web3.toWei(2


WrestlingInstance.wrestle({from: account1, value: web3.toWei(3,


// End of the first round

WrestlingInstance.wrestle({from: account0, value: web3.toWei(5,


WrestlingInstance.wrestle({from: account1, value: web3.toWei(20,


// End of the wrestling

The directive “web3.toWei(5, “ether”)” sends five ether, which is then converted to Wei.

When the last line is executed, then account1 becomes the winner, because we input 23 in it, which is twice as much as what was input into account0.

Using Geth

Download geth and if you are using a Windows computer, add the installation folder for geth in your PATH variable.

Download Mist.

Creating a Local Private Test Network

Create a file in the root folder “genesis.json.” This is the configuration that will set up a new blockchain.

You have to specify the parameters when running Mist and geth to avoid errors. You can use a public test network, but in our case, we will build a network using the “genesis.json” file we had created earlier.

Open up a new command line interface and input the following details

geth — datadir=./chaindata/ init ./genesis.json

Start geth and indicate that the blockchain will be placed in the Chiandata folder and start the “genesis.json” file.

Initialize geth using this command

geth — datadir=./chaindata/ — rpc

The “ — rpc” parameter indicates to Geth to get RPC connections and enable truffle to connect with geth.

Start up a new command line and Star up either Mist or Ethereum Wallet using similar parameters.

mist –rpc

The “rpc” instructs Mist or Ethereum Wallet to connect to geth.

Go to Wallets tab and click on Add Account and set up a new wallet. Use a private network and not Mainnet. I am going to use the password 123456789. You can set up a stronger password than this.

Start up a new command and input this command

geth attach

This will run the geth console, making it possible to work with it.

Once you have set up your new Mist account, run this command inside the “geth attach” console.


This will activate a miner. This is the procedure that will verify transactions.

Start up the “truffle-config.js”

Configure “ourTestNet” to connect to geth instance. Geth will be automatically activated on port 8545.

Using the command line interface where “get attach” was launched, you can use the command below to unblock the account to move the Smart Contract from Truffle.

personal.unlockAccount(‘0x6AC4660c960Aaf5d9E86403DDAcB4791767e5869’, ‘123456789’)

Notice that the address I have used here is for the account I created and the password 123456789. Make sure to use your address and password for these tests. Use this method when you are using a real account.

Go back to the command line where you launched Truffle and input this command

truffle migrate — network ourTestNet

The effect of this is that it will start moving the contract where geth is running. Ensure that the miner is running. Otherwise the procedure might not be completed.

To start the Truffle console, input the command below

truffle console — network ourTestNet

then input these commands



The first line will show the address of the installed Wrestling contract instance.

The second line will show the Wrestling Contract ABI. The ABI generally describes the contract. It includes a function list and variables.

Go back to Mist and then click on the Contracts tab then click on watch contract.

Next, paste the address of the contract that we installed.

Click OK, and it will be added to your contracts list. Click it open, and it will open the contracts page.

If you want to interact with the contract, use the select part function in a similar way to what we did with the Truffle console function.

This is how Ganache and geth are interconnected. When you want to install a contract in the real blockchain use the second method, where you will connect geth with mainnet.

Note that it is possible to set up a contract directly on the Mist platform without having to use the Truffle Migration system. Here is a videoexplanation of how to do that. During the real development process, you might get fascinated when using Truffle since you will be able to add many other smart contracts and scripts, in case you decide to use the modular approach.

More on the Wrestling Contract

When talking about the wrestling contract, we didn’t discuss anything about its lifecycle, like when it is started, used and its end when it is no longer in existence.

For instance, a wrestler might decide to withdraw from the game after they have only played one game. This gives rise to the question, then what happens to the other players? We should create a possibility for other players to withdraw from the game in case one person decides that they are no longer interested in playing.

We should also consider situations where the contract will no longer be used. In our case, when the wrestling comes to an end. To allow a contract to receive ether, you can do these three things. You can use the “payable” keyword to the function that is meant to receive it. You will also receive ether and not be able to refuse it in case the contract self-destructs, and all the ether in it is designed to be sent to a given address. You can also receive ether when ether is mined to a contract. Therefore, always keep the option of ether open in your contract.

In our case study the winner gets to keep all the ethers. Therefore, we can come up with an alternative withdraw function like the one below, to allow him to withdraw whatever amount they want from the ether as many times as they want.

There is always the possibility of the process failing when you are making a withdrawal. Therefore, give the contract users the option of withdrawing their money rather than sending it directly (Like in the case of the Wrestling match).

Keep in mind that even if a contract self-destructs, it is possible for it to receive ether still using any of the three methods discussed above since the contract will still be in the blockchain even after self-destruction.

Always have a plan B in case your original plan fails, and things do not go as expected. Once you have installed a contract, you cannot make any changes to it whatsoever. That is why it is advisable that you keep a kind of lock that you can trigger so that you can either pause the transactions or send the ether to another account, where it can be withdrawn. It is up to you to decide whether you want to have a trigger or not, since having a trigger introduces a third party to the equation hence, decentralizing the whole process.


To ensure safety in Solidity, keep in mind a few things. You need to follow common development techniques. You should also use the latest platform that is up to date and ensures that your platform doesn’t have any problems by testing it. You should also know the limits of the platform and try as much as possible to use simple codes. Since Ethereum also has bugs and is changing on a daily basis.

Restrictions on This Platform

You should be aware that when using Ethereum platform, you are discouraged from using complex computations. Also, your transactions will be restricted by gas. The logic used should be as simple and straightforward as possible. You should also know that there is infinite loops and storage is limited. There is also value overflow and many other details. It is important that you consider all these factors before you set up your contract, keeping in mind that once it is installed, it cannot be modified.

It’s important to stay updated about what is going on the industry since the application where the Ethereum Blockchain is founded and the compiler are being worked on and developed on a regular basis. Therefore, try and get information about what is going on about it, to keep up with the latest developments.

Related Posts