Ethereum Development Walkthrough (Part 2: Truffle, Ganache, Geth and Mist)

47,727 reads

We have seen in the first part of this tutorial series what a smart contract is, and how it works. Now, we will deploy it to two kinds of test networks.

The tools we will use

The most prominent tools at the moments are:

Truffle: A development environment, testing framework and asset pipeline for Ethereum. In other words, it helps you develop smart contracts, publish them, and test them, among other things. You can read the docs of the Truffle suite for more informations.

Ganache: It was called TestRPC before, if you have read a tutorial from a few months ago, chances are they use TestRPC with Truffle, but it was renamed upon the integration of TestRPC within the Truffle Suite. What Ganache does is simple, it creates a virtual Ethereum blockchain, and it generates some fake accounts that we will use during development.

Mist: It’s a browser for decentralized web apps. It seeks to be the equivalent of Chrome or Firefox, but for Dapps. It’s still insecure and you shouldn’t use it with untrusted dapps as of yet.

Ethereum wallet: It’s a version of Mist, but only opens one single dapp, the Ethereum Wallet. Mist and Ethereum Wallet are just UI fronts. And we need a core that will connect us to an Ethereum blockchain(It could be the real Ethereum blockchain, or a test one).

Geth: Is the core application on your computer that will connect you to a blockchain. It can also start a new one (in our case we will create a local test blockchain), create contract, mine ether etc.

We will start by using Truffle and Ganache, and then use Truffle with geth and Mist.

Installations

The requirements for this tutorial are that you know what is and how to use a command-line tool, and you are a bit familiar with NPM.

Truffle

Open up a command-line, and type the next part:

npm install -g truffle

If something goes wrong I advise you to read more on Truffle’s docs.

Ganache

Then, install Ganache’s command-line interface:

npm install -g ganache-cli

If you are unsure about something, here is Ganache’s Github page.

N.B: There is a GUI for Ganache, but because we are such haxors, we will use the CLI.

Let’s start

First, create a new folder, and type the next line:

truffle init

It will initialize an empty truffle project.

Then copy the Wrestling.sol file from the last tutorial, into the folder “contracts”.

Next, open the folder “migrations” and create a new file named “2_deploy_contracts.js”. Migrations are simply scripts that’ll help us deploy our contracts to a blockchain.

Paste the following code inside, and save.

The first line is there to import the “Wrestling.sol” file from the “contracts” folder, and the 4th line deploys it to the blockchain.

Now, back to the root folder, you’ll see two files, “truffle.js” and “truffle-config.js”. If you are on Windows, remove “truffle.js”, if you are on another system, remove one of them, or keep them both, it’s not important. The reason for that is, on Windows there is a naming problem, and when we will want to execute Truffle commands, it will open the config file “truffle.js” instead of reading what’s inside.

I’m writing this tutorial on Windows, so I’ll remove the “truffle.js” file. Then I’ll put this code inside of “truffle-config.js”:

It basically says, that, when using the development network, connect to the host at “127.0.0.1” (localhost), using the port 7545.

Now we are ready to test our code on a blockchain!

Testing our code

In the first part, we will use Ganache.

Fire a new command-line and type in the following command:

ganache-cli -p 7545

What it does is, it tells ganache-cli to start at the port 7545.

Ganache will generate test accounts for us, they have 100 ether by default, and are unlocked so we can send ether from them freely. The first account for example is the guy right here:

Now, back to our first command-line interface, we execute two commands:

truffle compile

truffle migrate --network development

Compile will compile our Solidity code to bytecode (the code that the Ethereum Virtual Machine (EVM) understands), in our case, Ganache emulates the EVM.

Migrate will deploy the code to the blockchain, in our case, the blockchain could be found in the network “development” we set earlier in the “truffle-config.js” file.

Now, if everything has gone as expected, you should see this on your terminal:

Notice that it shows the address of the instantiated Wrestling contract.

On the commande-line interface where ganache-cli is run, you can see the transactions being executed:

