anya-core 1.2.0

Enterprise-grade Bitcoin Infrastructure Platform
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
use std::error::Error;
// Anya Governance Token (AGT) Contract Implementation
// [AIR-3][AIS-3][AIT-3][AIP-3][BPC-3][DAO-3]

use std::collections::HashMap;
use std::sync::{Arc, Mutex};

use bitcoin::hashes::sha256;
use clarity_repl::clarity::types::{PrincipalData, QualifiedContractIdentifier};
use serde::{Deserialize, Serialize};
use tokio::sync::RwLock;
use tracing::{debug, info, warn};

use crate::blockchain::common::{BlockHeight, TokenAmount};
use crate::dao::governance::{GovernanceError, ProposalId};
use crate::dao::types::{Distribution, EmissionSchedule, TokenDistribution, VestingSchedule};
use crate::error::Error;
use crate::metrics::MetricsManager;
use crate::storage::Storage;

/// Token constants
pub const TOKEN_NAME: &str = "Anya Governance Token";
pub const TOKEN_SYMBOL: &str = "AGT";
pub const TOKEN_DECIMALS: u8 = 8;
pub const TOTAL_SUPPLY: u64 = 21_000_000_000_00000000; // 21 billion with 8 decimals

/// Updated emission constants for adaptive schedule
pub const INITIAL_BLOCK_REWARD: u64 = 10_000_00000000; // 10,000 tokens per block
pub const MIN_HALVING_INTERVAL: u64 = 105_000; // Minimum blocks between halvings
pub const DEFAULT_HALVING_INTERVAL: u64 = 105_000; // Default interval (adjustable)
pub const RESERVE_RATIO_THRESHOLD: f64 = 0.15; // 15% reserve requirement

/// Updated distribution percentages
pub const TREASURY_PERCENTAGE: u64 = 35; // 35% to Protocol Treasury
pub const LIQUIDITY_PERCENTAGE: u64 = 25; // 25% to Liquidity Provision
pub const TEAM_PERCENTAGE: u64 = 20; // 20% to Team & Development
pub const COMMUNITY_PERCENTAGE: u64 = 15; // 15% to Community Incentives
pub const PARTNERS_PERCENTAGE: u64 = 5; // 5% to Strategic Partners

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TokenConfig {
    pub name: String,
    pub symbol: String,
    pub decimals: u8,
    pub total_supply: TokenAmount,
    pub initial_block_reward: TokenAmount,
    pub halving_interval: BlockHeight,
    pub min_halving_interval: BlockHeight,
    pub reserve_ratio: f64,
    pub distribution: TokenDistribution,
}

impl Default for TokenConfig {
    fn default() -> Self {
        Self {
            name: TOKEN_NAME.to_string(),
            symbol: TOKEN_SYMBOL.to_string(),
            decimals: TOKEN_DECIMALS,
            total_supply: TokenAmount(TOTAL_SUPPLY),
            initial_block_reward: TokenAmount(INITIAL_BLOCK_REWARD),
            halving_interval: DEFAULT_HALVING_INTERVAL,
            min_halving_interval: MIN_HALVING_INTERVAL,
            reserve_ratio: RESERVE_RATIO_THRESHOLD,
            distribution: TokenDistribution {
                treasury: Distribution {
                    percentage: TREASURY_PERCENTAGE,
                    vesting: VestingSchedule::new_with_cliff(0, 48, 20),
                    allocation: TokenAmount(TOTAL_SUPPLY * TREASURY_PERCENTAGE / 100),
                },
                liquidity: Distribution {
                    percentage: LIQUIDITY_PERCENTAGE,
                    vesting: VestingSchedule::new_linear(0, 18, 50),
                    allocation: TokenAmount(TOTAL_SUPPLY * LIQUIDITY_PERCENTAGE / 100),
                },
                team: Distribution {
                    percentage: TEAM_PERCENTAGE,
                    vesting: VestingSchedule::new_with_cliff(12, 36, 0),
                    allocation: TokenAmount(TOTAL_SUPPLY * TEAM_PERCENTAGE / 100),
                },
                community: Distribution {
                    percentage: COMMUNITY_PERCENTAGE,
                    vesting: VestingSchedule::new_linear(0, 48, 10),
                    allocation: TokenAmount(TOTAL_SUPPLY * COMMUNITY_PERCENTAGE / 100),
                },
                partners: Distribution {
                    percentage: PARTNERS_PERCENTAGE,
                    vesting: VestingSchedule::new_linear(0, 36, 10),
                    allocation: TokenAmount(TOTAL_SUPPLY * PARTNERS_PERCENTAGE / 100),
                },
            },
        }
    }
}

