Skip to content

refactor(finance/escrow): extract PDA-aware token transfer/close helpers#65

Merged
mikemaccana merged 1 commit into
mainfrom
refactor/escrow-shared-token-helpers
Jun 9, 2026
Merged

refactor(finance/escrow): extract PDA-aware token transfer/close helpers#65
mikemaccana merged 1 commit into
mainfrom
refactor/escrow-shared-token-helpers

Conversation

@mikemaccana

Copy link
Copy Markdown
Collaborator

What

Refactors the Anchor escrow example to remove duplicated token-CPI boilerplate.

  • transfer_tokens gains an owning_pda_seeds: Option<&[&[u8]]> parameter so the same helper handles both signer-authority and PDA-authority transfers.
  • Adds a close_token_account helper covering the PDA-signed vault close.
  • make_offer, take_offer, and cancel_offer now call these helpers instead of hand-rolling CpiContext::new_with_signer + transfer_checked/close_account in each handler.

These helpers were adapted from the prototype escrow-2026 project (now removed), but updated to use Anchor 1.0's CPI calling convention (token_program.key()) rather than the to_account_info() style from that 0.32.1 prototype.

Behavior

No on-chain behavior change. The per-maker PDA seeds ([b"offer", maker, id]) and the has_one constraints are unchanged.

Testing

cargo test — 5 tests pass:

  • test_make_offer
  • test_take_offer
  • test_cancel_offer
  • test_cancel_offer_rejects_non_maker
  • test_id

Add an owning_pda_seeds option to transfer_tokens and a new
close_token_account helper in shared.rs, then use them across
make_offer, take_offer, and cancel_offer. This removes the duplicated
CpiContext::new_with_signer + transfer_checked/close_account boilerplate
that was hand-rolled in each handler.

No behavior change: per-maker PDA seeds and has_one checks are unchanged.
Verified with cargo test (5 tests pass).
@mikemaccana mikemaccana merged commit 9c0f67d into main Jun 9, 2026
18 checks passed
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