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')
|
2017-10-09 18:25:23 +02:00
|
|
|
|
|
|
|
module.exports = CurrencyDisplay
|
|
|
|
|
|
|
|
inherits(CurrencyDisplay, Component)
|
|
|
|
function CurrencyDisplay () {
|
|
|
|
Component.call(this)
|
|
|
|
}
|
2018-03-22 01:41:47 +01:00
|
|
|
//
|
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
|
|
|
|
? conversionUtil(value, {
|
|
|
|
fromNumericBase: 'hex',
|
|
|
|
toCurrency: symbol,
|
|
|
|
conversionRate: multiplier,
|
|
|
|
invertConversionRate: true,
|
|
|
|
})
|
|
|
|
: conversionUtil(value, {
|
|
|
|
fromNumericBase: 'hex',
|
|
|
|
toNumericBase: 'dec',
|
|
|
|
fromDenomination: 'WEI',
|
|
|
|
numberOfDecimals: 6,
|
|
|
|
conversionRate,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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',
|
|
|
|
conversionRate,
|
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()
|
2017-10-09 18:25:23 +02:00
|
|
|
|
2017-11-10 11:37:53 +01:00
|
|
|
let convertedValue = conversionUtil(valueToRender, {
|
2017-10-09 18:25:23 +02:00
|
|
|
fromNumericBase: 'dec',
|
|
|
|
fromCurrency: primaryCurrency,
|
|
|
|
toCurrency: convertedCurrency,
|
2017-10-13 22:19:22 +02:00
|
|
|
numberOfDecimals: 2,
|
2017-10-09 18:25:23 +02:00
|
|
|
conversionRate,
|
|
|
|
})
|
2017-11-10 11:37:53 +01:00
|
|
|
convertedValue = Number(convertedValue).toFixed(2)
|
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,
|
|
|
|
},
|
2017-11-10 19:47:02 +01:00
|
|
|
onClick: () => 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', [
|
|
|
|
|
2017-11-08 00:36:26 +01:00
|
|
|
h(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,
|
2017-11-08 00:36:26 +01:00
|
|
|
onInputChange: newValue => {
|
|
|
|
handleChange(this.getAmount(newValue))
|
2017-10-11 15:18:27 +02:00
|
|
|
},
|
2017-11-10 04:49:16 +01:00
|
|
|
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,
|
2017-10-19 04:09:26 +02:00
|
|
|
}, `${convertedValue} ${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
|
|
|
}
|
|
|
|
|