Ethereum
Prêt à utiliser cela en production ?
Le niveau gratuit couvre les projets personnels. Le paiement à l'usage évolue sans carte bancaire.
Ethereum
Le niveau gratuit couvre les projets personnels. Le paiement à l'usage évolue sans carte bancaire.
These are the operations almost every Ethereum integration needs on day one: read an account's ETH balance, look up a transaction and its receipt, fetch a block, call a contract's view function, and pull the gas price and nonce you need before sending. Each one is a plain JSON-RPC POST to https://ethereum.therpc.io/YOUR_API_KEY. The examples below run unmodified against chain 1.
Start with two reads that everything else builds on. eth_blockNumber returns the height of the current chain head as a hex string — handy for confirming your endpoint is live and tracking how far behind you are. eth_getBalance returns an account's balance, but mind the units: the value comes back in wei, the smallest denomination, so you divide by 10^18 (ETH has 18 decimals) to read it as ETH. A balance of 0xde0b6b3a7640000 is one whole ETH.
There are two ways to look at a transaction. eth_getTransactionByHash returns the transaction itself — sender, recipient, value, gas settings, and whether it is still pending or already mined. eth_getTransactionReceipt returns the outcome after it lands in a block: the status field (0x1 success, 0x0 revert), the block it was included in, gas actually used, and the full array of event logs the transaction emitted. A null receipt means the transaction has not been mined yet, so receipts are what you poll while waiting for confirmation.
You can fetch a block either by its number with eth_getBlockByNumber or by its hash with eth_getBlockByHash. By-number also accepts the named tags — latest, safe, finalized, pending, earliest — which is how you read at proof-of-stake finality instead of at the head. Both take a boolean second parameter: pass false to get just the list of transaction hashes (smaller, faster), or true to get every transaction expanded into a full object inline.
Two methods cover reading from contracts. eth_call executes a function against the current state without sending a transaction or spending gas — this is how you read an ERC-20 balanceOf, a Uniswap pool's reserves, or any view function. The data field carries the ABI-encoded selector and arguments. eth_getCode returns the deployed bytecode at an address; a non-empty result confirms the address is a contract rather than an externally owned account, and 0x means there is no code there.
Two checks tell you about the node and the network. net_version returns the network ID as a string — 1 for Ethereum mainnet — which is a quick way to confirm you have not accidentally pointed at Sepolia or Holesky. eth_syncing reports whether the node is still catching up: it returns false once fully synced, or an object with current and highest block numbers while it is still importing. Reads served while a node is mid-sync can be stale, so this is worth checking if results look behind.
Before you broadcast a transaction you need two values. eth_gasPrice returns a single suggested price in wei — the legacy path; for EIP-1559 type-2 transactions you would instead build maxFeePerGas and maxPriorityFeePerGas from eth_feeHistory and eth_maxPriorityFeePerGas. eth_getTransactionCount returns the account's nonce, the count of transactions it has already sent. The nonce sequences transactions from an address: each one must use the next integer in order, and a gap leaves later transactions stuck in the queued pool until the missing nonce arrives.