Skip to content

Commit

Permalink
api: refactor the market data api (#1569)
Browse files Browse the repository at this point in the history
* api: refactor the market data api

* api: refactor the market data api

* api: refactor the market data api
  • Loading branch information
ns212 authored Sep 27, 2023
1 parent b87943d commit 711bc4e
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 107 deletions.
103 changes: 103 additions & 0 deletions api/market_data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// Dia to Coingecko names
const tickers = {
"Tether USD": "tether",
"Acala USD": "acala-dollar",
"BNB": "binancecoin",
"Wrapped BTC": "wrapped-bitcoin",
"Dai Stablecoin": "dai",
"Ether": "ethereum",
"USD Coin": "usd-coin",
"tBTC v2": "tbtc"
}

// Coingecko to Dia asset ids
const dia_assets = {
"bitcoin": "/Bitcoin/0x0000000000000000000000000000000000000000",
"ethereum": "/Ethereum/0x0000000000000000000000000000000000000000",
"interlay": "/Interlay/0x0000000000000000000000000000000000000000",
"polkadot": "/Polkadot/0x0000000000000000000000000000000000000000",
"kusama": "/Kusama/0x0000000000000000000000000000000000000000",
"kintsugi": "/Kintsugi/Token:KINT",
"acala-dollar": "/Acala/Token:AUSD",
"karura": "/Bifrost/518",
"tether": "/Ethereum/0xdAC17F958D2ee523a2206206994597C13D831ec7",
"voucher-dot": "/Bifrost-polkadot/2304",
"binancecoin": "/Ethereum/0xB8c77482e45F1F44dE1745F52C74426C631bDD52",
"bnb": "/Ethereum/0xB8c77482e45F1F44dE1745F52C74426C631bDD52",
"tbtc": "/Ethereum/0x18084fbA666a33d37592fA2633fD49a74DD93a88",
"dai": "/Ethereum/0x6B175474E89094C44Da98b954EedeAC495271d0F",
"moonbeam": "/Moonbeam/0x0000000000000000000000000000000000000000",
"usd-coin": "/Ethereum/0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
"wrapped-bitcoin": "/Ethereum/0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599"
}

const fetchDiaAsset = async (asset) => {
try {
if (!dia_assets[asset]) {
console.log('Missing DIA asset: ', asset)
return coingecko({ ids: [asset], vs_currencies: ["usd"] })
}
const url = 'https://api.diadata.org/v1/assetQuotation' + dia_assets[asset]
const response = await fetch(url, { headers: { "accept": "application/json" } })
if (!response.ok) {
throw new Error(response.status)
}
const json = await response.json()

// optionally rename the ticker
const name = (tickers[json.Name] ?? json.Name).toLowerCase()
return {
[name]: {
'usd': json.Price
}
}
} catch (error) {
console.log(error)
}
}

const dia = async (args) => {
const assets = args.ids.split(',')

return Promise
.all(assets.map(x => fetchDiaAsset(x)))
.then(x => x.reduce((map, obj) => {
// we need to convert the list to an object
const k = Object.keys(obj)[0]
map[k] = obj[k]
return map
}, {}))
}

const coingecko = async (args) => {
const url = 'https://api.coingecko.com/api/v3/simple/price?' + new URLSearchParams(args)
const response = await fetch(url, { headers: { "accept": "application/json" } })
return await response.json()
}

const fetchPrices = (priceSource, args) => {
if (priceSource === 'coingecko') {
return coingecko(args)
} else if (priceSource === 'dia') {
return dia(args)
} else {
try {
return dia(args)
} catch (error) {
console.log(error)
return coingecko(args)
}
}
}

export default async function (request, response) {
const args = request.query
const priceSource = args['price-source']

const resp = await fetchPrices(priceSource, args)
return response
.status(200)
.setHeader("content-type", "application/json")
.setHeader("cache-control", "public, maxage=0, s-maxage=300")
.json(resp)
}
106 changes: 0 additions & 106 deletions api/market_data.py

This file was deleted.

6 changes: 5 additions & 1 deletion vercel.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
"api/terms.js": {
"memory": 256,
"maxDuration": 10
},
"api/market_data.js": {
"memory": 128,
"maxDuration": 5
}
},
"rewrites": [
Expand All @@ -20,7 +24,7 @@
},
{
"source": "/marketdata/(.*)",
"destination": "/api/market_data.py"
"destination": "/api/market_data.js"
},
{
"source": "/terms/(.*)",
Expand Down

2 comments on commit 711bc4e

@vercel
Copy link

@vercel vercel bot commented on 711bc4e Sep 27, 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 711bc4e Sep 27, 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.