Ethers.js is an alternative to web3.js and is designed to make it easier to communicate with Ethereum Blockchain. Well, Using Keythereum along with Ethers.js helps us to implement wallet function saving private key on the client side which we will see in detail in part 2 of this series which will be released soon.

Blockchain is the solution to the real world problems and Ethers.js is one of our tool to realize it.

We will walk through this in three sections:

Smart Contract Deploying the smart contract to Ethereum Rinkeby Testnet Communicating with the contract from Nodejs

1. Smart Contract

2. Deploying Smart Contract to Ethereum Rinkeby Testnet

To deploy this smart contract we need to have Metamask plugin installed on our google chrome browser and account should be loaded with sufficient ether. Please refer to the below guide, if you are new to installing metamask.

Switch your network to Rinkeby testnet and copy your address. Anyone having a Twitter, Google+ or Facebook account may request funds within the permitted limits from https://faucet.rinkeby.io/.

I am going to use twitter where my account address is 0x7c477A59578710eC7bfD2bf29D7a24F53A33979a.

I will now tweet with this address and copy the tweet link to paste it in faucet.

Copying the tweet to use it in the faucet to load ethers

Paste the tweet link in Rinkeby faucet and request ethers as shown in the picture below.

Load 18.75 Ethers for 3 days

Upon success, you will be able to see 18.75 Ether as balance in your account address in Metamask.

Well, now we have to deploy our smart contract to Ethereum Rinkeby Testnet from online IDE called Remix. Open Remix at: http://remix.ethereum.org

Deploying Smart Contract

Please ensure to use Environment as Injected Web3 and when you press the Deploy button, Metamask popup appears asking you to confirm by submitting the transaction. Once you submit the transaction, you will be able to see the Rinkeby transaction link explorer beneath in the Remix console. Once you click on the link, the transaction can be viewed in the Rinkeby Etherscan as shown below.

When the contract creation is successful, the TxReceiptStatus: will show ‘Success’ and please take note of the ‘To’ address which we will have to use later once we have to communicate with the Blockchain contract from our Nodejs app.

In my case, the To address is 0x74a9a20f67d5499b62255bfa1dca195d06aa4617.

Now from Remix IDE, we need to copy the ABI which is the Application Binary Interface with which is also required to communicate with the smart contract later. In Remix, under the ‘Compile’ tab, click on ‘Details’ where you will see ABI. Click just right to ABI to copy it. You may use a JSON uglifier(http://jsonmate.com/) to uglify the obtained ABI and it will look like this.

3. Communicating with Smart Contract from Nodejs

Now we have the address where our contract lives and also the ABI which are enough for us to communicate with our smart contract from Nodejs. We need to use the module ‘ethers’ to communicate with the contract.

Go to your working directory and type ‘npm init’ from your command line to create the package.json file. Please note that you need to have ‘node’ and ‘npm’ installed in your system.

Now, we can install ethers module by executing ‘npm install — save ethers@3’ which will install ethers node modules in the directory.

‘Please note that at the time of writing this, it was ethers version 3, but now ethers has gone to newer versions and hence when you are installing ethers with npm, you have to use npm install — save ethers@3’.

Now create setvalue.js file in the same directory with the below contents.

‘Please note that at the time of writing this, the ethers version 3 supported directly accessing the infura rinkeby node by calling provider in the above method. However, now infura has changed and you have to create an account with infura and you will get a project id for free. I have created one and a sample of how the new provider initialisation has to be done is given below.

var provider = new ethers.providers.JsonRpcProvider(“https://rinkeby.infura.io/v3/62349edb370e4523b328b8823d211551",“rinkeby”);

where 62349edb370e4523b328b8823d211551 is my project id. Please take care to replace everywhere in the following with this provider. Please do not spam.

This sets the network provider to be Rinkeby testnet. Now we need to add the address where the contract lives and also the ABI which we have obtained from Step 2.

3.1 Setting Hello World

Now our smart contract has two functions, one is to set a value and the other is to get the value. Please note that setting a value is changing the state of the smart contract where a transaction has to take place while getting the value does not invoke state change and is a constant function.

So, to set the value, we have to sign the transaction with the private key of the account from which we are communicating with the smart contract. We can get the private key of the account from the Metamask. Click on ‘Export Private Key’ from Metamask and provide your password.

Exporting Private Key From Metamask

The obtained private key for me is 3ab6468f2465130c51946a5456b8e2d309be7af2f8afcd6823996d281c0990d0.

We have to add prefix 0x to make it hex and hence the private key will be 0x3ab6468f2465130c51946a5456b8e2d309be7af2f8afcd6823996d281c0990d0.

Now we have to have the wallet functionality provided by Ethers.js to sign our transaction and the Contract functionality which initializes the contract to communicate with it as shown below.

Lets now call the setValue function by setting ‘Hello World’ as the parameter.

Now from your working directory, run ‘node setvalue.js’ to call setValue function setting the value to ‘Hello World’.

You will be able to see the transaction as shown below in the console.

3.2 Getting HelloWorld

As I mentioned, getting the value is a constant function which does not change the state of the blockchain, so we dont need to sign he transaction or use wallet feature from Ethers.js. So, instead of wallet as a parameter in contract, we will just use provider which is enough to communicate with the blockchain.

Now we will call the getValue function as shown below.

Now from your working directory, run ‘node callPromise.js’ to get the returned value from the smart contract function getValue().

You will be able to see the returned ‘Hello World’ in your console.

Thank you for reading and feel free to clap aside if you found this worth.