1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-02 22:24:27 +01:00
metamask-extension/ui/app/components/send/currency-display.js

117 lines
2.9 KiB
JavaScript
Raw Normal View History

2017-10-09 18:25:23 +02:00
const Component = require('react').Component
const h = require('react-hyperscript')
const inherits = require('util').inherits
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)
}
2017-10-17 22:13:20 +02:00
function toHexWei (value) {
return conversionUtil(value, {
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 {
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-11-08 02:17:14 +01:00
const valueToRender = this.getValueToRender()
2017-10-09 18:25:23 +02:00
let convertedValue = conversionUtil(valueToRender, {
2017-10-09 18:25:23 +02:00
fromNumericBase: 'dec',
fromCurrency: primaryCurrency,
toCurrency: convertedCurrency,
numberOfDecimals: 2,
2017-10-09 18:25:23 +02:00
conversionRate,
})
convertedValue = Number(convertedValue).toFixed(2)
2017-10-09 18:25:23 +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,
},
onClick: () => this.currencyInput.focus(),
2017-10-09 18:25:23 +02:00
}, [
h('div.currency-display__primary-row', [
h('div.currency-display__input-wrapper', [
h(CurrencyInput, {
className: primaryBalanceClassName,
value: `${valueToRender}`,
placeholder: '0',
readOnly,
onInputChange: newValue => {
handleChange(this.getAmount(newValue))
},
inputRef: input => { this.currencyInput = input },
}),
h('span.currency-display__currency-symbol', primaryCurrency),
]),
2017-10-09 18:25:23 +02:00
]),
h('div', {
className: convertedBalanceClassName,
}, `${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
}