pub struct TokenContract {
    config: Arc<RwLock<TokenConfig>>,
    balances: Arc<RwLock<HashMap<PrincipalData, TokenAmount>>>,
    allowances: Arc<RwLock<HashMap<(PrincipalData, PrincipalData), TokenAmount>>>,
    total_minted: Arc<RwLock<TokenAmount>>,
    total_burned: Arc<RwLock<TokenAmount>>,
    last_halving_block: Arc<RwLock<BlockHeight>>,
    current_block_reward: Arc<RwLock<TokenAmount>>,
    contract_id: QualifiedContractIdentifier,
    storage: Arc<dyn Storage>,
    metrics: Arc<MetricsManager>,
    treasury_controller: Arc<Mutex<TreasuryController>>,
    emission_schedule: Arc<RwLock<EmissionSchedule>>,
}

impl TokenContract {
    pub async fn new(
        storage: Arc<dyn Storage>,
        metrics: Arc<MetricsManager>,
        contract_id: QualifiedContractIdentifier,
    ) -> Result<Self, Error> {
        let config = Arc::new(RwLock::new(TokenConfig::default()));
        let balances = Arc::new(RwLock::new(HashMap::new()));
        let allowances = Arc::new(RwLock::new(HashMap::new()));
        let total_minted = Arc::new(RwLock::new(TokenAmount(0)));
        let total_burned = Arc::new(RwLock::new(TokenAmount(0)));
        let last_halving_block = Arc::new(RwLock::new(0));
        let current_block_reward = Arc::new(RwLock::new(TokenAmount(INITIAL_BLOCK_REWARD)));
        
        // Initialize emission schedule with adaptive halving
        let emission_schedule = EmissionSchedule::new(
            INITIAL_BLOCK_REWARD,
            DEFAULT_HALVING_INTERVAL,
            MIN_HALVING_INTERVAL,
        );
        
        // Initialize treasury controller with the updated distribution model
        let treasury_controller = TreasuryController::new(config.clone());

        Ok(Self {
            config,
            balances,
            allowances,
            total_minted,
            total_burned,
            last_halving_block,
            current_block_reward,
            contract_id,
            storage,
            metrics,
            treasury_controller: Arc::new(Mutex::new(treasury_controller)),
            emission_schedule: Arc::new(RwLock::new(emission_schedule)),
        })
    }

    /// Initialize the token distribution according to the model
    pub async fn initialize_distribution(&self) -> Result<(), Error> {
        let config = self.config.read().await;
        let mut balances = self.balances.write().await;
        let mut total_minted = self.total_minted.write().await;
        
        // Protocol Treasury (35%)
        let treasury_principal = self.get_treasury_principal()?;
        let treasury_initial = config.distribution.treasury.initial_release();
        balances.insert(treasury_principal.clone(), treasury_initial);
        
        // Liquidity Provision (25%)
        let liquidity_principal = self.get_liquidity_principal()?;
        let liquidity_initial = config.distribution.liquidity.initial_release();
        balances.insert(liquidity_principal.clone(), liquidity_initial);
        
        // Team & Development (20%) - Initially locked
        let team_principal = self.get_team_principal()?;
        let team_initial = config.distribution.team.initial_release();
        balances.insert(team_principal.clone(), team_initial);
        
        // Community Incentives (15%)
        let community_principal = self.get_community_principal()?;
        let community_initial = config.distribution.community.initial_release();
        balances.insert(community_principal.clone(), community_initial);
        
        // Strategic Partners (5%)
        let partners_principal = self.get_partners_principal()?;
        let partners_initial = config.distribution.partners.initial_release();
        balances.insert(partners_principal.clone(), partners_initial);
        
        // Update total minted
        *total_minted = TokenAmount(
            treasury_initial.0 + 
            liquidity_initial.0 + 
            team_initial.0 + 
            community_initial.0 + 
            partners_initial.0
        );
        
        info!(
            "Token distribution initialized: Treasury={}, Liquidity={}, Team={}, Community={}, Partners={}",
            treasury_initial.0, liquidity_initial.0, team_initial.0, community_initial.0, partners_initial.0
        );
        
        Ok(())
    }
    
