Cherry-picking commits from main to release/8.0.0 for PR #32062#32087
Open
runway-github[bot] wants to merge 1 commit into
Open
Cherry-picking commits from main to release/8.0.0 for PR #32062#32087runway-github[bot] wants to merge 1 commit into
runway-github[bot] wants to merge 1 commit into
Conversation
…8.0.0 (#32062) <!-- Please submit this PR as a draft initially. Do not mark it as "Ready for review" until this PR meets the canonical Definition of Ready For Review in `docs/readme/ready-for-review.md`. In short: the template must be materially complete (not just section titles present), all status checks must be currently passing, and the only expected follow-up commits must be reviewer-driven. --> <!-- mms-check directive vocabulary — read by .github/scripts/shared/pr-template-checks.ts at module load to build the validation plan. Directives are invisible in rendered markdown and must NOT be removed or edited without updating the validator registry. type=text Section must contain non-placeholder prose. type=changelog Section must have a valid CHANGELOG entry: line. type=issue-link Section must have a Fixes:/Closes:/Refs: line with a value. type=manual-testing Section must have real testing steps or an explicit N/A. type=screenshot Section must have evidence (image/URL) or an explicit N/A. type=checklist Section must have all checkboxes consciously checked. required=true|false Whether a missing/invalid section runs the validator at all. blocking=true|false Whether a failure of this check fails the CI workflow. Default: false — failures are shown as warnings in the sticky comment but do not block the PR. Sections without a directive are checked for structural presence only. --> ## **Description** <!-- mms-check: type=text required=true --> mUSD withdrawals were failing intermittently with `TellerWithMultiAssetSupport__MinimumAssetsNotMet` (`0x9883d840`). The root cause is a double-truncation bug: `getSharesForWithdrawal()` uses BigInt floor division to compute the number of shares, and then the on-chain teller's `mulDivDown` floors again when converting shares back to assets. This causes `assetsOut` to land up to 1 base unit below `minimumAssets`, triggering a revert. For example, a $1.96 withdrawal at rate ≈1,000,094 computed `shareAmount = 1,959,815` (floored), which the contract converted back to `assetsOut = 1,959,999` — 1 unit below the `minimumAssets` of `1,960,000`. A $1.00 withdrawal at the same rate passed only by coincidence (the double-floor happened to land exactly on the minimum). This PR applies two defense-in-depth fixes in `moneyAccountTransactions.ts`: 1. **Ceiling division for shares**: `getSharesForWithdrawal` now uses `(amount * SCALAR + rate - 1n) / rate` instead of floor division, guaranteeing enough shares to cover the requested withdrawal amount after the contract's `mulDivDown`. 2. **1-unit slippage on `minimumAssets`**: `minimumAssets` is now set to `amount - 1n` (with a `0n` guard) instead of the exact `amount`. This tolerates any residual rounding error without affecting the user — the subsequent ERC-20 transfer still uses the original `amount`. ## **Changelog** <!-- mms-check: type=changelog required=true blocking=true --> CHANGELOG entry: Fixed a bug causing mUSD withdrawals to fail intermittently due to a rounding precision error ## **Related issues** <!-- mms-check: type=issue-link required=true --> Fixes: https://consensyssoftware.atlassian.net/browse/MUSD-1029 ## **Manual testing steps** <!-- mms-check: type=manual-testing required=true --> ```gherkin Feature: mUSD withdrawal rounding fix Scenario: user withdraws a non-round mUSD amount successfully Given the user has an mUSD money account with a balance of at least $5 And the vault rate is not exactly 1:1 When user initiates a withdrawal of $1.96 Then the withdrawal transaction completes successfully And the user receives the expected USDC amount Scenario: user withdraws $1.00 successfully Given the user has an mUSD money account with a balance of at least $2 When user initiates a withdrawal of $1.00 Then the withdrawal transaction completes successfully Scenario: repeated withdrawals have a high success rate Given the user has an mUSD money account with sufficient balance When user performs 10+ withdrawals of varying amounts ($1.00, $1.50, $1.96, $2.50, $5.00) Then all withdrawal transactions complete successfully And none revert with MinimumAssetsNotMet ``` Joao Tavares' visual agent testing tool can be used to run withdrawals at volume against a branch to verify success rate (previously very low, should now be ~100%). ## **Screenshots/Recordings** <!-- mms-check: type=screenshot required=true --> N/A — This is a pure arithmetic fix in transaction encoding logic with no UI changes. Verification is via automated withdrawal testing and unit tests. ### **Before** ### **After** ## **Pre-merge author checklist** <!-- mms-check: type=checklist required=true --> - [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. #### Performance checks (if applicable) - [ ] I've tested on Android - Ideally on a mid-range device; emulator is acceptable - [ ] I've tested with a power user scenario - Use these [power-user SRPs](https://consensyssoftware.atlassian.net/wiki/spaces/TL1/pages/edit-v2/401401446401?draftShareId=9d77e1e1-4bdc-4be1-9ebb-ccd916988d93) to import wallets with many accounts and tokens - [ ] I've instrumented key operations with Sentry traces for production performance metrics - See [`trace()`](/app/util/trace.ts) for usage and [`addToken`](/app/components/Views/AddAsset/components/AddCustomToken/AddCustomToken.tsx#L274) for an example For performance guidelines and tooling, see the [Performance Guide](https://consensyssoftware.atlassian.net/wiki/spaces/TL1/pages/400085549067/Performance+Guide+for+Engineers). ## **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] > **Medium Risk** > Touches live withdrawal transaction encoding for money accounts; logic is narrow and well-tested but incorrect rounding could still affect on-chain success or slippage checks. > > **Overview** > Fixes intermittent mUSD withdrawal reverts from **`MinimumAssetsNotMet`** when floor division in **`getSharesForWithdrawal`** and on-chain **`mulDivDown`** both truncate, so **`assetsOut`** can sit one base unit below the encoded minimum. > > **`getSharesForWithdrawal`** now uses **ceiling division** so share count is high enough that contract-side conversion back to assets meets the requested amount. **`buildMoneyAccountWithdrawBatch`** sets **`minimumAssets`** to **`amount - 1`** (or **`0`** for zero-amount placeholder batches) as a second guard; the follow-on ERC-20 transfer still uses the full **`amount`**, so payout to the user is unchanged. > > Tests add the reported **$1.96** case, rate sweeps, and calldata checks for **`shareAmount`** and **`minimumAssets`**. > > <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit b43d77d. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).</sup> <!-- /CURSOR_SUMMARY -->
Contributor
🔍 Smart E2E Test Selection⏭️ Smart E2E selection skipped - PR targets a release or stable branch (release/* or stable) All E2E tests pre-selected. |
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Description
mUSD withdrawals were failing intermittently with
TellerWithMultiAssetSupport__MinimumAssetsNotMet(0x9883d840). Theroot cause is a double-truncation bug:
getSharesForWithdrawal()usesBigInt floor division to compute the number of shares, and then the
on-chain teller's
mulDivDownfloors again when converting shares backto assets. This causes
assetsOutto land up to 1 base unit belowminimumAssets, triggering a revert.For example, a $1.96 withdrawal at rate ≈1,000,094 computed
shareAmount = 1,959,815(floored), which the contract converted back toassetsOut = 1,959,999— 1 unit below theminimumAssetsof1,960,000. A $1.00withdrawal at the same rate passed only by coincidence (the double-floor
happened to land exactly on the minimum).
This PR applies two defense-in-depth fixes in
moneyAccountTransactions.ts:getSharesForWithdrawalnow uses(amount * SCALAR + rate - 1n) / rateinstead of floor division,guaranteeing enough shares to cover the requested withdrawal amount
after the contract's
mulDivDown.minimumAssets:minimumAssetsis now set toamount - 1n(with a0nguard) instead of the exactamount. Thistolerates any residual rounding error without affecting the user — the
subsequent ERC-20 transfer still uses the original
amount.Changelog
CHANGELOG entry: Fixed a bug causing mUSD withdrawals to fail
intermittently due to a rounding precision error
Related issues
Fixes: https://consensyssoftware.atlassian.net/browse/MUSD-1029
Manual testing steps
Joao Tavares' visual agent testing tool can be used to run withdrawals
at volume against a branch to verify success rate (previously very low,
should now be ~100%).
Screenshots/Recordings
N/A — This is a pure arithmetic fix in transaction encoding logic with
no UI changes. Verification is via automated withdrawal testing and unit
tests.
Before
After
Pre-merge author checklist
Docs and MetaMask Mobile
Coding
Standards.
if applicable
guidelines).
Not required for external contributors.
Performance checks (if applicable)
SRPs
to import wallets with many accounts and tokens
performance metrics
trace()for usage andaddTokenfor an example
For performance guidelines and tooling, see the Performance
Guide.
Pre-merge reviewer checklist
app, test code being changed).
in the ticket it closes and includes the necessary testing evidence such
as recordings and or screenshots.
Note
Medium Risk
Touches live withdrawal transaction encoding for money accounts; logic
is narrow and well-tested but incorrect rounding could still affect
on-chain success or slippage checks.
Overview
Fixes intermittent mUSD withdrawal reverts from
MinimumAssetsNotMetwhen floor division ingetSharesForWithdrawaland on-chainmulDivDownbothtruncate, so
assetsOutcan sit one base unit below the encodedminimum.
getSharesForWithdrawalnow uses ceiling division so sharecount is high enough that contract-side conversion back to assets meets
the requested amount.
buildMoneyAccountWithdrawBatchsetsminimumAssetstoamount - 1(or0for zero-amountplaceholder batches) as a second guard; the follow-on ERC-20 transfer
still uses the full
amount, so payout to the user is unchanged.Tests add the reported $1.96 case, rate sweeps, and calldata
checks for
shareAmountandminimumAssets.Reviewed by Cursor Bugbot for commit
b43d77d. Bugbot is set up for automated
code reviews on this repo. Configure
here.