Skip to main content

Query a Sol Contract

info

For this section we assume the contract has already been deployed in Hedera. If you want to learn how to deploy a contract in Hedera see the previous section or check out our FreeTradeCoffee demo application.

Query a Contract

For this example we will use web3.js on the Greeter example.

Generate Abi

Make sure you have solcjs installed.

  • Run solcjs --abi Greeter.sol
  • Verify an Greeter.abi has been generated
info

ABIs are key interface files to interact with a contract in an application. Let's see below how to call a method using web3.js

Call the Greet method

// Main init varsimport { ethers } from "ethers";import Web3 from "web3";import { Contract } from "web3-eth-contract";const providerUrl = `<YOUR_ARKHIA_JSON_RPC_URL>/<ARKHIA_API_KEY>`;const web3 = new Web3(providerUrl);const abi = `<YOUR_ABI_OUTPUT_HERE>`;// Main contract varsconst contractJson = await JSON.parse(abi);const contractId = `<YOUR_SOLIDITY_CONTRACT_ID_HERE>`;const greeterContract =  new web3.eth.Contract(contractJson, contractId);const getGreetingMethod = async () : Promise<string> => {    const result  = await greeterContract.methods.greet().call();    console.log(`Greet method called`);    console.log(result);    return result;};getGreetingMethod();
info

Checkpoint

That's it, now you are able to query a contract already deployed on Hedera using familiar tools like Web3!