JavaScript theorytheory 0/50 · 0%
Web3 · medium

39. Reading and writing contracts with ethers

Providers, signers and receipts.

A provider reads; a signer writes. Reads are free `eth_call`s; writes cost gas and return a transaction you must await.

const c = new ethers.Contract(addr, abi, signer);
const bal = await c.balanceOf(user);      // read
const tx = await c.stake(amount);          // write
const receipt = await tx.wait(1);

Estimate gas and simulate with `staticCall` first to surface a revert reason before the user pays.

Check your understanding

  1. 1. What does a read call cost?

  2. 2. What does `tx.wait(1)` do?

  3. 3. How do you preview a revert reason?