Skip to content

fix: always extract special assets to prevent stale balances#234

Merged
ulissesferreira merged 3 commits intomainfrom
fix/always-extract-special-assets
Mar 11, 2026
Merged

fix: always extract special assets to prevent stale balances#234
ulissesferreira merged 3 commits intomainfrom
fix/always-extract-special-assets

Conversation

@ulissesferreira
Copy link
Contributor

@ulissesferreira ulissesferreira commented Mar 11, 2026

Summary

Fixes a stale-state bug where 4 special assets showed stale non-zero balances on the client after their on-chain balance dropped to zero.

Root cause: The extraction methods for staked-for-bandwidth, staked-for-energy, ready-for-withdrawal, and in-lock-period conditionally returned [] when the computed amount was 0. Since saveMany uses upsert semantics (only processes assets in the input array), omitting an asset meant no state update and no push event — the repository kept the old non-zero value indefinitely.

Fix: Always extract all special assets regardless of their balance, matching the pattern already used by #extractStakingRewardsAsset, #extractBandwidth, and #extractEnergy. This is safe because all affected assets are in ESSENTIAL_ASSETS, so saveMany already handles zero-amount essential assets correctly (they are never placed in the removed list).

Changes

  • #extractStakedNativeAssets: Removed if (stakedBandwidthAmount > 0) and if (stakedEnergyAmount > 0) guards. Method now always returns exactly 2 entries.
  • #extractReadyForWithdrawalAssets -> #extractReadyForWithdrawalAsset: Changed from returning AssetEntity[] (with conditional []) to returning a single AssetEntity always. Matches the #extractStakingRewardsAsset pattern.
  • #extractInLockPeriodAssets -> #extractInLockPeriodAsset: Same refactor.
  • Updated #extractAssets coordinator to use the new singular return types.
  • Updated 6 test assertions from toBeUndefined() to toBeDefined() + rawAmount: '0'.
  • Updated coverage thresholds (improved by the fix).

Assets affected

Asset Was buggy Now
Staked for Bandwidth (sTRX-BANDWIDTH) Only extracted when > 0 Always extracted
Staked for Energy (sTRX-ENERGY) Only extracted when > 0 Always extracted
Ready for Withdrawal (trx-ready-for-withdrawal) Only extracted when > 0 Always extracted
In Lock Period (trx-in-lock-period) Only extracted when > 0 Always extracted
Staking Rewards Already always extracted Always extracted
Bandwidth / Max Bandwidth Already always extracted Always extracted
Energy / Max Energy Already always extracted Always extracted

Four special assets (staked-for-bandwidth, staked-for-energy,
ready-for-withdrawal, in-lock-period) were only extracted when their
balance was greater than zero. When the balance dropped to zero, the
asset was not passed to saveMany, so the repository kept the old
non-zero value and no push event updated MetaMask. This caused stale
non-zero balances to persist on the client indefinitely.

Now all special assets are always extracted regardless of their balance,
matching the pattern already used by staking-rewards, bandwidth, and
energy. This is safe because all four assets are in ESSENTIAL_ASSETS, so
saveMany already handles zero-amount essential assets correctly (they
are never placed in the removed list).

Changes:
- #extractStakedNativeAssets: removed conditional guards, always returns
  both staked-for-bandwidth and staked-for-energy
- #extractReadyForWithdrawalAssets -> #extractReadyForWithdrawalAsset:
  changed to always return a single AssetEntity (matching
  #extractStakingRewardsAsset pattern)
- #extractInLockPeriodAssets -> #extractInLockPeriodAsset: same refactor
- Updated 6 test assertions from toBeUndefined() to toBeDefined() with
  rawAmount '0'

Made-with: Cursor
@ulissesferreira ulissesferreira requested a review from a team as a code owner March 11, 2026 11:07
@ulissesferreira ulissesferreira mentioned this pull request Mar 11, 2026
@ulissesferreira
Copy link
Contributor Author

@metamaskbot publish-preview

@github-actions
Copy link
Contributor

Preview builds have been published. See these instructions for more information about preview builds.

Expand for full list of packages and versions.
{
  "@metamask-previews/tron-wallet-snap": "1.23.1-preview-8b315a6"
}

@maxime-oe
Copy link

lgtm

@ulissesferreira ulissesferreira merged commit 8af499e into main Mar 11, 2026
11 checks passed
@ulissesferreira ulissesferreira deleted the fix/always-extract-special-assets branch March 11, 2026 15:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants