Skip to content

Commit b77ee50

Browse files
test(e2e): cover send and swap max amount parity (#47386)
<!-- CURSOR_AGENT_PR_BODY_BEGIN --> ## **Description** Send Max and the confirmation amount can drift apart when gas is subtracted on one screen and not the other. Swap Max has a separate rule: Ethereum uses the full native balance, while Monad intentionally reserves 10 MON. This adds Mockttp-only coverage for Ethereum mainnet and Monad. Both specs share the same account, balance, and gas mocks, with no local chain. The send spec clicks Max, then checks that the confirmation amount is lower by the mocked gas fee. The swap spec clicks Max and checks the source amount without submitting a quote. ## **Changelog** CHANGELOG entry: null ## **Related issues** [WPN-1800](https://consensyssoftware.atlassian.net/browse/WPN-1800) ## **Manual testing steps** 1. Unlock the extension with Ethereum mainnet selected and a native ETH balance. 2. Start a send, select ETH, enter a recipient, and click Max. 3. Confirm the send amount equals the full ETH balance. 4. Continue to the confirmation screen and confirm the displayed ETH amount is lower than that send amount by the estimated network fee. 5. Go back home, open Swap, and select ETH as the token you are swapping from. 6. Click Max and confirm the amount equals the full ETH balance. 7. Switch the selected network to Monad. 8. Repeat the send flow with MON and confirm the confirmation amount is lower than Send Max by the estimated network fee. 9. Open Swap on Monad, select MON, click Max, and confirm the amount is the MON balance minus 10 MON. <!-- ## **Screenshots/Recordings** ### **Before** ### **After** --> ## **Pre-merge author checklist** - [x] I've followed [MetaMask Contributor Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Extension Coding Standards](https://github.com/MetaMask/metamask-extension/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 - [ ] I’ve applied the right labels on the PR (see [labeling guidelines](https://github.com/MetaMask/metamask-extension/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_AGENT_PR_BODY_END --> <div><a href="https://cursor.com/agents/bc-291b14cd-4dc9-4d1d-8980-6b0612c6ab31?cursor_ref=pr_footer&cursor_cta=open_in_web"><picture><source media="(prefers-color-scheme: dark)" srcset="https://cursor.com/assets/images/open-in-web-dark.png"><source media="(prefers-color-scheme: light)" srcset="https://cursor.com/assets/images/open-in-web-light.png"><img alt="Open in Web" width="114" height="28" src="https://cursor.com/assets/images/open-in-web-dark.png"></picture></a>&nbsp;<a href="https://cursor.com/background-agent?bcId=bc-291b14cd-4dc9-4d1d-8980-6b0612c6ab31&cursor_ref=pr_footer&cursor_cta=open_in_cursor"><picture><source media="(prefers-color-scheme: dark)" srcset="https://cursor.com/assets/images/open-in-cursor-dark.png"><source media="(prefers-color-scheme: light)" srcset="https://cursor.com/assets/images/open-in-cursor-light.png"><img alt="Open in Cursor" width="131" height="28" src="https://cursor.com/assets/images/open-in-cursor-dark.png"></picture></a>&nbsp;</div> [WPN-1800]: https://consensyssoftware.atlassian.net/browse/WPN-1800?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ <!-- CURSOR_SUMMARY --> --- > [!NOTE] > **Low Risk** > Changes are limited to E2E tests and page-object helpers; no production wallet, send, or swap logic is modified. > > <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit 1762590. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).</sup> <!-- /CURSOR_SUMMARY --> --------- Co-authored-by: Cursor Agent <cursoragent@cursor.com> Co-authored-by: Xiaoming Wang <dawnseeker8@users.noreply.github.com>
1 parent d7335cd commit b77ee50

7 files changed

Lines changed: 942 additions & 0 deletions

File tree

‎test/e2e/page-objects/pages/bridge/quote-page.ts‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,39 @@ class BridgeQuotePage {
534534
});
535535
};
536536

