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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#[cfg(not(feature = "std"))]
use alloc::{format, string::String, vec, vec::Vec};
use alloc::fmt;

use codec::{Decode, Encode, Output};

use primitive_types::H256;

use crate::*;

pub type TxHash = H256;
pub type BlockHash = H256;
pub type BlockNumber = u32;

#[derive(Clone, Debug)]
pub struct StorageData(pub Vec<u8>);

#[derive(Clone, Debug)]
pub struct StorageKey(pub Vec<u8>);

/// `Encoded` is used to avoid encoding an extra length that isn't needed.
#[derive(Clone, Debug)]
pub struct Encoded(pub Vec<u8>);

impl From<Vec<u8>> for Encoded {
    fn from(data: Vec<u8>) -> Self {
        Self(data)
    }
}

impl<T: Encode> From<&T> for Encoded {
    fn from(v: &T) -> Self {
        Self(v.encode())
    }
}

impl Encode for Encoded {
  fn size_hint(&self) -> usize {
    self.0.len()
  }
  fn encode_to<T: Output + ?Sized>(&self, dest: &mut T) {
    dest.write(&self.0);
  }
}

impl Decode for Encoded {
  fn decode<I: codec::Input>(input: &mut I) -> Result<Self, codec::Error> {
    if let Some(len) = input.remaining_len()? {
      let mut data = vec![0u8; len];
      input.read(&mut data.as_mut_slice())?;
      Ok(Self(data))
    } else {
      let mut data = Vec::new();
      while let Ok(b) = input.read_byte() {
        data.push(b);
      }
      Ok(Self(data))
    }
  }
}

pub trait RuntimeTraits: Clone + Encode + Decode + fmt::Debug {}

impl<T> RuntimeTraits for T where T: Clone + Encode + Decode + fmt::Debug {}

pub struct Call {
  call: Vec<u8>,
}

impl Call {
  pub fn new(call: Vec<u8>) -> Self {
    Self { call }
  }

  pub fn encoded(&self) -> Encoded {
    Encoded(self.call.clone())
  }

  pub fn submit(&self) -> Result<()> {
    let runtime = crate::extension::new_instance();
    Ok(runtime.call_runtime(self.into())?)
  }
}

impl Encode for Call {
  fn size_hint(&self) -> usize {
    self.call.len()
  }
  fn encode_to<T: ::codec::Output + ?Sized>(&self, dest: &mut T) {
    dest.write(&self.call);
  }
}

impl fmt::Debug for Call {
  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    self.call.fmt(f)
  }
}#[derive(Clone, Debug)]
pub struct AccountInfo {
  pub nonce: u32,
}

#[derive(Clone, Debug, Decode, PartialEq, Eq)]
pub enum Phase {
  ApplyExtrinsic(u32),
  Finalization,
  Initialization,
}

#[derive(Clone, Debug, Decode)]
pub struct EventRecord<Event: RuntimeTraits> {
  pub phase: Phase,
  pub event: Event,
  pub topics: Vec<BlockHash>,
}

#[derive(Clone, Debug, Decode, Default)]
pub struct EventRecords<Event: RuntimeTraits>(pub Vec<EventRecord<Event>>);

impl<Event: RuntimeTraits> EventRecords<Event> {
  pub fn from_vec(mut events: Vec<EventRecord<Event>>, filter: Option<Phase>) -> Self {
    if let Some(filter) = filter {
      events.retain(|ev| ev.phase == filter);
    }
    Self(events)
  }

  pub fn to_string(&self) -> String {
    format!("{:#?}", self.0)
  }
}