fvm_std/
event.rs

1use super::prelude::*;
2use super::runtime;
3use serde::{Serialize};
4use crate::utils::encode_topic;
5
6extern crate std;
7
8
9#[derive(Debug, Serialize)]
10pub struct Event<T> where T: Serialize {
11    address: Address,
12    data: T,
13    topics: Vec<H256>,
14}
15
16
17impl<T> Event<T> where T: Serialize {
18    pub fn new(data: T, name: String, topics: Vec<String>) -> Self {
19        if topics.len() == 0 {
20            runtime::revert("topic can not be null")
21        }
22        Self {
23            address: runtime::self_address(),
24            data,
25            topics: {
26                let mut tp: Vec<H256> = Vec::new();
27                tp.push(encode_topic(name));
28                topics.iter().for_each(|x| {
29                    tp.push(encode_topic(x.to_string()));
30                });
31                tp
32            },
33        }
34    }
35    pub fn add_topics(&mut self, topic: String) {
36        self.topics.push(encode_topic(topic));
37    }
38
39    pub fn emit(&self) {
40        let result = serde_json::to_vec(&self).unwrap();
41        runtime::event(&result);
42    }
43}
44