Skip to content

Commit

Permalink
feat: improve events contract to be usable via dynamic link (#274)
Browse files Browse the repository at this point in the history
* feat: improve events contract usable for callee/caller

* chore: add doc comments for event contract
  • Loading branch information
loloicci authored Mar 28, 2023
1 parent fc54ae1 commit f46c6e9
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 2 deletions.
1 change: 1 addition & 0 deletions contracts/events/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ cosmwasm-storage = { path = "../../packages/storage", features = ["iterator"] }
schemars = "0.8.1"
serde = { version = "1.0.125", default-features = false, features = ["derive"] }
thiserror = { version = "1.0.24" }
wasmer-types = { version = "2.2.1", features = ["enable-serde"] }

[dev-dependencies]
cosmwasm-schema = { path = "../../packages/schema" }
Expand Down
130 changes: 129 additions & 1 deletion contracts/events/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
use cosmwasm_std::{entry_point, Attribute, DepsMut, Env, Event, MessageInfo, Response};
use cosmwasm_std::{
callable_point, dynamic_link, entry_point, Addr, Attribute, Contract, DepsMut, Env, Event,
MessageInfo, Response,
};

use crate::error::ContractError;
use crate::msg::{ExecuteMsg, InstantiateMsg};

#[derive(Contract)]
struct EventsContract {
address: Addr,
}

#[dynamic_link(EventsContract)]
trait Events: Contract {
fn add_event_dyn(&self, ty: String, attributes: Vec<Attribute>);
fn add_events_dyn(&self, events: Vec<Event>);
fn add_attribute_dyn(&self, key: String, value: String);
fn add_attributes_dyn(&self, attributes: Vec<Attribute>);
}

#[entry_point]
pub fn instantiate(
_deps: DepsMut,
Expand All @@ -25,6 +41,21 @@ pub fn execute(
ExecuteMsg::Events { events } => handle_events(deps, events),
ExecuteMsg::Attribute { key, value } => handle_attribute(deps, key, value),
ExecuteMsg::Attributes { attributes } => handle_attributes(deps, attributes),
ExecuteMsg::EventDyn {
address,
ty,
attributes,
} => handle_event_dyn(deps, address, ty, attributes),
ExecuteMsg::EventsDyn { address, events } => handle_events_dyn(deps, address, events),
ExecuteMsg::AttributeDyn {
address,
key,
value,
} => handle_attribute_dyn(deps, address, key, value),
ExecuteMsg::AttributesDyn {
address,
attributes,
} => handle_attributes_dyn(deps, address, attributes),
}
}

Expand Down Expand Up @@ -52,3 +83,100 @@ fn handle_attributes(deps: DepsMut, attributes: Vec<Attribute>) -> Result<Respon
deps.api.add_attributes(&attributes)?;
Ok(Response::default())
}

/// This issues the given event three times in different ways
fn handle_event_dyn(
deps: DepsMut,
address: Addr,
ty: String,
attributes: Vec<Attribute>,
) -> Result<Response, ContractError> {
let contract = EventsContract { address };

// issue event via dynamic link
contract.add_event_dyn(ty.clone(), attributes.clone());

let event = Event::new(ty).add_attributes(attributes);

// issue event via api
deps.api.add_event(&event)?;

// issue event via response
Ok(Response::default().add_event(event))
}

/// This issues given events three times in different ways
fn handle_events_dyn(
deps: DepsMut,
address: Addr,
events: Vec<Event>,
) -> Result<Response, ContractError> {
let contract = EventsContract { address };

// issue events via dynamic link
contract.add_events_dyn(events.clone());

// issue events via api
deps.api.add_events(&events)?;

// issue events via response
Ok(Response::default().add_events(events))
}

/// This issues the given attribute three times in different ways
fn handle_attribute_dyn(
deps: DepsMut,
address: Addr,
key: String,
value: String,
) -> Result<Response, ContractError> {
let contract = EventsContract { address };

// issue attribute via dynamic link
contract.add_attribute_dyn(key.clone(), value.clone());

// issue attribute via api
deps.api.add_attribute(&key, &value)?;

// issue attribute via response
Ok(Response::default().add_attribute(key, value))
}

/// This issues the given attributes three times in different ways
fn handle_attributes_dyn(
deps: DepsMut,
address: Addr,
attributes: Vec<Attribute>,
) -> Result<Response, ContractError> {
let contract = EventsContract { address };

// issue attributes via dynamic link
contract.add_attributes_dyn(attributes.clone());

// issue attributes via api
deps.api.add_attributes(&attributes)?;

// issue attributes via response
Ok(Response::default().add_attributes(attributes))
}

#[callable_point]
fn add_event_dyn(deps: DepsMut, _env: Env, ty: String, attributes: Vec<Attribute>) {
let event = Event::new(ty).add_attributes(attributes);
deps.api.add_event(&event).unwrap();
}

#[callable_point]
fn add_events_dyn(deps: DepsMut, _env: Env, events: Vec<Event>) {
deps.api.add_events(&events).unwrap();
}

#[callable_point]
fn add_attribute_dyn(deps: DepsMut, _env: Env, key: String, value: String) {
deps.api.add_attribute(&key, &value).unwrap();
}

#[callable_point]
fn add_attributes_dyn(deps: DepsMut, _env: Env, attributes: Vec<Attribute>) {
deps.api.add_attributes(&attributes).unwrap();
}
21 changes: 20 additions & 1 deletion contracts/events/src/msg.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use cosmwasm_std::{Attribute, Event};
use cosmwasm_std::{Addr, Attribute, Event};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

Expand All @@ -23,4 +23,23 @@ pub enum ExecuteMsg {
Attributes {
attributes: Vec<Attribute>,
},
EventDyn {
address: Addr,
#[serde(rename = "type")]
ty: String,
attributes: Vec<Attribute>,
},
EventsDyn {
address: Addr,
events: Vec<Event>,
},
AttributeDyn {
address: Addr,
key: String,
value: String,
},
AttributesDyn {
address: Addr,
attributes: Vec<Attribute>,
},
}

0 comments on commit f46c6e9

Please sign in to comment.