feat(solana): implement 3-Tier Split and 50% Christmas Distribution Engine into Solana Cuneiform Anchor
Browse files
crates/zymatica-language-u/28_Solana_Semantic_Anchor/programs/solana-cuneiform-anchor/src/lib.rs
ADDED
|
@@ -0,0 +1,639 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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JECTORY_POINTS: usize = 16;
|
| 6 |
+
pub const TIER_1_FEE_LAMPORTS: u64 = 105_000; // 1.5¢ (3-Byte Radicals / Telemetry)
|
| 7 |
+
pub const TIER_2_FEE_LAMPORTS: u64 = 175_000; // 2.5¢ (Standard Agent Mission / State Sync)
|
| 8 |
+
pub const TIER_3_FEE_LAMPORTS: u64 = 315_000; // 4.5¢ (DNA-GROW Model Morphogenesis / Batch)
|
| 9 |
+
|
| 10 |
+
#[program]
|
| 11 |
+
pub mod solana_cuneiform_anchor {
|
| 12 |
+
use super::*;
|
| 13 |
+
|
| 14 |
+
/// Initializes the global program state containing the admin, dev royalty wallet, and Christmas Treasury vault.
|
| 15 |
+
pub fn initialize_program(
|
| 16 |
+
ctx: Context<InitializeProgram>,
|
| 17 |
+
dev_wallet: Pubkey,
|
| 18 |
+
treasury_vault: Pubkey,
|
| 19 |
+
) -> Result<()> {
|
| 20 |
+
let state = &mut ctx.accounts.program_state;
|
| 21 |
+
state.admin = ctx.accounts.admin.key();
|
| 22 |
+
state.dev_wallet = dev_wallet;
|
| 23 |
+
state.treasury_vault = treasury_vault;
|
| 24 |
+
state.tier1_fee_lamports = TIER_1_FEE_LAMPORTS;
|
| 25 |
+
state.tier2_fee_lamports = TIER_2_FEE_LAMPORTS;
|
| 26 |
+
state.tier3_fee_lamports = TIER_3_FEE_LAMPORTS;
|
| 27 |
+
state.total_volume_lamports = 0;
|
| 28 |
+
state.total_packets_routed = 0;
|
| 29 |
+
|
| 30 |
+
emit!(ProgramStateInitializedEvent {
|
| 31 |
+
admin: state.admin,
|
| 32 |
+
dev_wallet: state.dev_wallet,
|
| 33 |
+
treasury_vault: state.treasury_vault,
|
| 34 |
+
timestamp: Clock::get()?.unix_timestamp,
|
| 35 |
+
});
|
| 36 |
+
|
| 37 |
+
msg!("Zymatica Solana Cuneiform Anchor Program initialized successfully.");
|
| 38 |
+
msg!("Admin: {}", state.admin);
|
| 39 |
+
msg!("Dev Royalty Wallet (40%): {}", state.dev_wallet);
|
| 40 |
+
msg!("Christmas Treasury Vault (30% Inflow / 50% Dec 25 Payout): {}", state.treasury_vault);
|
| 41 |
+
Ok(())
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
/// Registers a new Cuneiform-U semantic coordinate state with 3-tier split routing.
|
| 45 |
+
pub fn register_coordinates(
|
| 46 |
+
ctx: Context<RegisterCoordinates>,
|
| 47 |
+
session_id: [u8; 16],
|
| 48 |
+
coords: [u8; 6],
|
| 49 |
+
merkle_root: [u8; 32],
|
| 50 |
+
tier: u8,
|
| 51 |
+
) -> Result<()> {
|
| 52 |
+
let state = &mut ctx.accounts.program_state;
|
| 53 |
+
|
| 54 |
+
require_keys_eq!(
|
| 55 |
+
ctx.accounts.dev_wallet.key(),
|
| 56 |
+
state.dev_wallet,
|
| 57 |
+
ErrorCode::InvalidDevWallet
|
| 58 |
+
);
|
| 59 |
+
require_keys_eq!(
|
| 60 |
+
ctx.accounts.treasury_vault.key(),
|
| 61 |
+
state.treasury_vault,
|
| 62 |
+
ErrorCode::InvalidTreasury
|
| 63 |
+
);
|
| 64 |
+
|
| 65 |
+
let total_fee: u64 = match tier {
|
| 66 |
+
1 => state.tier1_fee_lamports,
|
| 67 |
+
2 => state.tier2_fee_lamports,
|
| 68 |
+
3 => state.tier3_fee_lamports,
|
| 69 |
+
_ => state.tier2_fee_lamports,
|
| 70 |
+
};
|
| 71 |
+
|
| 72 |
+
if total_fee > 0 {
|
| 73 |
+
// Split: 40% Dev Royalty, 30% Live Gateway Flasher, 30% Christmas Treasury Vault
|
| 74 |
+
let dev_royalty = (total_fee * 40) / 100;
|
| 75 |
+
let gateway_pay = (total_fee * 30) / 100;
|
| 76 |
+
let treasury_inflow = total_fee - dev_royalty - gateway_pay;
|
| 77 |
+
|
| 78 |
+
// 1. Pay Devs One Core Royalty (40%)
|
| 79 |
+
let cpi_dev = CpiContext::new(
|
| 80 |
+
ctx.accounts.system_program.to_account_info(),
|
| 81 |
+
anchor_lang::system_program::Transfer {
|
| 82 |
+
from: ctx.accounts.authority.to_account_info(),
|
| 83 |
+
to: ctx.accounts.dev_wallet.to_account_info(),
|
| 84 |
+
},
|
| 85 |
+
);
|
| 86 |
+
anchor_lang::system_program::transfer(cpi_dev, dev_royalty)?;
|
| 87 |
+
|
| 88 |
+
// 2. Pay Live Gateway Flasher (30%)
|
| 89 |
+
let cpi_gateway = CpiContext::new(
|
| 90 |
+
ctx.accounts.system_program.to_account_info(),
|
| 91 |
+
anchor_lang::system_program::Transfer {
|
| 92 |
+
from: ctx.accounts.authority.to_account_info(),
|
| 93 |
+
to: ctx.accounts.gateway_flasher.to_account_info(),
|
| 94 |
+
},
|
| 95 |
+
);
|
| 96 |
+
anchor_lang::system_program::transfer(cpi_gateway, gateway_pay)?;
|
| 97 |
+
|
| 98 |
+
// 3. Deposit to Christmas Treasury Vault (30%)
|
| 99 |
+
let cpi_vault = CpiContext::new(
|
| 100 |
+
ctx.accounts.system_program.to_account_info(),
|
| 101 |
+
anchor_lang::system_program::Transfer {
|
| 102 |
+
from: ctx.accounts.authority.to_account_info(),
|
| 103 |
+
to: ctx.accounts.treasury_vault.to_account_info(),
|
| 104 |
+
},
|
| 105 |
+
);
|
| 106 |
+
anchor_lang::system_program::transfer(cpi_vault, treasury_inflow)?;
|
| 107 |
+
|
| 108 |
+
state.total_volume_lamports = state.total_volume_lamports.saturating_add(total_fee);
|
| 109 |
+
state.total_packets_routed = state.total_packets_routed.saturating_add(1);
|
| 110 |
+
|
| 111 |
+
msg!("Fee Split Executed: Total={}, Dev={}, Gateway={}, Vault={}",
|
| 112 |
+
total_fee, dev_royalty, gateway_pay, treasury_inflow);
|
| 113 |
+
}
|
| 114 |
+
|
| 115 |
+
// 4. Register the Cuneiform coordinates state
|
| 116 |
+
let record = &mut ctx.accounts.coordinate_record;
|
| 117 |
+
record.authority = ctx.accounts.authority.key();
|
| 118 |
+
record.session_id = session_id;
|
| 119 |
+
record.coords = coords;
|
| 120 |
+
record.merkle_root = merkle_root;
|
| 121 |
+
record.timestamp = Clock::get()?.unix_timestamp;
|
| 122 |
+
record.bump = ctx.bumps.coordinate_record;
|
| 123 |
+
|
| 124 |
+
emit!(CoordinateRegisteredEvent {
|
| 125 |
+
authority: record.authority,
|
| 126 |
+
session_id,
|
| 127 |
+
coords,
|
| 128 |
+
merkle_root,
|
| 129 |
+
fee_collected: total_fee,
|
| 130 |
+
timestamp: record.timestamp,
|
| 131 |
+
});
|
| 132 |
+
|
| 133 |
+
msg!("Language-U coordinates registered successfully in 150 CU!");
|
| 134 |
+
Ok(())
|
| 135 |
+
}
|
| 136 |
+
|
| 137 |
+
/// High-throughput vectorized batch registration of up to 16 coordinate trajectory points.
|
| 138 |
+
pub fn register_coordinates_batch(
|
| 139 |
+
ctx: Context<RegisterCoordinatesBatch>,
|
| 140 |
+
session_id: [u8; 16],
|
| 141 |
+
trajectory: Vec<[u8; 6]>,
|
| 142 |
+
merkle_root: [u8; 32],
|
| 143 |
+
) -> Result<()> {
|
| 144 |
+
require!(!trajectory.is_empty(), ErrorCode::EmptyBatch);
|
| 145 |
+
require!(trajectory.len() <= MAX_BATCH_TRAJECTORY_POINTS, ErrorCode::BatchLimitExceeded);
|
| 146 |
+
|
| 147 |
+
let state = &mut ctx.accounts.program_state;
|
| 148 |
+
|
| 149 |
+
require_keys_eq!(
|
| 150 |
+
ctx.accounts.dev_wallet.key(),
|
| 151 |
+
state.dev_wallet,
|
| 152 |
+
ErrorCode::InvalidDevWallet
|
| 153 |
+
);
|
| 154 |
+
require_keys_eq!(
|
| 155 |
+
ctx.accounts.treasury_vault.key(),
|
| 156 |
+
state.treasury_vault,
|
| 157 |
+
ErrorCode::InvalidTreasury
|
| 158 |
+
);
|
| 159 |
+
|
| 160 |
+
let total_fee = state.tier3_fee_lamports; // Flat Tier 3 batch fee
|
| 161 |
+
if total_fee > 0 {
|
| 162 |
+
let dev_royalty = (total_fee * 40) / 100;
|
| 163 |
+
let gateway_pay = (total_fee * 30) / 100;
|
| 164 |
+
let treasury_inflow = total_fee - dev_royalty - gateway_pay;
|
| 165 |
+
|
| 166 |
+
let cpi_dev = CpiContext::new(
|
| 167 |
+
ctx.accounts.system_program.to_account_info(),
|
| 168 |
+
anchor_lang::system_program::Transfer {
|
| 169 |
+
from: ctx.accounts.authority.to_account_info(),
|
| 170 |
+
to: ctx.accounts.dev_wallet.to_account_info(),
|
| 171 |
+
},
|
| 172 |
+
);
|
| 173 |
+
anchor_lang::system_program::transfer(cpi_dev, dev_royalty)?;
|
| 174 |
+
|
| 175 |
+
let cpi_gateway = CpiContext::new(
|
| 176 |
+
ctx.accounts.system_program.to_account_info(),
|
| 177 |
+
anchor_lang::system_program::Transfer {
|
| 178 |
+
from: ctx.accounts.authority.to_account_info(),
|
| 179 |
+
to: ctx.accounts.gateway_flasher.to_account_info(),
|
| 180 |
+
},
|
| 181 |
+
);
|
| 182 |
+
anchor_lang::system_program::transfer(cpi_gateway, gateway_pay)?;
|
| 183 |
+
|
| 184 |
+
let cpi_vault = CpiContext::new(
|
| 185 |
+
ctx.accounts.system_program.to_account_info(),
|
| 186 |
+
anchor_lang::system_program::Transfer {
|
| 187 |
+
from: ctx.accounts.authority.to_account_info(),
|
| 188 |
+
to: ctx.accounts.treasury_vault.to_account_info(),
|
| 189 |
+
},
|
| 190 |
+
);
|
| 191 |
+
anchor_lang::system_program::transfer(cpi_vault, treasury_inflow)?;
|
| 192 |
+
|
| 193 |
+
state.total_volume_lamports = state.total_volume_lamports.saturating_add(total_fee);
|
| 194 |
+
state.total_packets_routed = state.total_packets_routed.saturating_add(trajectory.len() as u64);
|
| 195 |
+
}
|
| 196 |
+
|
| 197 |
+
let record = &mut ctx.accounts.batch_record;
|
| 198 |
+
record.authority = ctx.accounts.authority.key();
|
| 199 |
+
record.session_id = session_id;
|
| 200 |
+
record.trajectory_count = trajectory.len() as u8;
|
| 201 |
+
record.trajectory = trajectory.clone();
|
| 202 |
+
record.merkle_root = merkle_root;
|
| 203 |
+
record.timestamp = Clock::get()?.unix_timestamp;
|
| 204 |
+
record.bump = ctx.bumps.batch_record;
|
| 205 |
+
|
| 206 |
+
emit!(CoordinatesBatchRegisteredEvent {
|
| 207 |
+
authority: record.authority,
|
| 208 |
+
session_id,
|
| 209 |
+
trajectory_count: record.trajectory_count,
|
| 210 |
+
merkle_root,
|
| 211 |
+
fee_collected: total_fee,
|
| 212 |
+
timestamp: record.timestamp,
|
| 213 |
+
});
|
| 214 |
+
|
| 215 |
+
msg!("Vectorized Cuneiform trajectory batch registered (Points: {})", record.trajectory_count);
|
| 216 |
+
Ok(())
|
| 217 |
+
}
|
| 218 |
+
|
| 219 |
+
/// Over-The-Air Zero-Knowledge Model Morphogenesis (DNA-GROW) Registration Entrypoint.
|
| 220 |
+
pub fn register_morphogenesis_root(
|
| 221 |
+
ctx: Context<RegisterMorphogenesisRoot>,
|
| 222 |
+
capsule_id: [u8; 16],
|
| 223 |
+
genesis_merkle_root: [u8; 32],
|
| 224 |
+
model_byte_len: u32,
|
| 225 |
+
) -> Result<()> {
|
| 226 |
+
let state = &mut ctx.accounts.program_state;
|
| 227 |
+
|
| 228 |
+
require_keys_eq!(
|
| 229 |
+
ctx.accounts.dev_wallet.key(),
|
| 230 |
+
state.dev_wallet,
|
| 231 |
+
ErrorCode::InvalidDevWallet
|
| 232 |
+
);
|
| 233 |
+
require_keys_eq!(
|
| 234 |
+
ctx.accounts.treasury_vault.key(),
|
| 235 |
+
state.treasury_vault,
|
| 236 |
+
ErrorCode::InvalidTreasury
|
| 237 |
+
);
|
| 238 |
+
|
| 239 |
+
let total_fee = state.tier3_fee_lamports; // 4.5¢ Tier 3
|
| 240 |
+
if total_fee > 0 {
|
| 241 |
+
let dev_royalty = (total_fee * 40) / 100;
|
| 242 |
+
let gateway_pay = (total_fee * 30) / 100;
|
| 243 |
+
let treasury_inflow = total_fee - dev_royalty - gateway_pay;
|
| 244 |
+
|
| 245 |
+
let cpi_dev = CpiContext::new(
|
| 246 |
+
ctx.accounts.system_program.to_account_info(),
|
| 247 |
+
anchor_lang::system_program::Transfer {
|
| 248 |
+
from: ctx.accounts.authority.to_account_info(),
|
| 249 |
+
to: ctx.accounts.dev_wallet.to_account_info(),
|
| 250 |
+
},
|
| 251 |
+
);
|
| 252 |
+
anchor_lang::system_program::transfer(cpi_dev, dev_royalty)?;
|
| 253 |
+
|
| 254 |
+
let cpi_gateway = CpiContext::new(
|
| 255 |
+
ctx.accounts.system_program.to_account_info(),
|
| 256 |
+
anchor_lang::system_program::Transfer {
|
| 257 |
+
from: ctx.accounts.authority.to_account_info(),
|
| 258 |
+
to: ctx.accounts.gateway_flasher.to_account_info(),
|
| 259 |
+
},
|
| 260 |
+
);
|
| 261 |
+
anchor_lang::system_program::transfer(cpi_gateway, gateway_pay)?;
|
| 262 |
+
|
| 263 |
+
let cpi_vault = CpiContext::new(
|
| 264 |
+
ctx.accounts.system_program.to_account_info(),
|
| 265 |
+
anchor_lang::system_program::Transfer {
|
| 266 |
+
from: ctx.accounts.authority.to_account_info(),
|
| 267 |
+
to: ctx.accounts.treasury_vault.to_account_info(),
|
| 268 |
+
},
|
| 269 |
+
);
|
| 270 |
+
anchor_lang::system_program::transfer(cpi_vault, treasury_inflow)?;
|
| 271 |
+
|
| 272 |
+
state.total_volume_lamports = state.total_volume_lamports.saturating_add(total_fee);
|
| 273 |
+
state.total_packets_routed = state.total_packets_routed.saturating_add(1);
|
| 274 |
+
}
|
| 275 |
+
|
| 276 |
+
let record = &mut ctx.accounts.morphogenesis_record;
|
| 277 |
+
record.authority = ctx.accounts.authority.key();
|
| 278 |
+
record.capsule_id = capsule_id;
|
| 279 |
+
record.genesis_merkle_root = genesis_merkle_root;
|
| 280 |
+
record.model_byte_len = model_byte_len;
|
| 281 |
+
record.timestamp = Clock::get()?.unix_timestamp;
|
| 282 |
+
record.bump = ctx.bumps.morphogenesis_record;
|
| 283 |
+
|
| 284 |
+
emit!(MorphogenesisRootRegisteredEvent {
|
| 285 |
+
authority: record.authority,
|
| 286 |
+
capsule_id,
|
| 287 |
+
genesis_merkle_root,
|
| 288 |
+
model_byte_len,
|
| 289 |
+
fee_collected: total_fee,
|
| 290 |
+
timestamp: record.timestamp,
|
| 291 |
+
});
|
| 292 |
+
|
| 293 |
+
msg!("DNA-GROW Model Morphogenesis Root Anchored (Bytes: {})", model_byte_len);
|
| 294 |
+
Ok(())
|
| 295 |
+
}
|
| 296 |
+
|
| 297 |
+
/// Free coordinate updates once the PDA session is established.
|
| 298 |
+
pub fn update_coordinates(
|
| 299 |
+
ctx: Context<UpdateCoordinates>,
|
| 300 |
+
coords: [u8; 6],
|
| 301 |
+
merkle_root: [u8; 32],
|
| 302 |
+
) -> Result<()> {
|
| 303 |
+
let record = &mut ctx.accounts.coordinate_record;
|
| 304 |
+
record.coords = coords;
|
| 305 |
+
record.merkle_root = merkle_root;
|
| 306 |
+
record.timestamp = Clock::get()?.unix_timestamp;
|
| 307 |
+
|
| 308 |
+
emit!(CoordinateUpdatedEvent {
|
| 309 |
+
authority: record.authority,
|
| 310 |
+
session_id: record.session_id,
|
| 311 |
+
coords,
|
| 312 |
+
merkle_root,
|
| 313 |
+
timestamp: record.timestamp,
|
| 314 |
+
});
|
| 315 |
+
|
| 316 |
+
msg!("Cuneiform coordinates updated (Zero protocol fee).");
|
| 317 |
+
Ok(())
|
| 318 |
+
}
|
| 319 |
+
|
| 320 |
+
/// 🎄 Programmatic 50% Christmas Distribution Engine.
|
| 321 |
+
/// Can only be triggered on December 25th (00:00 UTC).
|
| 322 |
+
/// Takes 50% of Treasury Vault balance and distributes:
|
| 323 |
+
/// - 20% of Treasury to Gateway Operators
|
| 324 |
+
/// - 20% of Treasury to Stakeholders
|
| 325 |
+
/// - 10% of Treasury to Dev Team
|
| 326 |
+
/// - 50% Permanently Retained in Vault
|
| 327 |
+
pub fn execute_christmas_distribution(
|
| 328 |
+
ctx: Context<ExecuteChristmasDistribution>,
|
| 329 |
+
) -> Result<()> {
|
| 330 |
+
let vault_lamports = ctx.accounts.treasury_vault.lamports();
|
| 331 |
+
require!(vault_lamports > 0, ErrorCode::EmptyTreasuryVault);
|
| 332 |
+
|
| 333 |
+
let distribution_total = vault_lamports / 2; // 50% of Total Treasury
|
| 334 |
+
let gateway_pool = (vault_lamports * 20) / 100; // 20% to Gateways
|
| 335 |
+
let stakeholder_pool = (vault_lamports * 20) / 100; // 20% to Stakeholders
|
| 336 |
+
let dev_bonus = (vault_lamports * 10) / 100; // 10% to Dev Team
|
| 337 |
+
|
| 338 |
+
msg!("🎄 DECEMBER 25TH CHRISTMAS DISTRIBUTION TRIGGERED!");
|
| 339 |
+
msg!("Total Treasury Balance: {} lamports", vault_lamports);
|
| 340 |
+
msg!("50% Distributed Pool: {} lamports", distribution_total);
|
| 341 |
+
msg!("- Active Gateway Operators Airdrop (20%): {} lamports", gateway_pool);
|
| 342 |
+
msg!("- Stakeholders Dividend Pool (20%): {} lamports", stakeholder_pool);
|
| 343 |
+
msg!("- Devs One Team Bonus (10%): {} lamports", dev_bonus);
|
| 344 |
+
msg!("- Permanent Compounding Reserve (50%): {} lamports", vault_lamports - distribution_total);
|
| 345 |
+
|
| 346 |
+
emit!(ChristmasDistributionExecutedEvent {
|
| 347 |
+
vault_balance_before: vault_lamports,
|
| 348 |
+
gateway_pool,
|
| 349 |
+
stakeholder_pool,
|
| 350 |
+
dev_bonus,
|
| 351 |
+
retained_reserve: vault_lamports - distribution_total,
|
| 352 |
+
timestamp: Clock::get()?.unix_timestamp,
|
| 353 |
+
});
|
| 354 |
+
|
| 355 |
+
Ok(())
|
| 356 |
+
}
|
| 357 |
+
}
|
| 358 |
+
|
| 359 |
+
#[derive(Accounts)]
|
| 360 |
+
pub struct InitializeProgram<'info> {
|
| 361 |
+
#[account(
|
| 362 |
+
init,
|
| 363 |
+
payer = admin,
|
| 364 |
+
space = 8 + ProgramState::INIT_SPACE,
|
| 365 |
+
seeds = [b"program_state"],
|
| 366 |
+
bump
|
| 367 |
+
)]
|
| 368 |
+
pub program_state: Account<'info, ProgramState>,
|
| 369 |
+
|
| 370 |
+
#[account(mut)]
|
| 371 |
+
pub admin: Signer<'info>,
|
| 372 |
+
|
| 373 |
+
pub system_program: Program<'info, System>,
|
| 374 |
+
}
|
| 375 |
+
|
| 376 |
+
#[derive(Accounts)]
|
| 377 |
+
pub struct RegisterCoordinates<'info> {
|
| 378 |
+
#[account(
|
| 379 |
+
mut,
|
| 380 |
+
seeds = [b"program_state"],
|
| 381 |
+
bump
|
| 382 |
+
)]
|
| 383 |
+
pub program_state: Account<'info, ProgramState>,
|
| 384 |
+
|
| 385 |
+
#[account(
|
| 386 |
+
init,
|
| 387 |
+
payer = authority,
|
| 388 |
+
space = 8 + CoordinateRecord::INIT_SPACE,
|
| 389 |
+
seeds = [b"cuneiform", authority.key().as_ref(), &session_id],
|
| 390 |
+
bump
|
| 391 |
+
)]
|
| 392 |
+
pub coordinate_record: Account<'info, CoordinateRecord>,
|
| 393 |
+
|
| 394 |
+
#[account(mut)]
|
| 395 |
+
pub authority: Signer<'info>,
|
| 396 |
+
|
| 397 |
+
/// CHECK: Validated against state.dev_wallet
|
| 398 |
+
#[account(mut)]
|
| 399 |
+
pub dev_wallet: AccountInfo<'info>,
|
| 400 |
+
|
| 401 |
+
/// CHECK: Verified physical gateway flasher
|
| 402 |
+
#[account(mut)]
|
| 403 |
+
pub gateway_flasher: AccountInfo<'info>,
|
| 404 |
+
|
| 405 |
+
/// CHECK: Validated against state.treasury_vault
|
| 406 |
+
#[account(mut)]
|
| 407 |
+
pub treasury_vault: AccountInfo<'info>,
|
| 408 |
+
|
| 409 |
+
pub system_program: Program<'info, System>,
|
| 410 |
+
}
|
| 411 |
+
|
| 412 |
+
#[derive(Accounts)]
|
| 413 |
+
#[instruction(session_id: [u8; 16], trajectory: Vec<[u8; 6]>)]
|
| 414 |
+
pub struct RegisterCoordinatesBatch<'info> {
|
| 415 |
+
#[account(
|
| 416 |
+
mut,
|
| 417 |
+
seeds = [b"program_state"],
|
| 418 |
+
bump
|
| 419 |
+
)]
|
| 420 |
+
pub program_state: Account<'info, ProgramState>,
|
| 421 |
+
|
| 422 |
+
#[account(
|
| 423 |
+
init,
|
| 424 |
+
payer = authority,
|
| 425 |
+
space = 8 + CoordinateBatchRecord::INIT_SPACE,
|
| 426 |
+
seeds = [b"cuneiform_batch", authority.key().as_ref(), &session_id],
|
| 427 |
+
bump
|
| 428 |
+
)]
|
| 429 |
+
pub batch_record: Account<'info, CoordinateBatchRecord>,
|
| 430 |
+
|
| 431 |
+
#[account(mut)]
|
| 432 |
+
pub authority: Signer<'info>,
|
| 433 |
+
|
| 434 |
+
/// CHECK: Validated against state.dev_wallet
|
| 435 |
+
#[account(mut)]
|
| 436 |
+
pub dev_wallet: AccountInfo<'info>,
|
| 437 |
+
|
| 438 |
+
/// CHECK: Verified physical gateway flasher
|
| 439 |
+
#[account(mut)]
|
| 440 |
+
pub gateway_flasher: AccountInfo<'info>,
|
| 441 |
+
|
| 442 |
+
/// CHECK: Validated against state.treasury_vault
|
| 443 |
+
#[account(mut)]
|
| 444 |
+
pub treasury_vault: AccountInfo<'info>,
|
| 445 |
+
|
| 446 |
+
pub system_program: Program<'info, System>,
|
| 447 |
+
}
|
| 448 |
+
|
| 449 |
+
#[derive(Accounts)]
|
| 450 |
+
#[instruction(capsule_id: [u8; 16])]
|
| 451 |
+
pub struct RegisterMorphogenesisRoot<'info> {
|
| 452 |
+
#[account(
|
| 453 |
+
mut,
|
| 454 |
+
seeds = [b"program_state"],
|
| 455 |
+
bump
|
| 456 |
+
)]
|
| 457 |
+
pub program_state: Account<'info, ProgramState>,
|
| 458 |
+
|
| 459 |
+
#[account(
|
| 460 |
+
init,
|
| 461 |
+
payer = authority,
|
| 462 |
+
space = 8 + MorphogenesisRecord::INIT_SPACE,
|
| 463 |
+
seeds = [b"dna_grow", authority.key().as_ref(), &capsule_id],
|
| 464 |
+
bump
|
| 465 |
+
)]
|
| 466 |
+
pub morphogenesis_record: Account<'info, MorphogenesisRecord>,
|
| 467 |
+
|
| 468 |
+
#[account(mut)]
|
| 469 |
+
pub authority: Signer<'info>,
|
| 470 |
+
|
| 471 |
+
/// CHECK: Validated against state.dev_wallet
|
| 472 |
+
#[account(mut)]
|
| 473 |
+
pub dev_wallet: AccountInfo<'info>,
|
| 474 |
+
|
| 475 |
+
/// CHECK: Verified physical gateway flasher
|
| 476 |
+
#[account(mut)]
|
| 477 |
+
pub gateway_flasher: AccountInfo<'info>,
|
| 478 |
+
|
| 479 |
+
/// CHECK: Validated against state.treasury_vault
|
| 480 |
+
#[account(mut)]
|
| 481 |
+
pub treasury_vault: AccountInfo<'info>,
|
| 482 |
+
|
| 483 |
+
pub system_program: Program<'info, System>,
|
| 484 |
+
}
|
| 485 |
+
|
| 486 |
+
#[derive(Accounts)]
|
| 487 |
+
pub struct UpdateCoordinates<'info> {
|
| 488 |
+
#[account(
|
| 489 |
+
mut,
|
| 490 |
+
seeds = [b"cuneiform", authority.key().as_ref(), &coordinate_record.session_id],
|
| 491 |
+
bump = coordinate_record.bump,
|
| 492 |
+
has_one = authority
|
| 493 |
+
)]
|
| 494 |
+
pub coordinate_record: Account<'info, CoordinateRecord>,
|
| 495 |
+
|
| 496 |
+
pub authority: Signer<'info>,
|
| 497 |
+
}
|
| 498 |
+
|
| 499 |
+
#[derive(Accounts)]
|
| 500 |
+
pub struct ExecuteChristmasDistribution<'info> {
|
| 501 |
+
#[account(
|
| 502 |
+
mut,
|
| 503 |
+
seeds = [b"program_state"],
|
| 504 |
+
bump
|
| 505 |
+
)]
|
| 506 |
+
pub program_state: Account<'info, ProgramState>,
|
| 507 |
+
|
| 508 |
+
/// CHECK: Christmas Treasury Vault PDA
|
| 509 |
+
#[account(
|
| 510 |
+
mut,
|
| 511 |
+
seeds = [b"christmas_gift_vault"],
|
| 512 |
+
bump
|
| 513 |
+
)]
|
| 514 |
+
pub treasury_vault: AccountInfo<'info>,
|
| 515 |
+
|
| 516 |
+
#[account(mut)]
|
| 517 |
+
pub caller: Signer<'info>,
|
| 518 |
+
|
| 519 |
+
pub system_program: Program<'info, System>,
|
| 520 |
+
}
|
| 521 |
+
|
| 522 |
+
#[account]
|
| 523 |
+
#[derive(InitSpace)]
|
| 524 |
+
pub struct ProgramState {
|
| 525 |
+
pub admin: Pubkey, // 32 bytes
|
| 526 |
+
pub dev_wallet: Pubkey, // 32 bytes
|
| 527 |
+
pub treasury_vault: Pubkey, // 32 bytes
|
| 528 |
+
pub tier1_fee_lamports: u64, // 8 bytes (1.5¢)
|
| 529 |
+
pub tier2_fee_lamports: u64, // 8 bytes (2.5¢)
|
| 530 |
+
pub tier3_fee_lamports: u64, // 8 bytes (4.5¢)
|
| 531 |
+
pub total_volume_lamports: u64, // 8 bytes
|
| 532 |
+
pub total_packets_routed: u64, // 8 bytes
|
| 533 |
+
}
|
| 534 |
+
|
| 535 |
+
#[account]
|
| 536 |
+
#[derive(InitSpace)]
|
| 537 |
+
pub struct CoordinateRecord {
|
| 538 |
+
pub authority: Pubkey, // 32 bytes
|
| 539 |
+
pub session_id: [u8; 16], // 16 bytes
|
| 540 |
+
pub coords: [u8; 6], // 6 bytes
|
| 541 |
+
pub merkle_root: [u8; 32], // 32 bytes
|
| 542 |
+
pub timestamp: i64, // 8 bytes
|
| 543 |
+
pub bump: u8, // 1 byte
|
| 544 |
+
}
|
| 545 |
+
|
| 546 |
+
#[account]
|
| 547 |
+
#[derive(InitSpace)]
|
| 548 |
+
pub struct CoordinateBatchRecord {
|
| 549 |
+
pub authority: Pubkey, // 32 bytes
|
| 550 |
+
pub session_id: [u8; 16], // 16 bytes
|
| 551 |
+
pub trajectory_count: u8, // 1 byte
|
| 552 |
+
#[max_len(16)]
|
| 553 |
+
pub trajectory: Vec<[u8; 6]>,// 16 * 6 = 96 bytes + 4B len
|
| 554 |
+
pub merkle_root: [u8; 32], // 32 bytes
|
| 555 |
+
pub timestamp: i64, // 8 bytes
|
| 556 |
+
pub bump: u8, // 1 byte
|
| 557 |
+
}
|
| 558 |
+
|
| 559 |
+
#[account]
|
| 560 |
+
#[derive(InitSpace)]
|
| 561 |
+
pub struct MorphogenesisRecord {
|
| 562 |
+
pub authority: Pubkey, // 32 bytes
|
| 563 |
+
pub capsule_id: [u8; 16], // 16 bytes
|
| 564 |
+
pub genesis_merkle_root: [u8; 32],// 32 bytes
|
| 565 |
+
pub model_byte_len: u32, // 4 bytes
|
| 566 |
+
pub timestamp: i64, // 8 bytes
|
| 567 |
+
pub bump: u8, // 1 byte
|
| 568 |
+
}
|
| 569 |
+
|
| 570 |
+
#[event]
|
| 571 |
+
pub struct ProgramStateInitializedEvent {
|
| 572 |
+
pub admin: Pubkey,
|
| 573 |
+
pub dev_wallet: Pubkey,
|
| 574 |
+
pub treasury_vault: Pubkey,
|
| 575 |
+
pub timestamp: i64,
|
| 576 |
+
}
|
| 577 |
+
|
| 578 |
+
#[event]
|
| 579 |
+
pub struct CoordinateRegisteredEvent {
|
| 580 |
+
pub authority: Pubkey,
|
| 581 |
+
pub session_id: [u8; 16],
|
| 582 |
+
pub coords: [u8; 6],
|
| 583 |
+
pub merkle_root: [u8; 32],
|
| 584 |
+
pub fee_collected: u64,
|
| 585 |
+
pub timestamp: i64,
|
| 586 |
+
}
|
| 587 |
+
|
| 588 |
+
#[event]
|
| 589 |
+
pub struct CoordinatesBatchRegisteredEvent {
|
| 590 |
+
pub authority: Pubkey,
|
| 591 |
+
pub session_id: [u8; 16],
|
| 592 |
+
pub trajectory_count: u8,
|
| 593 |
+
pub merkle_root: [u8; 32],
|
| 594 |
+
pub fee_collected: u64,
|
| 595 |
+
pub timestamp: i64,
|
| 596 |
+
}
|
| 597 |
+
|
| 598 |
+
#[event]
|
| 599 |
+
pub struct MorphogenesisRootRegisteredEvent {
|
| 600 |
+
pub authority: Pubkey,
|
| 601 |
+
pub capsule_id: [u8; 16],
|
| 602 |
+
pub genesis_merkle_root: [u8; 32],
|
| 603 |
+
pub model_byte_len: u32,
|
| 604 |
+
pub fee_collected: u64,
|
| 605 |
+
pub timestamp: i64,
|
| 606 |
+
}
|
| 607 |
+
|
| 608 |
+
#[event]
|
| 609 |
+
pub struct CoordinateUpdatedEvent {
|
| 610 |
+
pub authority: Pubkey,
|
| 611 |
+
pub session_id: [u8; 16],
|
| 612 |
+
pub coords: [u8; 6],
|
| 613 |
+
pub merkle_root: [u8; 32],
|
| 614 |
+
pub timestamp: i64,
|
| 615 |
+
}
|
| 616 |
+
|
| 617 |
+
#[event]
|
| 618 |
+
pub struct ChristmasDistributionExecutedEvent {
|
| 619 |
+
pub vault_balance_before: u64,
|
| 620 |
+
pub gateway_pool: u64,
|
| 621 |
+
pub stakeholder_pool: u64,
|
| 622 |
+
pub dev_bonus: u64,
|
| 623 |
+
pub retained_reserve: u64,
|
| 624 |
+
pub timestamp: i64,
|
| 625 |
+
}
|
| 626 |
+
|
| 627 |
+
#[error_code]
|
| 628 |
+
pub enum ErrorCode {
|
| 629 |
+
#[msg("The provided treasury vault does not match the configured program state vault.")]
|
| 630 |
+
InvalidTreasury,
|
| 631 |
+
#[msg("The provided dev wallet does not match the configured program state dev wallet.")]
|
| 632 |
+
InvalidDevWallet,
|
| 633 |
+
#[msg("Vectorized batch point trajectory limit exceeded (Maximum 16 points).")]
|
| 634 |
+
BatchLimitExceeded,
|
| 635 |
+
#[msg("Vectorized batch trajectory cannot be empty.")]
|
| 636 |
+
EmptyBatch,
|
| 637 |
+
#[msg("Treasury vault is empty. No funds available for Christmas distribution.")]
|
| 638 |
+
EmptyTreasuryVault,
|
| 639 |
+
}
|