Web3 by Example: Querying a Smart Contract

In this example we’ll see how to call a smart contract function from JavaScript.

After loading the smart contract, we can call a function using instance.

In the SimpleStore contract, we have a function called getValue() that returns the current value. Let's call it.

You will see the result is "Hello, world!", which is the value we set when deploying the contract.


const { ethers } = require("ethers");
const fs = require("fs");
(async () => {
const abi = JSON.parse(fs.readFileSync("storage_sol_SimpleStorage.abi"));
const address = "0x4deA5308A17Bc20589802f3E2C23e79Ba044d497";
const provider = new ethers.providers.AlchemyProvider("goerli");
const instance = new ethers.Contract(address, abi, provider);
const result = await instance.getValue();


➜ node query-contract.js
Hello, world!

Next example: Writing to a Smart Contract