Note

This documentation is under construction and documents the 2.x alpha versions of web3.js. The current stable version of web3.js is 1.0 and should get preferred for production use cases.

TxPool Module

The web3-eth-txpool package gives you access to several non-standard RPC methods to inspect the contents of the transaction pool containing all the currently pending transactions as well as the ones queued for future processing.

import Web3 from 'web3';
import {TxPool} from 'web3-eth-txpool';

// "Web3.givenProvider" will be set if in an Ethereum supported browser.
const txPool = new TxPool(Web3.givenProvider || 'ws://some.local-or-remote.node:8546', null, options);

options

An Web3 module does provide several options for configuring the transaction confirmation worklfow or for defining default values. These are the currently available option properties on a Web3 module:

Example

import Web3 from 'web3';

const options = {
    defaultAccount: '0x0',
    defaultBlock: 'latest',
    defaultGas: 1,
    defaultGasPrice: 0,
    transactionBlockTimeout: 50,
    transactionConfirmationBlocks: 24,
    transactionPollingTimeout: 480,
    transactionSigner: new CustomTransactionSigner()
}

const web3 = new Web3('http://localhost:8545', null, options);

defaultBlock

web3.defaultBlock
web3.eth.defaultBlock
web3.shh.defaultBlock
...

The default block is used for all methods which have a block parameter. You can override it by passing the block parameter if a block is required.

Example:

Returns

The defaultBlock property can return the following values:

  • Number: A block number
  • "genesis" - String: The genesis block
  • "latest" - String: The latest block (current head of the blockchain)
  • "pending" - String: The currently mined block (including pending transactions)

Default is "latest"


defaultAccount

web3.defaultAccount
web3.eth.defaultAccount
web3.shh.defaultAccount
...

This default address is used as the default "from" property, if no "from" property is specified.

Returns

String - 20 Bytes: Any Ethereum address. You need to have the private key for that address in your node or keystore. (Default is undefined)


defaultGasPrice

web3.defaultGasPrice
web3.eth.defaultGasPrice
web3.shh.defaultGasPrice
...

The default gas price which will be used for a request.

Returns

string|number: The current value of the defaultGasPrice property.


defaultGas

web3.defaultGas
web3.eth.defaultGas
web3.shh.defaultGas
...

The default gas which will be used for a request.

Returns

string|number: The current value of the defaultGas property.


transactionBlockTimeout

web3.transactionBlockTimeout
web3.eth.transactionBlockTimeout
web3.shh.transactionBlockTimeout
...

The transactionBlockTimeout will be used over a socket based connection. This option does define the amount of new blocks it should wait until the first confirmation happens. This means the PromiEvent rejects with a timeout error when the timeout got exceeded.

Returns

number: The current value of transactionBlockTimeout


transactionConfirmationBlocks

web3.transactionConfirmationBlocks
web3.eth.transactionConfirmationBlocks
web3.shh.transactionConfirmationBlocks
...

This defines the number of blocks it requires until a transaction will be handled as confirmed.

Returns

number: The current value of transactionConfirmationBlocks


transactionPollingTimeout

web3.transactionPollingTimeout
web3.eth.transactionPollingTimeout
web3.shh.transactionPollingTimeout
...

The transactionPollingTimeout will be used over a HTTP connection. This option does define the amount of polls (each second) it should wait until the first confirmation happens.

Returns

number: The current value of transactionPollingTimeout


transactionSigner

web3.eth.transactionSigner
...

The transactionSigner property does provide us the possibility to customize the signing process of the Eth module and the related sub-modules.

The interface of a TransactionSigner:

interface TransactionSigner {
    sign(txObject: Transaction): Promise<SignedTransaction>
}

interface SignedTransaction {
    messageHash: string,
    v: string,
    r: string,
    s: string,
    rawTransaction: string
}

Returns

TransactionSigner: A JavaScript class of type TransactionSigner.


setProvider

web3.setProvider(myProvider)
web3.eth.setProvider(myProvider)
web3.shh.setProvider(myProvider)
...

Will change the provider for its module.

Note

When called on the umbrella package web3 it will also set the provider for all sub modules web3.eth, web3.shh, etc.

Parameters

  1. Object|String - provider: a valid provider
  2. Net - net: (optional) the node.js Net package. This is only required for the IPC provider.

Returns

Boolean

Example

