tribewarez-vault 0.3.2

PTtC Vault/Escrow Program for Tribewarez DeFi with tensor network support
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
# tribewarez-vault

**Vault & Escrow** - Secure token storage, time-locked savings, and conditional escrow agreements for Tribewarez DeFi.

[![Crates.io](https://img.shields.io/crates/v/tribewarez-vault.svg)](https://crates.io/crates/tribewarez-vault)
[![docs.rs](https://docs.rs/tribewarez-vault/badge.svg)](https://docs.rs/tribewarez-vault)
[![License: MIT](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)

Part of the [Tribewarez programs](../README.md) workspace and [PoT-O ecosystem](https://github.com/tribewarez).

---

## 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.

```rust
pub struct Treasury {
    pub authority: Pubkey,           // Admin authority
    pub token_mint: Pubkey,          // Token this treasury manages
    pub vault_token_account: Pubkey, // Account holding all tokens
    pub total_deposited: u64,        // Sum of all deposits
    pub total_vaults: u64,           // Count of active vaults
    pub bump: u8,                    // PDA bump seed
    pub is_active: bool,             // Accept new deposits
    pub created_at: i64,             // Creation timestamp
}
```

### UserVault
Individual time-locked vault for a user.

```rust
pub struct UserVault {
    pub owner: Pubkey,          // Vault owner
    pub treasury: Pubkey,       // Parent treasury
    pub name: String,           // Human-readable name (≀32 chars)
    pub balance: u64,           // Current token balance
    pub lock_until: i64,        // Unix timestamp when withdrawable
    pub created_at: i64,        // Creation timestamp
    pub last_activity: i64,     // Last deposit/withdrawal time
    pub is_locked: bool,        // Currently locked
    pub total_deposited: u64,   // Lifetime deposit sum
    pub total_withdrawn: u64,   // Lifetime withdrawal sum
}
```

### Escrow
Conditional two-party token release agreement.

```rust
pub struct Escrow {
    pub depositor: Pubkey,           // Party funding escrow
    pub beneficiary: Pubkey,         // Party receiving tokens
    pub token_mint: Pubkey,          // Token being escrowed
    pub escrow_token_account: Pubkey,// Account holding tokens
    pub amount: u64,                 // Escrowed amount
    pub release_time: i64,           // Unix timestamp for release
    pub created_at: i64,             // Creation timestamp
    pub is_released: bool,           // Released state
    pub is_cancelled: bool,          // Cancelled state
    pub bump: u8,                    // PDA bump seed
}
```

---

## Instructions

### 1. Initialize Treasury
Set up the main treasury account (admin-only operation).

**Accounts**:
- `authority` (signer) - Treasury admin
- `treasury` (write) - New treasury PDA
- `token_mint` (read) - Token to manage
- `vault_token_account` (write) - Token account for holding funds
- `token_program` (read) - SPL Token program
- `system_program` (read) - System program
- `rent` (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 owner
- `treasury` (write) - Parent treasury
- `user_vault` (write) - New vault PDA
- `system_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 owner
- `treasury` (write) - Tracks total deposits
- `user_vault` (write) - Destination vault
- `user_token_account` (read) - Source of tokens
- `vault_token_account` (write) - Treasury holding account
- `token_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 owner
- `treasury` (read) - For lock validation
- `user_vault` (write) - Source vault
- `user_token_account` (write) - Destination
- `vault_token_account` (write) - Treasury account
- `token_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 escrow
- `beneficiary` (read) - Party receiving tokens
- `token_mint` (read) - Token type
- `escrow` (write) - New escrow PDA
- `escrow_token_account` (write) - Escrow holding account
- `depositor_token_account` (read) - Token source
- `token_program` (read) - SPL Token program
- `system_program` (read) - System program
- `rent` (read) - Rent sysvar

**Parameters**:
- `amount: u64` - Escrow amount
- `release_time: i64` - Unix timestamp for release
- `escrow_bump: u8` - PDA bump seed

### 6. Release Escrow
Release escrowed tokens to beneficiary (after release time passes).

**Accounts**:
- `caller` (signer) - Anyone can trigger release
- `escrow` (write) - Escrow being released
- `escrow_token_account` (write) - Token source
- `beneficiary_token_account` (write) - Destination
- `token_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 funder
- `escrow` (write) - Escrow being cancelled
- `escrow_token_account` (write) - Token source
- `depositor_token_account` (write) - Destination
- `token_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 owner
- `user_vault` (write) - Vault to update

**Parameters**:
- `new_lock_until: i64` - New Unix timestamp (must be > current)

---

## Configuration

### Vault Constraints
```rust
pub const MAX_VAULT_NAME_LENGTH: usize = 32;
pub const MIN_LOCK_TIMESTAMP: i64 = 0;     // 0 = no lock
```

### Program Constants
```rust
pub const TREASURY_SEED: &[u8] = b"treasury";
pub const USER_VAULT_SEED: &[u8] = b"user_vault";
pub const ESCROW_SEED: &[u8] = b"escrow";
```

---

## Usage Examples

### Create and Deposit into a Vault
```rust
// 1. Create a time-locked vault (locks until timestamp)
let lock_until = Clock::get()?.unix_timestamp + (30 * 24 * 60 * 60); // 30 days
instruction::create_vault(
    ctx,
    "My Savings".to_string(),
    lock_until,
)?;

// 2. Deposit tokens into the vault
instruction::deposit(ctx, 1_000_000)?;

// Tokens locked until lock_until timestamp
```

### Create and Release Escrow
```rust
// 1. Depositor creates escrow agreement
let release_time = Clock::get()?.unix_timestamp + (7 * 24 * 60 * 60); // 7 days
instruction::create_escrow(
    ctx,
    500_000,    // amount
    release_time,
    escrow_bump,
)?;

// 2. After release time, anyone can trigger release
instruction::release_escrow(ctx)?;

// Beneficiary receives tokens
```

### Extend Vault Lock
```rust
let new_lock_time = Clock::get()?.unix_timestamp + (60 * 24 * 60 * 60); // 60 days
instruction::extend_lock(ctx, new_lock_time)?;

// Cannot reduce lock time - always increases
```

---

## Testing

Run tests locally:

```bash
# Build and test
cargo test --lib

# Test from workspace root
anchor test

# Test with logging
RUST_LOG=debug cargo test -- --nocapture
```

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](../DEPLOYMENT_GUIDE.md) for detailed devnet/testnet deployment instructions.

Quick deployment from workspace root:
```bash
cd pot-o-contractz
anchor build -p tribewarez_vault
anchor deploy --provider.cluster devnet
```

---

## API Documentation

Complete API documentation is available on [docs.rs](https://docs.rs/tribewarez-vault).

For trait-based service integration, see [SERVICE_API_REFERENCE.md](../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](LICENSE) for details.

---

## Related Programs

- [tribewarez-pot-o]../tribewarez-pot-o - PoT-O mining program
- [tribewarez-staking]../tribewarez-staking - Staking pool management
- [tribewarez-swap]../tribewarez-swap - AMM token swaps
- [pot-o-validator]../../pot-o-validator - Off-chain validator daemon

## Resources

- [GitHub Repository]https://github.com/tribewarez/tribe
- [Anchor Documentation]https://docs.anchor-lang.com
- [Solana Program Library]https://spl.solana.com
- [PoT-O Whitepaper]https://tribewarez.com/pot-o