Ethers.js is lightweight and widely used for interacting with Ethereum-compatible chains.
Install:
npm install ethers
Example: Connect Wallet & Call Contract
import { ethers } from "ethers";
import MyContractABI from "./MyContract.json";
const contractAddress = "0xYourContractAddress";
async function connect() {
if (!window.ethereum) throw new Error("Wallet not found");
await window.ethereum.request({ method: "eth_requestAccounts" });
const provider = new ethers.BrowserProvider(window.ethereum);
const signer = await provider.getSigner();
const contract = new ethers.Contract(contractAddress, MyContractABI.abi, signer);
// Example: read contract data
const value = await contract.myPublicVariable();
console.log("Contract value:", value);
// Example: write transaction
const tx = await contract.setMyPublicVariable(42);
await tx.wait();
console.log("Transaction confirmed!");
}