A simple and secure SOL vault program built with the Anchor framework on Solana. This program allows users to initialize a personal vault, deposit SOL, withdraw SOL, and close their vault when needed.
- Initialize: Create a personal vault account with PDA-based architecture
- Deposit: Transfer SOL into your vault
- Withdraw: Retrieve SOL from your vault
- Close: Close your vault and reclaim all remaining SOL and rent
The program uses two Program Derived Addresses (PDAs):
- Vault State PDA: Stores the bump seeds for both PDAs (derived from
["state", user.publicKey]) - Vault PDA: Holds the deposited SOL (derived from
["vault", vaultState.publicKey])
- Rust (version 1.89.0)
- Solana CLI
- Anchor Framework (version 0.32.1)
- Node.js and Yarn
- Clone the repository:
git clone <repository-url>
cd anchor_vault- Install dependencies:
yarn install- Build the program:
anchor buildRun the test suite:
anchor testThe tests cover:
- Vault initialization
- SOL deposits
- SOL withdrawals
- Vault closure
await program.methods
.initialize()
.accounts({
user: userPublicKey,
vaultState: vaultStatePda,
vault: vaultPda,
systemProgram: SystemProgram.programId,
})
.rpc();const depositAmount = 1 * LAMPORTS_PER_SOL; // 1 SOL
await program.methods
.deposit(new BN(depositAmount))
.accounts({
user: userPublicKey,
vault: vaultPda,
vaultState: vaultStatePda,
systemProgram: SystemProgram.programId,
})
.rpc();const withdrawAmount = 0.5 * LAMPORTS_PER_SOL; // 0.5 SOL
await program.methods
.withdraw(new BN(withdrawAmount))
.accounts({
user: userPublicKey,
vault: vaultPda,
vaultState: vaultStatePda,
systemProgram: SystemProgram.programId,
})
.rpc();await program.methods
.close()
.accounts({
user: userPublicKey,
vault: vaultPda,
vaultState: vaultStatePda,
systemProgram: SystemProgram.programId,
})
.rpc();anchor_vault/
├── programs/
│ └── anchor_vault/
│ └── src/
│ └── lib.rs # Main program logic
├── tests/
│ └── anchor_vault.ts # Test suite
├── migrations/
│ └── deploy.ts # Deployment script
├── Anchor.toml # Anchor configuration
├── Cargo.toml # Rust workspace configuration
└── package.json # Node.js dependencies
6RpYNZhk25mktpRowY71JzGsyQtRZTbxPN4n2FE1ga8w
- The vault uses PDA-based ownership, ensuring only the user who initialized the vault can interact with it
- Rent-exempt balance is automatically funded during initialization
- All operations require the user's signature
- The close instruction transfers all remaining lamports back to the user
yarn lintyarn lint:fixISC
Contributions are welcome! Please feel free to submit a Pull Request.