2020-01-09 04:34:58 +01:00
|
|
|
import log from 'loglevel'
|
|
|
|
import BigNumber from 'bignumber.js'
|
2018-08-23 01:54:18 +02:00
|
|
|
import contractMap from 'eth-contract-metadata'
|
2020-08-18 21:18:25 +02:00
|
|
|
import * as util from './util'
|
2020-06-10 20:04:56 +02:00
|
|
|
import { conversionUtil, multiplyCurrencies } from './conversion-util'
|
|
|
|
import { formatCurrency } from './confirm-tx.util'
|
2017-10-27 19:39:40 +02:00
|
|
|
|
2018-08-23 01:54:18 +02:00
|
|
|
const casedContractMap = Object.keys(contractMap).reduce((acc, base) => {
|
|
|
|
return {
|
|
|
|
...acc,
|
|
|
|
[base.toLowerCase()]: contractMap[base],
|
|
|
|
}
|
|
|
|
}, {})
|
2017-10-27 19:39:40 +02:00
|
|
|
|
2018-08-23 01:54:18 +02:00
|
|
|
const DEFAULT_SYMBOL = ''
|
|
|
|
const DEFAULT_DECIMALS = '0'
|
|
|
|
|
|
|
|
async function getSymbolFromContract (tokenAddress) {
|
|
|
|
const token = util.getContractAtAddress(tokenAddress)
|
|
|
|
|
|
|
|
try {
|
|
|
|
const result = await token.symbol()
|
|
|
|
return result[0]
|
|
|
|
} catch (error) {
|
|
|
|
log.warn(`symbol() call for token at address ${tokenAddress} resulted in error:`, error)
|
2020-08-12 21:06:57 +02:00
|
|
|
return undefined
|
2018-08-23 01:54:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getDecimalsFromContract (tokenAddress) {
|
|
|
|
const token = util.getContractAtAddress(tokenAddress)
|
|
|
|
|
|
|
|
try {
|
|
|
|
const result = await token.decimals()
|
|
|
|
const decimalsBN = result[0]
|
|
|
|
return decimalsBN && decimalsBN.toString()
|
|
|
|
} catch (error) {
|
|
|
|
log.warn(`decimals() call for token at address ${tokenAddress} resulted in error:`, error)
|
2020-08-12 21:06:57 +02:00
|
|
|
return undefined
|
2018-08-23 01:54:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getContractMetadata (tokenAddress) {
|
|
|
|
return tokenAddress && casedContractMap[tokenAddress.toLowerCase()]
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getSymbol (tokenAddress) {
|
|
|
|
let symbol = await getSymbolFromContract(tokenAddress)
|
|
|
|
|
|
|
|
if (!symbol) {
|
|
|
|
const contractMetadataInfo = getContractMetadata(tokenAddress)
|
|
|
|
|
|
|
|
if (contractMetadataInfo) {
|
|
|
|
symbol = contractMetadataInfo.symbol
|
2017-10-27 19:39:40 +02:00
|
|
|
}
|
2018-08-23 01:54:18 +02:00
|
|
|
}
|
2017-10-27 19:39:40 +02:00
|
|
|
|
2018-08-23 01:54:18 +02:00
|
|
|
return symbol
|
|
|
|
}
|
2017-10-27 19:39:40 +02:00
|
|
|
|
2018-08-23 01:54:18 +02:00
|
|
|
async function getDecimals (tokenAddress) {
|
|
|
|
let decimals = await getDecimalsFromContract(tokenAddress)
|
|
|
|
|
|
|
|
if (!decimals || decimals === '0') {
|
|
|
|
const contractMetadataInfo = getContractMetadata(tokenAddress)
|
|
|
|
|
|
|
|
if (contractMetadataInfo) {
|
|
|
|
decimals = contractMetadataInfo.decimals
|
|
|
|
}
|
2018-04-28 01:15:36 +02:00
|
|
|
}
|
2018-08-23 01:54:18 +02:00
|
|
|
|
|
|
|
return decimals
|
2018-04-28 01:15:36 +02:00
|
|
|
}
|
2017-10-27 19:39:40 +02:00
|
|
|
|
2019-06-28 05:53:12 +02:00
|
|
|
export async function fetchSymbolAndDecimals (tokenAddress) {
|
|
|
|
let symbol, decimals
|
|
|
|
|
|
|
|
try {
|
|
|
|
symbol = await getSymbol(tokenAddress)
|
|
|
|
decimals = await getDecimals(tokenAddress)
|
|
|
|
} catch (error) {
|
|
|
|
log.warn(`symbol() and decimal() calls for token at address ${tokenAddress} resulted in error:`, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
symbol: symbol || DEFAULT_SYMBOL,
|
|
|
|
decimals: decimals || DEFAULT_DECIMALS,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-23 01:54:18 +02:00
|
|
|
export async function getSymbolAndDecimals (tokenAddress, existingTokens = []) {
|
2018-04-28 01:15:36 +02:00
|
|
|
const existingToken = existingTokens.find(({ address }) => tokenAddress === address)
|
2018-08-23 01:54:18 +02:00
|
|
|
|
2018-04-28 01:15:36 +02:00
|
|
|
if (existingToken) {
|
2018-08-23 01:54:18 +02:00
|
|
|
return {
|
|
|
|
symbol: existingToken.symbol,
|
|
|
|
decimals: existingToken.decimals,
|
|
|
|
}
|
2018-04-28 01:15:36 +02:00
|
|
|
}
|
2018-07-03 00:49:33 +02:00
|
|
|
|
2018-08-23 01:54:18 +02:00
|
|
|
let symbol, decimals
|
|
|
|
|
2018-04-28 01:15:36 +02:00
|
|
|
try {
|
2018-08-23 01:54:18 +02:00
|
|
|
symbol = await getSymbol(tokenAddress)
|
|
|
|
decimals = await getDecimals(tokenAddress)
|
|
|
|
} catch (error) {
|
|
|
|
log.warn(`symbol() and decimal() calls for token at address ${tokenAddress} resulted in error:`, error)
|
|
|
|
}
|
2018-04-28 01:15:36 +02:00
|
|
|
|
2018-08-23 01:54:18 +02:00
|
|
|
return {
|
|
|
|
symbol: symbol || DEFAULT_SYMBOL,
|
|
|
|
decimals: decimals || DEFAULT_DECIMALS,
|
2018-04-28 01:15:36 +02:00
|
|
|
}
|
2018-08-23 01:54:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function tokenInfoGetter () {
|
|
|
|
const tokens = {}
|
2017-10-27 19:39:40 +02:00
|
|
|
|
2018-08-23 01:54:18 +02:00
|
|
|
return async (address) => {
|
|
|
|
if (tokens[address]) {
|
|
|
|
return tokens[address]
|
|
|
|
}
|
2017-10-27 19:39:40 +02:00
|
|
|
|
2018-08-23 01:54:18 +02:00
|
|
|
tokens[address] = await getSymbolAndDecimals(address)
|
|
|
|
|
|
|
|
return tokens[address]
|
2017-10-27 19:39:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-23 01:54:18 +02:00
|
|
|
export function calcTokenAmount (value, decimals) {
|
2017-10-30 18:48:50 +01:00
|
|
|
const multiplier = Math.pow(10, Number(decimals || 0))
|
2018-11-20 01:06:34 +01:00
|
|
|
return new BigNumber(String(value)).div(multiplier)
|
2017-10-30 18:48:50 +01:00
|
|
|
}
|
2018-10-09 02:05:57 +02:00
|
|
|
|
2019-11-05 16:13:48 +01:00
|
|
|
export function calcTokenValue (value, decimals) {
|
|
|
|
const multiplier = Math.pow(10, Number(decimals || 0))
|
|
|
|
return new BigNumber(String(value)).times(multiplier)
|
|
|
|
}
|
|
|
|
|
2020-08-22 04:29:19 +02:00
|
|
|
/**
|
|
|
|
* Attempts to get the address parameter of the given token transaction data
|
|
|
|
* (i.e. function call) per the Human Standard Token ABI, in the following
|
|
|
|
* order:
|
|
|
|
* - The '_to' parameter, if present
|
|
|
|
* - The first parameter, if present
|
|
|
|
*
|
|
|
|
* @param {Object} tokenData - ethers Interface token data.
|
|
|
|
* @returns {string | undefined} A lowercase address string.
|
|
|
|
*/
|
|
|
|
export function getTokenAddressParam (tokenData = {}) {
|
2020-10-22 19:07:02 +02:00
|
|
|
const value = tokenData?.args?._to || tokenData?.args?.[0]
|
2020-08-22 04:29:19 +02:00
|
|
|
return value?.toString().toLowerCase()
|
2018-10-09 02:05:57 +02:00
|
|
|
}
|
2019-06-28 05:53:12 +02:00
|
|
|
|
2020-08-22 04:29:19 +02:00
|
|
|
/**
|
|
|
|
* Gets the '_value' parameter of the given token transaction data
|
|
|
|
* (i.e function call) per the Human Standard Token ABI, if present.
|
|
|
|
*
|
|
|
|
* @param {Object} tokenData - ethers Interface token data.
|
|
|
|
* @returns {string | undefined} A decimal string value.
|
|
|
|
*/
|
|
|
|
export function getTokenValueParam (tokenData = {}) {
|
2020-10-22 19:07:02 +02:00
|
|
|
return tokenData?.args?._value?.toString()
|
2019-06-28 05:53:12 +02:00
|
|
|
}
|
2020-06-10 20:04:56 +02:00
|
|
|
|
2020-10-06 20:28:38 +02:00
|
|
|
export function getTokenValue (tokenParams = []) {
|
|
|
|
const valueData = tokenParams.find((param) => param.name === '_value')
|
|
|
|
return valueData && valueData.value
|
|
|
|
}
|
|
|
|
|
2020-06-10 20:04:56 +02:00
|
|
|
/**
|
2020-10-06 20:28:38 +02:00
|
|
|
* Get the token balance converted to fiat and optionally formatted for display
|
2020-06-10 20:04:56 +02:00
|
|
|
*
|
|
|
|
* @param {number} [contractExchangeRate] - The exchange rate between the current token and the native currency
|
|
|
|
* @param {number} conversionRate - The exchange rate between the current fiat currency and the native currency
|
|
|
|
* @param {string} currentCurrency - The currency code for the user's chosen fiat currency
|
|
|
|
* @param {string} [tokenAmount] - The current token balance
|
|
|
|
* @param {string} [tokenSymbol] - The token symbol
|
2020-10-06 20:28:38 +02:00
|
|
|
* @param {boolean} [formatted] - Whether the return value should be formatted or not
|
|
|
|
* @param {boolean} [hideCurrencySymbol] - excludes the currency symbol in the result if true
|
|
|
|
* @returns {string|undefined} The token amount in the user's chosen fiat currency, optionally formatted and localize
|
2020-06-10 20:04:56 +02:00
|
|
|
*/
|
2020-10-06 20:28:38 +02:00
|
|
|
export function getTokenFiatAmount (
|
2020-06-10 20:04:56 +02:00
|
|
|
contractExchangeRate,
|
|
|
|
conversionRate,
|
|
|
|
currentCurrency,
|
|
|
|
tokenAmount,
|
2020-07-14 17:20:41 +02:00
|
|
|
tokenSymbol,
|
2020-10-06 20:28:38 +02:00
|
|
|
formatted = true,
|
|
|
|
hideCurrencySymbol = false,
|
2020-06-10 20:04:56 +02:00
|
|
|
) {
|
|
|
|
// If the conversionRate is 0 (i.e. unknown) or the contract exchange rate
|
|
|
|
// is currently unknown, the fiat amount cannot be calculated so it is not
|
|
|
|
// shown to the user
|
|
|
|
if (conversionRate <= 0 || !contractExchangeRate || tokenAmount === undefined) {
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
|
|
|
|
const currentTokenToFiatRate = multiplyCurrencies(
|
|
|
|
contractExchangeRate,
|
2020-07-14 17:20:41 +02:00
|
|
|
conversionRate,
|
2020-06-10 20:04:56 +02:00
|
|
|
)
|
|
|
|
const currentTokenInFiat = conversionUtil(tokenAmount, {
|
|
|
|
fromNumericBase: 'dec',
|
|
|
|
fromCurrency: tokenSymbol,
|
|
|
|
toCurrency: currentCurrency.toUpperCase(),
|
|
|
|
numberOfDecimals: 2,
|
|
|
|
conversionRate: currentTokenToFiatRate,
|
|
|
|
})
|
2020-10-06 20:28:38 +02:00
|
|
|
let result
|
|
|
|
if (hideCurrencySymbol) {
|
|
|
|
result = formatCurrency(currentTokenInFiat, currentCurrency)
|
|
|
|
} else if (formatted) {
|
|
|
|
result = `${formatCurrency(currentTokenInFiat, currentCurrency)} ${currentCurrency.toUpperCase()}`
|
|
|
|
} else {
|
|
|
|
result = currentTokenInFiat
|
|
|
|
}
|
|
|
|
return result
|
2020-06-10 20:04:56 +02:00
|
|
|
}
|