1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-24 19:10:22 +01:00
metamask-extension/ui/app/pages/swaps/swaps-gas-customization-modal/swaps-gas-customization-modal.container.js
Dan J Miller 9849a9cf82
Fixes related to swaps custom gas limit (#9858)
* Fixes related to swaps custom gas limit

* Update ui/app/pages/swaps/view-quote/view-quote.js

Co-authored-by: Erik Marks <25517051+rekmarks@users.noreply.github.com>

* Move selectQuotePopover onSubmit dispatches to a single action creator

* Correct type of minimumGasLimit in view-quote.js

Co-authored-by: Erik Marks <25517051+rekmarks@users.noreply.github.com>
2020-11-12 15:42:04 -03:30

171 lines
4.8 KiB
JavaScript

import { connect } from 'react-redux'
import BigNumber from 'bignumber.js'
import { hideModal, customSwapsGasParamsUpdated } from '../../../store/actions'
import {
conversionRateSelector as getConversionRate,
getCurrentCurrency,
getCurrentEthBalance,
getDefaultActiveButtonIndex,
getRenderableGasButtonData,
getUSDConversionRate,
} from '../../../selectors'
import {
getSwapsCustomizationModalPrice,
getSwapsCustomizationModalLimit,
swapGasEstimateLoadingHasFailed,
swapGasPriceEstimateIsLoading,
getSwapGasPriceEstimateData,
swapCustomGasModalPriceEdited,
swapCustomGasModalLimitEdited,
shouldShowCustomPriceTooLowWarning,
swapCustomGasModalClosed,
} from '../../../ducks/swaps/swaps'
import {
addHexes,
getValueFromWeiHex,
sumHexWEIsToRenderableFiat,
} from '../../../helpers/utils/conversions.util'
import { formatETHFee } from '../../../helpers/utils/formatters'
import { calcGasTotal, isBalanceSufficient } from '../../send/send.utils'
import SwapsGasCustomizationModalComponent from './swaps-gas-customization-modal.component'
const mapStateToProps = (state) => {
const currentCurrency = getCurrentCurrency(state)
const conversionRate = getConversionRate(state)
const { modalState: { props: modalProps } = {} } = state.appState.modal || {}
const {
value,
customGasLimitMessage = '',
customTotalSupplement = '',
extraInfoRow = null,
initialGasPrice,
initialGasLimit,
minimumGasLimit,
} = modalProps
const buttonDataLoading = swapGasPriceEstimateIsLoading(state)
const swapsCustomizationModalPrice = getSwapsCustomizationModalPrice(state)
const swapsCustomizationModalLimit = getSwapsCustomizationModalLimit(state)
const customGasPrice = swapsCustomizationModalPrice || initialGasPrice
const customGasLimit = swapsCustomizationModalLimit || initialGasLimit
const customGasTotal = calcGasTotal(customGasLimit, customGasPrice)
const swapsGasPriceEstimates = getSwapGasPriceEstimateData(state)
const { averageEstimateData, fastEstimateData } = getRenderableGasButtonData(
swapsGasPriceEstimates,
customGasLimit,
true,
conversionRate,
currentCurrency,
)
const gasButtonInfo = [averageEstimateData, fastEstimateData]
const newTotalFiat = sumHexWEIsToRenderableFiat(
[value, customGasTotal, customTotalSupplement],
currentCurrency,
conversionRate,
)
const balance = getCurrentEthBalance(state)
const newTotalEth = sumHexWEIsToRenderableEth([
value,
customGasTotal,
customTotalSupplement,
])
const sendAmount = sumHexWEIsToRenderableEth([value, '0x0'])
const insufficientBalance = !isBalanceSufficient({
amount: value,
gasTotal: customGasTotal,
balance,
conversionRate,
})
const customGasLimitTooLow = new BigNumber(customGasLimit, 16).lt(
minimumGasLimit,
10,
)
return {
customGasPrice,
customGasLimit,
showCustomPriceTooLowWarning: shouldShowCustomPriceTooLowWarning(state),
gasPriceButtonGroupProps: {
buttonDataLoading,
defaultActiveButtonIndex: getDefaultActiveButtonIndex(
gasButtonInfo,
customGasPrice,
),
gasButtonInfo,
},
infoRowProps: {
originalTotalFiat: sumHexWEIsToRenderableFiat(
[value, customGasTotal, customTotalSupplement],
currentCurrency,
conversionRate,
),
originalTotalEth: sumHexWEIsToRenderableEth([
value,
customGasTotal,
customTotalSupplement,
]),
newTotalFiat,
newTotalEth,
transactionFee: sumHexWEIsToRenderableEth(['0x0', customGasTotal]),
sendAmount,
extraInfoRow,
},
gasEstimateLoadingHasFailed: swapGasEstimateLoadingHasFailed(state),
insufficientBalance,
customGasLimitMessage,
customTotalSupplement,
usdConversionRate: getUSDConversionRate(state),
disableSave: insufficientBalance || customGasLimitTooLow,
minimumGasLimit,
}
}
const mapDispatchToProps = (dispatch) => {
return {
cancelAndClose: () => {
dispatch(swapCustomGasModalClosed())
dispatch(hideModal())
},
onSubmit: async (gasLimit, gasPrice) => {
await dispatch(customSwapsGasParamsUpdated(gasLimit, gasPrice))
dispatch(swapCustomGasModalClosed())
dispatch(hideModal())
},
setSwapsCustomizationModalPrice: (newPrice) => {
dispatch(swapCustomGasModalPriceEdited(newPrice))
},
setSwapsCustomizationModalLimit: (newLimit) => {
dispatch(swapCustomGasModalLimitEdited(newLimit))
},
}
}
export default connect(
mapStateToProps,
mapDispatchToProps,
)(SwapsGasCustomizationModalComponent)
function sumHexWEIsToRenderableEth(hexWEIs) {
const hexWEIsSum = hexWEIs.filter((n) => n).reduce(addHexes)
return formatETHFee(
getValueFromWeiHex({
value: hexWEIsSum,
toCurrency: 'ETH',
numberOfDecimals: 6,
}),
)
}