Deploying a Smart Contract in Rinkeby using Infura (2024)

4 4

By Alok Ranjan Uncategorized September 5, 2018

Deploying a Smart Contract in Rinkeby using Infura (1)

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.

Infuraoffers 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. Itis a hosted Ethereum node cluster that let your users run your application without requiring them to set up their own Ethereum node or wallet.

Thisarticle will talk about steps and considerations for deploying an Ethereum smart contract in Rinkeby Testnet using 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 projectby 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:

Deploying a Smart Contract in Rinkeby using Infura (2)

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.

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 rinkebyoption by adding the following configuration:

Rinkeby Configuration

1

2

3

4

5

6

7

8

9

rinkeby:{

host: "localhost",

provider: function() {

return new HDWalletProvider( mnemonic, "https://rinkeby.infura.io/v3/" + tokenKey);

},

network_id:4

, gas : 6700000

, gasPrice : 10000000000

}

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

Step 2:Create a separate .env file fortokenKey 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. AddNEMONICandENDPOINT_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 anddotenv

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

1

2

3

4

require('dotenv').config();

var HDWalletProvider = require("truffle-hdwallet-provider");

var mnemonic = process.env["NEMONIC"];

var tokenKey = process.env["ENDPOINT_KEY"];

The inclusion of dotenvputs 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:

1

2

$npm install truffle-hdwallet-provider

$npm install dotenv

Visit below URLand 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:

Deploying a Smart Contract in Rinkeby using Infura (3)I generally select 18.75 Ethers option. However, you can choose whatever option works best for you.

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

1

$ truffle migrate --network rinkeby --reset --compile-all

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:

1

2

3

4

5

6

7

Running migration: 5_marketplace.js

Linking Utils to MarketPlace

Deploying MarketPlace...

... 0x57d5e0c632b465ad23d5e2b671cf8612a15013627550ac0f24f9c805fbb93141

Error encountered, bailing. Network state unknown. Review successful transactions manually.

Error: The contract code couldn't be stored, please check your gas amount.

at Object.callback (/usr/local/lib/node_modules/truffle/build/webpack:/~/web3/lib/web3/contract.js:147:1)

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:

1

2

3

4

5

6

solc: {

optimizer: {

enabled: true,

runs: 200

}

}

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 youa detail similar to as shown below:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

$ truffle migrate --network rinkeby --reset --compile-all

Compiling ./contracts/EIP20.sol...

Compiling ./contracts/EIP20DetailedInterface.sol...

Compiling ./contracts/EIP20Interface.sol...

Compiling ./contracts/MarketPlace.sol...

Compiling ./contracts/Migrations.sol...

Compiling ./contracts/Store.sol...

Compiling ./contracts/library/SafeMath.sol...

Compiling ./contracts/library/Utils.sol...

Writing artifacts to ./build/contracts

Using network 'rinkeby'.

Running migration: 1_initial_migration.js

Replacing Migrations...

... 0x861beecf361485aa078cf1111ab7775fa7d5497b3649530fb671e15dd636f159

Migrations: 0x70f564f896f6332ec40281243198299239455d42

Saving successful migration to network...

... 0x73e34224bf325052789ee922de0c48baf06987de283e75335b78fbd8f852d7b1

Saving artifacts...

Running migration: 2_utils.js

Replacing Utils...

... 0x77d22538166a7abbc5cc92c0e7aaf6ec4ee9c3861ebf0fa324c6d07ab8241cd3

Utils: 0x8aba48505fb02b8617a15e09bab1a14d2528781f

Saving successful migration to network...

... 0x46426101892a2aca2a4984a6e39ce4d0ee5e5ece2441e38ce8062f4280a8c39d

Saving artifacts...

Running migration: 3_store.js

Replacing Store...

... 0xf6b2941af133d68cc226547f4dd927cb257a5afa2eb9160e95dc5aa32c426f3f

Store: 0x38767ddd3e99cf4e3e742c9c661899a116dd4e61

Saving successful migration to network...

... 0xce21ac96acd98a043e6b5164fec46fee3de3844b2100658a5c771cc4ddc9b97e

Saving artifacts...

Running migration: 4_EIP20Token.js

Replacing SafeMath...

... 0x868fabc87078ad6ba172f8ca45f5f7c3824d542c98370ab55c744fd14072c566

SafeMath: 0x0ee0de3b98c7828cfde2e5101962836f1dd56a5e

Replacing EIP20...

... 0x46b52bb523f78255e2b8e40617e14c4558c807e7d94b68436bbcb86d53c31c14

EIP20: 0xef0deb8963b2039a58e3cda6cd26c3226db37c6a

