2017-10-09 18:25:23 +02:00
|
|
|
const Component = require('react').Component
|
|
|
|
const h = require('react-hyperscript')
|
|
|
|
const inherits = require('util').inherits
|
2017-11-08 00:36:26 +01:00
|
|
|
const CurrencyInput = require('../currency-input')
|
2017-10-21 03:57:59 +02:00
|
|
|
const { conversionUtil, multiplyCurrencies } = require('../../conversion-util')
|
2018-04-09 18:44:09 +02:00
|
|
|
const currencyFormatter = require('currency-formatter')
|
2018-04-09 21:24:52 +02:00
|
|
|
const currencies = require('currency-formatter/currencies')
|
2018-05-22 10:10:06 +02:00
|
|
|
const ethUtil = require('ethereumjs-util')
|
2017-10-09 18:25:23 +02:00
|
|
|
|
|
|
|
module.exports = CurrencyDisplay
|
|
|
|
|
|
|
|
inherits(CurrencyDisplay, Component)
|
|
|
|
function CurrencyDisplay () {
|
|
|
|
Component.call(this)
|
|
|
|
}
|
|
|
|
|
2017-10-17 22:13:20 +02:00
|
|
|
function toHexWei (value) {
|
|
|
|
return conversionUtil(value, {
|
2017-10-13 22:19:22 +02:00
|
|
|
fromNumericBase: 'dec',
|
|
|
|
toNumericBase: 'hex',
|
|
|
|
toDenomination: 'WEI',
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-10-21 03:26:18 +02:00
|
|
|
CurrencyDisplay.prototype.getAmount = function (value) {
|
|
|
|
const { selectedToken } = this.props
|
|
|
|
const { decimals } = selectedToken || {}
|
|
|
|
const multiplier = Math.pow(10, Number(decimals || 0))
|
2017-10-21 03:57:59 +02:00
|
|
|
|
|
|
|
const sendAmount = multiplyCurrencies(value, multiplier, {toNumericBase: 'hex'})
|
|
|
|
|
2017-10-21 03:26:18 +02:00
|
|
|
return selectedToken
|
|
|
|
? sendAmount
|
|
|
|
: toHexWei(value)
|
|
|
|
}
|
|
|
|
|
2017-11-08 02:17:14 +01:00
|
|
|
CurrencyDisplay.prototype.getValueToRender = function () {
|
|
|
|
const { selectedToken, conversionRate, value } = this.props
|
|
|
|
const { decimals, symbol } = selectedToken || {}
|
|
|
|
const multiplier = Math.pow(10, Number(decimals || 0))
|
|
|
|
|
|
|
|
return selectedToken
|
2018-05-22 10:10:06 +02:00
|
|
|
? conversionUtil(ethUtil.addHexPrefix(value), {
|
2017-11-08 02:17:14 +01:00
|
|
|
fromNumericBase: 'hex',
|
|
|
|
toCurrency: symbol,
|
|
|
|
conversionRate: multiplier,
|
|
|
|
invertConversionRate: true,
|
|
|
|
})
|
2018-05-22 10:10:06 +02:00
|
|
|
: conversionUtil(ethUtil.addHexPrefix(value), {
|
2017-11-08 02:17:14 +01:00
|
|
|
fromNumericBase: 'hex',
|
|
|
|
toNumericBase: 'dec',
|
|
|
|
fromDenomination: 'WEI',
|
|
|
|
numberOfDecimals: 6,
|
|
|
|
conversionRate,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-04-09 18:44:09 +02:00
|
|
|
CurrencyDisplay.prototype.getConvertedValueToRender = function (nonFormattedValue) {
|
|
|
|
const { primaryCurrency, convertedCurrency, conversionRate } = this.props
|
|
|
|
|
|
|
|
let convertedValue = conversionUtil(nonFormattedValue, {
|
|
|
|
fromNumericBase: 'dec',
|
|
|
|
fromCurrency: primaryCurrency,
|
|
|
|
toCurrency: convertedCurrency,
|
|
|
|
numberOfDecimals: 2,
|
|
|
|
conversionRate,
|
|
|
|
})
|
|
|
|
convertedValue = Number(convertedValue).toFixed(2)
|
|
|
|
|
|
|
|
const upperCaseCurrencyCode = convertedCurrency.toUpperCase()
|
|
|
|
|
|
|
|
return currencies.find(currency => currency.code === upperCaseCurrencyCode)
|
|
|
|
? currencyFormatter.format(Number(convertedValue), {
|
|
|
|
code: upperCaseCurrencyCode,
|
|
|
|
})
|
|
|
|
: convertedValue
|
|
|
|
}
|
|
|
|
|
2017-10-09 18:25:23 +02:00
|
|
|
CurrencyDisplay.prototype.render = function () {
|
|
|
|
const {
|
2017-10-13 22:19:22 +02:00
|
|
|
className = 'currency-display',
|
|
|
|
primaryBalanceClassName = 'currency-display__input',
|
|
|
|
convertedBalanceClassName = 'currency-display__converted-value',
|
2017-10-09 18:25:23 +02:00
|
|
|
primaryCurrency,
|
|
|
|
convertedCurrency,
|
|
|
|
readOnly = false,
|
2017-10-17 22:13:20 +02:00
|
|
|
inError = false,
|
|
|
|
handleChange,
|
2017-10-09 18:25:23 +02:00
|
|
|
} = this.props
|
2017-10-13 22:19:22 +02:00
|
|
|
|
2017-11-08 02:17:14 +01:00
|
|
|
const valueToRender = this.getValueToRender()
|
2018-04-09 18:44:09 +02:00
|
|
|
const convertedValueToRender = this.getConvertedValueToRender(valueToRender)
|
2017-10-09 18:25:23 +02:00
|
|
|
|
2017-10-13 22:19:22 +02:00
|
|
|
return h('div', {
|
2017-10-09 18:25:23 +02:00
|
|
|
className,
|
2017-10-17 22:13:20 +02:00
|
|
|
style: {
|
|
|
|
borderColor: inError ? 'red' : null,
|
|
|
|
},
|
2018-05-14 15:30:50 +02:00
|
|
|
onClick: () => this.currencyInput && this.currencyInput.focus(),
|
2017-10-09 18:25:23 +02:00
|
|
|
}, [
|
|
|
|
|
|
|
|
h('div.currency-display__primary-row', [
|
|
|
|
|
2017-10-11 15:18:27 +02:00
|
|
|
h('div.currency-display__input-wrapper', [
|
|
|
|
|
2018-05-14 15:30:50 +02:00
|
|
|
h(readOnly ? 'input' : CurrencyInput, {
|
2017-10-13 22:19:22 +02:00
|
|
|
className: primaryBalanceClassName,
|
2017-11-08 00:36:26 +01:00
|
|
|
value: `${valueToRender}`,
|
2017-10-23 14:05:53 +02:00
|
|
|
placeholder: '0',
|
2017-10-11 15:18:27 +02:00
|
|
|
readOnly,
|
2018-05-14 15:30:50 +02:00
|
|
|
...(!readOnly ? {
|
|
|
|
onInputChange: newValue => {
|
|
|
|
handleChange(this.getAmount(newValue))
|
|
|
|
},
|
|
|
|
inputRef: input => { this.currencyInput = input },
|
|
|
|
} : {}),
|
2017-10-11 15:18:27 +02:00
|
|
|
}),
|
|
|
|
|
2017-10-23 14:05:53 +02:00
|
|
|
h('span.currency-display__currency-symbol', primaryCurrency),
|
|
|
|
|
2017-10-11 15:18:27 +02:00
|
|
|
]),
|
2017-10-09 18:25:23 +02:00
|
|
|
|
|
|
|
]),
|
|
|
|
|
2017-10-13 22:19:22 +02:00
|
|
|
h('div', {
|
|
|
|
className: convertedBalanceClassName,
|
2018-04-09 18:44:09 +02:00
|
|
|
}, `${convertedValueToRender} ${convertedCurrency.toUpperCase()}`),
|
2017-10-09 18:25:23 +02:00
|
|
|
|
|
|
|
])
|
2017-11-02 13:15:59 +01:00
|
|
|
|
2017-10-09 18:25:23 +02:00
|
|
|
}
|
|
|
|
|