Skip to content
Cive

readContract

Calls a read-only function on a contract, and returns the response.

A "read-only" function (constant function) on a Solidity contract is denoted by a view or pure keyword. They can only read the state of the contract, and cannot make any changes to it. Since read-only methods do not change the state of the contract, they do not require any gas to be executed, and can be called by any user without the need to pay for gas.

Internally, readContract uses a Public Client to call the callaction with ABI-encoded data.

Usage

Below is a very basic example of how to call a read-only function on a contract (with no arguments).

import { publicClient } from "./client";
import { Abi } from "./abi";
 
const data = await publicClient.readContract({
  address: "cfx:...",
  abi: Abi,
  functionName: "totalSupply",
});

Passing Arguments

If your function requires argument(s), you can pass them through with the args attribute.

TypeScript types for args will be inferred from the function name & ABI, to guard you from inserting the wrong values.

For example, the balanceOf function name below requires an address argument, and it is typed as Address.

import { publicClient } from "./client";
import { Abi } from "./abi";
 
const data = await publicClient.readContract({
  address: "cfx:...",
  abi: Abi,
  functionName: "balanceOf",
  args: ["cfx:..."],
});

Return Value

The response from the contract. Type is inferred.

Parameters

address

The contract address.

const data = await publicClient.readContract({
  address: "cfx:...", 
  abi: Abi,
  functionName: "totalSupply",
});

abi

The contract's ABI.

const data = await publicClient.readContract({
  address: "cfx:...",
  abi: Abi, 
  functionName: "totalSupply",
});

functionName

  • Type: string

A function to extract from the ABI.

const data = await publicClient.readContract({
  address: "cfx:...",
  abi: Abi,
  functionName: "totalSupply", 
});

args (optional)

  • Type: Inferred from ABI.

Arguments to pass to function call.

const data = await publicClient.readContract({
  address: "cfx:...",
  abi: Abi,
  functionName: "balanceOf",
  args: ["cfx:..."], 
});

account (optional)

  • Type: Account | Address

Optional Account sender override.

Accepts a Local Account (Private Key, etc).

import { privateKeyToAccount } from "cive/accounts";
export const account = privateKeyToAccount("0x...", {
  networkId: mainnet.id,
});
const data = await publicClient.readContract({
  address: "cfx:...",
  abi: Abi,
  functionName: "totalSupply",
  account: account, 
});

epochNumber (optional)

  • Type: bigint

The block number to perform the read against.

const data = await publicClient.readContract({
  address: "cfx:...",
  abi: Abi,
  functionName: "totalSupply",
  epochNumber: 15121123n, 
});

epochTag (optional)

  • Type: latest_state | latest_confirmed | latest_checkpoint | earliest

  • Default: latest_state

    The block number to perform the read against.

const data = await publicClient.readContract({
  address: "cfx:...",
  abi: Abi,
  functionName: "totalSupply",
  epochTag: "latest_state", 
});