Skip to content

📝 replace all instances of Web3 with w3 #2357

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,43 +47,43 @@ def is_testrpc_provider(provider):
@pytest.fixture()
def skip_if_testrpc():

def _skip_if_testrpc(web3):
if is_testrpc_provider(web3.provider):
def _skip_if_testrpc(w3):
if is_testrpc_provider(w3.provider):
pytest.skip()
return _skip_if_testrpc


@pytest.fixture()
def wait_for_miner_start():
def _wait_for_miner_start(web3, timeout=60):
def _wait_for_miner_start(w3, timeout=60):
poll_delay_counter = PollDelayCounter()
with Timeout(timeout) as timeout:
while not web3.eth.mining or not web3.eth.hashrate:
while not w3.eth.mining or not w3.eth.hashrate:
time.sleep(poll_delay_counter())
timeout.check()
return _wait_for_miner_start


@pytest.fixture(scope="module")
def wait_for_block():
def _wait_for_block(web3, block_number=1, timeout=None):
def _wait_for_block(w3, block_number=1, timeout=None):
if not timeout:
timeout = (block_number - web3.eth.block_number) * 3
timeout = (block_number - w3.eth.block_number) * 3
poll_delay_counter = PollDelayCounter()
with Timeout(timeout) as timeout:
while web3.eth.block_number < block_number:
web3.manager.request_blocking("evm_mine", [])
while w3.eth.block_number < block_number:
w3.manager.request_blocking("evm_mine", [])
timeout.sleep(poll_delay_counter())
return _wait_for_block


@pytest.fixture(scope="module")
def wait_for_transaction():
def _wait_for_transaction(web3, txn_hash, timeout=120):
def _wait_for_transaction(w3, txn_hash, timeout=120):
poll_delay_counter = PollDelayCounter()
with Timeout(timeout) as timeout:
while True:
txn_receipt = web3.eth.get_transaction_receipt(txn_hash)
txn_receipt = w3.eth.get_transaction_receipt(txn_hash)
if txn_receipt is not None:
break
time.sleep(poll_delay_counter())
Expand All @@ -94,7 +94,7 @@ def _wait_for_transaction(web3, txn_hash, timeout=120):


@pytest.fixture()
def web3():
def w3():
provider = EthereumTesterProvider()
return Web3(provider)

Expand Down
24 changes: 12 additions & 12 deletions docs/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,7 @@ The script can be run with: ``python ./eventscanner.py <your JSON-RPC API URL>``
because it cannot correctly throttle and decrease the `eth_getLogs` block number range.
"""

def __init__(self, web3: Web3, contract: Contract, state: EventScannerState, events: List, filters: {},
def __init__(self, w3: Web3, contract: Contract, state: EventScannerState, events: List, filters: {},
max_chunk_scan_size: int = 10000, max_request_retries: int = 30, request_retry_seconds: float = 3.0):
"""
:param contract: Contract
Expand All @@ -878,7 +878,7 @@ The script can be run with: ``python ./eventscanner.py <your JSON-RPC API URL>``

self.logger = logger
self.contract = contract
self.web3 = web3
self.w3 = w3
self.state = state
self.events = events
self.filters = filters
Expand All @@ -903,7 +903,7 @@ The script can be run with: ``python ./eventscanner.py <your JSON-RPC API URL>``
def get_block_timestamp(self, block_num) -> datetime.datetime:
"""Get Ethereum block timestamp"""
try:
block_info = self.web3.eth.getBlock(block_num)
block_info = self.w3.eth.getBlock(block_num)
except BlockNotFound:
# Block was not mined yet,
# minor chain reorganisation?
Expand Down Expand Up @@ -931,7 +931,7 @@ The script can be run with: ``python ./eventscanner.py <your JSON-RPC API URL>``

# Do not scan all the way to the final block, as this
# block might not be mined yet
return self.web3.eth.blockNumber - 1
return self.w3.eth.blockNumber - 1

def get_last_scanned_block(self) -> int:
return self.state.get_last_scanned_block()
Expand Down Expand Up @@ -964,7 +964,7 @@ The script can be run with: ``python ./eventscanner.py <your JSON-RPC API URL>``

