Back
+20 XP
8/12
Challenge: Simulate a CPI Vault Transfer in Rust
Cross-Program Invocations (CPIs) let programs transfer SOL from PDA-controlled vaults. You'll simulate this pattern: derive a vault PDA, verify it has funds, and compute the transfer.
Your Task
Implement build_vault_transfer(user, vault_balance, recipient_balance, amount) that:
- Derives the vault PDA using
find_pda(&["vault", user], "system")to get the bump - Checks the vault has enough balance for the transfer
- Returns
Ok((new_vault_balance, new_recipient_balance, bump))on success - Returns
Err("INSUFFICIENT_VAULT_BALANCE")if vault can't cover the amount
This mirrors the real Anchor pattern:
invoke_signed(
&transfer_instruction,
&[vault_pda, recipient, system_program],
&[&[b"vault", user.key().as_ref(), &[bump]]],
)?;
The bump is needed for invoke_signed — the program proves it controls the PDA.
Test Cases
Should succeed with sufficient vault balance
Input:
"alice", 1000, 500, 200Expected: __result__.is_ok()Should fail with insufficient vault balance
Input:
"alice", 100, 500, 200Expected: __result__.is_err()