A minimal TypeScript client for interacting with JSON-RPC nodes, such as those used by Ethereum, Bitcoin, or other blockchains. This client allows you to make RPC calls and handle responses simply and efficiently.
- Send JSON-RPC requests to any RPC node
- Easy-to-use TypeScript API
- Handles method calls and error responses
First, install the required dependency:
npm install node-fetch- Create the client instance:
import { RpcNodeClient } from './RpcNodeClient';
const rpc = new RpcNodeClient('https://mainnet.infura.io/v3/YOUR_PROJECT_ID');- Make an RPC call:
rpc.call('eth_blockNumber')
.then(blockNumber => {
console.log('Latest block number:', blockNumber);
})
.catch(error => {
console.error('Error:', error);
});import { RpcNodeClient } from './RpcNodeClient';
async function main() {
const rpc = new RpcNodeClient('https://mainnet.infura.io/v3/YOUR_PROJECT_ID');
try {
const blockNumber = await rpc.call('eth_blockNumber');
console.log('Latest block number:', blockNumber);
} catch (error) {
console.error('RPC Error:', error);
}
}
main();new RpcNodeClient(url: string)url: The endpoint of the RPC node.
method: The RPC method name (e.g.,eth_blockNumber,eth_getBalance)params: Array of parameters for the RPC method (optional)
MIT