537+
/**
538+
* Waits until the source amount input holds a stable non-empty value.
539+
* Use this after Max, which writes the balance asynchronously.
540+
*
541+
* @returns The source amount input value.
542+
*/
543+
async getSourceAmount(): Promise<string> {
544+
console.log('Reading swap source amount');
545+
let stableValue = '';
546+
let stableReads = 0;
547+
await this.driver.waitUntil(
548+
async () => {
549+
const input = await this.driver.findElement(this.sourceAmount);
550+
const value = await input.getAttribute('value');
551+
if (!value) {
552+
stableValue = '';
553+
stableReads = 0;
554+
return false;
555+
}
556+
if (value === stableValue) {
557+
stableReads += 1;
558+
} else {
559+
stableValue = value;
560+
stableReads = 1;
561+
}
562+
return stableReads >= 2;
563+
},
564+
{ interval: 200, timeout: 15000 },
565+
);
566+
console.log(`Swap source amount is ${stableValue}`);
567+
return stableValue;
568+
}
569+
537570
goBack = async () => {
538571
await this.driver.waitForSelector(this.backButton);
539572
await this.driver.clickElement(this.backButton);
@@ -692,12 +725,31 @@ class BridgeQuotePage {
692725
}
693726

694727
async selectSrcToken(token: string): Promise<void> {
728+
console.log(`Selecting source token ${token}`);
695729
await this.driver.waitForSelector(this.sourceAssetPickerButton);
696730
await this.driver.clickElement(this.sourceAssetPickerButton);
697731
await this.driver.pasteIntoField(this.assetPrickerSearchInput, token);
698732
await this.selectAssetFromPicker(token);
699733
}
700734

735+
/**
736+
* Selects the source token when it is not already selected.
737+
* Re-selecting the current token clears the balance and hides Max.
738+
*
739+
* @param token - Token symbol, such as ETH or MON.
740+
*/
741+
async selectSrcTokenIfNeeded(token: string): Promise<void> {
742+
console.log(`Selecting source token ${token} if needed`);
743+
const sourceButton = await this.driver.findElement(
744+
this.sourceAssetPickerButton,
745+
);
746+
const label = await sourceButton.getText();
747+
if (label.includes(token)) {
748+
return;
749+
}
750+
await this.selectSrcToken(token);
751+
}
752+
701753
async setCustomSlippage(value: string): Promise<void> {
702754
await this.driver.clickElement(this.slippageEditButton);
703755
await this.driver.clickElement(this.slippageCustomButton);

‎test/e2e/page-objects/pages/confirmations/transaction-confirmation.ts‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@ class TransactionConfirmation extends Confirmation {
127127
testId: 'inline-alert',
128128
};
129129

130+
private readonly nativeAmountHeadingSelector =
131+
'[data-testid="confirm-send-amount"]';
132+
130133
private readonly networkName: RawLocator =
131134
'[data-testid="confirmation__details-network-name"]';
132135

@@ -538,6 +541,42 @@ class TransactionConfirmation extends Confirmation {
538541
await this.driver.fill(this.customNonceInput, nonce);
539542
}
540543

544+
/**
545+
* Reads the native amount shown on the confirmation heading.
546+
*
547+
* Prefers the heading tooltip title, which is the full-precision value when
548+
* the heading itself is rounded. Waits until that value stops changing so a
549+
* max-send gas refresh is included.
550+
*
551+
* @param symbol - Native symbol that must appear in the heading, such as ETH.
552+
* @returns The displayed amount, either the tooltip title or the heading text.
553+
*/
554+
async getDisplayedNativeAmount(symbol: string): Promise<string> {
555+
console.log(`Reading displayed native amount for ${symbol}`);
556+
let stableValue = '';
557+
let stableReads = 0;
558+
await this.driver.waitUntil(
559+
async () => {
560+
const current = await this.readDisplayedNativeAmount(symbol);
561+
if (!current) {
562+
stableValue = '';
563+
stableReads = 0;
564+
return false;
565+
}
566+
if (current === stableValue) {
567+
stableReads += 1;
568+
} else {
569+
stableValue = current;
570+
stableReads = 1;
571+
}
572+
return stableReads >= 3;
573+
},
574+
{ interval: 400, timeout: 20000 },
575+
);
576+
console.log(`Displayed native amount is ${stableValue}`);
577+
return stableValue;
578+
}
579+
541580
/**
542581
* Gets the network name displayed on the transaction confirmation page.
543582
*
@@ -584,6 +623,29 @@ class TransactionConfirmation extends Confirmation {
584623
await this.driver.clickElement(this.editGasFeeIcon);
585624
}
586625

626+
private async readDisplayedNativeAmount(
627+
symbol: string,
628+
): Promise<string | null> {
629+
const amount: unknown = await this.driver.executeScript(
630+
`const symbol = ${JSON.stringify(symbol)};
631+
const heading = document.querySelector(arguments[0]);
632+
if (!heading || !(heading.textContent || '').includes(symbol)) {
633+
return null;
634+
}
635+
let node = heading;
636+
while (node) {
637+
const title = node.getAttribute && node.getAttribute('title');
638+
if (title && /\\d/.test(title)) {
639+
return title;
640+
}
641+
node = node.parentElement;
642+
}
643+
return heading.textContent;`,
644+
this.nativeAmountHeadingSelector,
645+
);
646+
return typeof amount === 'string' && amount.trim() !== '' ? amount : null;
647+
}
648+
587649
async selectTokenFee(tokenSymbol: string): Promise<void> {
588650
console.log(`Select token ${tokenSymbol} to pay for the fees`);
589651
await this.driver.clickElement(this.tokenGasFeeDropdown);

‎test/e2e/page-objects/pages/send/send-page.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,39 @@ class SendPage {
401401
}
402402
}
403403

404+
/**
405+
* Waits until the amount input holds a stable non-empty value, then returns it.
406+
* Use this after Max, which fills the input asynchronously.
407+
*
408+
* @returns The amount input value.
409+
*/
410+
async getAmountInputValue(): Promise<string> {
411+
console.log('Reading send amount input value');
412+
let stableValue = '';
413+
let stableReads = 0;
414+
await this.driver.waitUntil(
415+
async () => {
416+
const inputElement = await this.driver.findElement(this.amountInput);
417+
const value = await inputElement.getAttribute('value');
418+
if (!value) {
419+
stableValue = '';
420+
stableReads = 0;
421+
return false;
422+
}
423+
if (value === stableValue) {
424+
stableReads += 1;
425+
} else {
426+
stableValue = value;
427+
stableReads = 1;
428+
}
429+
return stableReads >= 2;
430+
},
431+
{ interval: 200, timeout: 10000 },
432+
);
433+
console.log(`Send amount input value is ${stableValue}`);
434+
return stableValue;
435+
}
436+
404437
/**
405438
* Clicks Continue once the button is stably enabled, then acknowledges the
406439
* optional send-alert modal when present.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* Swap max parity.
3+
*
4+
* Captures Swap Max for the same account and balance used by the send parity
5+
* spec. Ethereum Swap Max is the full balance, matching send-page Max. The
6+
* send confirmation is lower by the mocked gas fee; that comparison lives in
7+
* the send spec.
8+
*
9+
* Monad Swap Max is the balance minus 10 MON. That reserve comes from
10+
* `MINIMUM_NATIVE_RESERVE_BALANCE_PER_CHAIN` in
11+
* `ui/pages/bridge/utils/minimum-reserve.ts` and is intentional. It is not a
12+
* send-confirmation discrepancy.
13+
*/
14+
15+
import { withFixtures } from '../../helpers';
16+
import { login } from '../../page-objects/flows/login.flow';
17+
import HomePage from '../../page-objects/pages/home/homepage';
18+
import BridgeQuotePage from '../../page-objects/pages/bridge/quote-page';
19+
import { MAX_AMOUNT_NETWORKS } from '../send/max-amount-parity.constants';
20+
import {
21+
assertAmountEquals,
22+
expectedSwapMax,
23+
getMaxAmountParityFixtures,
24+
} from '../send/max-amount-parity';
25+
26+
describe('Swap max amount', function () {
27+
MAX_AMOUNT_NETWORKS.forEach((network) => {
28+
it(`fills max for native ${network.symbol}`, async function () {
29+
await withFixtures(
30+
getMaxAmountParityFixtures(network, this.test?.fullTitle(), {
31+
includeBridgeFlags: true,
32+
}),
33+
async ({ driver }) => {
34+
await login(driver, {
35+
validateBalance: false,
36+
waitForNonEvmAccounts: false,
37+
});
38+
39+
const homePage = new HomePage(driver);
40+
await homePage.startSwapFlow();
41+
42+
const bridgePage = new BridgeQuotePage(driver);
43+
await bridgePage.checkPageIsLoaded();
44+
await bridgePage.selectSrcTokenIfNeeded(network.symbol);
45+
await bridgePage.clickMaxButton();
46+
47+
const swapMax = await bridgePage.getSourceAmount();
48+
49+
// Ethereum: full balance, same as send-page Max.
50+
// Monad: balance minus the intentional 10 MON native reserve.
51+
assertAmountEquals(
52+
swapMax,
53+
expectedSwapMax(network),
54+
`Swap max for ${network.symbol}`,
55+
);
56+
},
57+
);
58+
});
59+
});
60+
});
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/**
2+
* Shared balance, gas, and network values for max-amount parity E2E tests.
3+
*
4+
* Send and swap specs import these directly. Fixture builders and Mockttp
5+
* helpers live in `max-amount-parity.ts`.
6+
*/
7+
8+
import type { CaipAssetType } from '@metamask/utils';
9+
import {
10+
CHAIN_IDS,
11+
MAINNET_DISPLAY_NAME,
12+
MONAD_DISPLAY_NAME,
13+
} from '../../../../shared/constants/network';
14+
import { NETWORK_CLIENT_ID } from '../../constants';
15+
16+
/** Human native balance seeded for every network row. */
17+
export const NATIVE_BALANCE = '25';
18+
19+
/**
20+
* 25 * 10^18 wei. Matches {@link NATIVE_BALANCE} for `eth_getBalance` and the
21+
* account tracker.
22+
*/
23+
export const NATIVE_BALANCE_HEX = '0x15af1d78b58c40000';
24+
25+
/**
26+
* Gas limit returned by `eth_estimateGas`.
27+
* Included RPCs apply `confirmations_gas_buffer.included` (1.5), so the
28+
* confirmation uses 750000 gas. 750000 * 2000 gwei = 1.5 native units.
29+
*/
30+
export const GAS_LIMIT_HEX = '0x7a120';
31+
32+
/** 2000 gwei, in wei. Matches `suggestedMaxFeePerGas` on every gas API tier. */
33+
export const MAX_FEE_PER_GAS_HEX = '0x1d1a94a2000';
34+
35+
/**
36+
* Buffer applied to `eth_estimateGas` for Infura (included) RPCs.
37+
* From `confirmations_gas_buffer.included` in the production flag registry.
38+
*/
39+
export const GAS_BUFFER = 1.5;
40+
41+
/** Native-unit fee the confirmation should subtract: buffered gas * max fee. */
42+
export const GAS_FEE_NATIVE = 1.5;
43+
44+
/**
45+
* How far the send/confirmation gap may drift from {@link GAS_FEE_NATIVE}.
46+
* Large enough to absorb display rounding, small enough that a missed gas
47+
* subtraction (a full 1 native unit) still fails.
48+
*/
49+
export const GAS_FEE_TOLERANCE = 0.05;
50+
51+
/** Numeric tolerance for amounts that should match exactly. */
52+
export const AMOUNT_TOLERANCE = 1e-8;
53+
54+
export const MAX_SEND_RECIPIENT = '0x2f318C334780961FB129D2a6c30D0763d9a5C970';
55+
56+
export type MaxAmountNetwork = {
57+
chainId: string;
58+
chainIdDecimal: number;
59+
assetId: CaipAssetType;
60+
symbol: string;
61+
name: string;
62+
networkClientId: string;
63+
infuraHost: string;
64+
/** Human units held back by Swap Max. `0` on Ethereum, `10` on Monad. */
65+
swapReserve: string;
66+
sentinelNetwork: string;
67+
};
68+
69+
export const MAX_AMOUNT_NETWORKS: MaxAmountNetwork[] = [
70+
{
71+
chainId: CHAIN_IDS.MAINNET,
72+
chainIdDecimal: 1,
73+
assetId: 'eip155:1/slip44:60',
74+
symbol: 'ETH',
75+
name: MAINNET_DISPLAY_NAME,
76+
networkClientId: NETWORK_CLIENT_ID.MAINNET,
77+
infuraHost: 'mainnet.infura.io',
78+
swapReserve: '0',
79+
sentinelNetwork: 'ethereum-mainnet',
80+
},
81+
{
82+
chainId: CHAIN_IDS.MONAD,
83+
chainIdDecimal: 143,
84+
assetId: 'eip155:143/slip44:268435779',
85+
symbol: 'MON',
86+
name: MONAD_DISPLAY_NAME,
87+
networkClientId: 'monad-mainnet',
88+
infuraHost: 'monad-mainnet.infura.io',
89+
swapReserve: '10',
90+
sentinelNetwork: 'monad-mainnet',
91+
},
92+
];

0 commit comments

Comments
 (0)