add prices utils functions

This commit is contained in:
Barry Gitarts 2019-07-30 13:50:51 -04:00
parent 396bde9ecf
commit 8305ae857a
1 changed files with 27 additions and 0 deletions

27
src/utils/prices.js Normal file
View File

@ -0,0 +1,27 @@
import cc from 'cryptocompare'
import { getTokenLabel } from './currencies'
export const generatePairKey = (from, to) => `${from}_${to}`
export const getUsdPrice = async ticker => {
const price = await cc.price(ticker, 'USD')
return price
}
export const getPrices = async () => {
const prices = await cc.priceMulti(['ETH', 'SNT'], ['USD'])
return prices
}
const formatter = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
minimumFractionDigits: 2
})
export const convertTokenAmountUsd = (token, amount, prices) => {
const tokenLabel = getTokenLabel(token)
if (!amount || !token || !prices[tokenLabel]) return null
const rate = prices[tokenLabel]['USD']
const formatted = formatter.format(rate * amount)
return formatted
}