Notice that it shows the address of the instantiated Wrestling contract.

Now type the following command to start Truffle’s console. It will help us interact with ganache’s blockchain.

truffle console --network development

First, we will execute this commands:

account0 = web3.eth.accounts[0]

account1 = web3.eth.accounts[1]

It will assign the address of the first account to the variable account0, the second to the variable account1. Web3 is a JavaScript API that wraps RPC calls to help us interact with a blockchain in an easy way.

Then we will write this:

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

It assigns a reference to the instance of the contract that truffle deployed to the variable “WrestlingInstance”.

Execute the the next line:

WrestlingInstance.wrestler1.call()

It will return the address of the wrestler1, in our case it was the first account. Truffle, during the migration, picked up the default account from Ganache, which is the first account, because we didn’t specify another account address during the migration, or another address in the configuration file of Truffle.

We will then register the second account as an opponent:

WrestlingInstance.registerAsAnOpponent({from: account1})

Here, the “from” directive tells the function from which account the transaction should be fired.

Upon executing the line, it should return something similar to this:

Notice that the transaction used Gas, and it fired the event “WrestlingStartsEvent”.

You could retrieve the address of the second wrestler by executing the following line:

WrestlingInstance.wrestler2.call()

Now, the players can wrestle:

WrestlingInstance.wrestle({from: account0, value: web3.toWei(2, "ether")})

WrestlingInstance.wrestle({from: account1, value: web3.toWei(3, "ether")})

// End of the first round

WrestlingInstance.wrestle({from: account0, value: web3.toWei(5, "ether")})

WrestlingInstance.wrestle({from: account1, value: web3.toWei(20, "ether")})

// End of the wrestling

The “value” directive is used to send ether with a transaction. The “web3.toWei(5, “ether”)” part sends 5 ether, that value is converted to Wei. Wei is the base unit (lowest denomination) of ether. More infos could be found at this link.

Upon executing the last line, the account1 will be the winner, since we put 23 ether in total with it, and it’s more than the double of what we put with the account0.

A little exercise for you is to withdraw the ether from the contract.

Now, it’s up to you to research how to use Truffle’s and Ganache’s advanced features. You can start by reading the docs, and alternatively, here is an excellent introduction to Truffle if you felt lost or want to reinforce your knowledge on what we just saw.

How geth will fit in the picture

Now, if we used Ganache for development, we would want to use something closer to a real environment, if only just to be more familiar with it.

Installations

Start by downloading geth. On Windows, you might need to add geth’s installation folder to your PATH variable.

Download Mist or Ethereum Wallet. For our use, both will be alike, so it doesn’t matter which one you pick.

Onto the creation of the local private test network

In the same root folder, create a new file, and name it “genesis.json”. Then past the following content in it.

The file “genesis.json” is simply the configuration file that geth needs to create a new blockchain. It’s not important to understand the content of the file for the moment.

If you run geth without specifying any parameter, it will try to connect to the mainnet. The mainnet is the main network of Ethereum, the real blockchain of Ethereum.

If you run Mist without specifiying any parameter, it will throw an error if an instance of geth is running. If you tell Mist to connect to the geth instance that is actually running(what we will do in a little moment) it’ll work just fine. If you run Mist while no instance of geth is running, it will start a new instance of geth, and eventually ask you from which blockchain network it should download blocks.

There is a public Ethereum test network, but we will create our own private test network locally, using the “genesis.json” file we created earlier.

Fire up another command-line interface and type in the following (be sure to run it in your project root folder):

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

We launch geth and specify where our blockchain will be stored, here in the “chaindata” folder (it will be automatically generated), and we initialize it with our “genesis.json” configuration file.

We then start geth using the following command:

geth --datadir=./chaindata/ --rpc

The “--rpc” parameter tells geth to accept RPC connections, it’s needed so that truffle can connect to geth.

Open another command-line interface, and start Mist (or Ethereum Wallet), using the same parameters:

