readContract
Action for reading data from a Starknet contract.
Import
import { readContract } from "starkweb/core";
Usage
index.ts
import { readContract } from "starkweb/core";
import { erc20Abi } from "./abis/erc20";
import { config } from "./config";
const balance = await readContract(config, {
address: "0x123...",
abi: erc20Abi,
functionName: "balanceOf",
args: ["0x456..."],
});
Parameters
import { type ReadContractParameters } from "starkweb/core";
address
Address
The address of the contract to read from.
abi
Abi
The ABI of the contract.
functionName
string
The name of the function to call.
args (optional)
readonly unknown[]
The arguments to pass to the function.
index.ts
import { readContract } from "starkweb/core";
import { erc20Abi } from "./abis/erc20";
import { config } from "./config";
const result = await readContract(config, {
address: "0x123...",
abi: erc20Abi,
functionName: "balanceOf",
args: ["0x456..."],
});
chainId (optional)
Hex | undefined
The chain ID to read from. If not provided, uses the current chain.
index.ts
import { readContract } from "starkweb/core";
import { mainnet } from "starkweb/chains";
import { config } from "./config";
const result = await readContract(config, {
address: "0x123...",
abi: [...],
functionName: "balanceOf",
chainId: mainnet.chain_id,
});
Return Type
import { type ReadContractReturnType } from "starkweb/core";
Returns the data from the contract function call. The type depends on the function's return type as defined in the ABI.
Example
example.ts
import { readContract } from "starkweb/core";
import { erc20Abi } from "./abis/erc20";
import { mainnet } from "starkweb/chains";
import { config } from "./config";
// Read ERC20 balance
const balance = await readContract(config, {
address: "0x123...",
abi: erc20Abi,
functionName: "balanceOf",
args: ["0x456..."],
});
console.log("Token balance:", balance);
// Read from specific chain
const totalSupply = await readContract(config, {
address: "0x123...",
abi: erc20Abi,
functionName: "totalSupply",
chainId: mainnet.chain_id,
});
console.log("Total supply:", totalSupply);
Error
import { type ReadContractErrorType } from "starkweb/core";
TanStack Query
import {
type ReadContractData,
type ReadContractOptions,
type ReadContractQueryFnData,
type ReadContractQueryKey,
type ReadContractErrorType,
readContractQueryOptions,
} from "starkweb/query";