-
Notifications
You must be signed in to change notification settings - Fork 5.4k
fix: improvement in dapp swap functionality #38627
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
|
CLA Signature Action: All authors have signed the CLA. You may need to manually re-run the blocking PR check if it doesn't pass in a few minutes. |
| data, | ||
| V4_BASE_ACTIONS_ABI_DEFINITION, | ||
| ); | ||
| const parsedResult = parseV4ExactIn(result[0]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For srcTokenAddress and srcTokenAmount values from first swap command get precedence for destTokenAddress and destTokenAmount values from last swap command get precedence.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: Duplicate swap commands process wrong input data
When there are duplicate swap commands of the same type (e.g., two V3_SWAP_EXACT_OUT '01' commands in sequence like ['04', '00', '01', '01']), the swapCommands array will contain duplicates. The forEach loop iterates over each duplicate, but getCommandData uses findIndex which always returns the index of the first occurrence. This means the second '01' command will incorrectly process inputs[2] again instead of inputs[3], causing wrong token amounts and addresses to be computed for multi-hop swaps with repeated command types.
shared/modules/dapp-swap-comparison/dapp-swap-command-utils.ts#L556-L570
metamask-extension/shared/modules/dapp-swap-comparison/dapp-swap-command-utils.ts
Lines 556 to 570 in 4bc217c
| commands.forEach((command) => { | |
| const data = getCommandData(commandBytes, inputs, command); | |
| if (data !== undefined) { | |
| const commandParser: DAPP_SWAP_COMMANDS_PARSER_TYPE | undefined = | |
| parserDefinition.find( | |
| (parser: DAPP_SWAP_COMMANDS_PARSER_TYPE) => parser.value === command, | |
| ); | |
| const result = commandParser?.handler(data, decodingResult, chainId); | |
| if (result) { | |
| decodingResult = result; | |
| } | |
| } | |
| }); |
shared/modules/dapp-swap-comparison/dapp-swap-command-utils.ts#L231-L234
metamask-extension/shared/modules/dapp-swap-comparison/dapp-swap-command-utils.ts
Lines 231 to 234 in 4bc217c
| ) { | |
| const commandIndex = commandBytes.findIndex( | |
| (commandByte: string) => commandByte === command, | |
| ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: Duplicate swap commands only process first occurrence's data
When processing duplicate swap commands (e.g., two V3_SWAP_EXACT_IN commands in sequence), the getCommandData function uses findIndex which always returns the index of the first occurrence of that command type. Combined with iterating over filtered commands that preserves duplicates, this means duplicate commands will all process the same input data from the first occurrence, ignoring subsequent commands' actual input data. For example, with commands ['00', '00'], both iterations would use inputs[0] and inputs[1] would never be processed, potentially resulting in incorrect token amounts or addresses.
shared/modules/dapp-swap-comparison/dapp-swap-command-utils.ts#L556-L570
metamask-extension/shared/modules/dapp-swap-comparison/dapp-swap-command-utils.ts
Lines 556 to 570 in fe93b5f
| commands.forEach((command) => { | |
| const data = getCommandData(commandBytes, inputs, command); | |
| if (data !== undefined) { | |
| const commandParser: DAPP_SWAP_COMMANDS_PARSER_TYPE | undefined = | |
| parserDefinition.find( | |
| (parser: DAPP_SWAP_COMMANDS_PARSER_TYPE) => parser.value === command, | |
| ); | |
| const result = commandParser?.handler(data, decodingResult, chainId); | |
| if (result) { | |
| decodingResult = result; | |
| } | |
| } | |
| }); |
shared/modules/dapp-swap-comparison/dapp-swap-command-utils.ts#L231-L238
metamask-extension/shared/modules/dapp-swap-comparison/dapp-swap-command-utils.ts
Lines 231 to 238 in fe93b5f
| ) { | |
| const commandIndex = commandBytes.findIndex( | |
| (commandByte: string) => commandByte === command, | |
| ); | |
| if (commandIndex < 0) { | |
| return undefined; | |
| } | |
| return inputs[commandIndex]; |
Builds ready [fe93b5f]
UI Startup Metrics (1263 ± 83 ms)
📊 Page Load Benchmark ResultsCurrent Commit: 📄 Localhost MetaMask Test DappSamples: 100 Summary
📈 Detailed Results
Bundle size diffs [🚨 Warning! Bundle size has increased!]
|
Builds ready [42e6f84]
UI Startup Metrics (1251 ± 101 ms)
📊 Page Load Benchmark ResultsCurrent Commit: 📄 Localhost MetaMask Test DappSamples: 100 Summary
📈 Detailed Results
Bundle size diffs [🚨 Warning! Bundle size has increased!]
|
Builds ready [9472034]
UI Startup Metrics (1257 ± 79 ms)
📊 Page Load Benchmark ResultsCurrent Commit: 📄 Localhost MetaMask Test DappSamples: 100 Summary
📈 Detailed Results
Bundle size diffs [🚨 Warning! Bundle size has increased!]
|
Builds ready [91f660d]
UI Startup Metrics (1351 ± 116 ms)
📊 Page Load Benchmark ResultsCurrent Commit: 📄 Localhost MetaMask Test DappSamples: 100 Summary
📈 Detailed Results
Bundle size diffs [🚨 Warning! Bundle size has increased!]
|
Description
Dapp swap comparison: adding support for mode different command types.
Changelog
CHANGELOG entry:
Related issues
Fixes: https://github.com/MetaMask/MetaMask-planning/issues/6393
Manual testing steps
NA
Screenshots/Recordings
NA
Pre-merge author checklist
Pre-merge reviewer checklist
Note
Adds support for parsing multiple sequential swap commands with validation and refactors decoding to map inputs by index while adjusting field precedence for amountMin and token/amount selection.
shared/modules/dapp-swap-comparison/dapp-swap-command-utils.ts):validateSwapCommandsto require at least one swap and enforce sequential swap commands; invoked ingetCommandValues.getGenericValuesto iteratecommandBytesand readinputsby index; accept merged{...SwapCommands, ...NonSwapCommands}; remove one-swap restriction.getCommandDatahelper.amountMinwhere available.quotesInput, prefer existingsrcTokenAmount/srcTokenAddressand prefer decodeddestTokenAddress(fallbacks retained).||and cachedminimumAmount).shared/modules/dapp-swap-comparison/dapp-swap-command-utils.test.ts):Written by Cursor Bugbot for commit 91f660d. This will update automatically on new commits. Configure here.