Skip to content

Commit 51a1de3

Browse files
authored
chore(runway): cherry-pick fix: reset state to prevent stale quotes on token switch (#15626)
- fix: cp-7.47.0 reset state to prevent stale quotes on token switch (#15571) ## **Description** This PR ensures that the bridge feature resets the BridgeController state when switching tokens, preventing stale quotes from persisting after a token switch. ## **Related issues** Fixes: [MMS-2442](https://consensyssoftware.atlassian.net/browse/MMS-2442) ## **Manual testing steps** 1. Initiate a bridge transfer. 2. Switch the source and destination tokens. 3. Observe that the quote state is reset and no stale quotes are shown and dest amount is not glitching. ## **Screenshots/Recordings** ### **Before** https://github.com/user-attachments/assets/ac54f380-b897-4f6f-8b23-addc8282965b ### **After** https://github.com/user-attachments/assets/5f9f098b-be3e-4e78-8650-6d592cbe59bf ## **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. [MMS-2442]: https://consensyssoftware.atlassian.net/browse/MMS-2442?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ [9dc83e4](9dc83e4) [MMS-2442]: https://consensyssoftware.atlassian.net/browse/MMS-2442?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ [MMS-2442]: https://consensyssoftware.atlassian.net/browse/MMS-2442?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ
2 parents 6f23790 + dbf7cdd commit 51a1de3

3 files changed

Lines changed: 37 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
9191

9292
### Fixed
9393

94+
- fix(bridge): reset state to prevent stale quotes on token switch ([#15571](https://github.com/MetaMask/metamask-mobile/pull/15571))
95+
- fix(multi-srp): display multichain accounts in SRP list ([#14724](https://github.com/MetaMask/metamask-mobile/pull/14724))
96+
- fix(confirmations): remove transaction simulations from wallet initiated send flow ([#14994](https://github.com/MetaMask/metamask-mobile/pull/14994))
97+
- fix(bridge): add auto slippage option and improve slippage selection UI ([#15159](https://github.com/MetaMask/metamask-mobile/pull/15159))
98+
- fix(bridge): implement dynamic refresh rate in QuoteExpiredModal ([#15157](https://github.com/MetaMask/metamask-mobile/pull/15157))
9499
- fix(bridge): keyboard not appearing when error banner is displayed ([#14862](https://github.com/MetaMask/metamask-mobile/pull/14862))
95100
- fix(bridge): fix not switching networks when selecting source token ([#14712](https://github.com/MetaMask/metamask-mobile/pull/14712))
96101
- fix: updates a padding style specifically for Android devices ([#14725](https://github.com/MetaMask/metamask-mobile/pull/14725))

app/components/UI/Bridge/hooks/useSwitchTokens/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { useSwitchNetworks } from '../../../../Views/NetworkSelector/useSwitchNe
1010
import { isSolanaChainId } from '@metamask/bridge-controller';
1111
import { CaipChainId, Hex } from '@metamask/utils';
1212
import { selectEvmNetworkConfigurationsByChainId } from '../../../../../selectors/networkController';
13+
import Engine from '../../../../../core/Engine';
1314

1415
export const useSwitchTokens = () => {
1516
const dispatch = useDispatch();
@@ -36,6 +37,11 @@ export const useSwitchTokens = () => {
3637
);
3738

3839
const handleSwitchTokens = async () => {
40+
// Reset BridgeController state to prevent stale quotes
41+
if (Engine.context.BridgeController?.resetState) {
42+
Engine.context.BridgeController.resetState();
43+
}
44+
3945
// Switch tokens
4046
if (sourceToken && destToken) {
4147
dispatch(setSourceToken(destToken));

app/components/UI/Bridge/hooks/useSwitchTokens/useSwitchTokens.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { Hex } from '@metamask/utils';
77
import { SolScope } from '@metamask/keyring-api';
88
// eslint-disable-next-line import/no-namespace
99
import * as bridgeSlice from '../../../../../core/redux/slices/bridge';
10+
import Engine from '../../../../../core/Engine';
1011

1112
// Mock useSwitchNetworks
1213
const mockOnSetRpcTarget = jest.fn();
@@ -38,6 +39,10 @@ describe('useSwitchTokens', () => {
3839

3940
beforeEach(() => {
4041
jest.clearAllMocks();
42+
// Mock BridgeController with resetState
43+
Engine.context.BridgeController = {
44+
resetState: jest.fn(),
45+
} as unknown as typeof Engine.context.BridgeController;
4146
});
4247

4348
it('handles EVM to EVM chain switching', async () => {
@@ -167,4 +172,25 @@ describe('useSwitchTokens', () => {
167172
expect(mockOnNonEvmNetworkChange).not.toHaveBeenCalled();
168173
});
169174
});
175+
176+
it('calls BridgeController.resetState when switching tokens', async () => {
177+
const setSourceTokenSpy = jest.spyOn(bridgeSlice, 'setSourceToken');
178+
const setDestTokenSpy = jest.spyOn(bridgeSlice, 'setDestToken');
179+
const { result } = renderHookWithProvider(() => useSwitchTokens(), {
180+
state: {
181+
...initialState,
182+
bridge: {
183+
...initialState.bridge,
184+
sourceToken: mockEvmToken,
185+
destToken: mockSolanaToken,
186+
},
187+
},
188+
});
189+
190+
await result.current.handleSwitchTokens();
191+
192+
expect(Engine.context.BridgeController.resetState).toHaveBeenCalled();
193+
expect(setSourceTokenSpy).toHaveBeenCalledWith(mockSolanaToken);
194+
expect(setDestTokenSpy).toHaveBeenCalledWith(mockEvmToken);
195+
});
170196
});

0 commit comments

Comments
 (0)