w3.py 956 B

1234567891011121314151617181920212223242526272829303132333435
  1. from ens import ENS
  2. from web3 import Web3
  3. from typing import Dict, Any, Optional
  4. def get_balance(address: str, w3: Web3) -> float:
  5. # returns balance in ETH
  6. return round(w3.eth.get_balance(address) / 10**18, 3)
  7. def get_ens_name(address: str, ns: ENS) -> Optional[str]:
  8. ens_name: Optional[str] = ns.name(address)
  9. return ens_name
  10. def query_web3(address: str, w3: Web3, ns: ENS) -> Dict[str, Any]:
  11. address: str = Web3.toChecksumAddress(address)
  12. return dict(
  13. balance=get_balance(address, w3),
  14. ens_name=get_ens_name(address, ns),
  15. )
  16. def get_ens_address(name: str, ns: ENS) -> Optional[str]:
  17. return ns.address(name)
  18. def resolve_address(input_: str, ns: ENS) -> str:
  19. address: str = input_
  20. if '.eth' in input_: # only waste compute it .ens is in it
  21. address: Optional[str] = get_ens_address(input_, ns)
  22. if address is None:
  23. address: str = input_ # punt
  24. return address