Skip to content

Commit d6b2bbf

Browse files
authored
Merge pull request #91 from webmass/inverse-firm-add-susde-lp-markets
integration: refacto firm & add susde lp markets
2 parents 48d5d15 + 41f1a86 commit d6b2bbf

File tree

8 files changed

+3358
-32
lines changed

8 files changed

+3358
-32
lines changed

abi/curve_stable_swap_ng_lp.json

Lines changed: 1452 additions & 0 deletions
Large diffs are not rendered by default.

abi/yearn_v2_vault.json

Lines changed: 1737 additions & 0 deletions
Large diffs are not rendered by default.

constants/firm.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,26 @@
11
from web3 import Web3
22

33
FIRM_SUSDE_DEPLOYMENT_BLOCK = 20441987
4+
FIRM_SUSDE_DOLA_CLP_DEPLOYMENT_BLOCK = 21311218
5+
FIRM_YV_SUSDE_DOLA_CLP_DEPLOYMENT_BLOCK = 21311218
46

57
SUSDE_MARKET_ADDRESS = Web3.to_checksum_address(
68
"0x79eF6d28C41e47A588E2F2ffB4140Eb6d952AEc4"
79
)
10+
11+
# SUSDE DOLA Curve LP
12+
SUSDE_DOLA_CLP_ADDRESS = Web3.to_checksum_address(
13+
"0x744793B5110f6ca9cC7CDfe1CE16677c3Eb192ef"
14+
)
15+
# SUSDE DOLA Curve LP Convex Firm Market
16+
SUSDE_DOLA_CLP_MARKET_ADDRESS = Web3.to_checksum_address(
17+
"0xb427fC22561f3963B04202F9bb5BCEbd76c14A99"
18+
)
19+
# SUSDE DOLA Curve LP Yearn Firm Market
20+
YV_SUSDE_DOLA_CLP_MARKET_ADDRESS = Web3.to_checksum_address(
21+
"0x4E264618dC015219CD83dbc53B31251D73c2db1a"
22+
)
23+
# SUSDE DOLA Yearn Vault Token
24+
YV_SUSDE_DOLA_ADDRESS = Web3.to_checksum_address(
25+
"0x1Fc80CfCF5B345b904A0fB36d4222196Ed9eB8a5"
26+
)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from typing import List, Optional, Set
2+
from constants.chains import Chain
3+
from integrations.integration_ids import IntegrationID
4+
from integrations.integration import Integration
5+
from constants.firm import FIRM_SUSDE_DOLA_CLP_DEPLOYMENT_BLOCK, SUSDE_DOLA_CLP_MARKET_ADDRESS
6+
from utils.firm import get_firm_market_participants, get_firm_user_balance
7+
from utils.web3_utils import w3
8+
9+
10+
class Firm(Integration):
11+
def __init__(self):
12+
super().__init__(
13+
IntegrationID.FIRM_SUSDE_DOLA_CLP,
14+
FIRM_SUSDE_DOLA_CLP_DEPLOYMENT_BLOCK,
15+
Chain.ETHEREUM,
16+
[],
17+
20,
18+
1,
19+
None,
20+
None,
21+
)
22+
23+
def get_balance(self, user: str, block: int) -> float:
24+
balance = get_firm_user_balance(user, SUSDE_DOLA_CLP_MARKET_ADDRESS, block)
25+
return balance / 1e18
26+
27+
def get_participants(
28+
self,
29+
blocks: Optional[List[int]],
30+
) -> Set[str]:
31+
return get_firm_market_participants(FIRM_SUSDE_DOLA_CLP_DEPLOYMENT_BLOCK, SUSDE_DOLA_CLP_MARKET_ADDRESS)
32+
33+
34+
if __name__ == "__main__":
35+
firm = Firm()
36+
participants = firm.get_participants(None)
37+
print("participants", participants)
38+
currentBlock = w3.eth.get_block_number()
39+
if len(participants) > 0:
40+
print(firm.get_balance(list(participants)[len(participants) - 1], currentBlock))

integrations/firm_susde.py

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
from constants.chains import Chain
33
from integrations.integration_ids import IntegrationID
44
from integrations.integration import Integration
5-
from constants.firm import FIRM_SUSDE_DEPLOYMENT_BLOCK
6-
from utils.firm import get_escrow_contract, firm_susde_market_contract
7-
from utils.web3_utils import w3, fetch_events_logs_with_retry, call_with_retry
5+
from constants.firm import FIRM_SUSDE_DEPLOYMENT_BLOCK, SUSDE_MARKET_ADDRESS
6+
from utils.firm import get_firm_market_participants, get_firm_user_balance
7+
from utils.web3_utils import w3
88

99

1010
class Firm(Integration):
@@ -21,36 +21,14 @@ def __init__(self):
2121
)
2222

