Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Commit 6311abe

Browse files
Merge block tags support 5199 (#5410)
* formatter with POS block tags support * doc update for POS blocktags * POS tags in types * compareBlockNumbers with finalized tag support * test cases for compareBlockNumbers * change log update * eth call tests with new block tags * balance tests with new block tags * get block tests with new block tags * tx count tests with new block tags * get code tests with new block tags * get proof tests with new block tags * get storage tests with new block tags * formatter tests with new block tags * log formatter tests with new block tags * change log update * Update packages/web3-utils/src/index.js * return undefined for safe vs non zero number * additional test cases * Update packages/web3-utils/src/index.js Co-authored-by: Muhammad Altabba <[email protected]>
1 parent bd99127 commit 6311abe

16 files changed

+351
-39
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -600,3 +600,4 @@ Released with 1.0.0-beta.37 code base.
600600

601601
### Added
602602
- Add missing function type "receive" in `AbiType` (#5165)
603+
- Support of `safe` and `finalized` block tags added (#5410)

docs/web3-eth-contract.rst

+7-5
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ The default block parameters can be one of the following:
144144
- ``"earliest"`` - ``String``: The genesis block
145145
- ``"latest"`` - ``String``: The latest block (current head of the blockchain)
146146
- ``"pending"`` - ``String``: The currently mined block (including pending transactions)
147+
- ``"finalized"`` - ``String``: (For POS networks) The finalized block is one which has been accepted as canonical by >2/3 of validators.
148+
- ``"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 networks latest blocks.
147149

148150
Default is ``"latest"``.
149151

@@ -792,7 +794,7 @@ Parameters
792794
* ``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.
793795
* ``gasPrice`` - ``String`` (optional): The gas price in wei to use for this call "transaction".
794796
* ``gas`` - ``Number`` (optional): The maximum gas provided for this call "transaction" (gas limit).
795-
2. ``defaultBlock`` - ``Number|String|BN|BigNumber`` (optional): If you pass this parameter it will not use the default block set with :ref:`contract.defaultBlock <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.
797+
2. ``defaultBlock`` - ``Number|String|BN|BigNumber`` (optional): If you pass this parameter it will not use the default block set with :ref:`contract.defaultBlock <defaultblock>`. Pre-defined block numbers as ``"earliest"``, ``"latest"``, ``"pending"``, ``"safe"`` or ``"finalized"`` can also be used. Useful for requesting data from or replaying transactions in past blocks.
796798
3. ``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.
797799

798800
-------
@@ -1083,7 +1085,7 @@ Parameters
10831085
1. ``options`` - ``Object``: The options used for calling.
10841086
* ``from`` - ``String``: The address the call "transaction" should be made from.
10851087
* ``gas`` - ``Number`` (optional): The maximum gas provided for this call "transaction" (gas limit). Setting a specific value helps to detect out of gas errors. Access list response will return amount of gas used.
1086-
2. ``block`` - ``String|Number|BN|BigNumber`` (optional): The block number or hash. Or the string ``"earliest"``, ``"latest"`` or ``"pending"`` as in the :ref:`default block parameter <eth-defaultblock>`.
1088+
2. ``block`` - ``String|Number|BN|BigNumber`` (optional): The block number or hash. Or the string ``"earliest"``, ``"latest"`` , ``"pending"`` , ``"safe"`` or ``"finalized"`` as in the :ref:`default block parameter <eth-defaultblock>`.
10871089
3. ``callback`` - ``Function`` (optional): This callback will be fired with the result of the access list generation as the second argument, or with an error object as the first argument.
10881090

10891091
-------
@@ -1215,7 +1217,7 @@ Parameters
12151217

12161218
1. ``options`` - ``Object`` (optional): The options used for deployment.
12171219
* ``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.
1218-
* ``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 :ref:`getPastEvents <getPastEvents>`.
1220+
* ``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"`` , ``"pending"`` , ``"safe"`` or ``"finalized"`` can also be used. For specific range use :ref:`getPastEvents <getPastEvents>`.
12191221
* ``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.
12201222
2. ``callback`` - ``Function`` (optional): This callback will be fired for each *event* as the second argument, or an error as the first argument.
12211223

@@ -1324,8 +1326,8 @@ Parameters
13241326
1. ``event`` - ``String``: The name of the event in the contract, or ``"allEvents"`` to get all events.
13251327
2. ``options`` - ``Object`` (optional): The options used for deployment.
13261328
* ``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.
1327-
* ``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.
1328-
* ``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.
1329+
* ``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"`` , ``"pending"`` , ``"safe"`` or ``"finalized"`` can also be used.
1330+
* ``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"`` , ``"pending"`` , ``"safe"`` or ``"finalized"`` can also be used.
13291331
* ``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.
13301332
3. ``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.
13311333

docs/web3-eth.rst

+16-14
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,8 @@ Default block parameters can be one of the following:
191191
- ``"earliest"`` - ``String``: The genesis block
192192
- ``"latest"`` - ``String``: The latest block (current head of the blockchain)
193193
- ``"pending"`` - ``String``: The currently mined block (including pending transactions)
194+
- ``"finalized"`` - ``String``: (For POS networks) The finalized block is one which has been accepted as canonical by >2/3 of validators.
195+
- ``"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 networks latest blocks.
194196

195197
Default is ``"latest"``
196198

@@ -884,7 +886,7 @@ Parameters
884886
----------
885887

886888
1. ``String`` - The address to get the balance of.
887-
2. ``Number|String|BN|BigNumber`` - (optional) If you pass this parameter it will not use the default block set with :ref:`web3.eth.defaultBlock <eth-defaultblock>`. Pre-defined block numbers as ``"earliest"``, ``"latest"`` and ``"pending"`` can also be used.
889+
2. ``Number|String|BN|BigNumber`` - (optional) If you pass this parameter it will not use the default block set with :ref:`web3.eth.defaultBlock <eth-defaultblock>`. Pre-defined block numbers as ``"earliest"``, ``"latest"`` , ``"pending"``, ``"safe"`` or ``"finalized"`` can also be used.
888890
3. ``Function`` - (optional) Optional callback, returns an error object as first parameter and the result as second.
889891

890892
-------
@@ -925,7 +927,7 @@ Parameters
925927

926928
1. ``String`` - The address to get the storage from.
927929
2. ``Number|String|BN|BigNumber`` - The index position of the storage.
928-
3. ``Number|String|BN|BigNumber`` - (optional) If you pass this parameter it will not use the default block set with :ref:`web3.eth.defaultBlock <eth-defaultblock>`. Pre-defined block numbers as ``"earliest"``, ``"latest"`` and ``"pending"`` can also be used.
930+
3. ``Number|String|BN|BigNumber`` - (optional) If you pass this parameter it will not use the default block set with :ref:`web3.eth.defaultBlock <eth-defaultblock>`. Pre-defined block numbers as ``"earliest"``, ``"latest"`` , ``"pending"``, ``"safe"`` or ``"finalized"`` can also be used.
929931
4. ``Function`` - (optional) Optional callback, returns an error object as first parameter and the result as second.
930932

931933

@@ -963,7 +965,7 @@ Parameters
963965
----------
964966

965967
1. ``String`` - The address to get the code from.
966-
2. ``Number|String|BN|BigNumber`` - (optional) If you pass this parameter it will not use the default block set with :ref:`web3.eth.defaultBlock <eth-defaultblock>`. Pre-defined block numbers as ``"earliest"``, ``"latest"`` and ``"pending"`` can also be used.
968+
2. ``Number|String|BN|BigNumber`` - (optional) If you pass this parameter it will not use the default block set with :ref:`web3.eth.defaultBlock <eth-defaultblock>`. Pre-defined block numbers as ``"earliest"``, ``"latest"`` , ``"pending"``, ``"safe"`` or ``"finalized"`` can also be used.
967969
3. ``Function`` - (optional) Optional callback, returns an error object as first parameter and the result as second.
968970

969971
-------
@@ -1002,7 +1004,7 @@ Returns a block matching the block number or block hash.
10021004
Parameters
10031005
----------
10041006

1005-
1. ``String|Number|BN|BigNumber`` - The block number or block hash. Or the string ``"earliest"``, ``"latest"`` or ``"pending"`` as in the :ref:`default block parameter <eth-defaultblock>`.
1007+
1. ``String|Number|BN|BigNumber`` - The block number or block hash. Or the string ``"earliest"``, ``"latest"`` , ``"pending"``, ``"safe"`` or ``"finalized"`` as in the :ref:`default block parameter <eth-defaultblock>`.
10061008
2. ``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.
10071009
3. ``Function`` - (optional) Optional callback, returns an error object as first parameter and the result as second.
10081010

@@ -1085,7 +1087,7 @@ Parameters
10851087
----------
10861088

10871089

1088-
1. ``String|Number|BN|BigNumber`` - The block number or hash. Or the string ``"earliest"``, ``"latest"`` or ``"pending"`` as in the :ref:`default block parameter <eth-defaultblock>`.
1090+
1. ``String|Number|BN|BigNumber`` - The block number or hash. Or the string ``"earliest"``, ``"latest"`` , ``"pending"``, ``"safe"`` or ``"finalized"`` as in the :ref:`default block parameter <eth-defaultblock>`.
10891091
2. ``Function`` - (optional) Optional callback, returns an error object as first parameter and the result as second.
10901092

10911093
-------
@@ -1125,7 +1127,7 @@ Parameters
11251127
----------
11261128

11271129

1128-
1. ``String|Number|BN|BigNumber`` - The block number or hash. Or the string ``"earliest"``, ``"latest"`` or ``"pending"`` as in the :ref:`default block parameter <eth-defaultblock>`.
1130+
1. ``String|Number|BN|BigNumber`` - The block number or hash. Or the string ``"earliest"``, ``"latest"`` , ``"pending"``, ``"safe"`` or ``"finalized"`` as in the :ref:`default block parameter <eth-defaultblock>`.
11291131
2. ``Function`` - (optional) Optional callback, returns an error object as first parameter and the result as second.
11301132

11311133
-------
@@ -1162,7 +1164,7 @@ Returns a blocks uncle by a given uncle index position.
11621164
Parameters
11631165
----------
11641166

1165-
1. ``String|Number|BN|BigNumber`` - The block number or hash. Or the string ``"earliest"``, ``"latest"`` or ``"pending"`` as in the :ref:`default block parameter <eth-defaultblock>`.
1167+
1. ``String|Number|BN|BigNumber`` - The block number or hash. Or the string ``"earliest"``, ``"latest"`` , ``"pending"``, ``"safe"`` or ``"finalized"`` as in the :ref:`default block parameter <eth-defaultblock>`.
11661168
2. ``Number`` - The index position of the uncle.
11671169
3. ``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.
11681170
4. ``Function`` - (optional) Optional callback, returns an error object as first parameter and the result as second.
@@ -1356,7 +1358,7 @@ Parameters
13561358
----------
13571359

13581360

1359-
1. ``String|Number|BN|BigNumber`` - A block number or hash. Or the string ``"earliest"``, ``"latest"`` or ``"pending"`` as in the :ref:`default block parameter <eth-defaultblock>`.
1361+
1. ``String|Number|BN|BigNumber`` - A block number or hash. Or the string ``"earliest"``, ``"latest"`` , ``"pending"``, ``"safe"`` or ``"finalized"`` as in the :ref:`default block parameter <eth-defaultblock>`.
13601362
2. ``Number`` - The transaction's index position.
13611363
3. ``Function`` - (optional) Optional callback, returns an error object as first parameter and the result as second.
13621364

@@ -1486,7 +1488,7 @@ Parameters
14861488
----------
14871489

14881490
1. ``String`` - The address to get the numbers of transactions from.
1489-
2. ``Number|String|BN|BigNumber`` - (optional) If you pass this parameter it will not use the default block set with :ref:`web3.eth.defaultBlock <eth-defaultblock>`. Pre-defined block numbers as ``"earliest"``, ``"latest"`` and ``"pending"`` can also be used.
1491+
2. ``Number|String|BN|BigNumber`` - (optional) If you pass this parameter it will not use the default block set with :ref:`web3.eth.defaultBlock <eth-defaultblock>`. Pre-defined block numbers as ``"earliest"``, ``"latest"`` , ``"pending"``, ``"safe"`` or ``"finalized"`` can also be used.
14901492
3. ``Function`` - (optional) Optional callback, returns an error object as first parameter and the result as second.
14911493

14921494
-------
@@ -1804,7 +1806,7 @@ Parameters
18041806
----------
18051807

18061808
1. ``Object`` - A transaction object, see :ref:`web3.eth.sendTransaction <eth-sendtransaction-return>`. 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.
1807-
2. ``Number|String|BN|BigNumber`` - (optional) If you pass this parameter it will not use the default block set with :ref:`web3.eth.defaultBlock <eth-defaultblock>`. Pre-defined block numbers as ``"earliest"``, ``"latest"`` and ``"pending"`` can also be used.
1809+
2. ``Number|String|BN|BigNumber`` - (optional) If you pass this parameter it will not use the default block set with :ref:`web3.eth.defaultBlock <eth-defaultblock>`. Pre-defined block numbers as ``"earliest"``, ``"latest"`` , ``"pending"``, ``"safe"`` or ``"finalized"`` can also be used.
18081810
3. ``Function`` - (optional) Optional callback, returns an error object as first parameter and the result as second.
18091811

18101812
-------
@@ -1888,8 +1890,8 @@ Parameters
18881890
----------
18891891

18901892
1. ``Object`` - The filter options as follows:
1891-
- ``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"``.
1892-
- ``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"``.
1893+
- ``fromBlock`` - ``Number|String``: The number of the earliest block (or any of block tag ``"earliest"``, ``"latest"`` , ``"pending"``, ``"safe"`` or ``"finalized"`` can also be used). By default ``"latest"``.
1894+
- ``toBlock`` - ``Number|String``: The number of the latest block (or any of block tag ``"earliest"``, ``"latest"`` , ``"pending"``, ``"safe"`` or ``"finalized"`` can also be used). By default ``"latest"``.
18931895
- ``address`` - ``String|Array``: An address or a list of addresses to only get logs from particular account(s).
18941896
- ``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']]``
18951897

@@ -2133,7 +2135,7 @@ Parameters
21332135

21342136
1. ``String`` 20 Bytes: The Address of the account or contract.
21352137
2. ``Number[] | BigNumber[] | BN[] | String[]`` 32 Bytes: Array of storage-keys which should be proofed and included. See :ref:`web3.eth.getStorageAt <eth-getStorageAt>`.
2136-
3. ``Number | String | BN | BigNumber``: Integer block number. Pre-defined block numbers as ``"earliest"``, ``"latest"`` and ``"pending"`` can also be used.
2138+
3. ``Number | String | BN | BigNumber``: Integer block number. Pre-defined block numbers as ``"earliest"``, ``"latest"`` , ``"pending"``, ``"safe"`` or ``"finalized"`` can also be used.
21372139
4. ``Function`` - (optional) Optional callback, returns an error object as first parameter and the result as second.
21382140

21392141
-------
@@ -2208,7 +2210,7 @@ Parameters
22082210
----------
22092211

22102212
1. A transaction object, see :ref:`web3.eth.sendTransaction <eth-sendtransaction-return>` with the difference that this method is specifically for contract method executions.
2211-
2. ``block`` - ``String|Number|BN|BigNumber`` (optional): The block number or hash. Or the string ``"earliest"``, ``"latest"`` or ``"pending"`` as in the :ref:`default block parameter <eth-defaultblock>`.
2213+
2. ``block`` - ``String|Number|BN|BigNumber`` (optional): The block number or hash. Or the string ``"earliest"``, ``"latest"`` , ``"pending"``, ``"safe"`` or ``"finalized"`` as in the :ref:`default block parameter <eth-defaultblock>`.
22122214
3. ``callback`` - ``Function`` (optional): This callback will be fired with the result of the access list generation as the second argument, or with an error object as the first argument.
22132215

22142216

packages/web3-core-helpers/src/formatters.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ var outputBigNumberFormatter = function (number) {
7171
};
7272

7373
/**
74-
* Returns true if the given blockNumber is 'latest', 'pending', or 'earliest.
74+
* Returns true if the given blockNumber is 'latest', 'pending', 'earliest, 'finalized' or 'safe'
7575
*
7676
* @method isPredefinedBlockNumber
7777
*
@@ -80,7 +80,7 @@ var outputBigNumberFormatter = function (number) {
8080
* @returns {Boolean}
8181
*/
8282
var isPredefinedBlockNumber = function (blockNumber) {
83-
return blockNumber === 'latest' || blockNumber === 'pending' || blockNumber === 'earliest';
83+
return ['latest','pending','earliest','finalized','safe'].includes(blockNumber);
8484
};
8585

8686
/**
@@ -101,7 +101,7 @@ var inputDefaultBlockNumberFormatter = function (blockNumber) {
101101
};
102102

103103
/**
104-
* Returns the given block number as hex string or the predefined block number 'latest', 'pending', 'earliest', 'genesis'
104+
* Returns the given block number as hex string or the predefined block number 'latest', 'pending', 'earliest', 'finalized', 'safe', 'genesis'
105105
*
106106
* @param {String|Number|BN|BigNumber} blockNumber
107107
*

packages/web3-core/types/index.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ export interface LogsOptions {
433433
topics?: Array<string | string[] | null>;
434434
}
435435

436-
export type BlockNumber = string | number | BN | BigNumber | 'latest' | 'pending' | 'earliest' | 'genesis';
436+
export type BlockNumber = string | number | BN | BigNumber | 'latest' | 'pending' | 'earliest' | 'genesis' | 'finalized' | 'safe';
437437

438438
export interface RequestArguments {
439439
method: string;

0 commit comments

Comments
 (0)