2017-08-30 12:33:00 +02:00
|
|
|
const Component = require('react').Component
|
2018-03-29 17:00:44 +02:00
|
|
|
const PropTypes = require('prop-types')
|
2017-08-30 12:33:00 +02:00
|
|
|
const h = require('react-hyperscript')
|
2018-03-29 17:00:44 +02:00
|
|
|
const connect = require('react-redux').connect
|
2017-08-30 12:33:00 +02:00
|
|
|
const inherits = require('util').inherits
|
2017-09-12 23:59:33 +02:00
|
|
|
const classnames = require('classnames')
|
2017-09-14 10:09:57 +02:00
|
|
|
const abi = require('human-standard-token-abi')
|
|
|
|
const abiDecoder = require('abi-decoder')
|
|
|
|
abiDecoder.addABI(abi)
|
2017-08-30 12:33:00 +02:00
|
|
|
const Identicon = require('./identicon')
|
2017-10-27 19:39:40 +02:00
|
|
|
const contractMap = require('eth-contract-metadata')
|
2018-04-17 21:57:55 +02:00
|
|
|
const { checksumAddress } = require('../util')
|
2017-08-30 12:33:00 +02:00
|
|
|
|
2018-03-09 04:49:26 +01:00
|
|
|
const actions = require('../actions')
|
2017-10-27 07:55:34 +02:00
|
|
|
const { conversionUtil, multiplyCurrencies } = require('../conversion-util')
|
2017-10-30 18:48:50 +01:00
|
|
|
const { calcTokenAmount } = require('../token-util')
|
2017-09-12 22:40:28 +02:00
|
|
|
|
2017-10-19 04:09:26 +02:00
|
|
|
const { getCurrentCurrency } = require('../selectors')
|
|
|
|
|
2018-03-29 17:00:44 +02:00
|
|
|
TxListItem.contextTypes = {
|
|
|
|
t: PropTypes.func,
|
|
|
|
}
|
|
|
|
|
2018-03-09 04:49:26 +01:00
|
|
|
module.exports = connect(mapStateToProps, mapDispatchToProps)(TxListItem)
|
2017-09-14 10:09:57 +02:00
|
|
|
|
2018-03-29 17:00:44 +02:00
|
|
|
|
2017-09-14 10:09:57 +02:00
|
|
|
function mapStateToProps (state) {
|
|
|
|
return {
|
|
|
|
tokens: state.metamask.tokens,
|
2017-10-19 04:09:26 +02:00
|
|
|
currentCurrency: getCurrentCurrency(state),
|
2018-04-16 05:37:42 +02:00
|
|
|
contractExchangeRates: state.metamask.contractExchangeRates,
|
2018-03-09 04:49:26 +01:00
|
|
|
selectedAddressTxList: state.metamask.selectedAddressTxList,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function mapDispatchToProps (dispatch) {
|
|
|
|
return {
|
2018-03-14 03:14:05 +01:00
|
|
|
setSelectedToken: tokenAddress => dispatch(actions.setSelectedToken(tokenAddress)),
|
2018-03-09 04:49:26 +01:00
|
|
|
retryTransaction: transactionId => dispatch(actions.retryTransaction(transactionId)),
|
2017-09-14 10:09:57 +02:00
|
|
|
}
|
|
|
|
}
|
2017-08-30 12:33:00 +02:00
|
|
|
|
|
|
|
inherits(TxListItem, Component)
|
|
|
|
function TxListItem () {
|
|
|
|
Component.call(this)
|
2017-10-27 19:39:40 +02:00
|
|
|
|
|
|
|
this.state = {
|
|
|
|
total: null,
|
|
|
|
fiatTotal: null,
|
2018-03-14 03:14:05 +01:00
|
|
|
isTokenTx: null,
|
2017-10-27 19:39:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TxListItem.prototype.componentDidMount = async function () {
|
|
|
|
const { txParams = {} } = this.props
|
|
|
|
|
|
|
|
const decodedData = txParams.data && abiDecoder.decodeMethod(txParams.data)
|
|
|
|
const { name: txDataName } = decodedData || {}
|
2018-03-14 03:14:05 +01:00
|
|
|
const isTokenTx = txDataName === 'transfer'
|
2017-10-27 19:39:40 +02:00
|
|
|
|
2018-03-14 03:14:05 +01:00
|
|
|
const { total, fiatTotal } = isTokenTx
|
2017-10-27 19:39:40 +02:00
|
|
|
? await this.getSendTokenTotal()
|
|
|
|
: this.getSendEtherTotal()
|
|
|
|
|
2018-03-14 03:26:45 +01:00
|
|
|
this.setState({ total, fiatTotal, isTokenTx })
|
2017-08-30 12:33:00 +02:00
|
|
|
}
|
|
|
|
|
2017-09-14 10:09:57 +02:00
|
|
|
TxListItem.prototype.getAddressText = function () {
|
|
|
|
const {
|
|
|
|
address,
|
|
|
|
txParams = {},
|
2018-03-28 15:21:16 +02:00
|
|
|
isMsg,
|
2017-09-14 10:09:57 +02:00
|
|
|
} = this.props
|
|
|
|
|
|
|
|
const decodedData = txParams.data && abiDecoder.decodeMethod(txParams.data)
|
|
|
|
const { name: txDataName, params = [] } = decodedData || {}
|
|
|
|
const { value } = params[0] || {}
|
2018-04-17 21:57:55 +02:00
|
|
|
const checksummedAddress = checksumAddress(address)
|
|
|
|
const checksummedValue = checksumAddress(value)
|
2017-09-14 10:09:57 +02:00
|
|
|
|
2018-03-28 15:21:16 +02:00
|
|
|
let addressText
|
|
|
|
if (txDataName === 'transfer' || address) {
|
2018-04-17 21:57:55 +02:00
|
|
|
const addressToRender = txDataName === 'transfer' ? checksummedValue : checksummedAddress
|
2018-03-29 15:02:30 +02:00
|
|
|
addressText = `${addressToRender.slice(0, 10)}...${addressToRender.slice(-4)}`
|
2018-03-28 15:21:16 +02:00
|
|
|
} else if (isMsg) {
|
2018-03-30 04:30:31 +02:00
|
|
|
addressText = this.context.t('sigRequest')
|
2018-03-28 15:21:16 +02:00
|
|
|
} else {
|
2018-03-30 04:30:31 +02:00
|
|
|
addressText = this.context.t('contractDeployment')
|
2017-09-14 10:09:57 +02:00
|
|
|
}
|
2018-03-28 15:21:16 +02:00
|
|
|
|
|
|
|
return addressText
|
2017-08-30 12:33:00 +02:00
|
|
|
}
|
|
|
|
|
2017-09-14 10:09:57 +02:00
|
|
|
TxListItem.prototype.getSendEtherTotal = function () {
|
2017-08-30 12:33:00 +02:00
|
|
|
const {
|
|
|
|
transactionAmount,
|
2017-09-12 22:40:28 +02:00
|
|
|
conversionRate,
|
2017-09-14 10:09:57 +02:00
|
|
|
address,
|
2017-10-19 04:09:26 +02:00
|
|
|
currentCurrency,
|
2017-08-30 12:33:00 +02:00
|
|
|
} = this.props
|
|
|
|
|
2017-09-14 10:09:57 +02:00
|
|
|
if (!address) {
|
|
|
|
return {}
|
|
|
|
}
|
|
|
|
|
2017-10-19 04:09:26 +02:00
|
|
|
const totalInFiat = conversionUtil(transactionAmount, {
|
2017-09-12 22:40:28 +02:00
|
|
|
fromNumericBase: 'hex',
|
|
|
|
toNumericBase: 'dec',
|
|
|
|
fromCurrency: 'ETH',
|
2017-10-19 04:09:26 +02:00
|
|
|
toCurrency: currentCurrency,
|
2017-09-14 04:20:13 +02:00
|
|
|
fromDenomination: 'WEI',
|
2017-09-12 22:40:28 +02:00
|
|
|
numberOfDecimals: 2,
|
|
|
|
conversionRate,
|
|
|
|
})
|
|
|
|
const totalInETH = conversionUtil(transactionAmount, {
|
|
|
|
fromNumericBase: 'hex',
|
|
|
|
toNumericBase: 'dec',
|
|
|
|
fromCurrency: 'ETH',
|
|
|
|
toCurrency: 'ETH',
|
2017-09-14 04:20:13 +02:00
|
|
|
fromDenomination: 'WEI',
|
2017-09-12 22:40:28 +02:00
|
|
|
conversionRate,
|
|
|
|
numberOfDecimals: 6,
|
|
|
|
})
|
|
|
|
|
2017-09-14 10:09:57 +02:00
|
|
|
return {
|
|
|
|
total: `${totalInETH} ETH`,
|
2017-10-19 04:09:26 +02:00
|
|
|
fiatTotal: `${totalInFiat} ${currentCurrency.toUpperCase()}`,
|
2017-09-14 10:09:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-27 19:39:40 +02:00
|
|
|
TxListItem.prototype.getTokenInfo = async function () {
|
|
|
|
const { txParams = {}, tokenInfoGetter, tokens } = this.props
|
|
|
|
const toAddress = txParams.to
|
|
|
|
|
|
|
|
let decimals
|
|
|
|
let symbol
|
|
|
|
|
|
|
|
({ decimals, symbol } = tokens.filter(({ address }) => address === toAddress)[0] || {})
|
|
|
|
|
|
|
|
if (!decimals && !symbol) {
|
|
|
|
({ decimals, symbol } = contractMap[toAddress] || {})
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!decimals && !symbol) {
|
|
|
|
({ decimals, symbol } = await tokenInfoGetter(toAddress))
|
|
|
|
}
|
|
|
|
|
2018-04-16 05:37:42 +02:00
|
|
|
return { decimals, symbol, address: toAddress }
|
2017-10-27 19:39:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TxListItem.prototype.getSendTokenTotal = async function () {
|
2017-09-14 10:09:57 +02:00
|
|
|
const {
|
|
|
|
txParams = {},
|
2017-10-27 07:55:34 +02:00
|
|
|
conversionRate,
|
2018-04-16 05:37:42 +02:00
|
|
|
contractExchangeRates,
|
2017-10-27 07:55:34 +02:00
|
|
|
currentCurrency,
|
2017-09-14 10:09:57 +02:00
|
|
|
} = this.props
|
|
|
|
|
|
|
|
const decodedData = txParams.data && abiDecoder.decodeMethod(txParams.data)
|
|
|
|
const { params = [] } = decodedData || {}
|
|
|
|
const { value } = params[1] || {}
|
2018-04-16 05:37:42 +02:00
|
|
|
const { decimals, symbol, address } = await this.getTokenInfo()
|
2017-10-30 18:48:50 +01:00
|
|
|
const total = calcTokenAmount(value, decimals)
|
2017-09-14 10:09:57 +02:00
|
|
|
|
2017-10-27 07:55:34 +02:00
|
|
|
let tokenToFiatRate
|
|
|
|
let totalInFiat
|
|
|
|
|
2018-04-16 05:37:42 +02:00
|
|
|
if (contractExchangeRates[address]) {
|
2017-10-27 07:55:34 +02:00
|
|
|
tokenToFiatRate = multiplyCurrencies(
|
2018-04-16 05:37:42 +02:00
|
|
|
contractExchangeRates[address],
|
2017-10-27 07:55:34 +02:00
|
|
|
conversionRate
|
|
|
|
)
|
|
|
|
|
|
|
|
totalInFiat = conversionUtil(total, {
|
|
|
|
fromNumericBase: 'dec',
|
|
|
|
toNumericBase: 'dec',
|
|
|
|
fromCurrency: symbol,
|
|
|
|
toCurrency: currentCurrency,
|
|
|
|
numberOfDecimals: 2,
|
|
|
|
conversionRate: tokenToFiatRate,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const showFiat = Boolean(totalInFiat) && currentCurrency.toUpperCase() !== symbol
|
|
|
|
|
2017-09-14 10:09:57 +02:00
|
|
|
return {
|
|
|
|
total: `${total} ${symbol}`,
|
2017-10-27 07:55:34 +02:00
|
|
|
fiatTotal: showFiat && `${totalInFiat} ${currentCurrency.toUpperCase()}`,
|
2017-09-14 10:09:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-09 04:49:26 +01:00
|
|
|
TxListItem.prototype.showRetryButton = function () {
|
|
|
|
const {
|
2018-03-14 00:39:04 +01:00
|
|
|
transactionSubmittedTime,
|
2018-03-09 04:49:26 +01:00
|
|
|
selectedAddressTxList,
|
|
|
|
transactionId,
|
|
|
|
txParams,
|
|
|
|
} = this.props
|
2018-03-28 15:21:16 +02:00
|
|
|
if (!txParams) {
|
|
|
|
return false
|
|
|
|
}
|
2018-03-09 04:49:26 +01:00
|
|
|
const currentNonce = txParams.nonce
|
|
|
|
const currentNonceTxs = selectedAddressTxList.filter(tx => tx.txParams.nonce === currentNonce)
|
2018-03-14 02:59:47 +01:00
|
|
|
const currentNonceSubmittedTxs = currentNonceTxs.filter(tx => tx.status === 'submitted')
|
2018-03-14 01:02:22 +01:00
|
|
|
const lastSubmittedTxWithCurrentNonce = currentNonceSubmittedTxs[currentNonceSubmittedTxs.length - 1]
|
2018-05-05 17:11:53 +02:00
|
|
|
const currentTxIsLatestWithNonce = lastSubmittedTxWithCurrentNonce &&
|
|
|
|
lastSubmittedTxWithCurrentNonce.id === transactionId
|
2018-03-09 04:49:26 +01:00
|
|
|
|
2018-03-14 01:08:08 +01:00
|
|
|
return currentTxIsLatestWithNonce && Date.now() - transactionSubmittedTime > 30000
|
2018-03-09 04:49:26 +01:00
|
|
|
}
|
|
|
|
|
2018-03-14 03:14:05 +01:00
|
|
|
TxListItem.prototype.setSelectedToken = function (tokenAddress) {
|
|
|
|
this.props.setSelectedToken(tokenAddress)
|
|
|
|
}
|
|
|
|
|
2018-03-09 04:49:26 +01:00
|
|
|
TxListItem.prototype.resubmit = function () {
|
|
|
|
const { transactionId } = this.props
|
|
|
|
this.props.retryTransaction(transactionId)
|
|
|
|
}
|
|
|
|
|
2017-09-14 10:09:57 +02:00
|
|
|
TxListItem.prototype.render = function () {
|
|
|
|
const {
|
|
|
|
transactionStatus,
|
|
|
|
onClick,
|
2018-03-09 04:49:26 +01:00
|
|
|
transactionId,
|
2017-09-14 10:09:57 +02:00
|
|
|
dateString,
|
|
|
|
address,
|
|
|
|
className,
|
2018-03-14 03:14:05 +01:00
|
|
|
txParams,
|
2017-09-14 10:09:57 +02:00
|
|
|
} = this.props
|
2018-03-14 03:14:05 +01:00
|
|
|
const { total, fiatTotal, isTokenTx } = this.state
|
2017-09-14 10:09:57 +02:00
|
|
|
|
2017-08-30 12:33:00 +02:00
|
|
|
return h(`div${className || ''}`, {
|
2018-03-09 04:49:26 +01:00
|
|
|
key: transactionId,
|
|
|
|
onClick: () => onClick && onClick(transactionId),
|
2017-08-30 12:33:00 +02:00
|
|
|
}, [
|
|
|
|
h(`div.flex-column.tx-list-item-wrapper`, {}, [
|
|
|
|
|
|
|
|
h('div.tx-list-date-wrapper', {
|
|
|
|
style: {},
|
|
|
|
}, [
|
|
|
|
h('span.tx-list-date', {}, [
|
|
|
|
dateString,
|
|
|
|
]),
|
|
|
|
]),
|
|
|
|
|
|
|
|
h('div.flex-row.tx-list-content-wrapper', {
|
|
|
|
style: {},
|
|
|
|
}, [
|
|
|
|
|
|
|
|
h('div.tx-list-identicon-wrapper', {
|
|
|
|
style: {},
|
|
|
|
}, [
|
|
|
|
h(Identicon, {
|
|
|
|
address,
|
|
|
|
diameter: 28,
|
|
|
|
}),
|
|
|
|
]),
|
|
|
|
|
|
|
|
h('div.tx-list-account-and-status-wrapper', {}, [
|
|
|
|
h('div.tx-list-account-wrapper', {
|
|
|
|
style: {},
|
|
|
|
}, [
|
|
|
|
h('span.tx-list-account', {}, [
|
|
|
|
this.getAddressText(address),
|
|
|
|
]),
|
|
|
|
]),
|
|
|
|
|
|
|
|
h('div.tx-list-status-wrapper', {
|
|
|
|
style: {},
|
|
|
|
}, [
|
2017-09-12 23:59:33 +02:00
|
|
|
h('span', {
|
|
|
|
className: classnames('tx-list-status', {
|
2017-09-14 10:09:57 +02:00
|
|
|
'tx-list-status--rejected': transactionStatus === 'rejected',
|
|
|
|
'tx-list-status--failed': transactionStatus === 'failed',
|
2018-03-14 00:41:50 +01:00
|
|
|
'tx-list-status--dropped': transactionStatus === 'dropped',
|
2017-09-14 10:09:57 +02:00
|
|
|
}),
|
|
|
|
},
|
2018-03-20 20:06:59 +01:00
|
|
|
this.txStatusIndicator(),
|
2017-09-12 23:59:33 +02:00
|
|
|
),
|
2017-08-30 12:33:00 +02:00
|
|
|
]),
|
|
|
|
]),
|
|
|
|
|
|
|
|
h('div.flex-column.tx-list-details-wrapper', {
|
|
|
|
style: {},
|
|
|
|
}, [
|
|
|
|
|
2018-01-18 05:08:29 +01:00
|
|
|
h('span.tx-list-value', total),
|
2017-09-14 10:09:57 +02:00
|
|
|
|
2018-04-16 05:37:42 +02:00
|
|
|
fiatTotal && h('span.tx-list-fiat-value', fiatTotal),
|
2017-08-30 12:33:00 +02:00
|
|
|
|
|
|
|
]),
|
|
|
|
]),
|
2018-03-08 20:42:25 +01:00
|
|
|
|
2018-03-09 04:49:26 +01:00
|
|
|
this.showRetryButton() && h('div.tx-list-item-retry-container', [
|
2018-03-08 20:42:25 +01:00
|
|
|
|
|
|
|
h('span.tx-list-item-retry-copy', 'Taking too long?'),
|
|
|
|
|
2018-03-09 04:49:26 +01:00
|
|
|
h('span.tx-list-item-retry-link', {
|
|
|
|
onClick: (event) => {
|
|
|
|
event.stopPropagation()
|
2018-03-14 03:14:05 +01:00
|
|
|
if (isTokenTx) {
|
|
|
|
this.setSelectedToken(txParams.to)
|
|
|
|
}
|
2018-03-09 04:49:26 +01:00
|
|
|
this.resubmit()
|
|
|
|
},
|
2018-03-13 06:41:45 +01:00
|
|
|
}, 'Increase the gas price on your transaction'),
|
2018-03-09 04:49:26 +01:00
|
|
|
|
2018-03-08 20:42:25 +01:00
|
|
|
]),
|
|
|
|
|
2017-11-02 13:15:59 +01:00
|
|
|
]), // holding on icon from design
|
2017-08-30 12:33:00 +02:00
|
|
|
])
|
|
|
|
}
|
2018-03-20 20:06:59 +01:00
|
|
|
|
|
|
|
TxListItem.prototype.txStatusIndicator = function () {
|
|
|
|
const { transactionStatus } = this.props
|
|
|
|
|
|
|
|
let name
|
|
|
|
|
|
|
|
if (transactionStatus === 'unapproved') {
|
2018-03-29 17:00:44 +02:00
|
|
|
name = this.context.t('unapproved')
|
2018-03-20 20:06:59 +01:00
|
|
|
} else if (transactionStatus === 'rejected') {
|
2018-03-29 17:00:44 +02:00
|
|
|
name = this.context.t('rejected')
|
2018-03-20 20:06:59 +01:00
|
|
|
} else if (transactionStatus === 'approved') {
|
2018-03-29 17:00:44 +02:00
|
|
|
name = this.context.t('approved')
|
2018-03-20 20:06:59 +01:00
|
|
|
} else if (transactionStatus === 'signed') {
|
2018-03-29 17:00:44 +02:00
|
|
|
name = this.context.t('signed')
|
2018-03-20 20:06:59 +01:00
|
|
|
} else if (transactionStatus === 'submitted') {
|
2018-03-29 17:00:44 +02:00
|
|
|
name = this.context.t('submitted')
|
2018-03-20 20:06:59 +01:00
|
|
|
} else if (transactionStatus === 'confirmed') {
|
2018-03-29 17:00:44 +02:00
|
|
|
name = this.context.t('confirmed')
|
2018-03-20 20:06:59 +01:00
|
|
|
} else if (transactionStatus === 'failed') {
|
2018-03-29 17:00:44 +02:00
|
|
|
name = this.context.t('failed')
|
2018-03-20 20:06:59 +01:00
|
|
|
} else if (transactionStatus === 'dropped') {
|
2018-03-29 17:00:44 +02:00
|
|
|
name = this.context.t('dropped')
|
2018-03-20 20:06:59 +01:00
|
|
|
}
|
|
|
|
return name
|
|
|
|
}
|