Skip to content

Commit

Permalink
Update web3 api to compute eth transfers to an address (#79)
Browse files Browse the repository at this point in the history
This PR adjusts a bit some of the functionality of the web3 api and adds
a function that computes the native eth transfers to a target address.
This again is needed for the PR that tracks the MEV Blocker kickbacks
(that will be a follow-up to this PR).
  • Loading branch information
harisang authored Nov 2, 2023
1 parent f704c39 commit ee5e4c9
Showing 1 changed file with 41 additions and 12 deletions.
53 changes: 41 additions & 12 deletions src/apis/web3api.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,35 +45,64 @@ def get_current_block_number(self) -> Optional[int]:
self.logger.warning(f"Error while fetching block number: {err}")
return None

def get_tx_hashes_by_block(
self, start_block: int, end_block: int
) -> Optional[list[str]]:
def get_filtered_receipts(
self, start_block: int, end_block: int, target: str, topics: list[Any]
) -> Optional[list[Any]]:
"""
Function filters hashes by contract address, and block ranges
Function filters receipts by contract address, and block ranges
"""
filter_criteria: FilterParams = {
"fromBlock": int(start_block),
"toBlock": int(end_block),
"address": self.web_3.to_checksum_address(SETTLEMENT_CONTRACT_ADDRESS),
"topics": [
HexStr(
"0xa07a543ab8a018198e99ca0184c93fe9050a79400a0a723441f84de1d972cc17"
)
],
"address": self.web_3.to_checksum_address(target),
"topics": topics,
}

try:
log_receipts = self.web_3.eth.filter(filter_criteria).get_all_entries()
except ValueError as err:
self.logger.warning(f"ValueError while fetching hashes: {err}")
return None
return log_receipts

def get_tx_hashes_by_block(
self, start_block: int, end_block: int
) -> Optional[list[str]]:
"""
Function filters hashes by contract address, and block ranges
"""
topics = [
HexStr("0xa07a543ab8a018198e99ca0184c93fe9050a79400a0a723441f84de1d972cc17")
]
log_receipts = self.get_filtered_receipts(
start_block, end_block, SETTLEMENT_CONTRACT_ADDRESS, topics
)

if log_receipts is None:
return None
settlement_hashes_list = list(
{log_receipt["transactionHash"].hex() for log_receipt in log_receipts}
)

return settlement_hashes_list

def get_eth_transfers_by_block_range(
self, start_block: int, end_block: int, target: str
) -> Optional[float]:
"""
Function that computes total eth transfers to a target Safe address
within a certain block range
"""
log_receipts = self.get_filtered_receipts(start_block, end_block, target, [])
if log_receipts is None:
return None
total_transfers_in_eth = 0.0
for txs in log_receipts:
if (
txs["topics"][0].hex()
== "0x3d0ce9bfc3ed7d6862dbb28b2dea94561fe714a1b4d019aa8af39730d1ad7c3d"
):
total_transfers_in_eth += int(txs["data"].hex(), 16) / 10**18
return total_transfers_in_eth

def get_transaction(self, tx_hash: str) -> Optional[TxData]:
"""
Takes settlement hash as input, returns transaction data.
Expand Down

0 comments on commit ee5e4c9

Please sign in to comment.