Skip to content

Commit

Permalink
'Use Max' fixes (#1710)
Browse files Browse the repository at this point in the history
* SendFlow/Amount: fix amount max not clearing entire account and causing insufficient funds error

* SendFlow: fix again

* Amount: move maxFiatInput to existing setstate
  • Loading branch information
EtDu authored Jul 24, 2020
1 parent b4aa374 commit d01d6cd
Showing 1 changed file with 28 additions and 7 deletions.
35 changes: 28 additions & 7 deletions app/components/Views/SendFlow/Amount/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import { getTicker, generateTransferData, getEther } from '../../../../util/tran
import { util } from '@metamask/controllers';
import FadeIn from 'react-native-fade-in-image';
import ErrorMessage from '../ErrorMessage';
import { fetchBasicGasEstimates, apiEstimateModifiedToWEI } from '../../../../util/custom-gas';
import { fetchBasicGasEstimates, convertApiValueToGWEI } from '../../../../util/custom-gas';
import Engine from '../../../../core/Engine';
import CollectibleImage from '../../../UI/CollectibleImage';
import collectiblesTransferInformation from '../../../../util/collectibles-transfer';
Expand Down Expand Up @@ -445,8 +445,27 @@ class Amount extends PureComponent {
providerType,
onConfirm
} = this.props;
const { inputValue, inputValueConversion, internalPrimaryCurrencyIsCrypto, hasExchangeRate } = this.state;
const value = internalPrimaryCurrencyIsCrypto || !hasExchangeRate ? inputValue : inputValueConversion;
const {
inputValue,
inputValueConversion,
internalPrimaryCurrencyIsCrypto,
hasExchangeRate,
maxFiatInput
} = this.state;

let value;
if (internalPrimaryCurrencyIsCrypto || !hasExchangeRate) {
value = inputValue;
} else {
value = inputValueConversion;
if (maxFiatInput) {
value = `${renderFromWei(
fiatNumberToWei(handleWeiNumber(maxFiatInput), this.props.conversionRate),
18
)}`;
}
}

if (!selectedAsset.tokenId && this.validateAmount(value)) {
return;
} else if (selectedAsset.tokenId) {
Expand Down Expand Up @@ -636,7 +655,7 @@ class Amount extends PureComponent {
basicGasEstimates = { average: 20 };
}
const gas = hexToBN(estimation.gas);
const gasPrice = apiEstimateModifiedToWEI(basicGasEstimates.average);
const gasPrice = toWei(convertApiValueToGWEI(basicGasEstimates.average), 'gwei');
return gas.mul(gasPrice);
};

Expand All @@ -662,6 +681,7 @@ class Amount extends PureComponent {
input = fromWei(maxValue);
} else {
input = `${weiToFiatNumber(maxValue, conversionRate)}`;
this.setState({ maxFiatInput: `${weiToFiatNumber(maxValue, conversionRate, 12)}` });
}
} else {
const exchangeRate = contractExchangeRates[selectedAsset.address];
Expand All @@ -675,10 +695,10 @@ class Amount extends PureComponent {
)}`;
}
}
this.onInputChange(input);
this.onInputChange(input, undefined, true);
};

onInputChange = (inputValue, selectedAsset) => {
onInputChange = (inputValue, selectedAsset, useMax) => {
const { contractExchangeRates, conversionRate, currentCurrency, ticker } = this.props;
const { internalPrimaryCurrencyIsCrypto } = this.state;
let inputValueConversion, renderableInputValueConversion, hasExchangeRate, comma;
Expand Down Expand Up @@ -737,7 +757,8 @@ class Amount extends PureComponent {
inputValueConversion,
renderableInputValueConversion,
amountError: undefined,
hasExchangeRate
hasExchangeRate,
maxFiatInput: !useMax && undefined
});
};

Expand Down

0 comments on commit d01d6cd

Please sign in to comment.