Skip to content

Commit

Permalink
api: use diadata as main datasource (#1277)
Browse files Browse the repository at this point in the history
* api: use diadata as main datasource

* api: add header to select price source

---------

Co-authored-by: tomjeatt <[email protected]>
  • Loading branch information
ns212 and tomjeatt authored Jun 19, 2023
1 parent 1b48685 commit 7669181
Showing 1 changed file with 55 additions and 5 deletions.
60 changes: 55 additions & 5 deletions api/market_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@ def add_header(response):
response.cache_control.s_maxage = 300
return response


@app.route("/marketdata/price", methods=["GET"])
def get_price():
args = request.args

def coingecko(args):
headers_dict = {
"content-type": "application/json",
"accept": "application/json",
Expand All @@ -28,6 +24,60 @@ def get_price():
url = "https://api.coingecko.com/api/v3/simple/price"
resp = requests.get(url, params=args, headers=headers_dict)
data = resp.json()
return data

def dia(asset):
headers_dict = {
"content-type": "application/json",
"accept": "application/json",
"x-cg-pro-api-key": api_key,
}
url = "https://api.diadata.org/v1/assetQuotation"
if asset == "bitcoin":
url += "/Bitcoin/0x0000000000000000000000000000000000000000"
elif asset == "interlay":
url += "/Interlay/0x0000000000000000000000000000000000000000"
elif asset == "liquid-staking-dot":
return { "liquid-staking-dot": None }
elif asset == "polkadot":
url += "/Polkadot/0x0000000000000000000000000000000000000000/"
elif asset == "tether":
url += "/Ethereum/0xdAC17F958D2ee523a2206206994597C13D831ec7"

resp = requests.get(url, headers=headers_dict)
data = resp.json()

return {
data["Name"].lower(): {
"usd": data["Price"],
}
}


@app.route("/marketdata/price", methods=["GET"])
def get_price():
args = request.args

price_source = request.headers.get('x-price-source')

data = {}

def _dia():
ticker_ids = args["ids"].split(",")
for ticker_id in ticker_ids:
data.update(dia(ticker_id))

if price_source == "dia":
_dia()
elif price_source == "coingecko":
data = coingecko(args)
else:
try:
_dia()
except Exception as e:
print("Error", e)
data = coingecko(args)

return jsonify(data)


Expand Down

2 comments on commit 7669181

@vercel
Copy link

@vercel vercel bot commented on 7669181 Jun 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 7669181 Jun 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.