Skip to content

tusharpamnani/anchor_escrow

Repository files navigation

Solana Escrow Program

This project implements a secure, trustless escrow protocol on Solana using the Anchor framework. Two parties can safely swap SPL tokens using program-derived addresses (PDAs) and on-chain logic, with all flows enforced by the smart contract.

Features

  • Trustless Escrow: Securely swap SPL tokens between two parties with no intermediaries.
  • Offer/Take/Refund: Makers create offers, deposit tokens, and can refund if not taken. Takers can accept offers and atomically swap tokens.
  • Anchor Framework: Built with Anchor for safety, clarity, and easy testing.
  • TypeScript Tests: Comprehensive tests using Mocha/Chai and Anchor’s TypeScript client.
  • Deterministic PDAs: All state and vault accounts are derived using seeds for uniqueness and security.

How It Works

PDA Structure

  • Escrow State PDA: Stores offer details and bump. Derived as:
    • seeds = [b"escrow", maker_pubkey, seed (u64 le)]
  • Vault ATA: Associated token account for the escrow PDA, holds the offered tokens.
  • Maker/Taker ATAs: Standard SPL token accounts for each user.

Program Instructions

  • make: Initializes a new escrow offer and vault for the maker.
  • deposit: Deposits the offered tokens into the vault.
  • take: Taker accepts the offer, swaps tokens, and closes the vault.
  • refund: Maker cancels the offer and reclaims escrowed tokens if not taken.

Setup

  1. Install dependencies:

    yarn install
    # or
    npm install
  2. Configure Solana CLI:

    • Set up your wallet and localnet/devnet as needed.
    • Update Anchor.toml with your wallet path and cluster.
  3. Build and Deploy the Program:

    anchor build
    anchor deploy
  4. Run Tests:

    yarn test
    # or
    anchor test

Usage Example (Test Flow)

The test suite in tests/escrow.ts demonstrates the full lifecycle:

  1. Make Offer:

    • Derives PDAs for the maker and vault.
    • Calls make to create the escrow state and vault accounts.
  2. Deposit Tokens:

    • Calls deposit to transfer tokens from the maker to the vault.
    • Checks vault balance after deposit.
  3. Take Offer (Swap):

    • Taker calls take to send their tokens to the maker and receive the escrowed tokens.
    • Checks balances and asserts vault is closed.
  4. Refund:

    • Maker calls refund to reclaim tokens if the offer is not taken.
    • Asserts that the vault is closed and tokens are returned.

File Structure

  • programs/escrow/src/lib.rs — Anchor program logic (Rust, entrypoint)
  • programs/escrow/src/instructions/ — Instruction handlers (make.rs, deposit.rs, take.rs, refund.rs)
  • programs/escrow/src/state/ — State definitions (Escrow struct)
  • tests/escrow.ts — TypeScript tests using Anchor client
  • Anchor.toml — Anchor configuration
  • Cargo.toml, Xargo.toml — Rust and Solana BPF build setup
  • package.json, tsconfig.json — TypeScript and test setup

Requirements

Security Notes

  • All escrow and vault accounts are PDAs, so only the program can move funds from vaults.
  • Bump seeds are stored in the escrow state PDA for secure PDA derivation and signing.
  • Only the maker can refund, and only if the offer has not been taken.
  • All swaps and refunds are atomic and enforced by on-chain logic.

For full usage and API details, see the test suite in tests/escrow.ts.


About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published