How to Use a Contract in Ethers.js

This guide will introduce you to use a Contract in Ethers.js and level up your Web3 stack!

What is a Contract in Ethers.js?

A Contract is an abstraction to a specific contract on the blockchain. It can be used as a JavaScript/TypeScript object that allows developers to read, write and deploy contracts to and from the blockchain.

Ethers.js Contract Use Cases

The following consists of potential use cases with Contract within Ethers.js:

  • Creating Instances - use contract.attach to retrieve a new instance of a contract associated with an address. This allows users to interact with multiple identical contracts that inherit from each other and repeat actions.
  • Properties - use contract.address to retrieve the contract or ensName that created the contract to confirm with the user the validity or security of a specific contract.
  • Events - use contract.queryFilter to retrieve events that match a specific event a user wants to conduct.

There are many more use cases that can be explored by visiting the Contract docs, here.

How to Use a Contract with the Alchemy SDK

This example will teach you how to deploy a contract using the Alchemy SDK. Please Note: The following code snippets require prior installation of the alchemy-SDK.

1. Import Alchemy, Network, Wallet and ContractFactory via alchemy-sdk

javascript
1const { Network, Alchemy, Wallet, ContractFactory } = require("alchemy-sdk");

2. Optional Config object, but defaults to demo api-key and eth-mainnet.

javascript
1const settings = {
2 apiKey: "demo", // Replace with your Alchemy API Key.
3 network: Network.ETH_MAINNET, // Replace with your network.
4};
5
6const alchemy = new Alchemy(settings);

3. Insert the contract abi and bytecode of the smart contract.

  • 1**[Application Binary Interface (ABI)](https://www.alchemy.com/overviews/what-is-an-abi-of-a-smart-contract-examples-and-usage)**: The ability to community and interact with external applications and other smart contracts.
  • 1**[Bytecode](https://docs.alchemy.com/docs/how-to-interpret-binaries-in-solidity)**: The raw data published via a smart contract to the Ethereum blockchain.
javascript
1const abi = [{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"}]; // Replace with your Smart Contract’s ABI.
2
3const bytecode = "...";

4. Locate the private key of your wallet.

javascript
1let privateKey = "...";

5. Setup the wallet used to deploy the contract.

javascript
1let wallet = new Wallet(privateKey, alchemy);

6. Create an instance of a Contract Factory.

javascript
1async function deployContract() {
2
3 // Create an instance of a Contract Factory
4 let factory = new ContractFactory(abi, bytecode, wallet);
5
6};

7. Create a constructor that will hold “Hello World” as the parameter.

javascript
1async function deployContract() {
2 // ...
3 let contract = await factory.deploy("Hello World");
4};

8. Display the contract address and the transaction hash of the contract deployed.

javascript
1async function deployContract() {
2 // ...
3 console.log(contract.address);
4 console.log(contract.deployTransaction.hash);
5
6 await contract.deployed()
7};