npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

web3-batch-request

v1.0.1

Published

Web3 batch request allows you to create a batch request with web3 1.x and return a promise that resolves when it completes

Downloads

9

Readme

web3-batch-request

web3-batch-request is a node package for batching requests on the eth blockchain with web3 1.x. It returns a promise that resolves when all requests are complete or rejects when one fails. If you are using infura or another node provider which limits your requests, your batched requests will count as a single request.

For the package to work you will need to provide a web3 instance when making a batch call.

Usage:

Firstly install the dependency to your project

npm i web3-batch-request --save

Import the dependency where you need it:

const { makeBatchRequest } = require('web3-batch-request');

Create some calls that you would like to batch. A call is an object with the following properties:

{
    ethCall: web3 call object,
    onSuccess: Function,
    onError: Function
}

Both onSuccess and onError are optional. ethCall is mandatory.

An example of the above would be:

const usdtTokenAddress = '0xdac17f958d2ee523a2206206994597c13d831ec7';

{
    ethCall: new web3.eth.Contract(ERC20_ABI, usdtTokenAddress).methods.decimals().call,
    onSuccess: result => console.log("We can do something with our response if we like: " + result),
    onError: error => console.log("We can do some error handling with the error: " + err)
}

Once you have created some call objects, and added them to an array, you can perform the batch request.

const calls = [callOne, callTwo, callThree];
const web3 = new Web3(new Web3.providers.HttpProvider(`https://localhost:8545`));

[callOneResponse, callTwoResponse, callThreeResponse] = await makeBatchRequest(web3, calls);

You can also pass an options object when calling the makeBatchRequest function. The two options permitted are:

  • allowFailures - if one of the calls fails, the promise will still resolve
  • verbose - useful for debugging. will log both success and errors of calls.
[callOneResponse, callTwoResponse, callThreeResponse] = await makeBatchRequest(web3, calls, { allowFailures: true, verbose: true });

by default, both these options are set to false.

Examples

There's example code in the examples folder, largely the same as the example above. To use this you can rename the .env.example file to .env and change your infura project id in the file. If using a local node, replace the provider in the example with the RPC url of your local node before running.