Skip to content

tusharpamnani/anchor_vault

Repository files navigation

Solana Vault Program

This project implements a secure, user-specific vault on Solana using Anchor framework. Each user can create their own vault to deposit and withdraw SOL, with all logic and security enforced by program-derived addresses (PDAs).

Features

  • Personal Vaults: Each user gets a unique vault and state PDA.
  • Deposit/Withdraw: Users can deposit and withdraw SOL from their vault.
  • Close Vault: Users can close their vault, reclaiming all SOL and rent.
  • Anchor Framework: Built with Anchor for safety, clarity, and easy testing.
  • TypeScript Tests: Comprehensive tests using Mocha/Chai and Anchor's TypeScript client.

How It Works

PDA Structure

  • Vault State PDA: Stores bump seeds for security and uniqueness. Derived as:
    • seeds = [b"state", user_pubkey]
  • Vault PDA: Holds SOL. Derived as:
    • seeds = [b"vault", vault_state_pubkey]

Program Instructions

  • initialize: Creates the vault state and vault PDAs for the user.
  • deposit: Transfers SOL from the user's wallet to their vault PDA.
  • withdraw: Transfers SOL from the vault PDA back to the user's wallet (PDA signs).
  • close: Transfers all remaining SOL and closes both PDAs, refunding rent.

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/test.ts demonstrates the full lifecycle:

  1. Initialize Vault:

    • Derives PDAs for the user.
    • Calls initialize to create the vault state and vault accounts.
  2. Deposit SOL:

    • Calls deposit to transfer SOL from the user to the vault PDA.
    • Checks vault balance after deposit.
  3. Withdraw SOL:

    • Calls withdraw to transfer SOL from the vault PDA back to the user.
    • Checks vault balance after withdrawal.
  4. Close Vault:

    • Calls close to transfer all remaining SOL and close the PDAs.
    • Asserts that the vault state account is closed.

File Structure

  • programs/vault/src/lib.rs — Anchor program logic (Rust)
  • tests/test.ts — TypeScript tests using Anchor client
  • Anchor.toml — Anchor configuration
  • package.json, tsconfig.json — TypeScript and test setup

Requirements

Security Notes

  • All vaults are PDAs, so only the program can move funds from vaults.
  • Bump seeds are stored in the state PDA for secure PDA derivation and signing.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published