Back
+20 XP
10/12
Challenge: Process a Transfer Instruction
Simulate Solana's process_instruction pattern in pure Rust. You'll process a transfer by validating balances, deducting fees, and returning updated state.
Your Task
Implement process_transfer(sender_balance, recipient_balance, amount, fee) that:
- Checks the sender can afford
amount + fee - If not, returns
Err("INSUFFICIENT_BALANCE") - If yes, computes new balances and returns
Ok((new_sender, new_recipient))
Requirements:
- Sender pays both the transfer amount AND the fee
- Recipient receives only the amount (not the fee)
- The fee is burned (deducted from sender, not added anywhere)
- Use checked arithmetic to prevent overflow
This mirrors how Solana programs work:
process_instructionreceives account balances- Validates preconditions
- Mutates balances
- Returns success or error
Test Cases
Deducts amount + fee, credits recipient
Input:
1000, 500, 200, 5Expected: Ok((795, 700))Insufficient balance fails
Input:
100, 0, 100, 5Expected: __result__.is_err()Handles zero fee and exact drain
Input:
500, 200, 500, 0Expected: Ok((0, 700))