use super::prelude::*;
use super::runtime;
use serde::{Serialize};
use crate::utils::encode_topic;
extern crate std;
#[derive(Debug, Serialize)]
pub struct Event<T> where T: Serialize {
address: Address,
data: T,
topics: Vec<H256>,
}
impl<T> Event<T> where T: Serialize {
pub fn new(data: T, name: String, topics: Vec<String>) -> Self {
if topics.len() == 0 {
runtime::revert("topic can not be null")
}
Self {
address: runtime::self_address(),
data,
topics: {
let mut tp: Vec<H256> = Vec::new();
tp.push(encode_topic(name));
topics.iter().for_each(|x| {
tp.push(encode_topic(x.to_string()));
});
tp
},
}
}
pub fn add_topics(&mut self, topic: String) {
self.topics.push(encode_topic(topic));
}
pub fn emit(&self) {
let result = serde_json::to_vec(&self).unwrap();
runtime::event(&result);
}
}