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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
#![cfg_attr(not(feature = "std"), no_std)]
use codec::{Decode, Encode};
use error::Error;
use sp_core::U256;
use sp_runtime::traits::Convert;
use sp_std::marker::PhantomData;
use sp_std::prelude::*;
use sp_std::vec;
pub type Data = Vec<u8>;
pub type AssetId = u32;
pub type Gas = u64;
pub type AccountId32 = sp_runtime::AccountId32;
pub type AccountId20 = sp_core::H160;
pub type Value32 = u32;
pub type Value64 = u64;
pub type Value128 = u128;
pub type Value256 = U256;
pub type Value = Value128;
pub type ValueEvm = Value256;
pub mod error;
pub enum ValueLen {
U32,
U64,
U128,
U256,
}
pub trait TryConvert<A> {
type Outcome;
fn try_convert(a: A) -> Self::Outcome;
}
pub trait SubstrateAbi:
TryConvert<(AccountId20, [u8; 12]), Outcome = Result<AccountId32, Error>>
+ TryConvert<AccountId32, Outcome = Result<AccountId20, Error>>
+ Convert<u32, u64>
+ Convert<u32, u128>
+ Convert<u32, U256>
+ Convert<u64, u32>
+ Convert<u64, u128>
+ Convert<u64, U256>
+ Convert<u128, u32>
+ Convert<u128, u64>
+ Convert<u128, U256>
{
}
pub struct SubstrateAbiConverter;
impl TryConvert<(AccountId20, [u8; 12])> for SubstrateAbiConverter {
type Outcome = Result<AccountId32, Error>;
fn try_convert(value: (AccountId20, [u8; 12])) -> Self::Outcome {
let mut dest_bytes: Vec<u8> = vec![];
dest_bytes.append(&mut value.0.encode());
dest_bytes.append(&mut value.1.encode());
Decode::decode(&mut dest_bytes.as_slice())
.map_err(|_e| Error::FailedToCastBetweenTypesValue)
}
}
impl TryConvert<AccountId32> for SubstrateAbiConverter {
type Outcome = Result<AccountId20, Error>;
fn try_convert(account_32: AccountId32) -> Self::Outcome {
let mut dest_bytes: Vec<u8> = vec![];
let account_32_encoded = account_32.encode(); dest_bytes.append(&mut account_32_encoded[..20].to_vec());
Decode::decode(&mut dest_bytes.as_slice())
.map_err(|_e| Error::FailedToCastBetweenTypesValue)
}
}
pub fn associate<T: Decode, U: Encode>(value: U) -> Result<T, Error> {
Decode::decode(&mut &value.encode()[..]).map_err(|_| Error::FailedToAssociateTypes)
}
pub struct ValueMorphism<T, O> {
pub to_morph: T,
pub output: PhantomData<O>,
}
impl<T, O> ValueMorphism<T, O> {
pub fn new(to_morph: T) -> Self {
ValueMorphism {
to_morph,
output: Default::default(),
}
}
}
impl<T, O> From<T> for ValueMorphism<T, O> {
fn from(value: T) -> Self {
ValueMorphism::new(value)
}
}
impl<O> TryConvert<ValueMorphism<&mut &[u8], O>> for SubstrateAbiConverter
where
SubstrateAbiConverter: Convert<u32, O>,
SubstrateAbiConverter: Convert<u64, O>,
SubstrateAbiConverter: Convert<u128, O>,
SubstrateAbiConverter: Convert<U256, O>,
{
type Outcome = Result<O, Error>;
fn try_convert(a: ValueMorphism<&mut &[u8], O>) -> Self::Outcome {
match a.to_morph.len() {
4 => {
let val: Result<Value32, Error> =
Decode::decode(a.to_morph).map_err(|_| Error::FailedToCastBetweenTypesValue);
val.map(Self::convert)
}
8 => {
let val: Result<Value64, Error> =
Decode::decode(a.to_morph).map_err(|_| Error::FailedToCastBetweenTypesValue);
val.map(Self::convert)
}
16 => {
let val: Result<Value128, Error> =
Decode::decode(a.to_morph).map_err(|_| Error::FailedToCastBetweenTypesValue);
val.map(Self::convert)
}
32 => {
let val: Result<Value256, Error> =
Decode::decode(a.to_morph).map_err(|_| Error::FailedToCastBetweenTypesValue);
val.map(Self::convert)
}
_ => Err(Error::FailedToCastBetweenTypesValue),
}
}
}
impl<O> TryConvert<ValueMorphism<&mut &[u8], Option<O>>> for SubstrateAbiConverter
where
SubstrateAbiConverter: Convert<u32, O>,
SubstrateAbiConverter: Convert<u64, O>,
SubstrateAbiConverter: Convert<u128, O>,
SubstrateAbiConverter: Convert<U256, O>,
SubstrateAbiConverter: Convert<U256, O>,
{
type Outcome = Result<Option<O>, Error>;
fn try_convert(a: ValueMorphism<&mut &[u8], Option<O>>) -> Self::Outcome {
match a.to_morph.len() {
1 => Ok(None),
5 => {
let val: Result<Value32, Error> =
Decode::decode(a.to_morph).map_err(|_| Error::FailedToCastBetweenTypesValue);
val.map(Self::convert).map(|o| Some(o))
}
9 => {
let val: Result<Value64, Error> =
Decode::decode(a.to_morph).map_err(|_| Error::FailedToCastBetweenTypesValue);
val.map(Self::convert).map(|o| Some(o))
}
17 => {
let val: Result<Value128, Error> =
Decode::decode(a.to_morph).map_err(|_| Error::FailedToCastBetweenTypesValue);
val.map(Self::convert).map(|o| Some(o))
}
33 => {
let val: Result<Value256, Error> =
Decode::decode(a.to_morph).map_err(|_| Error::FailedToCastBetweenTypesValue);
val.map(Self::convert).map(|o| Some(o))
}
_ => Err(Error::FailedToCastBetweenTypesValue),
}
}
}
impl<T> Convert<T, T> for SubstrateAbiConverter {
fn convert(a: T) -> T {
a
}
}
impl Convert<u32, u64> for SubstrateAbiConverter {
fn convert(a: u32) -> u64 {
a as u64
}
}
impl Convert<u32, u128> for SubstrateAbiConverter {
fn convert(a: u32) -> u128 {
a as u128
}
}
impl Convert<u32, U256> for SubstrateAbiConverter {
fn convert(a: u32) -> U256 {
U256::from(a)
}
}
impl Convert<u64, u32> for SubstrateAbiConverter {
fn convert(a: u64) -> u32 {
a as u32
}
}
impl Convert<u64, u128> for SubstrateAbiConverter {
fn convert(a: u64) -> u128 {
a as u128
}
}
impl Convert<u64, U256> for SubstrateAbiConverter {
fn convert(a: u64) -> U256 {
U256::from(a)
}
}
impl Convert<u128, u32> for SubstrateAbiConverter {
fn convert(a: u128) -> u32 {
a as u32
}
}
impl Convert<u128, u64> for SubstrateAbiConverter {
fn convert(a: u128) -> u64 {
a as u64
}
}
impl Convert<u128, U256> for SubstrateAbiConverter {
fn convert(a: u128) -> U256 {
U256::from(a)
}
}
impl Convert<U256, u32> for SubstrateAbiConverter {
fn convert(a: U256) -> u32 {
a.as_u32()
}
}
impl Convert<U256, u64> for SubstrateAbiConverter {
fn convert(a: U256) -> u64 {
a.as_u64()
}
}
impl Convert<U256, u128> for SubstrateAbiConverter {
fn convert(a: U256) -> u128 {
a.as_u128()
}
}
impl SubstrateAbi for SubstrateAbiConverter {}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn sabi_decodes_u128_to_target_values_correctly() {
let input_val: u128 = 88;
let output_256: U256 = SubstrateAbiConverter::convert(input_val);
assert_eq!(output_256, U256::from(input_val));
}
#[test]
fn convert_account_id_20_to_32() {
let original_account = AccountId20::repeat_byte(1u8);
let padding = [4_u8; 12];
let origin_source: AccountId32 =
SubstrateAbiConverter::try_convert((original_account, padding)).unwrap();
assert_eq!(
origin_source,
AccountId32::from([
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, ])
)
}
#[test]
fn convert_account_id_32_to_20() {
let original_account = AccountId32::new([1u8; 32]);
let origin_source: AccountId20 =
SubstrateAbiConverter::try_convert(original_account).unwrap();
assert_eq!(origin_source, AccountId20::repeat_byte(1_u8))
}
#[test]
fn try_convert_u32_to_everything() {
let value = 563_u32;
let next = SubstrateAbiConverter::try_convert(ValueMorphism::<_, u32>::new(
&mut &value.encode()[..],
))
.unwrap();
assert_eq!(next, value);
let next = SubstrateAbiConverter::try_convert(ValueMorphism::<_, u64>::new(
&mut &value.encode()[..],
))
.unwrap();
assert_eq!(next, value as u64);
let next = SubstrateAbiConverter::try_convert(ValueMorphism::<_, u128>::new(
&mut &value.encode()[..],
))
.unwrap();
assert_eq!(next, value as u128);
let next = SubstrateAbiConverter::try_convert(ValueMorphism::<_, U256>::new(
&mut &value.encode()[..],
))
.unwrap();
assert_eq!(next, U256::from(value));
}
#[test]
fn try_convert_u64_to_everything() {
let value = 563324_u64;
let next = SubstrateAbiConverter::try_convert(ValueMorphism::<_, u32>::new(
&mut &value.encode()[..],
))
.unwrap();
assert_eq!(next, value as u32);
let next = SubstrateAbiConverter::try_convert(ValueMorphism::<_, u64>::new(
&mut &value.encode()[..],
))
.unwrap();
assert_eq!(next, value);
let next = SubstrateAbiConverter::try_convert(ValueMorphism::<_, u128>::new(
&mut &value.encode()[..],
))
.unwrap();
assert_eq!(next, value as u128);
let next = SubstrateAbiConverter::try_convert(ValueMorphism::<_, U256>::new(
&mut &value.encode()[..],
))
.unwrap();
assert_eq!(next, U256::from(value));
}
#[test]
fn try_convert_u128_to_everything() {
let value = 563321231232134_u128;
let next = SubstrateAbiConverter::try_convert(ValueMorphism::<_, u32>::new(
&mut &value.encode()[..],
))
.unwrap();
assert_eq!(next, value as u32);
let next = SubstrateAbiConverter::try_convert(ValueMorphism::<_, u64>::new(
&mut &value.encode()[..],
))
.unwrap();
assert_eq!(next, value as u64);
let next = SubstrateAbiConverter::try_convert(ValueMorphism::<_, u128>::new(
&mut &value.encode()[..],
))
.unwrap();
assert_eq!(next, value);
let next = SubstrateAbiConverter::try_convert(ValueMorphism::<_, U256>::new(
&mut &value.encode()[..],
))
.unwrap();
assert_eq!(next, U256::from(value));
}
#[test]
fn try_convert_u256_to_everything_that_is_within_range() {
let value = U256::from(563321231232134_u128);
let next = SubstrateAbiConverter::try_convert(ValueMorphism::<_, u64>::new(
&mut &value.encode()[..],
))
.unwrap();
assert_eq!(next, value.low_u64());
let next = SubstrateAbiConverter::try_convert(ValueMorphism::<_, u128>::new(
&mut &value.encode()[..],
))
.unwrap();
assert_eq!(next, value.low_u128());
let next = SubstrateAbiConverter::try_convert(ValueMorphism::<_, U256>::new(
&mut &value.encode()[..],
))
.unwrap();
assert_eq!(next, value);
}
#[test]
#[should_panic] fn try_convert_u256_panic_overflow() {
let value = U256::from(563321231232134_u128);
let next = SubstrateAbiConverter::try_convert(ValueMorphism::<_, u32>::new(
&mut &value.encode()[..],
))
.unwrap();
assert_eq!(next, value.low_u32());
}
}