Week 2: a SOL vault, a token escrow, and what ATAs really are
2026-11-11For week 2 of the Turbin3 builders cohort I wrote two Anchor programs: a SOL vault (initialize, deposit, withdraw, close) and a token escrow (make, take, refund, update), with 19 TypeScript tests between them. The code is in the repo: vault and escrow.
I already knew how a vault works, so Part 1 is short. The escrow was new to me, and it made me properly understand associated token accounts (ATAs), so most of this post is Part 2.
Part 1: a SOL vault
Each wallet gets two PDAs. vault_state is derived from ["state", user] and stores the bumps. vault is derived from ["vault", vault_state] and holds the SOL. Since the seeds chain back to the signer, a wallet can only ever reach its own vault. My test for this has a second wallet try to withdraw from the first wallet's vault, and Anchor rejects it with ConstraintSeeds before my code runs.
The vault is a plain system account, so moving SOL out of it needs a signature that only the program can produce. The program signs the System Program transfer with the vault's seeds:
let signer_seeds: &[&[&[u8]]] = &[&[VAULT_SEED, vault_state_key.as_ref(), &[vault_bump]]];
CpiContext::new_with_signer(system_program::ID, accounts, signer_seeds)
Two details:
initializefunds the vault with the rent-exempt minimum, andwithdrawnever goes below it. Otherwise a withdrawal could leave the account with too little rent and the runtime would reject the transaction.closesends everything, rent included, back to the user. An account at 0 lamports gets deleted, andclose = useronvault_staterefunds that account's rent too.
Part 2: a token escrow
An escrow is a swap between two people who don't trust each other. The maker locks token A and names a price in token B. Any taker who pays that price gets the A. The program holds the A in between, so neither side has to go first.
| Instruction | Who signs | What happens |
|---|---|---|
make | maker | creates the escrow, moves deposit of A into the vault, stores receive (the B price) |
take | taker | B goes taker → maker, A goes vault → taker, vault and escrow close |
refund | maker | A goes back to the maker, vault and escrow close |
update | maker | changes receive without a refund and re-make |
What I learned about ATAs
In week 1 I described an ATA as "a token account at an address derived from the wallet and the mint". Writing the escrow showed me what that means in practice.
The owner of an ATA can be a PDA. The vault isn't a special account type. It's the ATA for mint_a whose authority is the escrow PDA:
#[account(
init,
payer = maker,
associated_token::mint = mint_a,
associated_token::authority = escrow,
associated_token::token_program = token_program,
)]
pub vault: InterfaceAccount<'info, TokenAccount>,
Nobody holds a private key for escrow, so the only way to move tokens out of the vault is the program signing with ["escrow", maker, seed, bump]. That one line is what makes the escrow trustless.
The address depends on the token program too. An ATA is derived from wallet, token program and mint, so the same wallet and mint give a different address under Token and Token-2022. That's why every ATA constraint names token_program, and why I used token_interface and InterfaceAccount: the same code works with both programs.
Someone has to pay for an ATA. In take, the taker might not have an A account yet, and the maker might not have a B account. init_if_needed creates them, and the taker pays the rent for both, since the taker is the one calling. The vault's rent was paid by the maker, so the maker gets it back when the vault closes.
A token account must be empty before it can close. take and refund both transfer the full vault balance out first, then call close_account with the escrow PDA as authority. Swap the order and the close fails.
transfer_checked over transfer. It takes the mint and its decimals, so the token program confirms the mint matches and the amount is in the units I meant. Token-2022 requires it for some extensions anyway.
What I learned about the escrow itself
The seed lets one maker run many escrows. The escrow PDA is ["escrow", maker, seed], so a maker can have one open offer per seed instead of only one in total.
has_one keeps callers honest. take checks has_one = maker, has_one = mint_a and has_one = mint_b against the stored escrow. Without those, a taker could pass a different maker account or a worthless mint B and walk off with the A.
A failed take is all-or-nothing. In one test the taker doesn't hold enough B. The B transfer fails, so the whole transaction reverts and the vault still holds every token A. There's no state where the taker has paid and the A hasn't moved, or the other way round.
Closing the escrow makes a second take impossible. After a successful take, a second take on the same escrow fails with AccountNotInitialized, because the account no longer exists. No "already filled" flag needed.
I added update beyond the standard three. Without it, a maker who wants a different price has to refund and make again: two transactions, and rent paid twice. A test checks that take then charges the new price.
Why it's interesting and where I'd use it
The escrow is the smallest version of a pattern that's all over Solana: a PDA owns some tokens, and the program releases them only when the conditions in the code are met. OTC trades and NFT-for-token sales are direct uses. Freelance payments and milestone grants are the same thing with a different release condition. Order books and lending protocols are this idea scaled up.
Next, I want to make the escrow timed: store a deadline at make and read the clock sysvar so take only works before it and refund only after.