|
| 1 | +import logging |
| 2 | + |
| 3 | +from typing import Dict, List, Optional |
| 4 | +from constants.example_integrations import FIVA_EXAMPLE_USDE_START_BLOCK |
| 5 | +from constants.integration_token import Token |
| 6 | +from integrations.l2_delegation_integration import L2DelegationIntegration |
| 7 | +from utils.web3_utils import get_block_date |
| 8 | +from constants.chains import Chain |
| 9 | +from constants.summary_columns import SummaryColumn |
| 10 | +from integrations.integration_ids import IntegrationID |
| 11 | +from utils.request_utils import requests_retry_session |
| 12 | +from utils.slack import slack_message |
| 13 | + |
| 14 | +FIVA_ENDPOINT = "http://api2.thefiva.com/ethena_points" |
| 15 | + |
| 16 | +TOKEN_TO_METHOD = { |
| 17 | + Token.USDE: "queryUSDEPoints", |
| 18 | +} |
| 19 | + |
| 20 | + |
| 21 | +def get_fiva_payload( |
| 22 | + trade_date: str, method: str = "queryUSDEPoints", page_num: int = 0 |
| 23 | +) -> dict: |
| 24 | + return { |
| 25 | + "method": method, |
| 26 | + "content": { |
| 27 | + "trade_date": trade_date, |
| 28 | + "page_num": page_num, |
| 29 | + "page_size": 200, |
| 30 | + }, |
| 31 | + } |
| 32 | + |
| 33 | + |
| 34 | +class FivaL2DelegationExampleIntegration(L2DelegationIntegration): |
| 35 | + def __init__( |
| 36 | + self, |
| 37 | + integration_id: IntegrationID, |
| 38 | + start_block: int, |
| 39 | + summary_cols: Optional[List[SummaryColumn]] = None, |
| 40 | + chain: Chain = Chain.TON, |
| 41 | + reward_multiplier: int = 1, |
| 42 | + end_block: Optional[int] = None, |
| 43 | + ): |
| 44 | + |
| 45 | + super().__init__( |
| 46 | + integration_id=integration_id, |
| 47 | + start_block=start_block, |
| 48 | + chain=chain, |
| 49 | + summary_cols=( |
| 50 | + summary_cols |
| 51 | + if summary_cols |
| 52 | + else [ |
| 53 | + SummaryColumn.FIVA_EXAMPLE_PTS, |
| 54 | + ] |
| 55 | + ), |
| 56 | + reward_multiplier=reward_multiplier, |
| 57 | + end_block=end_block, |
| 58 | + ) |
| 59 | + self.token = integration_id.get_token() |
| 60 | + |
| 61 | + def get_l2_block_balances( |
| 62 | + self, cached_data: Dict[int, Dict[str, float]], blocks: List[int] |
| 63 | + ) -> Dict[int, Dict[str, float]]: |
| 64 | + logging.info( |
| 65 | + f"Getting block data for FIVA L2 (TON) delegation example and blocks {blocks}..." |
| 66 | + ) |
| 67 | + |
| 68 | + data_per_block: Dict[int, Dict[str, float]] = {} |
| 69 | + |
| 70 | + for target_block in blocks: |
| 71 | + if self.start_block > target_block or ( |
| 72 | + self.end_block and target_block > self.end_block |
| 73 | + ): |
| 74 | + data_per_block[target_block] = {} |
| 75 | + continue |
| 76 | + data_per_block[target_block] = self.get_participants_data(target_block) |
| 77 | + return data_per_block |
| 78 | + |
| 79 | + def get_participants_data(self, block: int) -> Dict[str, float]: |
| 80 | + logging.info( |
| 81 | + f"Fetching participants data for FIVA L2 (TON) delegation example at block {block}..." |
| 82 | + ) |
| 83 | + block_data: Dict[str, int | float] = {} |
| 84 | + target_date = get_block_date(block, self.chain, adjustment=3600) |
| 85 | + page_num = 0 |
| 86 | + while True: |
| 87 | + try: |
| 88 | + res = requests_retry_session().get( |
| 89 | + FIVA_ENDPOINT, |
| 90 | + json=get_fiva_payload( |
| 91 | + trade_date=target_date, |
| 92 | + method=TOKEN_TO_METHOD[self.token], |
| 93 | + page_num=page_num, |
| 94 | + ), |
| 95 | + timeout=60, |
| 96 | + ) |
| 97 | + response = res.json() |
| 98 | + if len(block_data) >= response["total_size"] : |
| 99 | + break |
| 100 | + for pos_data in response["data"]: |
| 101 | + if ( |
| 102 | + pos_data["market_indicator"].upper() |
| 103 | + != self.integration_id.get_token().value.upper() |
| 104 | + ): |
| 105 | + continue |
| 106 | + user_address = pos_data["user_id"] |
| 107 | + block_data[user_address] = round(pos_data["token_amount"], 4) |
| 108 | + page_num += 1 |
| 109 | + except Exception as e: |
| 110 | + err_msg = f"Error getting participants data for FIVA L2 (TON) delegation example: {e}" |
| 111 | + logging.error(err_msg) |
| 112 | + slack_message(err_msg) |
| 113 | + break |
| 114 | + |
| 115 | + return block_data |
| 116 | + |
| 117 | + |
| 118 | +if __name__ == "__main__": |
| 119 | + example_integration = FivaL2DelegationExampleIntegration( |
| 120 | + integration_id=IntegrationID.FIVA_USDE_EXAMPLE, |
| 121 | + start_block=FIVA_EXAMPLE_USDE_START_BLOCK, |
| 122 | + summary_cols=[ |
| 123 | + SummaryColumn.FIVA_EXAMPLE_PTS, |
| 124 | + ], |
| 125 | + chain=Chain.TON, |
| 126 | + reward_multiplier=1, |
| 127 | + ) |
| 128 | + |
| 129 | + example_integration_output = example_integration.get_l2_block_balances( |
| 130 | + cached_data={}, blocks=[22370718, 22370728] |
| 131 | + ) |
| 132 | + |
| 133 | + print("=" * 120) |
| 134 | + print("Run without cached data", example_integration_output) |
| 135 | + print("=" * 120, "\n" * 5) |
0 commit comments