Explorer
Read Contract Data
Learn how to use read contract with the Okto SDK to fetch on-chain data from smart contracts on EVM and Aptos.
The rawRead()
method provided by Okto SDK enables reading data directly from smart contracts on supported networks. It accepts structured input parameters and works seamlessly across both EVM and Aptos chains.
Example
import { useOkto } from '@okto_web3/react-sdk';
import { rawRead } from '@okto_web3/react-sdk';
function RawRead() {
const oktoClient = useOkto();
async function rawReadContract() {
try {
const payload = {
caip2Id: "eip155:8453",
data: {
contractAddress: "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913",
abi: {"inputs":[{"internalType":"address","name":"account","type":"address"}],"name":"balanceOf","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},
args: {"account":"0xB7B8F759E8Bd293b91632100f53a45859832f463"},
},
};
const response = await rawRead(oktoClient, payload);
console.log('Fetched Data: ', response);
} catch (error) {
console.error('Error fetching contract data:', error);
}
}
return (
<button onClick={rawReadContract}>
Read Contract
</button>
);
}
import { useOkto } from '@okto_web3/react-sdk';
import { rawRead } from '@okto_web3/react-sdk';
function RawRead() {
const oktoClient = useOkto();
async function rawReadContract() {
try {
const payload = {
caip2Id: "aptos:testnet",
data: {
function: "0x1::coin::balance",
typeArguments: ["0x1::aptos_coin::AptosCoin"],
functionArguments: ["0x9ed7f8c95c5e2c3cb06dfbb48681b87401fabeb88b7d710db3720f7a2ca3fffc"],
},
};
const response = await rawRead(oktoClient, payload);
console.log('Fetched Data: ', response);
} catch (error) {
console.error('Error fetching contract data:', error);
}
}
return (
<button onClick={rawReadContract}>
Read Contract
</button>
);
}
Note
For error handling:
- Use the error code to debug issues. Check out the SDK errors and warnings documentation
- For help, navigate to the troubleshooting guide to contact support
Method Overview
Methods | Description |
---|---|
async rawRead | Read data from a smart contract |
Raw Read
async rawRead()
reads the data from the smart contract.
Parameters for EVM Chain
Parameter | Type | Description | Required |
---|---|---|---|
oktoClient | OktoClient | Instance of OktoClient obtained from useOkto hook | Yes |
payload | EVMRawReadParams | Parameters for executing Raw Read on EVM | Yes |
Where EVMRawReadParams
contains:
Field | Type | Description | Required |
---|---|---|---|
caip2Id | string | The network identifier (e.g., eip155:1 for Ethereum) | Yes |
contractAddress | string | Address of the target smart contract (must be a valid Ethereum address) | Yes |
abi | string[] | ABI definition of the smart contract | Yes |
args | string[] | Arguments required for the contract method call | Yes |
Parameters for Aptos Chain
Parameter | Type | Description | Required |
---|---|---|---|
oktoClient | OktoClient | Instance of OktoClient obtained from useOkto hook | Yes |
payload | AptosRawReadParams | Parameters for executing Raw Read on Aptos | Yes |
Where AptosRawReadParams
contains:
Field | Type | Description | Required |
---|---|---|---|
caip2Id | string | The network identifier (e.g., aptos:testnet for Aptos Testnet) | Yes |
function | string | Function identifier in the format address::module_name::function_name (e.g., 0x1::coin::balance ) | Yes |
typeArguments | string[] | Array of type arguments for functions | Yes |
functionArguments | string[] | Array of function arguments required for functions | Yes |
Response
Success Response
Field Name | Type | Description |
---|---|---|
result | Promise<ReadContractResponse[]> | Returns the data from the contract |