|
| 1 | +import json |
| 2 | +from typing import Dict, List, Optional, Tuple |
| 3 | + |
| 4 | +from constants.affluent import AFFLUENT_ENDPOINT, AFFLUENT_USDE_START_BLOCK, AFFLUENT_SUSDE_START_BLOCK, TOKEN_ADDRESS_MAP |
| 5 | + |
| 6 | +from integrations.l2_delegation_integration import L2DelegationIntegration |
| 7 | +from integrations.integration_ids import IntegrationID |
| 8 | +from utils.web3_utils import get_block_date |
| 9 | +from utils.request_utils import requests_retry_session |
| 10 | +from utils.slack import slack_message |
| 11 | +from constants.summary_columns import SummaryColumn |
| 12 | +from constants.chains import Chain |
| 13 | + |
| 14 | +class AffluentIntegration(L2DelegationIntegration): |
| 15 | + def __init__( |
| 16 | + self, |
| 17 | + integration_id: IntegrationID, |
| 18 | + start_block: int, |
| 19 | + summary_cols: Optional[List[SummaryColumn]] = None, |
| 20 | + chain: Chain = Chain.TON, |
| 21 | + reward_multiplier: int = 1, |
| 22 | + end_block: Optional[int] = None, |
| 23 | + ): |
| 24 | + super().__init__( |
| 25 | + integration_id=integration_id, |
| 26 | + start_block=start_block, |
| 27 | + chain=chain, |
| 28 | + summary_cols=summary_cols if summary_cols else [SummaryColumn.AFFLUENT_USDE_PTS], |
| 29 | + reward_multiplier=reward_multiplier, |
| 30 | + end_block=end_block, |
| 31 | + ) |
| 32 | + |
| 33 | + def get_l2_block_balances( |
| 34 | + self, |
| 35 | + cached_data: Dict[int, Dict[str, float]], |
| 36 | + blocks: List[int] |
| 37 | + ) -> Dict[int, Dict[str, float]]: |
| 38 | + try: |
| 39 | + block_balances: Dict[int, Dict[str, float]] = {} |
| 40 | + |
| 41 | + for target_block in blocks: |
| 42 | + block_balances[target_block] = self.get_participants_data(target_block) |
| 43 | + |
| 44 | + return block_balances |
| 45 | + except Exception as e: |
| 46 | + err_msg = f"Error fetching Affluent balances at block {target_block}: {e}" |
| 47 | + print(err_msg) |
| 48 | + slack_message(err_msg) |
| 49 | + |
| 50 | + def get_token_symbol(self): |
| 51 | + return self.integration_id.get_token() |
| 52 | + |
| 53 | + def get_participants_data(self, block: int) -> Dict[str, float]: |
| 54 | + token = self.get_token_symbol() |
| 55 | + token_address = TOKEN_ADDRESS_MAP[token] |
| 56 | + block_data: Dict[str, float] = {} |
| 57 | + target_date = get_block_date(block, self.chain, adjustment=3600, fmt="%Y-%m-%dT%H:%M:%S") |
| 58 | + |
| 59 | + try: |
| 60 | + res = requests_retry_session().get( |
| 61 | + AFFLUENT_ENDPOINT + "/holdings", |
| 62 | + params={ |
| 63 | + "assets": token_address, |
| 64 | + "timestamp": target_date, |
| 65 | + }, |
| 66 | + timeout=60, |
| 67 | + ) |
| 68 | + payload = res.json() |
| 69 | + |
| 70 | + if payload is None: |
| 71 | + raise Exception(f"Error getting participants data for Affluent Protocol token {token} at block {block}: {e}") |
| 72 | + |
| 73 | + for user_data in payload["result"]: |
| 74 | + block_data[user_data["user_address"]] = user_data["balance"] |
| 75 | + |
| 76 | + except Exception as e: |
| 77 | + err_msg = f"Error getting participants data for Affluent Protocol at block {block}: {e}" |
| 78 | + slack_message(err_msg) |
| 79 | + |
| 80 | + return block_data |
| 81 | + |
| 82 | + |
| 83 | +if __name__ == "__main__": |
| 84 | + affluent_integration_usde = AffluentIntegration( |
| 85 | + integration_id=IntegrationID.AFFLUENT_USDE, |
| 86 | + start_block=AFFLUENT_USDE_START_BLOCK, |
| 87 | + summary_cols=[SummaryColumn.AFFLUENT_USDE_PTS], |
| 88 | + chain=Chain.TON, |
| 89 | + reward_multiplier=20 |
| 90 | + ) |
| 91 | + affluent_integration_susde = AffluentIntegration( |
| 92 | + integration_id=IntegrationID.AFFLUENT_SUSDE, |
| 93 | + start_block=AFFLUENT_SUSDE_START_BLOCK, |
| 94 | + summary_cols=[SummaryColumn.AFFLUENT_SUSDE_PTS], |
| 95 | + chain=Chain.TON, |
| 96 | + reward_multiplier=5 |
| 97 | + ) |
| 98 | + |
| 99 | + print("Affluent USDe :", json.dumps(affluent_integration_usde.get_l2_block_balances(cached_data={}, blocks=[22895215, 22895215 - 600]), indent=2)) |
| 100 | + print("Affluent sUSDe :", json.dumps(affluent_integration_susde.get_l2_block_balances(cached_data={}, blocks=[22895215]), indent=2)) |
| 101 | + |
0 commit comments