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-batched-send

v1.0.3

Published

A utility for sending queued batches of transactions efficiently.

Downloads

184

Readme

This repo houses a collection of bots that call smart contract action callbacks, and a utility for sending queued batches of transactions efficiently that is used in all of them. This utility is published as an npm package and since the code lives here, the rest of this document will document it.

Installation

npm install --save web3-batched-send

or

yarn add web3-batched-send

Why You Should Use It

  • Sending a batch of transactions costs less gas than sending them all individually.
  • When dealing with any non-trivial contract, you'll need to send transactions before previous ones have been mined. Dealing with nonces and gas prices in those cases is a pain, and depending on your approach, you can miss transactions or have one transaction with a low gas price blocking all the ones that come after. If you also want to do batching, it becomes even harder. This utility abstracts all of that away efficiently.
  • The "eager and preemptive, but protected" API makes it really easy to write bots. You can "poke" all the action callbacks of your contract every X minutes and be sure that you will never spend gas on a transaction that would fail.

How You Use It

const Web3 = require('web3')
const _batchedSend = require('batched-send')
const _contract = require('../assets/contracts/contract.json')

const web3 = new Web3(process.env.WEB3_PROVIDER_URL)
const batchedSend = _batchedSend(
  web3, // Your web3 object.
  // The address of the transaction batcher contract you wish to use. The addresses for the different networks are listed below. If the one you need is missing, feel free to deploy it yourself and make a PR to save the address here for others to use.
  process.env.TRANSACTION_BATCHER_CONTRACT_ADDRESS,
  process.env.PRIVATE_KEY, // The private key of the account you want to send transactions from.
  60000 // The debounce timeout period in milliseconds in which transactions are batched.
)
const contract = new web3.eth.Contract(
  _contract.abi,
  process.env.CONTRACT_ADDRESS
)

batchedSend({
  method: contract.methods.foo,
  to: contract.options.address
})
batchedSend([
  // Also supports an array of transactions.
  {
    args: [a], // This is how you pass arguments.
    method: contract.methods.foo,
    to: contract.options.address
  },
  {
    args: [b],
    method: contract.methods.bar,
    to: contract.options.address
  },
  {
    args: [c],
    method: contract.methods.baz,
    to: contract.options.address
  }
])

Transaction Batcher Contract Addresses

  • Mainnet:
  • Kovan: 0x741A4dCaD4f72B83bE9103a383911d78362611cf

Full Examples

See the src/bots folder in this repo.

What It Does

It batches all your transactions until the specified timeout elapses since the last time you added a transaction (classic debounce batch). After the timeout elapses, this batch is added to the list of current pending batches. Imagine we had already sent batches A, B, C, and D, but due to rising gas prices, they are still in the mempool. Your pending batches list would look like this:

Pending Batches 1

The letters inside the batch represent the fact that whenever we send a batch transaction for a new batch, we include in it, the transactions of all previous batches. This is because the new batch transaction will share their nonce, and we don't want to lose the transactions of previous batches. Before sending the new batch transaction with the new median gas price, we take the opportunity to prune all pending batches of transactions that would now fail, to avoid wasting gas on failed transactions. If no transactions are left after the pruning, we just don't do anything. Note that the batching contract uses a low level call so even if a transaction still fails, it will not affect the others. After a batch transaction gets mined, because of the way we store pending batches, we can perform a really neat trick that ensures the algorithm has all the properties we want. We slice off the head of the list, up to and including the batch whose transaction got mined, and if any batches are left, we send a new batch transaction with their contents and essentially restart the whole flow with a new nonce.

Pending Batches 2

Leaving batch D in the list is not a problem, because it has an old nonce, it will never be mined and it will just be sliced off with the next batch transaction that gets mined.

The Smart Contract

pragma solidity 0.5.3;
pragma experimental ABIEncoderV2;

contract TransactionBatcher {
    function batchSend(address[] memory targets, uint[] memory values, bytes[] memory datas) public payable {
        for (uint i = 0; i < targets.length; i++)
            targets[i].call.value(values[i])(datas[i]);
    }
}