When called on the umbrella package web3 it
will also set the provider for all sub modules web3.eth,
web3.shh,
etc. EXCEPT web3.bzz
which needs a separate provider at all times.
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.
You also have *.extend.formatters
as additional formatter functions to be used for input and output formatting. Please
see the source
file for function details.
methods
- Object:
Extension object with array of methods description objects as follows:
property
- String:
(optional) The name of the property to add to the module. If no
property is set it will be added to the module directly.
methods
- Array:
The array of method descriptions:
name
- String:
Name of the method to add.
call
- String:
The RPC method name.
params
- Number:
(optional) The number of parameters for that
function. Default 0.
inputFormatter
- Array:
(optional) Array of inputformatter functions. Each
array item responds to a function parameter, so if
you want some parameters not to be formatted, add a
null
instead.
outputFormatter-``Function:
(optional) Can be used to format the output of the
method.
All Ethereum addresses returned by functions of this package are returned as checksum
addresses.
This means some letters are uppercase and some are lowercase.
Based on that it will calculate a checksum for the address and prove its correctness.
Incorrect checksum addresses will throw an error when passed into functions.
If you want to circumvent the checksum check you can make an address all lower- or
uppercase.
When called on the umbrella package web3 it
will also set the provider for all sub modules web3.eth,
web3.shh,
etc. EXCEPT web3.bzz
which needs a separate provider at all times.
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.
You also have *.extend.formatters
as additional formatter functions to be used for input and output formatting.
Please see the source
file for function details.
methods
- Object:
Extension object with array of methods description objects as
follows:
property
- String:
(optional) The name of the property to add to the module. If
no property is set it will be added to the module directly.
methods
- Array:
The array of method descriptions:
name
- String:
Name of the method to add.
call
- String:
The RPC method name.
params
- Number:
(optional) The number of parameters for that
function. Default 0.
inputFormatter
- Array:
(optional) Array of inputformatter
functions. Each array item responds to a
function parameter, so if you want some
parameters not to be formatted, add a null
instead.
outputFormatter-``Function:
(optional) Can be used to format the output
of the method.
The transactionBlockTimeout
is used over socket-based connections. This option defines the amount of new blocks
it should wait until the first confirmation happens, otherwise the PromiEvent
rejects with a timeout error.
The transactionPollingTimeout
is used over HTTP connections. This option defines the number of seconds Web3 will
wait for a receipt which confirms that a transaction was mined by the network. Note:
If this method times out, the transaction may still be pending.
This defines the threshold above which a warning about the number of event listeners
attached to a provider which supports sockets subscriptions will be written to the
console.
You may see this warning if you call setProvider
on large numbers of Web3 contract objects.
Transaction fee history
Returns base fee per gas and transaction effective priority fee per gas history for
the requested block range if available.
The range between headBlock-4 and headBlock is guaranteed to be available while
retrieving data from the pending block and older
history are optional to support. For pre-EIP-1559 blocks the gas prices are returned
as rewards and zeroes are returned for the base fee per gas.
String|Number|BN|BigNumber
- Number of blocks in the requested range. Between 1 and 1024 blocks can be
requested in a single query. Less than requested may be returned if not all
blocks are available.
String|Number|BN|BigNumber
- Highest number block of the requested range.
Arrayofnumbers
- A monotonically increasing list of percentile values to sample from each
block’s effective priority fees per gas in ascending order, weighted by gas
used. Example: [“0”, “25”, “50”, “75”, “100”] or [“0”,
“0.5”, “1”, “1.5”, “3”, “80”]
Function
- (optional) Optional callback, returns an error object as first parameter
and the result as second.
Promise
returns Object
- Fee history for the returned block range. The object:
Number
oldestBlock - Lowest number block of the returned range.
Arrayofstrings
baseFeePerGas - An array of block base fees per gas. This includes the next
block after the newest of the returned range, because this value can be
derived from the newest block. Zeroes are returned for pre-EIP-1559 blocks.
Arrayofnumbers
gasUsedRatio - An array of block gas used ratios. These are calculated as
the ratio of gasUsed and gasLimit.
Arrayofstringarrays
reward - An array of effective priority fee per gas data points from a
single block. All zeroes are returned if the block is empty.
Number|String|BN|BigNumber
- The index position of the storage.
Number|String|BN|BigNumber
- (optional) If you pass this parameter it will not use the default block
set with web3.eth.defaultBlock. Pre-defined
block numbers as "earliest",
"latest"
and "pending"
can also be used.
Function
- (optional) Optional callback, returns an error object as first parameter
and the result as second.
Number|String|BN|BigNumber
- (optional) If you pass this parameter it will not use the default block
set with web3.eth.defaultBlock. Pre-defined
block numbers as "earliest",
"latest"
and "pending"
can also be used.
Function
- (optional) Optional callback, returns an error object as first parameter
and the result as second.
String|Number|BN|BigNumber
- The block number or block hash. Or the string "earliest",
"latest"
or "pending"
as in the default block parameter.
Boolean
- (optional, default false)
If specified true,
the returned block will contain all transactions as objects. If false
it will only contains the transaction hashes.
Function
- (optional) Optional callback, returns an error object as first parameter
and the result as second.
String|Number|BN|BigNumber
- The block number or hash. Or the string "earliest",
"latest"
or "pending"
as in the default block parameter.
Number
- The index position of the uncle.
Boolean
- (optional, default false)
If specified true,
the returned block will contain all transactions as objects. By default it
is false
so, there is no need to explictly specify false. And, if false
it will only contains the transaction hashes.
Function
- (optional) Optional callback, returns an error object as first parameter
and the result as second.
String
- The address to get the numbers of transactions from.
Number|String|BN|BigNumber
- (optional) If you pass this parameter it will not use the default block
set with web3.eth.defaultBlock. Pre-defined
block numbers as "earliest",
"latest"
and "pending"
can also be used.
Function
- (optional) Optional callback, returns an error object as first parameter
and the result as second.
from
- String|Number:
The address for the sending account. Uses the web3.eth.defaultAccount
property, if not specified. Or an address or index of a
local wallet in web3.eth.accounts.wallet.
to
- String:
(optional) The destination address of the message, left
undefined for a contract-creation transaction.
value
- Number|String|BN|BigNumber:
(optional) The value transferred for the transaction in
wei, also the
endowment if it’s a contract-creation transaction.
gas
- Number:
(optional, default: To-Be-Determined) The amount of gas to
use for the transaction (unused gas is refunded).
gasPrice
- Number|String|BN|BigNumber:
(optional) The price of gas for this transaction in wei, defaults to web3.eth.gasPrice.
type
- Number|String|BN|BigNumber:
(optional) A positive unsigned 8-bit number between 0 and
0x7f that represents the type of the transaction.
maxFeePerGas
- Number|String|BN:
(optional, defaulted to (2*block.baseFeePerGas)+maxPriorityFeePerGas)
The maximum fee per gas that the transaction is willing to
pay in total
maxPriorityFeePerGas
- Number|String|BN
(optional, defaulted to 1Gwei)
The maximum fee per gas to give miners to incentivize them
to include the transaction (Priority fee)
accessList
- Listofhexstrings
(optional) a list of addresses and storage keys that the
transaction plans to access
data
- String:
(optional) Either a ABI
byte string containing the data of the function call
on a contract, or in the case of a contract-creation
transaction the initialisation code.
nonce
- Number:
(optional) Integer of the nonce. This allows to overwrite
your own pending transactions that use the same nonce.
chain
- String:
(optional) Defaults to mainnet.
hardfork
- String:
(optional) Defaults to london.
common
- Object:
(optional) The common object
customChain
- Object:
The custom chain properties
name
- string:
(optional) The name of the
chain
networkId
- number:
Network ID of the custom
chain
chainId
- number:
Chain ID of the custom chain
baseChain
- string:
(optional) mainnet,
goerli,
kovan,
rinkeby,
or ropsten
hardfork
- string:
(optional) chainstart,
homestead,
dao,
tangerineWhistle,
spuriousDragon,
byzantium,
constantinople,
petersburg,
istanbul,
berlin,
or london
callback
- Function:
(optional) Optional callback, returns an error object as first parameter and
the result as second.
Note
The from
property can also be an address or index from the web3.eth.accounts.wallet. It will then
sign locally using the private key of that account, and send the transaction
via web3.eth.sendSignedTransaction(). If
the properties chain
and hardfork
or common
are not set, Web3 will try to set appropriate values by querying the network
for its chainId and networkId.
sending
returns payload:Object:
Fired immediately before transmitting the transaction request.
sent
returns payload:Object:
Fired immediately after the request body has been written to the client, but
before the transaction hash is received.
"transactionHash"
returns transactionHash:String:
Fired when the transaction hash is available.
"receipt"
returns receipt:Object:
Fired when the transaction receipt is available.
"confirmation"
returns confirmationNumber:Number,
receipt:Object,
latestBlockHash:String:
Fired for every confirmation up to the 12th confirmation. Receives the
confirmation number as the first and the receipt as the second argument. Fired
from confirmation 0 on, which is the block where it’s mined.
"error"
returns error:Error:
Fired if an error occurs during sending. If the transaction was rejected by the
network with a receipt, the receipt will be available as a property on the error
object.
Object
- A transaction object, see web3.eth.sendTransaction. For calls
the from
property is optional however it is highly recommended to explicitly set it
or it may default to address(0) depending on your node or
provider.
Number|String|BN|BigNumber
- (optional) If you pass this parameter it will not use the default block
set with web3.eth.defaultBlock. Pre-defined
block numbers as "earliest",
"latest"
and "pending"
can also be used.
Function
- (optional) Optional callback, returns an error object as first parameter
and the result as second.
Executes a message call or transaction and returns the amount of the gas used.
Note: You must specify a from
address otherwise you may experience odd behavior.
fromBlock
- Number|String:
The number of the earliest block ("latest"
may be given to mean the most recent and "pending"
currently mining, block). By default "latest".
toBlock
- Number|String:
The number of the latest block ("latest"
may be given to mean the most recent and "pending"
currently mining, block). By default "latest".
address
- String|Array:
An address or a list of addresses to only get logs from particular
account(s).
topics
- Array:
An array of values which must each appear in the log entries. The
order is important, if you want to leave topics out use null,
e.g. [null,'0x12...'].
You can also pass an array for each topic with options for that
topic e.g. [null,['option1','option2']]
This method will request/enable the accounts from the current environment. This
method will only work if you’re using the injected provider from a application like
Metamask, Status or TrustWallet. It doesn’t work if you’re connected to a node with
a default Web3.js provider (WebsocketProvider, HttpProvidder and IpcProvider).
balance
- String:
The balance of the account. See web3.eth.getBalance.
codeHash
- String:
hash of the code of the account. For a simple account without code
it will return "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470".
nonce
- String:
Nonce of the account.
storageHash
- String:
SHA3 of the StorageRoot. All storage will deliver a MerkleProof
starting with this rootHash.
accountProof
- String[]:Array
of rlp-serialized MerkleTree-Nodes, starting with the
stateRoot-Node, following the path of the SHA3 (address) as key.
storageProof
- Object[]
Array of storage-entries as requested.
String
- The subscription you want to subscribe to.
Mixed
- (optional) Optional additional parameters, depending on the subscription
type.
Function
- (optional) Optional callback, returns an error object as first parameter
and the result as second. Will be called for each incoming subscription, and
the subscription itself as the 3rd parameter.
String
- "pendingTransactions",
the type of the subscription.
Function
- (optional) Optional callback, returns an error object as first
parameter and the result as second. Will be called for each incoming
subscription.
String
- "newBlockHeaders",
the type of the subscription.
Function
- (optional) Optional callback, returns an error object as first
parameter and the result as second. Will be called for each incoming
subscription.
Function
- (optional) Optional callback, returns an error object as first
parameter and the result as second. Will be called for each incoming
subscription.
Subscribes to incoming logs, filtered by the given options.
If a valid numerical fromBlock
options property is set, Web3 will retrieve logs beginning from this point,
backfilling the response as necessary.
fromBlock
- Number:
The fromBlock
dictates at which block the subscription will start from, if it
is left empty, the default is latest
(a.k.a the chain head)
address
- String|Array:
An address or a list of addresses to only get logs from
particular account(s).
topics
- Array:
An array of values which must each appear in the log entries.
The order is important, if you want to leave topics out use
null,
e.g. [null,'0x00...'].
You can also pass another array for each topic with options for
that topic e.g. [null,['option1','option2']]
callback
- Function:
(optional) Optional callback, returns an error object as first parameter
and the result as second. Will be called for each incoming subscription.
The web3.eth.Contract
object makes it easy to interact with smart contracts on the ethereum
blockchain.
When you create a new contract object you give it the json interface of the
respective smart contract
and web3 will auto convert all calls into low level ABI calls over RPC for you.
This allows you to interact with smart contracts as if they were JavaScript
objects.
The transactionBlockTimeout
is used over socket-based connections. This option defines the amount of new
blocks it should wait until the first confirmation happens, otherwise the
PromiEvent rejects with a timeout error.
The transactionPollingTimeout
is used over HTTP connections. This option defines the number of seconds
Web3 will wait for a receipt which confirms that a transaction was mined by
the network. Note: If this method times out, the transaction may still be
pending.
Array
- arguments: The arguments passed to the method before. They can be
changed.
Function
- call: Will call the
“constant” method and execute its smart contract method in the EVM
without sending a transaction (Can’t alter the smart contract
state).
Function
- send: Will send a transaction
to the smart contract and execute its method (Can alter the smart
contract state).
Function
- estimateGas: Will estimate
the gas used when the method would be executed on chain. Note: You
must specify a from
address otherwise you may experience odd behavior.
Function
- encodeABI: Encodes the ABI
for this method. This can be send using a transaction, call the
method or passing into another smart contracts method as argument.
Will call a “constant” method and execute its smart contract method in the
EVM without sending any transaction. Note calling cannot alter the smart
contract state.
options
- Object
(optional): The options used for calling.
from
- String
(optional): The address the call “transaction”
should be made from. For calls the from
property is optional however it is highly
recommended to explicitly set it or it may default
to address(0) depending on your node or
provider.
gasPrice
- String
(optional): The gas price in wei to use for this
call “transaction”.
gas
- Number
(optional): The maximum gas provided for this call
“transaction” (gas limit).
defaultBlock
- Number|String|BN|BigNumber
(optional): If you pass this parameter it will not use the default
block set with contract.defaultBlock.
Pre-defined block numbers as "earliest",
"latest",
and "pending"
can also be used. Useful for requesting data from or replaying
transactions in past blocks.
callback
- Function
(optional): This callback will be fired with the result of the smart
contract method execution as the second argument, or with an error
object as the first argument.
Promise
returns Mixed:
The return value(s) of the smart contract method.
If it returns a single value, it’s returned as is. If it has multiple
return values they are returned as an object with properties and
indices:
The callback will return the 32 bytes transaction hash.
PromiEvent:
A promise combined event emitter.
Resolves when the transaction receipt is available, OR if this
send()
is called from a someContract.deploy(),
then the promise will resolve with the new contract instance.
Additionally the following events are available:
sending
returns payload:Object:
Fired immediately before transmitting the transaction request.
sent
returns payload:Object:
Fired immediately after the request body has been written to the
client, but before the transaction hash is received.
"transactionHash"
returns transactionHash:String:
Fired when the transaction hash is available.
"receipt"
returns receipt:Object:
Fired when the transaction receipt is available. Receipts
from contracts will have no logs
property, but instead an events
property with event names as keys and events as properties. See getPastEvents return values
for details about the returned event object.
"confirmation"
returns confirmation:Number,
receipt:Object,
latestBlockHash:String:
Fired for every confirmation up to the 24th confirmation.
"error"
returns error:Error:
Fired if an error occurs during sending. If the transaction was
rejected by the network with a receipt, the receipt will be
available as a property on the error object.
Will call to estimate the gas a method execution will take when executed in
the EVM.
The estimation can differ from the actual gas used when later sending a
transaction, as the state of the smart contract can be different at that
time.
Note: You must specify a from
address otherwise you may experience odd behavior.
options
- Object
(optional): The options used for calling.
from
- String
(optional): The address the call “transaction”
should be made from.
gas
- Number
(optional): The maximum gas provided for this call
“transaction” (gas limit). Setting a specific value
helps to detect out of gas errors. If all gas is
used it will return the same number.
value
- Number|String|BN|BigNumber
(optional): The value transferred for the call
“transaction” in wei.
callback
- Function
(optional): This callback will be fired with the result of the gas
estimation as the second argument, or with an error object as the
first argument.
Encodes the ABI for this method. The resulting hex string is 32-bit function
signature hash plus the passed parameters in Solidity tightly packed format.
This can be used to send a transaction, call a method, or pass it into
another smart contract’s method as arguments. Set the data
field on web3.eth.sendTransaction options as the
encodeABI() result and it is the same as calling the contract
method with contract.myMethod.send().
Some use cases for encodeABI() include: preparing a smart
contract transaction for a multisignature wallet, working with offline
wallets and cold storage and creating transaction payload for complex smart
contract proxy calls.
event
- String:
The name of the event in the contract, or "allEvents"
to get all events.
options
- Object
(optional): The options used for deployment.
filter
- Object
(optional): Lets you filter events by indexed
parameters, e.g. {filter:{myNumber:[12,13]}}
means all events where “myNumber” is 12 or 13.
topics
- Array
(optional): This allows you to manually set the
topics for the event filter. If given the filter
property and event signature, (topic[0]) will not be
set automatically. Each topic can also be a nested
array of topics that behaves as “or” operation
between the given nested topics.
callback
- Function:
This callback will be fired for the first event as the
second argument, or an error as the first argument. See getPastEvents return values
for details about the event structure.
options
- Object
(optional): The options used for deployment.
filter
- Object
(optional): Let you filter events by indexed
parameters, e.g. {filter:{myNumber:[12,13]}}
means all events where “myNumber” is 12 or 13.
fromBlock
- Number|String|BN|BigNumber
(optional): The block number (greater than or equal
to) from which to get events on. Pre-defined block
numbers as "earliest",
"latest"
and "pending"
can also be used. For specific range use getPastEvents.
topics
- Array
(optional): This allows to manually set the topics
for the event filter. If given the filter property
and event signature, (topic[0]) will not be set
automatically. Each topic can also be a nested array
of topics that behaves as “or” operation between the
given nested topics.
callback
- Function
(optional): This callback will be fired for each event as
the second argument, or an error as the first argument.
event
- String:
The name of the event in the contract, or "allEvents"
to get all events.
options
- Object
(optional): The options used for deployment.
filter
- Object
(optional): Lets you filter events by indexed
parameters, e.g. {filter:{myNumber:[12,13]}}
means all events where “myNumber” is 12 or 13.
fromBlock
- Number|String|BN|BigNumber
(optional): The block number (greater than or equal
to) from which to get events on. Pre-defined block
numbers as "earliest",
"latest"
and "pending"
can also be used.
toBlock
- Number|String|BN|BigNumber
(optional): The block number (less than or equal to)
to get events up to (Defaults to "latest").
Pre-defined block numbers as "earliest",
"latest"
and "pending"
can also be used.
topics
- Array
(optional): This allows manually setting the topics
for the event filter. If given the filter property
and event signature, (topic[0]) will not be set
automatically. Each topic can also be a nested array
of topics that behaves as “or” operation between the
given nested topics.
callback
- Function
(optional): This callback will be fired with an array of event logs
as the second argument, or an error as the first argument.