Client and backend. This class is shared: the same code is compiled into your app and into your server, so everything on this page works in both.

public class EvmRpc

  1. Object
  2. JsonRpc
  3. EvmRpc

An Ethereum JSON-RPC client, for any EVM chain: balances, nonces, fees, gas estimates, calls, broadcasting and receipts.

EvmRpc rpc = new EvmRpc(new NetworkTransport(), "https://ethereum-sepolia-rpc.publicnode.com");
Wallet wallet = Wallet.fromMnemonic(Mnemonic.generate(128), "");
EvmAddress me = wallet.getEvmAddress(0, 0);
EvmAddress to = EvmAddress.parse("0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed");
UInt256 value = UInt256.parseUnits("0.01", 18);
long nonce = rpc.getTransactionCount(me, "pending");
UInt256[] fees = rpc.suggestFees();
EvmTransaction tx = EvmTransaction.eip1559(rpc.chainId(), nonce, fees[0], fees[1], 21000, to, value, null);
String hash = rpc.sendRawTransaction(wallet.signEvmTransaction(tx, Wallet.evmPath(0, 0)));

Block tags are the node’s: latest, pending, safe, finalized, or a quantity.

Constructors

public EvmRpc(RpcTransport transport, String url)A client for a node.

Methods

public long chainId() throws IOExceptioneth_chainId.
public long blockNumber() throws IOExceptioneth_blockNumber.
public UInt256 getBalance(EvmAddress address, String block) throws IOExceptioneth_getBalance in wei at a block tag.
public UInt256 getBalance(EvmAddress address) throws IOExceptionThe latest balance in wei.
public long getTransactionCount(EvmAddress address, String block) throws IOExceptioneth_getTransactionCount: the next nonce at a block tag; pending counts what is queued in the node’s pool.
public UInt256 gasPrice() throws IOExceptioneth_gasPrice in wei.
public UInt256 maxPriorityFeePerGas() throws IOExceptioneth_maxPriorityFeePerGas in wei.
public Map<?, ?> feeHistory(int blocks, String newestBlock, double[] rewardPercentiles) throws IOExceptioneth_feeHistory, as the node answers it.
public UInt256[] suggestFees() throws IOExceptionEIP-1559 fees for a transaction to be included soon: {maxPriorityFeePerGas, maxFeePerGas}, the priority fee the node suggests and a fee cap of twice the latest base fee plus it, which covers six full blocks of base-fee growth.
public long estimateGas(EvmAddress from, EvmAddress to, UInt256 value, byte[] data) throws IOExceptioneth_estimateGas for a call; to is null for a contract creation.
public byte[] call(EvmAddress to, byte[] data, String block) throws IOExceptioneth_call at a block tag, answering the returned bytes.
public byte[] call(EvmAddress to, byte[] data) throws IOExceptioneth_call at the latest block.
public UInt256 erc20BalanceOf(EvmAddress token, EvmAddress owner) throws IOExceptionAn ERC-20 token balance in the token’s base units.
public String sendRawTransaction(EvmTransaction signed) throws IOExceptioneth_sendRawTransaction, answering the transaction hash.
public Map<?, ?> getTransactionReceipt(String txHash) throws IOExceptioneth_getTransactionReceipt: the receipt, or null while the transaction is unmined.
public static boolean isSuccess(Map<?, ?> receipt)Whether a mined receipt reports success (status 1).
public List<?> getLogs(Map<String, Object> filter) throws IOExceptioneth_getLogs for a filter object (address, topics, fromBlock, toBlock).

Inherited methods

Constructor details

EvmRpc

public EvmRpc(RpcTransport transport, String url)
A client for a node.

Method details

chainId

public long chainId() throws IOException
eth_chainId.

blockNumber

public long blockNumber() throws IOException
eth_blockNumber.

getBalance

public UInt256 getBalance(EvmAddress address, String block) throws IOException
eth_getBalance in wei at a block tag.

getBalance

public UInt256 getBalance(EvmAddress address) throws IOException
The latest balance in wei.

getTransactionCount

public long getTransactionCount(EvmAddress address, String block) throws IOException
eth_getTransactionCount: the next nonce at a block tag; pending counts what is queued in the node’s pool.

gasPrice

public UInt256 gasPrice() throws IOException
eth_gasPrice in wei.

maxPriorityFeePerGas

public UInt256 maxPriorityFeePerGas() throws IOException
eth_maxPriorityFeePerGas in wei.

feeHistory

public Map<?, ?> feeHistory(int blocks, String newestBlock, double[] rewardPercentiles) throws IOException
eth_feeHistory, as the node answers it.

suggestFees

public UInt256[] suggestFees() throws IOException
EIP-1559 fees for a transaction to be included soon: {maxPriorityFeePerGas, maxFeePerGas}, the priority fee the node suggests and a fee cap of twice the latest base fee plus it, which covers six full blocks of base-fee growth.

estimateGas

public long estimateGas(EvmAddress from, EvmAddress to, UInt256 value, byte[] data) throws IOException
eth_estimateGas for a call; to is null for a contract creation.

call

public byte[] call(EvmAddress to, byte[] data, String block) throws IOException
eth_call at a block tag, answering the returned bytes.

call

public byte[] call(EvmAddress to, byte[] data) throws IOException
eth_call at the latest block.

erc20BalanceOf

public UInt256 erc20BalanceOf(EvmAddress token, EvmAddress owner) throws IOException
An ERC-20 token balance in the token’s base units.

sendRawTransaction

public String sendRawTransaction(EvmTransaction signed) throws IOException
eth_sendRawTransaction, answering the transaction hash.

getTransactionReceipt

public Map<?, ?> getTransactionReceipt(String txHash) throws IOException
eth_getTransactionReceipt: the receipt, or null while the transaction is unmined.

isSuccess

public static boolean isSuccess(Map<?, ?> receipt)
Whether a mined receipt reports success (status 1).

getLogs

public List<?> getLogs(Map<String, Object> filter) throws IOException
eth_getLogs for a filter object (address, topics, fromBlock, toBlock).