|
| 1 | +from typing import Dict, List, Optional, Tuple |
| 2 | + |
| 3 | +from constants.evaa import EVAA_POOLS_MAP, EVAA_ENDPOINT, EVAA_USDE_START_BLOCK, EVAA_SUSDE_START_BLOCK |
| 4 | + |
| 5 | +from integrations.l2_delegation_integration import L2DelegationIntegration |
| 6 | +from integrations.integration_ids import IntegrationID |
| 7 | +from utils.web3_utils import get_block_date |
| 8 | +from utils.request_utils import requests_retry_session |
| 9 | +from utils.slack import slack_message |
| 10 | +from constants.summary_columns import SummaryColumn |
| 11 | +from constants.chains import Chain |
| 12 | +class EvaaIntegration(L2DelegationIntegration): |
| 13 | + def __init__( |
| 14 | + self, |
| 15 | + integration_id: IntegrationID, |
| 16 | + start_block: int, |
| 17 | + summary_cols: Optional[List[SummaryColumn]] = None, |
| 18 | + chain: Chain = Chain.TON, |
| 19 | + reward_multiplier: int = 1, |
| 20 | + end_block: Optional[int] = None, |
| 21 | + ): |
| 22 | + super().__init__( |
| 23 | + integration_id=integration_id, |
| 24 | + start_block=start_block, |
| 25 | + chain=chain, |
| 26 | + summary_cols=summary_cols if summary_cols else [SummaryColumn.EVAA_USDE_PTS], |
| 27 | + reward_multiplier=reward_multiplier, |
| 28 | + end_block=end_block, |
| 29 | + ) |
| 30 | + |
| 31 | + def get_l2_block_balances( |
| 32 | + self, |
| 33 | + cached_data: Dict[int, Dict[str, float]], |
| 34 | + blocks: List[int] |
| 35 | + ) -> Dict[int, Dict[str, float]]: |
| 36 | + """ |
| 37 | + Returns a dict of the form: { block_number: { ton_address: balance } } |
| 38 | + """ |
| 39 | + try: |
| 40 | + block_balances: Dict[int, Dict[str, float]] = {} |
| 41 | + |
| 42 | + for block_number in blocks: |
| 43 | + block_balances[block_number] = {} |
| 44 | + block_data = self.get_participants_data(block_number) |
| 45 | + for participant in block_data: |
| 46 | + ton_address = participant["ton_address"] |
| 47 | + balance = participant["balance"] |
| 48 | + block_balances[block_number][ton_address] = balance |
| 49 | + |
| 50 | + return block_balances |
| 51 | + except Exception as e: |
| 52 | + err_msg = f"Error fetching EVAA balances at block {block_number}: {e}" |
| 53 | + print(err_msg) |
| 54 | + slack_message(err_msg) |
| 55 | + |
| 56 | + def get_token_symbol(self): |
| 57 | + return self.integration_id.get_token() |
| 58 | + |
| 59 | + def get_participants_data(self, block: int) -> Dict[str, float]: |
| 60 | + """ |
| 61 | + Returns a list of "ton_address": "balance" |
| 62 | + """ |
| 63 | + |
| 64 | + token = self.get_token_symbol() |
| 65 | + pools_list = EVAA_POOLS_MAP[token] |
| 66 | + block_data: Dict[str, float] = {} |
| 67 | + target_date = get_block_date(block, self.chain, adjustment=3600, fmt="%Y-%m-%dT%H:%M:%S") |
| 68 | + |
| 69 | + |
| 70 | + try: |
| 71 | + for pool in pools_list: |
| 72 | + res = requests_retry_session().get( |
| 73 | + EVAA_ENDPOINT + "/query/adapters/ethena", |
| 74 | + params={ |
| 75 | + "pool_address": pool, |
| 76 | + "timestamp": target_date, |
| 77 | + "token": token.value |
| 78 | + }, |
| 79 | + |
| 80 | + timeout=60, |
| 81 | + ) |
| 82 | + payload = res.json() |
| 83 | + |
| 84 | + if payload is None: |
| 85 | + raise Exception(f"Error getting participants data for EVAA Protocol token {token} at block {block}: {e}") |
| 86 | + |
| 87 | + block_data = payload |
| 88 | + |
| 89 | + except Exception as e: |
| 90 | + err_msg = f"Error getting participants data for EVAA Protocol at block {block}: {e}" |
| 91 | + slack_message(err_msg) |
| 92 | + |
| 93 | + return block_data |
| 94 | + |
| 95 | + |
| 96 | +if __name__ == "__main__": |
| 97 | + evaa_integration_usde = EvaaIntegration( |
| 98 | + integration_id=IntegrationID.EVAA_TON_USDE, |
| 99 | + start_block=EVAA_USDE_START_BLOCK, |
| 100 | + summary_cols=[SummaryColumn.EVAA_USDE_PTS], |
| 101 | + chain=Chain.TON, |
| 102 | + reward_multiplier=20 |
| 103 | + ) |
| 104 | + evaa_integration_susde = EvaaIntegration( |
| 105 | + integration_id=IntegrationID.EVAA_TON_SUSDE, |
| 106 | + start_block=EVAA_SUSDE_START_BLOCK, |
| 107 | + summary_cols=[SummaryColumn.EVAA_SUSDE_PTS], |
| 108 | + chain=Chain.TON, |
| 109 | + reward_multiplier=5 |
| 110 | + ) |
| 111 | + |
| 112 | + balances_usde = evaa_integration_usde.get_l2_block_balances( |
| 113 | + cached_data={}, blocks=[22596292] |
| 114 | + ) |
| 115 | + balances_susde = evaa_integration_susde.get_l2_block_balances( |
| 116 | + cached_data={}, blocks=[22596292] |
| 117 | + ) |
| 118 | + |
| 119 | + print("Balances USDe:", balances_usde) |
| 120 | + print("Balances sUSDe:", balances_susde) |
0 commit comments