import Web3 from 'web3';

const web3 = new Web3('http://localhost:8545');

// or
const web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545'));

// change provider
web3.setProvider('ws://localhost:8546');
// or
web3.setProvider(new Web3.providers.WebsocketProvider('ws://localhost:8546'));

// Using the IPC provider in node.js
const net = require('net');
const web3 = new Web3('/Users/myuser/Library/Ethereum/geth.ipc', net); // mac os path

// or
const web3 = new Web3(new Web3.providers.IpcProvider('/Users/myuser/Library/Ethereum/geth.ipc', net)); // mac os path
// on windows the path is: '\\\\.\\pipe\\geth.ipc'
// on linux the path is: '/users/myuser/.ethereum/geth.ipc'

providers

Web3.providers
Eth.providers
...

Contains the current available providers.

Value

Object with the following providers:

  • Object - HttpProvider: The HTTP provider is deprecated, as it won’t work for subscriptions.
  • Object - WebsocketProvider: The Websocket provider is the standard for usage in legacy browsers.
  • Object - IpcProvider: The IPC provider is used node.js dapps when running a local node. Gives the most secure connection.

Example

const Web3 = require('web3');
// use the given Provider, e.g in Mist, or instantiate a new websocket provider
const web3 = new Web3(Web3.givenProvider || 'ws://localhost:8546');
// or
const web3 = new Web3(Web3.givenProvider || new Web3.providers.WebsocketProvider('ws://localhost:8546'));

// Using the IPC provider in node.js
const net = require('net');

const web3 = new Web3('/Users/myuser/Library/Ethereum/geth.ipc', net); // mac os path
// or
const web3 = new Web3(new Web3.providers.IpcProvider('/Users/myuser/Library/Ethereum/geth.ipc', net)); // mac os path
// on windows the path is: '\\\\.\\pipe\\geth.ipc'
// on linux the path is: '/users/myuser/.ethereum/geth.ipc'

givenProvider

Web3.givenProvider
web3.eth.givenProvider
web3.shh.givenProvider
...

When using web3.js in an Ethereum compatible browser, it will set with the current native provider by that browser. Will return the given provider by the (browser) environment, otherwise null.

Returns

Object: The given provider set or false.

Example

web3.setProvider(Web3.givenProvider || 'ws://localhost:8546');

currentProvider

web3.currentProvider
web3.eth.currentProvider
web3.shh.currentProvider
...

Will return the current provider.

Returns

Object: The current provider set.

Example

if (!web3.currentProvider) {
    web3.setProvider('http://localhost:8545');
}

BatchRequest

new web3.BatchRequest()
new web3.eth.BatchRequest()
new web3.shh.BatchRequest()
...

Class to create and execute batch requests.

Parameters

none

Returns

Object: With the following methods:

  • add(request): To add a request object to the batch call.
  • execute(): Will execute the batch request.

Example

const contract = new web3.eth.Contract(abi, address);

const batch = new web3.BatchRequest();
batch.add(web3.eth.getBalance.request('0x0000000000000000000000000000000000000000', 'latest'));
batch.add(contract.methods.balance(address).call.request({from: '0x0000000000000000000000000000000000000000'}));
batch.execute().then(...);

getContent

txPool.getContent([callback])

This API can be used to list the exact details of all the transactions currently pending for inclusion in the next block(s), as well as the ones that are being scheduled for future executions. The RPC method used is txpool_content.

Parameters

  1. Function - (optional) Optional callback, returns an error object as first parameter and the result as second.

Returns

Promise<Object> - The list of pending as well as queued transactions.

  • pending - Object: List of pending transactions with transaction details.

  • queued - Object: List of queued transactions with transaction details.

    • hash 32 Bytes - String: Hash of the transaction.
    • nonce - Number: The number of transactions made by the sender prior to this one.
    • blockHash 32 Bytes - String: Hash of the block where this transaction was in. null when its pending.
    • blockNumber - Number: Block number where this transaction was in. null when its pending.
    • transactionIndex - Number: Integer of the transactions index position in the block. null when its pending.
    • from - String: Address of the sender.
    • to - String: Address of the receiver. null when its a contract creation transaction.
    • value - String: Value transferred in wei.
    • gasPrice - String: The wei per unit of gas provided by the sender in wei.
    • gas - Number: Gas provided by the sender.
    • input - String: The data sent along with the transaction.

