Intro to Ether.js

ASHDEEP SINGH - Sep 1 - - Dev Community

Hello World

So this week was spent making a basic ether.js project. In this project we will be using the famous (as you might have guessed it) javascript based ether.js library for interacting with our contract.

Before we move on let us know why use this library instead of geth. So using geth also enables us to interact with the smart contract deployed on blockchain but it does so making our system a node. If we do not want to make our system a node, use ether.js as it will act as a bridge between local machine and smart contract.

Now let's get right into it.
Clone repo from here

To check if one can make connection do run blockchainConnection.js and check if it is printed in console.

If you look at blockchainConnection.js you'll notice following major things.

  • ethers.JsonRpcProvider: This creates a connection to the Ethereum network using the provided RPC URL.
  • getBalance(address): Fetches the balance of the given Ethereum address in wei.

If everything is set up correctly this will fetch balance of some block we randomly found on clankapp.com.

Now we will proceed with our setup.

First deploy some contract on remix and connect it with MetaMask here Storage.sol , then get it's abi and put it in abi.json and finally get metamask private key and place it into .env file.

Once done get contract deployment address and use it in files where we wish to read and write contract.
From here what we do depends upon if we wish to read or write the contract.
For reading, process will have following components.

  • Provider: Connects to the Sepolia testnet using the public RPC URL.
  • Contract Address: The Ethereum address where your smart contract is deployed.
  • ABI: The ABI defines the interface for your smart contract, specifying what functions you can call and their signatures.
  • Contract Instance: An instance of the contract that allows interaction with it using the defined ABI.
  • Function Call: Calls the retrieve function on the smart contract and logs the result. Once done with the file run it using
node filename
Enter fullscreen mode Exit fullscreen mode

( here scIntegration )

For writing, file will have following components.

  • Provider: Connects to the Sepolia testnet.
  • Contract Address: The Ethereum address where the smart contract is deployed.
  • Private Key: Your wallet's private key, used to sign transactions.
  • Wallet: An instance that connects your wallet to the provider.
  • Contract Instance: An instance of the contract that allows you to interact with it using your wallet to sign transactions.
  • Function Call: Calls the store function on the contract and logs a success message if the transaction is successful. Once done with the file run it using
node filename
Enter fullscreen mode Exit fullscreen mode

( here writeSC )

Wait for some 40-50 seconds, Now run again the read-Code file to know latest changes in contract.

My 2 cents :

  • Install ethers and not ether library.
  • Do not publish .env file to github. Last time I checked there were bots lingering in github which would check if there was sensitive Info available on some repo, thus exploituing it to the fullest. (we will be using chainlink in future to tackle this issue)
  • You will always get balance in wei so not forget to convert it into ether. To do so use following snippet (you didn't think of doing it manually, did you ? )
// Convert balance from wei to ether
    const balanceEther = ethers.formatEther(balanceWei);
Enter fullscreen mode Exit fullscreen mode

That's all folks.
Stay tuned for more....

. . . . . . . . . . . . .
Terabox Video Player