-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathevent.rs
140 lines (122 loc) · 3.28 KB
/
event.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
use near_sdk::{env, json_types::U128, log, serde::Serialize, serde_json, AccountId};
use crate::UnixTimestamp;
pub const PACKAGE_NAME: &str = "sweat_claim";
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
#[derive(Serialize, Debug)]
#[serde(
crate = "near_sdk::serde",
tag = "event",
content = "data",
rename_all = "snake_case"
)]
pub enum EventKind {
Burn(BurnData),
Claim(ClaimData),
Clean(CleanData),
Record(RecordData),
}
#[derive(Serialize, Debug)]
#[serde(crate = "near_sdk::serde")]
pub struct BurnData {
pub burnt_amount: U128,
}
#[derive(Serialize, Debug)]
#[serde(crate = "near_sdk::serde")]
pub struct ClaimData {
pub account_id: AccountId,
pub claimed: U128,
pub burnt: U128,
}
#[derive(Serialize, Debug)]
#[serde(crate = "near_sdk::serde")]
pub struct CleanData {
pub account_ids: Vec<AccountId>,
}
#[derive(Serialize, Debug)]
#[serde(crate = "near_sdk::serde")]
pub struct RecordData {
pub timestamp: UnixTimestamp,
pub amounts: Vec<(AccountId, RecordAmountDetailed)>,
}
#[derive(Serialize, Debug)]
#[serde(crate = "near_sdk::serde")]
pub struct RecordAmountDetailed {
pub credited: U128,
pub burnt: U128,
}
impl RecordData {
pub fn new(timestamp: UnixTimestamp) -> Self {
Self {
timestamp,
amounts: vec![],
}
}
}
#[derive(Serialize, Debug)]
#[serde(crate = "near_sdk::serde", rename_all = "snake_case")]
struct SweatClaimEvent {
standard: &'static str,
version: &'static str,
#[serde(flatten)]
event_kind: EventKind,
}
impl From<EventKind> for SweatClaimEvent {
fn from(event_kind: EventKind) -> Self {
Self {
standard: PACKAGE_NAME,
version: VERSION,
event_kind,
}
}
}
pub fn emit(event: EventKind) {
log!(SweatClaimEvent::from(event).to_json_event_string());
}
impl SweatClaimEvent {
fn to_json_string(&self) -> String {
serde_json::to_string(self)
.unwrap_or_else(|err| env::panic_str(&format!("Failed to serialize SweatClaimEvent: {err}")))
}
fn to_json_event_string(&self) -> String {
format!("EVENT_JSON:{}", self.to_json_string())
}
}
#[cfg(test)]
mod test {
use near_sdk::json_types::U128;
use crate::event::{BurnData, EventKind, SweatClaimEvent};
#[test]
fn event_to_string() {
assert_eq!(
strip(
SweatClaimEvent::from(EventKind::Burn(BurnData {
burnt_amount: U128(100_000_000),
}))
.to_json_event_string()
.as_str()
),
strip(
r#"EVENT_JSON:{
"standard": "sweat_claim",
"version": "1.0.0",
"event": "burn",
"data": {
"burnt_amount": "100000000"
}}"#
)
)
}
fn strip(s: &str) -> String {
let without_newlines: String = s.chars().filter(|&c| c != '\n').collect();
let mut previous_char = ' ';
let result: String = without_newlines
.chars()
.filter(|&c| {
let keep = !(c == ' ' && previous_char == ' ');
previous_char = c;
keep
})
.collect();
result
}
}