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
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
Inherited methods
Constructor details
EvmRpc
public EvmRpc(RpcTransport transport, String url)A client for a node.
Method details
chainId
public long chainId()
throws IOExceptioneth_chainId.Throws
blockNumber
public long blockNumber()
throws IOExceptioneth_blockNumber.Throws
getBalance
public UInt256 getBalance(EvmAddress address, String block)
throws IOExceptioneth_getBalance in wei at a block tag.Throws
getBalance
public UInt256 getBalance(EvmAddress address)
throws IOExceptionThe latest balance in wei.
Throws
getTransactionCount
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.Throws
gasPrice
public UInt256 gasPrice()
throws IOExceptioneth_gasPrice in wei.Throws
maxPriorityFeePerGas
public UInt256 maxPriorityFeePerGas()
throws IOExceptioneth_maxPriorityFeePerGas in wei.Throws
feeHistory
public Map<?, ?> feeHistory(int blocks, String newestBlock, double[] rewardPercentiles)
throws IOExceptioneth_feeHistory, as the node answers it.Throws
suggestFees
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.Throws
estimateGas
public long estimateGas(EvmAddress from, EvmAddress to, UInt256 value, byte[] data)
throws IOExceptioneth_estimateGas for a call; to is null for a contract creation.Throws
call
public byte[] call(EvmAddress to, byte[] data, String block)
throws IOExceptioneth_call at a block tag, answering the returned bytes.Throws
call
public byte[] call(EvmAddress to, byte[] data)
throws IOExceptioneth_call at the latest block.Throws
erc20BalanceOf
public UInt256 erc20BalanceOf(EvmAddress token, EvmAddress owner)
throws IOExceptionAn ERC-20 token balance in the token’s base units.
Throws
sendRawTransaction
public String sendRawTransaction(EvmTransaction signed)
throws IOExceptioneth_sendRawTransaction, answering the transaction hash.Throws
getTransactionReceipt
public Map<?, ?> getTransactionReceipt(String txHash)
throws IOExceptioneth_getTransactionReceipt: the receipt, or null while the transaction is unmined.Throws
isSuccess
public static boolean isSuccess(Map<?, ?> receipt)Whether a mined receipt reports success (
status 1).getLogs
public List<?> getLogs(Map<String, Object> filter)
throws IOExceptioneth_getLogs for a filter object (address, topics, fromBlock, toBlock).