tribewarez-vault
Vault & Escrow - Secure token storage, time-locked savings, and conditional escrow agreements for Tribewarez DeFi.
Part of the Tribewarez programs workspace and PoT-O ecosystem.
Overview
The tribewarez-vault program provides secure token storage and escrow functionality on Solana. It enables users to:
- Create personal vaults - Private token storage accounts with optional time-locks
- Time-locked savings - Lock tokens until a specified timestamp
- Escrow agreements - Two-party conditional token release
- Treasury management - Centralized vault administration with activity tracking
- Lock extension - Extend existing vault lock periods (cannot reduce)
This program works alongside the PoT-O mining, staking, and swap programs to provide a complete DeFi ecosystem.
Key Features
π Time-Locked Vaults
Deposit tokens and set a lock period. Tokens cannot be withdrawn until the lock expires, enforcing commitment to long-term holdings.
π€ Conditional Escrow
Create escrow agreements where tokens are held by the program until a release condition is met (time-based). Either party can interact with the escrow state.
πΌ Treasury Management
Central treasury account tracks aggregate vault statistics:
- Total tokens deposited across all vaults
- Number of active vaults
- Admin controls for emergency situations
π Authority-Based Control
Each vault and escrow has clear ownership and authorization rules. Only authorized parties can trigger state changes.
β° Temporal Enforcement
All lock periods enforced using Solana's Clock sysvar for consistent, tamper-proof time tracking.
Architecture Overview
ββββββββββββββββββββββββββββββββββββββββββββββββββββ
β User Initiates Vault / Escrow Action β
ββββββββββββββββββββ¬ββββββββββββββββββββββββββββββββ
β
ββββββββββββ΄βββββββββββββ
β β
βΌ βΌ
βββββββββββββββββ ββββββββββββββββββββ
β Create Vault β β Create Escrow β
β β β β
β - Set owner β β - Set depositor β
β - Set lock β β - Set beneficiaryβ
β - Init state β β - Set release β
βββββββββ¬ββββββββ β time β
β ββββββββββ¬ββββββββββ
β β
βΌ βΌ
βββββββββββββββββ ββββββββββββββββββββ
β Deposit β β Release Escrow β
β β β (after time) β
β - Transfer in β β β
β - Update bal β β - Check time β
β - Log event β β - Transfer out β
βββββββββ¬ββββββββ β - Mark released β
β ββββββββββ¬ββββββββββ
β β
ββββββββββββ¬βββββββββββββ
β
βΌ
ββββββββββββββββββββ
β Withdraw β
β β
β - Check lock β
β - Transfer out β
β - Update stats β
ββββββββββββββββββββ
Data Structures
Treasury
Central management account for all vaults and escrows.
UserVault
Individual time-locked vault for a user.
Escrow
Conditional two-party token release agreement.
Instructions
1. Initialize Treasury
Set up the main treasury account (admin-only operation).
Accounts:
authority(signer) - Treasury admintreasury(write) - New treasury PDAtoken_mint(read) - Token to managevault_token_account(write) - Token account for holding fundstoken_program(read) - SPL Token programsystem_program(read) - System programrent(read) - Rent sysvar
Parameters:
treasury_bump: u8- PDA bump seed
2. Create Vault
Create a new time-locked vault for the caller.
Accounts:
user(signer) - Vault ownertreasury(write) - Parent treasuryuser_vault(write) - New vault PDAsystem_program(read) - System program
Parameters:
vault_name: String- Name (max 32 chars)lock_until: i64- Unix timestamp (0 for no lock)
3. Deposit
Deposit tokens into a vault.
Accounts:
user(signer) - Token ownertreasury(write) - Tracks total depositsuser_vault(write) - Destination vaultuser_token_account(read) - Source of tokensvault_token_account(write) - Treasury holding accounttoken_program(read) - SPL Token program
Parameters:
amount: u64- Amount to deposit
4. Withdraw
Withdraw tokens from a vault (if lock period has expired).
Accounts:
user(signer) - Vault ownertreasury(read) - For lock validationuser_vault(write) - Source vaultuser_token_account(write) - Destinationvault_token_account(write) - Treasury accounttoken_program(read) - SPL Token program
Parameters:
amount: u64- Amount to withdraw
5. Create Escrow
Create a conditional escrow agreement between two parties.
Accounts:
depositor(signer) - Party funding escrowbeneficiary(read) - Party receiving tokenstoken_mint(read) - Token typeescrow(write) - New escrow PDAescrow_token_account(write) - Escrow holding accountdepositor_token_account(read) - Token sourcetoken_program(read) - SPL Token programsystem_program(read) - System programrent(read) - Rent sysvar
Parameters:
amount: u64- Escrow amountrelease_time: i64- Unix timestamp for releaseescrow_bump: u8- PDA bump seed
6. Release Escrow
Release escrowed tokens to beneficiary (after release time passes).
Accounts:
caller(signer) - Anyone can trigger releaseescrow(write) - Escrow being releasedescrow_token_account(write) - Token sourcebeneficiary_token_account(write) - Destinationtoken_program(read) - SPL Token program
Parameters: None
7. Cancel Escrow
Cancel escrow and return tokens to depositor (before release time only).
Accounts:
depositor(signer) - Original funderescrow(write) - Escrow being cancelledescrow_token_account(write) - Token sourcedepositor_token_account(write) - Destinationtoken_program(read) - SPL Token program
Parameters: None
8. Extend Lock
Extend the lock period of an existing vault (can only increase lock time).
Accounts:
user(signer) - Vault owneruser_vault(write) - Vault to update
Parameters:
new_lock_until: i64- New Unix timestamp (must be > current)
Configuration
Vault Constraints
pub const MAX_VAULT_NAME_LENGTH: usize = 32;
pub const MIN_LOCK_TIMESTAMP: i64 = 0; // 0 = no lock
Program Constants
pub const TREASURY_SEED: & = b"treasury";
pub const USER_VAULT_SEED: & = b"user_vault";
pub const ESCROW_SEED: & = b"escrow";
Usage Examples
Create and Deposit into a Vault
// 1. Create a time-locked vault (locks until timestamp)
let lock_until = get?.unix_timestamp + ; // 30 days
create_vault?;
// 2. Deposit tokens into the vault
deposit?;
// Tokens locked until lock_until timestamp
Create and Release Escrow
// 1. Depositor creates escrow agreement
let release_time = get?.unix_timestamp + ; // 7 days
create_escrow?;
// 2. After release time, anyone can trigger release
release_escrow?;
// Beneficiary receives tokens
Extend Vault Lock
let new_lock_time = get?.unix_timestamp + ; // 60 days
extend_lock?;
// Cannot reduce lock time - always increases
Testing
Run tests locally:
# Build and test
# Test from workspace root
# Test with logging
RUST_LOG=debug
Test coverage includes:
- Treasury initialization and state validation
- Vault creation with various lock times
- Deposit and withdrawal flows
- Lock period enforcement
- Escrow creation, release, and cancellation
- Error handling for invalid operations
- Boundary conditions (lock time validation)
Security Considerations
β Time-Lock Enforcement
Solana's Clock sysvar ensures tamper-proof timestamp enforcement. Withdrawals are impossible before the lock period expires.
β Ownership Validation
Each vault and escrow has clear ownership. Only authorized accounts can modify state:
- Vault withdrawals require owner signature
- Escrow cancellation requires depositor signature
- Escrow release can be triggered by anyone (time-based)
β SPL Token Safety
All token transfers use CPI to the Token program. No direct token account manipulation.
β PDA Signer Pattern
Treasury and escrow accounts sign token transfers using their PDA seeds. Prevents unauthorized transfers.
β State Consistency
Vault and escrow state are updated atomically with token transfers. No partial state updates.
β Overflow Protection
All arithmetic checked with .checked_add() and .checked_sub() to prevent overflow/underflow.
Performance Characteristics
- Create Vault: ~0.3 CUs - Account initialization only
- Deposit: ~1 CU - Token transfer + state update
- Withdraw: ~1-1.5 CUs - Time check + token transfer
- Create Escrow: ~1.5 CUs - Account creation + transfer
- Release Escrow: ~1 CU - Time check + transfer
- Cancel Escrow: ~1 CU - Time check + transfer
- Extend Lock: ~0.2 CUs - Arithmetic only
Total compute usage is minimal, well under 1.4M unit limits.
Integration with Other Programs
With tribewarez-pot-o
Miners lock up rewards in vaults to participate in governance. Time-locks enforce commitment periods.
With tribewarez-staking
Staked tokens can be locked in vaults. Escrow used for conditional reward distributions to stakers.
With tribewarez-swap
Swap program integrates with vault for conditional token swaps. Escrowed swaps prevent frontrunning.
Deployment
See DEPLOYMENT_GUIDE.md for detailed devnet/testnet deployment instructions.
Quick deployment from workspace root:
API Documentation
Complete API documentation is available on docs.rs.
For trait-based service integration, see SERVICE_API_REFERENCE.md.
Contributing
Contributions welcome! Please follow:
- Conventional commit messages (feat:, fix:, docs:, test:)
- 80%+ test coverage for new code
- Clear documentation for public APIs
- Security audit for state-modifying code
License
MIT - See LICENSE for details.
Related Programs
- tribewarez-pot-o - PoT-O mining program
- tribewarez-staking - Staking pool management
- tribewarez-swap - AMM token swaps
- pot-o-validator - Off-chain validator daemon