cfx_executor/internal_contract/components/
event.rs

1use cfx_types::H256;
2use solidity_abi::{ABIEncodable, EventIndexEncodable};
3
4use cfx_vm_types::{self as vm, ActionParams};
5
6use super::context::InternalRefContext;
7
8/// Native implementation of a solidity-interface function.
9pub trait SolidityEventTrait: Send + Sync {
10    type Indexed: EventIndexEncodable;
11    type NonIndexed: ABIEncodable;
12    const EVENT_SIG: H256;
13
14    fn log(
15        indexed: &Self::Indexed, non_indexed: &Self::NonIndexed,
16        param: &ActionParams, context: &mut InternalRefContext,
17    ) -> vm::Result<()> {
18        let mut topics = vec![Self::EVENT_SIG];
19        topics.extend_from_slice(&indexed.indexed_event_encode());
20
21        let data = non_indexed.abi_encode();
22
23        context.log(param, context.spec, topics, data)
24    }
25}
26
27#[macro_export]
28macro_rules! make_solidity_event {
29    ( $(#[$attr:meta])* $visibility:vis struct $name:ident ($interface:expr $(, indexed: $indexed:ty)? $(, non_indexed: $non_indexed:ty)?); ) => {
30        $(#[$attr])*
31        #[derive(Copy, Clone)]
32        $visibility struct $name;
33
34        impl SolidityEventTrait for $name {
35            $(type Indexed = $indexed;)?
36            $(type NonIndexed = $non_indexed;)?
37            const EVENT_SIG: H256 = H256(keccak!($interface));
38        }
39    };
40}