Saving successful migration to network...

... 0x81430b2a51c2a3984b446a295c549311fe16e8c47c5675df4b028ca8187539f2

Saving artifacts...

Running migration: 5_marketplace.js

Linking Utils to MarketPlace

Replacing MarketPlace...

... 0x4d885b06e6d95d1d9997589b9b2f1f4ffa6ceda0ef93d5ab05bd69565009f69b

MarketPlace: 0x9c6ef65f82f569cd27b599a0bb79f604bf931be2

Saving successful migration to network...

... 0xde89270b5fc1083782053e23ab7a640ec241aa7b0dee1a5bc499f8747954afb1

Saving artifacts...

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 is0x9c6ef65f82f569cd27b599a0bb79f604bf931be2.

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

Deploying a Smart Contract in Rinkeby using Infura (4)

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.

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:

1

2

3

4

5

6

7

8

9

$ truffle console --network rinkeby

truffle(rinkeby)> MarketPlace.deployed().then(function(instance){app = instance;})

undefined

truffle(rinkeby)> app.address

'0x9c6ef65f82f569cd27b599a0bb79f604bf931be2'

truffle(rinkeby)> app.checkAccess("0xF928ac0Ed0D0E91F51287A5F28dbdB1019fBbA55")

[ false, false, false ]

truffle(rinkeby)> app.checkAccess("0x535f2b296badd3d4064284edde91024c8e9df2cc")

[ true, true, false ]

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:

1

2

3

4

5

6

7

8

{ constant: true,

inputs: [ [Object] ],

name: 'checkAccess',

outputs: [ [Object], [Object], [Object] ],

payable: false,

stateMutability: 'view',

type: 'function'

}

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.

Deploying a Smart Contract in Rinkeby using Infura (2024)

FAQs

How do I deploy smart contract on Rinkeby using Infura? ›

Add Rinkeby Network Options
  1. Step 1: Add Rinkeby network options. In the network options of module.exports object, add rinkeby option by adding the following configuration: ...
  2. Step 2: Create a separate . env file for tokenKey and mnemonic. ...
  3. Step 3: Import truffle-hdwallet-provider and dotenv. ...
  4. Step 4: Install dotenv and hdwallet.
May 3, 2022

How do I deploy a smart contract on Infura? ›

Steps
  1. Install Truffle.
  2. Fund your Ethereum account.
  3. Create a project directory.
  4. Install Dotenv.
  5. Create a Truffle project.
  6. Install hdwallet-provider.
  7. Create the . env file.
  8. Create a smart contract.

How do I interact with my smart contract on Rinkeby? ›

How to deploy your smart contract to Rinkeby
  1. Deploy to Rinkeby step-by-step.
  2. Create app in Alchemy.
  3. How to receive Rinkeby Eth tokens.
  4. Configure Hardhat network.
  5. Configure Hardhat to use environment variables.
  6. Create and run deploy script.
  7. Conclusion.
Jan 4, 2022

How do you use Infura? ›

First off, the app server configures the Infura Ethereum node endpoint as a HttpProvider. // Configure the connection to an Ethereum node export const web3 = new Web3( new Web3.
...
Getting your current ETH Balance
  1. Use web3. ...
  2. Convert that value from Wei to ETH using web3. ...
  3. Return your wallet balance denominated in ETH.
Sep 21, 2022

How do I send an Infura transaction? ›

  1. Deploy a contract using web3.js.
  2. Deploy a contract using Truffle.
  3. Call a contract.
  4. Monitor transfers using Python.
  5. Track ERC-721 and ERC-1155 token transfers.
  6. Retrieve and display ERC-721 and ERC-1155 tokens.
  7. Track ERC-20 token transfers.
  8. Retrieve the balance of an ERC-20 token.

Does MetaMask use Infura? ›

When someone makes a blockchain transaction via their MetaMask wallet, it defaults to Infura, which broadcasts the transaction to the Ethereum blockchain. MetaMask connects to Infura through what's called a remote call procedure service (RPC).

Can you deploy a smart contract without compiling? ›

Deploying a contract also costs ether (ETH), so you should be familiar with gas and fees on Ethereum. Finally, you'll need to compile your contract before deploying it, so make sure you've read about compiling smart contracts.

How do I process and deploy a smart contract? ›

Create and Deploy your Smart Contract
  1. Step 1: Connect to the Ethereum network. ...
  2. Step 2: Create your app (and API key) ...
  3. Step 3: Create an Ethereum account (address) ...
  4. Step 4: Add ether from a Faucet. ...
  5. Step 5: Check your Balance. ...
  6. Step 6: Initialize our project. ...
  7. Step 7: Download Hardhat. ...
  8. Step 8: Create Hardhat project.

