Skip to content

Commit

Permalink
chore: emit staker events post dr removal
Browse files Browse the repository at this point in the history
  • Loading branch information
gluax committed Jan 23, 2025
1 parent 1f5e163 commit ec5b614
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion contract/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "seda-contract"
version = "0.5.1"
version = "0.5.2"
edition.workspace = true
rust-version.workspace = true

Expand Down
30 changes: 24 additions & 6 deletions contract/src/msgs/data_requests/sudo/remove_requests.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
use std::collections::HashSet;

use cosmwasm_std::{to_json_string, Addr, BankMsg, Coin, DepsMut, Env, Event, Response, Uint128};
use cw_storage_plus::KeyDeserialize;
use seda_common::{
msgs::data_requests::sudo::{remove_requests, DistributionMessage},
types::Hash,
types::{Hash, ToHexStr},
};
use serde_json::json;

use super::{ContractError, SudoHandler};
use crate::{
msgs::{
data_requests::state::{self, DR_ESCROW},
staking::state::{STAKERS, STAKING_CONFIG},
staking::{
execute::staking_events::create_executor_event,
state::{STAKERS, STAKING_CONFIG},
},
PublicKey,
},
state::TOKEN,
Expand All @@ -29,7 +34,7 @@ fn remove_request_and_process_distributions(
messages: &[DistributionMessage],
deps: &mut DepsMut,
token: &str,
) -> Result<(Event, Vec<BankMsg>), ContractError> {
) -> Result<(Event, Vec<BankMsg>, HashSet<PublicKey>), ContractError> {
// find the data request from the committed pool (if it exists, otherwise error)
let dr_id = Hash::from_hex_str(&dr_id_str)?;
let dr = state::load_request(deps.storage, &dr_id)?;
Expand All @@ -41,6 +46,7 @@ fn remove_request_and_process_distributions(

// add 1 so we can account for the refund message that may be sent
let mut bank_messages = Vec::new();
let mut stakers_effected = HashSet::new();

// We need to send messages in the order given.
for message in messages {
Expand Down Expand Up @@ -105,7 +111,8 @@ fn remove_request_and_process_distributions(
// send remaining reward to the staker pending withdrawal
staker.tokens_pending_withdrawal += remaining_reward;
dr_escrow.amount = dr_escrow.amount.saturating_sub(remaining_reward);
STAKERS.update(deps.storage, public_key, &staker)?;
STAKERS.update(deps.storage, public_key.clone(), &staker)?;
stakers_effected.insert(public_key);

event = event.add_attribute(
"executor_reward",
Expand All @@ -130,21 +137,32 @@ fn remove_request_and_process_distributions(
state::remove_request(deps.storage, dr_id)?;
DR_ESCROW.remove(deps.storage, &dr_id);

Ok((event, bank_messages))
Ok((event, bank_messages, stakers_effected))
}

impl SudoHandler for remove_requests::Sudo {
fn sudo(self, mut deps: DepsMut, _: Env) -> Result<Response, ContractError> {
let token = TOKEN.load(deps.storage)?;
let mut response = Response::new();

let mut all_stakers_effected = HashSet::new();
for removal in self
.requests
.into_iter()
.map(|(dr_id, messages)| remove_request_and_process_distributions(dr_id, &messages, &mut deps, &token))
{
let (event, bank_messages) = removal?;
let (event, bank_messages, stakers_effected) = removal?;
all_stakers_effected.extend(stakers_effected);
response = response.add_event(event).add_messages(bank_messages);
}

for staker in all_stakers_effected {
response = response.add_event(create_executor_event(
STAKERS.get_staker(deps.storage, &staker)?,
staker.to_hex(),
));
}

Ok(response)
}
}
2 changes: 1 addition & 1 deletion contract/src/msgs/staking/execute/staking_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use seda_common::msgs::staking::{Staker, StakingConfig};

use super::CONTRACT_VERSION;

pub(in crate::msgs::staking::execute) fn create_executor_event(staker: Staker, public_key: String) -> Event {
pub fn create_executor_event(staker: Staker, public_key: String) -> Event {
Event::new("seda-executor").add_attributes([
("version", CONTRACT_VERSION.to_string()),
("identity", public_key),
Expand Down
2 changes: 1 addition & 1 deletion contract/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use serde::{Deserialize, Serialize};

use super::*;

#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct PublicKey(pub [u8; 33]);

impl AsRef<[u8]> for PublicKey {
Expand Down

0 comments on commit ec5b614

Please sign in to comment.