Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'Use Max' fixes #1710

Merged
merged 4 commits into from
Jul 24, 2020
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 27 additions & 6 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,12 +695,13 @@ 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;
!useMax && this.setState({ maxFiatInput: undefined });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this need to be here or it can be put in https://github.com/MetaMask/metamask-mobile/pull/1710/files#diff-3fb35285a1d5260613283030e9e9a32cR756 just to not have two state updates on a method

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved!

let inputValueConversion, renderableInputValueConversion, hasExchangeRate, comma;
// Remove spaces from input
inputValue = inputValue && inputValue.replace(/\s+/g, '');
Expand Down