1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-10-23 11:46:13 +02:00
metamask-extension/ui/app/components/pending-tx/confirm-send-token.js

418 lines
12 KiB
JavaScript
Raw Normal View History

2017-09-22 23:34:56 +02:00
const Component = require('react').Component
const { connect } = require('react-redux')
const h = require('react-hyperscript')
const inherits = require('util').inherits
const abi = require('human-standard-token-abi')
const abiDecoder = require('abi-decoder')
abiDecoder.addABI(abi)
const actions = require('../../actions')
const clone = require('clone')
const Identicon = require('../identicon')
const ethUtil = require('ethereumjs-util')
const BN = ethUtil.BN
const hexToBn = require('../../../../app/scripts/lib/hex-to-bn')
const {
conversionUtil,
multiplyCurrencies,
addCurrencies,
} = require('../../conversion-util')
2017-09-22 23:34:56 +02:00
const MIN_GAS_PRICE_GWEI_BN = new BN(1)
const GWEI_FACTOR = new BN(1e9)
const MIN_GAS_PRICE_BN = MIN_GAS_PRICE_GWEI_BN.mul(GWEI_FACTOR)
const {
getSelectedTokenExchangeRate,
getTokenExchangeRate,
getSelectedAddress,
} = require('../../selectors')
2017-09-22 23:34:56 +02:00
module.exports = connect(mapStateToProps, mapDispatchToProps)(ConfirmSendToken)
function mapStateToProps (state, ownProps) {
const { token: { symbol }, txData } = ownProps
const { txParams } = txData || {}
const tokenData = txParams.data && abiDecoder.decodeMethod(txParams.data)
const {
conversionRate,
identities,
2017-10-20 22:43:55 +02:00
currentCurrency,
2017-09-22 23:34:56 +02:00
} = state.metamask
const accounts = state.metamask.accounts
const selectedAddress = getSelectedAddress(state)
const tokenExchangeRate = getTokenExchangeRate(state, symbol)
2017-09-22 23:34:56 +02:00
return {
conversionRate,
identities,
selectedAddress,
tokenExchangeRate,
tokenData: tokenData || {},
2017-10-20 22:43:55 +02:00
currentCurrency: currentCurrency.toUpperCase(),
2017-09-22 23:34:56 +02:00
}
}
function mapDispatchToProps (dispatch, ownProps) {
const { token: { symbol } } = ownProps
return {
backToAccountDetail: address => dispatch(actions.backToAccountDetail(address)),
cancelTransaction: ({ id }) => dispatch(actions.cancelTx({ id })),
updateTokenExchangeRate: () => dispatch(actions.updateTokenExchangeRate(symbol)),
}
}
inherits(ConfirmSendToken, Component)
function ConfirmSendToken () {
Component.call(this)
this.state = {}
this.onSubmit = this.onSubmit.bind(this)
}
ConfirmSendToken.prototype.componentWillMount = function () {
this.props.updateTokenExchangeRate()
}
ConfirmSendToken.prototype.getAmount = function () {
const { conversionRate, tokenExchangeRate, token, tokenData } = this.props
const { params = [] } = tokenData
const { value } = params[1] || {}
const { decimals } = token
const multiplier = Math.pow(10, Number(decimals || 0))
const sendTokenAmount = Number(value / multiplier)
return {
fiat: tokenExchangeRate
? +(sendTokenAmount * tokenExchangeRate * conversionRate).toFixed(2)
: null,
2017-10-21 01:18:25 +02:00
token: typeof value === 'undefined'
? 'Unknown'
: +sendTokenAmount.toFixed(decimals),
2017-09-22 23:34:56 +02:00
}
}
ConfirmSendToken.prototype.getGasFee = function () {
const { conversionRate, tokenExchangeRate, token, currentCurrency } = this.props
2017-09-22 23:34:56 +02:00
const txMeta = this.gatherTxMeta()
const txParams = txMeta.txParams || {}
const { decimals } = token
const gas = txParams.gas
const gasPrice = txParams.gasPrice || MIN_GAS_PRICE_BN.toString(16)
const gasTotal = multiplyCurrencies(gas, gasPrice, {
multiplicandBase: 16,
multiplierBase: 16,
})
2017-09-22 23:34:56 +02:00
2017-10-20 22:43:55 +02:00
const FIAT = conversionUtil(gasTotal, {
2017-09-22 23:34:56 +02:00
fromNumericBase: 'BN',
toNumericBase: 'dec',
fromDenomination: 'WEI',
fromCurrency: 'ETH',
toCurrency: currentCurrency,
2017-09-22 23:34:56 +02:00
numberOfDecimals: 2,
conversionRate,
})
const ETH = conversionUtil(gasTotal, {
2017-09-22 23:34:56 +02:00
fromNumericBase: 'BN',
toNumericBase: 'dec',
fromDenomination: 'WEI',
fromCurrency: 'ETH',
toCurrency: 'ETH',
numberOfDecimals: 6,
conversionRate,
})
const tokenGas = multiplyCurrencies(gas, gasPrice, {
toNumericBase: 'dec',
multiplicandBase: 16,
multiplierBase: 16,
toCurrency: 'BAT',
conversionRate: tokenExchangeRate,
invertConversionRate: true,
fromDenomination: 'WEI',
numberOfDecimals: decimals || 4,
})
2017-09-22 23:34:56 +02:00
return {
fiat: +Number(FIAT).toFixed(2),
2017-09-22 23:34:56 +02:00
eth: ETH,
token: tokenExchangeRate
? tokenGas
2017-09-22 23:34:56 +02:00
: null,
}
}
ConfirmSendToken.prototype.getData = function () {
2017-10-10 23:36:13 +02:00
const { identities, tokenData } = this.props
const { params = [] } = tokenData
const { value } = params[0] || {}
2017-09-22 23:34:56 +02:00
const txMeta = this.gatherTxMeta()
const txParams = txMeta.txParams || {}
2017-09-22 23:34:56 +02:00
return {
from: {
address: txParams.from,
name: identities[txParams.from].name,
},
to: {
address: value,
2017-10-10 23:36:13 +02:00
name: identities[value] ? identities[value].name : 'New Recipient',
2017-09-22 23:34:56 +02:00
},
memo: txParams.memo || '',
}
}
ConfirmSendToken.prototype.renderHeroAmount = function () {
const { token: { symbol }, currentCurrency } = this.props
2017-09-22 23:34:56 +02:00
const { fiat: fiatAmount, token: tokenAmount } = this.getAmount()
const txMeta = this.gatherTxMeta()
const txParams = txMeta.txParams || {}
const { memo = '' } = txParams
return fiatAmount
? (
h('div.confirm-send-token__hero-amount-wrapper', [
h('h3.flex-center.confirm-screen-send-amount', `${fiatAmount}`),
h('h3.flex-center.confirm-screen-send-amount-currency', currentCurrency),
2017-09-22 23:34:56 +02:00
h('div.flex-center.confirm-memo-wrapper', [
2017-10-10 23:30:20 +02:00
h('h3.confirm-screen-send-memo', [ memo ? `"${memo}"` : '' ]),
2017-09-22 23:34:56 +02:00
]),
])
)
: (
h('div.confirm-send-token__hero-amount-wrapper', [
h('h3.flex-center.confirm-screen-send-amount', tokenAmount),
h('h3.flex-center.confirm-screen-send-amount-currency', symbol),
h('div.flex-center.confirm-memo-wrapper', [
2017-10-10 23:30:20 +02:00
h('h3.confirm-screen-send-memo', [ memo ? `"${memo}"` : '' ]),
2017-09-22 23:34:56 +02:00
]),
])
)
}
ConfirmSendToken.prototype.renderGasFee = function () {
const { token: { symbol }, currentCurrency } = this.props
2017-09-22 23:34:56 +02:00
const { fiat: fiatGas, token: tokenGas, eth: ethGas } = this.getGasFee()
return (
h('section.flex-row.flex-center.confirm-screen-row', [
h('span.confirm-screen-label.confirm-screen-section-column', [ 'Gas Fee' ]),
h('div.confirm-screen-section-column', [
h('div.confirm-screen-row-info', `${fiatGas} ${currentCurrency}`),
2017-09-22 23:34:56 +02:00
h(
'div.confirm-screen-row-detail',
tokenGas ? `${tokenGas} ${symbol}` : `${ethGas} ETH`
),
]),
])
)
}
ConfirmSendToken.prototype.renderTotalPlusGas = function () {
const { token: { symbol }, currentCurrency } = this.props
2017-09-22 23:34:56 +02:00
const { fiat: fiatAmount, token: tokenAmount } = this.getAmount()
const { fiat: fiatGas, token: tokenGas } = this.getGasFee()
return fiatAmount && fiatGas
? (
h('section.flex-row.flex-center.confirm-screen-total-box ', [
h('div.confirm-screen-section-column', [
h('span.confirm-screen-label', [ 'Total ' ]),
h('div.confirm-screen-total-box__subtitle', [ 'Amount + Gas' ]),
]),
h('div.confirm-screen-section-column', [
h('div.confirm-screen-row-info', `${fiatAmount + fiatGas} ${currentCurrency}`),
2017-10-21 01:18:25 +02:00
h('div.confirm-screen-row-detail', `${addCurrencies(tokenAmount, tokenGas || '0')} ${symbol}`),
2017-09-22 23:34:56 +02:00
]),
])
)
: (
h('section.flex-row.flex-center.confirm-screen-total-box ', [
h('div.confirm-screen-section-column', [
h('span.confirm-screen-label', [ 'Total ' ]),
h('div.confirm-screen-total-box__subtitle', [ 'Amount + Gas' ]),
]),
h('div.confirm-screen-section-column', [
h('div.confirm-screen-row-info', `${tokenAmount} ${symbol}`),
h('div.confirm-screen-row-detail', `+ ${fiatGas} ${currentCurrency} Gas`),
2017-09-22 23:34:56 +02:00
]),
])
)
}
ConfirmSendToken.prototype.render = function () {
const { backToAccountDetail, selectedAddress } = this.props
const txMeta = this.gatherTxMeta()
const txParams = txMeta.txParams || {}
const {
from: {
address: fromAddress,
name: fromName,
},
to: {
address: toAddress,
name: toName,
},
} = this.getData()
this.inputs = []
return (
2017-10-10 23:30:20 +02:00
h('div.confirm-screen-container', {
2017-09-22 23:34:56 +02:00
style: { minWidth: '355px' },
}, [
// Main Send token Card
h('div.confirm-screen-wrapper.flex-column.flex-grow', [
h('h3.flex-center.confirm-screen-header', [
h('button.confirm-screen-back-button', {
onClick: () => backToAccountDetail(selectedAddress),
}, 'BACK'),
h('div.confirm-screen-title', 'Confirm Transaction'),
2017-10-10 23:30:20 +02:00
h('div.confirm-screen-header-tip'),
2017-09-22 23:34:56 +02:00
]),
h('div.flex-row.flex-center.confirm-screen-identicons', [
h('div.confirm-screen-account-wrapper', [
h(
Identicon,
{
address: fromAddress,
2017-10-10 23:30:20 +02:00
diameter: 60,
2017-09-22 23:34:56 +02:00
},
),
h('span.confirm-screen-account-name', fromName),
2017-10-10 23:30:20 +02:00
// h('span.confirm-screen-account-number', fromAddress.slice(fromAddress.length - 4)),
2017-09-22 23:34:56 +02:00
]),
h('i.fa.fa-arrow-right.fa-lg'),
h('div.confirm-screen-account-wrapper', [
h(
Identicon,
{
address: toAddress,
2017-10-10 23:30:20 +02:00
diameter: 60,
2017-09-22 23:34:56 +02:00
},
),
h('span.confirm-screen-account-name', toName),
2017-10-10 23:30:20 +02:00
// h('span.confirm-screen-account-number', toAddress.slice(toAddress.length - 4)),
2017-09-22 23:34:56 +02:00
]),
]),
2017-10-10 23:30:20 +02:00
// h('h3.flex-center.confirm-screen-sending-to-message', {
// style: {
// textAlign: 'center',
// fontSize: '16px',
// },
// }, [
// `You're sending to Recipient ...${toAddress.slice(toAddress.length - 4)}`,
// ]),
2017-09-22 23:34:56 +02:00
this.renderHeroAmount(),
h('div.confirm-screen-rows', [
h('section.flex-row.flex-center.confirm-screen-row', [
h('span.confirm-screen-label.confirm-screen-section-column', [ 'From' ]),
h('div.confirm-screen-section-column', [
h('div.confirm-screen-row-info', fromName),
h('div.confirm-screen-row-detail', `...${fromAddress.slice(fromAddress.length - 4)}`),
]),
]),
2017-10-21 01:18:25 +02:00
toAddress && h('section.flex-row.flex-center.confirm-screen-row', [
2017-09-22 23:34:56 +02:00
h('span.confirm-screen-label.confirm-screen-section-column', [ 'To' ]),
h('div.confirm-screen-section-column', [
h('div.confirm-screen-row-info', toName),
h('div.confirm-screen-row-detail', `...${toAddress.slice(toAddress.length - 4)}`),
]),
]),
this.renderGasFee(),
this.renderTotalPlusGas(),
]),
]),
2017-10-10 23:30:20 +02:00
h('form#pending-tx-form', {
2017-09-22 23:34:56 +02:00
onSubmit: this.onSubmit,
}, [
// Cancel Button
h('div.cancel.btn-light.confirm-screen-cancel-button', {
onClick: (event) => this.cancel(event, txMeta),
}, 'CANCEL'),
2017-10-10 23:30:20 +02:00
// Accept Button
h('button.confirm-screen-confirm-button', ['CONFIRM']),
2017-09-22 23:34:56 +02:00
]),
2017-10-10 23:30:20 +02:00
2017-09-22 23:34:56 +02:00
])
)
}
ConfirmSendToken.prototype.onSubmit = function (event) {
event.preventDefault()
const txMeta = this.gatherTxMeta()
const valid = this.checkValidity()
this.setState({ valid, submitting: true })
if (valid && this.verifyGasParams()) {
this.props.sendTransaction(txMeta, event)
} else {
this.props.dispatch(actions.displayWarning('Invalid Gas Parameters'))
this.setState({ submitting: false })
}
}
ConfirmSendToken.prototype.cancel = function (event, txMeta) {
event.preventDefault()
this.props.cancelTransaction(txMeta)
}
ConfirmSendToken.prototype.checkValidity = function () {
const form = this.getFormEl()
const valid = form.checkValidity()
return valid
}
ConfirmSendToken.prototype.getFormEl = function () {
const form = document.querySelector('form#pending-tx-form')
// Stub out form for unit tests:
if (!form) {
return { checkValidity () { return true } }
}
return form
}
// After a customizable state value has been updated,
ConfirmSendToken.prototype.gatherTxMeta = function () {
const props = this.props
const state = this.state
const txData = clone(state.txData) || clone(props.txData)
// log.debug(`UI has defaulted to tx meta ${JSON.stringify(txData)}`)
return txData
}
ConfirmSendToken.prototype.verifyGasParams = function () {
// We call this in case the gas has not been modified at all
if (!this.state) { return true }
return (
this._notZeroOrEmptyString(this.state.gas) &&
this._notZeroOrEmptyString(this.state.gasPrice)
)
}
ConfirmSendToken.prototype._notZeroOrEmptyString = function (obj) {
return obj !== '' && obj !== '0x0'
}
ConfirmSendToken.prototype.bnMultiplyByFraction = function (targetBN, numerator, denominator) {
const numBN = new BN(numerator)
const denomBN = new BN(denominator)
return targetBN.mul(numBN).div(denomBN)
}