1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00

Merge branch 'NewUI-flat' into MM-128-get-currentCurrency-from-state

This commit is contained in:
Chi Kei Chan 2017-10-19 12:35:39 -07:00 committed by GitHub
commit f1c097ab5c
3 changed files with 48 additions and 22 deletions

View File

@ -11,12 +11,22 @@ const Identicon = require('../identicon')
const ethUtil = require('ethereumjs-util') const ethUtil = require('ethereumjs-util')
const BN = ethUtil.BN const BN = ethUtil.BN
const hexToBn = require('../../../../app/scripts/lib/hex-to-bn') const hexToBn = require('../../../../app/scripts/lib/hex-to-bn')
const { conversionUtil } = require('../../conversion-util') const {
conversionUtil,
multiplyCurrencies,
addCurrencies,
} = require('../../conversion-util')
const MIN_GAS_PRICE_GWEI_BN = new BN(1) const MIN_GAS_PRICE_GWEI_BN = new BN(1)
const GWEI_FACTOR = new BN(1e9) const GWEI_FACTOR = new BN(1e9)
const MIN_GAS_PRICE_BN = MIN_GAS_PRICE_GWEI_BN.mul(GWEI_FACTOR) const MIN_GAS_PRICE_BN = MIN_GAS_PRICE_GWEI_BN.mul(GWEI_FACTOR)
const {
getSelectedTokenExchangeRate,
getTokenExchangeRate,
getSelectedAddress,
} = require('../../selectors')
module.exports = connect(mapStateToProps, mapDispatchToProps)(ConfirmSendToken) module.exports = connect(mapStateToProps, mapDispatchToProps)(ConfirmSendToken)
function mapStateToProps (state, ownProps) { function mapStateToProps (state, ownProps) {
@ -28,10 +38,8 @@ function mapStateToProps (state, ownProps) {
identities, identities,
} = state.metamask } = state.metamask
const accounts = state.metamask.accounts const accounts = state.metamask.accounts
const selectedAddress = state.metamask.selectedAddress || Object.keys(accounts)[0] const selectedAddress = getSelectedAddress(state)
const tokenExchangeRates = state.metamask.tokenExchangeRates const tokenExchangeRate = getTokenExchangeRate(state, symbol)
const pair = `${symbol.toLowerCase()}_eth`
const { rate: tokenExchangeRate = 0 } = tokenExchangeRates[pair] || {}
return { return {
conversionRate, conversionRate,
@ -86,15 +94,12 @@ ConfirmSendToken.prototype.getGasFee = function () {
const txParams = txMeta.txParams || {} const txParams = txMeta.txParams || {}
const { decimals } = token const { decimals } = token
// Gas
const gas = txParams.gas const gas = txParams.gas
const gasBn = hexToBn(gas)
// Gas Price
const gasPrice = txParams.gasPrice || MIN_GAS_PRICE_BN.toString(16) const gasPrice = txParams.gasPrice || MIN_GAS_PRICE_BN.toString(16)
const gasPriceBn = hexToBn(gasPrice) const gasTotal = multiplyCurrencies(gas, gasPrice, {
const txFeeBn = gasBn.mul(gasPriceBn) multiplicandBase: 16,
multiplierBase: 16,
})
const FIAT = conversionUtil(txFeeBn, { const FIAT = conversionUtil(txFeeBn, {
fromNumericBase: 'BN', fromNumericBase: 'BN',
@ -105,7 +110,7 @@ ConfirmSendToken.prototype.getGasFee = function () {
numberOfDecimals: 2, numberOfDecimals: 2,
conversionRate, conversionRate,
}) })
const ETH = conversionUtil(txFeeBn, { const ETH = conversionUtil(gasTotal, {
fromNumericBase: 'BN', fromNumericBase: 'BN',
toNumericBase: 'dec', toNumericBase: 'dec',
fromDenomination: 'WEI', fromDenomination: 'WEI',
@ -114,12 +119,22 @@ ConfirmSendToken.prototype.getGasFee = function () {
numberOfDecimals: 6, numberOfDecimals: 6,
conversionRate, conversionRate,
}) })
const tokenGas = multiplyCurrencies(gas, gasPrice, {
toNumericBase: 'dec',
multiplicandBase: 16,
multiplierBase: 16,
toCurrency: 'BAT',
conversionRate: tokenExchangeRate,
invertConversionRate: true,
fromDenomination: 'WEI',
numberOfDecimals: decimals || 4,
})
return { return {
fiat: +Number(FIAT).toFixed(2), fiat: +Number(FIAT).toFixed(2),
eth: ETH, eth: ETH,
token: tokenExchangeRate token: tokenExchangeRate
? +(ETH * tokenExchangeRate).toFixed(decimals) ? tokenGas
: null, : null,
} }
} }
@ -137,7 +152,7 @@ ConfirmSendToken.prototype.getData = function () {
name: identities[txParams.from].name, name: identities[txParams.from].name,
}, },
to: { to: {
address: txParams.to, address: value,
name: identities[value] ? identities[value].name : 'New Recipient', name: identities[value] ? identities[value].name : 'New Recipient',
}, },
memo: txParams.memo || '', memo: txParams.memo || '',
@ -196,6 +211,8 @@ ConfirmSendToken.prototype.renderTotalPlusGas = function () {
const { fiat: fiatAmount, token: tokenAmount } = this.getAmount() const { fiat: fiatAmount, token: tokenAmount } = this.getAmount()
const { fiat: fiatGas, token: tokenGas } = this.getGasFee() const { fiat: fiatGas, token: tokenGas } = this.getGasFee()
const tokenTotal = addCurrencies(tokenAmount, tokenGas || '0')
return fiatAmount && fiatGas return fiatAmount && fiatGas
? ( ? (
h('section.flex-row.flex-center.confirm-screen-total-box ', [ h('section.flex-row.flex-center.confirm-screen-total-box ', [
@ -273,7 +290,7 @@ ConfirmSendToken.prototype.render = function () {
h( h(
Identicon, Identicon,
{ {
address: txParams.to, address: toAddress,
diameter: 60, diameter: 60,
}, },
), ),

View File

@ -147,16 +147,16 @@ const addCurrencies = (a, b, options = {}) => {
const multiplyCurrencies = (a, b, options = {}) => { const multiplyCurrencies = (a, b, options = {}) => {
const { const {
toNumericBase,
numberOfDecimals,
multiplicandBase, multiplicandBase,
multiplierBase, multiplierBase,
...conversionOptions,
} = options } = options
const value = (new BigNumber(a, multiplicandBase)).times(b, multiplierBase); const value = (new BigNumber(a, multiplicandBase)).times(b, multiplierBase);
return converter({ return converter({
value, value,
toNumericBase, ...conversionOptions,
numberOfDecimals,
}) })
} }

View File

@ -6,6 +6,7 @@ const selectors = {
getSelectedAccount, getSelectedAccount,
getSelectedToken, getSelectedToken,
getSelectedTokenExchangeRate, getSelectedTokenExchangeRate,
getTokenExchangeRate,
conversionRateSelector, conversionRateSelector,
transactionsSelector, transactionsSelector,
accountsWithSendEtherInfoSelector, accountsWithSendEtherInfoSelector,
@ -58,6 +59,14 @@ function getSelectedTokenExchangeRate (state) {
return tokenExchangeRate return tokenExchangeRate
} }
function getTokenExchangeRate (state, tokenSymbol) {
const pair = `${tokenSymbol.toLowerCase()}_eth`
const tokenExchangeRates = state.metamask.tokenExchangeRates
const { rate: tokenExchangeRate = 0 } = tokenExchangeRates[pair] || {}
return tokenExchangeRate
}
function conversionRateSelector (state) { function conversionRateSelector (state) {
return state.metamask.conversionRate return state.metamask.conversionRate
} }