Skip to content

Commit d40ee04

Browse files
committed
Update web3 api to compute eth transfers to an address (#79)
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).
1 parent 905ba92 commit d40ee04

File tree

1 file changed

+41
-12
lines changed

1 file changed

+41
-12
lines changed

src/apis/web3api.py

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,35 +45,64 @@ def get_current_block_number(self) -> Optional[int]:
4545
self.logger.warning(f"Error while fetching block number: {err}")
4646
return None
4747

48-
def get_tx_hashes_by_block(
49-
self, start_block: int, end_block: int
50-
) -> Optional[list[str]]:
48+
def get_filtered_receipts(
49+
self, start_block: int, end_block: int, target: str, topics: list[Any]
50+
) -> Optional[list[Any]]:
5151
"""
52-
Function filters hashes by contract address, and block ranges
52+
Function filters receipts by contract address, and block ranges
5353
"""
5454
filter_criteria: FilterParams = {
5555
"fromBlock": int(start_block),
5656
"toBlock": int(end_block),
57-
"address": self.web_3.to_checksum_address(SETTLEMENT_CONTRACT_ADDRESS),
58-
"topics": [
59-
HexStr(
60-
"0xa07a543ab8a018198e99ca0184c93fe9050a79400a0a723441f84de1d972cc17"
61-
)
62-
],
57+
"address": self.web_3.to_checksum_address(target),
58+
"topics": topics,
6359
}
64-
6560
try:
6661
log_receipts = self.web_3.eth.filter(filter_criteria).get_all_entries()
6762
except ValueError as err:
6863
self.logger.warning(f"ValueError while fetching hashes: {err}")
6964
return None
65+
return log_receipts
7066

67+
def get_tx_hashes_by_block(
68+
self, start_block: int, end_block: int
69+
) -> Optional[list[str]]:
70+
"""
71+
Function filters hashes by contract address, and block ranges
72+
"""
73+
topics = [
74+
HexStr("0xa07a543ab8a018198e99ca0184c93fe9050a79400a0a723441f84de1d972cc17")
75+
]
76+
log_receipts = self.get_filtered_receipts(
77+
start_block, end_block, SETTLEMENT_CONTRACT_ADDRESS, topics
78+
)
79+
80+
if log_receipts is None:
81+
return None
7182
settlement_hashes_list = list(
7283
{log_receipt["transactionHash"].hex() for log_receipt in log_receipts}
7384
)
74-
7585
return settlement_hashes_list
7686

87+
def get_eth_transfers_by_block_range(
88+
self, start_block: int, end_block: int, target: str
89+
) -> Optional[float]:
90+
"""
91+
Function that computes total eth transfers to a target Safe address
92+
within a certain block range
93+
"""
94+
log_receipts = self.get_filtered_receipts(start_block, end_block, target, [])
95+
if log_receipts is None:
96+
return None
97+
total_transfers_in_eth = 0.0
98+
for txs in log_receipts:
99+
if (
100+
txs["topics"][0].hex()
101+
== "0x3d0ce9bfc3ed7d6862dbb28b2dea94561fe714a1b4d019aa8af39730d1ad7c3d"
102+
):
103+
total_transfers_in_eth += int(txs["data"].hex(), 16) / 10**18
104+
return total_transfers_in_eth
105+
77106
def get_transaction(self, tx_hash: str) -> Optional[TxData]:
78107
"""
79108
Takes settlement hash as input, returns transaction data.

0 commit comments

Comments
 (0)