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
use ethereum::Log;
use primitive_types::{H160, H256, U256};

use crate::types::Address;

/// Implementation of this trait should not be written manually,
/// instead use [`crate::ToLog`] proc macros.
///
/// See also [`evm_coder_procedural::ToLog`], [solidity docs on events](https://docs.soliditylang.org/en/develop/contracts.html#events)
pub trait ToLog {
	/// Convert event to [`ethereum::Log`].
	/// Because event by itself doesn't contains current contract
	/// address, it should be specified manually.
	fn to_log(&self, contract: H160) -> Log;
}

/// Only items implementing `ToTopic` may be used as `#[indexed]` field
/// in [`crate::ToLog`] macro usage.
///
/// See also (solidity docs on events)[<https://docs.soliditylang.org/en/develop/contracts.html#events>]
pub trait ToTopic {
	/// Convert value to topic to be used in [`ethereum::Log`]
	fn to_topic(&self) -> H256;
}

impl ToTopic for H256 {
	fn to_topic(&self) -> H256 {
		*self
	}
}

impl ToTopic for U256 {
	fn to_topic(&self) -> H256 {
		let mut out = [0u8; 32];
		self.to_big_endian(&mut out);
		H256(out)
	}
}

impl ToTopic for Address {
	fn to_topic(&self) -> H256 {
		let mut out = [0u8; 32];
		out[12..32].copy_from_slice(&self.0);
		H256(out)
	}
}

impl ToTopic for u32 {
	fn to_topic(&self) -> H256 {
		let mut out = [0u8; 32];
		out[28..32].copy_from_slice(&self.to_be_bytes());
		H256(out)
	}
}