alloy_sol_types/types/
function.rs1use crate::{
2 abi::{Token, TokenSeq},
3 private::SolTypeValue,
4 Result, SolType, Word,
5};
6use alloc::vec::Vec;
7
8pub trait SolCall: Sized {
16 type Parameters<'a>: SolType<Token<'a> = Self::Token<'a>>;
20
21 type Token<'a>: TokenSeq<'a>;
23
24 type Return;
26
27 type ReturnTuple<'a>: SolType<Token<'a> = Self::ReturnToken<'a>>;
31
32 type ReturnToken<'a>: TokenSeq<'a>;
34
35 const SIGNATURE: &'static str;
37
38 const SELECTOR: [u8; 4];
40
41 fn new(tuple: <Self::Parameters<'_> as SolType>::RustType) -> Self;
43
44 fn tokenize(&self) -> Self::Token<'_>;
46
47 #[inline]
49 fn abi_encoded_size(&self) -> usize {
50 if let Some(size) = <Self::Parameters<'_> as SolType>::ENCODED_SIZE {
51 return size;
52 }
53
54 let offset = <<Self::Parameters<'_> as SolType>::Token<'_> as Token>::DYNAMIC as usize * 32;
56 (self.tokenize().total_words() * Word::len_bytes()).saturating_sub(offset)
57 }
58
59 #[inline]
62 fn abi_decode_raw(data: &[u8], validate: bool) -> Result<Self> {
63 <Self::Parameters<'_> as SolType>::abi_decode_sequence(data, validate).map(Self::new)
64 }
65
66 #[inline]
69 fn abi_decode(data: &[u8], validate: bool) -> Result<Self> {
70 let data = data
71 .strip_prefix(&Self::SELECTOR)
72 .ok_or_else(|| crate::Error::type_check_fail_sig(data, Self::SIGNATURE))?;
73 Self::abi_decode_raw(data, validate)
74 }
75
76 #[inline]
78 fn abi_encode_raw(&self, out: &mut Vec<u8>) {
79 out.reserve(self.abi_encoded_size());
80 out.extend(crate::abi::encode_sequence(&self.tokenize()));
81 }
82
83 #[inline]
85 fn abi_encode(&self) -> Vec<u8> {
86 let mut out = Vec::with_capacity(4 + self.abi_encoded_size());
87 out.extend(&Self::SELECTOR);
88 self.abi_encode_raw(&mut out);
89 out
90 }
91
92 fn abi_decode_returns(data: &[u8], validate: bool) -> Result<Self::Return>;
94
95 #[inline]
97 fn abi_encode_returns<'a, E>(e: &'a E) -> Vec<u8>
98 where
99 E: SolTypeValue<Self::ReturnTuple<'a>>,
100 {
101 crate::abi::encode_sequence(&e.stv_to_tokens())
102 }
103}
104
105pub trait SolConstructor: Sized {
107 type Parameters<'a>: SolType<Token<'a> = Self::Token<'a>>;
111
112 type Token<'a>: TokenSeq<'a>;
114
115 fn new(tuple: <Self::Parameters<'_> as SolType>::RustType) -> Self;
117
118 fn tokenize(&self) -> Self::Token<'_>;
120
121 #[inline]
123 fn abi_encoded_size(&self) -> usize {
124 if let Some(size) = <Self::Parameters<'_> as SolType>::ENCODED_SIZE {
125 return size;
126 }
127
128 let offset = <<Self::Parameters<'_> as SolType>::Token<'_> as Token>::DYNAMIC as usize * 32;
130 (self.tokenize().total_words() * Word::len_bytes()).saturating_sub(offset)
131 }
132
133 #[inline]
135 fn abi_encode(&self) -> Vec<u8> {
136 crate::abi::encode_sequence(&self.tokenize())
137 }
138}