How much does it take to deploy a smart contract? ›

The cost of deploying and launching a smart contract on Ethereum is likely to vary based on the complexity of the contract. However, the average cost per transaction is $0.0015 – $0.0025 depending on the gas price. This means that for every 10,000 transactions, you can expect to spend around $150 – $300 in fees.

How do you manually interact with a smart contract? ›

Interact with deployed smart contracts
  1. Perform a read operation. To perform a read operation, you need the address that the contract was deployed to and the contract's ABI. ...
  2. Perform a write operation. To perform a write operation, send a transaction to update the stored value. ...
  3. Verify an updated value.
Aug 8, 2022

How do I deploy a smart contract in Testnet? ›

Deploying a smart contract, even in Testnet, requires payment for transactions in tokens which are in circulation inside the ecosystem. In Polygon, these tokens are represented by “MATIC”. To add Matics in the Testnet network, just visit this website and paste a wallet address in an input, then submit the form.

How do I connect to the Rinkeby test network? ›

Rinkeby. The Rinkeby Faucet requires that you make a social media post including your address on either Facebook, Twitter or Google Plus. Once you do so, put the direct link to that post in the input box and select 18.75 Ethers / 3 days from the dropdown on the right.

Why should I use Infura? ›

Infura provides the tools and infrastructure that allow developers to easily take their blockchain application from testing to scaled deployment - with simple, reliable access to Ethereum and IPFS. Why should I use Infura? There are many pain points for blockchain developers that can be solved by Infura.

How much of Ethereum runs on Infura? ›

Think of it as Amazon Web Services for Ethereum and other blockchains, like Polygon and Near. Lubin is a co-founder of Ethereum. At times, more than 50% of transactions on the Ethereum network ran through Infura, according to Eleazar Galano, the company's co-founder.

How do I set up Infura? ›

  1. Sign up to Infura.
  2. Create a project.
  3. Secure your project.
  4. Send requests. 4.1 Get the current block number. 4.2 View the Ether balance of a specified contract.
  5. Use the Infura dashboard.
  6. Manage your account.

Is Infura reliable? ›

Like Alchemy, Infura provides a simple and reliable infrastructure for your Ethereum projects. But they also took this one step further with their IPFS API, a decentralized protocol for storing and accessing files on the blockchain.

Is Infura an RPC? ›

The Infura Protocol was launched by E.G. Galano in 2016 with the objective of becoming the most comprehensive toolsuite for Web3 developers in the industry. Primarily, Infura provides developers with reliable RPC nodes. Using Infura, developers can transmit data between their applications and the blockchain itself.

What client does Infura use? ›

The most well-known portion of the Infura infrastructure is the network of hosted Ethereum clients that supports mainnet and testnets via client-compatible JSON-RPC, over HTTPS and WSS.

How do you deploy a token on a Rinkeby? ›

Steps to add Rinkeby network details in your project
  1. First we will create a '.env' file in the Project1 folder. i. Create your .env file. ...
  2. Inside the hardhat. config. js file, add rinkeby in the network section as follow:
  3. Go to deploy. js file, in line 17 replace the Greeter word with your token name(mine is GaneshaToken).
Oct 11, 2021

How do you deploy a smart contract to Rinkeby using truffle? ›

Checklist for deploying a contract with truffle
  1. You tested all of your functions in your contract locally with truffle and all of the tests pass successfully.
  2. You have the Migrations. ...
  3. You have a migrations file for Migrations.sol.
  4. You have a migrations file for your smart contract.

How do you deploy smart contract to ETH Testnet? ›

How to deploy a smart contract on ethereum testnet?
  1. Write a contract. ...
  2. The testnet I'm using here is Roptsen. ...
  3. Using a wallet to deploy the contract. ...
  4. Open the contract tab, click deploy button, you can fill the form to submit your contract to the testnet.

Top Articles
Latest Posts
Article information

Author: Jamar Nader

Last Updated:

Views: 5923

Rating: 4.4 / 5 (55 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Jamar Nader

Birthday: 1995-02-28

Address: Apt. 536 6162 Reichel Greens, Port Zackaryside, CT 22682-9804

Phone: +9958384818317

Job: IT Representative

Hobby: Scrapbooking, Hiking, Hunting, Kite flying, Blacksmithing, Video gaming, Foraging

Introduction: My name is Jamar Nader, I am a fine, shiny, colorful, bright, nice, perfect, curious person who loves writing and wants to share my knowledge and understanding with you.