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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#![feature(const_fn_trait_bound)]
use parity_scale_codec::{Compact, Decode, Encode, Error, Input};
use serde::{Deserialize, Serialize};
pub use sp_core::crypto::{AccountId32, Ss58AddressFormat, Ss58AddressFormatRegistry, Ss58Codec};
pub use sp_core::ecdsa::{Public, Signature};
pub use sp_core::{blake2_256, H256};
use crate::utils::deser_number_or_hex;
pub mod client;
pub mod pallets;
pub mod rpc;
mod utils;
pub type GenericAddress = MultiAddress<AccountId32, ()>;
pub type Balance = u128;
#[derive(Clone, Debug, Decode, Encode, PartialEq)]
pub enum MultiAddress<AccountId, AccountIndex> {
Id(AccountId),
Index(#[codec(compact)] AccountIndex),
Raw(Vec<u8>),
Address32([u8; 32]),
Address20([u8; 20]),
}
impl From<AccountId32> for GenericAddress {
fn from(a: AccountId32) -> Self {
MultiAddress::Id(a)
}
}
impl From<Public> for GenericAddress {
fn from(p: Public) -> Self {
let acct = public_into_account(p);
MultiAddress::Id(acct)
}
}
pub fn public_into_account(p: Public) -> AccountId32 {
let hash = blake2_256(&p.0);
hash.into()
}
#[derive(Clone, Debug, Decode, Encode, PartialEq)]
pub enum MultiSignature {
#[codec(index = 2)]
Ecdsa(Signature),
}
pub type SignedExtra = (u32, u32, H256, H256, (), (), ());
#[derive(Clone, Copy, Debug, Decode, Encode, PartialEq)]
pub struct GenericExtra(Era, Compact<u32>, Compact<Balance>);
impl GenericExtra {
pub fn new(era: Era, nonce: u32) -> GenericExtra {
GenericExtra(era, Compact(nonce), Compact(0u128))
}
}
#[derive(Clone, Copy, Debug, Decode, Encode, PartialEq)]
pub enum Era {
Immortal,
}
#[derive(Clone, Debug, PartialEq)]
pub struct UncheckedExtrinsic<Call> {
pub signature: Option<(GenericAddress, MultiSignature, GenericExtra)>,
pub function: Call,
}
impl<Call: Encode> UncheckedExtrinsic<Call> {
pub fn as_hex(&self) -> String {
format!("0x{}", hex::encode(self.encode()))
}
}
impl<Call> Encode for UncheckedExtrinsic<Call>
where
Call: Encode,
{
fn encode(&self) -> Vec<u8> {
encode_with_vec_prefix::<Self, _>(|v| {
match self.signature.as_ref() {
Some(s) => {
v.push(4 | 0b1000_0000);
s.encode_to(v);
}
None => {
v.push(4 & 0b0111_1111);
}
}
self.function.encode_to(v);
})
}
}
impl<Call> Decode for UncheckedExtrinsic<Call>
where
Call: Decode + Encode,
{
fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
let _length_do_not_remove_me_see_above: Vec<()> = Decode::decode(input)?;
let version = input.read_byte()?;
let is_signed = version & 0b1000_0000 != 0;
let version = version & 0b0111_1111;
if version != 4 {
return Err("Invalid transaction version".into());
}
Ok(UncheckedExtrinsic {
signature: if is_signed {
Some(Decode::decode(input)?)
} else {
None
},
function: Decode::decode(input)?,
})
}
}
fn encode_with_vec_prefix<T: Encode, F: Fn(&mut Vec<u8>)>(encoder: F) -> Vec<u8> {
let size = core::mem::size_of::<T>();
let reserve = match size {
0..=0b0011_1111 => 1,
0b0100_0000..=0b0011_1111_1111_1111 => 2,
_ => 4,
};
let mut v = Vec::with_capacity(reserve + size);
v.resize(reserve, 0);
encoder(&mut v);
let mut length: Vec<()> = Vec::new();
length.resize(v.len() - reserve, ());
length.using_encoded(|s| {
v.splice(0..reserve, s.iter().cloned());
});
v
}
#[derive(Clone, Copy, Debug, Encode)]
pub struct SignedPayload<Call>((Call, GenericExtra, SignedExtra));
impl<Call: Encode> SignedPayload<Call> {
pub fn new(call: Call, extra: GenericExtra, s_extra: SignedExtra) -> SignedPayload<Call> {
SignedPayload((call, extra, s_extra))
}
pub fn encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
self.0.using_encoded(|payload| {
if payload.len() > 256 {
f(&blake2_256(payload))
} else {
f(payload)
}
})
}
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize, Encode, Decode)]
#[serde(rename_all = "camelCase")]
pub struct RuntimeVersion {
pub spec_version: u32,
pub transaction_version: u32,
}
#[derive(Clone, Eq, PartialEq, Default, Debug, Encode, Decode, Deserialize)]
pub struct AccountDataGen<Balance> {
pub free: Balance,
pub reserved: Balance,
pub misc_frozen: Balance,
pub fee_frozen: Balance,
}
pub type RefCount = u32;
pub type Index = u32;
#[derive(Clone, Eq, PartialEq, Default, Debug, Encode, Decode, Deserialize)]
pub struct AccountInfoGen<Index, AccountData> {
pub nonce: Index,
pub consumers: RefCount,
pub providers: RefCount,
pub sufficients: RefCount,
pub data: AccountData,
}
pub type AccountData = AccountDataGen<Balance>;
pub type AccountInfo = AccountInfoGen<Index, AccountData>;
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct FeeDetails {
pub inclusion_fee: Option<InclusionFee>,
#[serde(skip)]
#[serde(deserialize_with = "deser_number_or_hex")]
pub tip: Balance,
}
impl FeeDetails {
pub fn final_fee(&self) -> Balance {
self.inclusion_fee
.as_ref()
.map(|i| i.inclusion_fee())
.unwrap_or_default()
.saturating_add(self.tip)
}
}
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct InclusionFee {
#[serde(deserialize_with = "deser_number_or_hex")]
pub base_fee: Balance,
#[serde(deserialize_with = "deser_number_or_hex")]
pub len_fee: Balance,
#[serde(deserialize_with = "deser_number_or_hex")]
pub adjusted_weight_fee: Balance,
}
impl InclusionFee {
pub fn inclusion_fee(&self) -> Balance {
self.base_fee
.saturating_add(self.len_fee)
.saturating_add(self.adjusted_weight_fee)
}
}