DEV Community

geraldarzy
geraldarzy

Posted on

Intro to Blockchain: Ethers JS Contract Instances

What is Ethers JS? Ethers JS is a Javascript library that serves to allow your webapp interact with the Ethereum Blockchain. Its competitor library is Web3 JS but here we will be talking about Ethers.

To get started, lets get familiar with the top 3 things you will need to properly communicate with a smart contract. First you will need a Provider, second a Signer, and lastly, a Contract.

What are these things? Lets break it down.

A Provider is what actually connects us the nodes that are in the blockchain. With Ethers JS we would do this by
new ethers.providers.Web3Provider(INSERT_PROVIDER_HERE).
This is creating a new instance of a provider with Ethers JS.

A Signer is actually just an abstraction of the users wallet address. This is what actually allows us to send over a signed transaction by use of our wallets address. And to do this you simply grab the provider you just made, and call provider.getSigner() on it. This will return a new Signer instance that you can use to send over signed transactions.

A contract instance is the most important part. To create a new instance of a contract, you need to pass in a couple things. First pass in the address of where the contract is deployed, whether it be locally, on a testnet, or the actual mainnet. Second we need to pass in the contracts ABI. And lastly we need to either pass in a provider or a signer.

What can we do with a contract instance? With a contract instance, we can call all the functions that are stored on the smart contract. We can communicate with the smart contract anyway we want. Up next is should we pass in a provider or a signer? If you pass in a provider to this contract instance, that instance will only be able to perform read-only functions. Whereas if you pass in a signer, that instance will be able to run both read and write functions.
new ethers.Contract(ADDRESS, ABI, SIGNER OR PROVIDER)

Top comments (0)