-
Notifications
You must be signed in to change notification settings - Fork 3
/
events.rs
274 lines (253 loc) · 9.33 KB
/
events.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
use crate::*;
use near_sdk::serde_json::json;
const EVENT_STANDARD: &str = "ref-ve";
const EVENT_STANDARD_VERSION: &str = "1.0.0";
#[derive(Serialize, Debug, Clone)]
#[serde(crate = "near_sdk::serde")]
#[serde(tag = "event", content = "data")]
#[serde(rename_all = "snake_case")]
pub enum Event<'a> {
ProposalCreate {
proposer_id: &'a AccountId,
proposal_id: u32,
kind: &'a String,
start_at: u64,
duration_sec: u32,
},
ProposalRemove {
proposer_id: &'a AccountId,
proposal_id: u32,
},
RemovedProposalAssets {
receiver_id: &'a AccountId,
token_id: &'a AccountId,
amount: &'a U128,
success: bool,
},
ActionProposal {
voter_id: &'a AccountId,
proposal_id: u32,
action: &'a String,
},
ActionCancel {
voter_id: &'a AccountId,
proposal_id: u32,
action: &'a String,
},
LptWithdraw {
caller_id: &'a AccountId,
withdraw_amount: &'a U128,
success: bool,
},
LptWithdrawLostfound {
receiver_id: &'a AccountId,
withdraw_amount: &'a U128,
success: bool,
},
RewardWithdraw {
caller_id: &'a AccountId,
token_id: &'a AccountId,
withdraw_amount: &'a U128,
success: bool,
},
RewardLostfound {
caller_id: &'a AccountId,
token_id: &'a AccountId,
withdraw_amount: &'a U128,
},
RewardDeposit {
caller_id: &'a AccountId,
proposal_id: u32,
incentive_key: u32,
token_id: &'a AccountId,
deposit_amount: &'a U128,
total_amount: &'a U128,
start_at: u64,
},
LptLock {
caller_id: &'a AccountId,
deposit_amount: &'a U128,
increased_ve_lpt: &'a U128,
duration: u32,
},
LptAppend {
caller_id: &'a AccountId,
deposit_amount: &'a U128,
increased_ve_lpt: &'a U128,
duration: u32,
},
}
impl Event<'_> {
pub fn emit(&self) {
emit_event(&self);
}
}
// Emit event that follows NEP-297 standard: https://nomicon.io/Standards/EventsFormat
// Arguments
// * `standard`: name of standard, e.g. nep171
// * `version`: e.g. 1.0.0
// * `event`: type of the event, e.g. nft_mint
// * `data`: associate event data. Strictly typed for each set {standard, version, event} inside corresponding NEP
pub (crate) fn emit_event<T: ?Sized + Serialize>(data: &T) {
let result = json!(data);
let event_json = json!({
"standard": EVENT_STANDARD,
"version": EVENT_STANDARD_VERSION,
"event": result["event"],
"data": [result["data"]]
})
.to_string();
log!(format!("EVENT_JSON:{}", event_json));
}
#[cfg(test)]
mod tests {
use super::*;
use near_sdk::{test_utils, AccountId};
fn alice() -> AccountId {
AccountId::new_unchecked("alice".to_string())
}
fn token_id() -> AccountId {
AccountId::new_unchecked("ref".to_string())
}
#[test]
fn event_proposal_create() {
let proposer_id = &alice();
let proposal_id = 0;
let kind = &format!("{:?}", ProposalKind::FarmingReward{ farm_list: vec!["noct.near|nref.near&2657".to_string(), "nusdt.near|nusdc.near|ndai.near&1910".to_string()], total_reward: 2});
let start_at = 1000_u64;
let duration_sec = 500_u32;
Event::ProposalCreate { proposer_id, proposal_id, kind, start_at, duration_sec }.emit();
assert_eq!(
test_utils::get_logs()[0],
r#"EVENT_JSON:{"standard":"ref-ve","version":"1.0.0","event":"proposal_create","data":[{"proposer_id":"alice","proposal_id":0,"kind":"FarmingReward { farm_list: [\"noct.near|nref.near&2657\", \"nusdt.near|nusdc.near|ndai.near&1910\"], total_reward: 2 }","start_at":1000,"duration_sec":500}]}"#
);
}
#[test]
fn event_proposal_remove() {
let proposer_id = &alice();
let proposal_id = 0;
Event::ProposalRemove { proposer_id, proposal_id }.emit();
assert_eq!(
test_utils::get_logs()[0],
r#"EVENT_JSON:{"standard":"ref-ve","version":"1.0.0","event":"proposal_remove","data":[{"proposer_id":"alice","proposal_id":0}]}"#
);
}
#[test]
fn event_removed_proposal_assets() {
let receiver_id = &alice();
let token_id = &token_id();
let amount = &U128(100);
let success = true;
Event::RemovedProposalAssets { receiver_id, token_id, amount, success }.emit();
assert_eq!(
test_utils::get_logs()[0],
r#"EVENT_JSON:{"standard":"ref-ve","version":"1.0.0","event":"removed_proposal_assets","data":[{"receiver_id":"alice","token_id":"ref","amount":"100","success":true}]}"#
);
}
#[test]
fn event_action_proposal() {
let voter_id = &alice();
let proposal_id = 0;
let action = &format!("{:?}", Action::VoteApprove);
Event::ActionProposal { voter_id, proposal_id, action }.emit();
assert_eq!(
test_utils::get_logs()[0],
r#"EVENT_JSON:{"standard":"ref-ve","version":"1.0.0","event":"action_proposal","data":[{"voter_id":"alice","proposal_id":0,"action":"VoteApprove"}]}"#
);
}
#[test]
fn event_action_cancel() {
let voter_id = &alice();
let proposal_id = 0;
let action = &format!("{:?}", Action::VoteApprove);
Event::ActionCancel { voter_id, proposal_id, action }.emit();
assert_eq!(
test_utils::get_logs()[0],
r#"EVENT_JSON:{"standard":"ref-ve","version":"1.0.0","event":"action_cancel","data":[{"voter_id":"alice","proposal_id":0,"action":"VoteApprove"}]}"#
);
}
#[test]
fn event_lpt_withdraw() {
let caller_id = &alice();
let withdraw_amount = &U128(100);
let success = true;
Event::LptWithdraw { caller_id, withdraw_amount, success }.emit();
assert_eq!(
test_utils::get_logs()[0],
r#"EVENT_JSON:{"standard":"ref-ve","version":"1.0.0","event":"lpt_withdraw","data":[{"caller_id":"alice","withdraw_amount":"100","success":true}]}"#
);
}
#[test]
fn event_lpt_withdraw_lostfound() {
let receiver_id = &alice();
let withdraw_amount = &U128(100);
let success = true;
Event::LptWithdrawLostfound { receiver_id, withdraw_amount, success }.emit();
assert_eq!(
test_utils::get_logs()[0],
r#"EVENT_JSON:{"standard":"ref-ve","version":"1.0.0","event":"lpt_withdraw_lostfound","data":[{"receiver_id":"alice","withdraw_amount":"100","success":true}]}"#
);
}
#[test]
fn event_reward_withdraw() {
let caller_id = &alice();
let token_id = &token_id();
let withdraw_amount = &U128(100);
let success = true;
Event::RewardWithdraw { caller_id, token_id, withdraw_amount, success }.emit();
assert_eq!(
test_utils::get_logs()[0],
r#"EVENT_JSON:{"standard":"ref-ve","version":"1.0.0","event":"reward_withdraw","data":[{"caller_id":"alice","token_id":"ref","withdraw_amount":"100","success":true}]}"#
);
}
#[test]
fn event_reward_lostfound() {
let caller_id = &alice();
let token_id = &token_id();
let withdraw_amount = &U128(100);
Event::RewardLostfound { caller_id, token_id, withdraw_amount }.emit();
assert_eq!(
test_utils::get_logs()[0],
r#"EVENT_JSON:{"standard":"ref-ve","version":"1.0.0","event":"reward_lostfound","data":[{"caller_id":"alice","token_id":"ref","withdraw_amount":"100"}]}"#
);
}
#[test]
fn event_reward_deposit() {
let caller_id = &alice();
let proposal_id = 0;
let incentive_key = 0;
let token_id = &token_id();
let deposit_amount = &U128(100);
let total_amount = &U128(1000);
let start_at = 1000000;
Event::RewardDeposit { caller_id, proposal_id, incentive_key, token_id, deposit_amount, total_amount, start_at }.emit();
assert_eq!(
test_utils::get_logs()[0],
r#"EVENT_JSON:{"standard":"ref-ve","version":"1.0.0","event":"reward_deposit","data":[{"caller_id":"alice","proposal_id":0,"incentive_key":0,"token_id":"ref","deposit_amount":"100","total_amount":"1000","start_at":1000000}]}"#
);
}
#[test]
fn event_lpt_deposit() {
let caller_id = &alice();
let deposit_amount = &U128(100);
let increased_ve_lpt = &U128(200);
let duration = 1000000;
Event::LptLock { caller_id, deposit_amount, increased_ve_lpt, duration }.emit();
assert_eq!(
test_utils::get_logs()[0],
r#"EVENT_JSON:{"standard":"ref-ve","version":"1.0.0","event":"lpt_lock","data":[{"caller_id":"alice","deposit_amount":"100","increased_ve_lpt":"200","duration":1000000}]}"#
);
}
#[test]
fn event_lpt_append() {
let caller_id = &alice();
let deposit_amount = &U128(100);
let increased_ve_lpt = &U128(200);
let duration = 1000000;
Event::LptAppend { caller_id, deposit_amount, increased_ve_lpt, duration }.emit();
assert_eq!(
test_utils::get_logs()[0],
r#"EVENT_JSON:{"standard":"ref-ve","version":"1.0.0","event":"lpt_append","data":[{"caller_id":"alice","deposit_amount":"100","increased_ve_lpt":"200","duration":1000000}]}"#
);
}
}