JavaScript & TypeScript for dApps

Wallet providers, ethers.js contract objects, BigInt maths and typed async transaction handling — the code behind the staking lab.

11 min read·5 quiz questions

The browser provider model

Wallet extensions inject a provider object into the page. EVM wallets follow EIP-1193 with window.ethereum and a request({ method, params }) interface; Solana wallets expose window.solana, and Bitcoin wallets their own namespaces. Always feature-detect instead of assuming a wallet is installed.

  • eth_requestAccounts prompts the connection.
  • Listen to accountsChanged and chainChanged and re-read state.
  • Multiple extensions can compete for window.ethereum.

Providers, signers and contracts

In ethers.js a Provider reads the chain and a Signer authorises writes. new Contract(address, abi, providerOrSigner) returns an object whose methods mirror the ABI: read calls resolve immediately, write calls return a transaction you await for a receipt.

  • Read-only UI needs only a provider.
  • Pass a signer for any state-changing call.
  • provider.getBalance is how the lab checks the reward pool.

BigInt and decimals

Token amounts are integers of the smallest unit, far beyond Number's safe range, so ethers returns BigInt. Never mix BigInt and Number in arithmetic. Convert at the display boundary with formatEther/formatUnits and parse user input with parseEther/parseUnits.

Async lifecycle and TypeScript

A write is two steps: send (user signs) then wait (mined). Reflect both in the UI and handle rejection (ACTION_REJECTED) separately from revert. TypeScript helps by typing provider access, narrowing unknown errors before reading .message, and keeping ABI-derived types honest.

  • const tx = await contract.stake({ value }); await tx.wait();
  • Disable the button between send and confirmation.
  • Surface the tx hash so students can open the explorer.

Key terms

EIP-1193
Standard JavaScript provider interface implemented by EVM wallets.
Signer
ethers.js object that signs transactions on behalf of an account.
BigInt
JS integer type used for token amounts beyond Number's safe range.
Receipt
Result object available once a transaction is mined.
ACTION_REJECTED
ethers error code for a user declining the wallet prompt.

Chapter quiz

5 questions · pass mark 75%
  1. 1. Which object does an EVM browser wallet inject?

  2. 2. To call a state-changing contract function you need…

  3. 3. Why are token amounts handled as BigInt?

  4. 4. await tx.wait() does what?

  5. 5. A user closes the wallet popup without signing. Your code should…

Answer every question to submit. Progress for js-ts-for-dapps is saved in this browser.