Skip to content

fix(stake): accept tokenProgramId parameter in account builders#177

Open
6figpsolseeker wants to merge 1 commit intodcccrypto:mainfrom
6figpsolseeker:fix/stake-token-program-id-param
Open

fix(stake): accept tokenProgramId parameter in account builders#177
6figpsolseeker wants to merge 1 commit intodcccrypto:mainfrom
6figpsolseeker:fix/stake-token-program-id-param

Conversation

@6figpsolseeker
Copy link
Copy Markdown

@6figpsolseeker 6figpsolseeker commented Apr 9, 2026

Summary

initPoolAccounts, depositAccounts, withdrawAccounts, and flushToInsuranceAccounts all hardcoded TOKEN_PROGRAM_ID (SPL Token). If the collateral mint or LP mint is owned by Token-2022 (TOKEN_2022_PROGRAM_ID), all token operations (create, transfer, burn) would fail because the wrong token program is referenced in the instruction account list.

Changes

Each function now accepts an optional tokenProgramId parameter that defaults to TOKEN_PROGRAM_ID for backward compatibility:

// Before
export function depositAccounts(a: StakeAccounts['deposit']) { ... }

// After
export function depositAccounts(a: StakeAccounts['deposit'], tokenProgramId: PublicKey = TOKEN_PROGRAM_ID) { ... }

Updated functions:

This is a non-breaking change — existing callers that omit the parameter get the same behaviour as before.

Test plan

  • npm run lint (tsc --noEmit) clean
  • Full vitest run: same 14 pre-existing failures as main, zero new failures
  • Existing stake tests pass (no changes needed — they use SPL Token mints)

Summary by CodeRabbit

  • Refactor
    • Enhanced stake account configuration and initialization functions to accept optional token program selection parameters. Developers can now specify custom token program implementations for their specific integration needs, providing greater flexibility and control. These updates maintain complete backward compatibility, with default behavior unchanged for existing implementations.

initPoolAccounts, depositAccounts, withdrawAccounts, and
flushToInsuranceAccounts all hardcoded TOKEN_PROGRAM_ID (SPL Token).
If the collateral mint or LP mint is owned by Token-2022, all token
operations (create, transfer, burn) would fail because the wrong token
program is referenced in the instruction account list.

Each function now accepts an optional tokenProgramId parameter that
defaults to TOKEN_PROGRAM_ID for backward compatibility. Callers with
Token-2022 mints pass TOKEN_2022_PROGRAM_ID explicitly.

This is a non-breaking change — existing callers that omit the parameter
get the same behaviour as before.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 9, 2026

📝 Walkthrough

Walkthrough

Four account-key builder functions in the Solana stake module (initPoolAccounts, depositAccounts, withdrawAccounts, flushToInsuranceAccounts) have been updated to accept an optional tokenProgramId parameter with a default value, replacing hardcoded token program references.

Changes

Cohort / File(s) Summary
Token Program Parameterization
src/solana/stake.ts
Added optional tokenProgramId parameter (defaulting to TOKEN_PROGRAM_ID) to four account-key builder functions and updated token program account entries to use the parameter instead of hardcoded imports. Includes corresponding JSDoc @param documentation updates.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 Four functions dressed in finer thread,
With token programs no longer stuck in red,
A flexible parameter hops into view,
Now builders can choose which program to use! ✨

🚥 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 accurately summarizes the main change: adding optional tokenProgramId parameter support to four account builder functions in the stake module.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

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

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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
Copy Markdown

@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.

🧹 Nitpick comments (1)
src/solana/stake.ts (1)

509-582: Add explicit tests for non-default tokenProgramId path.

Current coverage exercises default behavior; please add assertions that passing a custom tokenProgramId is reflected in each builder’s returned account list to prevent regressions.

🧪 Suggested test shape
+it('uses provided tokenProgramId in all stake account builders', () => {
+  const customTokenProgramId = new PublicKey('11111111111111111111111111111111');
+
+  expect(initPoolAccounts(initPoolArgs, customTokenProgramId)[8].pubkey.equals(customTokenProgramId)).toBe(true);
+  expect(depositAccounts(depositArgs, customTokenProgramId)[8].pubkey.equals(customTokenProgramId)).toBe(true);
+  expect(withdrawAccounts(withdrawArgs, customTokenProgramId)[8].pubkey.equals(customTokenProgramId)).toBe(true);
+  expect(flushToInsuranceAccounts(flushArgs, customTokenProgramId)[7].pubkey.equals(customTokenProgramId)).toBe(true);
+});
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/solana/stake.ts` around lines 509 - 582, Add unit tests that exercise the
non-default tokenProgramId path for initPoolAccounts, depositAccounts,
withdrawAccounts, and flushToInsuranceAccounts: call each function with a custom
PublicKey (not TOKEN_PROGRAM_ID) and assert the returned account array includes
that custom key (and not the default) in the expected slot for the token program
entry; ensure tests check both pubkey equality and isSigner/isWritable flags for
that entry to guard against regressions when the tokenProgramId parameter is
provided.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@src/solana/stake.ts`:
- Around line 509-582: Add unit tests that exercise the non-default
tokenProgramId path for initPoolAccounts, depositAccounts, withdrawAccounts, and
flushToInsuranceAccounts: call each function with a custom PublicKey (not
TOKEN_PROGRAM_ID) and assert the returned account array includes that custom key
(and not the default) in the expected slot for the token program entry; ensure
tests check both pubkey equality and isSigner/isWritable flags for that entry to
guard against regressions when the tokenProgramId parameter is provided.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: e654424e-e97d-467d-a3eb-cd60b3bf78be

📥 Commits

Reviewing files that changed from the base of the PR and between 2f80d79 and 196105d.

📒 Files selected for processing (1)
  • src/solana/stake.ts

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.

1 participant