Skip to content

contract.query.*

Process a named off-chain query of the contract. The query is declare as a function without mut modifier in the Ink! contract, and the function name will convert to camelCase when calling with SDK.

It share the compatbile API design with @polkadot/api-contract.

In Phat Contract, contract query is free and no gas fee required. You only need adjust the computation power percentage by adjust staking.

Usage

example.ts
import * as fs from 'node:fs'
import { client, pair, cert } from './setup'
 
const abi = fs.readFileSync('path/to/your/Certificate.abi.json', 'utf-8')
const contract = await getContract({
  client,
  contractId: '0x...',
  abi,
})
const result = await contract.query.getCaller(pair.address, { cert }, 'Hi Remark') 
const callerAddress = result.output.asOk

Parameters

address

string | AccountId

The address of the wallet that singing the certificateData for the query.

options

PinkContractQueryOptions

The options for the query. It's a object with the following properties:

  • cert: certificateData. Required. The certificateData must signed by the address.
  • salt: string. Optional. The salt for the query signing.
  • estimating: boolean. Optional. Declare it's estimating or not. If true, it will simulate the query and return the estimate result for gas fee and storage deposit fee.
  • deposit: number | bigint | BN. Optional. Use if estimating is true, it means the estimating will assume the user will deposit the deposit amount of PHA at the same time.
  • transfer: number | bigint | BN. Optional. Use if estimating is true, it means the estimating will assume the user will transfer the transfer amount to cluster at the same time.

...rest

any[]

...rest is the arguments for the query.

Returns

ContractCallResult<'promise', PinkContractCallOutcome<ILooseResult<TResult=Codec, TErr=Codec>>>

The result of the query is the ContractCallResult instance, same behavior to the @Polkadot/api-contract.

If the query returns meaningful result, you need fetch it from the output property, then use isOk to check and use asOk to get the succeed result.

const callerAddress = result.output.asOk