QubicTypeScript

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

NameTypeDescription
contractIndexnumberThe contract's index on-chain.
inputTypenumberThe function's input type number from the ABI.
payloadUint8ArrayEncoded input bytes. Build with buildPayload or a contract namespace builder.
options.refetchIntervalnumber | falsePolling interval in milliseconds. Pass false to disable.

Returns

FieldTypeDescription
dataUint8Array | undefinedRaw response bytes from the contract. Decode with a matching decoder.
isLoadingbooleanTrue while the first fetch is in progress.
errorQubicRpcError | nullNetwork or parse error.

On this page