evm-coder 0.4.3

EVM call decoding/encoding proc macros
Documentation
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
use hex_literal::hex;
use primitive_types::{H160, U256};

use super::{AbiEncode, AbiType};
use crate::{types::*, AbiDecode};

pub fn to_lines(is_call: bool, data: impl AsRef<[u8]>) -> Vec<String> {
	let mut data = data.as_ref();
	let mut offset = 0;
	let mut out = Vec::new();
	if is_call {
		offset += 4;
		out.push(hex::encode(&data[..4]));
		data = &data[4..];
	}
	assert!(data.len() % 32 == 0);
	for _ in (offset..data.len()).step_by(32) {
		let chunk = &data[..32];
		out.push(hex::encode(chunk));
		data = &data[32..];
	}

	out
}

fn test_impl<T>(function_identifier: u32, decoded_data: T, encoded_data: &[u8])
where
	T: AbiEncode + AbiDecode + std::cmp::PartialEq + std::fmt::Debug,
{
	let reencoded = decoded_data.abi_encode_call(BytesFixed(function_identifier.to_be_bytes()));
	similar_asserts::assert_eq!(to_lines(true, encoded_data), to_lines(true, reencoded));
	let (call, data) = T::abi_decode_call(encoded_data).unwrap();
	assert_eq!(call.0, u32::to_be_bytes(function_identifier));
	assert_eq!(data, decoded_data);
}

macro_rules! test_impl_uint {
	($type:ident) => {
		test_impl::<$type>(
			0xdeadbeef,
			255 as $type,
			&hex!(
				"
					deadbeef
					00000000000000000000000000000000000000000000000000000000000000ff
				"
			),
		);
	};
}

#[test]
fn encode_decode_uint8() {
	test_impl_uint!(u8);
}

#[test]
fn encode_decode_uint32() {
	test_impl_uint!(u32);
}

#[test]
fn encode_decode_uint128() {
	test_impl_uint!(u128);
}

#[test]
fn encode_decode_uint256() {
	test_impl::<U256>(
		0xdeadbeef,
		U256([255, 0, 0, 0]),
		&hex!(
			"
				deadbeef
				00000000000000000000000000000000000000000000000000000000000000ff
			"
		),
	);
}

#[test]
fn encode_decode_string() {
	test_impl::<(String,)>(
		0xdeadbeef,
		("some string".to_string(),),
		&hex!(
			"
				deadbeef
				0000000000000000000000000000000000000000000000000000000000000020
				000000000000000000000000000000000000000000000000000000000000000b
				736f6d6520737472696e67000000000000000000000000000000000000000000
			"
		),
	);
}

#[test]
fn encode_decode_tuple_string() {
	test_impl::<(String,)>(
		0xdeadbeef,
		("some string".to_string(),),
		&hex!(
			"
				deadbeef
				0000000000000000000000000000000000000000000000000000000000000020
				000000000000000000000000000000000000000000000000000000000000000b
				736f6d6520737472696e67000000000000000000000000000000000000000000
			"
		),
	);
}

#[test]
fn encode_decode_vec_tuple_address_uint256() {
	test_impl::<(Vec<(Address, U256)>,)>(
		0x1ACF2D55,
		(vec![
			(
				H160(hex!("2D2FF76104B7BACB2E8F6731D5BFC184EBECDDBC")),
				U256([10, 0, 0, 0]),
			),
			(
				H160(hex!("AB8E3D9134955566483B11E6825C9223B6737B10")),
				U256([20, 0, 0, 0]),
			),
			(
				H160(hex!("8C582BDF2953046705FC56F189385255EFC1BE18")),
				U256([30, 0, 0, 0]),
			),
		],),
		&hex!(
			"
				1ACF2D55
				0000000000000000000000000000000000000000000000000000000000000020 // offset of (address, uint256)[]
				0000000000000000000000000000000000000000000000000000000000000003 // length of (address, uint256)[]

				0000000000000000000000002D2FF76104B7BACB2E8F6731D5BFC184EBECDDBC // address
				000000000000000000000000000000000000000000000000000000000000000A // uint256

				000000000000000000000000AB8E3D9134955566483B11E6825C9223B6737B10 // address
				0000000000000000000000000000000000000000000000000000000000000014 // uint256

				0000000000000000000000008C582BDF2953046705FC56F189385255EFC1BE18 // address
				000000000000000000000000000000000000000000000000000000000000001E // uint256
			"
		),
	);
}

