Deploying smart contract to Rinkeby testnet using Truffle and mac OS X

Testnet as a place to test your smart solutions

There will be 2 articles about deploying smart contracts:

  1. Moving contract from test RPC to Rinkeby testnet using truffle and Mac OS
  2. Connecting to deployed contract using Infura. (In progress)

Introduction

Testnet is a place to test your smart contracts solutions. Basically, it’s a clone of the Ethereum network that allows you to deploy and test your smart contracts without paying real fees.

There are several main testnets:

Metamask capture
  • Ropsten Test Network — Proof-of-Work (PoW)
  • Kovan Test Network — Proof-of-Authority (PoA)
  • Rinkeby Test Network — Proof-of-Authority (PoA)

Here’s a bit on different consensus protocols.

One step left to start! Before moving your contract to Testnet, make sure you have installed geth, truffle and Mist.

Deploy contract from test RPC to testnet (Rinkeby)

First of all, to deploy your project, you have to download the full rinkeby node (containing transactions by users for all the time).

To download all chain data:

  • Open your finder, for example at Documents.
  • Create new folder, rename it as “rinkeby” and open it.
  • Create a new folder inside rinkeby and call it as “chaindata”.
  • Open your terminal and go to Documents/rinkeby with command:
$ cd Documents/rinkeby/
  • And then to download all chain data run next command:
$ geth --datadir=./chaindata --rinkeby

You will see something like this:

Mind about this url. It is your path_to_your_ipc_file
  • Copy path_to_your_ipc_file somewhere.

Progress of chain downloading you will find in next step in Mist.

Quick Tip: Parameter — rinkeby says to geth that chain data has to be downloaded from rinkeby testnet.

Create a test wallet with Mist and rinkeby network

Ok, we have to pay for deploying out contract, so we need create ethereum account, because we have to pay for this work with gas (don’t worry we will pay for not real money). So Mist will help us to do this.

Quick Tip: Mist is an ethereum client where you can create your new wallets or manage them. More about it you can find here.

  • To open Mist which will be connected to our rinkeby network we will use command (see example below):
$ /path_to_your_application_/Contents/MacOS/Mist --rpc /path_to_your_ipc_file

path_to_your_ipc_file you can find it at the beginning of this article.

I have installed Mist into my applications. So in my case it looks like this:

$ /Applications/Mist.app/Contents/MacOS/Mist --rpc /Users/bomalkevych/Documents/rinkeby/chaindata/geth.ipc

After this command you will see Mist’s banner:

Mist starting (rinkeby network)
  • Tap on «LAUNCH APPLICATION». (In some cases it can be launched automatically)
In my case, all packets were downloaded (1.655.084)

Then, you will see in the left bottom corner how much chain packets were downloaded. It can take you from several minutes to several hours depending on your internet connection and how much peers are connected to testnetwork.

Quick Tip: If you want to see downloading status without Mist you can attach to connection (connection which you opened during step 1) and check it out in a terminal.

So, to do this:

  • Open a new terminal window (CMD + T)
  • Run next command to attach to the connection:
$ geth attach /path_to_your_ipc_file

path_to_your_ipc_file — you can find it at the beginning of this article.

Again, in my case it looks like:

$ geth attach /Users/bomalkevych/Documents/rinkeby/chaindata/geth.ipc
  • Finally in this tab of terminal run next command:
$ eth.syncing

It can return something like this:

{
currentBlock: 13445,
highestBlock: 1604086,
knownStates: 32228,
pulledStates: 19524,
startingBlock: 0
}

What means what last block was loaded or can return «false» what means that full chain was loaded.

Create a new Wallet

If you have one you can skip this step. How to create a new wallet and manage funds step-by-step you can find here.

Adding funds to a test account

During our chain is loading, we can add some ether to our account. To do this, you have to:

  • Open Mist, choose your wallet, and copy it’s address.
Copy address, copy anyway

There are some short and clear information how to get funds. In several words:

  • Choose some social network: facebook, google+ or Twitter.
  • Make a post with the address wallet you copied before.
  • Copy link to the post you made.
  • Paste it in the input field on https://www.rinkeby.io/#faucet (see screen).
Add funds to your wallet
  • Wait while a block with your transaction will be added to blockchain register (It can take several minutes). To check status of your transaction use etherscan.

Etherscan

To see details of your account transactions (history, statuses etc).

Scan transaction details
  • Open some transaction and see status. Result should be like:
Example of transaction details

Deploy contract with Truffle

When syncing will stop (in first tab):

  • Go to first tab in terminal and break syncing with (CONTROL+C) which we run during first step.
  • Run command:
$ geth --datadir=./chaindata --rinkeby --rpc --rpcapi db,eth,net,web3,personal
  • Open your project’s truffle.js file.
  • Replace from parameter in code below with your wallet number and paste it in your truffle.js.
rinkeby: {
host: "localhost",
port: 8545,
network_id: "4", // Rinkeby ID 4
from: "0x99a4572656eb49FFEEFbe9588f8e7ab0F8D6Eb5e", // account from which to deploy
gas: 6712390
}

In my case, it looks like this:

Remember! Replace from with your wallet address.

Next step is to attach to the active connection we did at the begining of this Section and unlock our account. So to do this:

  • Open a new terminal window (CMD + T)
  • Run next command to attach to the connection:
$ geth attach --rpc /path_to_your_ipc_file

path_to_your_ipc_file — you can find it at the beginning of this article.

Again, in my case it looks like:

$ geth attach /Users/bomalkevych/Documents/rinkeby/chaindata/geth.ipc
  • Finally in this tab of terminal run next command (instead of 0x99a4572656eb49FFEEFbe9588… paste your wallet number):
$ personal.unlockAccount("0x99a4572656eb49FFEEFbe9588...")
  • Input account password and press Enter. (It should return “true”)
  • Open a new tab in the terminal and go to your project folder in terminal.
  • Finally to deploy your contract, use command:
$ truffle migrate --network rinkeby

Troubleshooting

First: If you want to redeploy your contract and you get a message like this:

Using network 'rinkeby'.
Network up to date.

Try to delete build folder and run command again.

Second: If you get error message during deploing which connected to gas limit.

Check your truffle.js file you set gas value as: 6712390


Thanks for reading!

If you have other errors during deployment or you interested in another topic, please add comments and upvote 👍. We‘re interested in the dialog.