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).
- 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.
- 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]
- 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.
-
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/test.ts
demonstrates the full lifecycle:
-
Initialize Vault:
- Derives PDAs for the user.
- Calls
initialize
to create the vault state and vault accounts.
-
Deposit SOL:
- Calls
deposit
to transfer SOL from the user to the vault PDA. - Checks vault balance after deposit.
- Calls
-
Withdraw SOL:
- Calls
withdraw
to transfer SOL from the vault PDA back to the user. - Checks vault balance after withdrawal.
- Calls
-
Close Vault:
- Calls
close
to transfer all remaining SOL and close the PDAs. - Asserts that the vault state account is closed.
- Calls
programs/vault/src/lib.rs
— Anchor program logic (Rust)tests/test.ts
— TypeScript tests using Anchor clientAnchor.toml
— Anchor configurationpackage.json
,tsconfig.json
— TypeScript and test setup
- Solana CLI
- Anchor CLI
- Node.js, Yarn or npm
- 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.