evm_coder/
events.rs

1use ethereum::Log;
2use primitive_types::{H160, H256, U256};
3
4use crate::types::Address;
5
6/// Implementation of this trait should not be written manually,
7/// instead use [`crate::ToLog`] proc macros.
8///
9/// See also [`evm_coder_procedural::ToLog`], [solidity docs on events](https://docs.soliditylang.org/en/develop/contracts.html#events)
10pub trait ToLog {
11	/// Convert event to [`ethereum::Log`].
12	/// Because event by itself doesn't contains current contract
13	/// address, it should be specified manually.
14	fn to_log(&self, contract: H160) -> Log;
15}
16
17/// Only items implementing `ToTopic` may be used as `#[indexed]` field
18/// in [`crate::ToLog`] macro usage.
19///
20/// See also (solidity docs on events)[<https://docs.soliditylang.org/en/develop/contracts.html#events>]
21pub trait ToTopic {
22	/// Convert value to topic to be used in [`ethereum::Log`]
23	fn to_topic(&self) -> H256;
24}
25
26impl ToTopic for H256 {
27	fn to_topic(&self) -> H256 {
28		*self
29	}
30}
31
32impl ToTopic for U256 {
33	fn to_topic(&self) -> H256 {
34		let mut out = [0u8; 32];
35		self.to_big_endian(&mut out);
36		H256(out)
37	}
38}
39
40impl ToTopic for Address {
41	fn to_topic(&self) -> H256 {
42		let mut out = [0u8; 32];
43		out[12..32].copy_from_slice(&self.0);
44		H256(out)
45	}
46}
47
48impl ToTopic for u32 {
49	fn to_topic(&self) -> H256 {
50		let mut out = [0u8; 32];
51		out[28..32].copy_from_slice(&self.to_be_bytes());
52		H256(out)
53	}
54}