forked from near-daos/sputnik-dao-contract
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add events * add tests * Update astra/src/events.rs Co-authored-by: Robert Zaremba <robert@zaremba.ch> --------- Co-authored-by: Robert Zaremba <robert@zaremba.ch>
- Loading branch information
1 parent
c5fac1d
commit bf7b322
Showing
8 changed files
with
165 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use near_sdk::serde::Serialize; | ||
use serde_json::json; | ||
|
||
use common::{EventPayload, NearEvent}; | ||
|
||
fn emit_event<T: Serialize>(event: EventPayload<T>) { | ||
NearEvent { | ||
standard: "astra++", | ||
version: "1.0.0", | ||
event, | ||
} | ||
.emit(); | ||
} | ||
|
||
pub(crate) fn emit_veto(prop_id: u64) { | ||
emit_event(EventPayload { | ||
event: "veto", | ||
data: json!({ "prop_id": prop_id }), | ||
}); | ||
} | ||
|
||
pub(crate) fn emit_dissolve() { | ||
emit_event(EventPayload { | ||
event: "dissolve", | ||
data: "", | ||
}) | ||
} | ||
|
||
#[cfg(test)] | ||
mod unit_tests { | ||
use near_sdk::{test_utils}; | ||
|
||
use super::*; | ||
|
||
#[test] | ||
fn log_hooks() { | ||
let expected1 = r#"EVENT_JSON:{"standard":"astra++","version":"1.0.0","event":"veto","data":{"prop_id":21}}"#; | ||
let expected2 = r#"EVENT_JSON:{"standard":"astra++","version":"1.0.0","event":"dissolve","data":"dao is dissolved"}"#; | ||
emit_veto(21); | ||
assert_eq!(vec![expected1], test_utils::get_logs()); | ||
emit_dissolve(); | ||
assert_eq!(vec![expected1, expected2], test_utils::get_logs()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[package] | ||
name = "common" | ||
version = "1.0.0" | ||
authors = { workspace = true } | ||
edition = { workspace = true } | ||
repository = { workspace = true } | ||
license = { workspace = true } | ||
|
||
[dependencies] | ||
uint.workspace = true | ||
near-sdk.workspace = true | ||
serde_json.workspace = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Set of common functions and structures | ||
|
||
- event handling |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
use near_sdk::env; | ||
use near_sdk::serde::Serialize; | ||
|
||
/// Helper struct to create Standard NEAR Event JSON. | ||
/// Arguments: | ||
/// * `standard`: name of standard e.g. nep171 | ||
/// * `version`: e.g. 1.0.0 | ||
/// * `event`: associate event data | ||
#[derive(Serialize)] | ||
#[serde(crate = "near_sdk::serde")] | ||
pub struct NearEvent<T: Serialize> { | ||
pub standard: &'static str, | ||
pub version: &'static str, | ||
|
||
// `flatten` to not have "event": {<EventVariant>} in the JSON, just have the contents of {<EventVariant>}. | ||
#[serde(flatten)] | ||
pub event: T, | ||
} | ||
|
||
impl<T: Serialize> NearEvent<T> { | ||
pub fn to_json_event_string(&self) -> String { | ||
let s = serde_json::to_string(&self) | ||
.ok() | ||
.unwrap_or_else(|| env::abort()); | ||
format!("EVENT_JSON:{}", s) | ||
} | ||
|
||
pub fn emit(self) { | ||
env::log_str(&self.to_json_event_string()); | ||
} | ||
} | ||
|
||
/// Helper struct to be used in `NearEvent.event` to construct NEAR Event compatible payload | ||
#[derive(Serialize)] | ||
#[serde(crate = "near_sdk::serde")] | ||
pub struct EventPayload<T: Serialize> { | ||
/// event name | ||
pub event: &'static str, | ||
/// event payload | ||
pub data: T, | ||
} | ||
|
||
impl<T: Serialize> EventPayload<T> { | ||
pub fn emit(self, standard: &'static str, version: &'static str) { | ||
NearEvent { | ||
standard, | ||
version, | ||
event: self, | ||
} | ||
.emit() | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use near_sdk::{test_utils, AccountId}; | ||
|
||
use super::*; | ||
|
||
fn alice() -> AccountId { | ||
AccountId::new_unchecked("alice.near".to_string()) | ||
} | ||
|
||
#[test] | ||
fn emit_event_payload() { | ||
let expected = r#"EVENT_JSON:{"standard":"nepXYZ","version":"1.0.1","event":"mint","data":["alice.near",[821,10,44]]}"#; | ||
let tokens = vec![821, 10, 44]; | ||
let event = EventPayload { | ||
event: "mint", | ||
data: (alice(), tokens), | ||
}; | ||
event.emit("nepXYZ", "1.0.1"); | ||
assert_eq!(vec![expected], test_utils::get_logs()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
mod events; | ||
|
||
pub use events::*; |