Skip to content

refactor(program): simplify after duplication and dead-code review#68

Merged
dev-jodee merged 1 commit intomainfrom
refactor/simplify-program
Apr 30, 2026
Merged

refactor(program): simplify after duplication and dead-code review#68
dev-jodee merged 1 commit intomainfrom
refactor/simplify-program

Conversation

@dev-jodee
Copy link
Copy Markdown
Collaborator

Summary

  • Delete dead init_header (duplicate of Header::init); use existing get_token_account_owner helper in fixed/recurring transfer paths.
  • Add SECS_PER_HOUR const and PlanTerms::period_length_secs helper; replace 3600 magic numbers across program code and tests.
  • Remove redundant data.plan_bump != plan.bump check in subscribe (verify_plan_pda already enforces bump correctness via PDA derivation).
  • Single-pass Plan::check_destination (drop iterator clone); move state/versioning logic out of mod.rs into core.rs (mod.rs convention from CLAUDE.md).
  • Extract resolve_optional_payer helper in helpers/system.rs (reused by 3 instruction processors); unify FixedTransferAccounts and RecurringTransferAccounts into shared DelegationTransferAccounts in helpers/transfer_utils.rs.
  • Strip narrating WHAT-comments in transfer/cancel handlers; keep WHY-comments only.

No behavior change. Net ~80 LOC removed across instruction handlers.

Test Plan

  • cargo check -p subscriptions clean
  • cargo clippy -p subscriptions --all-targets -- -D warnings clean
  • cargo test-sbf — 38/38 unit tests pass
  • git diff idl/ empty (no IDL drift; Codama attrs untouched)
  • just fmt clean
  • CI green

Skipped Findings (deliberate)

  • Removing find_program_address for the delegator ATA in transfer_utils.rs was flagged as a CU win, but the owner+mint check on the token account is not sufficient — the SubscriptionAuthority PDA may have been approved on multiple token accounts of the same user. Leaving the canonical-ATA derivation in place.
  • Refactoring instruction-data dispatch to thread lifetimes (avoid .clone() of packed structs) is invasive and audit-relevant; deferred.
  • Double-load of SubscriptionAuthority (check + load) deferred — fix forces a wider trait refactor.

- Delete unused init_header (duplicate of Header::init)
- Use existing get_token_account_owner helper in fixed/recurring transfer paths
- Add SECS_PER_HOUR const + PlanTerms::period_length_secs helper; replace 3600 magic numbers
- Remove redundant plan_bump check in subscribe (verify_plan_pda already enforces bump)
- Single-pass Plan::check_destination (drop iterator clone)
- Move state/versioning logic from mod.rs into core.rs (mod.rs convention)
- Extract resolve_optional_payer helper in helpers/system.rs (reused 3x)
- Unify FixedTransferAccounts and RecurringTransferAccounts into shared DelegationTransferAccounts
- Strip narrating WHAT-comments in transfer/cancel handlers; keep WHY-comments

No behavior change; no IDL drift. All 38 unit tests pass under cargo test-sbf;
clippy -D warnings clean for subscriptions crate.
@github-actions
Copy link
Copy Markdown

Compute Unit Report

Instruction Samples Min CUs Max CUs Avg CUs Est Cost (Low) [SOL] Est Cost (Med) [SOL] Est Cost (High) [SOL]
cancel_subscription 11 1805 2118 2003 0.000005000 0.000005080 0.000006001
close_subscription_authority 7 1881 1916 1886 0.000005000 0.000005075 0.000005943
create_fixed_delegation 38 3621 12649 5150 0.000005001 0.000005206 0.000007575
create_plan 84 3540 14053 5043 0.000005001 0.000005201 0.000007521
create_recurring_delegation 27 3640 14145 4980 0.000005001 0.000005199 0.000007490
delete_plan 8 416 416 416 0.000005000 0.000005016 0.000005208
init_subscription_authority 142 7849 27351 12511 0.000005003 0.000005500 0.000011255
revoke_delegation 19 318 585 420 0.000005000 0.000005016 0.000005210
subscribe 21 6666 12666 7890 0.000005002 0.000005315 0.000008945
transfer_fixed 6 10105 14605 11354 0.000005003 0.000005454 0.000010677
transfer_recurring 17 10212 20712 11748 0.000005003 0.000005469 0.000010874
transfer_subscription 10 10495 24008 15450 0.000005004 0.000005618 0.000012725
update_plan 21 424 500 474 0.000005000 0.000005018 0.000005237

Generated: 2026-04-30

@dev-jodee dev-jodee merged commit 12728d1 into main Apr 30, 2026
8 checks passed
@dev-jodee dev-jodee deleted the refactor/simplify-program branch April 30, 2026 15:30
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