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:

Method Overview

MethodsDescription
async rawReadRead data from a smart contract

Raw Read

async rawRead() reads the data from the smart contract.

Parameters for EVM Chain

ParameterTypeDescriptionRequired
oktoClientOktoClientInstance of OktoClient obtained from useOkto hookYes
payloadEVMRawReadParamsParameters for executing Raw Read on EVMYes

Where EVMRawReadParams contains:

FieldTypeDescriptionRequired
caip2IdstringThe network identifier (e.g., eip155:1 for Ethereum)Yes
contractAddressstringAddress of the target smart contract (must be a valid Ethereum address)Yes
abistring[]ABI definition of the smart contractYes
argsstring[]Arguments required for the contract method callYes

Parameters for Aptos Chain

ParameterTypeDescriptionRequired
oktoClientOktoClientInstance of OktoClient obtained from useOkto hookYes
payloadAptosRawReadParamsParameters for executing Raw Read on AptosYes

Where AptosRawReadParams contains:

FieldTypeDescriptionRequired
caip2IdstringThe network identifier (e.g., aptos:testnet for Aptos Testnet)Yes
functionstringFunction identifier in the format address::module_name::function_name (e.g., 0x1::coin::balance)Yes
typeArgumentsstring[]Array of type arguments for functionsYes
functionArgumentsstring[]Array of function arguments required for functionsYes

Response

Success Response

Field NameTypeDescription
resultPromise<ReadContractResponse[]>Returns the data from the contract