# Callable that takes care of the underlying web3 call
def _fetch_events(_start_block, _end_block):
return _fetch_events_for_all_contracts(self.web3,
return _fetch_events_for_all_contracts(self.w3,
event_type,
self.filters,
from_block=_start_block,
Expand Down Expand Up @@ -1133,7 +1133,7 @@ The script can be run with: ``python ./eventscanner.py <your JSON-RPC API URL>``


def _fetch_events_for_all_contracts(
web3,
w3,
event,
argument_filters: dict,
from_block: int,
Expand All @@ -1158,7 +1158,7 @@ The script can be run with: ``python ./eventscanner.py <your JSON-RPC API URL>``
# it might have Solidity ABI encoding v1 or v2.
# We just assume the default that you set on Web3 object here.
# More information here https://eth-abi.readthedocs.io/en/latest/index.html
codec: ABICodec = web3.codec
codec: ABICodec = w3.codec

# Here we need to poke a bit into Web3 internals, as this
# functionality is not exposed by default.
Expand All @@ -1179,7 +1179,7 @@ The script can be run with: ``python ./eventscanner.py <your JSON-RPC API URL>``

# Call JSON-RPC API on your Ethereum node.
# get_logs() returns raw AttributedDict entries
logs = web3.eth.get_logs(event_filter_params)
logs = w3.eth.get_logs(event_filter_params)

# Convert raw binary data to Python proxy objects as described by ABI
all_events = []
Expand Down Expand Up @@ -1358,19 +1358,19 @@ The script can be run with: ``python ./eventscanner.py <your JSON-RPC API URL>``
# throttle down.
provider.middlewares.clear()

web3 = Web3(provider)
w3 = Web3(provider)

# Prepare stub ERC-20 contract object
abi = json.loads(ABI)
ERC20 = web3.eth.contract(abi=abi)
ERC20 = w3.eth.contract(abi=abi)

# Restore/create our persistent state
state = JSONifiedState()
state.restore()

# chain_id: int, web3: Web3, abi: dict, state: EventScannerState, events: List, filters: {}, max_chunk_scan_size: int=10000
# chain_id: int, w3: Web3, abi: dict, state: EventScannerState, events: List, filters: {}, max_chunk_scan_size: int=10000
scanner = EventScanner(
web3=web3,
w3=w3,
contract=ERC20,
state=state,
events=[ERC20.events.Transfer],
Expand Down
18 changes: 9 additions & 9 deletions ens/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,23 +112,23 @@ def __init__(
:param hex-string addr: the address of the ENS registry on-chain. If not provided,
ENS.py will default to the mainnet ENS registry address.
"""
self.web3 = init_web3(provider, middlewares)
self.w3 = init_web3(provider, middlewares)

ens_addr = addr if addr else ENS_MAINNET_ADDR
self.ens = self.web3.eth.contract(abi=abis.ENS, address=ens_addr)
self._resolverContract = self.web3.eth.contract(abi=abis.RESOLVER)
self.ens = self.w3.eth.contract(abi=abis.ENS, address=ens_addr)
self._resolverContract = self.w3.eth.contract(abi=abis.RESOLVER)

@classmethod
def fromWeb3(cls, web3: 'Web3', addr: ChecksumAddress = None) -> 'ENS':
def fromWeb3(cls, w3: 'Web3', addr: ChecksumAddress = None) -> 'ENS':
"""
Generate an ENS instance with web3

:param `web3.Web3` web3: to infer connection information
:param `web3.Web3` w3: to infer connection information
:param hex-string addr: the address of the ENS registry on-chain. If not provided,
ENS.py will default to the mainnet ENS registry address.
"""
provider = web3.manager.provider
middlewares = web3.middleware_onion.middlewares
provider = w3.manager.provider
middlewares = w3.middleware_onion.middlewares
return cls(provider, addr=addr, middlewares=middlewares)

def address(self, name: str) -> Optional[ChecksumAddress]:
Expand Down Expand Up @@ -333,7 +333,7 @@ def setup_owner(

def _assert_control(self, account: ChecksumAddress, name: str,
parent_owned: Optional[str] = None) -> None:
if not address_in(account, self.web3.eth.accounts):
if not address_in(account, self.w3.eth.accounts):
raise UnauthorizedError(
"in order to modify %r, you must control account %r, which owns %r" % (
name, account, parent_owned or name
Expand Down Expand Up @@ -410,4 +410,4 @@ def _setup_reverse(

def _reverse_registrar(self) -> 'Contract':
addr = self.ens.caller.owner(normal_name_to_hash(REVERSE_REGISTRAR_DOMAIN))
return self.web3.eth.contract(address=addr, abi=abis.REVERSE_REGISTRAR)
return self.w3.eth.contract(address=addr, abi=abis.REVERSE_REGISTRAR)
4 changes: 2 additions & 2 deletions ethpm/_utils/chains.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
from web3 import Web3 # noqa: F401


def get_genesis_block_hash(web3: "Web3") -> HexBytes:
return web3.eth.get_block(BlockNumber(0))["hash"]
def get_genesis_block_hash(w3: "Web3") -> HexBytes:
return w3.eth.get_block(BlockNumber(0))["hash"]


BLOCK = "block"
Expand Down
6 changes: 3 additions & 3 deletions ethpm/contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def __init__(self, address: bytes, **kwargs: Any) -> None:

@classmethod
def factory(
cls, web3: "Web3", class_name: str = None, **kwargs: Any
cls, w3: "Web3", class_name: str = None, **kwargs: Any
) -> Contract:
dep_link_refs = kwargs.get("unlinked_references")
bytecode = kwargs.get("bytecode")
Expand All @@ -69,7 +69,7 @@ def factory(
if not is_prelinked_bytecode(to_bytes(hexstr=bytecode), dep_link_refs):
needs_bytecode_linking = True
kwargs = assoc(kwargs, "needs_bytecode_linking", needs_bytecode_linking)
return super(LinkableContract, cls).factory(web3, class_name, **kwargs)
return super(LinkableContract, cls).factory(w3, class_name, **kwargs)

@classmethod
def constructor(cls, *args: Any, **kwargs: Any) -> ContractConstructor:
Expand Down Expand Up @@ -98,7 +98,7 @@ def link_bytecode(cls, attr_dict: Dict[str, str]) -> Type["LinkableContract"]:
cls.bytecode_runtime, cls.linked_references, attr_dict
)
linked_class = cls.factory(
cls.web3, bytecode_runtime=runtime, bytecode=bytecode
cls.w3, bytecode_runtime=runtime, bytecode=bytecode
)
if linked_class.needs_bytecode_linking:
raise BytecodeLinkingError(
Expand Down
6 changes: 3 additions & 3 deletions ethpm/uri.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,15 @@ def create_latest_block_uri(w3: "Web3", from_blocks_ago: int = 3) -> URI:


@curry
def check_if_chain_matches_chain_uri(web3: "Web3", blockchain_uri: URI) -> bool:
def check_if_chain_matches_chain_uri(w3: "Web3", blockchain_uri: URI) -> bool:
chain_id, resource_type, resource_hash = parse_BIP122_uri(blockchain_uri)
genesis_block = web3.eth.get_block("earliest")
genesis_block = w3.eth.get_block("earliest")

if encode_hex(genesis_block["hash"]) != chain_id:
return False

if resource_type == BLOCK:
resource = web3.eth.get_block(resource_hash)
resource = w3.eth.get_block(resource_hash)
else:
raise ValueError(f"Unsupported resource type: {resource_type}")

Expand Down
1 change: 1 addition & 0 deletions newsfragments/1183.breaking-change.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Audit ``.rst`` and ``.py`` files and convert all Web3 instance variable names to ``w3`` to avoid confusion with the ``web3`` module.
12 changes: 6 additions & 6 deletions tests/core/admin-module/test_admin_addPeer.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import pytest


def test_admin_addPeer(web3, skip_if_testrpc):
skip_if_testrpc(web3)
def test_admin_addPeer(w3, skip_if_testrpc):
skip_if_testrpc(w3)

with pytest.warns(DeprecationWarning):
result = web3.geth.admin.addPeer(
result = w3.geth.admin.addPeer(
'enode://44826a5d6a55f88a18298bca4773fca5749cdc3a5c9f308aa7d810e9b31123f3e7c5fba0b1d70aac5308426f47df2a128a6747040a3815cc7dd7167d03be320d@127.0.0.1:30304', # noqa: E501
)
assert result is True


def test_admin_add_peer(web3, skip_if_testrpc):
skip_if_testrpc(web3)
def test_admin_add_peer(w3, skip_if_testrpc):
skip_if_testrpc(w3)

result = web3.geth.admin.add_peer(
result = w3.geth.admin.add_peer(
'enode://44826a5d6a55f88a18298bca4773fca5749cdc3a5c9f308aa7d810e9b31123f3e7c5fba0b1d70aac5308426f47df2a128a6747040a3815cc7dd7167d03be320d@127.0.0.1:30304', # noqa: E501
)
assert result is True
12 changes: 6 additions & 6 deletions tests/core/admin-module/test_admin_nodeInfo.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import pytest


def test_admin_nodeInfo(web3, skip_if_testrpc):
skip_if_testrpc(web3)
def test_admin_nodeInfo(w3, skip_if_testrpc):
skip_if_testrpc(w3)

with pytest.warns(DeprecationWarning):
node_info = web3.geth.admin.nodeInfo
node_info = w3.geth.admin.nodeInfo

assert 'enode' in node_info
assert 'id' in node_info


def test_admin_node_info(web3, skip_if_testrpc):
skip_if_testrpc(web3)
def test_admin_node_info(w3, skip_if_testrpc):
skip_if_testrpc(w3)

node_info = web3.geth.admin.node_info
node_info = w3.geth.admin.node_info

assert 'enode' in node_info
assert 'id' in node_info
6 changes: 3 additions & 3 deletions tests/core/admin-module/test_admin_peers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
def test_admin_peers(web3, skip_if_testrpc):
skip_if_testrpc(web3)
def test_admin_peers(w3, skip_if_testrpc):
skip_if_testrpc(w3)

assert web3.geth.admin.peers == []
assert w3.geth.admin.peers == []
Loading