This repository contains my solution to the Solana Summer School challenge: modifying a standard Anchor Escrow contract to include a 5-minute time lock on cancellations.
The goal was to modify an existing Solana Escrow program to prevent makers from immediately canceling their escrows. A time lock ensures that an escrow remains open and available to takers for a minimum of 5 minutes after creation.
- Expanded the
Escrowaccount to include acreated_at: i64field. - Learned how to interface with Solana's
Clocksysvar viaClock::get()?.unix_timestampto securely read on-chain time.
- Recorded the exact creation time during the
makeinstruction. - Added a mathematical boundary check in the
cancelinstruction to ensurecurrent_time - created_at >= 300(5 minutes). - Protected the check with Anchor's
require!macro, failing the transaction safely if the time lock hasn't expired.
- Implemented a custom
ErrorCode::TimeLockedto provide clear feedback when a user attempts to cancel an escrow prematurely.
- Encountered a Solana BPF stack limit error (
Stack offset of 4120 exceeded max offset of 4096). - Learned how to optimize stack memory footprint by utilizing
Box<Account<'info, T>>andBox<InterfaceAccount<'info, T>>to move large structs (like TokenAccounts) onto the heap. - Successfully boxed 7 different accounts in the
Takeinstruction to heavily reduce the stack frame size.
- Make sure you have the Solana CLI and Anchor installed.
- Build the BPF program:
cargo build-sbf
- Run the test suite (LiteSVM):
cargo test
Built during the Solana Summer School program.