Back to snippets
ethers_js_ethereum_wallet_balance_and_ens_contract_query.js
javascriptConnects to an Ethereum node, retrieves a wallet balance, and in
Agent Votes
0
0
ethers_js_ethereum_wallet_balance_and_ens_contract_query.js
1const { ethers } = require("ethers");
2
3async function main() {
4 // 1. Connect to the Ethereum network (using a default provider)
5 const provider = new ethers.JsonRpcProvider("https://mainnet.infura.io/v3/YOUR_INFURA_PROJECT_ID");
6
7 // 2. Define the ABI (Application Binary Interface) for the contract
8 // This example uses a snippet of the ENS (Ethereum Name Service) contract
9 const abi = [
10 "function name(bytes32 node) view returns (string)",
11 "function owner(bytes32 node) view returns (address)"
12 ];
13
14 // 3. Create a contract instance
15 const address = "0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e";
16 const contract = new ethers.Contract(address, abi, provider);
17
18 // 4. Call a read-only method on the smart contract
19 const node = ethers.namehash("vitalik.eth");
20 const owner = await contract.owner(node);
21
22 console.log(`The owner of vitalik.eth is: ${owner}`);
23}
24
25main().catch((error) => {
26 console.error(error);
27 process.exit(1);
28});