2323
def get_balance(self, user: str, block: int) -> float:
24-
# get user escrow
25-
escrow_contract = get_escrow_contract(user)
26-
# get the balance from the escrow
27-
balance = call_with_retry(
28-
escrow_contract.functions.balance(),
29-
block,
30-
)
24+
balance = get_firm_user_balance(user, SUSDE_MARKET_ADDRESS, block)
3125
return balance / 1e18
3226

3327
def get_participants(
3428
self,
3529
blocks: Optional[List[int]],
3630
) -> Set[str]:
37-
page_size = 1900
38-
start_block = FIRM_SUSDE_DEPLOYMENT_BLOCK
39-
target_block = w3.eth.get_block_number()
40-
41-
all_users = set()
42-
while start_block < target_block:
43-
to_block = min(start_block + page_size, target_block)
44-
escrow_creations = fetch_events_logs_with_retry(
45-
f"Inverse Finance FiRM users from {start_block} to {to_block}",
46-
firm_susde_market_contract.events.CreateEscrow(),
47-
start_block,
48-
to_block,
49-
)
50-
for escrow_creation in escrow_creations:
51-
all_users.add(escrow_creation["args"]["user"])
52-
start_block += page_size
53-
return all_users
31+
return get_firm_market_participants(FIRM_SUSDE_DEPLOYMENT_BLOCK, SUSDE_MARKET_ADDRESS)
5432

5533

5634
if __name__ == "__main__":
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from typing import List, Optional, Set
2+
from constants.chains import Chain
3+
from integrations.integration_ids import IntegrationID
4+
from integrations.integration import Integration
5+
from constants.firm import FIRM_YV_SUSDE_DOLA_CLP_DEPLOYMENT_BLOCK, YV_SUSDE_DOLA_ADDRESS, YV_SUSDE_DOLA_CLP_MARKET_ADDRESS
6+
from utils.firm import get_firm_market_participants, get_firm_user_balance, get_yearn_vault_v2_contract
7+
from utils.web3_utils import w3, call_with_retry
8+
9+
10+
class Firm(Integration):
11+
def __init__(self):
12+
super().__init__(
13+
IntegrationID.FIRM_YV_SUSDE_DOLA_CLP,
14+
FIRM_YV_SUSDE_DOLA_CLP_DEPLOYMENT_BLOCK,
15+
Chain.ETHEREUM,
16+
[],
17+
20,
18+
1,
19+
None,
20+
None,
21+
)
22+
23+
def get_balance(self, user: str, block: int) -> float:
24+
balance = get_firm_user_balance(user, YV_SUSDE_DOLA_CLP_MARKET_ADDRESS, block)
25+
# Yearn-specific logic
26+
yearn_vault_v2_contract = get_yearn_vault_v2_contract(YV_SUSDE_DOLA_ADDRESS)
27+
# get the price_per_share to then get the underlying lp balance
28+
price_per_share = call_with_retry(
29+
yearn_vault_v2_contract.functions.pricePerShare(),
30+
block,
31+
)
32+
return balance / 1e18 * price_per_share / 1e18
33+
34+
def get_participants(
35+
self,
36+
blocks: Optional[List[int]],
37+
) -> Set[str]:
38+
return get_firm_market_participants(FIRM_YV_SUSDE_DOLA_CLP_DEPLOYMENT_BLOCK, YV_SUSDE_DOLA_CLP_MARKET_ADDRESS)
39+
40+
41+
if __name__ == "__main__":
42+
firm = Firm()
43+
participants = firm.get_participants(None)
44+
print("participants", participants)
45+
currentBlock = w3.eth.get_block_number()
46+
if len(participants) > 0:
47+
print(firm.get_balance(list(participants)[len(participants) - 1], currentBlock))

integrations/integration_ids.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,8 @@ class IntegrationID(Enum):
398398

399399
# Inverse Finance FiRM
400400
FIRM_SUSDE = ("firm_susde", "Inverse Finance FiRM sUSDe", Token.SUSDE)
401+
FIRM_SUSDE_DOLA_CLP = ("firm_susde_dola_clp", "Inverse Finance FiRM sUSDe/DOLA CLP", Token.SUSDE)
402+
FIRM_YV_SUSDE_DOLA_CLP = ("firm_yv_susde_dola_clp", "Inverse Finance FiRM YV sUSDe/DOLA CLP", Token.SUSDE)
401403

402404
# Hyperdrive
403405
HYPERDRIVE_SUSDE = (

utils/firm.py

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import json
2+
from typing import Any, Set
23
from web3 import Web3
34
from utils.web3_utils import (
5+
call_with_retry,
6+
fetch_events_logs_with_retry,
47
w3,
58
)
69

@@ -12,16 +15,64 @@
1215
with 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

Comments
 (0)