Scroll Top

Deploying a Smart Contract in Rinkeby using Infura

Rinkeby is a “Proof-of-Authority” network, which means that blocks are signed by well-known trusted community members. This prevents attackers from taking over the mining power on the network.

Infura offers Infrastructure-as-a-service for Ethereum, which offers developers a suite of tools to connect their apps to the Ethereum network and other decentralized platforms. It is a hosted Ethereum node cluster that let your users run your application without requiring them to set up their own Ethereum node or wallet.

This article will talk about steps and considerations for deploying an Ethereum smart contract in Rinkeby Testnet using Infura.

Sign-up with Infura

Infura requires a quick signup, which you can do by following the below steps:

  • Visit the following URL: https://infura.io/register
  • Submit registration form by filling in the relevant details
  • Confirm the email receipt

Creating a new project

When you login into Infura, you can see that you don’t have any project. Create your project by providing the project name and you shall be all set with the required details like API Key & Secret and the End Point, as shown below:

In the above screenshot, Infura provides you the following options for the endpoint:

  • Mainnet
  • Ropsten
  • Kovan
  • Rinkeby

Select Rinkeby from the options list. However, the steps discussed in this article will be applicable to other networks as well.

Add Rinkeby Network Options

Depending on your Operating System, edit truffle.js or truffle-config.js and make the changes explained in this section.

Step 1: Add Rinkeby network options

In the network options of module.exports object, add rinkeby option by adding the following configuration:

Essentially Infura doesn’t manage your private keys (i.e. it is not your wallet), hence it cannot sign your transaction. That is why you use HDWalletProvider object so that that truffle can sign transactions using this.

Step 2: Create a separate .env file for tokenKey and mnemonic

Since Rinkeby is a testnet, many people may take an easy approach and directly include the mnemonic and token key inside the truffle.js file. However, I strongly recommend separating the environment specific variables from the code.

Follow below sub-steps:

  1. Create a “.env” file in the project root folder
  2. Add NEMONIC and ENDPOINT_KEY as environment variables

Here are a sample .env file details for you (you must use your own details here):

Step 3: Import truffle-hdwallet-provider and dotenv

Inside truffle.js, add the following code at the top:

The inclusion of dotenv puts all the environment variables in the process.env array, which you can use to set your respective variables.

Step 4: Install dotenv and hdwallet

Inside your project root folder, run the following npm commands:

Acquiring Rinkeby Ether

Visit below URL and follow the instruction about sharing your account details on the social channel (currently it supports G+, Facebook, and Twitter) and using the post details to seek Rinkeby ether:

https://www.rinkeby.io/#faucet

The following screen shows sample options for seeking ether:

I generally select 18.75 Ethers option. However, you can choose whatever option works best for you.

Run migration/deployment script

By now you should be all set to run your migration script. Run the following command:

Ideally, this should deploy your contract in the Rinkeby Testnet. However, sometimes it does give errors. For example, for me, it was giving below error, which indicated that my contract deployment needs more gas than the block gas limit configured in the network:

Setting up the gas limit in Rinkeby network option

The Rinkeby network has a block gas limit of around 7 million. Refer to below URL to know the gas stats: https://www.rinkeby.io/#stats

Of course, setting a gas limit above the block gas limit will not work. However, you can definitely push the boundary. For example, I have kept around 6.7M.

Optimizing your solidity compiler configuration

The following configuration in the truffle.js helped me to reduce the overall gas need significantly:

However, it is still important to understand the gas usage closely and handle it well through design and programming.

A successful deployment

A successful deployment shall give you a detail similar to as shown below:

Verifying the deployed contracts

You can verify your deployed contracts on Rinkeby by visiting the following URL and looking for your contract address: https://www.rinkeby.io/#explorer

For example, in previous section the contract address for MarketPlace contract is 0x9c6ef65f82f569cd27b599a0bb79f604bf931be2.

If you look for this address in Rinkeby then you shall see the following details:

You can see more details by clicking on the transaction hash. Also, on that screen, you have various tools and utilities, which give you options for seeing various aspects, including OP Codes, of this contract.

Testing the deployed contract using Truffle Console

You can either setup test cases using Mocha and Chai or hook your contract with the DAPP to perform a full-fledged testing. However, for the sake of verification of a deployed contract, you can simply launch the truffle console, instantiate the contract and get started with the invocation of the functions as shown below:

When you look at the ABI of the checkAccess function, you will notice the following which matches with the output received in the previous block:

Summary

While it is easier to test your smart contracts using the local blockchains like Ganache, it is always important that you test your contract in an environment which is closer to the production environment. Even though Rinkeby uses proof of authority (PoA) for consensus, it is still closer to the production environment as well as it is immune to attack. So, it does make sense to run your test cases against Rinkeby. Ropsten is closest to the production environment and it uses PoW consensus mechanism. However, it is not immune to the spam attacks. Hence, you must run your final testing against Ropsten. The process for deploying on Ropsten will be similar.

References

Related Posts

Comments (4)

I’m truly enjoying the design and layout of your blog. It’s
a very easy on the eyes which makes it much more pleasant for me to come here and visit
more often.
Fantastic work!

Kolekcje i sztuka

I quite like looking through an article that can make people think.
Also, thanks for allowing for me to comment!

Hi there! I simply want to give you a big thumbs up for the excellent
info you have got here on this post. I am returning to your blog for more soon.

Having read this I believed it was extremely informative.
I appreciate you taking the time and effort to put this information together.

I once again find myself spending way too much time both reading and commenting.
But so what, it was still worthwhile!

Leave a comment

Privacy Preferences
When you visit our website, it may store information through your browser from specific services, usually in form of cookies. Here you can change your privacy preferences. Please note that blocking some types of cookies may impact your experience on our website and the services we offer.