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

send: fix max send for native balances #5126

Merged
merged 3 commits into from
Oct 20, 2023
Merged
Changes from all 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
42 changes: 24 additions & 18 deletions src/screens/SendSheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ export default function SendSheet(props) {
const [recipient, setRecipient] = useState('');
const [nickname, setNickname] = useState('');
const [selected, setSelected] = useState({});
const [maxEnabled, setMaxEnabled] = useState(false);
const { maxInputBalance, updateMaxInputBalance } = useMaxInputBalance();

const [debouncedInput] = useDebounce(currentInput, 500);
Expand Down Expand Up @@ -322,17 +323,6 @@ export default function SendSheet(props) {
};
}, [stopPollingGasFees]);

// Recalculate balance when gas price changes
useEffect(() => {
if (
selected?.isNativeAsset &&
(prevSelectedGasFee?.gasFee?.estimatedFee?.value?.amount ?? 0) !==
(selectedGasFee?.gasFee?.estimatedFee?.value?.amount ?? 0)
) {
updateMaxInputBalance(selected);
}
}, [prevSelectedGasFee, selected, selectedGasFee, updateMaxInputBalance]);

useEffect(() => {
const updateNetworkAndProvider = async () => {
const assetNetwork = isNft
Expand Down Expand Up @@ -374,6 +364,9 @@ export default function SendSheet(props) {
const onChangeNativeAmount = useCallback(
newNativeAmount => {
if (!isString(newNativeAmount)) return;
if (maxEnabled) {
setMaxEnabled(false);
}
const _nativeAmount = newNativeAmount.replace(/[^0-9.]/g, '');
let _assetAmount = '';
if (_nativeAmount.length) {
Expand All @@ -395,22 +388,35 @@ export default function SendSheet(props) {
});
analytics.track('Changed native currency input in Send flow');
},
[maxInputBalance, selected.decimals, selected?.price?.value]
[maxEnabled, maxInputBalance, selected.decimals, selected?.price?.value]
);

const sendMaxBalance = useCallback(async () => {
const newBalanceAmount = await updateMaxInputBalance(selected);
sendUpdateAssetAmount(newBalanceAmount);
}, [selected, sendUpdateAssetAmount, updateMaxInputBalance]);
useEffect(() => {
if (maxEnabled) {
const newBalanceAmount = updateMaxInputBalance(selected);
sendUpdateAssetAmount(newBalanceAmount);
}
// we want to listen to the gas fee and update when it changes
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [
selected,
sendUpdateAssetAmount,
updateMaxInputBalance,
selectedGasFee,
maxEnabled,
]);

const onChangeAssetAmount = useCallback(
newAssetAmount => {
if (isString(newAssetAmount)) {
if (maxEnabled) {
setMaxEnabled(false);
}
sendUpdateAssetAmount(newAssetAmount);
analytics.track('Changed token input in Send flow');
}
},
[sendUpdateAssetAmount]
[maxEnabled, sendUpdateAssetAmount]
);

useEffect(() => {
Expand Down Expand Up @@ -1009,7 +1015,7 @@ export default function SendSheet(props) {
onChangeNativeAmount={onChangeNativeAmount}
onResetAssetSelection={onResetAssetSelection}
selected={selected}
sendMaxBalance={sendMaxBalance}
sendMaxBalance={() => setMaxEnabled(true)}
setLastFocusedInputHandle={setLastFocusedInputHandle}
txSpeedRenderer={
<GasSpeedButton
Expand Down