Skip to content

mrthomasbigproblem/glowing-octo-memory

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 

Repository files navigation

RPC Node Client (TypeScript)

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.

Features

  • Send JSON-RPC requests to any RPC node
  • Easy-to-use TypeScript API
  • Handles method calls and error responses

Installation

First, install the required dependency:

npm install node-fetch

Usage

  1. Create the client instance:
import { RpcNodeClient } from './RpcNodeClient';

const rpc = new RpcNodeClient('https://mainnet.infura.io/v3/YOUR_PROJECT_ID');
  1. Make an RPC call:
rpc.call('eth_blockNumber')
  .then(blockNumber => {
    console.log('Latest block number:', blockNumber);
  })
  .catch(error => {
    console.error('Error:', error);
  });

Example

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();

API

RpcNodeClient

Constructor

new RpcNodeClient(url: string)
  • url: The endpoint of the RPC node.

Methods

call(method: string, params?: any[]): Promise<any>
  • method: The RPC method name (e.g., eth_blockNumber, eth_getBalance)
  • params: Array of parameters for the RPC method (optional)

License

MIT

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published