alloy_sol_types/abi/
mod.rs

1//! Ethereum ABI codec implementation.
2//!
3//! This module provides the low-level ABI [`Encoder`] and [`Decoder`] structs,
4//! along with generic functions for their operation. These utilize an
5//! intermediate representation, referred to as tokens. For additional
6//! information about tokens, see the [`token`] module documentation.
7//!
8//! You should not need this module in most cases, as the
9//! [`SolType`](crate::SolType) and [`SolValue`](crate::SolValue) traits
10//! provide a higher-level and easier to use interface. If you're sure you need
11//! the low-level functionality of this module, there are three main interfaces:
12//!
13//! ### `{encode,decode}`
14//!
15//! [`encode`] operates on a single token. It wrap this token in a
16//! single-element tuple, and passes it to the encoder. Similarly, [`decode`]
17//! decodes a single token from a blob by decoding a single-element tuple.
18//!
19//! Use this interface when ABI-encoding a single token. This is suitable for
20//! encoding a type in isolation, or for encoding parameters for single-param
21//! functions.
22//!
23//! ### `{encode,decode}_params`
24//!
25//! [`encode_params`] operates on a sequence. If the sequence is a tuple, the
26//! tuple is inferred to be a set of Solidity function parameters,
27//! The corresponding [`decode_params`] reverses this operation, decoding a
28//! tuple from a blob.
29//!
30//! This is used to encode the parameters for a Solidity function.
31//!
32//! ### `{encode,decode}_sequence`
33//!
34//! [`encode_sequence`] operates on a sequence of tokens. This sequence is
35//! inferred not to be function parameters.
36//!
37//! This is the least useful one. Most users will not need it.
38
39mod encoder;
40pub use encoder::{encode, encode_params, encode_sequence, Encoder};
41
42mod decoder;
43pub use decoder::{decode, decode_params, decode_sequence, Decoder, RECURSION_LIMIT};
44
45pub mod token;
46pub use token::{Token, TokenSeq};
47
48/// The ABI encoding of an empty byte array (`bytes` or `string`).
49pub const EMPTY_BYTES: &[u8; 64] = &alloy_primitives::hex!(
50    "0000000000000000000000000000000000000000000000000000000000000020" // offset, points to the next word
51    "0000000000000000000000000000000000000000000000000000000000000000" // length, 0
52);
53
54#[cfg(test)]
55mod tests {
56    use super::*;
57    use crate::{SolType, SolValue};
58
59    #[test]
60    fn empty_bytes() {
61        assert_eq!(EMPTY_BYTES.len(), 64);
62        assert_eq!(EMPTY_BYTES[..], crate::sol_data::String::abi_encode("")[..]);
63        assert_eq!(EMPTY_BYTES[..], crate::sol_data::Bytes::abi_encode(b"")[..]);
64        assert_eq!(EMPTY_BYTES[..], "".abi_encode());
65        assert_eq!(EMPTY_BYTES[..], b"".abi_encode());
66    }
67}