From 1d9b0ebd42ad3691de6fdcb9fa168eec77de598e Mon Sep 17 00:00:00 2001 From: Hardhat Chad Date: Fri, 13 Jun 2025 15:37:20 -0700 Subject: [PATCH] reward event --- api/src/event.rs | 15 +++++++++++-- program/src/close.rs | 12 +++++++++++ program/src/mine.rs | 51 +++++++++++++++++++++++++++++++++----------- 3 files changed, 63 insertions(+), 15 deletions(-) diff --git a/api/src/event.rs b/api/src/event.rs index 7080b95..a8e0aa9 100644 --- a/api/src/event.rs +++ b/api/src/event.rs @@ -45,14 +45,25 @@ impl SwapEvent { #[repr(C)] #[derive(Clone, Copy, Debug, Default, PartialEq, Pod, Zeroable)] pub struct RewardEvent { + /// The amount of ORE distributed as a reward. + pub amount: u64, + /// The authority who received the reward. pub authority: Pubkey, /// The block id. pub block_id: u64, - /// The amount of ORE distributed as a reward. - pub amount: u64, + /// The type of reward. + pub rewards_type: u64, +} + +#[repr(u8)] +#[derive(Clone, Copy, Debug, Eq, PartialEq, TryFromPrimitive)] +pub enum RewardsType { + Nugget = 0, + Lode = 1, + Motherlode = 2, } event!(SwapEvent); diff --git a/program/src/close.rs b/program/src/close.rs index 5d1f5f0..9974031 100644 --- a/program/src/close.rs +++ b/program/src/close.rs @@ -57,7 +57,10 @@ pub fn process_close(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResul // Payout block reward. if block.reward.lode_reward > 0 && block.reward.lode_authority != Pubkey::default() { + // Load recipient. recipient_info.as_associated_token_account(&block.reward.lode_authority, &MINT_ADDRESS)?; + + // Mint reward to recipient. mint_to_signed( mint_quote_info, recipient_info, @@ -66,6 +69,15 @@ pub fn process_close(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResul block.reward.lode_reward, &[TREASURY], )?; + + // Emit event. + RewardEvent { + amount: block.reward.lode_reward, + authority: block.reward.lode_authority, + block_id: block.id, + rewards_type: RewardsType::Lode as u64, + } + .log(); } // Burn base liquidity. diff --git a/program/src/mine.rs b/program/src/mine.rs index e91402a..559d031 100644 --- a/program/src/mine.rs +++ b/program/src/mine.rs @@ -97,7 +97,7 @@ pub fn process_mine(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult } // Mine and accumulate rewards. - let mut miner_reward = 0; + let mut nugget_reward = 0; for _ in 0..amount { // Update stats block.total_hashes += 1; @@ -109,7 +109,7 @@ pub fn process_mine(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult // Score and increment rewards. let score = difficulty(miner.hash) as u64; if score >= block.reward.nugget_threshold { - miner_reward += block.reward.nugget_reward; + nugget_reward += block.reward.nugget_reward; } // If hash is best hash, update best hash. @@ -120,28 +120,53 @@ pub fn process_mine(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult // If hash is motherlode hash, pay the motherlode reward. if score >= block.reward.motherlode_threshold { + // Execute transfer. + let motherlode_amount = treasury_tokens.amount(); transfer_signed( authority_info, treasury_tokens_info, recipient_info, token_program, - treasury_tokens.amount(), + motherlode_amount, &[TREASURY], )?; + + // Emit event. + RewardEvent { + amount: motherlode_amount, + authority: miner.authority, + block_id: block.id, + rewards_type: RewardsType::Motherlode as u64, + } + .log(); } } // Payout ORE. - block.total_rewards += miner_reward; - miner.total_rewards += miner_reward; - mint_to_signed( - mint_ore_info, - recipient_info, - treasury_info, - token_program, - miner_reward, - &[TREASURY], - )?; + if nugget_reward > 0 { + // Update stats. + block.total_rewards += nugget_reward; + miner.total_rewards += nugget_reward; + + // Mint to recipient. + mint_to_signed( + mint_ore_info, + recipient_info, + treasury_info, + token_program, + nugget_reward, + &[TREASURY], + )?; + + // Emit event. + RewardEvent { + amount: nugget_reward, + authority: miner.authority, + block_id: block.id, + rewards_type: RewardsType::Nugget as u64, + } + .log(); + } Ok(()) }