Skip to main content

How to Call a View Function

This guide explains how to call a view function using Aztec.js.

To do this from the CLI, go here.

Prerequisites

You should have a wallet to act as the caller, and a contract that has been deployed.

You can learn how to create wallets from this guide.

You can learn how to deploy a contract here.

Relevent imports

You will need to import this from Aztec.js:

import_contract
import { Contract } from '@aztec/aztec.js';
Source code: /yarn-project/end-to-end/src/docs_examples.test.ts#L5-L7 (aztec-packages-v0.24.0)

Define contract

Get a previously deployed contract like this:

get_contract
const contract = await Contract.at(deployedContract.address, TokenContractArtifact, wallet);
Source code: /yarn-project/end-to-end/src/docs_examples.test.ts#L36-L38 (aztec-packages-v0.24.0)

Call view function

Call the view function on the contract like this:

call_view_function
const _balance = await contract.methods.getBalance(wallet.getAddress()).view();
Source code: /yarn-project/end-to-end/src/docs_examples.test.ts#L44-L46 (aztec-packages-v0.24.0)