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,
|
|
|
|
updateGasAndCalculate,
|
|
|
|
} from '../../../ducks/confirm-transaction.duck'
|
|
|
|
import { clearSend, cancelTx, updateAndApproveTx, showModal } from '../../../actions'
|
2018-07-06 20:58:41 +02:00
|
|
|
import {
|
|
|
|
INSUFFICIENT_FUNDS_ERROR_KEY,
|
|
|
|
GAS_LIMIT_TOO_LOW_ERROR_KEY,
|
|
|
|
} from '../../../constants/error-keys'
|
|
|
|
import { getHexGasTotal } from '../../../helpers/confirm-transaction/util'
|
2018-07-16 17:36:46 +02:00
|
|
|
import { isBalanceSufficient } from '../../send/send.utils'
|
2018-07-06 20:58:41 +02:00
|
|
|
import { conversionGreaterThan } from '../../../conversion-util'
|
2018-07-16 17:36:46 +02:00
|
|
|
import { MIN_GAS_LIMIT_DEC } from '../../send/send.constants'
|
2018-07-20 23:18:50 +02:00
|
|
|
import { addressSlicer } from '../../../util'
|
|
|
|
|
|
|
|
const casedContractMap = Object.keys(contractMap).reduce((acc, base) => {
|
|
|
|
return {
|
|
|
|
...acc,
|
|
|
|
[base.toLowerCase()]: contractMap[base],
|
|
|
|
}
|
|
|
|
}, {})
|
2018-06-23 08:52:45 +02:00
|
|
|
|
|
|
|
const mapStateToProps = (state, props) => {
|
|
|
|
const { toAddress: propsToAddress } = props
|
|
|
|
const { confirmTransaction, metamask } = state
|
|
|
|
const {
|
|
|
|
ethTransactionAmount,
|
|
|
|
ethTransactionFee,
|
|
|
|
ethTransactionTotal,
|
|
|
|
fiatTransactionAmount,
|
|
|
|
fiatTransactionFee,
|
|
|
|
fiatTransactionTotal,
|
|
|
|
hexGasTotal,
|
|
|
|
tokenData,
|
2018-06-28 20:23:31 +02:00
|
|
|
methodData,
|
2018-06-23 08:52:45 +02:00
|
|
|
txData,
|
|
|
|
tokenProps,
|
|
|
|
nonce,
|
|
|
|
} = confirmTransaction
|
|
|
|
const { txParams = {}, lastGasPrice, id: transactionId } = txData
|
|
|
|
const { from: fromAddress, to: txParamsToAddress } = txParams
|
|
|
|
const {
|
|
|
|
conversionRate,
|
|
|
|
identities,
|
|
|
|
currentCurrency,
|
|
|
|
accounts,
|
|
|
|
selectedAddress,
|
|
|
|
selectedAddressTxList,
|
2018-08-30 04:13:25 +02:00
|
|
|
assetImages,
|
2018-06-23 08:52:45 +02:00
|
|
|
} = metamask
|
2018-08-30 04:13:25 +02:00
|
|
|
const assetImage = assetImages[txParamsToAddress]
|
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
|
|
|
|
: casedContractMap[toAddress] ? casedContractMap[toAddress].name : addressSlicer(toAddress)
|
|
|
|
|
2018-06-23 08:52:45 +02:00
|
|
|
const isTxReprice = Boolean(lastGasPrice)
|
|
|
|
|
|
|
|
const transaction = R.find(({ id }) => id === transactionId)(selectedAddressTxList)
|
|
|
|
const transactionStatus = transaction ? transaction.status : ''
|
|
|
|
|
|
|
|
return {
|
|
|
|
balance,
|
|
|
|
fromAddress,
|
|
|
|
fromName,
|
|
|
|
toAddress,
|
|
|
|
toName,
|
|
|
|
ethTransactionAmount,
|
|
|
|
ethTransactionFee,
|
|
|
|
ethTransactionTotal,
|
|
|
|
fiatTransactionAmount,
|
|
|
|
fiatTransactionFee,
|
|
|
|
fiatTransactionTotal,
|
|
|
|
hexGasTotal,
|
|
|
|
txData,
|
|
|
|
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-06-23 08:52:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const mapDispatchToProps = dispatch => {
|
|
|
|
return {
|
|
|
|
clearConfirmTransaction: () => dispatch(clearConfirmTransaction()),
|
|
|
|
clearSend: () => dispatch(clearSend()),
|
|
|
|
showTransactionConfirmedModal: ({ onHide }) => {
|
|
|
|
return dispatch(showModal({ name: 'TRANSACTION_CONFIRMED', onHide }))
|
|
|
|
},
|
|
|
|
showCustomizeGasModal: ({ txData, onSubmit, validate }) => {
|
|
|
|
return dispatch(showModal({ name: 'CONFIRM_CUSTOMIZE_GAS', txData, onSubmit, validate }))
|
|
|
|
},
|
|
|
|
updateGasAndCalculate: ({ gasLimit, gasPrice }) => {
|
|
|
|
return dispatch(updateGasAndCalculate({ gasLimit, gasPrice }))
|
|
|
|
},
|
|
|
|
cancelTransaction: ({ id }) => dispatch(cancelTx({ id })),
|
|
|
|
sendTransaction: txData => dispatch(updateAndApproveTx(txData)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) => {
|
|
|
|
const { balance, conversionRate, txData } = stateProps
|
|
|
|
const {
|
|
|
|
showCustomizeGasModal: dispatchShowCustomizeGasModal,
|
|
|
|
updateGasAndCalculate: dispatchUpdateGasAndCalculate,
|
|
|
|
...otherDispatchProps
|
|
|
|
} = dispatchProps
|
|
|
|
|
|
|
|
const validateEditGas = getValidateEditGas({ balance, conversionRate, txData })
|
|
|
|
|
|
|
|
return {
|
|
|
|
...stateProps,
|
|
|
|
...otherDispatchProps,
|
|
|
|
...ownProps,
|
|
|
|
showCustomizeGasModal: () => dispatchShowCustomizeGasModal({
|
|
|
|
txData,
|
|
|
|
onSubmit: txData => dispatchUpdateGasAndCalculate(txData),
|
|
|
|
validate: validateEditGas,
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|