Last active
December 26, 2024 09:25
-
-
Save tgreiser/cb11797a543e233e198151cb91d28a98 to your computer and use it in GitHub Desktop.
Sirius DEX balance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import datetime | |
from datetime import timezone | |
import pytz | |
# Constants | |
TEZOS_CONTRACT = "KT1TxqZ8QtKvLu3V3JH7Gx58n7Co8pgtpQU5" | |
TZKT_API_BASE = "https://api.tzkt.io/v1" | |
COINGECKO_API_BASE = "https://api.coingecko.com/api/v3" | |
HISTORICAL_DATA_FILE = "historical_data.csv" | |
def convert_to_utc(date_str, from_timezone="America/Los_Angeles"): | |
"""Convert local time to UTC timestamp, using current time of day""" | |
local_tz = pytz.timezone(from_timezone) | |
# Get current time in the local timezone | |
current_time = datetime.datetime.now(local_tz) | |
# Parse the date string and combine with current time | |
local_date = datetime.datetime.strptime(date_str, "%Y-%m-%d") | |
local_datetime = local_tz.localize( | |
datetime.datetime.combine(local_date.date(), current_time.time()) | |
) | |
# Convert to UTC | |
utc_datetime = local_datetime.astimezone(timezone.utc) | |
return utc_datetime.strftime("%Y-%m-%dT%H:%M:%SZ") | |
def get_contract_storage(contract_address, level = ""): | |
url = f"{TZKT_API_BASE}/contracts/{contract_address}/storage" | |
if level: | |
url += f"?level={level}" | |
response = requests.get(url) | |
response.raise_for_status() | |
return response.json() | |
def get_level_by_timestamp(timestamp): | |
response = requests.get(f"{TZKT_API_BASE}/blocks/{timestamp}/level") | |
response.raise_for_status() | |
return response.json() | |
def get_historical_price(date_str, coin, from_timezone="America/Los_Angeles"): | |
"""Get historical price adjusting for timezone and current time""" | |
local_tz = pytz.timezone(from_timezone) | |
# Get current time in the local timezone | |
current_time = datetime.datetime.now(local_tz) | |
# Parse the input date and combine with current time | |
local_date = datetime.datetime.strptime(date_str, "%Y-%m-%d") | |
local_datetime = local_tz.localize( | |
datetime.datetime.combine(local_date.date(), current_time.time()) | |
) | |
# Convert to UTC | |
utc_datetime = local_datetime.astimezone(timezone.utc) | |
# If UTC time is past midnight (e.g., after 4PM PST), use next day's data | |
if utc_datetime.hour >= 0: # Adjust based on when you want the cutoff to be | |
local_datetime = local_datetime + datetime.timedelta(days=1) | |
utc_datetime = local_datetime.astimezone(timezone.utc) | |
# Format date for CoinGecko (they expect DD-MM-YYYY) | |
dmyDate = utc_datetime.strftime("%d-%m-%Y") | |
url = f"https://api.coingecko.com/api/v3/coins/{coin}/history" | |
params = {"date": dmyDate, "localization": "false"} | |
response = requests.get(url, params=params) | |
response.raise_for_status() | |
data = response.json() | |
return data["market_data"]["current_price"]["usd"] | |
def calculate_share_price(storage, xtz_usd_price, btc_usd_price): | |
xtz_balance = float(storage["xtzPool"]) / 1000000 | |
btc_balance = float(storage["tokenPool"]) / 100000000 | |
lqt_total = float(storage["lqtTotal"]) | |
usd_value = xtz_balance * xtz_usd_price + btc_balance * btc_usd_price | |
return usd_value / lqt_total | |
def save_data(date, usd_value, lqt_total, share_price): | |
with open(HISTORICAL_DATA_FILE, "a") as file: | |
file.write(f"{date},{usd_value},{lqt_total},{share_price}\n") | |
def run_date(date, timezone="America/Los_Angeles"): | |
# Convert the input date to UTC for the API call | |
utc_timestamp = convert_to_utc(date, timezone) | |
level = get_level_by_timestamp(utc_timestamp) | |
storage = get_contract_storage(TEZOS_CONTRACT, level) | |
xtz_usd_price = get_historical_price(date, "tezos", timezone) | |
btc_usd_price = get_historical_price(date, "bitcoin", timezone) | |
share_price = calculate_share_price(storage, xtz_usd_price, btc_usd_price) | |
return share_price |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment