Skip to content

Commit c11bbc0

Browse files
committed
Refactor integrations to improve type hints and error handling
- Added type hints for better clarity in bulbaswap_integration.py and evaa_integration.py. - Enhanced error handling in evaa_integration.py and affluent_integration.py to return empty dictionaries on exceptions. - Updated variable types for consistency and clarity in bulbaswap_integration.py.
1 parent 309629c commit c11bbc0

27 files changed

Lines changed: 95 additions & 77 deletions

constants/uniswap_v4.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import json
2+
from typing import cast
23
from web3 import Web3
34
from web3.contract import Contract
5+
from web3.types import HexStr
46
from utils.web3_utils import w3
57

68
UNISWAP_V4_USDE_POOL = Web3.to_bytes(
7-
hexstr="0x63bb22f47c7ede6578a25c873e77eb782ec8e4c19778e36ce64d37877b5bd1e7"
9+
hexstr=cast(HexStr, "0x63bb22f47c7ede6578a25c873e77eb782ec8e4c19778e36ce64d37877b5bd1e7")
810
)
911

1012
UNISWAP_V4_STATE_VIEW = Web3.to_checksum_address(

integrations/affluent_integration.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ def get_l2_block_balances(
4646
err_msg = f"Error fetching Affluent balances at block {target_block}: {e}"
4747
print(err_msg)
4848
slack_message(err_msg)
49+
return {}
4950

5051
def get_token_symbol(self):
5152
return self.integration_id.get_token()

integrations/bulbaswap_integration.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Dict, List, Optional, Set
1+
from typing import Callable, Dict, List, Optional, Set
22
import requests
33
from eth_typing import ChecksumAddress
44
from web3 import Web3
@@ -22,7 +22,7 @@ def __init__(
2222
excluded_addresses: Optional[Set[ChecksumAddress]] = None,
2323
end_block: Optional[int] = None,
2424
ethereal_multiplier: int = 0,
25-
ethereal_multiplier_func: Optional[callable] = None,
25+
ethereal_multiplier_func: Optional[Callable[..., int]] = None,
2626
):
2727
super().__init__(
2828
integration_id,
@@ -64,7 +64,7 @@ def get_block_balances(
6464
result[block] = cached_data[block]
6565
continue
6666

67-
block_data = {}
67+
block_data: Dict[ChecksumAddress, float] = {}
6868

6969
try:
7070
# Fetch data for both token addresses
@@ -79,8 +79,8 @@ def get_block_balances(
7979
"blockNumber": block,
8080
"page": page,
8181
"limit": self.page_size
82-
}
83-
)
82+
},
83+
) # type: ignore[arg-type]
8484
data = response.json()
8585

8686
if data["code"] == 200 and data["data"]["status"] == 0:
@@ -91,7 +91,7 @@ def get_block_balances(
9191
user_address = Web3.to_checksum_address(item["userAddress"])
9292

9393
# Sum up liquidity for all pools
94-
total_liquidity = 0
94+
total_liquidity: float = 0.0
9595
for pool_data in item["userPositions"].values():
9696
if float(pool_data["liquidityUSD"]) > 0:
9797
total_liquidity += float(pool_data["liquidityUSD"])

integrations/cork_susde.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ def update_pair_config(
220220
# For each pair, update term config...
221221
for pair_id, pair_config in pair_config_by_id.items():
222222
start_block = max(from_block, pair_config.start_block)
223-
if len(new_lpt_events) > 0:
223+
if len(list(new_lpt_events)) > 0:
224224
# print(f"Found {len(new_lpt_events)} new LPT events")
225225
# Update the LP token address for each term
226226
for term_id, term_config in pair_config.terms.items():
@@ -684,7 +684,7 @@ def get_block_balances(
684684
for pair_id in pair_ids
685685
]
686686
multicall_results = multicall_by_address(
687-
w3=self.w3,
687+
wb3=self.w3,
688688
multical_address=MULTICALL_ADDRESS_BY_CHAIN[self.chain],
689689
calls=vault_calls,
690690
block_identifier=block,
@@ -724,7 +724,7 @@ def get_block_balances(
724724
for amm_pool in amm_pools
725725
]
726726
multicall_results = multicall_by_address(
727-
w3=self.w3,
727+
wb3=self.w3,
728728
multical_address=MULTICALL_ADDRESS_BY_CHAIN[self.chain],
729729
calls=amm_calls,
730730
block_identifier=block,
@@ -761,7 +761,7 @@ def get_block_balances(
761761
for term_id in pair_config.terms
762762
]
763763
multicall_results = multicall_by_address(
764-
w3=self.w3,
764+
wb3=self.w3,
765765
multical_address=MULTICALL_ADDRESS_BY_CHAIN[self.chain],
766766
calls=psm_calls,
767767
block_identifier=block,

integrations/cork_usde.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ def update_pair_config(
220220
# For each pair, update term config...
221221
for pair_id, pair_config in pair_config_by_id.items():
222222
start_block = max(from_block, pair_config.start_block)
223-
if len(new_lpt_events) > 0:
223+
if len(list(new_lpt_events)) > 0:
224224
# print(f"Found {len(new_lpt_events)} new LPT events")
225225
# Update the LP token address for each term
226226
for term_id, term_config in pair_config.terms.items():
@@ -684,7 +684,7 @@ def get_block_balances(
684684
for pair_id in pair_ids
685685
]
686686
multicall_results = multicall_by_address(
687-
w3=self.w3,
687+
wb3=self.w3,
688688
multical_address=MULTICALL_ADDRESS_BY_CHAIN[self.chain],
689689
calls=vault_calls,
690690
block_identifier=block,
@@ -724,7 +724,7 @@ def get_block_balances(
724724
for amm_pool in amm_pools
725725
]
726726
multicall_results = multicall_by_address(
727-
w3=self.w3,
727+
wb3=self.w3,
728728
multical_address=MULTICALL_ADDRESS_BY_CHAIN[self.chain],
729729
calls=amm_calls,
730730
block_identifier=block,
@@ -761,7 +761,7 @@ def get_block_balances(
761761
for term_id in pair_config.terms
762762
]
763763
multicall_results = multicall_by_address(
764-
w3=self.w3,
764+
wb3=self.w3,
765765
multical_address=MULTICALL_ADDRESS_BY_CHAIN[self.chain],
766766
calls=psm_calls,
767767
block_identifier=block,

integrations/echelon_integration.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import json
44
import requests
55

6-
from typing import Dict, List, Set
6+
from typing import Dict, List, Optional, Set
77
from dotenv import load_dotenv
88
from constants.summary_columns import SummaryColumn
99
from constants.example_integrations import (
@@ -95,7 +95,8 @@ def get_participants_data(self, block, user_addresses=[]):
9595
print(f"Unexpected error: {e}")
9696
raise
9797

98-
def get_participants(self, block: int) -> List[str]:
98+
def get_participants(self, blocks: Optional[List[int]] = None) -> Set[str]:
99+
block = blocks[0] if blocks else 0
99100
try:
100101
response = requests.get(
101102
f"{ETHENA_ADDRESS_API_URL}?block={block}",
@@ -106,16 +107,16 @@ def get_participants(self, block: int) -> List[str]:
106107
data = response.json()['data']
107108
if not isinstance(data, list):
108109
logging.warning(f"Unexpected response format from API: {data}")
109-
return []
110+
return set()
110111

111-
return [addr for addr in data if isinstance(addr, str)]
112+
return set(addr for addr in data if isinstance(addr, str))
112113

113114
except requests.RequestException as e:
114115
logging.error(f"Request failed for block {block}: {str(e)}")
115-
return []
116+
return set()
116117
except Exception as e:
117118
logging.error(f"Error processing participants for block {block}: {str(e)}")
118-
return []
119+
return set()
119120

120121
if __name__ == "__main__":
121122
example_integration = EchelonAptosIntegration(

integrations/echelon_xlpt_integration.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import json
44
import requests
55

6-
from typing import Dict, List, Set
6+
from typing import Dict, List, Optional, Set
77
from dotenv import load_dotenv
88
from constants.summary_columns import SummaryColumn
99
from constants.example_integrations import (
@@ -97,7 +97,8 @@ def get_participants_data(self, block, user_addresses=[]):
9797
print(f"Unexpected error: {e}")
9898
raise
9999

100-
def get_participants(self, block: int) -> List[str]:
100+
def get_participants(self, blocks: Optional[List[int]] = None) -> Set[str]:
101+
block = blocks[0] if blocks else 0
101102
try:
102103
response = requests.get(
103104
f"{ETHENA_SUSDE_USDC_XLPT_ADDRESS_API_URL}?block={block}",
@@ -108,16 +109,16 @@ def get_participants(self, block: int) -> List[str]:
108109
data = response.json()['data']
109110
if not isinstance(data, list):
110111
logging.warning(f"Unexpected response format from API: {data}")
111-
return []
112+
return set()
112113

113-
return [addr for addr in data if isinstance(addr, str)]
114+
return set(addr for addr in data if isinstance(addr, str))
114115

115116
except requests.RequestException as e:
116117
logging.error(f"Request failed for block {block}: {str(e)}")
117-
return []
118+
return set()
118119
except Exception as e:
119120
logging.error(f"Error processing participants for block {block}: {str(e)}")
120-
return []
121+
return set()
121122

122123
if __name__ == "__main__":
123124
example_integration = EchelonAptosIntegration(

integrations/evaa_integration.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Dict, List, Optional, Tuple
1+
from typing import Any, Dict, List, Optional, Tuple
22

33
from constants.evaa import EVAA_POOLS_MAP, EVAA_ENDPOINT, EVAA_USDE_START_BLOCK, EVAA_SUSDE_START_BLOCK
44

@@ -42,28 +42,29 @@ def get_l2_block_balances(
4242
for block_number in blocks:
4343
block_balances[block_number] = {}
4444
block_data = self.get_participants_data(block_number)
45-
for participant in block_data:
45+
participants: List[Dict[str, Any]] = block_data if isinstance(block_data, list) else []
46+
for participant in participants:
4647
ton_address = participant["ton_address"]
47-
balance = participant["balance"]
48+
balance = float(participant["balance"])
4849
block_balances[block_number][ton_address] = balance
4950

5051
return block_balances
5152
except Exception as e:
52-
err_msg = f"Error fetching EVAA balances at block {block_number}: {e}"
53+
err_msg = f"Error fetching EVAA balances: {e}"
5354
print(err_msg)
5455
slack_message(err_msg)
56+
return {}
5557

5658
def get_token_symbol(self):
5759
return self.integration_id.get_token()
5860

59-
def get_participants_data(self, block: int) -> Dict[str, float]:
61+
def get_participants_data(self, block: int) -> List[Dict[str, Any]]:
6062
"""
61-
Returns a list of "ton_address": "balance"
63+
Returns a list of dicts with "ton_address" and "balance".
6264
"""
63-
6465
token = self.get_token_symbol()
6566
pools_list = EVAA_POOLS_MAP[token]
66-
block_data: Dict[str, float] = {}
67+
block_data: List[Dict[str, Any]] = []
6768
target_date = get_block_date(block, self.chain, adjustment=3600, fmt="%Y-%m-%dT%H:%M:%S")
6869

6970

@@ -84,7 +85,10 @@ def get_participants_data(self, block: int) -> Dict[str, float]:
8485
if payload is None:
8586
raise Exception(f"Error getting participants data for EVAA Protocol token {token} at block {block}: empty response")
8687

87-
block_data = payload
88+
if isinstance(payload, list):
89+
block_data.extend(payload)
90+
else:
91+
block_data.append(payload)
8892

8993
except Exception as e:
9094
err_msg = f"Error getting participants data for EVAA Protocol at block {block}: {e}"

integrations/merchantmoe_lbt_integration.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def get_participants(self, blocks: list[int] | None) -> set[str]:
8484
)
8585

8686
logging.info(
87-
f"[{self.name}] Scanning blocks {start_block} to {to_block}, received {len(transfers)} mETH/USDe transfer events"
87+
f"[{self.name}] Scanning blocks {start_block} to {to_block}, received {len(list(transfers))} mETH/USDe transfer events"
8888
)
8989
for transfer in transfers:
9090
from_address = transfer["args"]["from"]

integrations/nuri.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def get_participants(self, blocks: list[int] | None) -> set[str]:
128128
start_block,
129129
to_block,
130130
)
131-
print(f"Fetched {len(mint_events)} Mint events")
131+
print(f"Fetched {len(list(mint_events))} Mint events")
132132

133133
for event in mint_events:
134134
tx_hash = event["transactionHash"]

0 commit comments

Comments
 (0)