2020-09-10 07:00:04 +02:00
|
|
|
import React, { useContext, useMemo } from 'react'
|
2018-07-14 22:47:07 +02:00
|
|
|
import PropTypes from 'prop-types'
|
2020-09-10 07:00:04 +02:00
|
|
|
import BigNumber from 'bignumber.js'
|
|
|
|
import { I18nContext } from '../../contexts/i18n'
|
2018-07-14 22:47:07 +02:00
|
|
|
import ConfirmTransactionBase from '../confirm-transaction-base'
|
2019-03-22 00:03:30 +01:00
|
|
|
import UserPreferencedCurrencyDisplay from '../../components/app/user-preferenced-currency-display'
|
2018-07-14 22:47:07 +02:00
|
|
|
import {
|
|
|
|
formatCurrency,
|
|
|
|
convertTokenToFiat,
|
|
|
|
addFiat,
|
2018-07-26 21:15:34 +02:00
|
|
|
roundExponential,
|
2019-03-22 00:03:30 +01:00
|
|
|
} from '../../helpers/utils/confirm-tx.util'
|
|
|
|
import { getWeiHexFromDecimalValue } from '../../helpers/utils/conversions.util'
|
|
|
|
import { ETH, PRIMARY } from '../../helpers/constants/common'
|
2018-07-14 22:47:07 +02:00
|
|
|
|
2020-09-10 07:00:04 +02:00
|
|
|
export default function ConfirmTokenTransactionBase ({
|
|
|
|
toAddress,
|
|
|
|
tokenAddress,
|
|
|
|
tokenAmount = '0',
|
|
|
|
tokenSymbol,
|
|
|
|
fiatTransactionTotal,
|
|
|
|
ethTransactionTotal,
|
|
|
|
contractExchangeRate,
|
|
|
|
conversionRate,
|
|
|
|
currentCurrency,
|
|
|
|
}) {
|
|
|
|
const t = useContext(I18nContext)
|
2018-07-14 22:47:07 +02:00
|
|
|
|
2020-09-10 07:00:04 +02:00
|
|
|
const hexWeiValue = useMemo(() => {
|
|
|
|
if (tokenAmount === '0' || !contractExchangeRate) {
|
|
|
|
return '0'
|
|
|
|
}
|
2018-07-14 22:47:07 +02:00
|
|
|
|
2020-09-10 07:00:04 +02:00
|
|
|
const decimalEthValue = (
|
|
|
|
(new BigNumber(tokenAmount)).times(new BigNumber(contractExchangeRate))
|
|
|
|
).toFixed()
|
2018-07-14 22:47:07 +02:00
|
|
|
|
2020-09-10 07:00:04 +02:00
|
|
|
return getWeiHexFromDecimalValue({
|
2018-10-17 01:03:29 +02:00
|
|
|
value: decimalEthValue,
|
|
|
|
fromCurrency: ETH,
|
|
|
|
fromDenomination: ETH,
|
|
|
|
})
|
2020-09-10 07:00:04 +02:00
|
|
|
}, [tokenAmount, contractExchangeRate])
|
2018-10-17 01:03:29 +02:00
|
|
|
|
2020-09-10 07:00:04 +02:00
|
|
|
const secondaryTotalTextOverride = useMemo(() => {
|
2018-07-14 22:47:07 +02:00
|
|
|
if (typeof contractExchangeRate === 'undefined') {
|
|
|
|
return formatCurrency(fiatTransactionTotal, currentCurrency)
|
|
|
|
}
|
2020-09-10 07:00:04 +02:00
|
|
|
|
|
|
|
const fiatTransactionAmount = convertTokenToFiat({
|
|
|
|
value: tokenAmount,
|
|
|
|
toCurrency: currentCurrency,
|
|
|
|
conversionRate,
|
|
|
|
contractExchangeRate,
|
|
|
|
})
|
2020-08-19 18:27:05 +02:00
|
|
|
const fiatTotal = addFiat(fiatTransactionAmount, fiatTransactionTotal)
|
|
|
|
const roundedFiatTotal = roundExponential(fiatTotal)
|
|
|
|
return formatCurrency(roundedFiatTotal, currentCurrency)
|
2020-09-10 07:00:04 +02:00
|
|
|
},
|
|
|
|
[
|
|
|
|
currentCurrency,
|
|
|
|
conversionRate,
|
|
|
|
contractExchangeRate,
|
|
|
|
fiatTransactionTotal,
|
|
|
|
tokenAmount,
|
|
|
|
])
|
2018-07-14 22:47:07 +02:00
|
|
|
|
2020-09-10 07:00:04 +02:00
|
|
|
const tokensText = `${tokenAmount} ${tokenSymbol}`
|
2018-07-14 22:47:07 +02:00
|
|
|
|
2020-09-10 07:00:04 +02:00
|
|
|
return (
|
|
|
|
<ConfirmTransactionBase
|
|
|
|
toAddress={toAddress}
|
|
|
|
identiconAddress={tokenAddress}
|
|
|
|
title={tokensText}
|
|
|
|
subtitleComponent={
|
|
|
|
contractExchangeRate === undefined
|
|
|
|
? (
|
|
|
|
<span>
|
|
|
|
{ t('noConversionRateAvailable') }
|
|
|
|
</span>
|
|
|
|
) : (
|
|
|
|
<UserPreferencedCurrencyDisplay
|
|
|
|
value={hexWeiValue}
|
|
|
|
type={PRIMARY}
|
|
|
|
showEthLogo
|
|
|
|
hideLabel
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
primaryTotalTextOverride={(
|
|
|
|
<div>
|
|
|
|
<span>{ `${tokensText} + ` }</span>
|
|
|
|
<img
|
|
|
|
src="/images/eth.svg"
|
|
|
|
height="18"
|
|
|
|
/>
|
|
|
|
<span>{ ethTransactionTotal }</span>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
secondaryTotalTextOverride={secondaryTotalTextOverride}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|
2018-07-14 22:47:07 +02:00
|
|
|
|
2020-09-10 07:00:04 +02:00
|
|
|
ConfirmTokenTransactionBase.propTypes = {
|
|
|
|
tokenAddress: PropTypes.string,
|
|
|
|
toAddress: PropTypes.string,
|
|
|
|
tokenAmount: PropTypes.string,
|
|
|
|
tokenSymbol: PropTypes.string,
|
|
|
|
fiatTransactionTotal: PropTypes.string,
|
|
|
|
ethTransactionTotal: PropTypes.string,
|
|
|
|
contractExchangeRate: PropTypes.number,
|
|
|
|
conversionRate: PropTypes.number,
|
|
|
|
currentCurrency: PropTypes.string,
|
2018-07-14 22:47:07 +02:00
|
|
|
}
|