ink_primitives/sol/
params.rs1use alloy_sol_types::{
16 abi::{
17 self,
18 Encoder,
19 },
20 SolType as AlloySolType,
21};
22use impl_trait_for_tuples::impl_for_tuples;
23use ink_prelude::vec::Vec;
24
25use super::{
26 encodable::{
27 Encodable,
28 EncodableParams,
29 },
30 Error,
31 SolDecode,
32 SolEncode,
33 SolTypeDecode,
34 SolTypeEncode,
35};
36
37pub trait SolParamsDecode: SolDecode + Sized + private::Sealed {
43 const SOL_NAME: &'static str = <Self as SolDecode>::SOL_NAME;
45
46 fn decode(data: &[u8]) -> Result<Self, Error>;
48}
49
50pub trait SolParamsEncode<'a>: SolEncode<'a> + private::Sealed {
57 const SOL_NAME: &'static str = <Self as SolEncode<'a>>::SOL_NAME;
59
60 fn encode(&'a self) -> Vec<u8>;
62}
63
64#[impl_for_tuples(1, 12)]
68#[tuple_types_custom_trait_bound(SolDecode)]
69impl SolParamsDecode for Tuple {
70 fn decode(data: &[u8]) -> Result<Self, Error> {
71 abi::decode_params::<
72 <<<Self as SolDecode>::SolType as SolTypeDecode>::AlloyType as AlloySolType>::Token<'_>,
73 >(data)
74 .map_err(Error::from)
75 .and_then(<<Self as SolDecode>::SolType as SolTypeDecode>::detokenize)
76 .and_then(<Self as SolDecode>::from_sol_type)
77 }
78}
79
80#[impl_for_tuples(1, 12)]
81#[tuple_types_custom_trait_bound(SolEncode<'a>)]
82impl<'a> SolParamsEncode<'a> for Tuple {
83 fn encode(&'a self) -> Vec<u8> {
84 let params = self.to_sol_type();
85 let token = <<Self as SolEncode>::SolType as SolTypeEncode>::tokenize(¶ms);
86 let mut encoder = Encoder::with_capacity(token.total_words());
87 EncodableParams::encode_params(&token, &mut encoder);
88 encoder.into_bytes()
89 }
90}
91
92impl SolParamsDecode for () {
94 fn decode(_: &[u8]) -> Result<Self, Error> {
95 Ok(())
97 }
98}
99
100impl SolParamsEncode<'_> for () {
101 fn encode(&self) -> Vec<u8> {
102 Vec::new()
103 }
104}
105
106#[impl_for_tuples(12)]
107#[tuple_types_no_default_trait_bound]
108impl private::Sealed for Tuple {}
109
110mod private {
111 pub trait Sealed {}
113}