Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

event emission #39

Merged
merged 2 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading