Skip to content

SGDlab67/solana-time-locked-escrow

Repository files navigation

Solana Time-Locked Escrow ⏳

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 Challenge

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.

🛠️ What I Built & Learned

1. Modifying Program State with Timestamps

  • Expanded the Escrow account to include a created_at: i64 field.
  • Learned how to interface with Solana's Clock sysvar via Clock::get()?.unix_timestamp to securely read on-chain time.

2. Enforcing Time Locks

  • Recorded the exact creation time during the make instruction.
  • Added a mathematical boundary check in the cancel instruction to ensure current_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.

3. Custom Error Codes

  • Implemented a custom ErrorCode::TimeLocked to provide clear feedback when a user attempts to cancel an escrow prematurely.

4. Stack Optimization (Boxing)

  • 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>> and Box<InterfaceAccount<'info, T>> to move large structs (like TokenAccounts) onto the heap.
  • Successfully boxed 7 different accounts in the Take instruction to heavily reduce the stack frame size.

🏃‍♂️ How to Run

  1. Make sure you have the Solana CLI and Anchor installed.
  2. Build the BPF program:
    cargo build-sbf
  3. Run the test suite (LiteSVM):
    cargo test

Built during the Solana Summer School program.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors