11import json
2+ from typing import Any , Set
23from web3 import Web3
34from utils .web3_utils import (
5+ call_with_retry ,
6+ fetch_events_logs_with_retry ,
47 w3 ,
58)
69
1215with open ("abi/firm_simple_escrow.json" ) as f :
1316 firm_simple_escrow_abi = json .load (f )
1417
18+ with open ("abi/curve_stable_swap_ng_lp.json" ) as f :
19+ curve_stable_swap_ng_lp_abi = json .load (f )
1520
16- firm_susde_market_contract = w3 .eth .contract (
17- address = SUSDE_MARKET_ADDRESS , abi = firm_market_abi
18- )
21+ with open ("abi/yearn_v2_vault.json" ) as f :
22+ yearn_v2_vault_abi = json .load (f )
1923
24+ def get_firm_market_contract (marketAddress : str ):
25+ return w3 .eth .contract (
26+ address = Web3 .to_checksum_address (marketAddress ), abi = firm_market_abi
27+ )
2028
21- def get_escrow_contract (user_address : str ):
22- escrow_address : str = firm_susde_market_contract .functions .escrows (
29+ def get_escrow_contract (user_address : str , marketAddress : str ):
30+ escrow_address : str = get_firm_market_contract ( marketAddress ) .functions .escrows (
2331 user_address
2432 ).call ()
2533 return w3 .eth .contract (
2634 address = Web3 .to_checksum_address (escrow_address ), abi = firm_simple_escrow_abi
2735 )
36+
37+ def get_curve_lp_contract (lpAddress : str ):
38+ return w3 .eth .contract (
39+ address = Web3 .to_checksum_address (lpAddress ), abi = curve_stable_swap_ng_lp_abi
40+ )
41+
42+ def get_yearn_vault_v2_contract (yvAddress : str ):
43+ return w3 .eth .contract (
44+ address = Web3 .to_checksum_address (yvAddress ), abi = yearn_v2_vault_abi
45+ )
46+
47+ def get_firm_user_balance (user : str , marketAddress : str , block : int ) -> Any :
48+ # get user escrow
49+ escrow_contract = get_escrow_contract (user , marketAddress )
50+ # get the balance from the escrow
51+ balance = call_with_retry (
52+ escrow_contract .functions .balance (),
53+ block ,
54+ )
55+ return balance
56+
57+ def get_firm_market_participants (
58+ start_block : int ,
59+ marketAddress : str ,
60+ ) -> Set [str ]:
61+ page_size = 1900
62+ target_block = w3 .eth .get_block_number ()
63+
64+ firm_market_contract = get_firm_market_contract (marketAddress )
65+
66+ all_users = set ()
67+ while start_block < target_block :
68+ to_block = min (start_block + page_size , target_block )
69+ escrow_creations = fetch_events_logs_with_retry (
70+ f"Inverse Finance FiRM users from { start_block } to { to_block } " ,
71+ firm_market_contract .events .CreateEscrow (),
72+ start_block ,
73+ to_block ,
74+ )
75+ for escrow_creation in escrow_creations :
76+ all_users .add (escrow_creation ["args" ]["user" ])
77+ start_block += page_size
78+ return all_users
0 commit comments