Skip to content

Commit f124f8c

Browse files
authored
fix: Implement nonEVM zero balance check to send flow before onAmountInput (#23037)
<!-- Please submit this PR as a draft initially. Do not mark it as "Ready for review" until the template has been completely filled out, and PR status checks have passed at least once. --> ## **Description** <!-- Write a short description of the changes included in this pull request, also include relevant motivation and context. Have in mind the following questions: 1. What is the reason for the change? 2. What is the improvement/solution? --> ## **Changelog** This PR adds zero balance check before `onAmountInput` RPC call to nonEVM send flow amount validations. <!-- If this PR is not End-User-Facing and should not show up in the CHANGELOG, you can choose to either: 1. Write `CHANGELOG entry: null` 2. Label with `no-changelog` If this PR is End-User-Facing, please write a short User-Facing description in the past tense like: `CHANGELOG entry: Added a new tab for users to see their NFTs` `CHANGELOG entry: Fixed a bug that was causing some NFTs to flicker` (This helps the Release Engineer do their job more quickly and accurately) --> CHANGELOG entry: null ## **Related issues** Fixes: #22812 ## **Manual testing steps** 1. Go to send flow 2. Don't have any Tron / SOL / BTC 3. Try sending it but see "insufficient balance" error ## **Screenshots/Recordings** <!-- If applicable, add screenshots and/or recordings to visualize the before and after of your change. --> ### **Before** <!-- [screenshots/recordings] --> ### **After** https://github.com/user-attachments/assets/ab16a8fe-7da7-4da5-9ee5-684bb0a62e50 ## **Pre-merge author checklist** - [X] I’ve followed [MetaMask Contributor Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Mobile Coding Standards](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/CODING_GUIDELINES.md). - [X] I've completed the PR template to the best of my ability - [X] I’ve included tests if applicable - [X] I’ve documented my code using [JSDoc](https://jsdoc.app/) format if applicable - [X] I’ve applied the right labels on the PR (see [labeling guidelines](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/LABELING_GUIDELINES.md)). Not required for external contributors. ## **Pre-merge reviewer checklist** - [ ] I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed). - [ ] I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots. <!-- CURSOR_SUMMARY --> --- > [!NOTE] > Adds a zero-balance pre-check for non-EVM assets in amount validation and updates tests to cover it. > > - **Validation (useAmountValidation)**: > - Add non-EVM pre-check `rawBalanceBN.isZero()` to return `"send.insufficient_funds"` before `onAmountInput` snap validation in `useAmountValidation.ts`. > - Update hook dependencies to include `rawBalanceBN`. > - **Tests**: > - Extend `useAmountValidation.test.ts` with SOLANA mocks and a test asserting error when non-EVM asset balance is zero. > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit bf695af. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY -->
1 parent 3697982 commit f124f8c

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

app/components/Views/confirmations/hooks/send/useAmountValidation.test.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ import { waitFor } from '@testing-library/react-native';
22
import BN from 'bnjs4';
33

44
import { renderHookWithProvider } from '../../../../../util/test/renderWithProvider';
5-
import { EVM_NATIVE_ASSET, evmSendStateMock } from '../../__mocks__/send.mock';
5+
import {
6+
EVM_NATIVE_ASSET,
7+
evmSendStateMock,
8+
SOLANA_ASSET,
9+
solanaSendStateMock,
10+
} from '../../__mocks__/send.mock';
611
import {
712
useAmountValidation,
813
validateERC1155Balance,
@@ -320,4 +325,23 @@ describe('useAmountValidation', () => {
320325
const error = await result.current.validateNonEvmAmountAsync();
321326
expect(error).toEqual(undefined);
322327
});
328+
329+
it('return error when non-EVM asset has zero balance', async () => {
330+
jest.spyOn(SendContext, 'useSendContext').mockReturnValue({
331+
asset: {
332+
...SOLANA_ASSET,
333+
rawBalance: '0x0',
334+
},
335+
from: MOCK_ADDRESS_1,
336+
value: '1',
337+
} as unknown as SendContext.SendContextType);
338+
339+
const { result } = renderHookWithProvider(() => useAmountValidation(), {
340+
state: solanaSendStateMock,
341+
});
342+
343+
await waitFor(() =>
344+
expect(result.current.amountError).toEqual('Insufficient funds'),
345+
);
346+
});
323347
});

app/components/Views/confirmations/hooks/send/useAmountValidation.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ export const useAmountValidation = () => {
3434
return undefined;
3535
}
3636

37+
if (rawBalanceBN.isZero()) {
38+
return strings('send.insufficient_funds');
39+
}
40+
3741
try {
3842
const result = (await validateAmountWithSnap(
3943
value || '0',
@@ -49,7 +53,7 @@ export const useAmountValidation = () => {
4953
} catch (error) {
5054
return strings('send.invalid_value');
5155
}
52-
}, [value, validateAmountWithSnap, isNonEvmSendType]);
56+
}, [value, validateAmountWithSnap, isNonEvmSendType, rawBalanceBN]);
5357

5458
const validateAmountAsync = useCallback(async () => {
5559
if (!value) {

0 commit comments

Comments
 (0)