From 5a715c439064b4d935da59dead26002f0f90cc71 Mon Sep 17 00:00:00 2001 From: skylarbarrera Date: Mon, 16 Oct 2023 13:27:31 -0400 Subject: [PATCH 1/3] send: fix max send for native balances --- src/screens/SendSheet.js | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/src/screens/SendSheet.js b/src/screens/SendSheet.js index 2e6b4b2c5e2..02f90ab36bb 100644 --- a/src/screens/SendSheet.js +++ b/src/screens/SendSheet.js @@ -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); @@ -374,6 +375,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) { @@ -395,22 +399,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]); + useMemo(async () => { + if (maxEnabled) { + const newBalanceAmount = await 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(() => { @@ -1009,7 +1026,7 @@ export default function SendSheet(props) { onChangeNativeAmount={onChangeNativeAmount} onResetAssetSelection={onResetAssetSelection} selected={selected} - sendMaxBalance={sendMaxBalance} + sendMaxBalance={() => setMaxEnabled(!maxEnabled)} setLastFocusedInputHandle={setLastFocusedInputHandle} txSpeedRenderer={ Date: Fri, 20 Oct 2023 11:08:20 -0400 Subject: [PATCH 2/3] code review --- src/screens/SendSheet.js | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/src/screens/SendSheet.js b/src/screens/SendSheet.js index 02f90ab36bb..92c7610cb6b 100644 --- a/src/screens/SendSheet.js +++ b/src/screens/SendSheet.js @@ -323,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 @@ -402,10 +391,13 @@ export default function SendSheet(props) { [maxEnabled, maxInputBalance, selected.decimals, selected?.price?.value] ); - useMemo(async () => { - if (maxEnabled) { + useEffect(() => { + const updateMaxBalance = async () => { const newBalanceAmount = await updateMaxInputBalance(selected); sendUpdateAssetAmount(newBalanceAmount); + }; + if (maxEnabled) { + updateMaxBalance(); } // we want to listen to the gas fee and update when it changes // eslint-disable-next-line react-hooks/exhaustive-deps @@ -1026,7 +1018,7 @@ export default function SendSheet(props) { onChangeNativeAmount={onChangeNativeAmount} onResetAssetSelection={onResetAssetSelection} selected={selected} - sendMaxBalance={() => setMaxEnabled(!maxEnabled)} + sendMaxBalance={() => setMaxEnabled(true)} setLastFocusedInputHandle={setLastFocusedInputHandle} txSpeedRenderer={ Date: Fri, 20 Oct 2023 13:00:04 -0400 Subject: [PATCH 3/3] Removing unnecessary await and simplifying updateMaxBalance useEffect in SendSheet --- src/screens/SendSheet.js | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/screens/SendSheet.js b/src/screens/SendSheet.js index 92c7610cb6b..c9060407277 100644 --- a/src/screens/SendSheet.js +++ b/src/screens/SendSheet.js @@ -392,12 +392,9 @@ export default function SendSheet(props) { ); useEffect(() => { - const updateMaxBalance = async () => { - const newBalanceAmount = await updateMaxInputBalance(selected); - sendUpdateAssetAmount(newBalanceAmount); - }; if (maxEnabled) { - updateMaxBalance(); + 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