komple_framework_utils/
response.rs

1use cosmwasm_std::{Attribute, Event, Response};
2use std::string::ToString;
3
4pub struct ResponseHelper;
5impl ResponseHelper {
6    pub fn new_module(module: &str, action: &str) -> Response {
7        Response::new()
8            .add_attribute("name", "komple_framework")
9            .add_attribute("module", module)
10            .add_attribute("action", action)
11    }
12
13    pub fn new_permission(permission: &str, action: &str) -> Response {
14        Response::new()
15            .add_attribute("name", "komple_framework")
16            .add_attribute("permission", permission)
17            .add_attribute("action", action)
18    }
19}
20
21pub struct EventHelper {
22    event: Event,
23}
24
25impl EventHelper {
26    pub fn new(event_name: impl Into<String>) -> EventHelper {
27        Self {
28            event: Event::new(event_name),
29        }
30    }
31
32    pub fn check_add_attribute<T: ToString>(
33        &mut self,
34        check: &Option<T>,
35        key: &str,
36        value: impl Into<String>,
37    ) -> EventHelper {
38        if check.is_none() {
39            return Self {
40                event: self.event.clone(),
41            };
42        };
43        Self {
44            event: self.get().add_attribute(key, value),
45        }
46    }
47
48    pub fn add_attribute(self, key: impl Into<String>, value: impl Into<String>) -> EventHelper {
49        Self {
50            event: self.get().add_attribute(key, value),
51        }
52    }
53
54    pub fn add_attributes(self, attributes: Vec<Attribute>) -> EventHelper {
55        Self {
56            event: self.get().add_attributes(attributes),
57        }
58    }
59
60    pub fn get(&self) -> Event {
61        self.event.clone()
62    }
63}