Providers and Signers in Hardhat Smart Contract Development

Takshil Patil
Coinmonks

--

Why I wrote this blog? When starting with smart contract testing, I found it very confusing to understand signers while coding.

In this article I will use Hardhat with Ether.js to deploy and interact with blockchain.

Signers vs Providers

Signers = Wallet Accounts and we get a signers from a providers.

Introduction to Signers

  • In-short, signer = Wallet Account. A Signer in Ethers.js is an object that represents an Ethereum account.
  • To understand what is “Wallet Account” or “Ethereum Account”, we can simply related to what we already know.
  • If you have used metamask, for the fist time you need to create your “wallet”. Once your wallet is created, you get a wallet unique account from metamsk.
  • This account has no meaning if we cannot link it with a “Ethereum Blockchain”, because you always query “Balance of my account in Goerli Testnet” we dont simply query “Balance of my account”.
  • Here goerli testnet is the ethereum blockchain. This can be directly linked to “provider”.
  • If we know the private key of an account, it means that we are the owner of the account.

What can a signer do?

  • Whatever we can do using a ethereum wallet / account.
  • Using ethereum account we can send “signed” messages.

How many signers can you generate?

  • We dont “generate” new signers. These are simply ethereum account present in the blockchain, similar to the metamask account we need to create during our first installation.
  • We can generate as many signers as many private keys we have.
  • In production, we may have couple of wallet accounts with us. But when development of a smart contract we will need more to test functionality such that we dont have to go through the pain of metamask to generate a new account on-demand.
  • For dev using Hardhat framework, we can make use of import {ethers} from “Hardhat”; to create an instance of Hardhat-Ethers library. Then we can generate around 20 such “on-demand” addresses on “test blockchain that are local and created just for testing purposes”. We can generate using ethers.getAccount()

There are 3 ways of generating signers

  1. (Production) Using Wallet - given you already have an ethereum account / wallet account.
  2. (Development) Using ethers.js ethers.getSigners() - generates exactly 20 accounts. Reference
  3. (Development) Using ethers.js ethers.Wallet.createRandom() - if you need more, this will keep on generating new accounts one by one. Reference.

hardhat-ethers vs ethers.js

  • Hardhat-ethers = ethers.js + additional hardhat helpers
  • We generally use hardhat-ethers, instead of ethers.js in hardhat development for ease of development.
  • To get a complete list of helpers present in hardhat-ethers visit Reference.

Providers

  • We connect to a blockchain via a node of a blockchain. Providers are used to connect to these blockchain node. There are two types providers JsonRpcProvider() and Web3Provider()

JsonRpcProvider() vs Web3Provider()

  • JsonRpcProvider() : It is used when we are directly connecting to the blockchain using its RPC URL.
  • Web3Provider() : It is used when we are not connecting to the blockchain using RPC URL. We are using 3rd party tools like metamask, which then connects to the blockchain using RPC. While development of dapp, we need to specify which EVM chain we need to connect via metamask or other wallets.
  • We can have multiple signers in both JsonRpcProvider() approach, given we know the private key, but generally, we use only 1 signer for 1 transaction for a dapp.
  1. Using Web3Provider(), When we want to do transactions via metamask.
web3provider

2. Using JsonRpcProvider(), When we directly want to programmatically want to access the blockchain directly using private key of an account, we use this. This will not show any window/popup to user, as the transaction does not require to connect to user wallet. This is used, when we dont want metamask wallet for our payment.

jsonrpcprovider

New to trading? Try crypto trading bots or copy trading

--

--