Ethereum — Fund And Deploy Smart Contract To RinkeBy Test Network

By Munish Kohli on ALTCOIN MAGAZINE

Munish Kohli
The Dark Side
Published in
5 min readAug 5, 2019

--

In the previous 3 part article, we touched upon numerous properties of wallets, types of wallets, MetaMask, Private and Public keys. MetaMask can store multiple accounts and multiple private keys can be generated from a seed key.

In this article, I will further explore MetaMask and show some of its properties. I will also explain how to fund local Blockchain and test Networks like Rinkeby with Ethers. We will also deploy SC to Ethereum TestNet Rinkeby. With RinkeBy at your disposal, there is no need to install full Ethereum node on your computer.

MetaMask — Connect to TestNet/Ganache

MetaMask, as we know, is not only used for managing Ethereum but also injects Web3 into a browser so that DApps can be built. MetaMask can be connected to various networks as shown in below picture.

Let’s first connect to Localhost 8545. Before making a connection, you should have local (TestNet/Ganache) blockchain running in your local machine. This version of MetaMask does not automatically import accounts. One would have to manually import. On successful connection with Localhost click on the round colored circle marked as Click in below picture. Next, click on the Import Account option.

Import Account click will present below picture. Copy and paste one of the private keys from the 10 given accounts. Click on Import will create a new account and also populate with 100 Ethers. Remember these are fake Ethers/Money.

On successful import, you should see something like the below picture. Voila

MetaMask — Connect to Rinkeby

Rinkeby is another Test network, but not residing local on your machine. It’s a gateway to Ethereum blockchain. Rinkeby is hosted by a company INFURA. There is no need to download full blockchain on your local machine which can run in Gigas. Unlike private blockchain ganache, Rinkeby accounts do not provide default Ethers. You will have to fund them. Here are the steps.

Change to Rinkeby Test Network from Localhost 8545. Below screen appears. Click Deposit button.

Let us fund RinkeBy account using “Test Faucet” option. Click on the link which will take you to the site through which your public account on RinkeBy can be funded through 3 social media Twitter, Facebook or Google Plus. Site looks like below picture. Once steps are completed you should see 18.75 Ethers in RinkeBy account.

Congrats for completing above the first step in setting up RinkeBy account. Now with Ethers in your account, you don’t have to worry about Gas during the transactions. The second step is to sign up for service in INFURA. On signing, tokens are provided for the different below Ethereum Networks.

Main Ethereum Network: https://mainnet.infura.io/<your-token>

Test Ethereum Network (Rinkeby): https://rinkeby.infura.io/<your-token>

Test Ethereum Network (Ropsten): https://ropsten.infura.io/<your-token>

Test Ethereum Network (Kovan): https://kovan.infura.io/<your-token>

One of the main differences between deploying to a local test network and Rinkeby public test network is provider type. When communicating with Rinkeyby networks, we will use Truffle HD Wallet provider.

I explained the HD Wallet in one of my previous articles.

HD Wallet provider will communicate with the Rinkeby test network through an infura API. Local test network has Ganache as the provider which automatically provides private/public keys and fake ethers with unlocked accounts. In the case of HD provider, MetaMask will be the source of accounts, but they will need to be explicitly unlocked using account mnemonic. I explained the account mnemonic in the previous article.

Install HD Wallet through the command npm install — save truffle-hdwallet-provider

Store following set of lines in file deployToRinkeBy.Js. Run from command prompt by running node deployToRinkeBy.js

const HDWProvider = require(‘truffle-hdwallet-provider’);

//To capture Interface and Bytecode from compile.js

const { interface, bytecode } = require(‘../compile’);

require(‘events’).EventEmitter.prototype._maxListeners = 100;

//Web3 is a constructor
const Web3 = require(‘web3’);

//Using above constructor we create instance of web3

const provider = new HDWProvider (‘thumb want head control setup pen civil chapter candy then trade win’,’https://rinkeby.infura.io/v3/080f2745f9264e32a84bb3f5d5dca73f');

//Now we have unlocked the account in MetaMask for the Ether source. We can deploy our SC
const web3 = new Web3(provider); //This web3 instance willl be used to deploy the contract

const deploySC = async () => {

try {
accounts = await web3.eth.getAccounts();
console.log(‘Total number of accounts provided ‘+accounts.length);
//console.log(‘List of Accounts :’);
//console.log(accounts);
console.log(‘accounts 0 is ‘+accounts[0]);

const deployContract = await new web3.eth.Contract(JSON.parse(interface))
.deploy({data: ‘0x’+ bytecode, arguments: [2000]})
.send({ from: accounts[0], gas:5000000});
console.log(‘deployment done’);
} catch (error) {
console.error(error);
}
}
deploySC();

Leave comments/clap in case you like this article.

--

--

Munish Kohli
The Dark Side

Technology Enthusiast | Business & Data Analyst | Machine Learning | Big Data | Blockchain | IBM Hyperledger | Ethereum Smart Contracts