Skip to content

Commit

Permalink
Issue #2095 - Provide constants for e.g. the zero address and max uin…
Browse files Browse the repository at this point in the history
…t256 (#2109)
  • Loading branch information
ernestosperanza authored Aug 26, 2021
1 parent 13685d6 commit 73d7da0
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 16 deletions.
30 changes: 30 additions & 0 deletions docs/constants.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
.. _constants:

Constants
=========

The web3.contants module contains commonly used values.

Strings
*******

.. code-block:: python
#The Address Zero, which is 20 bytes (40 nibbles) of zero.
web3.constants.ADDRESS_ZERO
#The hexadecimal version of Max uint256.
web3.constants.MAX_INT
#The Hash Zero, which is 32 bytes (64 nibbles) of zero.
web3.constants.HASH_ZERO
Int
***

.. code-block:: python
#The amount of Wei in one Ether
web3.constants.WEI_PER_ETHER
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ Table of Contents
web3.parity
gas_price
ens
constants

.. toctree::
:maxdepth: 1
Expand Down
1 change: 1 addition & 0 deletions newsfragments/2109.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add constants for the zero address, zero hash, max int, and wei per ether.
6 changes: 5 additions & 1 deletion tests/core/contracts/test_contract_deployment.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
decode_hex,
)

from web3 import (
constants,
)


def test_contract_deployment_no_constructor(web3, MathContract,
MATH_RUNTIME):
Expand Down Expand Up @@ -56,7 +60,7 @@ def test_contract_deployment_with_constructor_with_arguments(web3,

@pytest.mark.parametrize('constructor_arg', (
b'1234\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00', # noqa: E501
'0x0000000000000000000000000000000000000000000000000000000000000000')
constants.HASH_ZERO)
)
def test_contract_deployment_with_constructor_with_arguments_strict(w3_strict_abi,
WithConstructorArgumentsContractStrict, # noqa: E501
Expand Down
5 changes: 4 additions & 1 deletion tests/core/contracts/test_contract_method_abi_encoding.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import json
import pytest

from web3 import (
constants,
)
from web3.exceptions import (
ValidationError,
)
Expand Down Expand Up @@ -125,7 +128,7 @@ def test_contract_abi_encoding_strict_with_error(w3_strict_abi, arguments):
),
pytest.param(
ABI_C,
['0x0000000000000000000000000000000000000000000000000000000000000000'],
[constants.HASH_ZERO],
None,
'0x9f3fab580000000000000000000000000000000000000000000000000000000000000000',
id='ABI_C, valid hexstring args, no data'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import pytest

from web3 import Web3
from web3 import (
Web3,
constants,
)
from web3.exceptions import (
ValidationError,
)
Expand Down Expand Up @@ -87,7 +90,7 @@ def _get_block_by_something(method, params):
return {
'hash': '0x0000000000000000000000000000000000000000000000000000000000000001',
'number': 1,
'parentHash': '0x0000000000000000000000000000000000000000000000000000000000000000',
'parentHash': constants.HASH_ZERO,
'transactions': [
{'gasPrice': 30},
{'gasPrice': 35},
Expand All @@ -100,7 +103,7 @@ def _get_block_by_something(method, params):
block_identifier == '0x0'
):
return {
'hash': '0x0000000000000000000000000000000000000000000000000000000000000000',
'hash': constants.HASH_ZERO,
'number': 0,
'parentHash': None,
'transactions': [
Expand Down
7 changes: 5 additions & 2 deletions tests/core/middleware/test_name_to_address_middleware.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import pytest

from web3 import Web3
from web3 import (
Web3,
constants,
)
from web3.exceptions import (
InvalidAddress,
)
Expand All @@ -13,7 +16,7 @@
)

NAME = "dump.eth"
ADDRESS = "0x0000000000000000000000000000000000000000"
ADDRESS = constants.ADDRESS_ZERO
BALANCE = 0


Expand Down
7 changes: 5 additions & 2 deletions tests/integration/generate_fixtures/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
to_text,
)

from web3 import (
constants,
)
from web3.exceptions import (
TransactionNotFound,
)
Expand Down Expand Up @@ -57,11 +60,11 @@
"0000000000000000000000000000000000000006": {"balance": "1"},
},
"timestamp": "0x00",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"parentHash": constants.HASH_ZERO,
"extraData": "0x3535353535353535353535353535353535353535353535353535353535353535",
"gasLimit": "0x3b9aca00", # 1,000,000,000
"difficulty": "0x10000",
"mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"mixhash": constants.HASH_ZERO,
"coinbase": COINBASE
}

