read
A function of ContractClient
to read the state of a smart contract by the getter's name.
Usage
Call the read
method of ContractClient
with the getter name and its arguments.
import { parseTon } from '@fotonjs/core';
import { contractClient, publicClient } from './client';
// Imagine the contract is a counter with `counter` getter.
const res = await contractClient.read({
getter: 'counter',
arguments: [],
});
// > res.data – 1 (counter's state example)
Returns
type ReadContractReturn<
CONTRACT extends CompiledContract,
GETTER extends ContractGetterNames<CONTRACT>
> =
DataOrTypedError<
ContractGetterReturn<CONTRACT, GETTER> | undefined,
| 'MissingContractAddressError'
| 'IncorrectContractError'
| 'TonReadError'
| 'TonRateLimitError'
>;
Any value set that a getter can return. Foton parses the state based on the contract's ABI and returns the value or one of the typed errors.
Parameters
getter
- Type:
string
Getter name of the contract to call.
const res = await contractClient.read({
getter: 'counter',
arguments: [],
});
arguments
- Type:
Array<GetterArguments>
Arguments of the getter. The getter declaration can be found in the contract's ABI or contract class' getter.
For example, a contract in Tact might have a getter declaration get fun multiplier(factor: Int): Int
. It means that the getter fun
expects one argument of type bigint
.
const res = await contractClient.read({
getter: 'multiplied',
arguments: [1n],
});