Example

txPool.getContent().then(console.log);
> {
    pending: {
        0x0216d5032f356960cd3749c31ab34eeff21b3395: {
        806: [{
            blockHash: "0x0000000000000000000000000000000000000000000000000000000000000000",
            blockNumber: null,
            from: "0x0216d5032f356960cd3749c31ab34eeff21b3395",
            gas: "0x5208",
            gasPrice: "0xba43b7400",
            hash: "0xaf953a2d01f55cfe080c0c94150a60105e8ac3d51153058a1f03dd239dd08586",
            input: "0x",
            nonce: "0x326",
            to: "0x7f69a91a3cf4be60020fb58b893b7cbb65376db8",
            transactionIndex: null,
            value: "0x19a99f0cf456000"
        }]
        },
        0x24d407e5a0b506e1cb2fae163100b5de01f5193c: {
        34: [{
            blockHash: "0x0000000000000000000000000000000000000000000000000000000000000000",
            blockNumber: null,
            from: "0x24d407e5a0b506e1cb2fae163100b5de01f5193c",
            gas: "0x44c72",
            gasPrice: "0x4a817c800",
            hash: "0xb5b8b853af32226755a65ba0602f7ed0e8be2211516153b75e9ed640a7d359fe",
            input: "0xb61d27f600000000000000000000000024d407e5a0b506e1cb2fae163100b5de01f5193c00000000000000000000000000000000000000000000000053444835ec580000000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
            nonce: "0x22",
            to: "0x7320785200f74861b69c49e4ab32399a71b34f1a",
            transactionIndex: null,
            value: "0x0"
        }]
        }
    },
    queued: {
        0x976a3fc5d6f7d259ebfb4cc2ae75115475e9867c: {
            3: [{
                blockHash: "0x0000000000000000000000000000000000000000000000000000000000000000",
                blockNumber: null,
                from: "0x976a3fc5d6f7d259ebfb4cc2ae75115475e9867c",
                gas: "0x15f90",
                gasPrice: "0x4a817c800",
                hash: "0x57b30c59fc39a50e1cba90e3099286dfa5aaf60294a629240b5bbec6e2e66576",
                input: "0x",
                nonce: "0x3",
                to: "0x346fb27de7e7370008f5da379f74dd49f5f2f80f",
                transactionIndex: null,
                value: "0x1f161421c8e0000"
            }]
        },
        0x9b11bf0459b0c4b2f87f8cebca4cfc26f294b63a: {
            2: [{
                blockHash: "0x0000000000000000000000000000000000000000000000000000000000000000",
                blockNumber: null,
                from: "0x9b11bf0459b0c4b2f87f8cebca4cfc26f294b63a",
                gas: "0x15f90",
                gasPrice: "0xba43b7400",
                hash: "0x3a3c0698552eec2455ed3190eac3996feccc806970a4a056106deaf6ceb1e5e3",
                input: "0x",
                nonce: "0x2",
                to: "0x24a461f25ee6a318bdef7f33de634a67bb67ac9d",
                transactionIndex: null,
                value: "0xebec21ee1da40000"
            }]
        }
    }
}

getInspection

txPool.getInspection([, callback])

The property can be queried to list a textual summary of all the transactions currently pending for inclusion in the next block(s), as well as the ones that are being scheduled for future executions. The RPC method used is txpool_inspect.

Parameters

  1. Function - (optional) Optional callback, returns an error object as first parameter and the result as second.

Returns

Promise<Object> - The List of pending and queued transactions summary.

  • pending - Object: List of pending transactions with transaction details.
  • queued - Object: List of queued transactions with transaction details.

Example

