Docs

eth_call - MegaETH RPC Method

Execute smart contract calls without creating transactions on MegaETH. Essential for reading contract state for high-frequency trading, real-time gaming, instant payments, and latency-sensitive applications.

Executes a new message call immediately without creating a transaction on MegaETH. Used for reading smart contract state.

Why MegaETH? Build on the first real-time blockchain with sub-millisecond latency and 100,000+ TPS with sub-millisecond transaction streaming with 100,000+ sustained TPS and full EVM compatibility.

When to Use This Method

The eth_call method serves these key scenarios for high-frequency DeFi developers, gaming studios, and teams building real-time applications:

  • Read smart contract state - Execute view and pure functions to query token balances, DeFi positions, and protocol data without spending gas
  • Simulate transactions - Test contract interactions before submitting them on-chain, avoiding failed transactions and wasted gas costs
  • Multi-call aggregator queries - Batch multiple read calls into a single request using multicall contracts, reducing API overhead on MegaETH
  • MEV and arbitrage analysis - Simulate transaction bundles to evaluate profitable opportunities before execution on high-frequency trading, real-time gaming, instant payments, and latency-sensitive applications

Common Use Cases

1. Read ERC20 Token Balance

Query an ERC20 token contract to retrieve the balance for a specific wallet address using the balanceOf(address) function selector encoded as calldata. The selector is derived from the first 4 bytes of keccak256("balanceOf(address)"), followed by the 32-byte zero-padded address argument.

JavaScript
import { JsonRpcProvider } from 'ethers';

const provider = new JsonRpcProvider('https://api-megaeth-mainnet.n.dwellir.com/YOUR_API_KEY');
const tokenAddress = '0x0E0f4Dd25ae8AB20E1583D9E8EDFf319A88e1d3f';
const walletAddress = '0x0E0f4Dd25ae8AB20E1583D9E8EDFf319A88e1d3f';

const balanceSelector = '0x70a08231' + walletAddress.slice(2).padStart(64, '0');

async function getTokenBalance() {
  const result = await provider.call({
    to: tokenAddress,
    data: balanceSelector
  });
  console.log('Balance (raw):', BigInt(result).toString());
  return result;
}

getTokenBalance();

2. Query DeFi Protocol State

Read protocol reserves, price oracles, or user positions from DeFi contracts on MegaETH. Each protocol exposes view functions that let you inspect pool state without modifying it - ideal for building analytics dashboards and trading bots.

JavaScript
import { JsonRpcProvider, Interface } from 'ethers';

const provider = new JsonRpcProvider('https://api-megaeth-mainnet.n.dwellir.com/YOUR_API_KEY');

const poolAbi = [
  'function getReserves() view returns (uint112 reserve0, uint112 reserve1, uint32 blockTimestamp)'
];
const poolInterface = new Interface(poolAbi);
const poolAddress = '0x0E0f4Dd25ae8AB20E1583D9E8EDFf319A88e1d3f';

async function getPoolReserves() {
  const data = poolInterface.encodeFunctionData('getReserves');
  const result = await provider.call({ to: poolAddress, data });
  const decoded = poolInterface.decodeFunctionResult('getReserves', result);
  console.log('Reserve 0:', decoded[0].toString());
  console.log('Reserve 1:', decoded[1].toString());
  return decoded;
}

getPoolReserves();

3. Simulate a Swap Before Execution

Before committing a token swap, call the router contract's quote function to calculate the expected output. This lets you validate slippage tolerance and compare rates across DEXes without risking gas on a failed trade.

JavaScript
import { JsonRpcProvider, Interface, parseEther } from 'ethers';

const provider = new JsonRpcProvider('https://api-megaeth-mainnet.n.dwellir.com/YOUR_API_KEY');
const routerAddress = '0x0E0f4Dd25ae8AB20E1583D9E8EDFf319A88e1d3f';

const routerAbi = [
  'function getAmountsOut(uint amountIn, address[] calldata path) view returns (uint[] amounts)'
];
const routerInterface = new Interface(routerAbi);

async function simulateSwap(amountIn, tokenIn, tokenOut) {
  const data = routerInterface.encodeFunctionData('getAmountsOut', [
    parseEther(amountIn),
    [tokenIn, tokenOut]
  ]);
  const result = await provider.call({ to: routerAddress, data });
  const decoded = routerInterface.decodeFunctionResult('getAmountsOut', result);
  console.log('Expected output:', decoded[0][1].toString());
  return decoded[0][1];
}

simulateSwap('1.0', '0xTokenA...', '0xTokenB...');

Gas Limit Restriction

Gas Limit on MegaETH

On MegaETH, eth_call requests are subject to a maximum gas limit of 10,000,000. This limit ensures optimal real-time performance across the network.

If your contract call requires more gas, consider:

  • Breaking the operation into smaller calls
  • Optimizing your smart contract logic
  • Using view functions that return aggregated data

Best Practices

  • Use latest for current-state reads and pending for pre-confirmation simulation on MegaETH
  • Encode function selectors correctly: take the first 4 bytes of the keccak256 hash of the function signature
  • Handle revert errors gracefully by parsing the revert reason from the error response data
  • For batch reads, use multicall contracts to combine multiple eth_call requests into a single RPC call
  • eth_call does not consume gas, making it ideal for unlimited read queries on MegaETH

Code Examples

Error Handling

Error CodeMessageDescription
-32000Execution revertedContract function reverted
-32602Invalid parametersInvalid data encoding
-32015VM execution errorContract logic error