add price conversion for all currencies

This commit is contained in:
Barry Gitarts 2019-08-22 12:55:11 -04:00 committed by Barry G
parent 772e165f7a
commit 35a39fa10d
2 changed files with 20 additions and 3 deletions

View File

@ -38,7 +38,7 @@ export const currencies = [
},
{
value: '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2',
label: 'Wrapped Ether',
label: 'WETH',
img: `${TOKEN_COIN_API}/60.png`,
width: '5%',
humanReadibleFn: toEther,

View File

@ -1,6 +1,7 @@
import cc from 'cryptocompare'
import { getTokenLabel } from './currencies'
const COMPOUND_V2_API = 'https://api.compound.finance/api/v2/ctoken'
export const generatePairKey = (from, to) => `${from}_${to}`
export const getUsdPrice = async ticker => {
const price = await cc.price(ticker, 'USD')
@ -11,9 +12,25 @@ export const formatPercent = number => Number(number).toLocaleString(undefined,{
export const percentToGoal = (pledged, goal) => formatPercent(Number(pledged) / Number(goal))
const supportedCompoundTokens = ['cETH', 'cDAI']
const getCompoundRates = async prices => {
const values = {}
const compound = await fetch(COMPOUND_V2_API)
const res = await compound.json()
res.cToken
.filter(t => supportedCompoundTokens.includes(t.symbol))
.forEach(t => {
const { symbol, exchange_rate, underlying_price: { value } } = t
const underlyingUsd = Number(value) * Number(prices['ETH']['USD'])
const USD = Number(exchange_rate.value) * Number(underlyingUsd)
values[symbol] = { USD }
})
return values
}
export const getPrices = async () => {
const prices = await cc.priceMulti(['ETH', 'SNT'], ['USD'])
return prices
const prices = await cc.priceMulti(['ETH', 'SNT', 'DAI'], ['USD'])
const compound = await getCompoundRates(prices)
return { ...prices, ...compound, WETH: {...prices['ETH'] } }
}
const formatter = new Intl.NumberFormat('en-US', {