2021-02-04 19:15:23 +01:00
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import BigNumber from 'bignumber.js';
|
|
|
|
import { hideModal, customSwapsGasParamsUpdated } from '../../../store/actions';
|
2020-11-04 17:14:08 +01:00
|
|
|
import {
|
|
|
|
conversionRateSelector as getConversionRate,
|
|
|
|
getCurrentCurrency,
|
|
|
|
getCurrentEthBalance,
|
|
|
|
getDefaultActiveButtonIndex,
|
|
|
|
getRenderableGasButtonData,
|
2020-11-10 18:39:45 +01:00
|
|
|
getUSDConversionRate,
|
2021-03-29 21:30:06 +02:00
|
|
|
getSwapsDefaultToken,
|
2021-02-04 19:15:23 +01:00
|
|
|
} from '../../../selectors';
|
2021-06-08 18:03:59 +02:00
|
|
|
import { getNativeCurrency } from '../../../ducks/metamask/metamask';
|
2020-11-04 17:14:08 +01:00
|
|
|
|
|
|
|
import {
|
|
|
|
getSwapsCustomizationModalPrice,
|
|
|
|
getSwapsCustomizationModalLimit,
|
|
|
|
swapGasEstimateLoadingHasFailed,
|
|
|
|
swapGasPriceEstimateIsLoading,
|
|
|
|
getSwapGasPriceEstimateData,
|
|
|
|
swapCustomGasModalPriceEdited,
|
|
|
|
swapCustomGasModalLimitEdited,
|
|
|
|
shouldShowCustomPriceTooLowWarning,
|
2020-11-12 20:12:04 +01:00
|
|
|
swapCustomGasModalClosed,
|
2021-02-04 19:15:23 +01:00
|
|
|
} from '../../../ducks/swaps/swaps';
|
2020-11-04 17:14:08 +01:00
|
|
|
import {
|
|
|
|
addHexes,
|
|
|
|
getValueFromWeiHex,
|
|
|
|
sumHexWEIsToRenderableFiat,
|
2021-02-04 19:15:23 +01:00
|
|
|
} 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';
|
2020-11-04 17:14:08 +01:00
|
|
|
|
|
|
|
const mapStateToProps = (state) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const currentCurrency = getCurrentCurrency(state);
|
|
|
|
const conversionRate = getConversionRate(state);
|
2021-03-29 21:30:06 +02:00
|
|
|
const nativeCurrencySymbol = getNativeCurrency(state);
|
|
|
|
const { symbol: swapsDefaultCurrencySymbol } = getSwapsDefaultToken(state);
|
|
|
|
const usedCurrencySymbol = nativeCurrencySymbol || swapsDefaultCurrencySymbol;
|
2020-11-04 17:14:08 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const { modalState: { props: modalProps } = {} } = state.appState.modal || {};
|
2020-11-04 17:14:08 +01:00
|
|
|
const {
|
|
|
|
value,
|
|
|
|
customGasLimitMessage = '',
|
|
|
|
customTotalSupplement = '',
|
|
|
|
extraInfoRow = null,
|
|
|
|
initialGasPrice,
|
|
|
|
initialGasLimit,
|
2020-11-10 21:20:39 +01:00
|
|
|
minimumGasLimit,
|
2021-02-04 19:15:23 +01:00
|
|
|
} = modalProps;
|
|
|
|
const buttonDataLoading = swapGasPriceEstimateIsLoading(state);
|
2020-11-04 17:14:08 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const swapsCustomizationModalPrice = getSwapsCustomizationModalPrice(state);
|
|
|
|
const swapsCustomizationModalLimit = getSwapsCustomizationModalLimit(state);
|
2020-11-04 17:14:08 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const customGasPrice = swapsCustomizationModalPrice || initialGasPrice;
|
|
|
|
const customGasLimit = swapsCustomizationModalLimit || initialGasLimit;
|
2020-11-04 17:14:08 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const customGasTotal = calcGasTotal(customGasLimit, customGasPrice);
|
2020-11-04 17:14:08 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const swapsGasPriceEstimates = getSwapGasPriceEstimateData(state);
|
2020-11-04 17:14:08 +01:00
|
|
|
|
|
|
|
const { averageEstimateData, fastEstimateData } = getRenderableGasButtonData(
|
|
|
|
swapsGasPriceEstimates,
|
|
|
|
customGasLimit,
|
|
|
|
true,
|
|
|
|
conversionRate,
|
|
|
|
currentCurrency,
|
2021-03-29 21:30:06 +02:00
|
|
|
usedCurrencySymbol,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
const gasButtonInfo = [averageEstimateData, fastEstimateData];
|
2020-11-04 17:14:08 +01:00
|
|
|
|
|
|
|
const newTotalFiat = sumHexWEIsToRenderableFiat(
|
|
|
|
[value, customGasTotal, customTotalSupplement],
|
|
|
|
currentCurrency,
|
|
|
|
conversionRate,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-11-04 17:14:08 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const balance = getCurrentEthBalance(state);
|
2020-11-04 17:14:08 +01:00
|
|
|
|
2021-03-29 21:30:06 +02:00
|
|
|
const newTotalEth = sumHexWEIsToRenderableEth(
|
|
|
|
[value, customGasTotal, customTotalSupplement],
|
|
|
|
usedCurrencySymbol,
|
|
|
|
);
|
2020-11-04 17:14:08 +01:00
|
|
|
|
2021-03-29 21:30:06 +02:00
|
|
|
const sendAmount = sumHexWEIsToRenderableEth(
|
|
|
|
[value, '0x0'],
|
|
|
|
usedCurrencySymbol,
|
|
|
|
);
|
2020-11-04 17:14:08 +01:00
|
|
|
|
|
|
|
const insufficientBalance = !isBalanceSufficient({
|
|
|
|
amount: value,
|
|
|
|
gasTotal: customGasTotal,
|
|
|
|
balance,
|
|
|
|
conversionRate,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-11-04 17:14:08 +01:00
|
|
|
|
2020-11-12 20:12:04 +01:00
|
|
|
const customGasLimitTooLow = new BigNumber(customGasLimit, 16).lt(
|
|
|
|
minimumGasLimit,
|
|
|
|
10,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-11-12 20:12:04 +01:00
|
|
|
|
2020-11-04 17:14:08 +01:00
|
|
|
return {
|
|
|
|
customGasPrice,
|
|
|
|
customGasLimit,
|
|
|
|
showCustomPriceTooLowWarning: shouldShowCustomPriceTooLowWarning(state),
|
|
|
|
gasPriceButtonGroupProps: {
|
|
|
|
buttonDataLoading,
|
|
|
|
defaultActiveButtonIndex: getDefaultActiveButtonIndex(
|
|
|
|
gasButtonInfo,
|
|
|
|
customGasPrice,
|
|
|
|
),
|
|
|
|
gasButtonInfo,
|
|
|
|
},
|
|
|
|
infoRowProps: {
|
|
|
|
originalTotalFiat: sumHexWEIsToRenderableFiat(
|
|
|
|
[value, customGasTotal, customTotalSupplement],
|
|
|
|
currentCurrency,
|
|
|
|
conversionRate,
|
|
|
|
),
|
2021-03-29 21:30:06 +02:00
|
|
|
originalTotalEth: sumHexWEIsToRenderableEth(
|
|
|
|
[value, customGasTotal, customTotalSupplement],
|
|
|
|
usedCurrencySymbol,
|
|
|
|
),
|
2020-11-04 17:14:08 +01:00
|
|
|
newTotalFiat,
|
|
|
|
newTotalEth,
|
2021-03-29 21:30:06 +02:00
|
|
|
transactionFee: sumHexWEIsToRenderableEth(
|
|
|
|
['0x0', customGasTotal],
|
|
|
|
usedCurrencySymbol,
|
|
|
|
),
|
2020-11-04 17:14:08 +01:00
|
|
|
sendAmount,
|
|
|
|
extraInfoRow,
|
|
|
|
},
|
|
|
|
gasEstimateLoadingHasFailed: swapGasEstimateLoadingHasFailed(state),
|
|
|
|
insufficientBalance,
|
|
|
|
customGasLimitMessage,
|
|
|
|
customTotalSupplement,
|
2020-11-10 18:39:45 +01:00
|
|
|
usdConversionRate: getUSDConversionRate(state),
|
2020-11-12 20:12:04 +01:00
|
|
|
disableSave: insufficientBalance || customGasLimitTooLow,
|
2020-11-10 21:20:39 +01:00
|
|
|
minimumGasLimit,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
|
|
|
};
|
2020-11-04 17:14:08 +01:00
|
|
|
|
|
|
|
const mapDispatchToProps = (dispatch) => {
|
|
|
|
return {
|
|
|
|
cancelAndClose: () => {
|
2021-02-04 19:15:23 +01:00
|
|
|
dispatch(swapCustomGasModalClosed());
|
|
|
|
dispatch(hideModal());
|
2020-11-04 17:14:08 +01:00
|
|
|
},
|
2020-11-12 20:12:04 +01:00
|
|
|
onSubmit: async (gasLimit, gasPrice) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
await dispatch(customSwapsGasParamsUpdated(gasLimit, gasPrice));
|
|
|
|
dispatch(swapCustomGasModalClosed());
|
|
|
|
dispatch(hideModal());
|
2020-11-04 17:14:08 +01:00
|
|
|
},
|
|
|
|
setSwapsCustomizationModalPrice: (newPrice) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
dispatch(swapCustomGasModalPriceEdited(newPrice));
|
2020-11-04 17:14:08 +01:00
|
|
|
},
|
|
|
|
setSwapsCustomizationModalLimit: (newLimit) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
dispatch(swapCustomGasModalLimitEdited(newLimit));
|
2020-11-04 17:14:08 +01:00
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
|
|
|
};
|
2020-11-04 17:14:08 +01:00
|
|
|
|
|
|
|
export default connect(
|
|
|
|
mapStateToProps,
|
|
|
|
mapDispatchToProps,
|
2021-02-04 19:15:23 +01:00
|
|
|
)(SwapsGasCustomizationModalComponent);
|
2020-11-04 17:14:08 +01:00
|
|
|
|
2021-03-29 21:30:06 +02:00
|
|
|
function sumHexWEIsToRenderableEth(hexWEIs, currencySymbol = 'ETH') {
|
2021-02-04 19:15:23 +01:00
|
|
|
const hexWEIsSum = hexWEIs.filter(Boolean).reduce(addHexes);
|
2020-11-04 17:14:08 +01:00
|
|
|
return formatETHFee(
|
|
|
|
getValueFromWeiHex({
|
|
|
|
value: hexWEIsSum,
|
2021-03-29 21:30:06 +02:00
|
|
|
fromCurrency: currencySymbol,
|
|
|
|
toCurrency: currencySymbol,
|
2020-11-04 17:14:08 +01:00
|
|
|
numberOfDecimals: 6,
|
|
|
|
}),
|
2021-03-29 21:30:06 +02:00
|
|
|
currencySymbol,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-11-04 17:14:08 +01:00
|
|
|
}
|