Skip to content

Commit f30e1b5

Browse files
committed
first commit
0 parents  commit f30e1b5

File tree

2 files changed

+114
-0
lines changed

2 files changed

+114
-0
lines changed

.env.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
TOKEN_CODE=OVRL
2+
TOKEN_ISSUER=GBZH36ATUXJZKFRMQTAAW42MWNM34SOA4N6E7DQ62V3G5NVITC3QOVRL

main.py

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import os
2+
import time
3+
import requests
4+
from flask import Flask, jsonify
5+
from stellar_sdk.server import Server, Asset
6+
from dotenv import load_dotenv
7+
import threading
8+
9+
load_dotenv()
10+
TOKEN_CODE = os.getenv("TOKEN_CODE", "OVRL")
11+
TOKEN_ISSUER = os.getenv("TOKEN_ISSUER", "GBZH36ATUXJZKFRMQTAAW42MWNM34SOA4N6E7DQ62V3G5NVITC3QOVRL")
12+
13+
server = Server(horizon_url="https://horizon.stellar.org")
14+
app = Flask(__name__)
15+
token_price = {"xlm": "0.0000000", "usd": "0.0000000"}
16+
17+
def fetch_price():
18+
global token_price
19+
while True:
20+
try:
21+
last_trade_price_xlm = 0.0
22+
amm_price_xlm = 0.0
23+
24+
# Get trade price
25+
try:
26+
trades = server.trades().for_asset_pair(
27+
base=Asset(TOKEN_CODE, TOKEN_ISSUER),
28+
counter=Asset.native()
29+
).limit(1).order("desc").call()
30+
31+
if trades["_embedded"]["records"]:
32+
trade = trades["_embedded"]["records"][0]
33+
if "price" in trade and "n" in trade["price"] and "d" in trade["price"]:
34+
last_trade_price_xlm = float(trade["price"]["n"]) / float(trade["price"]["d"])
35+
except Exception:
36+
pass
37+
38+
# Get pool price
39+
try:
40+
token_asset = Asset(TOKEN_CODE, TOKEN_ISSUER)
41+
native_asset = Asset.native()
42+
43+
pool_response = server.liquidity_pools().for_reserves([token_asset, native_asset]).call()
44+
45+
if pool_response["_embedded"]["records"]:
46+
for pool in pool_response["_embedded"]["records"]:
47+
reserves = pool["reserves"]
48+
xlm_amount = 0.0
49+
token_amount = 0.0
50+
51+
for reserve in reserves:
52+
asset_str = reserve["asset"]
53+
if asset_str == "native":
54+
xlm_amount = float(reserve["amount"])
55+
elif TOKEN_CODE in asset_str and TOKEN_ISSUER in asset_str:
56+
token_amount = float(reserve["amount"])
57+
58+
if xlm_amount > 0 and token_amount > 0:
59+
amm_price_xlm = xlm_amount / token_amount
60+
break
61+
else:
62+
pool_response = server.liquidity_pools().for_reserves([token_asset]).call()
63+
64+
if pool_response["_embedded"]["records"]:
65+
for pool in pool_response["_embedded"]["records"]:
66+
reserves = pool["reserves"]
67+
xlm_amount = 0.0
68+
token_amount = 0.0
69+
70+
for reserve in reserves:
71+
asset_str = reserve["asset"]
72+
if asset_str == "native":
73+
xlm_amount = float(reserve["amount"])
74+
elif TOKEN_CODE in asset_str and TOKEN_ISSUER in asset_str:
75+
token_amount = float(reserve["amount"])
76+
77+
if xlm_amount > 0 and token_amount > 0:
78+
amm_price_xlm = xlm_amount / token_amount
79+
break
80+
except Exception:
81+
pass
82+
83+
# Choose best price
84+
final_price_xlm = amm_price_xlm if amm_price_xlm > 0 else last_trade_price_xlm
85+
86+
# Get XLM/USD price
87+
xlm_usd = 0.0
88+
try:
89+
response = requests.get("https://api.kraken.com/0/public/Ticker?pair=XLMUSD", timeout=5)
90+
data = response.json()
91+
xlm_usd = float(data["result"]["XXLMZUSD"]["c"][0])
92+
except Exception:
93+
pass
94+
95+
# Update price
96+
price_usd = final_price_xlm * xlm_usd
97+
if final_price_xlm > 0:
98+
token_price = {"xlm": f"{final_price_xlm:.7f}", "usd": f"{price_usd:.7f}"}
99+
100+
except Exception:
101+
pass
102+
103+
time.sleep(5)
104+
105+
threading.Thread(target=fetch_price, daemon=True).start()
106+
107+
@app.route("/price", methods=["GET"])
108+
def get_price():
109+
return jsonify(token_price)
110+
111+
if __name__ == "__main__":
112+
app.run(host="0.0.0.0", port=5000)

0 commit comments

Comments
 (0)