Skip to content

Commit

Permalink
fetch yield and token data
Browse files Browse the repository at this point in the history
  • Loading branch information
Bosun-Josh121 authored and tosoham committed Jan 26, 2025
1 parent 77275ad commit 4819605
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions fetch_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import os
import json
import requests

YIELD_API_URL = "https://yields.llama.fi/pools"
TOKEN_API_URL = "https://starknet.api.avnu.fi/v1/starknet/tokens"

DATA_DIR = "./data"
YIELD_DIR = f"{DATA_DIR}/yields"

os.makedirs(YIELD_DIR, exist_ok=True)

def save_json(data, filepath):
try:
with open(filepath, "w") as file:
json.dump(data, file, indent=4)
print(f"✅ Saved: {filepath}")
except IOError as e:
print(f"❌ Error saving {filepath}: {e}")

def fetch_yield_data():

try:
response = requests.get(YIELD_API_URL, timeout=10)
response.raise_for_status()
yield_data = response.json().get("data", [])
chain_data = {}
for pool in yield_data:
chain = pool.get("chain", "unknown").lower()
if chain not in chain_data:
chain_data[chain] = []
chain_data[chain].append(pool)

for chain, pools in chain_data.items():
save_json(pools, f"{YIELD_DIR}/{chain}.json")

print(f"✅ Fetched and saved yield data for {len(chain_data)} chains.")

except (requests.exceptions.RequestException, IOError, json.JSONDecodeError) as e:
print(f"❌ Error fetching yield data: {e}")

def fetch_token_data():
try:
response = requests.get(TOKEN_API_URL, timeout=10)
response.raise_for_status()
token_data = response.json().get("content", [])

save_json(token_data, f"{DATA_DIR}/tokens.json")
print(f"✅ Fetched and saved {len(token_data)} tokens successfully.")

except (requests.exceptions.RequestException, IOError, json.JSONDecodeError) as e:
print(f"❌ Error fetching token data: {e}")

fetch_yield_data()
fetch_token_data()

0 comments on commit 4819605

Please sign in to comment.