ac_node_api/
lib.rs

1/*
2	Copyright 2021 Integritee AG and Supercomputing Systems AG
3	Licensed under the Apache License, Version 2.0 (the "License");
4	you may not use this file except in compliance with the License.
5	You may obtain a copy of the License at
6		http://www.apache.org/licenses/LICENSE-2.0
7	Unless required by applicable law or agreed to in writing, software
8	distributed under the License is distributed on an "AS IS" BASIS,
9	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10	See the License for the specific language governing permissions and
11	limitations under the License.
12*/
13
14//! Contains stuff to instantiate communication with a substrate node.
15
16#![cfg_attr(not(feature = "std"), no_std)]
17
18extern crate alloc;
19
20use alloc::{borrow::ToOwned, vec::Vec};
21use codec::{Decode, Encode};
22
23pub use alloc::{collections::BTreeMap, vec};
24pub use events::{EventDetails, Events, RawEventDetails};
25pub use metadata::{Metadata, MetadataError};
26pub use scale_decode::DecodeAsType;
27
28pub mod error;
29pub mod events;
30pub mod metadata;
31pub mod storage;
32
33#[cfg(any(feature = "mocks", test))]
34pub mod test_utils;
35
36/// Wraps an already encoded byte vector, prevents being encoded as a raw byte vector as part of
37/// the transaction payload
38#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
39pub struct Encoded(pub Vec<u8>);
40
41impl codec::Encode for Encoded {
42	fn encode(&self) -> Vec<u8> {
43		self.0.to_owned()
44	}
45}
46
47// This following types were taken from subxt (Parity Technologies (UK))
48// https://github.com/paritytech/subxt/
49
50/// Trait to uniquely identify the events's identity from the runtime metadata.
51///
52/// Generated API structures that represent an event implement this trait.
53///
54/// The trait is utilized to decode emitted events from a block, via obtaining the
55/// form of the `Event` from the metadata.
56pub trait StaticEvent: Decode {
57	/// Pallet name.
58	const PALLET: &'static str;
59	/// Event name.
60	const EVENT: &'static str;
61
62	/// Returns true if the given pallet and event names match this event.
63	fn is_event(pallet: &str, event: &str) -> bool {
64		Self::PALLET == pallet && Self::EVENT == event
65	}
66}
67
68/// A phase of a block's execution.
69// https://github.com/paritytech/substrate/blob/2bfc1dd66ef32cf8beb90007dfb544a9d28f1b2f/frame/system/src/lib.rs#L698-L708
70#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Encode, Decode)]
71pub enum Phase {
72	/// Applying an extrinsic.
73	ApplyExtrinsic(u32),
74	/// Finalizing the block.
75	Finalization,
76	/// Initializing the block.
77	Initialization,
78}
79
80/// Record of an event happening.
81// https://github.com/paritytech/substrate/blob/2bfc1dd66ef32cf8beb90007dfb544a9d28f1b2f/frame/system/src/lib.rs#L716-L726
82#[derive(Encode, Decode, PartialEq, Eq, Clone, Debug)]
83pub struct EventRecord<E, T> {
84	/// The phase of the block it happened in.
85	pub phase: Phase,
86	/// The event itself.
87	pub event: E,
88	/// The list of the topics this event has.
89	pub topics: Vec<T>,
90}