    /// Process mining rewards according to adaptive emission schedule
    pub async fn process_block_rewards(&self, block_height: BlockHeight) -> Result<(), Error> {
        let mut emission_schedule = self.emission_schedule.write().await;
        let mut current_reward = self.current_block_reward.write().await;
        let mut last_halving = self.last_halving_block.write().await;
        
        // Check if halving should occur
        if emission_schedule.should_halve(block_height, *last_halving) {
            // Perform halving
            *current_reward = TokenAmount(current_reward.0 / 2);
            *last_halving = block_height;
            
            info!(
                "Halving occurred at block {}: New block reward = {}",
                block_height, current_reward.0
            );
            
            // Record metrics
            self.metrics.record_halving(block_height, current_reward.0);
        }
        
        // Mint block rewards to the mining rewards address
        let mining_principal = self.get_mining_rewards_principal()?;
        self.mint_internal(mining_principal, current_reward.0).await?;
        
        Ok(())
    }
    
    /// Update emission parameters via governance
    pub async fn update_emission_parameters(
        &self,
        halving_interval: Option<BlockHeight>,
        proposal_id: ProposalId,
    ) -> Result<(), GovernanceError> {
        // Validate the proposal is approved
        self.validate_governance_action(proposal_id)?;
        
        let mut emission_schedule = self.emission_schedule.write().await;
        let mut config = self.config.write().await;
        
        if let Some(interval) = halving_interval {
            // Ensure the interval is not below the minimum
            if interval < config.min_halving_interval {
                return Err(GovernanceError::InvalidParameter(
                    format!("Halving interval cannot be less than {}", config.min_halving_interval)
                ));
            }
            
            // Update the interval
            emission_schedule.set_halving_interval(interval);
            config.halving_interval = interval;
            
            info!(
                "Emission parameters updated via proposal {}: halving_interval={}",
                proposal_id, interval
            );
        }
        
        Ok(())
    }
    
    /// Perform token buyback and burn operation
    pub async fn execute_buyback_and_burn(
        &self,
        amount: TokenAmount,
        proposal_id: ProposalId,
    ) -> Result<(), GovernanceError> {
        // Validate the proposal is approved
        self.validate_governance_action(proposal_id)?;
        
        // Get the treasury principal
        let treasury_principal = self.get_treasury_principal()?;
        
        // Burn tokens from treasury
        self.burn_internal(treasury_principal, amount.0).await
            .map_err(|e| GovernanceError::ExecutionFailed(format!("Burn failed: {}", e)))?;
        
        info!(
            "Executed buyback and burn via proposal {}: amount={}",
            proposal_id, amount.0
        );
        
        // Record the operation
        self.metrics.record_token_burn(amount.0);
        
        Ok(())
    }
    
