Skip to content

Commit

Permalink
added in emits
Browse files Browse the repository at this point in the history
  • Loading branch information
coachchucksol committed Jun 28, 2024
2 parents 7b7b426 + f5c08dd commit aa6b45f
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 44 deletions.
6 changes: 4 additions & 2 deletions programs/steward/src/delegation.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use anchor_lang::idl::*;
use anchor_lang::prelude::*;
use borsh::BorshSerialize;
use spl_stake_pool::big_vec::BigVec;

use crate::{
Expand All @@ -7,15 +9,15 @@ use crate::{
StewardState,
};

#[derive(Debug)]
#[derive(Debug, Clone)]
pub enum RebalanceType {
Increase(u64),
Decrease(DecreaseComponents),
None,
}

#[event]
#[derive(Debug, PartialEq, Eq)]
#[derive(Default, Debug, Clone, PartialEq, Eq)]
pub struct DecreaseComponents {
pub scoring_unstake_lamports: u64,
pub instant_unstake_lamports: u64,
Expand Down
6 changes: 4 additions & 2 deletions programs/steward/src/instructions/compute_delegations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@ pub fn handler(ctx: Context<ComputeDelegations>) -> Result<()> {
.state
.compute_delegations(clock.epoch, &config)?;

maybe_transition_and_emit(
if let Some(event) = maybe_transition_and_emit(
&mut state_account.state,
&clock,
&config.parameters,
&epoch_schedule,
)?;
)? {
emit!(event);
}

Ok(())
}
12 changes: 8 additions & 4 deletions programs/steward/src/instructions/compute_instant_unstake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,25 @@ pub fn handler(ctx: Context<ComputeInstantUnstake>, validator_list_index: usize)
return Err(StewardError::StateMachinePaused.into());
}

state_account.state.compute_instant_unstake(
if let Some(instant_unstake) = state_account.state.compute_instant_unstake(
&clock,
&epoch_schedule,
&validator_history,
validator_list_index,
&cluster,
&config,
)?;
)? {
emit!(instant_unstake);
}

maybe_transition_and_emit(
if let Some(event) = maybe_transition_and_emit(
&mut state_account.state,
&clock,
&config.parameters,
&epoch_schedule,
)?;
)? {
emit!(event);
}

Ok(())
}
12 changes: 8 additions & 4 deletions programs/steward/src/instructions/compute_score.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,22 +81,26 @@ pub fn handler(ctx: Context<ComputeScore>, validator_list_index: usize) -> Resul
StewardError::InvalidState
);

state_account.state.compute_score(
if let Some(score) = state_account.state.compute_score(
&clock,
&epoch_schedule,
&validator_history,
validator_list_index,
&cluster_history,
&config,
num_pool_validators as u64,
)?;
)? {
emit!(score);
}

maybe_transition_and_emit(
if let Some(event) = maybe_transition_and_emit(
&mut state_account.state,
&clock,
&config.parameters,
&epoch_schedule,
)?;
)? {
emit!(event);
}

Ok(())
}
95 changes: 89 additions & 6 deletions programs/steward/src/instructions/rebalance.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
use std::num::NonZeroU32;

use anchor_lang::{
idl::types::*,
idl::*,
prelude::*,
solana_program::{
program::invoke_signed,
stake::{self, tools::get_minimum_delegation},
system_program, sysvar, vote,
},
};
use borsh::{BorshDeserialize, BorshSerialize};
use spl_pod::solana_program::stake::state::StakeStateV2;
use spl_stake_pool::{
find_stake_program_address, find_transient_stake_program_address, minimum_delegation,
Expand All @@ -17,7 +20,7 @@ use validator_history::ValidatorHistory;

use crate::{
constants::STAKE_POOL_WITHDRAW_SEED,
delegation::RebalanceType,
delegation::{DecreaseComponents, RebalanceType},
errors::StewardError,
maybe_transition_and_emit,
utils::{deserialize_stake_pool, get_stake_pool_address, get_validator_stake_info_at_index},
Expand Down Expand Up @@ -183,10 +186,9 @@ pub fn handler(ctx: Context<Rebalance>, validator_list_index: usize) -> Result<(
)?
};

// Have to drop the state account before calling the CPI
drop(state_account);

match result {
match result.clone() {
RebalanceType::Decrease(decrease_components) => {
invoke_signed(
&spl_stake_pool::instruction::decrease_validator_stake_with_reserve(
Expand Down Expand Up @@ -264,16 +266,97 @@ pub fn handler(ctx: Context<Rebalance>, validator_list_index: usize) -> Result<(
}

{
// We had to drop the state account before calling the CPI
let mut state_account = ctx.accounts.state_account.load_mut()?;

maybe_transition_and_emit(
emit!(rebalance_to_event(
ctx.accounts.vote_account.key(),
clock.epoch as u16,
result
));

if let Some(event) = maybe_transition_and_emit(
&mut state_account.state,
&clock,
&config.parameters,
&epoch_schedule,
)?;
)? {
emit!(event);
}
}

Ok(())
}

#[event]
pub struct RebalanceEvent {
pub vote_account: Pubkey,
pub epoch: u16,
pub rebalance_type_tag: RebalanceTypeTag,
pub increase_lamports: u64,
pub decrease_components: DecreaseComponents,
}

fn rebalance_to_event(
vote_account: Pubkey,
epoch: u16,
rebalance_type: RebalanceType,
) -> RebalanceEvent {
match rebalance_type {
RebalanceType::None => RebalanceEvent {
vote_account,
epoch,
rebalance_type_tag: RebalanceTypeTag::None,
increase_lamports: 0,
decrease_components: DecreaseComponents::default(),
},
RebalanceType::Increase(lamports) => RebalanceEvent {
vote_account,
epoch,
rebalance_type_tag: RebalanceTypeTag::Increase,
increase_lamports: lamports,
decrease_components: DecreaseComponents::default(),
},
RebalanceType::Decrease(decrease_components) => RebalanceEvent {
vote_account,
epoch,
rebalance_type_tag: RebalanceTypeTag::Decrease,
increase_lamports: 0,
decrease_components,
},
}
}

#[derive(BorshSerialize, BorshDeserialize, Debug)]
pub enum RebalanceTypeTag {
None,
Increase,
Decrease,
}

impl IdlBuild for RebalanceTypeTag {
fn create_type() -> Option<IdlTypeDef> {
Some(IdlTypeDef {
name: "RebalanceTypeTag".to_string(),
ty: IdlTypeDefTy::Enum {
variants: vec![
IdlEnumVariant {
name: "None".to_string(),
fields: None,
},
IdlEnumVariant {
name: "Increase".to_string(),
fields: None,
},
IdlEnumVariant {
name: "Decrease".to_string(),
fields: None,
},
],
},
docs: Default::default(),
generics: Default::default(),
serialization: Default::default(),
repr: Default::default(),
})
}
}
Loading

0 comments on commit aa6b45f

Please sign in to comment.