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

132 lines
3.5 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')
const currencyFormatter = require('currency-formatter')
2018-04-09 21:24:52 +02:00
const currencies = require('currency-formatter/currencies')
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,
})
}
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 {
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-11-08 02:17:14 +01:00
const valueToRender = this.getValueToRender()
2017-10-09 18:25:23 +02:00
const convertedValueToRender = this.getConvertedValueToRender(valueToRender)
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,
}, `${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
}