    /// Execute treasury operation for protocol-owned liquidity
    pub async fn manage_protocol_liquidity(
        &self,
        amount: TokenAmount,
        action: LiquidityAction,
        proposal_id: ProposalId,
    ) -> Result<(), GovernanceError> {
        // Validate the proposal is approved
        self.validate_governance_action(proposal_id)?;
        
        let treasury_principal = self.get_treasury_principal()?;
        let liquidity_principal = self.get_liquidity_principal()?;
        
        match action {
            LiquidityAction::Add => {
                // Transfer tokens from treasury to liquidity pool
                self.transfer_internal(treasury_principal, liquidity_principal, amount.0).await
                    .map_err(|e| GovernanceError::ExecutionFailed(format!("Transfer failed: {}", e)))?;
                
                info!(
                    "Added {} tokens to protocol-owned liquidity via proposal {}",
                    amount.0, proposal_id
                );
            },
            LiquidityAction::Remove => {
                // Transfer tokens from liquidity pool to treasury
                self.transfer_internal(liquidity_principal, treasury_principal, amount.0).await
                    .map_err(|e| GovernanceError::ExecutionFailed(format!("Transfer failed: {}", e)))?;
                
                info!(
                    "Removed {} tokens from protocol-owned liquidity via proposal {}",
                    amount.0, proposal_id
                );
            }
        }
        
        // Update metrics
        let mut treasury_controller = self.treasury_controller.lock()
            .map_err(|_| GovernanceError::LockAcquisitionFailed)?;
            
        treasury_controller.update_liquidity_metrics(action, amount);
        
        Ok(())
    }
    
    // Internal token operations
    
    async fn mint_internal(&self, to: PrincipalData, amount: u64) -> Result<(), Error> {
        let mut balances = self.balances.write().await;
        let mut total_minted = self.total_minted.write().await;
        
        // Update recipient balance
        let current_balance = balances.get(&to).cloned().unwrap_or(TokenAmount(0));
        balances.insert(to.clone(), TokenAmount(current_balance.0 + amount));
        
        // Update total minted
        *total_minted = TokenAmount(total_minted.0 + amount);
        
        // Record the operation
        debug!("Minted {} tokens to {}", amount, to);
        self.metrics.record_token_mint(amount);
        
        Ok(())
    }
    
    async fn burn_internal(&self, from: PrincipalData, amount: u64) -> Result<(), Error> {
        let mut balances = self.balances.write().await;
        let mut total_burned = self.total_burned.write().await;
        
        // Check balance
        let current_balance = balances.get(&from).cloned().unwrap_or(TokenAmount(0));
        if current_balance.0 < amount {
            return Err(Error::InsufficientBalance);
        }
        
        // Update balance
        balances.insert(from.clone(), TokenAmount(current_balance.0 - amount));
        
        // Update total burned
        *total_burned = TokenAmount(total_burned.0 + amount);
        
        // Record the operation
        debug!("Burned {} tokens from {}", amount, from);
        self.metrics.record_token_burn(amount);
        
        Ok(())
    }
    
    async fn transfer_internal(
        &self,
        from: PrincipalData,
        to: PrincipalData,
        amount: u64,
    ) -> Result<(), Error> {
        let mut balances = self.balances.write().await;
        
        // Check balance
        let from_balance = balances.get(&from).cloned().unwrap_or(TokenAmount(0));
        if from_balance.0 < amount {
            return Err(Error::InsufficientBalance);
        }
        
        // Update balances
        balances.insert(from.clone(), TokenAmount(from_balance.0 - amount));
        
        let to_balance = balances.get(&to).cloned().unwrap_or(TokenAmount(0));
        balances.insert(to.clone(), TokenAmount(to_balance.0 + amount));
        
        // Record the operation
        debug!("Transferred {} tokens from {} to {}", amount, from, to);
        self.metrics.record_token_transfer(amount);
        
        Ok(())
    }
    
    // Helper methods for accessing standard addresses
    
    fn get_treasury_principal(&self) -> Result<PrincipalData, Error> {
        // Implementation for getting the treasury principal
        // Typically would be derived from contract_id or configuration
        Ok(PrincipalData::parse("ST2CY5V39NHDPWSXMW9QDT3HC3GD6Q6XX4CFRK9AG.treasury")?)
    }
    
    fn get_liquidity_principal(&self) -> Result<PrincipalData, Error> {
        Ok(PrincipalData::parse("ST2CY5V39NHDPWSXMW9QDT3HC3GD6Q6XX4CFRK9AG.liquidity")?)
    }
    
