polymesh_api_ink/
block.rs

1use alloc::fmt;
2#[cfg(not(feature = "std"))]
3use alloc::{format, string::String, vec, vec::Vec};
4
5use codec::{Decode, Encode, Output};
6
7use primitive_types::H256;
8
9use crate::*;
10
11pub type TxHash = H256;
12pub type BlockHash = H256;
13pub type BlockNumber = u32;
14
15#[derive(Clone, Debug)]
16pub struct StorageData(pub Vec<u8>);
17
18#[derive(Clone, Debug)]
19pub struct StorageKey(pub Vec<u8>);
20
21/// `Encoded` is used to avoid encoding an extra length that isn't needed.
22#[derive(Clone, Debug)]
23pub struct Encoded(pub Vec<u8>);
24
25impl From<Vec<u8>> for Encoded {
26  fn from(data: Vec<u8>) -> Self {
27    Self(data)
28  }
29}
30
31impl<T: Encode> From<&T> for Encoded {
32  fn from(v: &T) -> Self {
33    Self(v.encode())
34  }
35}
36
37impl Encode for Encoded {
38  fn size_hint(&self) -> usize {
39    self.0.len()
40  }
41  fn encode_to<T: Output + ?Sized>(&self, dest: &mut T) {
42    dest.write(&self.0);
43  }
44}
45
46impl Decode for Encoded {
47  fn decode<I: codec::Input>(input: &mut I) -> Result<Self, codec::Error> {
48    if let Some(len) = input.remaining_len()? {
49      let mut data = vec![0u8; len];
50      input.read(&mut data.as_mut_slice())?;
51      Ok(Self(data))
52    } else {
53      let mut data = Vec::new();
54      while let Ok(b) = input.read_byte() {
55        data.push(b);
56      }
57      Ok(Self(data))
58    }
59  }
60}
61
62pub trait RuntimeTraits: Clone + Encode + Decode + fmt::Debug {}
63
64impl<T> RuntimeTraits for T where T: Clone + Encode + Decode + fmt::Debug {}
65
66pub struct Call {
67  call: Vec<u8>,
68}
69
70impl Call {
71  pub fn new(call: Vec<u8>) -> Self {
72    Self { call }
73  }
74
75  pub fn encoded(&self) -> Encoded {
76    Encoded(self.call.clone())
77  }
78
79  pub fn submit(&self) -> Result<()> {
80    let runtime = crate::extension::new_instance();
81    let call_runtime_res = runtime.call_runtime_with_error(self.into())?;
82    Ok(call_runtime_res?)
83  }
84}
85
86impl Encode for Call {
87  fn size_hint(&self) -> usize {
88    self.call.len()
89  }
90  fn encode_to<T: ::codec::Output + ?Sized>(&self, dest: &mut T) {
91    dest.write(&self.call);
92  }
93}
94
95impl fmt::Debug for Call {
96  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
97    self.call.fmt(f)
98  }
99}
100#[derive(Clone, Debug)]
101pub struct AccountInfo {
102  pub nonce: u32,
103}
104
105#[derive(Clone, Debug, Decode, PartialEq, Eq)]
106pub enum Phase {
107  ApplyExtrinsic(u32),
108  Finalization,
109  Initialization,
110}
111
112#[derive(Clone, Debug, Decode)]
113pub struct EventRecord<Event: RuntimeTraits> {
114  pub phase: Phase,
115  pub event: Event,
116  pub topics: Vec<BlockHash>,
117}
118
119#[derive(Clone, Debug, Decode, Default)]
120pub struct EventRecords<Event: RuntimeTraits>(pub Vec<EventRecord<Event>>);
121
122impl<Event: RuntimeTraits> EventRecords<Event> {
123  pub fn from_vec(mut events: Vec<EventRecord<Event>>, filter: Option<Phase>) -> Self {
124    if let Some(filter) = filter {
125      events.retain(|ev| ev.phase == filter);
126    }
127    Self(events)
128  }
129
130  pub fn to_string(&self) -> String {
131    format!("{:#?}", self.0)
132  }
133}