Skip to content

chore: add transfer2 close safeguard#2234

Merged
ananas-block merged 1 commit intomainfrom
jorrit/chore-add-compress-and-close-safeguard
Feb 6, 2026
Merged

chore: add transfer2 close safeguard#2234
ananas-block merged 1 commit intomainfrom
jorrit/chore-add-compress-and-close-safeguard

Conversation

@ananas-block
Copy link
Contributor

@ananas-block ananas-block commented Feb 5, 2026

Summary by CodeRabbit

  • Bug Fixes
    • Added validation to prevent closing token accounts that contain non-zero balances, providing additional safeguards against unintended account closure operations.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 5, 2026

📝 Walkthrough

Walkthrough

This PR adds a runtime validation check to the Solana token closing path that verifies the token account balance is zero before allowing the close operation to proceed. Non-zero balances will trigger a NonNativeHasBalance error, preventing improper account closure.

Changes

Cohort / File(s) Summary
Balance Verification on Close
programs/compressed-token/program/src/compressed_token/transfer2/compression/ctoken/compress_and_close.rs
Added zero-balance runtime assertion before token account closure. Borrows account data, parses Token amount, and returns error if balance is non-zero. Includes conditional compilation for Solana-specific imports (AccountInfoTrait, Token).

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

  • fix: compress and close hotpath #2059: Also modifies compress_and_close.rs validation logic, adding presence checks and error handling for compressed outputs in the same closure workflow.

Suggested labels

ai-review

Suggested reviewers

  • sergeytimoshin
  • SwenSchaeferjohann

Poem

🔐 A guardian check stands sentinel and true,
Before the account closes, balance whispers through—
Zero or bust, the rule now holds so tight,
No phantom coins slip away in the night! ✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'chore: add transfer2 close safeguard' accurately describes the main change: adding a safety check to prevent closing token accounts with non-zero balances in the transfer2 compression workflow.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 70.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch jorrit/chore-add-compress-and-close-safeguard

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
programs/compressed-token/program/src/compressed_token/transfer2/compression/ctoken/compress_and_close.rs (1)

28-30: ⚠️ Potential issue | 🟠 Major

Import and use convert_token_error for Token::amount_from_slice failures.

At lines 274-275, Token::amount_from_slice returns a TokenError from the SPL Token interface, but the current code discards it and maps to the generic ProgramError::InvalidAccountData. This breaks the expected ErrorCode contract by losing the SPL Token error information.

Import convert_token_error from the shared module and apply it to the error:

🔧 Fix
 use crate::{
     compressed_token::transfer2::accounts::Transfer2Accounts, shared::convert_program_error,
+    shared::convert_token_error,
 };
 ...
                 let amount = Token::amount_from_slice(&data)
-                    .map_err(|_| ProgramError::InvalidAccountData)?;
+                    .map_err(convert_token_error)?;

@ananas-block ananas-block merged commit fd8026b into main Feb 6, 2026
31 checks passed
@ananas-block ananas-block deleted the jorrit/chore-add-compress-and-close-safeguard branch February 6, 2026 07:18
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