#[derive(Debug, PartialEq)]
struct TokenId(u32);
impl AbiType for TokenId {
	const SIGNATURE: crate::custom_signature::SignatureUnit = u32::SIGNATURE;
	const IS_DYNAMIC: bool = u32::IS_DYNAMIC;
	const HEAD_WORDS: u32 = u32::HEAD_WORDS;
}
impl AbiDecode for TokenId {
	fn dec(input: &mut crate::AbiDecoder<'_>) -> super::Result<Self> {
		Ok(Self(u32::dec(input)?))
	}
}
impl AbiEncode for TokenId {
	fn enc(&self, out: &mut crate::AbiEncoder) {
		self.0.enc(out)
	}
}
impl From<u32> for TokenId {
	fn from(value: u32) -> Self {
		Self(value)
	}
}
#[test]
fn encode_decode_vec_tuple_uint256_string() {
	test_impl::<(Vec<(TokenId, String)>,)>(
		0xdeadbeef,
		(vec![
			(14u32.into(), "Test URI 0".to_string()),
			(11u32.into(), "Test URI 1".to_string()),
			(12u32.into(), "Test URI 2".to_string()),
		],),
		&hex!(
			"
				deadbeef
				0000000000000000000000000000000000000000000000000000000000000020 // offset of (uint256, string)[]

				0000000000000000000000000000000000000000000000000000000000000003 // length of (uint256, string)[]
				0000000000000000000000000000000000000000000000000000000000000060 // offset of first elem
				00000000000000000000000000000000000000000000000000000000000000e0 // offset of second elem
				0000000000000000000000000000000000000000000000000000000000000160 // offset of third elem

				000000000000000000000000000000000000000000000000000000000000000e // first token #60
				0000000000000000000000000000000000000000000000000000000000000040 // offset of string
				000000000000000000000000000000000000000000000000000000000000000a // size of string
				5465737420555249203000000000000000000000000000000000000000000000 // string

				000000000000000000000000000000000000000000000000000000000000000b // second token #e0
				0000000000000000000000000000000000000000000000000000000000000040 // offset of string
				000000000000000000000000000000000000000000000000000000000000000a // size of string
				5465737420555249203100000000000000000000000000000000000000000000 // string

				000000000000000000000000000000000000000000000000000000000000000c // third token #160
				0000000000000000000000000000000000000000000000000000000000000040 // offset of string
				000000000000000000000000000000000000000000000000000000000000000a // size of string
				5465737420555249203200000000000000000000000000000000000000000000 // string
			"
		),
	);
}

#[test]
fn mint_sample() {
	let (call, decoder) = <(Address, u32, String)>::abi_decode_call(&hex!(
		"
			50bb4e7f
			000000000000000000000000ad2c0954693c2b5404b7e50967d3481bea432374
			0000000000000000000000000000000000000000000000000000000000000001
			0000000000000000000000000000000000000000000000000000000000000060
			0000000000000000000000000000000000000000000000000000000000000008
			5465737420555249000000000000000000000000000000000000000000000000
		"
	))
	.unwrap();
	assert_eq!(call, BytesFixed(u32::to_be_bytes(0x50bb4e7f)));
	assert_eq!(
		format!("{:?}", decoder.0),
		"0xad2c0954693c2b5404b7e50967d3481bea432374"
	);
	assert_eq!(decoder.1, 1);
	assert_eq!(decoder.2, "Test URI");
}