The “-rpc” tells Mist (or Ethereum Wallet) to connect to the instance of geth we started.

In the Wallets tab, press Add Account and create a new wallet:

Notice that we are using a private network. Beware of that, you wouldn’t want to use you ether on the Mainnet for development purposes.

I will create an account using the password “123456789”. In a real environment, use a stronger password.

Open up a new command-line interface and run this command:

geth attach

It will run the console of geth, and we can now interact with it.

With out main account created on the Mist UI, we will run this command inside of “geth attach” console:

miner.start()

It will start a miner, the process that will confirm transactions, and after a few seconds or minutes (depending or your computer), you should start seeing ether being added to your balance (And also to your main account):

Note that if you don’t have enough RAM available, it may not start mining at all. You can stop the mining using the command “miner.stop()”.

Now, open the “truffle-config.js” file again, and modify it like this:

The “ourTestNet” is the configuration necessary to connect to the geth instance. Geth starts by default on port 8545.

In the command-line interface where we launched “geth attach”, we will unlock the account, so we can use it to migrate the Smart Contract from Truffle, using the following command:

personal.unlockAccount('0x6AC4660c960Aaf5d9E86403DDAcB4791767e5869', '123456789')

Here, I used the address of the account I just created, it would be a different address for you, and the password “123456789” that I’ll use for these tests. Notice that the password is shown in plain text, and you should use a different method with a real account.

Now, back to the command-line interface where we previously launched Truffle, run this command:

truffle migrate --network ourTestNet

It will start migrating the contract to the blockchain that geth is running. Start the miner if you previously stopped it, otherwise, the migration will not be executed.

If the migration was successful, you would see an output like this:

Now, run the following command to start Truffle’s console:

truffle console --network ourTestNet

Then run these two commands:

Wrestling.address

JSON.stringify(Wrestling.abi)

You should have an output like this:

The first line returns the address of the deployed Wresting contract instance.

The second line, will return the Wresting contract ABI. The ABI is basically a description of the contract. It contains a list of its functions and variables among other things.

When copying the address and the ABI, remove the apostrophes highlighted by a red arrow in the screenshot.

Now, back to Mist, we will open the contracts tab, and click on watch contract.

Then we will past the address and the ABI of the Wrestling contract we deployed:

Click on “OK”, then it will be shown in your watched contracts list. Click on it and it will open the contract page. If you scroll down, you’ll see something like this:

Use the select a function part to interact with the contract. It’s the same as we did above using Truffle’s console.

And that’s it, we saw how Ganache and geth come in play. When you will want to deploy a contract to the real blockchain, you should use the second method and connect geth with the mainnet.

N.B: You could deploy a contract directly on Mist without using Truffle migration system, here is an example video of that process. Using Truffle is more interesting in a real development process though, since you’ll be able to include and import multiple other smart contracts and scripts if you use a modular approach to develop your smart contracts.

N.B.2: You could always write your contract code on a basic nodepad application, and deploy it to the mainnet using some trusted third party’s portal, but I advise you to not.

The repository for this tutorial can be found here:

In conclusion

We have seen 4 ways of developing and deploying our Smart Contracts:

The first one is to use Truffle and Ganache. Since we copied the code from the last tutorial, I want to tell you that there are plugins available for the most popular text editors and IDEs. Some offer only syntax highlighting, while others help with other areas.

The second one is to deploy code from Truffle to geth (and the GUI app Mist).

The third, is to use Remix to write small, simple contract when you are just learning Solidity, and deploying the code in Mist like shown in the video previously linked.

Or like a real cowboy, you could use a simple text editor to write and then deploy your untested contract using a nameless third party’s drag-and-drop deployment feature.

In the next part, we will discuss security since our “Wrestling” script is far from being ready to be launched in a real environment.

After, that we will see Token creation, and Initial Coin Offerings (ICOs).

If you liked this second part, you can find me on twitter @dev_zl.

Tags