Skip to content

Conversation

@gitteri
Copy link
Collaborator

@gitteri gitteri commented Jul 25, 2025

Add Token-2022 Program Support

Overview

This PR branches off of #247 to support recent changes and improve test coverage. It adds comprehensive support for the Token-2022 program to Solana Pay's createTransfer functionality, enabling seamless transfers of both legacy SPL tokens and Token-2022 tokens while maintaining full backward compatibility.

Key Changes

Core Functionality

  • Automatic Token Program Detection: The createTransfer function now automatically detects whether a token uses the Token-2022 program (TOKEN_2022_PROGRAM_ID) or the legacy Token program (TOKEN_PROGRAM_ID) by checking the token mint's account owner
  • Enhanced SPL Token Transfer Support: All SPL token operations now correctly pass the detected token program ID to ensure compatibility with both token standards

🔧 Implementation Details

core/src/createTransfer.ts

  • Added imports for TOKEN_2022_PROGRAM_ID and TOKEN_PROGRAM_ID from @solana/spl-token
  • Implemented automatic token program detection in createSPLTokenInstruction():
    const accountInfo = await connection.getParsedAccountInfo(splToken);
    const accountOwner = accountInfo.value?.owner;
    const tokenProgram = 
        accountOwner && accountOwner === TOKEN_2022_PROGRAM_ID 
            ? TOKEN_2022_PROGRAM_ID 
            : TOKEN_PROGRAM_ID;
  • Updated all SPL token function calls to include the appropriate tokenProgram parameter:
    • getMint(connection, splToken, undefined, tokenProgram)
    • getAssociatedTokenAddress(splToken, sender, undefined, tokenProgram)
    • getAccount(connection, senderATA, undefined, tokenProgram)
    • createTransferCheckedInstruction(..., tokenProgram)

🧪 Testing Improvements

core/test/createTransfer.test.ts

  • Complete Test Suite Refactor: Restructured tests with improved organization and maintainability
  • Test Fixtures & Utilities: Added reusable test fixtures and helper classes:
    • TestFixtures class for consistent test data
    • MockFactories for creating mock objects
    • TestHelpers for common test setup patterns
  • Comprehensive Token-2022 Coverage: Added dedicated test cases for Token-2022 transfers
  • Enhanced Error Testing: Expanded error condition testing for both token programs
  • Better Mock Management: Improved mocking strategy with proper TypeScript types

Benefits

Backward Compatibility: Existing integrations continue to work without any changes
Future-Proof: Ready for the growing Token-2022 ecosystem
Automatic Detection: No manual configuration required - the library automatically detects the correct token program
Comprehensive Testing: Robust test coverage ensures reliability across both token standards

Testing

  • All existing tests pass, ensuring no regressions
  • New test cases cover Token-2022 specific scenarios
  • Enhanced test structure provides better maintainability and coverage

Migration Guide

No migration required! This change is fully backward compatible. The library will automatically detect and handle both legacy SPL tokens and Token-2022 tokens seamlessly.


Closes: Issues related to Token-2022 support
Branch: t22-supportmaster


Important

Adds support for Token-2022 in Solana Pay's createTransfer, with automatic token program detection and comprehensive test updates.

  • Behavior:
    • createTransfer in createTransfer.ts now detects Token-2022 or legacy SPL tokens automatically.
    • Updates SPL token operations to pass the correct token program ID.
  • Testing:
    • Refactors test suite in createTransfer.test.ts for better organization.
    • Adds test cases for Token-2022 transfers and error conditions.
    • Introduces TestFixtures, MockFactories, and TestHelpers for reusable test components.
  • Misc:
    • Updates @solana/spl-token to ^0.4.13 and @solana/web3.js to ^1.98.2 in package.json.
    • Fixes links in SPEC.md and SPEC1.1.md to use solana-foundation.github.io URLs.

This description was created by Ellipsis for a12376b. You can customize this summary. It will automatically update as commits are pushed.

Copy link

@ellipsis-dev ellipsis-dev bot left a comment

Choose a reason for hiding this comment

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

