Skip to main content

eth_call

Execute a read-only call in the specified block state. eth_call will not create on-chain transactions and will not consume user Gas. It is often used to read contract status or pre-check contract call results.


Inputs

IndexTypeDescription
0objectcall object
1stringBlock number or label, such as latest, earliest, pending

Transaction call object

FieldVariantNeedRemarks
tostringyesContract address being called
datastringnoABI-encoded call data
fromstringnoSender used for call-context simulation
gasstringnoGas limit encoded as hex
gasPricestringnoGas price encoded as hex
valuestringnoAttached wei amount

return value

string - ​​Contract return data, hexadecimal encoding.


Demo

Read ERC-20 balance

// Method selector for balanceOf(address): 0x70a08231
const address = "0x1a2b3c4d5e6f708192a3b4c5d6e7f8091a2b3c4d".slice(2).padStart(64, "0");
const data = "0x70a08231" + address;

const balance = await window.$ukey.ethereum.request({
method: "eth_call",
requestParams: [
{
to: "0x2b4d6f8091a3c5e7f9b1d3f507192b4d6f8091a3", // Note: sample token contract
data: data,
},
"latest",
],
});

console.log("Current balance:", parseInt(balance, 16));

Read contract name

// Method selector for name(): 0x06fdde03
const name = await window.$ukey.ethereum.request({
method: "eth_call",
requestParams: [
{
to: "0x2b4d6f8091a3c5e7f9b1d3f507192b4d6f8091a3",
data: "0x06fdde03",
},
"latest",
],
});

// Decoded output from the ABI-encoded string
console.log("Decoded contract name:", decodeString(name));

Using ethers.js

import { Contract, BrowserProvider } from "ethers";

const provider = new BrowserProvider(window.$ukey.ethereum);

const contract = new Contract(
"0x2b4d6f8091a3c5e7f9b1d3f507192b4d6f8091a3",
["function balanceOf(address) view returns (uint256)"],
provider,
);

const balance = await contract.balanceOf("0x1a2b3c4d5e6f708192a3b4c5d6e7f8091a2b3c4d");
console.log("USDC token balance:", balance.toString());

error code

CodeMessageDetails
-32000Execution revertedContract call rollback
-32602Parameters failed validationInvalid call inputs

illustrate

  • This method will not modify the on-chain status, nor will it initiate transaction confirmation to the user.
  • from Although optional: some contracts will return different results depending on the caller address.
  • Use latest to read the latest known status; if you need historical status, you can pass in a specific block.
  • Leave complex ABI encoding and decoding to ethers.js, viem or web3.js.