Skip to content

Commit

Permalink
Merge pull request #39 from burnt-labs/feat/event-emit
Browse files Browse the repository at this point in the history
event emission
  • Loading branch information
ash-burnt authored Jan 15, 2025
2 parents 13d0dab + 150255a commit e8aab99
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 1 deletion.
3 changes: 2 additions & 1 deletion contracts/account/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use cosmwasm_std::{
};

use crate::error::ContractError;
use crate::execute::{add_auth_method, assert_self, remove_auth_method};
use crate::execute::{add_auth_method, assert_self, emit, remove_auth_method};
use crate::msg::{ExecuteMsg, MigrateMsg};
use crate::{
error::ContractResult,
Expand Down Expand Up @@ -90,6 +90,7 @@ pub fn execute(
add_auth_method(deps, &env, add_authenticator)
}
ExecuteMsg::RemoveAuthMethod { id } => remove_auth_method(deps, env, *id),
ExecuteMsg::Emit { data } => emit(env, data.to_string()),
}
}

Expand Down
3 changes: 3 additions & 0 deletions contracts/account/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ pub enum ContractError {
#[error("cannot override existing authenticator at index {index}")]
OverridingIndex { index: u8 },

#[error("emit data too large")]
EmissionSizeExceeded,

/// Doesn't support PartialEq, moved below
#[error("{0}")]
SerdeJSON(String),
Expand Down
12 changes: 12 additions & 0 deletions contracts/account/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,18 @@ pub fn remove_auth_method(deps: DepsMut, env: Env, id: u8) -> ContractResult<Res
)
}

const MAX_SIZE: usize = 1024;
pub fn emit(env: Env, data: String) -> ContractResult<Response> {
if data.len() > MAX_SIZE {
Err(ContractError::EmissionSizeExceeded)
} else {
let emit_event = Event::new("account_emit")
.add_attribute("address", env.contract.address)
.add_attribute("data", data);
Ok(Response::new().add_event(emit_event))
}
}

pub fn assert_self(sender: &Addr, contract: &Addr) -> ContractResult<()> {
if sender != contract {
return Err(ContractError::Unauthorized);
Expand Down
1 change: 1 addition & 0 deletions contracts/account/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub struct InstantiateMsg {
pub enum ExecuteMsg {
AddAuthMethod { add_authenticator: AddAuthenticator },
RemoveAuthMethod { id: u8 },
Emit { data: String },
}

#[cw_serde]
Expand Down

0 comments on commit e8aab99

Please sign in to comment.