1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-10-24 20:32:02 +02:00
metamask-extension/ui/app/pages/send/send-content/send-gas-row/send-gas-row.container.js

132 lines
3.9 KiB
JavaScript
Raw Normal View History

2018-04-26 18:38:38 +02:00
import { connect } from 'react-redux'
2018-04-11 16:21:54 +02:00
import {
getConversionRate,
getGasTotal,
getGasPrice,
getGasLimit,
getSendAmount,
getSendFromBalance,
getTokenBalance,
getSendMaxModeState,
getGasLoadingError,
gasFeeIsInError,
getGasButtonGroupShown,
getAdvancedInlineGasShown,
getCurrentEthBalance,
getSendToken,
getBasicGasEstimateLoadingStatus,
getRenderableEstimateDataForSmallButtonsFromGWEI,
getDefaultActiveButtonIndex,
} from '../../../../selectors'
import {
isBalanceSufficient,
calcGasTotal,
} from '../../send.utils'
import { calcMaxAmount } from '../send-amount-row/amount-max-button/amount-max-button.utils'
import {
showGasButtonGroup,
updateSendErrors,
} from '../../../../ducks/send/send.duck'
import {
resetCustomData,
setCustomGasPrice,
setCustomGasLimit,
} from '../../../../ducks/gas/gas.duck'
import { showModal, setGasPrice, setGasLimit, setGasTotal, updateSendAmount } from '../../../../store/actions'
2018-04-26 18:38:38 +02:00
import SendGasRow from './send-gas-row.component'
2018-04-11 16:21:54 +02:00
export default connect(mapStateToProps, mapDispatchToProps, mergeProps)(SendGasRow)
2018-04-11 16:21:54 +02:00
function mapStateToProps (state) {
const gasButtonInfo = getRenderableEstimateDataForSmallButtonsFromGWEI(state)
const gasPrice = getGasPrice(state)
2019-02-08 14:50:25 +01:00
const gasLimit = getGasLimit(state)
const activeButtonIndex = getDefaultActiveButtonIndex(gasButtonInfo, gasPrice)
const gasTotal = getGasTotal(state)
const conversionRate = getConversionRate(state)
const balance = getCurrentEthBalance(state)
const insufficientBalance = !isBalanceSufficient({
amount: getSendToken(state) ? '0x0' : getSendAmount(state),
gasTotal,
balance,
conversionRate,
})
2018-04-11 16:21:54 +02:00
return {
balance: getSendFromBalance(state),
gasTotal,
2018-06-29 19:19:40 +02:00
gasFeeError: gasFeeIsInError(state),
gasLoadingError: getGasLoadingError(state),
gasPriceButtonGroupProps: {
buttonDataLoading: getBasicGasEstimateLoadingStatus(state),
defaultActiveButtonIndex: 1,
newActiveButtonIndex: activeButtonIndex > -1 ? activeButtonIndex : null,
gasButtonInfo,
},
gasButtonGroupShown: getGasButtonGroupShown(state),
advancedInlineGasShown: getAdvancedInlineGasShown(state),
2019-02-08 14:50:25 +01:00
gasPrice,
gasLimit,
insufficientBalance,
maxModeOn: getSendMaxModeState(state),
sendToken: getSendToken(state),
tokenBalance: getTokenBalance(state),
2018-04-11 16:21:54 +02:00
}
}
function mapDispatchToProps (dispatch) {
return {
showCustomizeGasModal: () => dispatch(showModal({ name: 'CUSTOMIZE_GAS', hideBasic: true })),
setGasPrice: (newPrice, gasLimit) => {
dispatch(setGasPrice(newPrice))
2019-02-08 14:50:25 +01:00
dispatch(setCustomGasPrice(newPrice))
if (gasLimit) {
dispatch(setGasTotal(calcGasTotal(gasLimit, newPrice)))
}
},
setGasLimit: (newLimit, gasPrice) => {
dispatch(setGasLimit(newLimit))
2019-02-08 14:50:25 +01:00
dispatch(setCustomGasLimit(newLimit))
if (gasPrice) {
dispatch(setGasTotal(calcGasTotal(newLimit, gasPrice)))
}
},
2020-02-15 21:34:12 +01:00
setAmountToMax: (maxAmountDataObject) => {
dispatch(updateSendErrors({ amount: null }))
dispatch(updateSendAmount(calcMaxAmount(maxAmountDataObject)))
},
showGasButtonGroup: () => dispatch(showGasButtonGroup()),
resetCustomData: () => dispatch(resetCustomData()),
2018-04-11 16:21:54 +02:00
}
}
function mergeProps (stateProps, dispatchProps, ownProps) {
const { gasPriceButtonGroupProps } = stateProps
const { gasButtonInfo } = gasPriceButtonGroupProps
const {
setGasPrice: dispatchSetGasPrice,
showGasButtonGroup: dispatchShowGasButtonGroup,
resetCustomData: dispatchResetCustomData,
...otherDispatchProps
} = dispatchProps
return {
...stateProps,
...otherDispatchProps,
...ownProps,
gasPriceButtonGroupProps: {
...gasPriceButtonGroupProps,
handleGasPriceSelection: dispatchSetGasPrice,
},
resetGasButtons: () => {
dispatchResetCustomData()
dispatchSetGasPrice(gasButtonInfo[1].priceInHexWei)
dispatchShowGasButtonGroup()
},
setGasPrice: dispatchSetGasPrice,
}
}