Giter VIP home page Giter VIP logo

web3-transaction-batcher's Introduction

Ethereum Transaction Batcher

Build Status

A utility to batch Ethereum transactions in a single transaction.

Why use this?

  • better user experience if you need do send (or re-send) many transactions
  • save gas
  • execute a sequence of transactions as an atomic transaction - if one fails, all do

Usage

Install the package npm install ethereum-transaction-batcher.

const Web3 = require('web3')
const Batcher = require('ethereum-transaction-batcher')

// the address where the helper contrat is deployed
const batcherAddress = '0x741A4dCaD4f72B83bE9103a383911d78362611cf'
const web3 = new Web3('http://localhost:8545/') // or another ethereum provider

batcher = new Batcher({web3, batcherAddress})

// create web3 transactions as you would normally
const tx1 = myContract.myMethod(myArg1, myarg2)

// call a function on a contract
const tx2 = {
  to: '0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe',
  data: '0x123454...',
}
// send some money to your friend
const tx3 =  {
  to: '0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe',
  value: '1000000000000000'
}

// send the transactions as a single transaction to the blockchain
// the batched transaction will fail if one of the subtransactions fail
const receipt = await batcher.sendTransaction([tx1, tx2, tx3])
console.log(receipt.events)

API

const batcher = new Batcher({
  web, // web3 instance, required
  batcherAdddress // address of batcher contract, optional if the network is main or rinkeby
})

A Batcher instance exposes a fucntion sendTransaction. The signature and behavior of the function is similar to the sendTransaction method in web3.js - the function can be called by providing it with a callback, if the callback is omitted it will return a Promise - but instead of taking a single transaction object as its first argument, it takes a list of transactions.

batcher.sendTransaction(
  transactions, // a list of web3-style transaction objects
  callback // optional, like in web3.js
)

The batcher.sendTransaction call will try to execute the entire list of transactions in the order that they are given. If one of them fails, the batched transaction fails atomically - i.e.\ none of the transactions will be executed.

sendTransaction behaves like web3.eth.sendTransaction - if a callback is provided, it will be called, if no callback is provided, it will return a promise that resolves to a transaction receipt.

The Contract

The solidity code is here.

Limitations

  • The batched transactions will be sent from the Batcher contract - which means that transactions that require msg.sender to be (say) the account with which the batched transaction is signed, will fail. However, the batcher will work when for sending Ether in batch transactions.
  • The batcher contract can only cal functions on existing contracts - it does not create new contracts

TODO

  • use assembly call to save gas
  • use assembly create to also allow for contract creation
  • forward tokens, so that one can batch-send tokens as well as Ether

License

GNU GPL

web3-transaction-batcher's People

Contributors

jellegerbrandy avatar leviadam avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.