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
| Index | Type | Description |
|---|---|---|
| 0 | object | call object |
| 1 | string | Block number or label, such as latest, earliest, pending |
Transaction call object
| Field | Variant | Need | Remarks |
|---|---|---|---|
to | string | yes | Contract address being called |
data | string | no | ABI-encoded call data |
from | string | no | Sender used for call-context simulation |
gas | string | no | Gas limit encoded as hex |
gasPrice | string | no | Gas price encoded as hex |
value | string | no | Attached 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
| Code | Message | Details |
|---|---|---|
| -32000 | Execution reverted | Contract call rollback |
| -32602 | Parameters failed validation | Invalid call inputs |
illustrate
- This method will not modify the on-chain status, nor will it initiate transaction confirmation to the user.
fromAlthough optional: some contracts will return different results depending on the caller address.- Use
latestto 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.