#[test]
fn parse_vec_with_dynamic_type() {
	let decoded_data = (
		0x36543006,
		vec![
			(1.into(), "Test URI 0".to_string()),
			(11.into(), "Test URI 1".to_string()),
			(12.into(), "Test URI 2".to_string()),
		],
	);

	let encoded_data = &hex!(
		"
			36543006
			00000000000000000000000053744e6da587ba10b32a2554d2efdcd985bc27a3 // address
			0000000000000000000000000000000000000000000000000000000000000040 // offset of (uint256, string)[]

			0000000000000000000000000000000000000000000000000000000000000003 // length of (uint256, string)[]

			0000000000000000000000000000000000000000000000000000000000000060 // offset of first elem
			00000000000000000000000000000000000000000000000000000000000000e0 // offset of second elem
			0000000000000000000000000000000000000000000000000000000000000160 // offset of third elem

			0000000000000000000000000000000000000000000000000000000000000001 // first token id?						#60
			0000000000000000000000000000000000000000000000000000000000000040 // offset of string
			000000000000000000000000000000000000000000000000000000000000000a // size of string
			5465737420555249203000000000000000000000000000000000000000000000 // string

			000000000000000000000000000000000000000000000000000000000000000b // second token id? Why ==11?			#e0
			0000000000000000000000000000000000000000000000000000000000000040 // offset of string
			000000000000000000000000000000000000000000000000000000000000000a // size of string
			5465737420555249203100000000000000000000000000000000000000000000 // string

			000000000000000000000000000000000000000000000000000000000000000c // third token id?  Why ==12?			#160
			0000000000000000000000000000000000000000000000000000000000000040 // offset of string
			000000000000000000000000000000000000000000000000000000000000000a // size of string
			5465737420555249203200000000000000000000000000000000000000000000 // string
		"
	);

	let (call, decoder) = <(Address, Vec<(U256, String)>)>::abi_decode_call(encoded_data).unwrap();
	assert_eq!(call, BytesFixed(u32::to_be_bytes(decoded_data.0)));
	assert_eq!(decoder.1, decoded_data.1);

	let ed = (decoder.0, decoded_data.1).abi_encode_call(BytesFixed(decoded_data.0.to_be_bytes()));
	similar_asserts::assert_eq!(encoded_data, ed.as_slice());
}

#[test]
fn encode_decode_vec_tuple_string_bytes() {
	test_impl::<(Vec<(String, Bytes)>,)>(
		0xdeadbeef,
		(vec![
			(
				"Test URI 0".to_string(),
				Bytes(vec![
					0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
					0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
					0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
					0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
				]),
			),
			(
				"Test URI 1".to_string(),
				Bytes(vec![
					0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
					0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
					0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
					0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
				]),
			),
			("Test URI 2".to_string(), Bytes(vec![0x33, 0x33])),
		],),
		&hex!(
			"
				deadbeef
				0000000000000000000000000000000000000000000000000000000000000020
				0000000000000000000000000000000000000000000000000000000000000003

				0000000000000000000000000000000000000000000000000000000000000060 // 32
				0000000000000000000000000000000000000000000000000000000000000140 // 96 
				0000000000000000000000000000000000000000000000000000000000000220 // 544

				0000000000000000000000000000000000000000000000000000000000000040 // 32
				0000000000000000000000000000000000000000000000000000000000000080
				000000000000000000000000000000000000000000000000000000000000000a // 64
				5465737420555249203000000000000000000000000000000000000000000000
				0000000000000000000000000000000000000000000000000000000000000030
				1111111111111111111111111111111111111111111111111111111111111111
				1111111111111111111111111111111100000000000000000000000000000000

				0000000000000000000000000000000000000000000000000000000000000040 // 96
				0000000000000000000000000000000000000000000000000000000000000080
				000000000000000000000000000000000000000000000000000000000000000a
				5465737420555249203100000000000000000000000000000000000000000000
				000000000000000000000000000000000000000000000000000000000000002f
				2222222222222222222222222222222222222222222222222222222222222222
				2222222222222222222222222222220000000000000000000000000000000000

				0000000000000000000000000000000000000000000000000000000000000040 // 544
				0000000000000000000000000000000000000000000000000000000000000080
				000000000000000000000000000000000000000000000000000000000000000a
				5465737420555249203200000000000000000000000000000000000000000000
				0000000000000000000000000000000000000000000000000000000000000002
				3333000000000000000000000000000000000000000000000000000000000000
			"
		),
	);
}

