Read Only Calls
Read-only function calls allow you to query data from smart contracts without creating a transaction. These calls are free, instant, and don't require wallet interaction.
Basic read-only call
Call a read-only function to get contract data without any transaction fees:
import { fetchCallReadOnlyFunction, type ClarityValue, cvToString } from '@stacks/transactions';
const contractAddress = 'SM3VDXK3WZZSA84XXFKAFAF15NNZX32CTSG82JFQ4';
const contractName = 'sbtc-token';
const functionName = 'get-total-supply';
const response: ClarityValue = await fetchCallReadOnlyFunction({
contractName,
contractAddress,
functionName,
functionArgs: [],
senderAddress: 'SP2W7056R74EXF6GMGYXEKP6T5NT0FPQET74HXSCS',
});
console.log(response);
// { type: 'ok',
// value: { type: 'uint', value: 452551588021n }
// }
console.log(cvToString(response))
// (ok u452551588021)Passing function arguments
Most read-only functions require arguments. Use Clarity value builders to construct the appropriate types:
Handling response types
Read-only functions can return response types (ok/err). Check the response type to handle both success and error cases:
Using custom network
Specify a custom network URL for testnet or custom node connections:
Last updated
Was this helpful?