Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Thala adapter #56

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions constants/chains.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ class Chain(Enum):
LYRA = "Lyra"
SWELL = "Swell"
SOLANA = "Solana"
APTOS = "Aptos"
BASE = "Base"
SEPOLIA = "Sepolia"
2 changes: 2 additions & 0 deletions constants/example_integrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@

KAMINO_SUSDE_COLLATERAL_START_BLOCK_EXAMPLE = 20471904

THALA_SUSDE_START_BLOCK = 2393932881

RATEX_EXAMPLE_USDE_START_BLOCK = 21202656
3 changes: 3 additions & 0 deletions constants/summary_columns.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ class SummaryColumn(Enum):
AMBIENT_SCROLL_SHARDS = ("ambient_scroll_shards", SummaryColumnType.ETHENA_PTS)
AMBIENT_SWELL_SHARDS = ("ambient_swell_shards", SummaryColumnType.ETHENA_PTS)


THALA_SHARDS = ("thala_shards", SummaryColumnType.ETHENA_PTS)

NURI_SHARDS = ("nuri_shards", SummaryColumnType.ETHENA_PTS)
LENDLE_MANTLE_SHARDS = ("lendle_mantle_shards", SummaryColumnType.ETHENA_PTS)

Expand Down
11 changes: 11 additions & 0 deletions constants/thala.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
SUSDE_LPT_COIN = "0x7ca61cf9aa2239412154145e863823814b9fec37ef34b469718c5f690919e69e::coins::Coin5"

SUSDE_LPT_PID = 27

ETHENA_ADDRESS_API_URL="https://app.thala.fi/api/ethena-addresses"

THALA_FARMING_V1_ADDRESS = "0x6b3720cd988adeaf721ed9d4730da4324d52364871a68eac62b46d21e4d2fa99"

THALASWAP_V2_ADDRESS = "0x7730cd28ee1cdc9e999336cbc430f99e7c44397c0aa77516f6f23a78559bb5"

SUSDE_LPT_ADDRESS = "0xce9e3b2437fd2cddc5c14f6c4259fc7d3cef160b820837591aa48170bb509368"
7 changes: 7 additions & 0 deletions integrations/integration_ids.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ class IntegrationID(Enum):
)
PENDLE_ZIRCUIT_USDE_YT = ("pendle_zircuit_usde_yt_held", "Pendle Zircuit USDe YT")

# Thala
THALA_SUSDE_LP = (
"thala_susde_usdc_lp",
"Thala sUSDe/USDC LP",
Token.SUSDE,
)

# Stake DAO
STAKEDAO_SUSDE_JULY_LPT = (
"stakedao_susde_july_effective_lpt_held",
Expand Down
147 changes: 147 additions & 0 deletions integrations/thala_integration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
import logging
import subprocess
import json
from typing import Dict, List, Optional
import requests

from dotenv import load_dotenv
from constants.summary_columns import SummaryColumn
from constants.example_integrations import (
THALA_SUSDE_START_BLOCK,
)
from constants.thala import (
ETHENA_ADDRESS_API_URL,
SUSDE_LPT_ADDRESS,
SUSDE_LPT_COIN,
SUSDE_LPT_PID,
THALA_FARMING_V1_ADDRESS,
THALASWAP_V2_ADDRESS,
)
from constants.chains import Chain
from integrations.integration_ids import IntegrationID as IntID
from integrations.l2_delegation_integration import L2DelegationIntegration

load_dotenv()


class ThalaAptosIntegration(L2DelegationIntegration):
def __init__(
self,
integration_id: IntID,
start_block: int,
token_address: str,
decimals: int,
chain: Chain = Chain.APTOS,
reward_multiplier: int = 1,
):
super().__init__(
integration_id=integration_id,
start_block=start_block,
chain=chain,
summary_cols=[SummaryColumn.THALA_SHARDS],
reward_multiplier=reward_multiplier,
)
self.token_address = token_address
self.decimals = str(decimals)
self.thala_ts_location = "ts/thala_balances.ts"

def get_l2_block_balances(
self, cached_data: Dict[int, Dict[str, float]], blocks: List[int]
) -> Dict[int, Dict[str, float]]:
logging.info("Getting block data for Thala sUSDe LP...")
# Ensure blocks are sorted smallest to largest
block_data: Dict[int, Dict[str, float]] = {}
sorted_blocks = sorted(blocks)

# Populate block data from smallest to largest
for block in sorted_blocks:
user_addresses = self.get_thala_block_participants(block)

result = self.get_thala_block_data(block, user_addresses)

# Store the balances and cache the exchange rate
if result:
block_data[block] = result

return block_data

def get_thala_block_participants(self, block: int) -> List[str]:
try:
response = requests.get(
f"{ETHENA_ADDRESS_API_URL}?block={block}", timeout=10
)
response.raise_for_status()

data = response.json()["data"]
if not isinstance(data, list):
logging.warning(f"Unexpected response format from API: {data}")
return []

return [addr for addr in data if isinstance(addr, str)]

except requests.RequestException as e:
logging.error(f"Request failed for block {block}: {str(e)}")
return []
except Exception as e:
logging.error(f"Error processing participants for block {block}: {str(e)}")
return []

def get_thala_block_data(
self, block: int, user_addresses: Optional[List[str]] = None
):
print("Getting participants data for block: ", block)
if not user_addresses:
user_addresses = []
try:
response = subprocess.run(
[
"ts-node",
self.thala_ts_location,
THALA_FARMING_V1_ADDRESS,
THALASWAP_V2_ADDRESS,
str(SUSDE_LPT_PID),
str(SUSDE_LPT_ADDRESS),
str(self.decimals),
str(block),
json.dumps(user_addresses),
],
capture_output=True,
text=True,
check=True,
)

try:
result = json.loads(response.stdout)
return result
except json.JSONDecodeError as e:
print(f"JSON Decode Error: {e}")
print(f"Raw output: {response.stdout}")
raise

except subprocess.CalledProcessError as e:
print(f"Process error: {e}")
print(f"stderr: {e.stderr}")
raise
except Exception as e:
print(f"Unexpected error: {e}")
raise


if __name__ == "__main__":
example_integration = ThalaAptosIntegration(
integration_id=IntID.THALA_SUSDE_LP,
start_block=THALA_SUSDE_START_BLOCK,
token_address=SUSDE_LPT_COIN,
decimals=8,
chain=Chain.APTOS,
reward_multiplier=5,
)

example_integration_output = example_integration.get_l2_block_balances(
cached_data={},
blocks=list(range(THALA_SUSDE_START_BLOCK, THALA_SUSDE_START_BLOCK + 25306000, 1500000)),
)

print("=" * 120)
print("Run without cached data", example_integration_output)
print("=" * 120, "\n" * 5)
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"dependencies": {
"@kamino-finance/klend-sdk": "^3.2.7",
"@solana/web3.js": "^1.95.2",
"dotenv": "^16.4.5"
"dotenv": "^16.4.5",
"@aptos-labs/ts-sdk": "1.33.1"
}
}
Loading