Skip to content

Commit 95b96ca

Browse files
fix: cp-7.60.0 automatic highest balance token in metamask pay (MetaMask#23152)
## **Description** Automatically select the highest balance token on any chain in Perps and Predict deposits. ## **Changelog** CHANGELOG entry: null ## **Related issues** Fixes: MetaMask#23131 ## **Manual testing steps** ## **Screenshots/Recordings** ### **Before** ### **After** ## **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] > Simplifies automatic pay token selection to the first available token with balance, falling back to target token when none or on hardware wallets, and updates tests accordingly. > > - **Logic (useAutomaticTransactionPayToken.ts)** > - Replace multi-step selection (target match → highest balance same chain → highest balance alt chain) with simple rule: pick `tokens[0]` if any; otherwise fall back to required target token; always fall back for hardware wallets. > - **Tests (useAutomaticTransactionPayToken.test.ts)** > - Update expectations to select the first available token. > - Remove scenarios around highest-balance and cross-chain selection. > - Keep fallbacks: target token when no tokens, hardware wallets, and disabled state does nothing. > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit 6907490. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY -->
1 parent b58ddba commit 95b96ca

2 files changed

Lines changed: 8 additions & 80 deletions

File tree

app/components/Views/confirmations/hooks/pay/useAutomaticTransactionPayToken.test.ts

Lines changed: 5 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -84,76 +84,32 @@ describe('useAutomaticTransactionPayToken', () => {
8484
isHardwareAccountMock.mockReturnValue(false);
8585
});
8686

87-
it('selects target token if has balance', () => {
87+
it('selects first token', () => {
8888
useTransactionPayAvailableTokensMock.mockReturnValue([
8989
{
9090
address: TOKEN_ADDRESS_2_MOCK,
91-
chainId: CHAIN_ID_1_MOCK,
92-
},
93-
{
94-
address: TOKEN_ADDRESS_3_MOCK,
9591
chainId: CHAIN_ID_2_MOCK,
9692
},
97-
{
98-
address: TOKEN_ADDRESS_1_MOCK,
99-
chainId: CHAIN_ID_1_MOCK,
100-
},
101-
] as AssetType[]);
102-
103-
runHook();
104-
105-
expect(setPayTokenMock).toHaveBeenCalledWith({
106-
address: TOKEN_ADDRESS_1_MOCK,
107-
chainId: CHAIN_ID_1_MOCK,
108-
});
109-
});
110-
111-
it('selects token with highest balance on same chain if insufficient balance on target token', () => {
112-
useTransactionPayAvailableTokensMock.mockReturnValue([
11393
{
11494
address: TOKEN_ADDRESS_3_MOCK,
11595
chainId: CHAIN_ID_2_MOCK,
11696
},
11797
{
118-
address: TOKEN_ADDRESS_2_MOCK,
119-
chainId: CHAIN_ID_2_MOCK,
120-
},
121-
{
122-
address: TOKEN_ADDRESS_3_MOCK,
98+
address: TOKEN_ADDRESS_1_MOCK,
12399
chainId: CHAIN_ID_1_MOCK,
124100
},
125101
] as AssetType[]);
126102

127103
runHook();
128104

129105
expect(setPayTokenMock).toHaveBeenCalledWith({
130-
address: TOKEN_ADDRESS_3_MOCK,
131-
chainId: CHAIN_ID_1_MOCK,
132-
});
133-
});
134-
135-
it('selects token with highest balance on alternate chain if insufficient balance on same chain', () => {
136-
useTransactionPayAvailableTokensMock.mockReturnValue([
137-
{
138-
address: TOKEN_ADDRESS_3_MOCK,
139-
chainId: CHAIN_ID_2_MOCK,
140-
},
141-
{
142-
address: TOKEN_ADDRESS_2_MOCK,
143-
chainId: CHAIN_ID_2_MOCK,
144-
},
145-
] as AssetType[]);
146-
147-
runHook();
148-
149-
expect(setPayTokenMock).toHaveBeenCalledWith({
150-
address: TOKEN_ADDRESS_3_MOCK,
106+
address: TOKEN_ADDRESS_2_MOCK,
151107
chainId: CHAIN_ID_2_MOCK,
152108
});
153109
});
154110

155-
it('selects target token if insufficient balance on all chains', () => {
156-
useTransactionPayAvailableTokensMock.mockReturnValue([]);
111+
it('selects target token if no tokens with balance', () => {
112+
useTransactionPayAvailableTokensMock.mockReturnValue([] as AssetType[]);
157113

158114
runHook();
159115

app/components/Views/confirmations/hooks/pay/useAutomaticTransactionPayToken.ts

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -97,38 +97,10 @@ function getBestToken({
9797
return targetTokenFallback;
9898
}
9999

100-
const requiredToken = tokens.find(
101-
(t) =>
102-
t.address.toLowerCase() === targetToken?.address.toLowerCase() &&
103-
t.chainId === targetToken?.chainId,
104-
);
105-
106-
if (requiredToken) {
107-
return {
108-
address: requiredToken.address as Hex,
109-
chainId: requiredToken.chainId as Hex,
110-
};
111-
}
112-
113-
const sameChainHighestBalanceToken = tokens.find(
114-
(t) => t.chainId === targetToken?.chainId,
115-
);
116-
117-
if (sameChainHighestBalanceToken) {
118-
return {
119-
address: sameChainHighestBalanceToken.address as Hex,
120-
chainId: sameChainHighestBalanceToken.chainId as Hex,
121-
};
122-
}
123-
124-
const alternateChainHighestBalanceToken = tokens.find(
125-
(t) => t.chainId !== targetToken?.chainId,
126-
);
127-
128-
if (alternateChainHighestBalanceToken) {
100+
if (tokens?.length) {
129101
return {
130-
address: alternateChainHighestBalanceToken.address as Hex,
131-
chainId: alternateChainHighestBalanceToken.chainId as Hex,
102+
address: tokens[0].address as Hex,
103+
chainId: tokens[0].chainId as Hex,
132104
};
133105
}
134106

0 commit comments

Comments
 (0)