Create smart_contracts/diamond_rewards.rs
Browse files
smart_contracts/diamond_rewards.rs
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Diamante AI Rewards Program - Solana Smart Contract
|
| 2 |
+
use solana_program::{
|
| 3 |
+
account_info::{next_account_info, AccountInfo},
|
| 4 |
+
entrypoint,
|
| 5 |
+
entrypoint::ProgramResult,
|
| 6 |
+
msg,
|
| 7 |
+
program_error::ProgramError,
|
| 8 |
+
pubkey::Pubkey,
|
| 9 |
+
system_instruction,
|
| 10 |
+
sysvar::{rent::Rent, Sysvar},
|
| 11 |
+
program_pack::Pack,
|
| 12 |
+
};
|
| 13 |
+
use spl_token::state::Account as TokenAccount;
|
| 14 |
+
use std::convert::TryInto;
|
| 15 |
+
|
| 16 |
+
// Declare program ID
|
| 17 |
+
solana_program::declare_id!("5VqjB57PnrTUSQRoN8skwKtstqPrMFJiZaDGz6BF5FuKhSGSYhbcwPdgKPVNbgtWhi9AkJ8z8HvgaBDzccNHjzE3");
|
| 18 |
+
|
| 19 |
+
// Instruction types
|
| 20 |
+
#[repr(C)]
|
| 21 |
+
#[derive(Clone, Debug, PartialEq)]
|
| 22 |
+
pub enum DiamondInstruction {
|
| 23 |
+
/// Reward user with DIAMOND tokens for AI contributions
|
| 24 |
+
/// Accounts expected:
|
| 25 |
+
/// 0. [signer] Platform authority
|
| 26 |
+
/// 1. [writable] User token account
|
| 27 |
+
/// 2. [writable] Platform token account
|
| 28 |
+
/// 3. [] Token mint (DIAMANTE)
|
| 29 |
+
/// 4. [] System program
|
| 30 |
+
/// 5. [] Token program
|
| 31 |
+
RewardUser {
|
| 32 |
+
amount: u64,
|
| 33 |
+
contribution_type: u8, // 0: inference, 1: training, 2: data, 3: validation
|
| 34 |
+
},
|
| 35 |
+
|
| 36 |
+
/// Stake DIAMANTE tokens to earn yield
|
| 37 |
+
StakeTokens {
|
| 38 |
+
amount: u64,
|
| 39 |
+
duration: u64, // in seconds
|
| 40 |
+
},
|
| 41 |
+
|
| 42 |
+
/// Unstake tokens and claim rewards
|
| 43 |
+
UnstakeTokens {
|
| 44 |
+
stake_account_bump: u8,
|
| 45 |
+
},
|
| 46 |
+
|
| 47 |
+
/// Register AI model contribution
|
| 48 |
+
RegisterModel {
|
| 49 |
+
model_hash: [u8; 32],
|
| 50 |
+
performance_score: u8,
|
| 51 |
+
},
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
entrypoint!(process_instruction);
|
| 55 |
+
|
| 56 |
+
pub fn process_instruction(
|
| 57 |
+
program_id: &Pubkey,
|
| 58 |
+
accounts: &[AccountInfo],
|
| 59 |
+
instruction_data: &[u8],
|
| 60 |
+
) -> ProgramResult {
|
| 61 |
+
let instruction = DiamondInstruction::unpack(instruction_data)?;
|
| 62 |
+
|
| 63 |
+
match instruction {
|
| 64 |
+
DiamondInstruction::RewardUser { amount, contribution_type } => {
|
| 65 |
+
process_reward_user(program_id, accounts, amount, contribution_type)
|
| 66 |
+
}
|
| 67 |
+
DiamondInstruction::StakeTokens { amount, duration } => {
|
| 68 |
+
process_stake_tokens(program_id, accounts, amount, duration)
|
| 69 |
+
}
|
| 70 |
+
DiamondInstruction::UnstakeTokens { stake_account_bump } => {
|
| 71 |
+
process_unstake_tokens(program_id, accounts, stake_account_bump)
|
| 72 |
+
}
|
| 73 |
+
DiamondInstruction::RegisterModel { model_hash, performance_score } => {
|
| 74 |
+
process_register_model(program_id, accounts, model_hash, performance_score)
|
| 75 |
+
}
|
| 76 |
+
}
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
fn process_reward_user(
|
| 80 |
+
program_id: &Pubkey,
|
| 81 |
+
accounts: &[AccountInfo],
|
| 82 |
+
amount: u64,
|
| 83 |
+
contribution_type: u8,
|
| 84 |
+
) -> ProgramResult {
|
| 85 |
+
let accounts_iter = &mut accounts.iter();
|
| 86 |
+
|
| 87 |
+
let authority = next_account_info(accounts_iter)?;
|
| 88 |
+
let user_token_account = next_account_info(accounts_iter)?;
|
| 89 |
+
let platform_token_account = next_account_info(accounts_iter)?;
|
| 90 |
+
let token_mint = next_account_info(accounts_iter)?;
|
| 91 |
+
let system_program = next_account_info(accounts_iter)?;
|
| 92 |
+
let token_program = next_account_info(accounts_iter)?;
|
| 93 |
+
|
| 94 |
+
// Verify authority is signer
|
| 95 |
+
if !authority.is_signer {
|
| 96 |
+
msg!("Authority must be signer");
|
| 97 |
+
return Err(ProgramError::MissingRequiredSignature);
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
// Calculate reward based on contribution type
|
| 101 |
+
let adjusted_amount = match contribution_type {
|
| 102 |
+
0 => amount, // Inference
|
| 103 |
+
1 => amount * 2, // Training (2x reward)
|
| 104 |
+
2 => amount * 3 / 2, // Data contribution (1.5x)
|
| 105 |
+
3 => amount * 5 / 4, // Validation (1.25x)
|
| 106 |
+
_ => amount,
|
| 107 |
+
};
|
| 108 |
+
|
| 109 |
+
// Transfer tokens from platform to user
|
| 110 |
+
let transfer_instruction = spl_token::instruction::transfer(
|
| 111 |
+
token_program.key,
|
| 112 |
+
platform_token_account.key,
|
| 113 |
+
user_token_account.key,
|
| 114 |
+
authority.key,
|
| 115 |
+
&[],
|
| 116 |
+
adjusted_amount,
|
| 117 |
+
)?;
|
| 118 |
+
|
| 119 |
+
solana_program::program::invoke(
|
| 120 |
+
&transfer_instruction,
|
| 121 |
+
&[
|
| 122 |
+
platform_token_account.clone(),
|
| 123 |
+
user_token_account.clone(),
|
| 124 |
+
authority.clone(),
|
| 125 |
+
token_program.clone(),
|
| 126 |
+
],
|
| 127 |
+
)?;
|
| 128 |
+
|
| 129 |
+
msg!(
|
| 130 |
+
"Rewarded user {} with {} DIAMOND tokens for contribution type {}",
|
| 131 |
+
user_token_account.key,
|
| 132 |
+
adjusted_amount,
|
| 133 |
+
contribution_type
|
| 134 |
+
);
|
| 135 |
+
|
| 136 |
+
Ok(())
|
| 137 |
+
}
|
| 138 |
+
|
| 139 |
+
// Implementaci贸n de stake/unstake y registro de modelos...
|