#[test]
// #[ignore = "reason"]
fn encode_decode_tuple0_tuple1_uint8_tuple1_string_bytes_tuple1_uint8_bytes() {
	let int = 0xff;
	let by = Bytes(vec![0x11, 0x22, 0x33]);
	let string = "some string".to_string();

	test_impl::<(((u8,), (String, Bytes), (u8, Bytes)),)>(
		0xdeadbeef,
		(((int,), (string, by.clone()), (int, by)),),
		&hex!(
			"
				deadbeef
				0000000000000000000000000000000000000000000000000000000000000020
				00000000000000000000000000000000000000000000000000000000000000ff
				0000000000000000000000000000000000000000000000000000000000000060
				0000000000000000000000000000000000000000000000000000000000000120
				0000000000000000000000000000000000000000000000000000000000000040
				0000000000000000000000000000000000000000000000000000000000000080
				000000000000000000000000000000000000000000000000000000000000000b
				736f6d6520737472696e67000000000000000000000000000000000000000000
				0000000000000000000000000000000000000000000000000000000000000003
				1122330000000000000000000000000000000000000000000000000000000000
				00000000000000000000000000000000000000000000000000000000000000ff
				0000000000000000000000000000000000000000000000000000000000000040
				0000000000000000000000000000000000000000000000000000000000000003
				1122330000000000000000000000000000000000000000000000000000000000
			"
		),
	);
}

#[test]
fn encode_decode_tuple0_tuple1_uint8_tuple1_uint8_uint8_tuple1_uint8_uint8() {
	test_impl::<((u8,), (u8, u8), (u8, u8))>(
		0xdeadbeef,
		((43,), (44, 45), (46, 47)),
		&hex!(
			"
				deadbeef
				000000000000000000000000000000000000000000000000000000000000002b
				000000000000000000000000000000000000000000000000000000000000002c
				000000000000000000000000000000000000000000000000000000000000002d
				000000000000000000000000000000000000000000000000000000000000002e
				000000000000000000000000000000000000000000000000000000000000002f
			"
		),
	);
}

#[test]
fn encode_decode_tuple0_tuple1_uint8_tuple1_uint8() {
	test_impl::<((u8,), (u8,))>(
		0xdeadbeef,
		((43,), (44,)),
		&hex!(
			"
				deadbeef
				000000000000000000000000000000000000000000000000000000000000002b
				000000000000000000000000000000000000000000000000000000000000002c
			"
		),
	);
}

#[test]
fn encode_decode_tuple0_tuple1_uint8_uint8() {
	test_impl::<((u8, u8),)>(
		0xdeadbeef,
		((43, 44),),
		&hex!(
			"
				deadbeef
				000000000000000000000000000000000000000000000000000000000000002b
				000000000000000000000000000000000000000000000000000000000000002c
			"
		),
	);
}

#[test]
fn encode_decode_tuple_uint8_uint8() {
	test_impl::<(u8, u8)>(
		0xdeadbeef,
		(43, 44),
		&hex!(
			"
				deadbeef
				000000000000000000000000000000000000000000000000000000000000002b
				000000000000000000000000000000000000000000000000000000000000002c
			"
		),
	);
}

#[test]
fn encode_decode_tuple0_tuple1_uint8_uint8_tuple1_uint8_uint8_and_uint8() {
	test_impl::<((u8, u8), (u8, u8), u8)>(
		0xdeadbeef,
		((10, 11), (12, 13), 14),
		&hex!(
			"
				deadbeef
				000000000000000000000000000000000000000000000000000000000000000a
				000000000000000000000000000000000000000000000000000000000000000b
				000000000000000000000000000000000000000000000000000000000000000c
				000000000000000000000000000000000000000000000000000000000000000d
				000000000000000000000000000000000000000000000000000000000000000e
			"
		),
	);
}