Expand Down
9 changes: 6 additions & 3 deletions tests/integration/generate_fixtures/parity.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
from tests.utils import (
get_open_port,
)
from web3 import Web3
from web3 import (
Web3,
constants,
)

CHAIN_CONFIG = {
"name": "CrossClient",
Expand Down Expand Up @@ -75,13 +78,13 @@
"seal": {
"ethereum": {
"nonce": "0x0000000000000042",
"mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000"
"mixHash": constants.HASH_ZERO
}
},
"difficulty": "0x10000",
"author": common.COINBASE,
"timestamp": "0x00",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"parentHash": constants.HASH_ZERO,
"extraData": "0x3535353535353535353535353535353535353535353535353535353535353535",
"gasLimit": "0x1000000"
},
Expand Down
11 changes: 7 additions & 4 deletions web3/_utils/module_testing/personal_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
HexBytes,
)

from web3 import (
constants,
)
from web3.types import ( # noqa: F401
TxParams,
Wei,
Expand Down Expand Up @@ -143,7 +146,7 @@ def test_personal_send_transaction(
unlockable_account_dual_type: ChecksumAddress,
unlockable_account_pw: str,
) -> None:
assert web3.eth.get_balance(unlockable_account_dual_type) > web3.toWei(1, 'ether')
assert web3.eth.get_balance(unlockable_account_dual_type) > constants.WEI_PER_ETHER
txn_params: TxParams = {
'from': unlockable_account_dual_type,
'to': unlockable_account_dual_type,
Expand All @@ -167,7 +170,7 @@ def test_personal_sendTransaction_deprecated(
unlockable_account_dual_type: ChecksumAddress,
unlockable_account_pw: str,
) -> None:
assert web3.eth.get_balance(unlockable_account_dual_type) > web3.toWei(1, 'ether')
assert web3.eth.get_balance(unlockable_account_dual_type) > constants.WEI_PER_ETHER
txn_params: TxParams = {
'from': unlockable_account_dual_type,
'to': unlockable_account_dual_type,
Expand Down Expand Up @@ -460,7 +463,7 @@ def test_personal_send_transaction(
unlockable_account_dual_type: ChecksumAddress,
unlockable_account_pw: str,
) -> None:
assert web3.eth.get_balance(unlockable_account_dual_type) > web3.toWei(1, 'ether')
assert web3.eth.get_balance(unlockable_account_dual_type) > constants.WEI_PER_ETHER
txn_params: TxParams = {
'from': unlockable_account_dual_type,
'to': unlockable_account_dual_type,
Expand All @@ -484,7 +487,7 @@ def test_personal_sendTransaction_deprecated(
unlockable_account_dual_type: ChecksumAddress,
unlockable_account_pw: str,
) -> None:
assert web3.eth.get_balance(unlockable_account_dual_type) > web3.toWei(1, 'ether')
assert web3.eth.get_balance(unlockable_account_dual_type) > constants.WEI_PER_ETHER
txn_params: TxParams = {
'from': unlockable_account_dual_type,
'to': unlockable_account_dual_type,
Expand Down
9 changes: 9 additions & 0 deletions web3/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Constants as Strings
ADDRESS_ZERO = "0x0000000000000000000000000000000000000000"

MAX_INT = "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"

HASH_ZERO = "0x0000000000000000000000000000000000000000000000000000000000000000"

# Constants as Int
WEI_PER_ETHER = 1000000000000000000

0 comments on commit 73d7da0

Please sign in to comment.