tribewarez-staking 0.3.2

PTtC Staking 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
# tribewarez-staking

**PTtC Staking Program** - Flexible staking pools with configurable rewards, automated distribution, and multi-token support for Solana DeFi.

[![Crates.io](https://img.shields.io/crates/v/tribewarez-staking.svg)](https://crates.io/crates/tribewarez-staking)
[![docs.rs](https://docs.rs/tribewarez-staking/badge.svg)](https://docs.rs/tribewarez-staking)
[![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-staking` program implements flexible staking pools on Solana, enabling users to:

- **Stake tokens** - Deposit PTtC or other tokens into liquidity pools
- **Earn rewards** - Automatic reward accumulation based on stake amount and duration
- **Configure pools** - Create custom pools with different reward rates (APY)
- **Compound rewards** - Reinvest rewards or claim separately
- **Multi-token support** - Accept any token as staked or reward currency

This is a core component of the PoT-O DeFi ecosystem, working with mining, vaults, and swaps to create a complete platform.

---

## Key Features

### 🏦 Flexible Staking Pools
Create pools with custom configurations:
- **Staked Token**: Any SPL token (PTtC, NMTC, PPTC, or others)
- **Reward Token**: Any SPL token (can be same or different from staked token)
- **APY Rate**: Configurable annual percentage yield
- **Pool Size Limits**: Optional cap on total staked amount

### 💰 Configurable Rewards
Flexible reward mechanisms:
- **Fixed APY**: Static rewards based on percentage
- **Compound vs Simple**: Choose auto-compounding or manual claims
- **Reward Decay**: Optional decreasing rewards over time
- **Lock Periods**: Optional time-lock for early withdrawal penalties

### 📊 Automated Distribution
Smart reward calculations:
- Rewards accrue per slot (Solana block time ~400ms)
- Automatic calculations based on stake duration
- Pending rewards tracked per staker
- One-click claim with atomic token transfer

### 🔐 Governance Ready
Pools can be managed by DAOs:
- Admin authority for pool configuration
- Multi-signature authority support
- Governance token participation (stake governance tokens for voting weight)

### 🔄 Integration Ready
Works seamlessly with tribewarez-pot-o for mining rewards and tribewarez-swap for liquidity.

---

## Architecture Overview

```
User Deposits Tokens
┌─────────────────────────┐
│  StakingPool Account    │ (on-chain PDA)
│ - Total staked amount   │
│ - Reward rate (APY)     │
│ - Reward token mint     │
└─────────────────────────┘
┌─────────────────────────┐
│  StakeAccount           │ (per-user PDA)
│ - Staked amount         │
│ - Entry timestamp       │
│ - Pending rewards       │
└─────────────────────────┘
Rewards Accrue Each Slot
┌─────────────────────────┐
│  User Claims Rewards    │
│ - Verify eligibility    │
│ - Calculate pending     │
│ - Transfer tokens       │
│ - Update account state  │
└─────────────────────────┘
```

---

## Data Structures

### StakingPool
Represents a single staking pool configuration and statistics.

```rust
pub struct StakingPool {
    pub token_mint: Pubkey,            // Token being staked
    pub reward_mint: Pubkey,           // Reward token distributed
    pub pool_token_account: Pubkey,    // Vault holding staked tokens
    pub reward_token_account: Pubkey,  // Vault holding rewards
    pub reward_rate: u64,              // APY as basis points (e.g., 500 = 5%)
    pub total_staked: u64,             // Total staked in pool
    pub total_rewards_distributed: u64,// Lifetime rewards
    pub authority: Pubkey,             // Pool admin authority
    pub pool_bump: u8,                 // PDA bump for pool
    pub is_active: bool,               // Active/paused status
}
```

### StakeAccount
Tracks individual user's stake and accumulated rewards.

```rust
pub struct StakeAccount {
    pub owner: Pubkey,                 // Token owner
    pub pool: Pubkey,                  // Pool account
    pub amount: u64,                   // Current staked amount
    pub entry_timestamp: i64,          // When staked
    pub last_claim_timestamp: i64,     // Last reward claim
    pub pending_rewards: u64,          // Unclaimed rewards
    pub total_rewards_claimed: u64,    // Lifetime claimed
    pub stake_bump: u8,                // PDA bump for stake
}
```

---

## Instructions

### 1. Initialize Pool
Create a new staking pool with custom configuration.

**Accounts**:
- `staking_pool` (write) - New pool PDA
- `token_mint` (read) - Token being staked
- `reward_mint` (read) - Reward token
- `pool_token_account` (write) - Vault for staked tokens
- `reward_token_account` (write) - Vault for rewards
- `authority` (signer) - Pool admin
- `payer` (signer) - Transaction payer

**Parameters**:
- `reward_rate: u64` - APY in basis points (e.g., 500 = 5%)

### 2. Create Stake Account
Register as a staker in a pool.

**Accounts**:
- `stake_account` (write) - New stake PDA
- `staking_pool` (read) - Pool to join
- `owner` (signer) - Token owner
- `payer` (signer) - Transaction payer

**Parameters**: None

### 3. Stake Tokens
Deposit tokens into the pool.

**Accounts**:
- `stake_account` (write) - User's stake account
- `staking_pool` (write) - Pool being staked in
- `user_token_account` (write) - User's token account
- `pool_token_account` (write) - Pool's vault
- `token_mint` (read) - Token mint
- `owner` (signer) - Token owner

**Parameters**:
- `amount: u64` - Tokens to stake

### 4. Claim Rewards
Claim accumulated rewards.

**Accounts**:
- `stake_account` (write) - User's stake account
- `staking_pool` (write) - Pool account
- `user_reward_account` (write) - User's reward account
- `pool_reward_account` (write) - Pool's reward vault
- `reward_mint` (read) - Reward token mint
- `owner` (signer) - Reward owner

**Parameters**: None

### 5. Unstake Tokens
Withdraw staked tokens.

**Accounts**:
- `stake_account` (write) - User's stake account
- `staking_pool` (write) - Pool account
- `user_token_account` (write) - User's token account
- `pool_token_account` (write) - Pool's vault
- `token_mint` (read) - Token mint
- `owner` (signer) - Token owner

**Parameters**:
- `amount: u64` - Tokens to unstake

### 6. Update Pool
Update pool configuration (admin only).

**Accounts**:
- `staking_pool` (write) - Pool to update
- `authority` (signer) - Pool authority

**Parameters**:
- `reward_rate: u64` - New APY (basis points)
- `is_active: bool` - Pool active status

---

## Configuration

### Reward Calculation
Rewards are calculated per block as:

```
pending_reward = (staked_amount * reward_rate * blocks_elapsed) / (blocks_per_year * 10000)

Example:
- Stake: 1000 PTtC
- APY: 5% (500 basis points)
- Blocks: 2.5M per year (~400ms slots)
- After 1 year: 50 PTtC

Daily rate:
- 1000 * 0.05 / 365 = 0.137 PTtC per day
```

### Key Parameters
```rust
pub const BLOCKS_PER_YEAR: u64 = 2_628_000;     // ~365 days * blocks/day
pub const BASIS_POINTS_DIVISOR: u64 = 10_000;  // For percentage calculation
pub const MIN_REWARD_RATE: u64 = 0;            // 0% minimum
pub const MAX_REWARD_RATE: u64 = 50_000;       // 500% maximum
```

---

## Usage Examples

### Create a Staking Pool
```bash
# Admin authority creates pool with 5% APY
# reward_rate = 500 (basis points)
# Accepts PTtC, distributes PTtC or other token
```

### Join a Pool
```bash
# User stakes 1000 PTtC
# Rewards begin accruing per block
# Pending rewards visible on-chain
```

### Claim Rewards
```bash
# User claims accumulated rewards
# Tokens transferred automatically
# Stake remains in pool
# Can re-claim next epoch
```

### Unstake
```bash
# User withdraws staked tokens
# All pending rewards must be claimed first
# Stake account state cleared
```

---

## Testing

Run comprehensive tests:

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

# Test from workspace root
anchor test

# Test with specific feature
cargo test --lib test_compound_rewards -- --nocapture
```

Test coverage includes:
- Pool creation and configuration
- Stake creation and deposits
- Reward calculation and accrual
- Claim mechanics and token transfers
- Unstaking and withdrawal
- Admin pool updates
- Error conditions and boundary cases

---

## Security Considerations

### ✅ Authority Validation
Only designated authorities can:
- Create pools
- Update pool configurations
- Pause/resume pools
- Manage treasury accounts

### ✅ Precision & Overflow Protection
- Reward calculations use checked arithmetic
- No unchecked multiplication or division
- Pending rewards tracked with u128 intermediate values
- Prevents rounding errors in reward distribution

### ✅ Token Custody
- Pool vaults use program-derived accounts (PDAs)
- Only pool authority can control vault accounts
- Staked tokens isolated in vault, not accessible elsewhere
- Reward tokens verified before distribution

### ✅ Reentrancy Safety
- State updates are atomic
- No callback hooks or external calls
- Transaction fails entirely if any step fails

### ✅ Rate Limiting
- Configuration changes limited per block
- Reward rate changes bounded (no 10x increases)
- Large stakes can't cause excessive minting

---

## Performance Characteristics

- **Stake Deposit**: ~1-2 CUs - Token transfer + state update
- **Reward Claim**: ~1-2 CUs - Calculation + token transfer
- **Reward Accrual**: O(1) - Per-block calculations
- **Pool Update**: ~0.5 CU - Configuration change only

Designed for high-frequency claims without excessive compute overhead.

---

## Integration with Other Programs

### With tribewarez-pot-o
Mining rewards can be directly distributed as staking pool rewards. Miners stake their mining yields for additional returns.

### With tribewarez-swap
Stakers can trade staking rewards on AMM pools. LP shares can be staked for governance rewards.

### With tribewarez-vault
Stakers can lock up rewards in time-locked vaults for governance participation incentives.

---

## Deployment

See [DEPLOYMENT_GUIDE.md](../DEPLOYMENT_GUIDE.md) for detailed devnet/testnet deployment.

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

---

## API Documentation

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

For trait-based service integration, see [SERVICE_API_REFERENCE.md](../SERVICE_API_REFERENCE.md).

---

## Contributing

Contributions welcome! Follow:
- Conventional commit format
- 80%+ test coverage for new code
- Clear public API documentation
- Audit all arithmetic operations

---

## License

MIT - See [LICENSE](LICENSE) for details.

---

## Related Programs

- [tribewarez-pot-o]../tribewarez-pot-o - Mining and proof validation
- [tribewarez-vault]../tribewarez-vault - Escrow and treasury
- [tribewarez-swap]../tribewarez-swap - AMM token swaps
- [pot-o-validator]../../pot-o-validator - Off-chain validator

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