txPool.getInspection().then(console.log);
> {
    pending: {
        0x26588a9301b0428d95e6fc3a5024fce8bec12d51: {
        31813: ["0x3375ee30428b2a71c428afa5e89e427905f95f7e: 0 wei + 500000 × 20000000000 gas"]
        },
        0x2a65aca4d5fc5b5c859090a6c34d164135398226: {
        563662: ["0x958c1fa64b34db746925c6f8a3dd81128e40355e: 1051546810000000000 wei + 90000 × 20000000000 gas"],
        563663: ["0x77517b1491a0299a44d668473411676f94e97e34: 1051190740000000000 wei + 90000 × 20000000000 gas"],
        563664: ["0x3e2a7fe169c8f8eee251bb00d9fb6d304ce07d3a: 1050828950000000000 wei + 90000 × 20000000000 gas"],
        563665: ["0xaf6c4695da477f8c663ea2d8b768ad82cb6a8522: 1050544770000000000 wei + 90000 × 20000000000 gas"],
        563666: ["0x139b148094c50f4d20b01caf21b85edb711574db: 1048598530000000000 wei + 90000 × 20000000000 gas"],
        563667: ["0x48b3bd66770b0d1eecefce090dafee36257538ae: 1048367260000000000 wei + 90000 × 20000000000 gas"],
        563668: ["0x468569500925d53e06dd0993014ad166fd7dd381: 1048126690000000000 wei + 90000 × 20000000000 gas"],
        563669: ["0x3dcb4c90477a4b8ff7190b79b524773cbe3be661: 1047965690000000000 wei + 90000 × 20000000000 gas"],
        563670: ["0x6dfef5bc94b031407ffe71ae8076ca0fbf190963: 1047859050000000000 wei + 90000 × 20000000000 gas"]
        },
        0x9174e688d7de157c5c0583df424eaab2676ac162: {
        3: ["0xbb9bc244d798123fde783fcc1c72d3bb8c189413: 30000000000000000000 wei + 85000 × 21000000000 gas"]
        },
        0xb18f9d01323e150096650ab989cfecd39d757aec: {
        777: ["0xcd79c72690750f079ae6ab6ccd7e7aedc03c7720: 0 wei + 1000000 × 20000000000 gas"]
        },
        0xb2916c870cf66967b6510b76c07e9d13a5d23514: {
        2: ["0x576f25199d60982a8f31a8dff4da8acb982e6aba: 26000000000000000000 wei + 90000 × 20000000000 gas"]
        },
        0xbc0ca4f217e052753614d6b019948824d0d8688b: {
        0: ["0x2910543af39aba0cd09dbb2d50200b3e800a63d2: 1000000000000000000 wei + 50000 × 1171602790622 gas"]
        },
        0xea674fdde714fd979de3edf0f56aa9716b898ec8: {
        70148: ["0xe39c55ead9f997f7fa20ebe40fb4649943d7db66: 1000767667434026200 wei + 90000 × 20000000000 gas"]
        }
    },
    queued: {
        0x0f6000de1578619320aba5e392706b131fb1de6f: {
        6: ["0x8383534d0bcd0186d326c993031311c0ac0d9b2d: 9000000000000000000 wei + 21000 × 20000000000 gas"]
        },
        0x5b30608c678e1ac464a8994c3b33e5cdf3497112: {
        6: ["0x9773547e27f8303c87089dc42d9288aa2b9d8f06: 50000000000000000000 wei + 90000 × 50000000000 gas"]
        },
        0x976a3fc5d6f7d259ebfb4cc2ae75115475e9867c: {
        3: ["0x346fb27de7e7370008f5da379f74dd49f5f2f80f: 140000000000000000 wei + 90000 × 20000000000 gas"]
        },
        0x9b11bf0459b0c4b2f87f8cebca4cfc26f294b63a: {
        2: ["0x24a461f25ee6a318bdef7f33de634a67bb67ac9d: 17000000000000000000 wei + 90000 × 50000000000 gas"],
        6: ["0x6368f3f8c2b42435d6c136757382e4a59436a681: 17990000000000000000 wei + 90000 × 20000000000 gas", "0x8db7b4e0ecb095fbd01dffa62010801296a9ac78: 16998950000000000000 wei + 90000 × 20000000000 gas"],
        7: ["0x6368f3f8c2b42435d6c136757382e4a59436a681: 17900000000000000000 wei + 90000 × 20000000000 gas"]
        }
    }
}

getStatus

txPool.getStatus([, callback])

This will provide the number of transactions currently pending for inclusion in the next block(s), as well as the ones that are being scheduled for future executions. The RPC method used is txpool_status.

Parameters

  1. Function - (optional) Optional callback, returns an error object as first parameter and the result as second.

Returns

Promise<Object> - A list of number of pending and queued transactions.

  • pending - number: Number of pending transactions.
  • queued - number: Number of queued transactions.

Example

txPool.getStatus().then(console.log);
> {
    pending: 10,
    queued: 7
}