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.
- 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.
- 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.
- 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.
-
Install dependencies:
yarn install # or npm install
-
Configure Solana CLI:
- Set up your wallet and localnet/devnet as needed.
- Update
Anchor.toml
with your wallet path and cluster.
-
Build and Deploy the Program:
anchor build anchor deploy
-
Run Tests:
yarn test # or anchor test
The test suite in tests/escrow.ts
demonstrates the full lifecycle:
-
Make Offer:
- Derives PDAs for the maker and vault.
- Calls
make
to create the escrow state and vault accounts.
-
Deposit Tokens:
- Calls
deposit
to transfer tokens from the maker to the vault. - Checks vault balance after deposit.
- Calls
-
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.
- Taker calls
-
Refund:
- Maker calls
refund
to reclaim tokens if the offer is not taken. - Asserts that the vault is closed and tokens are returned.
- Maker calls
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 clientAnchor.toml
— Anchor configurationCargo.toml
,Xargo.toml
— Rust and Solana BPF build setuppackage.json
,tsconfig.json
— TypeScript and test setup
- Solana CLI
- Anchor CLI
- Node.js, Yarn or npm
- 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
.