-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Only try to resolve ENS names on mainnet
- Loading branch information
1 parent
38b0303
commit 606ee3a
Showing
3 changed files
with
75 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import pytest | ||
|
||
from web3 import Web3 | ||
from web3.exceptions import ( | ||
InvalidAddress, | ||
) | ||
from web3.middleware import ( # noqa: F401 | ||
construct_fixture_middleware, | ||
name_to_address_middleware, | ||
) | ||
from web3.providers.base import ( | ||
BaseProvider, | ||
) | ||
|
||
NAME = "dump.eth" | ||
ADDRESS = "0x0000000000000000000000000000000000000000" | ||
BALANCE = 0 | ||
|
||
|
||
class TempENS(): | ||
def __init__(self, name_addr_pairs): | ||
self.registry = dict(name_addr_pairs) | ||
|
||
def address(self, name, guess_tld=True): | ||
# no automated web3 usages should be guessing the TLD | ||
assert not guess_tld | ||
return self.registry.get(name, None) | ||
|
||
|
||
@pytest.fixture | ||
def w3(): | ||
w3 = Web3(providers=[BaseProvider()], middlewares=[]) | ||
w3.ens = TempENS({NAME: ADDRESS}) | ||
w3.middleware_stack.add(name_to_address_middleware(w3)) | ||
return w3 | ||
|
||
|
||
def test_pass_name_resolver(w3): | ||
return_chain_on_mainnet = construct_fixture_middleware({ | ||
'net_version': '1', | ||
}) | ||
return_balance = construct_fixture_middleware({ | ||
'eth_getBalance': BALANCE | ||
}) | ||
w3.middleware_stack.inject(return_chain_on_mainnet, layer=0) | ||
w3.middleware_stack.inject(return_balance, layer=0) | ||
assert w3.eth.getBalance(NAME) == BALANCE | ||
|
||
|
||
def test_fail_name_resolver(w3): | ||
return_chain_on_mainnet = construct_fixture_middleware({ | ||
'net_version': '2', | ||
}) | ||
w3.middleware_stack.inject(return_chain_on_mainnet, layer=0) | ||
with pytest.raises(InvalidAddress, match='.*ethereum\.eth.*'): | ||
w3.eth.getBalance("ethereum.eth") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters