Skip to content

Commit

Permalink
chore: some logs (#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey authored Aug 28, 2023
1 parent 9ad463b commit 106d846
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
16 changes: 12 additions & 4 deletions ape_etherscan/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from ape.logging import logger
from ape.utils import USER_AGENT, ManagerAccessMixin
from requests import Session
from yarl import URL

from ape_etherscan.exceptions import (
UnhandledResultError,
Expand Down Expand Up @@ -176,6 +177,11 @@ def _retries(self) -> int:
def _min_time_between_calls(self) -> float:
return 1 / self._rate_limit # seconds / calls per second

@property
def _clean_uri(self) -> str:
url = URL(self.base_uri).with_user(None).with_password(None)
return f"{url.with_path('')}/[hidden]" if url.path else f"{url}"

def _get(
self,
params: Optional[Dict] = None,
Expand Down Expand Up @@ -216,6 +222,7 @@ def _request(
) -> EtherscanResponse:
headers = headers or self.DEFAULT_HEADERS
for i in range(self._retries):
logger.debug(f"Request sent to {self._clean_uri}.")
response = self.session.request(
method.upper(),
self.base_uri,
Expand All @@ -225,14 +232,16 @@ def _request(
timeout=1024,
)
if response.status_code == 429:
time.sleep(2**i)
time_to_sleep = 2**i
logger.debug(f"Request was throttled. Retrying in {time_to_sleep} seconds.")
time.sleep(time_to_sleep)
continue

# Recieved a real response unrelated to rate limiting.
if raise_on_exceptions:
response.raise_for_status()
elif not 200 <= response.status_code < 300:
logger.error(response.text)
logger.error(f"Response was not successful: {response.text}")

break

Expand Down Expand Up @@ -264,9 +273,8 @@ def get_source_code(self) -> SourceCodeResponse:
"address": self._address,
}
result = self._get(params=params)
result_list = result.value or []

if not result_list:
if not (result_list := result.value or []):
return SourceCodeResponse()

elif len(result_list) > 1:
Expand Down
10 changes: 5 additions & 5 deletions ape_etherscan/explorer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from ape.api import ExplorerAPI
from ape.contracts import ContractInstance
from ape.exceptions import ProviderNotConnectedError
from ape.logging import logger
from ape.types import AddressType, ContractType

from ape_etherscan.client import ClientFactory, get_etherscan_uri
Expand Down Expand Up @@ -35,20 +36,19 @@ def get_contract_type(self, address: AddressType) -> Optional[ContractType]:

client = self._client_factory.get_contract_client(address)
source_code = client.get_source_code()
abi_string = source_code.abi
if not abi_string:
if not (abi_string := source_code.abi):
return None

try:
abi = json.loads(abi_string)
except JSONDecodeError:
except JSONDecodeError as err:
logger.error(f"Error with contract ABI: {err}")
return None

contract_type = ContractType(abi=abi, contractName=source_code.name)
if source_code.name == "Vyper_contract" and "symbol" in contract_type.view_methods:
try:
checksummed_address = self.provider.network.ecosystem.decode_address(address)
contract = ContractInstance(checksummed_address, contract_type)
contract = ContractInstance(address, contract_type)
contract_type.name = contract.symbol() or contract_type.name
except ProviderNotConnectedError:
pass
Expand Down

0 comments on commit 106d846

Please sign in to comment.