#[test]
fn encode_decode_tuple0_tuple1_string() {
	test_impl::<(((String,),),)>(
		0xdeadbeef,
		((("some string".to_string(),),),),
		&hex!(
			"
				deadbeef
				0000000000000000000000000000000000000000000000000000000000000020
				0000000000000000000000000000000000000000000000000000000000000020
				0000000000000000000000000000000000000000000000000000000000000020
				000000000000000000000000000000000000000000000000000000000000000b
				736f6d6520737472696e67000000000000000000000000000000000000000000
			"
		),
	);
}

#[test]
fn encode_decode_tuple0_tuple1_uint8_string() {
	test_impl::<(((u8, String),),)>(
		0xdeadbeef,
		(((0xff, "some string".to_string()),),),
		&hex!(
			"
				deadbeef
				0000000000000000000000000000000000000000000000000000000000000020
				0000000000000000000000000000000000000000000000000000000000000020
				00000000000000000000000000000000000000000000000000000000000000ff
				0000000000000000000000000000000000000000000000000000000000000040
				000000000000000000000000000000000000000000000000000000000000000b
				736f6d6520737472696e67000000000000000000000000000000000000000000
			"
		),
	);
}

#[test]
fn encode_decode_tuple0_tuple1_string_bytes() {
	test_impl::<((String, Bytes),)>(
		0xdeadbeef,
		(("some string".to_string(), Bytes(vec![1, 2, 3])),),
		&hex!(
			"
				deadbeef
				0000000000000000000000000000000000000000000000000000000000000020
				0000000000000000000000000000000000000000000000000000000000000040
				0000000000000000000000000000000000000000000000000000000000000080
				000000000000000000000000000000000000000000000000000000000000000b
				736f6d6520737472696e67000000000000000000000000000000000000000000
				0000000000000000000000000000000000000000000000000000000000000003
				0102030000000000000000000000000000000000000000000000000000000000
			"
		),
	);
}

#[test]
fn encode_decode_tuple0_tuple1_uint8_tuple1_string() {
	test_impl::<(((u8,), (String,)),)>(
		0xdeadbeef,
		(((0xff,), ("some string".to_string(),)),),
		&hex!(
			"
				deadbeef
				0000000000000000000000000000000000000000000000000000000000000020
				00000000000000000000000000000000000000000000000000000000000000ff
				0000000000000000000000000000000000000000000000000000000000000040
				0000000000000000000000000000000000000000000000000000000000000020
				000000000000000000000000000000000000000000000000000000000000000b
				736f6d6520737472696e67000000000000000000000000000000000000000000
			"
		),
	);
}

#[test]
fn encode_decode_option_uint8_some() {
	test_impl::<Option<u8>>(
		0xdeadbeef,
		Some(44),
		&hex!(
			"
				deadbeef
				0000000000000000000000000000000000000000000000000000000000000001
				000000000000000000000000000000000000000000000000000000000000002c
			"
		),
	);
}

#[test]
fn encode_decode_option_uint8_none() {
	test_impl::<Option<u8>>(
		0xdeadbeef,
		None,
		&hex!(
			"
				deadbeef
				0000000000000000000000000000000000000000000000000000000000000000
				0000000000000000000000000000000000000000000000000000000000000000
			"
		),
	);
}

#[test]
fn encode_decode_option_string_some() {
	test_impl::<(Option<String>,)>(
		0xdeadbeef,
		(Some("some string".to_string()),),
		&hex!(
			"
				deadbeef
				0000000000000000000000000000000000000000000000000000000000000020
				0000000000000000000000000000000000000000000000000000000000000001
				0000000000000000000000000000000000000000000000000000000000000040
				000000000000000000000000000000000000000000000000000000000000000b
				736f6d6520737472696e67000000000000000000000000000000000000000000
			"
		),
	);
}

#[test]
fn encode_decode_option_string_none() {
	test_impl::<(Option<String>,)>(
		0xdeadbeef,
		(None,),
		&hex!(
			"
				deadbeef
				0000000000000000000000000000000000000000000000000000000000000020 // Offset of option
				0000000000000000000000000000000000000000000000000000000000000000 // Option value
				0000000000000000000000000000000000000000000000000000000000000040 // Offset of string from option
				0000000000000000000000000000000000000000000000000000000000000000
			"
		),
	);
}