RPC Guide
Last updated
const axios = require('axios');
const RPC_URL = 'https://RPC-URL.io';
async function getLatestBlock() {
const response = await axios.post(RPC_URL, {
jsonrpc: '2.0',
id: 1,
method: 'eth_blockNumber',
params: []
});
console.log('Latest Block:', response.data.result);
}
getLatestBlock();const Web3 = require('web3');
const web3 = new Web3('https://RPC-URL.io');
async function getNetworkInfo() {
const [blockNumber, gasPrice, chainId] = await Promise.all([
web3.eth.getBlockNumber(),
web3.eth.getGasPrice(),
web3.eth.getChainId()
]);
console.log({
blockNumber,
gasPrice: web3.utils.fromWei(gasPrice, 'gwei') + ' gwei',
chainId
});
}
getNetworkInfo();const { ethers } = require('ethers');
const provider = new ethers.JsonRpcProvider('https://RPC-URL.io');
async function getAccountBalance(address) {
const balance = await provider.getBalance(address);
console.log(
'Balance:',
ethers.formatEther(balance),
'LUMIA'
);
}
getAccountBalance('0x742d35Cc6634C0532925a3b844Bc454e4438f44e');// Get latest block number
eth_blockNumber
// Get network version
net_version
// Get gas price
eth_gasPrice
// Get balance
eth_getBalance
// Get transaction count
eth_getTransactionCount
// Send raw transaction
eth_sendRawTransaction
// Get transaction by hash
eth_getTransactionByHash
// Get block by number
eth_getBlockByNumber
// Get logs
eth_getLogsconst { ethers } = require('ethers');
async function sendTransaction() {
// Initialize provider
const provider = new ethers.JsonRpcProvider('https://RPC-URL.io');
// Create wallet from private key
const privateKey = 'your-private-key';
const wallet = new ethers.Wallet(privateKey, provider);
// Transaction parameters
const tx = {
to: "0x742d35Cc6634C0532925a3b844Bc454e4438f44e",
value: ethers.parseEther("0.1")
};
try {
// Send transaction
const transaction = await wallet.sendTransaction(tx);
console.log('Transaction Hash:', transaction.hash);
// Wait for confirmation
const receipt = await transaction.wait();
console.log('Transaction confirmed in block:', receipt.blockNumber);
} catch (error) {
console.error('Error:', error);
}
}