How to Simulate a Function Call
This guide explains how to simulate
a function call using Aztec.js.
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.
Relevant 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/composed/docs_examples.test.ts#L5-L7
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/composed/docs_examples.test.ts#L38-L40
Simulating function calls
Call the simulate
function on the typescript contract wrapper like this:
simulate_function
const balance = await contract.methods.balance_of_public(wallet.getAddress()).simulate();
expect(balance).toEqual(1n);
Source code: yarn-project/end-to-end/src/composed/docs_examples.test.ts#L46-L49
Note
- If the simulated function is
unconstrained
you will get a properly typed value. - If the simulated function is
public
orprivate
it will return a Field array of size 4.