Skip to content

jackripper235/drpc-client

 
 

Repository files navigation

DRPC SDK

Client-side library for working with DRPC (drpc.org). It's responsibility to communicate with DRPC and also to check provided signatures for response data, validating data authenticity. Supports node and browser.

Installation

npm install @drpcorg/drpc-sdk

Example

import { HTTPApi } from '@drpcorg/drpc-sdk';

async function getBlockHeight() {
  let api = new HTTPApi({
    dkey: 'api key',
    url: 'https://drpc.org/api',
    provider_ids: ['test'],
    provider_num: 1,
  });

  // single request
  let blockheight = await api.call({
    method: 'eth_blockNumber',
    params: [],
  });

  // batch request
  let batch = await api.callMulti([
    {
      method: 'eth_blockNumber',
      params: [],
    },
    {
      method: 'eth_getBlockByNumber',
      params: ['0x100001'],
    },
  ]);
}

Documentation

API Documentation

Using in browser

This module is written to work in node and browser. Because of that, by default webpack and other bundlers will try to bundle node dependencies (like node-fetch, etc). However, if you use webpack you can just define constant and it will eliminate any non-browser code.

{
  // ....
  plugins: [
    new webpack.DefinePlugin({
      __isBrowser__: 'true',
    }),
  ],
};

Web3.js provider

If you're using web3.js, drpc-sdk exposes the provider

import { HttpDrpcProvider } from '@drpcorg/drpc-sdk/dist/esm/providers/web3';
// for cjs
// import { DrpcProvider } from '@drpcorg/drpc-sdk/dist/cjs/providers/web3';

async function getBlock(tag) {
  let state = provider({
    dkey: 'api key',
    url: 'https://drpc.org/api',
    provider_ids: ['test'],
    provider_num: 1,
  });
  // or WsDrpcProvider for websockets
  let provider = new HttpDrpcProvider(state);
  let web3 = new Web3(provider);

  let result = await web3.eth.getBlockNumber();
}

Ethers.js provider

If you're using ethers.js, drpc-sdk exposes the provider

import { DrpcProvider } from '@drpcorg/drpc-sdk/dist/esm/providers/ethers';
// for cjs
// import { DrpcProvider } from '@drpcorg/drpc-sdk/dist/cjs/providers/ethers';

async function getBlock(tag) {
  let state = provider({
    dkey: 'api key',
    url: 'https://drpc.org/api',
    provider_ids: ['test'],
    provider_num: 1,
  });
  let provider = new DrpcProvider(state);
  let block = await provider.getBlock(tag);
}

About

JavaScript drpc.org client

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 92.6%
  • JavaScript 6.0%
  • Makefile 1.4%