Skip to content

Commit

Permalink
add block identifier
Browse files Browse the repository at this point in the history
  • Loading branch information
guanqun committed May 23, 2021
1 parent b20838c commit 780301d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
21 changes: 15 additions & 6 deletions async_web3/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@
from .types import Wei, Address, HexString


def _format_block_identifier(block_identifier: Union[int, str, bytes]):
if block_identifier is None:
return 'latest'
elif isinstance(block_identifier, int):
return hex(block_identifier)
else:
return block_identifier


class AsyncWeb3:
logger = logging.getLogger("async_web3.AsyncWeb3")

Expand All @@ -30,7 +39,7 @@ async def connect(self):

async def is_connect(self):
try:
await self.client_version()
await self.client_version
except Exception:
return False

Expand All @@ -57,6 +66,10 @@ async def get_balance(self, address: Address) -> Wei:
assert isinstance(address, Address)
return Wei(await self._do_request(RPCMethod.eth_getBalance, [address]))

async def get_transaction_count(self, address: Address, block_identifier: Union[int, str, bytes] = None) -> Wei:
block_identifier = _format_block_identifier(block_identifier)
return Wei(await self._do_request(RPCMethod.eth_getTransactionCount, [address, block_identifier]))

async def get_storage_at(
self, address: Address, storage_position: Union[int, str], block: Any
) -> str:
Expand All @@ -80,11 +93,7 @@ async def get_block_by_number(self, block_number: int, with_details: bool = Fals
)

async def call(self, call_transaction: Dict, block_identifier: Union[int, str, bytes] = None):
if block_identifier is None:
block_identifier = 'latest'
elif isinstance(block_identifier, int):
block_identifier = hex(block_identifier)

block_identifier = _format_block_identifier(block_identifier)
return await self._do_request(RPCMethod.eth_call, [call_transaction, block_identifier])

async def send_raw_transaction(self, txdata):
Expand Down
1 change: 1 addition & 0 deletions async_web3/methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class RPCMethod:
eth_getStorageAt = "eth_getStorageAt"
eth_getBlockByHash = "eth_getBlockByHash"
eth_getBlockByNumber = "eth_getBlockByNumber"
eth_getTransactionCount = "eth_getTransactionCount"

eth_call = "eth_call"
eth_sendRawTransaction = "eth_sendRawTransaction"
Expand Down

0 comments on commit 780301d

Please sign in to comment.