Skipped PR review on 1aef700 because no changed files had a supported extension. If you think this was in error, please contact us and we'll fix it right away.

Copy link

@ellipsis-dev ellipsis-dev bot left a comment

Choose a reason for hiding this comment

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

Important

Looks good to me! 👍

Reviewed everything up to a12376b in 2 minutes and 23 seconds. Click for details.
  • Reviewed 984 lines of code in 14 files
  • Skipped 1 files when reviewing.
  • Skipped posting 9 draft comments. View those below.
  • Modify your settings and rules to customize what types of comments Ellipsis leaves. And don't forget to react with 👍 or 👎 to teach Ellipsis.
1. core/src/createTransfer.ts:143
  • Draft comment:
    Good implementation of Token-2022 detection. Consider adding an inline comment clarifying that if getParsedAccountInfo returns null or an unexpected owner, the legacy TOKEN_PROGRAM_ID is used.
  • Reason this comment was not posted:
    Decided after close inspection that this draft comment was likely wrong and/or not actionable: usefulness confidence = 20% vs. threshold = 50% The code is fairly self-explanatory with the existing comment and clear ternary operator. The fallback behavior is standard programming practice - if a condition isn't met, use the default. The suggestion would add verbosity without adding much clarity. The code change itself is good but doesn't need more documentation. Perhaps some developers less familiar with Token-2022 would benefit from the extra clarity about fallback behavior. The current comment doesn't explicitly state what happens in the negative case. While extra documentation can sometimes help, in this case the logic is straightforward enough that the existing comment plus the clear code structure is sufficient. We should avoid over-documenting simple patterns. The comment should be deleted as it suggests adding unnecessary documentation to already clear code.
2. core/src/createTransfer.ts:150
  • Draft comment:
    Mint initialization check and precision validation are clear. No changes required.
  • Reason this comment was not posted:
    Confidence changes required: 0% <= threshold 50% None
3. core/src/fetchTransaction.ts:28
  • Draft comment:
    Nice update using getLatestBlockhash over the deprecated getRecentBlockhash, which improves compatibility for Token-2022.
  • Reason this comment was not posted:
    Confidence changes required: 0% <= threshold 50% None
4. core/test/createTransfer.test.ts:269
  • Draft comment:
    Comprehensive tests for both legacy SPL and Token-2022 transfers. The mocks and error validations appear robust. Great job!
  • Reason this comment was not posted:
    Confidence changes required: 0% <= threshold 50% None
5. docs/src/INTRODUCTION.md:2
  • Draft comment:
    Minor title spacing issue: 'Solana Pay' has an extra space. Consider changing it to 'Solana Pay' for consistency.
  • Reason this comment was not posted:
    Comment was not on a location in the diff, so it can't be submitted as a review comment.
6. docs/src/SPEC.md:100
  • Draft comment:
    Verify query string delimiter; the example URL appears to use '&label=Michael' without a preceding '?' delimiter.
  • Reason this comment was not posted:
    Comment was not on a location in the diff, so it can't be submitted as a review comment.
7. SPEC.md:173
  • Draft comment:
    Typographical note: There is an extra space between 'set the' and '[feePayer]' in this line. Consider removing the additional space for consistency.
  • Reason this comment was not posted:
    Comment was on unchanged code.
8. SPEC1.1.md:187
  • Draft comment:
    Typographical error: There is an extra space before [feePayer] in this line. Please remove the extra space.
  • Reason this comment was not posted:
    Comment was on unchanged code.
9. docs/src/SPEC.md:178
  • Draft comment:
    Typo: There is an extra space between 'set the' and '[feePayer]' in this line. Please remove the extra space.
  • Reason this comment was not posted:
    Comment was on unchanged code.

Workflow ID: wflow_5afmfGYCOd89NvSc

You can customize Ellipsis by changing your verbosity settings, reacting with 👍 or 👎, replying to comments, or adding code review rules.

@gitteri gitteri merged commit 72f510d into master Jul 25, 2025
2 checks passed
@gitteri gitteri deleted the t22-support branch July 25, 2025 21: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.

7 participants