    fn get_team_principal(&self) -> Result<PrincipalData, Error> {
        Ok(PrincipalData::parse("ST2CY5V39NHDPWSXMW9QDT3HC3GD6Q6XX4CFRK9AG.team")?)
    }
    
    fn get_community_principal(&self) -> Result<PrincipalData, Error> {
        Ok(PrincipalData::parse("ST2CY5V39NHDPWSXMW9QDT3HC3GD6Q6XX4CFRK9AG.community")?)
    }
    
    fn get_partners_principal(&self) -> Result<PrincipalData, Error> {
        Ok(PrincipalData::parse("ST2CY5V39NHDPWSXMW9QDT3HC3GD6Q6XX4CFRK9AG.partners")?)
    }
    
    fn get_mining_rewards_principal(&self) -> Result<PrincipalData, Error> {
        Ok(PrincipalData::parse("ST2CY5V39NHDPWSXMW9QDT3HC3GD6Q6XX4CFRK9AG.mining")?)
    }
    
    fn validate_governance_action(&self, proposal_id: ProposalId) -> Result<(), GovernanceError> {
        // Implementation would check if the proposal is valid and approved
        // This is just a placeholder
        Ok(())
    }
}

// Additional types for token operations

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum LiquidityAction {
    Add,
    Remove,
}

/// Treasury controller for managing protocol-owned assets
struct TreasuryController {
    config: Arc<RwLock<TokenConfig>>,
    pol_amount: TokenAmount,  // Protocol-owned liquidity
    reserve_amount: TokenAmount, // Strategic reserves
}

impl TreasuryController {
    fn new(config: Arc<RwLock<TokenConfig>>) -> Self {
        Self {
            config,
            pol_amount: TokenAmount(0),
            reserve_amount: TokenAmount(0),
        }
    }
    
    fn update_liquidity_metrics(&mut self, action: LiquidityAction, amount: TokenAmount) {
        match action {
            LiquidityAction::Add => {
                self.pol_amount = TokenAmount(self.pol_amount.0 + amount.0);
            },
            LiquidityAction::Remove => {
                if amount.0 <= self.pol_amount.0 {
                    self.pol_amount = TokenAmount(self.pol_amount.0 - amount.0);
                }
            }
        }
    }
    
    fn update_reserve_metrics(&mut self, action: LiquidityAction, amount: TokenAmount) {
        match action {
            LiquidityAction::Add => {
                self.reserve_amount = TokenAmount(self.reserve_amount.0 + amount.0);
            },
            LiquidityAction::Remove => {
                if amount.0 <= self.reserve_amount.0 {
                    self.reserve_amount = TokenAmount(self.reserve_amount.0 - amount.0);
                }
            }
        }
    }
    
    async fn check_reserve_ratio(&self, circulating_supply: TokenAmount) -> bool {
        let config = match self.config.read().await {
            Ok(config) => config,
            Err(_) => return false,
        };
        
        let required_reserves = TokenAmount((circulating_supply.0 as f64 * config.reserve_ratio) as u64);
        self.reserve_amount.0 >= required_reserves.0
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    
    // Test the updated tokenomics model
    #[tokio::test]
    async fn test_token_distribution() {
        // Initialize test environment
        // ...
        
        // Verify distribution percentages
        assert_eq!(TREASURY_PERCENTAGE, 35);
        assert_eq!(LIQUIDITY_PERCENTAGE, 25);
        assert_eq!(TEAM_PERCENTAGE, 20);
        assert_eq!(COMMUNITY_PERCENTAGE, 15);
        assert_eq!(PARTNERS_PERCENTAGE, 5);
        
        // Verify total equals 100%
        assert_eq!(
            TREASURY_PERCENTAGE + LIQUIDITY_PERCENTAGE + TEAM_PERCENTAGE + 
            COMMUNITY_PERCENTAGE + PARTNERS_PERCENTAGE,
            100
        );
        
        // Test initial block reward
        assert_eq!(INITIAL_BLOCK_REWARD, 10_000_00000000);
        
        // Test minimum halving interval
        assert_eq!(MIN_HALVING_INTERVAL, 105_000);
    }
}