Skip to content

Commit 032eefd

Browse files
authored
Merge pull request #81 from AlehNat/fiva-integration
add FIVA (TON) integration
2 parents 093638b + bdc3dde commit 032eefd

File tree

6 files changed

+147
-2
lines changed

6 files changed

+147
-2
lines changed

constants/chains.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ class Chain(Enum):
1414
SWELL = "Swell"
1515
SOLANA = "Solana"
1616
APTOS = "Aptos"
17-
TON = "TON"
1817
BASE = "Base"
19-
SEPOLIA = "Sepolia"
18+
SEPOLIA = "Sepolia"
19+
TON = "Ton"

constants/example_integrations.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,5 @@
2222
ECHELON_SUSDE_COLLATERAL_START_BLOCK = 2379805052
2323

2424
RATEX_EXAMPLE_USDE_START_BLOCK = 21202656
25+
26+
FIVA_EXAMPLE_USDE_START_BLOCK = 22370179

constants/summary_columns.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ class SummaryColumn(Enum):
5555

5656
RATEX_EXAMPLE_PTS = ("ratex_example_pts", SummaryColumnType.ETHENA_PTS)
5757

58+
FIVA_EXAMPLE_PTS = ("fiva_example_pts", SummaryColumnType.ETHENA_PTS)
59+
5860
STONFI_USDE_PTS = ("stonfi_usde_pts", SummaryColumnType.ETHENA_PTS)
5961

6062
VENUS_SUSDE_PTS = ("venus_susde_pts", SummaryColumnType.ETHENA_PTS)
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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)

integrations/integration_ids.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,9 +436,12 @@ class IntegrationID(Enum):
436436

437437
RATEX_USDE_EXAMPLE = ("ratex_usde_example", "Ratex USDe Example", Token.USDE)
438438

439+
FIVA_USDE_EXAMPLE = ("fiva_usde_example", "Fiva USDe Example", Token.USDE)
440+
439441
# STON.fi
440442
STONFI_USDE = ("stonfi_usde", "STON.fi USDe", Token.USDE)
441443

444+
442445
# Upshift sUSDe
443446
UPSHIFT_UPSUSDE = ("upshift_upsusde", "Upshift upsUSDe", Token.SUSDE)
444447

utils/web3_utils.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@
7878
Chain.SEPOLIA: {
7979
"w3": w3_sepolia,
8080
},
81+
Chain.TON: {
82+
"w3": w3,
83+
},
8184
}
8285

8386

0 commit comments

Comments
 (0)