useContractQuery
Raw contract read hook — sends a query payload and returns the response bytes for manual decoding.
useContractQuery is the low-level hook for calling contract read functions. It returns raw bytes that you decode with a contract-specific decoder from @qubic.org/contracts or @qubic.org/registry.
For contracts that have generated wrappers in @qubic.org/contracts, prefer using those directly. Use useContractQuery when you need to call a function not yet covered by the generated package, or when you're integrating a custom contract.
useContractQuery(contractIndex, inputType, payload, options?)
The typical usage pattern is: build payload → query → decode response.
import { useContractQuery } from "@qubic.org/react"
import { qearn } from "@qubic.org/contracts"
import { useMemo } from "react"
function QearnRoundState({ epoch }: { epoch: number }) {
// Step 1: build the input payload
const payload = useMemo(
() => qearn.buildGetLockInfoPerEpochInput({ Epoch: epoch }),
[epoch],
)
// Step 2: send the query
const { data: rawBytes, isLoading, error } = useContractQuery(
qearn.contractIndex,
1, // QEARN_GET_LOCK_INFO_PER_EPOCH_INPUT_TYPE
payload,
{ refetchInterval: 30_000 },
)
// Step 3: decode the response
const info = rawBytes
? qearn.decodeLockOutput(rawBytes)
: undefined
if (isLoading) return <p>Loading...</p>
if (error) return <p>Error: {error.message}</p>
return <p>Locked amount: {info?.lockedAmount?.toString()}</p>
}Parameters
| Name | Type | Description |
|---|---|---|
contractIndex | number | The contract's index on-chain. |
inputType | number | The function's input type number from the ABI. |
payload | Uint8Array | Encoded input bytes. Build with buildPayload or a contract namespace builder. |
options.refetchInterval | number | false | Polling interval in milliseconds. Pass false to disable. |
Returns
| Field | Type | Description |
|---|---|---|
data | Uint8Array | undefined | Raw response bytes from the contract. Decode with a matching decoder. |
isLoading | boolean | True while the first fetch is in progress. |
error | QubicRpcError | null | Network or parse error. |