Ether.js Web 3.0 Development Guide

Aug 9, 2024

Ether.js Crash Course Notes

Introduction to Ether.js

  • Ether.js: A rapidly growing library for building Web 3.0 applications.
  • Preferred by developers for its elegance and speed in creating JavaScript-based blockchain applications.

Overview of Ether.js Usage

  • Target Audience: Suitable for both advanced and beginner developers.
  • Goal: To provide knowledge for building blockchain applications effectively.

Web Architecture Overview

Traditional Web (Web 2.0)

  • Users interact through web browsers (Chrome, Safari, etc.) with a central server.
  • Frontend is developed using HTML, CSS, and JavaScript; backend is stored on a central server.

Decentralized Web (Web 3.0)

  • Users interact with decentralized applications (dApps).
  • Instead of a central server, the application communicates directly with a blockchain.
  • Smart contracts act as the backend, with data stored on the blockchain itself.

Connecting to the Blockchain with Ether.js

  • Ether.js serves as a bridge between client applications (websites, scripts, etc.) and the blockchain.
  • Allows interaction with smart contracts and fetching information from the blockchain.

Setting Up a Project with Ether.js

Example Setup

  1. Clone the Repository: git clone <repository-url>
  2. Install Packages: Run npm install in the cloned directory.
  3. Text Editor: Open the project using any text editor (e.g., Sublime Text).
  4. Example Scripts: Various scripts will demonstrate using Ether.js.

First Example: Fetching Account Balance

Steps

  1. Import Ether.js:
    const ethers = require('ethers');
    
  2. Connect to Ethereum Node: Use a provider such as Infura.
    const provider = new ethers.providers.JsonRpcProvider('<infura-url>');
    
  3. Fetch Balance:
    const balance = await provider.getBalance('<ethereum-address>');
    console.log(ethers.utils.formatEther(balance));
    
  4. Async Function: Wrap in an async function to handle waits.

Interacting with Smart Contracts

Smart Contracts Overview

  • Smart contracts are programs on the blockchain that can be interacted with.
  • Example: Reading the DAI stablecoin contract using its ABI.

Steps to Interact with Smart Contracts

  1. Setup Contract:
    const contract = new ethers.Contract(<contract-address>, <abi>, provider);
    
  2. Call Functions: Use functions like contract.name() or contract.symbol() to fetch data.

Sending Transactions

Basic Ether Transactions

  1. Create Wallet: Use a private key to create a wallet instance.
    const wallet = new ethers.Wallet(<private-key>, provider);
    
  2. Send Transaction:
    const tx = await wallet.sendTransaction({ to: <receiver-address>, value: ethers.utils.parseEther('0.025') });
    
  3. Transaction Receipt: Await for the transaction to be mined and log the receipt.

Writing to Smart Contracts

Sending Tokens Example

  1. Setup Contract for Tokens:
    const tokenContract = new ethers.Contract(<token-address>, <erc20-abi>, wallet);
    
  2. Transfer Tokens:
    const tx = await tokenContract.transfer(<receiver-address>, ethers.utils.parseUnits('10', 18));
    

Listening for Smart Contract Events

  1. Query Contract Events:
    const filter = tokenContract.filters.Transfer();
    const events = await tokenContract.queryFilter(filter);
    console.log(events);
    

Inspecting Blockchain Blocks

  1. Get Latest Block:
    const blockNumber = await provider.getBlockNumber();
    const block = await provider.getBlock(blockNumber);
    console.log(block);
    

Conclusion

  • Ether.js allows developers to build decentralized applications efficiently.
  • Skills gained from this course can be applied to create real-world blockchain applications.