-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Partial restructuring of repo #80
base: main
Are you sure you want to change the base?
Changes from all commits
2cac0df
c49bbf4
855579f
cc5aff1
563f83a
2971079
e578a2b
c77e790
9110d3d
d255c4d
08cfefd
2d36ef9
6d41ab6
41c80c1
64d8b67
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -121,21 +121,28 @@ def extract_actions(self, traces: list[AttributeDict], address: str) -> list[dic | |
actions.append(dict(action)) | ||
return actions | ||
|
||
def calculate_native_eth_imbalance(self, actions: list[dict], address: str) -> int: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change looks like a regression in terms of complexity and code style. What was the rational? If the change of return type is required, make only that change here, e.g. via The docstring should mention what it means if |
||
def calculate_native_eth_imbalance( | ||
self, actions: list[dict], address: str | ||
) -> int | None: | ||
"""Extract ETH imbalance from transfer actions.""" | ||
# inflow is the total value transferred to address param | ||
inflow = sum( | ||
_to_int(action["value"]) | ||
for action in actions | ||
if Web3.to_checksum_address(action.get("to", "")) == address | ||
) | ||
# outflow is the total value transferred out of address param | ||
outflow = sum( | ||
_to_int(action["value"]) | ||
for action in actions | ||
if Web3.to_checksum_address(action.get("from", "")) == address | ||
) | ||
return inflow - outflow | ||
native_eth_imbalance = 0 | ||
is_it_none = True | ||
for action in actions: | ||
flow = 0 | ||
if Web3.to_checksum_address(action.get("to", "")) == address: | ||
flow = _to_int(action["value"]) | ||
else: | ||
if Web3.to_checksum_address(action.get("from", "")) == address: | ||
flow = (-1) * _to_int(action["value"]) | ||
if flow != 0: | ||
is_it_none = False | ||
native_eth_imbalance += flow | ||
|
||
if is_it_none: | ||
return None | ||
else: | ||
return native_eth_imbalance | ||
|
||
def extract_events(self, tx_receipt: dict) -> dict[str, list[dict]]: | ||
"""Extract relevant events from the transaction receipt.""" | ||
|
@@ -297,7 +304,7 @@ def filter_sdai_events(event_list: list[dict], is_deposit: bool) -> None: | |
filter_sdai_events(events["DepositSDAI"], is_deposit=True) | ||
filter_sdai_events(events["WithdrawSDAI"], is_deposit=False) | ||
|
||
def compute_imbalances(self, tx_hash: str) -> dict[str, int]: | ||
def aggregate_imbalances(self, tx_hash: str) -> dict[str, int]: | ||
try: | ||
tx_receipt = self.get_transaction_receipt(tx_hash) | ||
if not tx_receipt: | ||
|
@@ -332,14 +339,29 @@ def compute_imbalances(self, tx_hash: str) -> dict[str, int]: | |
logger.error("Error computing imbalances for %s: %s", tx_hash, e) | ||
raise | ||
|
||
def compute_token_imbalances(self, tx_hash: str) -> dict[str, int]: | ||
"""Process token imbalances for a given transaction and return imbalances.""" | ||
try: | ||
return self.aggregate_imbalances(tx_hash) | ||
except Exception as e: | ||
logger.error(f"Failed to compute imbalances for transaction {tx_hash}: {e}") | ||
return {} | ||
|
||
def get_transaction_tokens(self, tx_hash: str) -> list[tuple[str, str]]: | ||
token_imbalances = self.compute_token_imbalances(tx_hash) | ||
transaction_tokens = [] | ||
for token_address in token_imbalances.keys(): | ||
transaction_tokens.append((tx_hash, token_address)) | ||
return transaction_tokens | ||
|
||
|
||
def main() -> None: | ||
"""main function for finding imbalance for a single tx hash.""" | ||
tx_hash = input("Enter transaction hash: ") | ||
chain_name, web3 = find_chain_with_tx(tx_hash) | ||
rt = RawTokenImbalances(web3, chain_name) | ||
try: | ||
imbalances = rt.compute_imbalances(tx_hash) | ||
imbalances = rt.aggregate_imbalances(tx_hash) | ||
if imbalances: | ||
logger.info(f"Token Imbalances on {chain_name}:") | ||
for token_address, imbalance in imbalances.items(): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using config variables here sounds good.