https://github.com/daostack/web3-transaction-batcher
Send many ethereum transactions as a single transaction
https://github.com/daostack/web3-transaction-batcher
Last synced: 8 months ago
JSON representation
Send many ethereum transactions as a single transaction
- Host: GitHub
- URL: https://github.com/daostack/web3-transaction-batcher
- Owner: daostack
- License: gpl-3.0
- Created: 2019-10-14T11:28:52.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2023-01-24T00:42:28.000Z (almost 3 years ago)
- Last Synced: 2025-03-24T15:21:46.314Z (9 months ago)
- Language: JavaScript
- Size: 505 KB
- Stars: 63
- Watchers: 4
- Forks: 17
- Open Issues: 17
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Ethereum Transaction Batcher
[](https://travis-ci.com/daostack/web3-transaction-batcher)
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`.
```javascript
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
```javascript
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](https://web3js.readthedocs.io/en/v1.2.0/web3-eth.html#sendtransaction) - 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.
```javascript
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](./contracts/Batcher.sol) 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](./LICENSE)