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
Transfer 200 with fee 5 from 1000 balance
Input:
1000, 500, 200, 5Expected: Ok((795, 700))Should fail when sender can't cover amount + fee
Input:
100, 0, 100, 5Expected: __result__.is_err()