Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 3 additions & 2 deletions src/ethproto/wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

ETHERSCAN_TOKEN = env.str("ETHERSCAN_TOKEN", None)
ETHERSCAN_DOMAIN = env.str("ETHERSCAN_DOMAIN", "api.etherscan.io")
ETHERSCAN_URL = env.str("ETHERSCAN_URL", "https://{domain}/api?apikey={token}&")
ETHERSCAN_URL = env.str("ETHERSCAN_URL", "https://{domain}/v2/api?apikey={token}&")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Acá ya tendría que incluir el chainId también. Que get_etherscan_url lo complete.

AMOUNT_DECIMALS = env.int("AMOUNT_DECIMALS", 18)
AMOUNT_CLASSNAME = env.str("AMOUNT_CLASSNAME", None)

Expand Down Expand Up @@ -273,9 +273,10 @@ def get_first_block(self, eth_wrapper):
if not etherscan_url:
return 0
address = self.get_contract_address(eth_wrapper)
chain_id = self.w3.eth.chain_id
url = (
etherscan_url
+ f"&module=account&action=txlist&address={address}&startblock=0&"
+ f"chainid={chain_id}&module=account&action=txlist&address={address}&startblock=0&"
+ "endblock=99999999&page=1&offset=10&sort=asc"
)
resp = requests.get(url)
Expand Down
49 changes: 49 additions & 0 deletions tests/test_w3.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import importlib
import os
import sys

import pytest
from web3 import Web3
Expand All @@ -11,6 +13,32 @@
]


@pytest.fixture
def provider_with_etherscan_env(mocker):
mocker.patch.dict(
os.environ,
{
"ETHERSCAN_URL": "https://{domain}/v2/api?apikey={token}&",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mejor no sobreescribir la URL para el test, podría enmascarar errores (o cambios que rompen compatibilidad en el futuro)

"ETHERSCAN_TOKEN": "abc123",
"ETHERSCAN_DOMAIN": "api.etherscan.io",
},
)
sys.modules.pop("ethproto.wrappers", None)
sys.modules.pop("ethproto.w3wrappers", None)
wrappers = importlib.import_module("ethproto.wrappers")
w3wrappers = importlib.import_module("ethproto.w3wrappers")

w3wrappers.register_w3_provider("w3_test", Web3(Web3.EthereumTesterProvider()))
provider = wrappers.get_provider("w3_test")

return provider


class DummyWrapper:
address = "0x8e3aab1fc53e8b0f5d987c20b1899a2db3b2f95c" # random generated address
contract = type("Contract", (), {"address": address})()


class Counter(wrappers.ETHWrapper):
eth_contract = "Counter"
# libraries_required = []
Expand Down Expand Up @@ -182,3 +210,24 @@ def test_sign_and_send_interact_with_existing_contract(sign_and_send):
assert connected.value() == 1

assert counter.value() == 1 # sanity check


def test_get_etherscan_url_v2_format(provider_with_etherscan_env):
provider = provider_with_etherscan_env
assert provider.get_etherscan_url() == "https://api.etherscan.io/v2/api?apikey=abc123&"


def test_get_first_block_makes_request(provider_with_etherscan_env, requests_mock):
provider = provider_with_etherscan_env
address = "0x8e3aab1fc53e8b0f5d987c20b1899a2db3b2f95c" # random generated address
chain_id = provider.w3.eth.chain_id

requests_mock.get(
f"https://api.etherscan.io/v2/api?apikey=abc123&"
f"chainid={chain_id}&module=account&action=txlist&address={address}&startblock=0&"
"endblock=99999999&page=1&offset=10&sort=asc",
json={"status": "1", "message": "OK", "result": [{"blockNumber": "1"}]},
)

block = provider.get_first_block(DummyWrapper())
assert block == 1
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ extras =
testing
deps =
warrant @ git+https://github.com/gnarvaja/warrant.git#egg=warrant
requests-mock
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Por qué esta dependencia que no usamos en ningún lado? Usemos responses. Además la dependencia habría que ponerla en setup.cfg, en la sección de testing.

Si no podés usar cassete (pytest-recording) que ya está incluida, pero capaz para este caso es mejor responses.

commands =
pytest --block-network {posargs}

Expand Down