2018-06-23 08:52:45 +02:00
|
|
|
import { connect } from 'react-redux'
|
|
|
|
import { compose } from 'recompose'
|
|
|
|
import { withRouter } from 'react-router-dom'
|
|
|
|
import R from 'ramda'
|
2018-07-20 23:18:50 +02:00
|
|
|
import contractMap from 'eth-contract-metadata'
|
2018-06-23 08:52:45 +02:00
|
|
|
import ConfirmTransactionBase from './confirm-transaction-base.component'
|
|
|
|
import {
|
|
|
|
clearConfirmTransaction,
|
2019-03-22 00:03:30 +01:00
|
|
|
} from '../../ducks/confirm-transaction/confirm-transaction.duck'
|
2019-07-03 22:33:44 +02:00
|
|
|
|
2019-09-27 06:30:36 +02:00
|
|
|
import {
|
|
|
|
updateCustomNonce,
|
|
|
|
clearSend,
|
|
|
|
cancelTx,
|
|
|
|
cancelTxs,
|
|
|
|
updateAndApproveTx,
|
|
|
|
showModal,
|
|
|
|
setMetaMetricsSendCount,
|
|
|
|
updateTransaction,
|
|
|
|
getNextNonce,
|
|
|
|
} from '../../store/actions'
|
2018-07-06 20:58:41 +02:00
|
|
|
import {
|
|
|
|
INSUFFICIENT_FUNDS_ERROR_KEY,
|
|
|
|
GAS_LIMIT_TOO_LOW_ERROR_KEY,
|
2019-03-22 00:03:30 +01:00
|
|
|
} from '../../helpers/constants/error-keys'
|
|
|
|
import { getHexGasTotal } from '../../helpers/utils/confirm-tx.util'
|
2019-04-17 21:15:13 +02:00
|
|
|
import { isBalanceSufficient, calcGasTotal } from '../send/send.utils'
|
2019-03-22 00:03:30 +01:00
|
|
|
import { conversionGreaterThan } from '../../helpers/utils/conversion-util'
|
2019-04-17 21:15:13 +02:00
|
|
|
import { MIN_GAS_LIMIT_DEC } from '../send/send.constants'
|
2019-03-22 00:03:30 +01:00
|
|
|
import { checksumAddress, addressSlicer, valuesFor } from '../../helpers/utils/util'
|
2019-09-27 06:30:36 +02:00
|
|
|
import { getMetaMaskAccounts, getCustomNonceValue, getUseNonceField, getAdvancedInlineGasShown, preferencesSelector, getIsMainnet, getKnownMethodData } from '../../selectors/selectors'
|
2019-07-03 22:33:44 +02:00
|
|
|
import { transactionFeeSelector } from '../../selectors/confirm-transaction'
|
2018-07-20 23:18:50 +02:00
|
|
|
|
|
|
|
const casedContractMap = Object.keys(contractMap).reduce((acc, base) => {
|
|
|
|
return {
|
|
|
|
...acc,
|
|
|
|
[base.toLowerCase()]: contractMap[base],
|
|
|
|
}
|
|
|
|
}, {})
|
2018-06-23 08:52:45 +02:00
|
|
|
|
2019-09-27 06:30:36 +02:00
|
|
|
let customNonceValue = ''
|
|
|
|
const customNonceMerge = txData => customNonceValue ? ({
|
|
|
|
...txData,
|
|
|
|
customNonceValue,
|
|
|
|
}) : txData
|
|
|
|
|
2019-06-18 14:17:14 +02:00
|
|
|
const mapStateToProps = (state, ownProps) => {
|
|
|
|
const { toAddress: propsToAddress, match: { params = {} } } = ownProps
|
|
|
|
const { id: paramsTransactionId } = params
|
2019-02-26 19:30:41 +01:00
|
|
|
const { showFiatInTestnets } = preferencesSelector(state)
|
|
|
|
const isMainnet = getIsMainnet(state)
|
2019-07-03 22:33:44 +02:00
|
|
|
const { confirmTransaction, metamask } = state
|
|
|
|
const {
|
|
|
|
conversionRate,
|
|
|
|
identities,
|
2019-08-08 20:45:30 +02:00
|
|
|
addressBook,
|
2019-07-03 22:33:44 +02:00
|
|
|
currentCurrency,
|
|
|
|
selectedAddress,
|
|
|
|
selectedAddressTxList,
|
|
|
|
assetImages,
|
|
|
|
network,
|
|
|
|
unapprovedTxs,
|
|
|
|
metaMetricsSendCount,
|
2019-09-27 06:30:36 +02:00
|
|
|
nextNonce,
|
2019-07-03 22:33:44 +02:00
|
|
|
} = metamask
|
2018-06-23 08:52:45 +02:00
|
|
|
const {
|
|
|
|
tokenData,
|
|
|
|
txData,
|
|
|
|
tokenProps,
|
|
|
|
nonce,
|
|
|
|
} = confirmTransaction
|
2019-06-18 14:17:14 +02:00
|
|
|
const { txParams = {}, lastGasPrice, id: transactionId, transactionCategory } = txData
|
2019-07-03 22:33:44 +02:00
|
|
|
const transaction = R.find(({ id }) => id === (transactionId || Number(paramsTransactionId)))(selectedAddressTxList) || {}
|
2019-02-06 01:24:28 +01:00
|
|
|
const {
|
|
|
|
from: fromAddress,
|
|
|
|
to: txParamsToAddress,
|
|
|
|
gasPrice,
|
|
|
|
gas: gasLimit,
|
|
|
|
value: amount,
|
2019-06-18 14:17:14 +02:00
|
|
|
data,
|
2019-07-03 22:33:44 +02:00
|
|
|
} = transaction && transaction.txParams || txParams
|
2018-11-30 23:51:24 +01:00
|
|
|
const accounts = getMetaMaskAccounts(state)
|
2018-08-30 04:13:25 +02:00
|
|
|
const assetImage = assetImages[txParamsToAddress]
|
2018-09-13 10:47:05 +02:00
|
|
|
|
2018-06-23 08:52:45 +02:00
|
|
|
const { balance } = accounts[selectedAddress]
|
|
|
|
const { name: fromName } = identities[selectedAddress]
|
|
|
|
const toAddress = propsToAddress || txParamsToAddress
|
2018-07-20 23:18:50 +02:00
|
|
|
const toName = identities[toAddress]
|
|
|
|
? identities[toAddress].name
|
2019-02-14 15:13:12 +01:00
|
|
|
: (
|
|
|
|
casedContractMap[toAddress]
|
|
|
|
? casedContractMap[toAddress].name
|
|
|
|
: addressSlicer(checksumAddress(toAddress))
|
|
|
|
)
|
2018-07-20 23:18:50 +02:00
|
|
|
|
2019-08-08 20:45:30 +02:00
|
|
|
const addressBookObject = addressBook[checksumAddress(toAddress)]
|
|
|
|
const toNickname = addressBookObject ? addressBookObject.name : ''
|
2018-06-23 08:52:45 +02:00
|
|
|
const isTxReprice = Boolean(lastGasPrice)
|
|
|
|
const transactionStatus = transaction ? transaction.status : ''
|
|
|
|
|
2019-07-03 22:33:44 +02:00
|
|
|
const {
|
|
|
|
ethTransactionAmount,
|
|
|
|
ethTransactionFee,
|
|
|
|
ethTransactionTotal,
|
|
|
|
fiatTransactionAmount,
|
|
|
|
fiatTransactionFee,
|
|
|
|
fiatTransactionTotal,
|
|
|
|
hexTransactionAmount,
|
|
|
|
hexTransactionFee,
|
|
|
|
hexTransactionTotal,
|
|
|
|
} = transactionFeeSelector(state, transaction)
|
|
|
|
|
2019-06-25 21:42:35 +02:00
|
|
|
if (transaction && transaction.simulationFails) {
|
|
|
|
txData.simulationFails = transaction.simulationFails
|
|
|
|
}
|
|
|
|
|
2018-10-01 18:34:50 +02:00
|
|
|
const currentNetworkUnapprovedTxs = R.filter(
|
|
|
|
({ metamaskNetworkId }) => metamaskNetworkId === network,
|
2018-11-13 18:19:12 +01:00
|
|
|
unapprovedTxs,
|
2018-10-01 18:34:50 +02:00
|
|
|
)
|
2018-11-13 18:19:12 +01:00
|
|
|
const unapprovedTxCount = valuesFor(currentNetworkUnapprovedTxs).length
|
2018-09-20 02:48:28 +02:00
|
|
|
|
2019-02-06 01:24:28 +01:00
|
|
|
const insufficientBalance = !isBalanceSufficient({
|
|
|
|
amount,
|
|
|
|
gasTotal: calcGasTotal(gasLimit, gasPrice),
|
|
|
|
balance,
|
|
|
|
conversionRate,
|
|
|
|
})
|
|
|
|
|
2019-06-18 14:17:14 +02:00
|
|
|
const methodData = getKnownMethodData(state, data) || {}
|
|
|
|
|
2018-06-23 08:52:45 +02:00
|
|
|
return {
|
|
|
|
balance,
|
|
|
|
fromAddress,
|
|
|
|
fromName,
|
|
|
|
toAddress,
|
|
|
|
toName,
|
2019-08-08 20:45:30 +02:00
|
|
|
toNickname,
|
2018-06-23 08:52:45 +02:00
|
|
|
ethTransactionAmount,
|
|
|
|
ethTransactionFee,
|
|
|
|
ethTransactionTotal,
|
|
|
|
fiatTransactionAmount,
|
|
|
|
fiatTransactionFee,
|
|
|
|
fiatTransactionTotal,
|
2018-10-17 01:03:29 +02:00
|
|
|
hexTransactionAmount,
|
|
|
|
hexTransactionFee,
|
|
|
|
hexTransactionTotal,
|
2019-07-03 22:33:44 +02:00
|
|
|
txData: { ...txData, ...transaction },
|
2018-06-23 08:52:45 +02:00
|
|
|
tokenData,
|
2018-06-28 20:23:31 +02:00
|
|
|
methodData,
|
2018-06-23 08:52:45 +02:00
|
|
|
tokenProps,
|
|
|
|
isTxReprice,
|
|
|
|
currentCurrency,
|
|
|
|
conversionRate,
|
|
|
|
transactionStatus,
|
|
|
|
nonce,
|
2018-08-30 04:13:25 +02:00
|
|
|
assetImage,
|
2018-09-20 02:48:28 +02:00
|
|
|
unapprovedTxs,
|
|
|
|
unapprovedTxCount,
|
2018-11-13 18:19:12 +01:00
|
|
|
currentNetworkUnapprovedTxs,
|
2018-09-13 10:47:05 +02:00
|
|
|
customGas: {
|
2019-07-03 22:33:44 +02:00
|
|
|
gasLimit,
|
|
|
|
gasPrice,
|
2018-09-13 10:47:05 +02:00
|
|
|
},
|
2019-02-06 01:24:28 +01:00
|
|
|
advancedInlineGasShown: getAdvancedInlineGasShown(state),
|
2019-09-27 06:30:36 +02:00
|
|
|
useNonceField: getUseNonceField(state),
|
|
|
|
customNonceValue: getCustomNonceValue(state),
|
2019-02-06 01:24:28 +01:00
|
|
|
insufficientBalance,
|
2019-02-26 19:30:41 +01:00
|
|
|
hideSubtitle: (!isMainnet && !showFiatInTestnets),
|
|
|
|
hideFiatConversion: (!isMainnet && !showFiatInTestnets),
|
2019-03-05 16:45:01 +01:00
|
|
|
metaMetricsSendCount,
|
2019-06-18 14:17:14 +02:00
|
|
|
transactionCategory,
|
2019-09-27 06:30:36 +02:00
|
|
|
nextNonce,
|
2018-06-23 08:52:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-27 06:30:36 +02:00
|
|
|
export const mapDispatchToProps = dispatch => {
|
2018-06-23 08:52:45 +02:00
|
|
|
return {
|
2019-09-27 06:30:36 +02:00
|
|
|
updateCustomNonce: value => {
|
|
|
|
customNonceValue = value
|
|
|
|
dispatch(updateCustomNonce(value))
|
|
|
|
},
|
2018-06-23 08:52:45 +02:00
|
|
|
clearConfirmTransaction: () => dispatch(clearConfirmTransaction()),
|
|
|
|
clearSend: () => dispatch(clearSend()),
|
2018-09-17 19:34:29 +02:00
|
|
|
showTransactionConfirmedModal: ({ onSubmit }) => {
|
|
|
|
return dispatch(showModal({ name: 'TRANSACTION_CONFIRMED', onSubmit }))
|
2018-06-23 08:52:45 +02:00
|
|
|
},
|
|
|
|
showCustomizeGasModal: ({ txData, onSubmit, validate }) => {
|
2018-07-31 04:28:05 +02:00
|
|
|
return dispatch(showModal({ name: 'CUSTOMIZE_GAS', txData, onSubmit, validate }))
|
2018-06-23 08:52:45 +02:00
|
|
|
},
|
2019-07-03 22:33:44 +02:00
|
|
|
updateGasAndCalculate: (updatedTx) => {
|
|
|
|
return dispatch(updateTransaction(updatedTx))
|
2018-06-23 08:52:45 +02:00
|
|
|
},
|
2018-09-24 07:07:19 +02:00
|
|
|
showRejectTransactionsConfirmationModal: ({ onSubmit, unapprovedTxCount }) => {
|
|
|
|
return dispatch(showModal({ name: 'REJECT_TRANSACTIONS', onSubmit, unapprovedTxCount }))
|
|
|
|
},
|
2018-06-23 08:52:45 +02:00
|
|
|
cancelTransaction: ({ id }) => dispatch(cancelTx({ id })),
|
2018-09-20 02:48:28 +02:00
|
|
|
cancelAllTransactions: (txList) => dispatch(cancelTxs(txList)),
|
2019-09-27 06:30:36 +02:00
|
|
|
sendTransaction: txData => dispatch(updateAndApproveTx(customNonceMerge(txData))),
|
2019-03-05 16:45:01 +01:00
|
|
|
setMetaMetricsSendCount: val => dispatch(setMetaMetricsSendCount(val)),
|
2019-09-27 06:30:36 +02:00
|
|
|
getNextNonce: () => dispatch(getNextNonce()),
|
2018-06-23 08:52:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-06 20:58:41 +02:00
|
|
|
const getValidateEditGas = ({ balance, conversionRate, txData }) => {
|
|
|
|
const { txParams: { value: amount } = {} } = txData
|
|
|
|
|
|
|
|
return ({ gasLimit, gasPrice }) => {
|
|
|
|
const gasTotal = getHexGasTotal({ gasLimit, gasPrice })
|
|
|
|
const hasSufficientBalance = isBalanceSufficient({
|
|
|
|
amount,
|
|
|
|
gasTotal,
|
|
|
|
balance,
|
|
|
|
conversionRate,
|
|
|
|
})
|
|
|
|
|
|
|
|
if (!hasSufficientBalance) {
|
|
|
|
return {
|
|
|
|
valid: false,
|
|
|
|
errorKey: INSUFFICIENT_FUNDS_ERROR_KEY,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const gasLimitTooLow = gasLimit && conversionGreaterThan(
|
|
|
|
{
|
|
|
|
value: MIN_GAS_LIMIT_DEC,
|
|
|
|
fromNumericBase: 'dec',
|
|
|
|
conversionRate,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: gasLimit,
|
|
|
|
fromNumericBase: 'hex',
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
if (gasLimitTooLow) {
|
|
|
|
return {
|
|
|
|
valid: false,
|
|
|
|
errorKey: GAS_LIMIT_TOO_LOW_ERROR_KEY,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
valid: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const mergeProps = (stateProps, dispatchProps, ownProps) => {
|
2018-09-20 02:48:28 +02:00
|
|
|
const { balance, conversionRate, txData, unapprovedTxs } = stateProps
|
2018-07-06 20:58:41 +02:00
|
|
|
const {
|
2018-09-20 02:48:28 +02:00
|
|
|
cancelAllTransactions: dispatchCancelAllTransactions,
|
2018-07-06 20:58:41 +02:00
|
|
|
showCustomizeGasModal: dispatchShowCustomizeGasModal,
|
|
|
|
updateGasAndCalculate: dispatchUpdateGasAndCalculate,
|
|
|
|
...otherDispatchProps
|
|
|
|
} = dispatchProps
|
|
|
|
|
|
|
|
const validateEditGas = getValidateEditGas({ balance, conversionRate, txData })
|
|
|
|
|
|
|
|
return {
|
|
|
|
...stateProps,
|
|
|
|
...otherDispatchProps,
|
|
|
|
...ownProps,
|
|
|
|
showCustomizeGasModal: () => dispatchShowCustomizeGasModal({
|
|
|
|
txData,
|
2018-09-13 10:47:05 +02:00
|
|
|
onSubmit: customGas => dispatchUpdateGasAndCalculate(customGas),
|
2018-07-06 20:58:41 +02:00
|
|
|
validate: validateEditGas,
|
|
|
|
}),
|
2018-09-20 02:48:28 +02:00
|
|
|
cancelAllTransactions: () => dispatchCancelAllTransactions(valuesFor(unapprovedTxs)),
|
2019-07-03 22:33:44 +02:00
|
|
|
updateGasAndCalculate: ({ gasLimit, gasPrice }) => {
|
|
|
|
const updatedTx = {
|
|
|
|
...txData,
|
|
|
|
txParams: {
|
|
|
|
...txData.txParams,
|
|
|
|
gas: gasLimit,
|
|
|
|
gasPrice,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
dispatchUpdateGasAndCalculate(updatedTx)
|
|
|
|
},
|
2018-07-06 20:58:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-23 08:52:45 +02:00
|
|
|
export default compose(
|
|
|
|
withRouter,
|
2018-07-06 20:58:41 +02:00
|
|
|
connect(mapStateToProps, mapDispatchToProps, mergeProps)
|
2018-06-23 08:52:45 +02:00
|
|
|
)(ConfirmTransactionBase)
|