Class EvmRpc
java.lang.Object
com.codename1.crypto.rpc.JsonRpc
com.codename1.crypto.rpc.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.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionlongeth_blockNumber.byte[]call(EvmAddress to, byte[] data) eth_callat the latest block.byte[]call(EvmAddress to, byte[] data, String block) eth_callat a block tag, answering the returned bytes.longchainId()eth_chainId.erc20BalanceOf(EvmAddress token, EvmAddress owner) An ERC-20 token balance in the token's base units.longestimateGas(EvmAddress from, EvmAddress to, UInt256 value, byte[] data) eth_estimateGasfor a call;toisnullfor a contract creation.Map<?, ?> feeHistory(int blocks, String newestBlock, double[] rewardPercentiles) eth_feeHistory, as the node answers it.gasPrice()eth_gasPricein wei.getBalance(EvmAddress address) The latest balance in wei.getBalance(EvmAddress address, String block) eth_getBalancein wei at a block tag.List<?> eth_getLogsfor a filter object (address,topics,fromBlock,toBlock).longgetTransactionCount(EvmAddress address, String block) eth_getTransactionCount: the next nonce at a block tag;pendingcounts what is queued in the node's pool.Map<?, ?> getTransactionReceipt(String txHash) eth_getTransactionReceipt: the receipt, ornullwhile the transaction is unmined.static booleanWhether a mined receipt reports success (status1).eth_maxPriorityFeePerGasin wei.sendRawTransaction(EvmTransaction signed) eth_sendRawTransaction, answering the transaction hash.UInt256[]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.
-
Constructor Details
-
EvmRpc
A client for a node.
-
-
Method Details
-
chainId
-
blockNumber
-
getBalance
eth_getBalancein wei at a block tag.- Throws:
IOException
-
getBalance
The latest balance in wei.- Throws:
IOException
-
getTransactionCount
eth_getTransactionCount: the next nonce at a block tag;pendingcounts what is queued in the node's pool.- Throws:
IOException
-
gasPrice
-
maxPriorityFeePerGas
eth_maxPriorityFeePerGasin wei.- Throws:
IOException
-
feeHistory
public Map<?,?> feeHistory(int blocks, String newestBlock, double[] rewardPercentiles) throws IOException eth_feeHistory, as the node answers it.- Throws:
IOException
-
suggestFees
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.- Throws:
IOException
-
estimateGas
public long estimateGas(EvmAddress from, EvmAddress to, UInt256 value, byte[] data) throws IOException eth_estimateGasfor a call;toisnullfor a contract creation.- Throws:
IOException
-
call
eth_callat a block tag, answering the returned bytes.- Throws:
IOException
-
call
eth_callat the latest block.- Throws:
IOException
-
erc20BalanceOf
An ERC-20 token balance in the token's base units.- Throws:
IOException
-
sendRawTransaction
eth_sendRawTransaction, answering the transaction hash.- Throws:
IOException
-
getTransactionReceipt
eth_getTransactionReceipt: the receipt, ornullwhile the transaction is unmined.- Throws:
IOException
-
isSuccess
Whether a mined receipt reports success (status1). -
getLogs
eth_getLogsfor a filter object (address,topics,fromBlock,toBlock).- Throws:
IOException
-