Web3Eth
The Web3Eth allows you to interact with an Ethereum blockchain.
For using Web3 Eth functions, first install Web3 package using npm i web3
or yarn add web3
based on your package manager usage.
After that, Web3 Eth functions will be available as mentioned in following snippet.
import { Web3 } from 'web3';
const web3 = new Web3('https://mainnet.infura.io/v3/<YOURPROJID>');
const block = await web3.eth.getBlock(0);
For using individual package install web3-eth
package using npm i web3-eth
or yarn add web3-eth
and only import required functions.
This is more efficient approach for building lightweight applications.
import { Web3Eth } from 'web3-eth';
const eth = new Web3Eth('https://mainnet.infura.io/v3/<YOURPROJID>');
const block = await eth.getBlock(0);
Hierarchy
-
Web3Context
<Web3EthExecutionAPI
,RegisteredSubscription
>↳
Web3Eth
Accessors
BatchRequest
• get
BatchRequest(): Object
Will return the Web3BatchRequest constructor.
Returns
Object
Inherited from
Web3Context.BatchRequest
blockHeaderTimeout
• get
blockHeaderTimeout(): number
The blockHeaderTimeout is used over socket-based connections. This option defines the amount seconds it should wait for 'newBlockHeaders'
event before falling back to polling to fetch transaction receipt.
Default is 10
seconds.
Returns
number
Inherited from
Web3Context.blockHeaderTimeout
• set
blockHeaderTimeout(val
): void
Will set the blockHeaderTimeout
Parameters
Name | Type |
---|---|
val | number |
Returns
void
Inherited from
Web3Context.blockHeaderTimeout
contractDataInputFill
• get
contractDataInputFill(): "data"
| "input"
| "both"
The contractDataInputFill
options property will allow you to set the hash of the method signature and encoded parameters to the property
either data
, input
or both within your contract.
This will affect the contracts send, call and estimateGas methods
Default is input
.
Returns
"data"
| "input"
| "both"
Inherited from
Web3Context.contractDataInputFill
• set
contractDataInputFill(val
): void
Will set the contractDataInputFill
Parameters
Name | Type |
---|---|
val | "data" | "input" | "both" |
Returns
void
Inherited from
Web3Context.contractDataInputFill
currentProvider
• get
currentProvider(): undefined
| Web3BaseProvider
<API
>
Will return the current provider. (The same as provider
)
Returns
undefined
| Web3BaseProvider
<API
>
Returns the current provider
Example
const web3Context = new Web3Context("http://localhost:8545");
console.log(web3Context.provider);
> HttpProvider {
clientUrl: 'http://localhost:8545',
httpProviderOptions: undefined
}
Inherited from
Web3Context.currentProvider
• set
currentProvider(provider
): void
Will set the current provider. (The same as provider
)
Parameters
Name | Type | Description |
---|---|---|
provider | undefined | string | LegacyRequestProvider | LegacySendProvider | LegacySendAsyncProvider | EIP1193Provider <API > | Web3BaseProvider <API > | SimpleProvider <API > | MetaMaskProvider <API > | SupportedProviders The provider to set |
Returns
void
Example
const web3Context = new Web3Context("http://localhost:8545");
web3Context.currentProvider = "ws://localhost:8545";
console.log(web3Context.provider);
> WebSocketProvider {
_eventEmitter: EventEmitter {
_events: [Object: null prototype] {},
_eventsCount: 0,
...
}
Inherited from
Web3Context.currentProvider
defaultAccount
• get
defaultAccount(): undefined
| string
This default address is used as the default from
property, if no from
property is specified in for the following methods:
- web3.eth.sendTransaction()
- web3.eth.call()
- myContract.methods.myMethod().call()
- myContract.methods.myMethod().send()
Returns
undefined
| string
Inherited from
Web3Context.defaultAccount
• set
defaultAccount(val
): void
Will set the default account.
Parameters
Name | Type |
---|---|
val | undefined | string |
Returns
void
Inherited from
Web3Context.defaultAccount
defaultBlock
• get
defaultBlock(): BlockNumberOrTag
The default block is used for certain methods. You can override it by passing in the defaultBlock as last parameter. The default value is "latest"
.
- web3.eth.getBalance()
- web3.eth.getCode()
- web3.eth.getTransactionCount()
- web3.eth.getStorageAt()
- web3.eth.call()
- myContract.methods.myMethod().call()
Returns
BlockNumberOrTag
Inherited from
Web3Context.defaultBlock
• set
defaultBlock(val
): void
Will set the default block.
- A block number
"earliest"
- String: The genesis block"latest"
- String: The latest block (current head of the blockchain)"pending"
- String: The currently mined block (including pending transactions)"finalized"
- String: (For POS networks) The finalized block is one which has been accepted as canonical by greater than 2/3 of validators"safe"
- String: (For POS networks) The safe head block is one which under normal network conditions, is expected to be included in the canonical chain. Under normal network conditions the safe head and the actual tip of the chain will be equivalent (with safe head trailing only by a few seconds). Safe heads will be less likely to be reorged than the proof of work network`s latest blocks.
Parameters
Name | Type |
---|---|
val | BlockNumberOrTag |
Returns
void
Inherited from
Web3Context.defaultBlock
defaultCommon
• get
defaultCommon(): undefined
| Common
Will get the default common property The default common property does contain the following Common object:
customChain
-Object
: The custom chain propertiesname
-string
: (optional) The name of the chainnetworkId
-number
: Network ID of the custom chainchainId
-number
: Chain ID of the custom chain
baseChain
-string
: (optional) mainnet, goerli, kovan, rinkeby, or ropstenhardfork
-string
: (optional) chainstart, homestead, dao, tangerineWhistle, spuriousDragon, byzantium, constantinople, petersburg, istanbul, berlin, or london Default isundefined
.
Returns
undefined
| Common
Inherited from
Web3Context.defaultCommon
• set
defaultCommon(val
): void
Will set the default common property
Parameters
Name | Type |
---|---|
val | undefined | Common |
Returns
void
Inherited from
Web3Context.defaultCommon
defaultHardfork
• get
defaultHardfork(): string
Will return the default hardfork. Default is london
The default hardfork property can be one of the following:
chainstart
homestead
dao
tangerineWhistle
spuriousDragon
byzantium
constantinople
petersburg
istanbul
berlin
london
- 'arrowGlacier',
- 'tangerineWhistle',
- 'muirGlacier'
Returns
string
Inherited from
Web3Context.defaultHardfork
• set
defaultHardfork(val
): void
Will set the default hardfork.
Parameters
Name | Type |
---|---|
val | string |
Returns
void
Inherited from
Web3Context.defaultHardfork
enableExperimentalFeatures
• get
enableExperimentalFeatures(): Object
The enableExperimentalFeatures is used to enable trying new experimental features that are still not fully implemented or not fully tested or still have some related issues.
Default is false
for every feature.
Returns
Object
Name | Type |
---|---|
useRpcCallSpecification | boolean |
useSubscriptionWhenCheckingBlockTimeout | boolean |
Inherited from
Web3Context.enableExperimentalFeatures
• set
enableExperimentalFeatures(val
): void
Will set the enableExperimentalFeatures
Parameters
Name | Type |
---|---|
val | Object |
val.useRpcCallSpecification | boolean |
val.useSubscriptionWhenCheckingBlockTimeout | boolean |
Returns
void
Inherited from
Web3Context.enableExperimentalFeatures
givenProvider
• get
givenProvider(): undefined
| LegacyRequestProvider
| LegacySendProvider
| LegacySendAsyncProvider
| EIP1193Provider
<never
> | Web3BaseProvider
<never
> | SimpleProvider
<never
> | MetaMaskProvider
<never
>
Will return the givenProvider if available.
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 undefined
.
Returns
undefined
| LegacyRequestProvider
| LegacySendProvider
| LegacySendAsyncProvider
| EIP1193Provider
<never
> | Web3BaseProvider
<never
> | SimpleProvider
<never
> | MetaMaskProvider
<never
>
Inherited from
Web3Context.givenProvider
handleRevert
• get
handleRevert(): boolean
The handleRevert
options property returns the revert reason string if enabled for the following methods:
- web3.eth.sendTransaction()
- web3.eth.call()
- myContract.methods.myMethod().call()
- myContract.methods.myMethod().send()
Default is
false
.
Note
: At the moment handleRevert
is only supported for sendTransaction
and not for sendSignedTransaction
Returns
boolean
Inherited from
Web3Context.handleRevert
• set
handleRevert(val
): void
Will set the handleRevert
Parameters
Name | Type |
---|---|
val | boolean |
Returns
void
Inherited from
Web3Context.handleRevert
provider
• get
provider(): undefined
| Web3BaseProvider
<API
>
Will return the current provider.
Returns
undefined
| Web3BaseProvider
<API
>
Returns the current provider
Example
const web3 = new Web3Context("http://localhost:8545");
console.log(web3.provider);
> HttpProvider {
clientUrl: 'http://localhost:8545',
httpProviderOptions: undefined
}
Inherited from
Web3Context.provider
• set
provider(provider
): void
Will set the current provider.
Parameters
Name | Type | Description |
---|---|---|
provider | undefined | string | LegacyRequestProvider | LegacySendProvider | LegacySendAsyncProvider | EIP1193Provider <API > | Web3BaseProvider <API > | SimpleProvider <API > | MetaMaskProvider <API > | The provider to set Accepted providers are of type SupportedProviders |
Returns
void
Example
const web3Context = new web3ContextContext("http://localhost:8545");
web3Context.provider = "ws://localhost:8545";
console.log(web3Context.provider);
> WebSocketProvider {
_eventEmitter: EventEmitter {
_events: [Object: null prototype] {},
_eventsCount: 0,
...
}
Inherited from
Web3Context.provider
subscriptionManager
• get
subscriptionManager(): Web3SubscriptionManager
<API
, RegisteredSubs
>
Will return the current subscriptionManager (Web3SubscriptionManager)
Returns
Web3SubscriptionManager
<API
, RegisteredSubs
>
Inherited from
Web3Context.subscriptionManager
transactionBlockTimeout
• get
transactionBlockTimeout(): number
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.
Default is 50
.
Returns
number
Inherited from
Web3Context.transactionBlockTimeout
• set
transactionBlockTimeout(val
): void
Will set the transactionBlockTimeout.
Parameters
Name | Type |
---|---|
val | number |
Returns
void
Inherited from
Web3Context.transactionBlockTimeout
transactionConfirmationBlocks
• get
transactionConfirmationBlocks(): number
This defines the number of blocks it requires until a transaction is considered confirmed.
Default is 24
.
Returns
number
Inherited from
Web3Context.transactionConfirmationBlocks
• set
transactionConfirmationBlocks(val
): void
Will set the transactionConfirmationBlocks.
Parameters
Name | Type |
---|---|
val | number |
Returns
void
Inherited from
Web3Context.transactionConfirmationBlocks
transactionPollingInterval
• get
transactionPollingInterval(): number
Used over HTTP connections. This option defines the number of seconds between Web3 calls for a receipt which confirms that a transaction was mined by the network.
Default is 1000
ms.
Returns
number
Inherited from
Web3Context.transactionPollingInterval
• set
transactionPollingInterval(val
): void
Will set the transactionPollingInterval.
Parameters
Name | Type |
---|---|
val | number |
Returns
void
Inherited from
Web3Context.transactionPollingInterval
transactionPollingTimeout
• get
transactionPollingTimeout(): number
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.
Default is 750
seconds (12.5 minutes).
Returns
number
Inherited from
Web3Context.transactionPollingTimeout
• set
transactionPollingTimeout(val
): void
Will set the transactionPollingTimeout.
Parameters
Name | Type |
---|---|
val | number |
Returns
void
Inherited from
Web3Context.transactionPollingTimeout
transactionReceiptPollingInterval
• get
transactionReceiptPollingInterval(): undefined
| number
The transactionPollingInterval
is used over HTTP connections. This option defines the number of seconds between Web3 calls for a receipt which confirms that a transaction was mined by the network.
Default is undefined
Returns
undefined
| number
Inherited from
Web3Context.transactionReceiptPollingInterval
• set
transactionReceiptPollingInterval(val
): void
Will set the transactionReceiptPollingInterval
Parameters
Name | Type |
---|---|
val | undefined | number |
Returns
void
Inherited from
Web3Context.transactionReceiptPollingInterval
transactionSendTimeout
• get
transactionSendTimeout(): number
The time used to wait for Ethereum Node to return the sent transaction result.
Note: If the RPC call stuck at the Node and therefor timed-out, the transaction may still be pending or even mined by the Network. We recommend checking the pending transactions in such a case.
Default is 750
seconds (12.5 minutes).
Returns
number
Inherited from
Web3Context.transactionSendTimeout
• set
transactionSendTimeout(val
): void
Will set the transactionSendTimeout.
Parameters
Name | Type |
---|---|
val | number |
Returns
void
Inherited from
Web3Context.transactionSendTimeout
Methods
calculateFeeData
▸ calculateFeeData(baseFeePerGasFactor?
, alternativeMaxPriorityFeePerGas?
): Promise
<FeeData
>
Calculates the current Fee Data.
If the node supports EIP-1559, then the maxFeePerGas
and maxPriorityFeePerGas
will be calculated.
If the node does not support EIP-1559, then the gasPrice
will be returned and the rest are null
s.
Parameters
Name | Type | Default value | Description |
---|---|---|---|
baseFeePerGasFactor | bigint | undefined | The factor to multiply the baseFeePerGas with, if the node supports EIP-1559. |
alternativeMaxPriorityFeePerGas | bigint | ethUnitMap.Gwei | The alternative maxPriorityFeePerGas to use, if the node supports EIP-1559, but does not support the method eth_maxPriorityFeePerGas . |
Returns
Promise
<FeeData
>
The current fee data.
web3.eth.getFeeData().then(console.log);
> {
gasPrice: 20000000000n,
maxFeePerGas: 20000000000n,
maxPriorityFeePerGas: 20000000000n,
baseFeePerGas: 20000000000n
}
web3.eth.getFeeData(ethUnitMap.Gwei, 2n).then(console.log);
> {
gasPrice: 20000000000n,
maxFeePerGas: 40000000000n,
maxPriorityFeePerGas: 20000000000n,
baseFeePerGas: 20000000000n
}
call
▸ call<ReturnFormat
>(transaction
, blockNumber?
, returnFormat?
): Promise
<ByteTypes
[ReturnFormat
["bytes"
]]>
Executes a message call within the EVM without creating a transaction. It does not publish anything to the blockchain and does not consume any gas.
Type parameters
Name | Type |
---|---|
ReturnFormat | extends DataFormat = { bytes : HEX ; number : BIGINT } |
Parameters
Name | Type | Description |
---|---|---|
transaction | TransactionCall | A transaction object where all properties are optional except to , however it's recommended to include the from property or it may default to 0x0000000000000000000000000000000000000000 depending on your node or provider. |
blockNumber | BlockNumberOrTag | (BlockNumberOrTag defaults to Web3Eth.defaultBlock) - Specifies what block to use as the current state of the blockchain while processing the transaction. |
returnFormat | ReturnFormat | (DataFormat defaults to DEFAULT_RETURN_FORMAT) - Specifies how the return data from the call should be formatted. |
Returns
Promise
<ByteTypes
[ReturnFormat
["bytes"
]]>
The returned data of the call, e.g. a smart contract function's return value.
clearSubscriptions
▸ clearSubscriptions(notClearSyncing?
): undefined
| Promise
<string
[]>
Resets subscriptions.
Parameters
Name | Type | Default value | Description |
---|---|---|---|
notClearSyncing | boolean | false | If true it keeps the syncing subscription. |
Returns
undefined
| Promise
<string
[]>
A promise to an array of subscription ids that were cleared.
web3.eth.clearSubscriptions().then(console.log);
> [...] An array of subscription ids that were cleared
createAccessList
▸ createAccessList<ReturnFormat
>(transaction
, blockNumber?
, returnFormat?
): Promise
<{ accessList?
: { address?
: string
; storageKeys?
: string
[] }[] ; gasUsed?
: NumberTypes
[ReturnFormat
["number"
]] }>
This method generates an access list for a transaction.
Type parameters
Name | Type |
---|---|
ReturnFormat | extends DataFormat = { bytes : HEX ; number : BIGINT } |
Parameters
Name | Type | Description |
---|---|---|
transaction | TransactionForAccessList | A transaction object where all properties are optional except from , however it's recommended to include the to property. |
blockNumber | BlockNumberOrTag | (BlockNumberOrTag defaults to Web3Eth.defaultBlock) - Specifies what block to use as the current state of the blockchain while processing the transaction. |
returnFormat | ReturnFormat | (DataFormat defaults to DEFAULT_RETURN_FORMAT) - Specifies how the return data from the createAccessList should be formatted. |
Returns
Promise
<{ accessList?
: { address?
: string
; storageKeys?
: string
[] }[] ; gasUsed?
: NumberTypes
[ReturnFormat
["number"
]] }>
The returned data of the createAccessList, e.g. The generated access list for transaction.
Example
web3.eth.createAccessList({
from: '0xDe95305a63302C3aa4d3A9B42654659AeA72b694',
data: '0x9a67c8b100000000000000000000000000000000000000000000000000000000000004d0',
gasPrice: '0x3b9aca00',
gas: '0x3d0900',
to: '0x940b25304947ae863568B3804434EC77E2160b87'
})
.then(console.log);
> {
"accessList": [
{
"address": "0x15859bdf5aff2080a9968f6a410361e9598df62f",
"storageKeys": [
"0x0000000000000000000000000000000000000000000000000000000000000000"
]
}
],
"gasUsed": "0x7671"
}
estimateGas
▸ estimateGas<ReturnFormat
>(transaction
, blockNumber?
, returnFormat?
): Promise
<NumberTypes
[ReturnFormat
["number"
]]>
Simulates the transaction within the EVM to estimate the amount of gas to be used by the transaction. The transaction will not be added to the blockchain, and actual gas usage can vary when interacting with a contract as a result of updating the contract's state.
Type parameters
Name | Type |
---|---|
ReturnFormat | extends DataFormat = { bytes : HEX ; number : BIGINT } |
Parameters
Name | Type | Description |
---|---|---|
transaction | Transaction | The Transaction object to estimate the gas for. |
blockNumber | BlockNumberOrTag | (BlockNumberOrTag defaults to Web3Eth.defaultBlock) - Specifies what block to use as the current state of the blockchain while processing the gas estimation. |
returnFormat | ReturnFormat | (DataFormat defaults to DEFAULT_RETURN_FORMAT) - Specifies how the return data from the call should be formatted. |
Returns
Promise
<NumberTypes
[ReturnFormat
["number"
]]>
The used gas for the simulated transaction execution.
const transaction = {
from: '0xe899f0130FD099c0b896B2cE4E5E15A25b23139a',
to: '0xe899f0130FD099c0b896B2cE4E5E15A25b23139a',
value: '0x1',
nonce: '0x1',
type: '0x0'
}
web3.eth.estimateGas(transaction).then(console.log);
> 21000n
web3.eth.estimateGas(transaction, { number: FMT_NUMBER.NUMBER , bytes: FMT_BYTES.HEX }).then(console.log);
> 21000
extend
▸ extend(extendObj
): this
This method allows extending the web3 modules. Note: This method is only for backward compatibility, and It is recommended to use Web3 v4 Plugin feature for extending web3.js functionality if you are developing something new.
Parameters
Name | Type |
---|---|
extendObj | ExtensionObject |
Returns
this
Inherited from
Web3Context.extend
getAccounts
▸ getAccounts(): Promise
<string
[]>
Returns
Promise
<string
[]>
A list of accounts the node controls (addresses are checksummed).
web3.eth.getAccounts().then(console.log);
> ["0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe", "0xDCc6960376d6C6dEa93647383FfB245CfCed97Cf"]
getBalance
▸ getBalance<ReturnFormat
>(address
, blockNumber?
, returnFormat?
): Promise
<NumberTypes
[ReturnFormat
["number"
]]>
Get the balance of an address at a given block.
Type parameters
Name | Type |
---|---|
ReturnFormat | extends DataFormat = { bytes : HEX ; number : BIGINT } |
Parameters
Name | Type | Description |
---|---|---|
address | string | The address to get the balance of. |
blockNumber | BlockNumberOrTag | (BlockNumberOrTag defaults to Web3Eth.defaultBlock) Specifies what block to use as the current state for the balance query. |
returnFormat | ReturnFormat | (DataFormat defaults to DEFAULT_RETURN_FORMAT) Specifies how the return data should be formatted. |
Returns
Promise
<NumberTypes
[ReturnFormat
["number"
]]>
The current balance for the given address in wei
.
web3.eth.getBalance("0x407d73d8a49eeb85d32cf465507dd71d507100c1").then(console.log);
> 1000000000000n
web3.eth.getBalance("0x407d73d8a49eeb85d32cf465507dd71d507100c1").then(console.log);
> "0xe8d4a51000"
getBlock
▸ getBlock<ReturnFormat
>(block?
, hydrated?
, returnFormat?
): Promise
<{ baseFeePerGas?
: NumberTypes
[ReturnFormat
["number"
]] ; difficulty?
: NumberTypes
[ReturnFormat
["number"
]] ; extraData
: ByteTypes
[ReturnFormat
["bytes"
]] ; gasLimit
: NumberTypes
[ReturnFormat
["number"
]] ; gasUsed
: NumberTypes
[ReturnFormat
["number"
]] ; hash?
: ByteTypes
[ReturnFormat
["bytes"
]] ; logsBloom?
: ByteTypes
[ReturnFormat
["bytes"
]] ; miner
: ByteTypes
[ReturnFormat
["bytes"
]] ; mixHash
: ByteTypes
[ReturnFormat
["bytes"
]] ; nonce
: NumberTypes
[ReturnFormat
["number"
]] ; number
: NumberTypes
[ReturnFormat
["number"
]] ; parentHash
: ByteTypes
[ReturnFormat
["bytes"
]] ; receiptsRoot
: ByteTypes
[ReturnFormat
["bytes"
]] ; sha3Uncles
: ByteTypes
[ReturnFormat
["bytes"
]] ; size
: NumberTypes
[ReturnFormat
["number"
]] ; stateRoot
: ByteTypes
[ReturnFormat
["bytes"
]] ; timestamp
: NumberTypes
[ReturnFormat
["number"
]] ; totalDifficulty
: NumberTypes
[ReturnFormat
["number"
]] ; transactions
: string
[] | { accessList?
: { address?
: string
; storageKeys?
: string
[] }[] ; blockHash?
: ByteTypes
[ReturnFormat
["bytes"
]] ; blockNumber?
: NumberTypes
[ReturnFormat
["number"
]] ; chain?
: "mainnet"
| "goerli"
| "kovan"
| "rinkeby"
| "ropsten"
| "sepolia"
; chainId?
: NumberTypes
[ReturnFormat
["number"
]] ; common?
: { baseChain?
: "mainnet"
| "goerli"
| "kovan"
| "rinkeby"
| "ropsten"
| "sepolia"
; customChain
: { chainId
: NumberTypes
[ReturnFormat
["number"
]] ; name?
: string
; networkId
: NumberTypes
[ReturnFormat
["number"
]] } ; hardfork?
: "london"
| "chainstart"
| "frontier"
| "homestead"
| "dao"
| "tangerineWhistle"
| "spuriousDragon"
| "byzantium"
| "constantinople"
| "petersburg"
| "istanbul"
| "muirGlacier"
| "berlin"
| "altair"
| "arrowGlacier"
| "grayGlacier"
| "bellatrix"
| "merge"
| "capella"
| "shanghai"
} ; data?
: ByteTypes
[ReturnFormat
["bytes"
]] ; from
: string
; gas?
: NumberTypes
[ReturnFormat
["number"
]] ; gasLimit?
: NumberTypes
[ReturnFormat
["number"
]] ; gasPrice?
: NumberTypes
[ReturnFormat
["number"
]] ; hardfork?
: "london"
| "chainstart"
| "frontier"
| "homestead"
| "dao"
| "tangerineWhistle"
| "spuriousDragon"
| "byzantium"
| "constantinople"
| "petersburg"
| "istanbul"
| "muirGlacier"
| "berlin"
| "altair"
| "arrowGlacier"
| "grayGlacier"
| "bellatrix"
| "merge"
| "capella"
| "shanghai"
; hash
: ByteTypes
[ReturnFormat
["bytes"
]] ; input?
: ByteTypes
[ReturnFormat
["bytes"
]] ; maxFeePerGas?
: NumberTypes
[ReturnFormat
["number"
]] ; maxPriorityFeePerGas?
: NumberTypes
[ReturnFormat
["number"
]] ; networkId?
: NumberTypes
[ReturnFormat
["number"
]] ; nonce?
: NumberTypes
[ReturnFormat
["number"
]] ; r?
: ByteTypes
[ReturnFormat
["bytes"
]] ; s?
: ByteTypes
[ReturnFormat
["bytes"
]] ; to?
: null
| string
; transactionIndex?
: NumberTypes
[ReturnFormat
["number"
]] ; type?
: NumberTypes
[ReturnFormat
["number"
]] ; v?
: NumberTypes
[ReturnFormat
["number"
]] ; value?
: NumberTypes
[ReturnFormat
["number"
]] ; yParity?
: string
}[] ; transactionsRoot
: ByteTypes
[ReturnFormat
["bytes"
]] ; uncles
: string
[] }>
Retrieves a Block matching the provided block number, block hash or block tag.
Type parameters
Name | Type |
---|---|
ReturnFormat | extends DataFormat = { bytes : HEX ; number : BIGINT } |
Parameters
Name | Type | Default value | Description |
---|---|---|---|
block | BlockNumberOrTag | undefined | The BlockNumberOrTag (defaults to Web3Eth.defaultBlock) or block hash of the desired block. |
hydrated | boolean | false | If specified true , the returned block will contain all transactions as objects. If false it will only contain transaction hashes. |
returnFormat | ReturnFormat | undefined | (DataFormat defaults to DEFAULT_RETURN_FORMAT) Specifies how the return data should be formatted (does not format transaction objects or hashes). |
Returns
Promise
<{ baseFeePerGas?
: NumberTypes
[ReturnFormat
["number"
]] ; difficulty?
: NumberTypes
[ReturnFormat
["number"
]] ; extraData
: ByteTypes
[ReturnFormat
["bytes"
]] ; gasLimit
: NumberTypes
[ReturnFormat
["number"
]] ; gasUsed
: NumberTypes
[ReturnFormat
["number"
]] ; hash?
: ByteTypes
[ReturnFormat
["bytes"
]] ; logsBloom?
: ByteTypes
[ReturnFormat
["bytes"
]] ; miner
: ByteTypes
[ReturnFormat
["bytes"
]] ; mixHash
: ByteTypes
[ReturnFormat
["bytes"
]] ; nonce
: NumberTypes
[ReturnFormat
["number"
]] ; number
: NumberTypes
[ReturnFormat
["number"
]] ; parentHash
: ByteTypes
[ReturnFormat
["bytes"
]] ; receiptsRoot
: ByteTypes
[ReturnFormat
["bytes"
]] ; sha3Uncles
: ByteTypes
[ReturnFormat
["bytes"
]] ; size
: NumberTypes
[ReturnFormat
["number"
]] ; stateRoot
: ByteTypes
[ReturnFormat
["bytes"
]] ; timestamp
: NumberTypes
[ReturnFormat
["number"
]] ; totalDifficulty
: NumberTypes
[ReturnFormat
["number"
]] ; transactions
: string
[] | { accessList?
: { address?
: string
; storageKeys?
: string
[] }[] ; blockHash?
: ByteTypes
[ReturnFormat
["bytes"
]] ; blockNumber?
: NumberTypes
[ReturnFormat
["number"
]] ; chain?
: "mainnet"
| "goerli"
| "kovan"
| "rinkeby"
| "ropsten"
| "sepolia"
; chainId?
: NumberTypes
[ReturnFormat
["number"
]] ; common?
: { baseChain?
: "mainnet"
| "goerli"
| "kovan"
| "rinkeby"
| "ropsten"
| "sepolia"
; customChain
: { chainId
: NumberTypes
[ReturnFormat
["number"
]] ; name?
: string
; networkId
: NumberTypes
[ReturnFormat
["number"
]] } ; hardfork?
: "london"
| "chainstart"
| "frontier"
| "homestead"
| "dao"
| "tangerineWhistle"
| "spuriousDragon"
| "byzantium"
| "constantinople"
| "petersburg"
| "istanbul"
| "muirGlacier"
| "berlin"
| "altair"
| "arrowGlacier"
| "grayGlacier"
| "bellatrix"
| "merge"
| "capella"
| "shanghai"
} ; data?
: ByteTypes
[ReturnFormat
["bytes"
]] ; from
: string
; gas?
: NumberTypes
[ReturnFormat
["number"
]] ; gasLimit?
: NumberTypes
[ReturnFormat
["number"
]] ; gasPrice?
: NumberTypes
[ReturnFormat
["number"
]] ; hardfork?
: "london"
| "chainstart"
| "frontier"
| "homestead"
| "dao"
| "tangerineWhistle"
| "spuriousDragon"
| "byzantium"
| "constantinople"
| "petersburg"
| "istanbul"
| "muirGlacier"
| "berlin"
| "altair"
| "arrowGlacier"
| "grayGlacier"
| "bellatrix"
| "merge"
| "capella"
| "shanghai"
; hash
: ByteTypes
[ReturnFormat
["bytes"
]] ; input?
: ByteTypes
[ReturnFormat
["bytes"
]] ; maxFeePerGas?
: NumberTypes
[ReturnFormat
["number"
]] ; maxPriorityFeePerGas?
: NumberTypes
[ReturnFormat
["number"
]] ; networkId?
: NumberTypes
[ReturnFormat
["number"
]] ; nonce?
: NumberTypes
[ReturnFormat
["number"
]] ; r?
: ByteTypes
[ReturnFormat
["bytes"
]] ; s?
: ByteTypes
[ReturnFormat
["bytes"
]] ; to?
: null
| string
; transactionIndex?
: NumberTypes
[ReturnFormat
["number"
]] ; type?
: NumberTypes
[ReturnFormat
["number"
]] ; v?
: NumberTypes
[ReturnFormat
["number"
]] ; value?
: NumberTypes
[ReturnFormat
["number"
]] ; yParity?
: string
}[] ; transactionsRoot
: ByteTypes
[ReturnFormat
["bytes"
]] ; uncles
: string
[] }>
A Block object matching the provided block number or block hash.
web3.eth.getBlock(0).then(console.log);
> {
hash: '0x7dbfdc6a7a67a670cb9b0c3f81ca60c007762f1e4e598cb027a470678ff26d0d',
parentHash: '0x0000000000000000000000000000000000000000000000000000000000000000',
sha3Uncles: '0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347',
miner: '0x0000000000000000000000000000000000000000',
stateRoot: '0x5ed9882897d363c4632a6e67fba6203df61bd994813dcf048da59be442a9c6c4',
transactionsRoot: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421',
receiptsRoot: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421',
logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
difficulty: 1n,
number: 0n,
gasLimit: 30000000n,
gasUsed: 0n,
timestamp: 1658281638n,
extraData: '0x',
mixHash: '0x0000000000000000000000000000000000000000000000000000000000000000',
nonce: 0n,
totalDifficulty: 1n,
baseFeePerGas: 1000000000n,
size: 514n,
transactions: [],
uncles: []
}
web3.eth.getBlock(
"0x7dbfdc6a7a67a670cb9b0c3f81ca60c007762f1e4e598cb027a470678ff26d0d",
false,
{ number: FMT_NUMBER.NUMBER , bytes: FMT_BYTES.HEX }
).then(console.log);
> {
hash: '0x7dbfdc6a7a67a670cb9b0c3f81ca60c007762f1e4e598cb027a470678ff26d0d',
parentHash: '0x0000000000000000000000000000000000000000000000000000000000000000',
sha3Uncles: '0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347',
miner: '0x0000000000000000000000000000000000000000',
stateRoot: '0x5ed9882897d363c4632a6e67fba6203df61bd994813dcf048da59be442a9c6c4',
transactionsRoot: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421',
receiptsRoot: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421',
logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
difficulty: 1,
number: 0,
gasLimit: 30000000,
gasUsed: 0,
timestamp: 1658281638,
extraData: '0x',
mixHash: '0x0000000000000000000000000000000000000000000000000000000000000000',
nonce: 0,
totalDifficulty: 1,
baseFeePerGas: 1000000000,
size: 514,
transactions: [],
uncles: []
}
getBlockNumber
▸ getBlockNumber<ReturnFormat
>(returnFormat?
): Promise
<NumberTypes
[ReturnFormat
["number"
]]>
Type parameters
Name | Type |
---|---|
ReturnFormat | extends DataFormat = { bytes : HEX ; number : BIGINT } |
Parameters
Name | Type | Description |
---|---|---|
returnFormat | ReturnFormat | (DataFormat defaults to DEFAULT_RETURN_FORMAT) Specifies how the return data should be formatted. |
Returns
Promise
<NumberTypes
[ReturnFormat
["number"
]]>
The current block number.
web3.eth.getBlockNumber().then(console.log);
> 2744n
web3.eth.getBlockNumber({ number: FMT_NUMBER.HEX , bytes: FMT_BYTES.HEX }).then(console.log);
> "0xab8"
getBlockTransactionCount
▸ getBlockTransactionCount<ReturnFormat
>(block?
, returnFormat?
): Promise
<NumberTypes
[ReturnFormat
["number"
]]>
Type parameters
Name | Type |
---|---|
ReturnFormat | extends DataFormat = { bytes : HEX ; number : BIGINT } |
Parameters
Name | Type | Description |
---|---|---|
block | BlockNumberOrTag | The BlockNumberOrTag (defaults to Web3Eth.defaultBlock) or block hash of the desired block. |
returnFormat | ReturnFormat | (DataFormat defaults to DEFAULT_RETURN_FORMAT) Specifies how the return data should be formatted. |
Returns
Promise
<NumberTypes
[ReturnFormat
["number"
]]>
The number of transactions in the provided block.
web3.eth.getBlockTransactionCount("0x407d73d8a49eeb85d32cf465507dd71d507100c1").then(console.log);
> 1n
web3.eth.getBlockTransactionCount(
"0x407d73d8a49eeb85d32cf465507dd71d507100c1",
{ number: FMT_NUMBER.NUMBER , bytes: FMT_BYTES.HEX }
).then(console.log);
> 1
getBlockUncleCount
▸ getBlockUncleCount<ReturnFormat
>(block?
, returnFormat?
): Promise
<NumberTypes
[ReturnFormat
["number"
]]>
Type parameters
Name | Type |
---|---|
ReturnFormat | extends DataFormat = { bytes : HEX ; number : BIGINT } |
Parameters
Name | Type | Description |
---|---|---|
block | BlockNumberOrTag | The BlockNumberOrTag (defaults to Web3Eth.defaultBlock) or block hash of the desired block. |
returnFormat | ReturnFormat | (DataFormat defaults to DEFAULT_RETURN_FORMAT) Specifies how the return data should be formatted. |
Returns
Promise
<NumberTypes
[ReturnFormat
["number"
]]>
The number of uncles in the provided block.
web3.eth.getBlockUncleCount("0x407d73d8a49eeb85d32cf465507dd71d507100c1").then(console.log);
> 1n
web3.eth.getBlockUncleCount(
"0x407d73d8a49eeb85d32cf465507dd71d507100c1",
{ number: FMT_NUMBER.NUMBER , bytes: FMT_BYTES.HEX }
).then(console.log);
> 1
getChainId
▸ getChainId<ReturnFormat
>(returnFormat?
): Promise
<NumberTypes
[ReturnFormat
["number"
]]>
Type parameters
Name | Type |
---|---|
ReturnFormat | extends DataFormat = { bytes : HEX ; number : BIGINT } |
Parameters
Name | Type | Description |
---|---|---|
returnFormat | ReturnFormat | (DataFormat defaults to DEFAULT_RETURN_FORMAT) - Specifies how the return data from the call should be formatted. |
Returns
Promise
<NumberTypes
[ReturnFormat
["number"
]]>
The chain ID of the current connected node as described in the EIP-695.
web3.eth.getChainId().then(console.log);
> 61n
web3.eth.getChainId({ number: FMT_NUMBER.NUMBER , bytes: FMT_BYTES.HEX }).then(console.log);
> 61
getCode
▸ getCode<ReturnFormat
>(address
, blockNumber?
, returnFormat?
): Promise
<ByteTypes
[ReturnFormat
["bytes"
]]>
Get the code at a specific address.