Skip to content
This repository was archived by the owner on Aug 7, 2026. It is now read-only.

fix: evaluated positive or negative balanceDiff to put transactions in from or to - #632

Closed
gabrieledm wants to merge 2 commits into
mainfrom
fix/WPN-1223_solana-incorrect-event-logged-for-swap-operation-from-usdc-to-sol
Closed

fix: evaluated positive or negative balanceDiff to put transactions in from or to#632
gabrieledm wants to merge 2 commits into
mainfrom
fix/WPN-1223_solana-incorrect-event-logged-for-swap-operation-from-usdc-to-sol

Conversation

@gabrieledm

@gabrieledm gabrieledm commented Jul 27, 2026

Copy link
Copy Markdown

Description

Fixes incorrect transaction direction and type detection when the fee payer receives an amount of SOL that is less than or equal to the transaction fee.
Previously, native transfer direction was determined using the raw pre and post transaction balances.
For a USDC-to-SOL swap, the fee payer’s balance can still decrease when the received SOL is smaller than the fee.
This caused the incoming SOL to be classified as outgoing and could prevent the transaction from being identified as a swap.

The mapper now determines direction from the fee-adjusted balance difference:

  • Positive difference -> outgoing SOL
  • Negative difference -> incoming SOL
  • Zero difference -> fee-only balance change, not a transfer

Fixes MetaMask/metamask-extension#43164

Changes

  • Use the fee-adjusted SOL balance difference to determine native transfer direction.
  • Ignore SOL balance decreases caused exclusively by transaction fees.
  • Update the existing swap expectation to reflect the correct native SOL direction.
  • Add tests covering incoming SOL amounts that are:
    • Less than the transaction fee
    • Equal to the transaction fee
    • Greater than the transaction fee
  • Add equivalent coverage for SPL-token-to-native-SOL swaps.
  • Use fresh mapper dependencies in the new parameterized tests to prevent mock state from leaking between cases.

Testing

  • Added native SOL transfer boundary tests around the transaction fee.
  • Added a fee-only transaction test.
  • Added SPL-token-to-SOL swap classification tests for all fee boundary cases.

Before

WPN-1223  Before
WPN-1223.Before.mov

After

WPN-1223  After
WPN-1223.After.mp4

Note

Medium Risk
Changes transaction type/direction logic for all native SOL balance parsing in activity; wrong edge cases could misclassify sends, receives, or swaps.

Overview
Fixes Solana activity mislabeling small USDC→SOL swaps as sends when the fee payer’s net SOL change is dominated by the transaction fee.

TransactionMapper now classifies native SOL movement from the fee-adjusted balance difference (pre − post − fee for the fee payer): positive → from, negative → to, zero → skip (fee-only, not a transfer). Direction no longer uses raw pre/post balances alone.

Tests add a withTransactionMapper helper and cover incoming SOL at fee boundaries, fee-only txs, and SPL→SOL swap classification; one swap fixture expectation is updated for correct SOL leg ordering. Changelog and snap manifest shasum are updated.

Reviewed by Cursor Bugbot for commit 0895f9d. Bugbot is set up for automated code reviews on this repo. Configure here.

@gabrieledm
gabrieledm requested a review from a team as a code owner July 27, 2026 20:44
@gabrieledm gabrieledm changed the title fix: evaluated positive or negative 'balanceDiff' to put transactions in from or to fix: evaluated positive or negative balanceDiff to put transactions in from or to Jul 27, 2026
@gabrieledm

Copy link
Copy Markdown
Author

@metamaskbot publish-preview

@github-actions

Copy link
Copy Markdown
Contributor

Preview builds have been published. Learn how to use preview builds in other projects.

Expand for full list of packages and versions.
@metamask-previews/solana-wallet-snap@3.0.0-preview-649ae97

@Battambang

Copy link
Copy Markdown
Contributor

May worth to add a changelog fixed entry in packages/snap/CHANGELOG.md since this is a user-facing activity

@sonarqubecloud

Copy link
Copy Markdown

@gabrieledm

Copy link
Copy Markdown
Author

Replaced by: MetaMask/internal-snaps#108

@gabrieledm gabrieledm closed this Aug 4, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Incorrect event logged for Swap operation from USDC to SOL

2 participants