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
use std::str::FromStr;
use thiserror::Error;
use web3::types::{H160, H256, U256};
#[derive(Error, Debug, Clone)]
pub enum EventParseError {
#[error("no topics")]
NoTopics,
#[error("{0} topics found, {1} expected")]
InvalidTopics(usize, usize),
#[error("{0} data length, {1} bytes expected")]
InvalidDataSize(usize, usize),
}
pub struct LogReader {
pub topics: Vec<H256>,
pub data: Vec<u8>,
pub current_topic: usize,
pub data_offset: usize,
}
impl LogReader {
pub fn new(
log: &web3::types::Log,
expected_topics: usize,
expected_data_size: Option<usize>,
) -> Result<Self, EventParseError> {
if log.topics.len() == 0 {
return Err(EventParseError::NoTopics);
}
if log.topics.len() - 1 != expected_topics {
return Err(EventParseError::InvalidTopics(
log.topics.len() - 1,
expected_topics,
));
}
let data: &Vec<u8> = &log.data.0;
if let Some(sz) = expected_data_size {
if data.len() != sz * 32 {
return Err(EventParseError::InvalidDataSize(data.len(), sz));
}
}
Ok(Self {
current_topic: 1,
data_offset: 0,
topics: log.topics.clone(),
data: data.clone(),
})
}
fn has_topics(&self) -> bool {
self.topics.len() > self.current_topic
}
fn has_data(&self) -> bool {
self.data.len() > self.data_offset
}
fn next32(&mut self) -> String {
if self.has_topics() {
let hex_str = format!("{:?}", self.topics.get(self.current_topic).unwrap());
self.current_topic += 1;
hex_str[2..].to_owned()
} else {
let hex_str = hex::encode(&self.data);
let offs: usize = 2 * self.data_offset;
let res: String = hex_str.chars().skip(offs).take(64).collect();
self.data_offset += 32;
res
}
}
pub fn text(&mut self) -> String {
let _hex_size = self.next32();
let _str_size = self.next32();
let mut s = String::from("");
while self.has_data() {
let nextword = self.next32();
let bts: Vec<u8> = hex::decode(nextword).unwrap();
bts.iter().filter(|ch| **ch != 0).for_each(|ch| {
if *ch == 0x1F {
s.push('|');
} else if *ch == '\\' as u8
|| *ch == '"' as u8
|| *ch == '\'' as u8
|| *ch == '<' as u8
|| *ch == '>' as u8
{
s.push(' ');
} else if *ch > 0x1F && *ch < 0x80 {
s.push(*ch as char);
}
});
}
s
}
pub fn address(&mut self) -> H160 {
let hex_str = self.next32();
H160::from_str(&hex_str[24..]).unwrap()
}
pub fn addresses(&mut self) -> Vec<H160> {
let _ = self.next32();
let _ = self.next32();
let mut res = vec![];
while self.has_data() {
res.push(self.address());
}
res
}
pub fn value(&mut self) -> U256 {
let hex_str = self.next32();
U256::from_str(hex_str.as_str()).unwrap()
}
pub fn value224_32(&mut self) -> (U256, u64) {
let packed = self.value();
let mut value = packed;
value.0[0] = value.0[0] & 0xFFFFFFFF;
let timestamp = packed.0[0];
(value, timestamp)
}
pub fn skip(&mut self) {
let _ = self.value();
}
pub fn values(&mut self) -> Vec<U256> {
let mut res = vec![];
while self.has_data() {
res.push(self.value());
}
res
}
pub fn bool(&mut self) -> bool {
let val = self.value().as_u64();
val > 0
}
}
#[cfg(test)]
mod tests {
use super::*;
use hex_literal::hex;
use web3::types::{Address, Log};
#[test]
pub fn test_it_reads() {
let log = Log {
address: Address::from_low_u64_be(1),
topics: vec![
hex!("06fbd2297e6f6f7701a9cf99685a6af911cab275ec5c75ac7aaaf13b5cf3d61f").into(),
hex!("000000000000000000000000061b8335e1d2042975c4ed849943334bd07fb504").into(),
],
data: hex!("0000000000000000000000000000000000000000000000056bc75e2d631000000000000000000000000000000000000000000000000000056bb73f60696ee4160000000000000000000000000000000000000000000000000000000060da02bd").into(),
block_hash: Some(H256::from_low_u64_be(2)),
block_number: Some(1.into()),
transaction_hash: Some(H256::from_low_u64_be(3)),
transaction_index: Some(0.into()),
log_index: Some(0.into()),
transaction_log_index: Some(0.into()),
log_type: None,
removed: Some(true),
};
let mut r = LogReader::new(&log, 1, Some(3)).unwrap();
assert_eq!(
r.address(),
hex!("061b8335e1d2042975c4ed849943334bd07fb504").into()
);
assert_eq!(
r.value(),
hex!("0000000000000000000000000000000000000000000000056bc75e2d63100000").into()
);
assert_eq!(
r.value(),
hex!("0000000000000000000000000000000000000000000000056bb73f60696ee416").into()
);
assert_eq!(
r.value(),
hex!("0000000000000000000000000000000000000000000000000000000060da02bd").into()
);
}
#[test]
pub fn test_reads_meta_data() {
let log = Log {
address: Address::from_low_u64_be(1),
topics: vec![
hex!("4d72fe0577a3a3f7da968d7b892779dde102519c25527b29cf7054f245c791b9").into(),
hex!("0000000000000000000000000000000000000000000000000000000000000000").into(),
hex!("000000000000000000000000061b8335e1d2042975c4ed849943334bd07fb504").into()
],
data: hex!(
"00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000047311f7472616e7366657228616464726573732c75696e74323536291f4d7920666972737420415049332070726f706f73616c1f466f722074657374696e6720707572706f73657300000000000000000000000000000000000000000000000000"
).into(),
block_hash: Some(H256::from_low_u64_be(2)),
block_number: Some(1.into()),
transaction_hash: Some(H256::from_low_u64_be(3)),
transaction_index: Some(0.into()),
log_index: Some(0.into()),
transaction_log_index: Some(0.into()),
log_type: None,
removed: Some(true),
};
let mut r = LogReader::new(&log, 2, None).unwrap();
assert_eq!(
r.value(),
hex!("0000000000000000000000000000000000000000000000000000000000000000").into()
);
assert_eq!(
r.address(),
hex!("061b8335e1d2042975c4ed849943334bd07fb504").into()
);
assert_eq!(
r.text(),
"1|transfer(address,uint256)|My first API3 proposal|For testing purposes"
);
}
}