-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
api: refactor the market data api (#1569)
* api: refactor the market data api * api: refactor the market data api * api: refactor the market data api
- Loading branch information
Showing
3 changed files
with
108 additions
and
107 deletions.
There are no files selected for viewing
This file contains 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
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) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains 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
711bc4e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
interbtc-ui-interlay-testnet – ./
interbtc-ui-interlay-testnet.vercel.app
testnet.interlay.io
interbtc-ui-interlay-testnet-git-master-interlay.vercel.app
interbtc-ui-interlay-testnet-interlay.vercel.app
711bc4e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
interbtc-ui-kintsugi-testnet – ./
interbtc-ui-kintsugi-testnet-git-master-interlay.vercel.app
interbtc-ui-kintsugi-testnet-interlay.vercel.app
interbtc-ui.vercel.app
kintnet.interlay.io