From 39da226cd1233282e59cefefeef3ded502f42eed Mon Sep 17 00:00:00 2001 From: William O'Beirne Date: Mon, 5 Feb 2018 18:14:23 -0500 Subject: [PATCH] Prevent invalid gas price states (#996) * Slider using value instead of raw to prevent errors. Dont show empty gas price as invalid. Clamp slider values to min / max on mount. * Remove gas price from local storage. --- .../TXMetaDataPanel/TXMetaDataPanel.tsx | 6 ++++- .../components/AdvancedGas.tsx | 4 +++- .../TXMetaDataPanel/components/SimpleGas.tsx | 23 +++++++++++++++++- common/store.ts | 24 ------------------- 4 files changed, 30 insertions(+), 27 deletions(-) diff --git a/common/components/TXMetaDataPanel/TXMetaDataPanel.tsx b/common/components/TXMetaDataPanel/TXMetaDataPanel.tsx index fe63ad46..75d01ece 100644 --- a/common/components/TXMetaDataPanel/TXMetaDataPanel.tsx +++ b/common/components/TXMetaDataPanel/TXMetaDataPanel.tsx @@ -96,7 +96,11 @@ class TXMetaDataPanel extends React.Component { options={advancedGasOptions} /> ) : ( - + )} {!offline && diff --git a/common/components/TXMetaDataPanel/components/AdvancedGas.tsx b/common/components/TXMetaDataPanel/components/AdvancedGas.tsx index a20d1e8b..92ea3333 100644 --- a/common/components/TXMetaDataPanel/components/AdvancedGas.tsx +++ b/common/components/TXMetaDataPanel/components/AdvancedGas.tsx @@ -74,7 +74,9 @@ class AdvancedGas extends React.Component {
{ + public componentDidMount() { + this.fixGasPrice(this.props.gasPrice); + } + public render() { const { gasPrice, gasLimitEstimationTimedOut, isWeb3Node } = this.props; @@ -53,7 +59,7 @@ class SimpleGas extends React.Component { onChange={this.handleSlider} min={gasPriceDefaults.gasPriceMinGwei} max={gasPriceDefaults.gasPriceMaxGwei} - value={parseFloat(gasPrice.raw)} + value={this.getGasPriceGwei(gasPrice.value)} tipFormatter={gas => `${gas} Gwei`} />
@@ -78,7 +84,22 @@ class SimpleGas extends React.Component { private handleSlider = (gasGwei: number) => { this.props.inputGasPrice(gasGwei.toString()); }; + + private fixGasPrice(gasPrice: AppState['transaction']['fields']['gasPrice']) { + // If the gas price is above or below our minimum, bring it in line + const gasPriceGwei = this.getGasPriceGwei(gasPrice.value); + if (gasPriceGwei > gasPriceDefaults.gasPriceMaxGwei) { + this.props.setGasPrice(gasPriceDefaults.gasPriceMaxGwei.toString()); + } else if (gasPriceGwei < gasPriceDefaults.gasPriceMinGwei) { + this.props.setGasPrice(gasPriceDefaults.gasPriceMinGwei.toString()); + } + } + + private getGasPriceGwei(gasPriceValue: Wei) { + return parseFloat(fromWei(gasPriceValue, 'gwei')); + } } + export default connect((state: AppState) => ({ gasLimitEstimationTimedOut: getGasLimitEstimationTimedOut(state), isWeb3Node: getIsWeb3Node(state) diff --git a/common/store.ts b/common/store.ts index 49b9cabb..958a1d1a 100644 --- a/common/store.ts +++ b/common/store.ts @@ -5,10 +5,6 @@ import { State as CustomTokenState, INITIAL_STATE as customTokensInitialState } from 'reducers/customTokens'; -import { - INITIAL_STATE as transactionInitialState, - State as TransactionState -} from 'reducers/transaction'; import { State as SwapState, INITIAL_STATE as swapInitialState } from 'reducers/swap'; import { applyMiddleware, createStore } from 'redux'; import { composeWithDevTools } from 'redux-devtools-extension'; @@ -21,7 +17,6 @@ import { getNodeConfigFromId } from 'utils/node'; import { getNetworkConfigFromId } from 'utils/network'; import { dedupeCustomTokens } from 'utils/tokens'; import sagas from './sagas'; -import { gasPricetoBase } from 'libs/units'; const configureStore = () => { const logger = createLogger({ @@ -60,7 +55,6 @@ const configureStore = () => { } : { ...swapInitialState }; - const savedTransactionState = loadStatePropertyOrEmptyObject('transaction'); const savedConfigState = loadStatePropertyOrEmptyObject('config'); // If they have a saved node, make sure we assign that too. The node selected @@ -94,19 +88,6 @@ const configureStore = () => { ...configInitialState, ...savedConfigState }, - transaction: { - ...transactionInitialState, - fields: { - ...transactionInitialState.fields, - gasPrice: - savedTransactionState && savedTransactionState.fields.gasPrice - ? { - raw: savedTransactionState.fields.gasPrice.raw, - value: gasPricetoBase(+savedTransactionState.fields.gasPrice.raw) - } - : transactionInitialState.fields.gasPrice - } - }, customTokens, // ONLY LOAD SWAP STATE FROM LOCAL STORAGE IF STEP WAS 3 swap: swapState @@ -134,11 +115,6 @@ const configureStore = () => { customNodes: state.config.customNodes, customNetworks: state.config.customNetworks }, - transaction: { - fields: { - gasPrice: state.transaction.fields.gasPrice - } - }, swap: { ...state.swap, options: {