• Stars
    star
    377
  • Rank 113,535 (Top 3 %)
  • Language
    JavaScript
  • Created over 5 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A basic cheatsheet of Web3.js vs Ethers (along w/ example apps!)

Web3.js vs Ethers.js

A guide to the basic differences between Web3.js and Ethers.js, the two most popular libraries for interacting with the Ethereum blockchain. And two example frontend apps using React + Hooks!


Sample Dapp Contract

Inside the smart-contracts folder, you will find a simple Truffle project with the following Solidity contract:

pragma solidity ^0.5.0;

contract Counter {
  uint count = 0;

  function increment() public {
    count = count + 1;
  }

  function getCount() public view returns (uint) {
    return count;
  }
}

Setup Truffle project

Before you run any of the frontend UIs, make sure to start the development console with truffle develop, and then run the migrate command to compile and deploy the contract onto the development chain.

Two Frontend UIs

There are two folders (app-ethers and app-web3js) each containing a simple React frontend for the above contract. The only substantial difference between these two UIs is located in the useCounterContract.js files.

Here are the direct links for your convenience:

Running the apps

In each of these apps, you can serve the frontends with the following commands:

npm install
npm start

This will serve the frontend on http://localhost:1234 which you can view in your browser.

Differences

There are three major portions in this code: the setup, reading (calling a constant method), and writing (calling a non-constant mutating method).

Setup

With Web3.js, we need the following to instantiate a connected contract instance that can make read/write calls:

  • contract ABI
  • deployed contract address
  • a from address (for send transactions)

Note that the networkId is required for us to fetch the deployed address from our contract artifact.

// Web3.js
const web3 = new Web3("http://127.0.0.1:8545");
const accounts = await web3.eth.getAccounts();
const networkId = await web3.eth.net.getId();
const contractAddress = artifact.networks[networkId].address;

contractInstance = new web3.eth.Contract(artifact.abi, contractAddress, {
  from: accounts[0],
});

With Ethers.js, we need the following for our contract instance:

  • deployed contract address
  • contract ABI
  • a Signer object (similar to Provider, but with a specified Signer)
// Ethers.js
const provider = new ethers.providers.JsonRpcProvider();
const network = await provider.getNetwork();
const contractAddress = artifact.networks[network.chainId].address;

contractInstance = new ethers.Contract(
  contractAddress,
  artifact.abi,
  provider.getSigner(),
);

Calling a constant method

// Web3.js
const count = await contractInstance.methods.getCount().call();
console.log(count); // returns a String
// Ethers.js
const count = await contractInstance.getCount();
console.log(count); // returns a BigNumber instance

These two are very similar, but in our example Ethers.js returns a BigNumber instance by default whereas Web3.js will return the number as a String.

Calling a non-constant method

// Web3.js
await contract.current.methods.increment().send();
// tx has been mined
// Ethers.js
const tx = await contract.current.increment();
await tx.wait(); // wait for mining

Note that Web3.js will return a PromiEvent which allows you to subscribe to confirmations, errors, and the transaction hash.

Ethers.js will return a transaction object where a bunch of information relating to the transaction is kept. You can grab the hash via tx.hash, but you must await on tx.wait() if you want to make sure it has been mined.

More Repositories

1

eth95

🛠️ A smart contract UI for your Ethereum dapp project
TypeScript
527
star
2

react-rotating-text

📝 A simple react component to display an array of words/sentences with a typewriter effect.
JavaScript
223
star
3

truffle-next

🛰️ A boilerplate Truffle Box project with Next.js for rapid Ethereum Dapp development
JavaScript
194
star
4

react-reveal-text

✨ A small react library for animating the revealing of text.
JavaScript
186
star
5

dapp-boilerplate

⚡A minimal types-first boilerplate for Ethereum frontend dapp development.
TypeScript
112
star
6

omg-counters

😍 Increment decrement counters using various frontend frameworks.
48
star
7

truffle-react

⚛️ A boilerplate Truffle Box project with Create React App for rapid Ethereum Dapp development
JavaScript
46
star
8

firebase-nextjs-example

An example app for using firebase with Next.js
JavaScript
19
star
9

js-state-encapsulation

Examples of state encapsulation in Javascript
JavaScript
19
star
10

ganache-jest-example

🚀 Minimal Solidity contract testing with Ganache and Jest
JavaScript
15
star
11

next-boilerplate

📐 A modern universal boilerplate for React applications using Next.js.
JavaScript
14
star
12

styled-mui-next

An example application of using Styled-Components and Next.js with Material-UI
JavaScript
13
star
13

drizzle-react-from-scratch

Trying to learn Drizzle by making projects with it from scratch
JavaScript
9
star
14

react-build-dist

A simple utility for compiling your React components to standalone modules.
JavaScript
9
star
15

rehydrate-workshop

A small collection of basic components built with Rehydrate 💧.
OCaml
7
star
16

simple-js-blockchain

🔗 A simple JS blockchain using only Flow types and functions
JavaScript
6
star
17

ethbtc-example

JavaScript
5
star
18

with-redux-combineReducers

JavaScript
3
star
19

react-build-lib

A simple utility for compiling your React components to CommonJS modules.
JavaScript
3
star
20

nextjs-symlink-bug

JavaScript
3
star
21

olis

A collaborative chat platform for the web built with Meteor and React
JavaScript
3
star
22

hodlbuddy

A script for me to see how much my cryptocurrency portfolio is worth
JavaScript
2
star
23

nouns.run

TypeScript
2
star
24

zenith

🌌 A better API wallet for ZenCash
JavaScript
2
star
25

meta-auth-example

An example usage of MetaAuth with a React frontend and an Express backend
JavaScript
2
star
26

create-react-app-brunch

Create simple configurable React apps with a Brunch-based CLI.
JavaScript
2
star
27

simple-express-jest

A simple ExpressJS and Jest starter project for absolute beginners
JavaScript
2
star
28

learn-more-cs

A simple project for us to track and manage our learning of certain computer science principles.
2
star
29

dev-react-component

A minimalistic boilerplate for publishing a React component to NPM.
JavaScript
2
star
30

twilio-sms-verification-example

A minimalistic example of SMS verification with Twilio using a React app and a Node server.
JavaScript
1
star
31

web3-CI-bug

1
star
32

simple-zap

An attempt to demonstrate a simple zap contract
Solidity
1
star
33

hangtight.club

🎟️ A queue ticketing system for Nounish events!
Solidity
1
star
34

consensys-rxjs-talk

Example project for my RxJS talk w/ ConsenSys (along w/ a Drizzle-Utils demo)
JavaScript
1
star
35

practice-typing

A simple Angular 2 app to help people practice typing phrases on their keyboard.
TypeScript
1
star
36

track-page-requests

A node.js script for tracking page requests
JavaScript
1
star
37

repro-ganache-bug

JavaScript
1
star
38

defi-experiments

TypeScript
1
star
39

web3-CI-bug-npm

1
star
40

cruise-reviews

JavaScript
1
star
41

lookintodefi

1
star
42

hackernews-node

TypeScript
1
star
43

bc-parks

JavaScript
1
star
44

repro-eth-block-tracker-bug

JavaScript
1
star
45

non-overwhelming-zkp

1
star
46

decoder-import-bug

JavaScript
1
star
47

buidler-deploy-repro

A reproduction repo of buidler deploy not showing contract addresses for a local deploy
JavaScript
1
star