From 63a4bd6fcbd93437e25016c3ff1a3b625aebaf84 Mon Sep 17 00:00:00 2001 From: Shroominic Date: Fri, 25 Apr 2025 13:27:05 +0800 Subject: [PATCH] btcusd price fetcher --- proxy/price.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 proxy/price.py diff --git a/proxy/price.py b/proxy/price.py new file mode 100644 index 00000000..5dbec3af --- /dev/null +++ b/proxy/price.py @@ -0,0 +1,35 @@ +import os +import httpx +import asyncio + +# artifical spread to cover conversion fees +EXCHANGE_FEE = os.environ.get("EXCHANGE_FEE", 1.005) # 0.5% default + + +async def kraken_btc_usd(client: httpx.AsyncClient) -> int: + api = "https://api.kraken.com/0/public/Ticker?pair=XBTUSD" + return int(float((await client.get(api)).json()["result"]["XXBTZUSD"]["c"][0])) + + +async def coinbase_btc_usd(client: httpx.AsyncClient) -> int: + api = "https://api.coinbase.com/v2/prices/BTC-USD/spot" + return int(float((await client.get(api)).json()["data"]["amount"])) + + +async def binance_btc_usdt(client: httpx.AsyncClient) -> int: + api = "https://api.binance.com/api/v3/ticker/price?symbol=BTCUSDT" + return int(float((await client.get(api)).json()["price"])) + + +async def btc_usd_ask_price() -> int: + async with httpx.AsyncClient() as client: + return int( + max( + await asyncio.gather( + kraken_btc_usd(client), + coinbase_btc_usd(client), + binance_btc_usdt(client), + ) + ) + * EXCHANGE_FEE + )