Skip to main content

solabi/
lib.rs

1//! Solidity ABI encoding and decoding implementation.
2//!
3//! At a high level, this crate provides Solidity ABI [`encode()`]-ing and
4//! [`decode()`]-ing implementations for a collection of primitive types, as
5//! well as generic implementations for various collections and tuple types.
6//!
7//! ```
8//! # use solabi::*;
9//! let encoded = solabi::encode(&(42_i32, true, Bytes::borrowed(&[0, 1, 2, 3])));
10//! let (a, b, c): (i32, bool, Bytes<Vec<u8>>) = solabi::decode(&encoded).unwrap();
11//! assert_eq!(a, 42);
12//! assert_eq!(b, true);
13//! assert_eq!(c.as_bytes(), [0, 1, 2, 3]);
14//! ```
15//!
16//! # Encoders
17//!
18//! Furthermore, this library provides encoders for various Solidity ABI items:
19//! - Functions ([`FunctionEncoder`] and [`ConstructorEncoder`]).
20//! - Events ([`EventEncoder`] and [`AnonymousEventEncoder`]).
21//! - Errors ([`ErrorEncoder`]).
22//!
23//! These encoders provide a type-safe interface for Solidity encoding and
24//! decoding their parameters.
25//!
26//! ```
27//! # use solabi::*;
28//! const TRANSFER: FunctionEncoder<(Address, U256), (bool,)> =
29//!     FunctionEncoder::new(selector!("transfer(address,uint256)"));
30//!
31//! let call = TRANSFER.encode_params(&(
32//!     address!("0x0101010101010101010101010101010101010101"),
33//!     uint!("4_200_000_000_000_000_000"),
34//! ));
35//! # let _ = call;
36//! ```
37//!
38//! # Dynamic Values
39//!
40//! The [`value::Value`] type provides dynamic Solidity values. This allows
41//! Solidity ABI encoding and decoding when types are not known at compile-time.
42//!
43//! ```
44//! # use solabi::*;
45//! let event = abi::EventDescriptor::parse_declaration(
46//!     "event Transfer(address indexed to, address indexed from, uint256 value)",
47//! )
48//! .unwrap();
49//! let encoder = value::EventEncoder::new(&event).unwrap();
50//! let log = encoder
51//!     .encode(&[
52//!         Value::Address(address!("0x0101010101010101010101010101010101010101")),
53//!         Value::Address(address!("0x0202020202020202020202020202020202020202")),
54//!         Value::Uint(value::Uint::new(256, uint!("4_200_000_000_000_000_000")).unwrap()),
55//!     ])
56//!     .unwrap();
57//! # let _ = log;
58//! ```
59//!
60//! # Custom Encoding Implementation
61//!
62//! It can be useful to define custom types that encode and decode to the
63//! Solidity ABI. This can be done by implementing the [`encode::Encode`] and
64//! [`decode::Decode`] traits.
65//!
66//! ```
67//! use solabi::{
68//!     decode::{Decode, DecodeError, Decoder},
69//!     encode::{Encode, Encoder, Size},
70//!     encode_packed::EncodePacked,
71//! };
72//!
73//! #[derive(Debug, Eq, PartialEq)]
74//! struct MyStruct {
75//!     a: u64,
76//!     b: String,
77//! }
78//!
79//! impl Encode for MyStruct {
80//!     fn size(&self) -> Size {
81//!         (self.a, self.b.as_str()).size()
82//!     }
83//!
84//!     fn encode(&self, encoder: &mut Encoder) {
85//!         (self.a, self.b.as_str()).encode(encoder);
86//!     }
87//! }
88//!
89//! impl EncodePacked for MyStruct {
90//!     fn packed_size(&self) -> usize {
91//!         (self.a, self.b.as_str()).packed_size()
92//!     }
93//!
94//!     fn encode_packed(&self, out: &mut [u8]) {
95//!         (self.a, self.b.as_str()).encode_packed(out);
96//!     }
97//! }
98//!
99//! impl Decode for MyStruct {
100//!     fn is_dynamic() -> bool {
101//!         <(u64, String)>::is_dynamic()
102//!     }
103//!
104//!     fn decode(decoder: &mut Decoder) -> Result<Self, DecodeError> {
105//!         let (a, b) = Decode::decode(decoder)?;
106//!         Ok(Self { a, b })
107//!     }
108//! }
109//!
110//! let my_struct = MyStruct {
111//!     a: 42,
112//!     b: "The Answer to Life the Universe and Everything".to_string(),
113//! };
114//!
115//! let encoded = solabi::encode(&my_struct);
116//! let decoded = solabi::decode(&encoded).unwrap();
117//!
118//! assert_eq!(my_struct, decoded);
119//!
120//! let packed = solabi::encode_packed(&my_struct);
121//!
122//! assert_eq!(
123//!     packed,
124//!     b"\x00\x00\x00\x00\x00\x00\x00\x2a\
125//!       The Answer to Life the Universe and Everything",
126//! );
127//! ```
128//!
129//! > There are plans to provide `Encode` and `Decode` procedural macros to
130//! > automatically implement these traits in the future.
131
132pub mod abi;
133pub mod bitint;
134pub mod bytes;
135pub mod constructor;
136pub mod decode;
137pub mod encode;
138pub mod encode_packed;
139pub mod error;
140pub mod event;
141mod fmt;
142#[macro_use]
143pub mod function;
144pub mod log;
145pub mod primitive;
146pub mod value;
147
148/// The `solabi` prelude.
149pub mod prelude {
150    pub use crate::{
151        bitint::{
152            Int104, Int112, Int120, Int128, Int136, Int144, Int152, Int16, Int160, Int168, Int176,
153            Int184, Int192, Int200, Int208, Int216, Int224, Int232, Int24, Int240, Int248, Int256,
154            Int32, Int40, Int48, Int56, Int64, Int72, Int8, Int80, Int88, Int96, Uint104, Uint112,
155            Uint120, Uint128, Uint136, Uint144, Uint152, Uint16, Uint160, Uint168, Uint176,
156            Uint184, Uint192, Uint200, Uint208, Uint216, Uint224, Uint232, Uint24, Uint240,
157            Uint248, Uint256, Uint32, Uint40, Uint48, Uint56, Uint64, Uint72, Uint8, Uint80,
158            Uint88, Uint96,
159        },
160        bytes::Bytes,
161        constructor::ConstructorEncoder,
162        error::ErrorEncoder,
163        event::{AnonymousEventEncoder, EventEncoder},
164        function::{ExternalFunction, FunctionEncoder, Selector},
165        log::{Log, Topics},
166        value::{Value, ValueKind},
167    };
168    pub use ethprim::prelude::*;
169}
170
171pub use self::{
172    decode::{decode, decode_with_prefix, decode_with_selector},
173    encode::{encode, encode_to, encode_with_prefix, encode_with_selector},
174    encode_packed::{encode_packed, encode_packed_to},
175    prelude::*,
176};
177pub use ethprim::{self, address, digest, int, keccak, uint};
178
179#[cfg(test)]
180mod tests {
181    use super::*;
182    use crate::{
183        bytes::Bytes,
184        function::{ExternalFunction, Selector},
185        primitive::Primitive as _,
186    };
187    use ethprim::{address, uint};
188    use hex_literal::hex;
189
190    macro_rules! assert_encoding {
191        ($value:expr, $encoded:expr, $packed:expr $(,)?) => {{
192            let (value, encoded, packed) = ($value, $encoded, $packed);
193            assert_eq!(encode(&value), encoded);
194            assert_eq!(encode_packed(&value), packed);
195
196            // Small work around to avoid manually specifying types.
197            #[allow(unused_assignments)]
198            let decoded = {
199                let mut result = value.clone();
200                result = decode(&encoded).unwrap();
201                result
202            };
203            assert_eq!(decoded, value);
204        }};
205    }
206
207    #[test]
208    fn the_answer_to_life_the_universe_everything() {
209        assert_encoding!(
210            uint!("42"),
211            hex!("000000000000000000000000000000000000000000000000000000000000002a"),
212            hex!("000000000000000000000000000000000000000000000000000000000000002a"),
213        );
214    }
215
216    #[test]
217    fn root_has_no_jump() {
218        assert_encoding!(
219            ("hello".to_owned(),),
220            hex!(
221                "0000000000000000000000000000000000000000000000000000000000000020
222                 0000000000000000000000000000000000000000000000000000000000000005
223                 68656c6c6f000000000000000000000000000000000000000000000000000000"
224            ),
225            hex!("68656c6c6f"),
226        );
227
228        assert_encoding!(
229            "hello".to_owned(),
230            hex!(
231                "0000000000000000000000000000000000000000000000000000000000000005
232                 68656c6c6f000000000000000000000000000000000000000000000000000000"
233            ),
234            hex!("68656c6c6f"),
235        );
236    }
237
238    #[test]
239    fn ethereum_basic_abi_tests() {
240        // <https://github.com/ethereum/tests/blob/0e8d25bb613cab7f9e99430f970e1e6cbffdbf1a/ABITests/basic_abi_tests.json>
241
242        assert_encoding!(
243            (
244                291_i32,
245                vec![1110_i32, 1929_i32],
246                Bytes(*b"1234567890"),
247                "Hello, world!".to_owned(),
248            ),
249            hex!(
250                "0000000000000000000000000000000000000000000000000000000000000123
251                 0000000000000000000000000000000000000000000000000000000000000080
252                 3132333435363738393000000000000000000000000000000000000000000000
253                 00000000000000000000000000000000000000000000000000000000000000e0
254                 0000000000000000000000000000000000000000000000000000000000000002
255                 0000000000000000000000000000000000000000000000000000000000000456
256                 0000000000000000000000000000000000000000000000000000000000000789
257                 000000000000000000000000000000000000000000000000000000000000000d
258                 48656c6c6f2c20776f726c642100000000000000000000000000000000000000"
259            ),
260            hex!(
261                "0000012300000456000007893132333435363738393048656c6c6f2c20776f72
262                 6c6421"
263            ),
264        );
265
266        assert_encoding!(
267            98127491_i32,
268            hex!("0000000000000000000000000000000000000000000000000000000005d94e83"),
269            hex!("05d94e83"),
270        );
271
272        assert_encoding!(
273            (
274                324124_i32,
275                address!("0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826")
276            ),
277            hex!(
278                "000000000000000000000000000000000000000000000000000000000004f21c
279                 000000000000000000000000cd2a3d9f938e13cd947ec05abc7fe734df8dd826"
280            ),
281            hex!("0004f21ccd2a3d9f938e13cd947ec05abc7fe734df8dd826"),
282        );
283    }
284
285    #[test]
286    fn solidity_abi_encoder_tests() {
287        // <https://github.com/ethereum/solidity/blob/43f29c00da02e19ff10d43f7eb6955d627c57728/test/libsolidity/ABIEncoderTests.cpp>
288
289        assert_encoding!(
290            (
291                10_i32,
292                u16::MAX - 1,
293                0x121212_i32,
294                -1_i32,
295                Bytes(hex!("1babab")),
296                true,
297                address!(~"0xfffffffffffffffffffffffffffffffffffffffb")
298            ),
299            hex!(
300                "000000000000000000000000000000000000000000000000000000000000000a
301                 000000000000000000000000000000000000000000000000000000000000fffe
302                 0000000000000000000000000000000000000000000000000000000000121212
303                 ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
304                 1babab0000000000000000000000000000000000000000000000000000000000
305                 0000000000000000000000000000000000000000000000000000000000000001
306                 000000000000000000000000fffffffffffffffffffffffffffffffffffffffb"
307            ),
308            hex!(
309                "0000000afffe00121212ffffffff1babab01ffffffffffffffffffffffffffff
310                 fffffffffffb"
311            ),
312        );
313
314        assert_encoding!(
315            (
316                "abcdef".to_owned(),
317                Bytes(*b"abcde\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"),
318                "abcdefabcdefgehabcabcasdfjklabcdefabcedefghabcabcasdfjklabcdefab\
319                 cdefghabcabcasdfjklabcdeefabcdefghabcabcasdefjklabcdefabcdefghab\
320                 cabcasdfjkl"
321                    .to_owned(),
322            ),
323            hex!(
324                "0000000000000000000000000000000000000000000000000000000000000060
325                 6162636465000000000000000000000000000000000000000000000000000000
326                 00000000000000000000000000000000000000000000000000000000000000a0
327                 0000000000000000000000000000000000000000000000000000000000000006
328                 6162636465660000000000000000000000000000000000000000000000000000
329                 000000000000000000000000000000000000000000000000000000000000008b
330                 616263646566616263646566676568616263616263617364666a6b6c61626364
331                 6566616263656465666768616263616263617364666a6b6c6162636465666162
332                 636465666768616263616263617364666a6b6c61626364656566616263646566
333                 676861626361626361736465666a6b6c61626364656661626364656667686162
334                 63616263617364666a6b6c000000000000000000000000000000000000000000"
335            ),
336            hex!(
337                "6162636465666162636465000000000000000000000000000000616263646566
338                 616263646566676568616263616263617364666a6b6c61626364656661626365
339                 6465666768616263616263617364666a6b6c6162636465666162636465666768
340                 616263616263617364666a6b6c61626364656566616263646566676861626361
341                 626361736465666a6b6c61626364656661626364656667686162636162636173
342                 64666a6b6c"
343            ),
344        );
345
346        assert_encoding!(
347            0_u8,
348            hex!("0000000000000000000000000000000000000000000000000000000000000000"),
349            hex!("00"),
350        );
351        assert_encoding!(
352            1_u8,
353            hex!("0000000000000000000000000000000000000000000000000000000000000001"),
354            hex!("01"),
355        );
356        assert_encoding!(
357            2_u8,
358            hex!("0000000000000000000000000000000000000000000000000000000000000002"),
359            hex!("02"),
360        );
361
362        assert_encoding!(
363            (
364                Bytes([0_u8, 0, 0, 10]),
365                Bytes(hex!("f1f20000")),
366                0xff_u8,
367                0xff_u8,
368                -1_i8,
369                1_i8,
370            ),
371            hex!(
372                "0000000a00000000000000000000000000000000000000000000000000000000
373                 f1f2000000000000000000000000000000000000000000000000000000000000
374                 00000000000000000000000000000000000000000000000000000000000000ff
375                 00000000000000000000000000000000000000000000000000000000000000ff
376                 ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
377                 0000000000000000000000000000000000000000000000000000000000000001"
378            ),
379            hex!("0000000af1f20000ffffff01"),
380        );
381
382        assert_encoding!(
383            (
384                10_i32,
385                vec![0xfffffffe_u64, 0xffffffff, 0x100000000],
386                11_i32
387            ),
388            hex!(
389                "000000000000000000000000000000000000000000000000000000000000000a
390                 0000000000000000000000000000000000000000000000000000000000000060
391                 000000000000000000000000000000000000000000000000000000000000000b
392                 0000000000000000000000000000000000000000000000000000000000000003
393                 00000000000000000000000000000000000000000000000000000000fffffffe
394                 00000000000000000000000000000000000000000000000000000000ffffffff
395                 0000000000000000000000000000000000000000000000000000000100000000"
396            ),
397            hex!("0000000a00000000fffffffe00000000ffffffff00000001000000000000000b"),
398        );
399
400        assert_encoding!(
401            (10_i32, [vec![7_i16, 0x0506, -1], vec![4, 5]], 11_i32),
402            hex!(
403                "000000000000000000000000000000000000000000000000000000000000000a
404                 0000000000000000000000000000000000000000000000000000000000000060
405                 000000000000000000000000000000000000000000000000000000000000000b
406                 0000000000000000000000000000000000000000000000000000000000000040
407                 00000000000000000000000000000000000000000000000000000000000000c0
408                 0000000000000000000000000000000000000000000000000000000000000003
409                 0000000000000000000000000000000000000000000000000000000000000007
410                 0000000000000000000000000000000000000000000000000000000000000506
411                 ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
412                 0000000000000000000000000000000000000000000000000000000000000002
413                 0000000000000000000000000000000000000000000000000000000000000004
414                 0000000000000000000000000000000000000000000000000000000000000005"
415            ),
416            hex!("0000000a00070506ffff000400050000000b"),
417        );
418
419        assert_encoding!(
420            (
421                10_i32,
422                vec![
423                    "abcabcdefghjklmnopqrsuvwabcdefgijklmnopqrstuwabcdefgijklmnoprstuvw".to_owned(),
424                    "abcdefghijklmnopqrtuvwabcfghijklmnopqstuvwabcdeghijklmopqrstuvw".to_owned(),
425                ],
426                11_i32,
427            ),
428            hex!(
429                "000000000000000000000000000000000000000000000000000000000000000a
430                 0000000000000000000000000000000000000000000000000000000000000060
431                 000000000000000000000000000000000000000000000000000000000000000b
432                 0000000000000000000000000000000000000000000000000000000000000002
433                 0000000000000000000000000000000000000000000000000000000000000040
434                 00000000000000000000000000000000000000000000000000000000000000c0
435                 0000000000000000000000000000000000000000000000000000000000000042
436                 61626361626364656667686a6b6c6d6e6f707172737576776162636465666769
437                 6a6b6c6d6e6f7071727374757761626364656667696a6b6c6d6e6f7072737475
438                 7677000000000000000000000000000000000000000000000000000000000000
439                 000000000000000000000000000000000000000000000000000000000000003f
440                 6162636465666768696a6b6c6d6e6f70717274757677616263666768696a6b6c
441                 6d6e6f7071737475767761626364656768696a6b6c6d6f707172737475767700"
442            ),
443            hex!(
444                "0000000a61626361626364656667686a6b6c6d6e6f7071727375767761626364
445                 656667696a6b6c6d6e6f7071727374757761626364656667696a6b6c6d6e6f70
446                 7273747576776162636465666768696a6b6c6d6e6f7071727475767761626366
447                 6768696a6b6c6d6e6f7071737475767761626364656768696a6b6c6d6f707172
448                 73747576770000000b"
449            ),
450        );
451
452        assert_encoding!(
453            (
454                "123456789012345678901234567890a".to_owned(),
455                "ffff123456789012345678901234567890afffffffff123456789012345678901234567890a"
456                    .to_owned(),
457            ),
458            hex!(
459                "0000000000000000000000000000000000000000000000000000000000000040
460                 0000000000000000000000000000000000000000000000000000000000000080
461                 000000000000000000000000000000000000000000000000000000000000001f
462                 3132333435363738393031323334353637383930313233343536373839306100
463                 000000000000000000000000000000000000000000000000000000000000004b
464                 6666666631323334353637383930313233343536373839303132333435363738
465                 3930616666666666666666663132333435363738393031323334353637383930
466                 3132333435363738393061000000000000000000000000000000000000000000"
467            ),
468            hex!(
469                "3132333435363738393031323334353637383930313233343536373839306166
470                 6666663132333435363738393031323334353637383930313233343536373839
471                 3061666666666666666666313233343536373839303132333435363738393031
472                 32333435363738393061"
473            ),
474        );
475
476        assert_encoding!(
477            [
478                address!(~"0xffffffffffffffffffffffffffffffffffffffff"),
479                address!(~"0xfffffffffffffffffffffffffffffffffffffffe"),
480                address!(~"0xfffffffffffffffffffffffffffffffffffffffd"),
481            ],
482            hex!(
483                "000000000000000000000000ffffffffffffffffffffffffffffffffffffffff
484                 000000000000000000000000fffffffffffffffffffffffffffffffffffffffe
485                 000000000000000000000000fffffffffffffffffffffffffffffffffffffffd"
486            ),
487            hex!(
488                "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
489                 fffffffffffffffefffffffffffffffffffffffffffffffffffffffd"
490            ),
491        );
492
493        assert_encoding!(
494            (vec![
495                address!("0x0000000000000000000000000000000000000001"),
496                address!("0x0000000000000000000000000000000000000002"),
497                address!("0x0000000000000000000000000000000000000003"),
498            ],),
499            hex!(
500                "0000000000000000000000000000000000000000000000000000000000000020
501                 0000000000000000000000000000000000000000000000000000000000000003
502                 0000000000000000000000000000000000000000000000000000000000000001
503                 0000000000000000000000000000000000000000000000000000000000000002
504                 0000000000000000000000000000000000000000000000000000000000000003"
505            ),
506            hex!(
507                "0000000000000000000000000000000000000001000000000000000000000000
508                 00000000000000020000000000000000000000000000000000000003"
509            ),
510        );
511
512        assert_encoding!(
513            (vec![-1_i32, 2, -3, 4, -5, 6, -7, 8],),
514            hex!(
515                "0000000000000000000000000000000000000000000000000000000000000020
516                 0000000000000000000000000000000000000000000000000000000000000008
517                 ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
518                 0000000000000000000000000000000000000000000000000000000000000002
519                 fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd
520                 0000000000000000000000000000000000000000000000000000000000000004
521                 fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffb
522                 0000000000000000000000000000000000000000000000000000000000000006
523                 fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff9
524                 0000000000000000000000000000000000000000000000000000000000000008"
525            ),
526            hex!("ffffffff00000002fffffffd00000004fffffffb00000006fffffff900000008"),
527        );
528
529        assert_encoding!(
530            (
531                ExternalFunction {
532                    address: address!("0x001C08AB857afe5A9633887e7A4e2A429D1d8D42"),
533                    selector: Selector(hex!("b3de648b")),
534                },
535                ExternalFunction {
536                    address: address!("0x001C08AB857afe5A9633887e7A4e2A429D1d8D42"),
537                    selector: Selector(hex!("b3de648b")),
538                },
539            ),
540            hex!(
541                "001c08ab857afe5a9633887e7a4e2a429d1d8d42b3de648b0000000000000000
542                 001c08ab857afe5a9633887e7a4e2a429d1d8d42b3de648b0000000000000000"
543            ),
544            hex!(
545                "001c08ab857afe5a9633887e7a4e2a429d1d8d42b3de648b001c08ab857afe5a
546                 9633887e7a4e2a429d1d8d42b3de648b"
547            ),
548        );
549
550        assert_encoding!(
551            (
552                ExternalFunction {
553                    address: address!(~"0xffffffffffffffffffffffffffffffffffffffff"),
554                    selector: Selector(hex!("ffffffff")),
555                },
556                ExternalFunction {
557                    address: address!(~"0xffffffffffffffffffffffffffffffffffffffff"),
558                    selector: Selector(hex!("ffffffff")),
559                },
560            ),
561            hex!(
562                "ffffffffffffffffffffffffffffffffffffffffffffffff0000000000000000
563                 ffffffffffffffffffffffffffffffffffffffffffffffff0000000000000000"
564            ),
565            hex!(
566                "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
567                 ffffffffffffffffffffffffffffffff"
568            ),
569        );
570
571        assert_encoding!(
572            ("abcdef".to_owned(),),
573            hex!(
574                "0000000000000000000000000000000000000000000000000000000000000020
575                 0000000000000000000000000000000000000000000000000000000000000006
576                 6162636465660000000000000000000000000000000000000000000000000000"
577            ),
578            hex!("616263646566"),
579        );
580        assert_encoding!(
581            (
582                "abcdefgggggggggggggggggggggggggggggggggggggggghhheeeeeeeeeeeeeeeeeeeeeeeeeeeeee"
583                    .to_owned(),
584            ),
585            hex!(
586                "0000000000000000000000000000000000000000000000000000000000000020
587                 000000000000000000000000000000000000000000000000000000000000004f
588                 6162636465666767676767676767676767676767676767676767676767676767
589                 6767676767676767676767676767686868656565656565656565656565656565
590                 6565656565656565656565656565650000000000000000000000000000000000"
591            ),
592            hex!(
593                "6162636465666767676767676767676767676767676767676767676767676767
594                 6767676767676767676767676767686868656565656565656565656565656565
595                 656565656565656565656565656565"
596            ),
597        );
598
599        assert_encoding!(
600            0_i32,
601            hex!("0000000000000000000000000000000000000000000000000000000000000000"),
602            hex!("00000000"),
603        );
604        assert_encoding!(
605            1_i32,
606            hex!("0000000000000000000000000000000000000000000000000000000000000001"),
607            hex!("00000001"),
608        );
609        assert_encoding!(
610            7_i32,
611            hex!("0000000000000000000000000000000000000000000000000000000000000007"),
612            hex!("00000007"),
613        );
614
615        assert_encoding!(
616            (
617                7_i32,
618                (
619                    8_i32,
620                    9_i32,
621                    vec![(11_i32, 0_i32), (12, 0), (0, 13)],
622                    10_i32,
623                ),
624            ),
625            hex!(
626                "0000000000000000000000000000000000000000000000000000000000000007
627                 0000000000000000000000000000000000000000000000000000000000000040
628                 0000000000000000000000000000000000000000000000000000000000000008
629                 0000000000000000000000000000000000000000000000000000000000000009
630                 0000000000000000000000000000000000000000000000000000000000000080
631                 000000000000000000000000000000000000000000000000000000000000000a
632                 0000000000000000000000000000000000000000000000000000000000000003
633                 000000000000000000000000000000000000000000000000000000000000000b
634                 0000000000000000000000000000000000000000000000000000000000000000
635                 000000000000000000000000000000000000000000000000000000000000000c
636                 0000000000000000000000000000000000000000000000000000000000000000
637                 0000000000000000000000000000000000000000000000000000000000000000
638                 000000000000000000000000000000000000000000000000000000000000000d"
639            ),
640            hex!(
641                "0000000700000008000000090000000b000000000000000c0000000000000000
642                 0000000d0000000a"
643            ),
644        );
645
646        assert_encoding!(
647            (
648                7_i32,
649                [
650                    (
651                        address!("0x1111111111111111111111111111111111111111"),
652                        vec![(0x11_i32, 1_i32, 0x12_i32)],
653                    ),
654                    Default::default(),
655                ],
656                vec![
657                    Default::default(),
658                    (
659                        address!("0x0000000000000000000000000000000000001234"),
660                        vec![(0_i32, 0_i32, 0_i32), (0x21, 2, 0x22), (0, 0, 0)]
661                    ),
662                ],
663                8_i32,
664            ),
665            hex!(
666                "0000000000000000000000000000000000000000000000000000000000000007
667                 0000000000000000000000000000000000000000000000000000000000000080
668                 00000000000000000000000000000000000000000000000000000000000001e0
669                 0000000000000000000000000000000000000000000000000000000000000008
670                 0000000000000000000000000000000000000000000000000000000000000040
671                 0000000000000000000000000000000000000000000000000000000000000100
672                 0000000000000000000000001111111111111111111111111111111111111111
673                 0000000000000000000000000000000000000000000000000000000000000040
674                 0000000000000000000000000000000000000000000000000000000000000001
675                 0000000000000000000000000000000000000000000000000000000000000011
676                 0000000000000000000000000000000000000000000000000000000000000001
677                 0000000000000000000000000000000000000000000000000000000000000012
678                 0000000000000000000000000000000000000000000000000000000000000000
679                 0000000000000000000000000000000000000000000000000000000000000040
680                 0000000000000000000000000000000000000000000000000000000000000000
681                 0000000000000000000000000000000000000000000000000000000000000002
682                 0000000000000000000000000000000000000000000000000000000000000040
683                 00000000000000000000000000000000000000000000000000000000000000a0
684                 0000000000000000000000000000000000000000000000000000000000000000
685                 0000000000000000000000000000000000000000000000000000000000000040
686                 0000000000000000000000000000000000000000000000000000000000000000
687                 0000000000000000000000000000000000000000000000000000000000001234
688                 0000000000000000000000000000000000000000000000000000000000000040
689                 0000000000000000000000000000000000000000000000000000000000000003
690                 0000000000000000000000000000000000000000000000000000000000000000
691                 0000000000000000000000000000000000000000000000000000000000000000
692                 0000000000000000000000000000000000000000000000000000000000000000
693                 0000000000000000000000000000000000000000000000000000000000000021
694                 0000000000000000000000000000000000000000000000000000000000000002
695                 0000000000000000000000000000000000000000000000000000000000000022
696                 0000000000000000000000000000000000000000000000000000000000000000
697                 0000000000000000000000000000000000000000000000000000000000000000
698                 0000000000000000000000000000000000000000000000000000000000000000"
699            ),
700            hex!(
701                "0000000711111111111111111111111111111111111111110000001100000001
702                 0000001200000000000000000000000000000000000000000000000000000000
703                 0000000000000000000000000000000000000000000000000000000000001234
704                 0000000000000000000000000000002100000002000000220000000000000000
705                 0000000000000008"
706            )
707        );
708
709        assert_encoding!((), hex!(""), hex!(""));
710        assert_encoding!(
711            (vec![true, false, true, false], [true, false, true, false]),
712            hex!(
713                "00000000000000000000000000000000000000000000000000000000000000a0
714                 0000000000000000000000000000000000000000000000000000000000000001
715                 0000000000000000000000000000000000000000000000000000000000000000
716                 0000000000000000000000000000000000000000000000000000000000000001
717                 0000000000000000000000000000000000000000000000000000000000000000
718                 0000000000000000000000000000000000000000000000000000000000000004
719                 0000000000000000000000000000000000000000000000000000000000000001
720                 0000000000000000000000000000000000000000000000000000000000000000
721                 0000000000000000000000000000000000000000000000000000000000000001
722                 0000000000000000000000000000000000000000000000000000000000000000"
723            ),
724            hex!("0100010001000100")
725        );
726
727        macro_rules! bytes_nn_arrays {
728            ([$($size:literal),*] { $($nn:literal),* }) => {
729                bytes_nn_arrays!(__outer: [ $($size),* ] { $($nn,)* });
730            };
731            (__impl: $size:literal $nn:literal) => {{
732                const SIZE: usize = $size;
733                const NN: usize = $nn;
734
735                let mut y: [Bytes<[u8; NN]>; SIZE] = Default::default();
736                for (i, y) in y.iter_mut().enumerate() {
737                    y.0[NN - 1] = (i as u8) + 1;
738                }
739
740                let mut buffer = Vec::new();
741                buffer.extend_from_slice(&(0x20 * (1 + SIZE)).to_word());
742                for y in &y {
743                    buffer.extend_from_slice(&y.to_word());
744                }
745                buffer.extend_from_slice(&2.to_word());
746                buffer.extend_from_slice(&Bytes(*b"abc").to_word());
747                buffer.extend_from_slice(&Bytes(*b"def").to_word());
748
749                let mut packed = Vec::new();
750                packed.extend_from_slice(b"abc");
751                packed.extend_from_slice(b"def");
752                for y in &y {
753                    packed.extend_from_slice(y.as_ref());
754                }
755
756                assert_encoding!(
757                    (vec![Bytes(*b"abc"), Bytes(*b"def")], y),
758                    buffer,
759                    packed,
760                );
761            }};
762
763            // Cartesian product of `$t * $n`
764            (__outer: [ $($size:literal),* ] $rest:tt) => {$(
765                bytes_nn_arrays!(__inner: $size $rest);
766            )*};
767            (__inner: $size:literal { $($nn:literal,)* }) => {$(
768                bytes_nn_arrays!(__impl: $size $nn);
769            )*};
770        }
771
772        bytes_nn_arrays!(
773            [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
774            {1, 2, 4, 5, 7, 15, 16, 17, 31, 32}
775        );
776
777        macro_rules! bytes_nn_arrays_dyn {
778            ([ $size:expr ] { $($nn:literal),* }) => {$({
779                let size = $size;
780                const NN: usize = $nn;
781
782                for size in size {
783                    let y = (0..size)
784                        .map(|i| {
785                            let mut e = [0_u8; NN];
786                            e[NN - 1] = (i as u8) + 1;
787                            Bytes(e)
788                        })
789                        .collect::<Vec<_>>();
790
791                    let mut buffer = Vec::new();
792                    buffer.extend_from_slice(&(0x20 * 2).to_word());
793                    buffer.extend_from_slice(&(0x20 * (3 + size)).to_word());
794                    buffer.extend_from_slice(&size.to_word());
795                    for y in &y {
796                        buffer.extend_from_slice(&y.to_word());
797                    }
798                    buffer.extend_from_slice(&2.to_word());
799                    buffer.extend_from_slice(&Bytes(*b"abc").to_word());
800                    buffer.extend_from_slice(&Bytes(*b"def").to_word());
801
802                    let mut packed = Vec::new();
803                    for y in &y {
804                        packed.extend_from_slice(y.as_ref());
805                    }
806                    packed.extend_from_slice(b"abc");
807                    packed.extend_from_slice(b"def");
808
809                    assert_encoding!(
810                        (y, vec![Bytes(*b"abc"), Bytes(*b"def")]),
811                        buffer,
812                        packed,
813                    );
814                }
815            })*};
816        }
817
818        bytes_nn_arrays_dyn!([1..15] {1, 2, 4, 5, 7, 15, 16, 17, 31, 32});
819
820        assert_encoding!(
821            (
822                false,
823                -5_i32,
824                ExternalFunction {
825                    address: address!("0x903D3a9A4266EB4432407ea5B1B4f80094f17957"),
826                    selector: Selector(hex!("e2179b8e")),
827                },
828                Bytes(hex!("010203")),
829                -3_i32,
830            ),
831            hex!(
832                "0000000000000000000000000000000000000000000000000000000000000000
833                 fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffb
834                 903d3a9a4266eb4432407ea5b1b4f80094f17957e2179b8e0000000000000000
835                 0102030000000000000000000000000000000000000000000000000000000000
836                 fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd"
837            ),
838            hex!(
839                "00fffffffb903d3a9a4266eb4432407ea5b1b4f80094f17957e2179b8e010203
840                 fffffffd"
841            ),
842        );
843
844        assert_encoding!(
845            ("".to_owned(), 3_i32, "".to_owned()),
846            hex!(
847                "0000000000000000000000000000000000000000000000000000000000000060
848                 0000000000000000000000000000000000000000000000000000000000000003
849                 0000000000000000000000000000000000000000000000000000000000000080
850                 0000000000000000000000000000000000000000000000000000000000000000
851                 0000000000000000000000000000000000000000000000000000000000000000"
852            ),
853            hex!("00000003")
854        );
855
856        assert_encoding!(
857            ("abc".to_owned(), 7_i32, "def".to_owned()),
858            hex!(
859                "0000000000000000000000000000000000000000000000000000000000000060
860                 0000000000000000000000000000000000000000000000000000000000000007
861                 00000000000000000000000000000000000000000000000000000000000000a0
862                 0000000000000000000000000000000000000000000000000000000000000003
863                 6162630000000000000000000000000000000000000000000000000000000000
864                 0000000000000000000000000000000000000000000000000000000000000003
865                 6465660000000000000000000000000000000000000000000000000000000000"
866            ),
867            hex!("61626300000007646566")
868        );
869    }
870
871    #[test]
872    fn bitints() {
873        assert_encoding!(
874            (
875                Int24::new(1).unwrap(),
876                Int40::new(-2).unwrap(),
877                Int48::new(3).unwrap(),
878                Int56::new(-4).unwrap(),
879                Int72::new(5).unwrap(),
880                Int80::new(-6).unwrap(),
881                Int88::new(7).unwrap(),
882                Int96::new(-8).unwrap(),
883                Int104::new(9).unwrap(),
884                Int112::new(-10).unwrap(),
885                Int120::new(11).unwrap(),
886            ),
887            hex!(
888                "0000000000000000000000000000000000000000000000000000000000000001
889                 fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
890                 0000000000000000000000000000000000000000000000000000000000000003
891                 fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc
892                 0000000000000000000000000000000000000000000000000000000000000005
893                 fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffa
894                 0000000000000000000000000000000000000000000000000000000000000007
895                 fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8
896                 0000000000000000000000000000000000000000000000000000000000000009
897                 fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff6
898                 000000000000000000000000000000000000000000000000000000000000000b"
899            ),
900            hex!(
901                "000001fffffffffe000000000003fffffffffffffc000000000000000005ffff
902                 fffffffffffffffa0000000000000000000007fffffffffffffffffffffff800
903                 000000000000000000000009fffffffffffffffffffffffffff6000000000000
904                 00000000000000000b"
905            ),
906        );
907
908        assert_encoding!(
909            (
910                Int136::new(int!("-12")).unwrap(),
911                Int144::new(int!("13")).unwrap(),
912                Int152::new(int!("-14")).unwrap(),
913                Int160::new(int!("15")).unwrap(),
914                Int168::new(int!("-16")).unwrap(),
915                Int176::new(int!("17")).unwrap(),
916                Int184::new(int!("-18")).unwrap(),
917                Int192::new(int!("19")).unwrap(),
918                Int200::new(int!("-20")).unwrap(),
919                Int208::new(int!("21")).unwrap(),
920                Int216::new(int!("-22")).unwrap(),
921                Int224::new(int!("23")).unwrap(),
922            ),
923            hex!(
924                "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff4
925                 000000000000000000000000000000000000000000000000000000000000000d
926                 fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff2
927                 000000000000000000000000000000000000000000000000000000000000000f
928                 fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0
929                 0000000000000000000000000000000000000000000000000000000000000011
930                 ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffee
931                 0000000000000000000000000000000000000000000000000000000000000013
932                 ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffec
933                 0000000000000000000000000000000000000000000000000000000000000015
934                 ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffea
935                 0000000000000000000000000000000000000000000000000000000000000017"
936            ),
937            hex!(
938                "fffffffffffffffffffffffffffffffff4000000000000000000000000000000
939                 00000dfffffffffffffffffffffffffffffffffffff200000000000000000000
940                 0000000000000000000ffffffffffffffffffffffffffffffffffffffffff000
941                 000000000000000000000000000000000000000011ffffffffffffffffffffff
942                 ffffffffffffffffffffffee0000000000000000000000000000000000000000
943                 00000013ffffffffffffffffffffffffffffffffffffffffffffffffec000000
944                 0000000000000000000000000000000000000000000015ffffffffffffffffff
945                 ffffffffffffffffffffffffffffffffffea0000000000000000000000000000
946                 0000000000000000000000000017"
947            ),
948        );
949
950        assert_encoding!(
951            (
952                Int232::new(int!("-24")).unwrap(),
953                Int240::new(int!("25")).unwrap(),
954                Int248::new(int!("-26")).unwrap(),
955            ),
956            hex!(
957                "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe8
958                 0000000000000000000000000000000000000000000000000000000000000019
959                 ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe6"
960            ),
961            hex!(
962                "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffe8000000
963                 000000000000000000000000000000000000000000000000000019ffffffffff
964                 ffffffffffffffffffffffffffffffffffffffffffffffffffe6"
965            ),
966        );
967
968        assert_encoding!(
969            (
970                Uint24::new(27).unwrap(),
971                Uint40::new(28).unwrap(),
972                Uint48::new(29).unwrap(),
973                Uint56::new(30).unwrap(),
974                Uint72::new(31).unwrap(),
975                Uint80::new(32).unwrap(),
976                Uint88::new(33).unwrap(),
977                Uint96::new(34).unwrap(),
978                Uint104::new(35).unwrap(),
979                Uint112::new(36).unwrap(),
980                Uint120::new(37).unwrap(),
981            ),
982            hex!(
983                "000000000000000000000000000000000000000000000000000000000000001b
984                 000000000000000000000000000000000000000000000000000000000000001c
985                 000000000000000000000000000000000000000000000000000000000000001d
986                 000000000000000000000000000000000000000000000000000000000000001e
987                 000000000000000000000000000000000000000000000000000000000000001f
988                 0000000000000000000000000000000000000000000000000000000000000020
989                 0000000000000000000000000000000000000000000000000000000000000021
990                 0000000000000000000000000000000000000000000000000000000000000022
991                 0000000000000000000000000000000000000000000000000000000000000023
992                 0000000000000000000000000000000000000000000000000000000000000024
993                 0000000000000000000000000000000000000000000000000000000000000025"
994            ),
995            hex!(
996                "00001b000000001c00000000001d0000000000001e00000000000000001f0000
997                 0000000000000020000000000000000000002100000000000000000000002200
998                 0000000000000000000000230000000000000000000000000024000000000000
999                 000000000000000025"
1000            ),
1001        );
1002
1003        assert_encoding!(
1004            (
1005                Uint136::new(uint!("38")).unwrap(),
1006                Uint144::new(uint!("39")).unwrap(),
1007                Uint152::new(uint!("40")).unwrap(),
1008                Uint160::new(uint!("41")).unwrap(),
1009                Uint168::new(uint!("42")).unwrap(),
1010                Uint176::new(uint!("43")).unwrap(),
1011                Uint184::new(uint!("44")).unwrap(),
1012                Uint192::new(uint!("45")).unwrap(),
1013                Uint200::new(uint!("46")).unwrap(),
1014                Uint208::new(uint!("47")).unwrap(),
1015                Uint216::new(uint!("48")).unwrap(),
1016                Uint224::new(uint!("49")).unwrap(),
1017            ),
1018            hex!(
1019                "0000000000000000000000000000000000000000000000000000000000000026
1020                 0000000000000000000000000000000000000000000000000000000000000027
1021                 0000000000000000000000000000000000000000000000000000000000000028
1022                 0000000000000000000000000000000000000000000000000000000000000029
1023                 000000000000000000000000000000000000000000000000000000000000002a
1024                 000000000000000000000000000000000000000000000000000000000000002b
1025                 000000000000000000000000000000000000000000000000000000000000002c
1026                 000000000000000000000000000000000000000000000000000000000000002d
1027                 000000000000000000000000000000000000000000000000000000000000002e
1028                 000000000000000000000000000000000000000000000000000000000000002f
1029                 0000000000000000000000000000000000000000000000000000000000000030
1030                 0000000000000000000000000000000000000000000000000000000000000031"
1031            ),
1032            hex!(
1033                "0000000000000000000000000000000026000000000000000000000000000000
1034                 0000270000000000000000000000000000000000002800000000000000000000
1035                 0000000000000000002900000000000000000000000000000000000000002a00
1036                 00000000000000000000000000000000000000002b0000000000000000000000
1037                 00000000000000000000002c0000000000000000000000000000000000000000
1038                 0000002d0000000000000000000000000000000000000000000000002e000000
1039                 000000000000000000000000000000000000000000002f000000000000000000
1040                 0000000000000000000000000000000000300000000000000000000000000000
1041                 0000000000000000000000000031"
1042            ),
1043        );
1044
1045        assert_encoding!(
1046            (
1047                Uint232::new(uint!("50")).unwrap(),
1048                Uint240::new(uint!("51")).unwrap(),
1049                Uint248::new(uint!("52")).unwrap(),
1050            ),
1051            hex!(
1052                "0000000000000000000000000000000000000000000000000000000000000032
1053                 0000000000000000000000000000000000000000000000000000000000000033
1054                 0000000000000000000000000000000000000000000000000000000000000034"
1055            ),
1056            hex!(
1057                "0000000000000000000000000000000000000000000000000000000032000000
1058                 0000000000000000000000000000000000000000000000000000330000000000
1059                 0000000000000000000000000000000000000000000000000034"
1060            ),
1061        );
1062    }
1063}