-
Notifications
You must be signed in to change notification settings - Fork 64
/
impls.rs
243 lines (193 loc) · 6.98 KB
/
impls.rs
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
// Copyright 2023, Offchain Labs, Inc.
// For licensing, see https://github.com/OffchainLabs/stylus-sdk-rs/blob/stylus/licenses/COPYRIGHT.md
use super::{AbiType, ConstString};
use alloc::{string::String, vec::Vec};
use alloy_primitives::{Address, Signed, Uint};
use alloy_sol_types::sol_data::{self, IntBitCount, SupportedInt};
#[cfg(test)]
use alloy_primitives::FixedBytes;
/// Generates a test to ensure the two-way relationship between Rust Types and Sol Types is bijective.
macro_rules! test_type {
($name:tt, $as_arg:expr, $($ty:tt)*) => {
#[cfg(test)]
paste::paste! {
#[allow(non_snake_case)]
#[test]
fn [<test_ $name>]() {
assert_eq!(
<$($ty)* as AbiType>::ABI.as_str(),
<<$($ty)* as AbiType>::SolType as alloy_sol_types::SolType>::sol_type_name(),
"{}'s ABI didn't match its SolType sol_type_name",
stringify!($($ty)*),
);
assert_eq!(
<$($ty)* as AbiType>::EXPORT_ABI_ARG.as_str(),
$as_arg,
);
}
}
};
}
macro_rules! append {
($stem:expr, $leaf:expr) => {
$stem.concat(ConstString::new($leaf))
};
}
macro_rules! append_dec {
($stem:expr, $num:expr) => {
ConstString::new($stem).concat(ConstString::from_decimal_number($num))
};
}
test_type!(bytes, "bytes calldata", super::Bytes);
impl<const BITS: usize, const LIMBS: usize> AbiType for Uint<BITS, LIMBS>
where
IntBitCount<BITS>: SupportedInt<Uint = Self>,
{
type SolType = sol_data::Uint<BITS>;
const ABI: ConstString = append_dec!("uint", BITS);
}
// test_type!(uint160, "uint160", Uint<160, 3>); TODO: audit alloy
test_type!(uint256, "uint256", Uint<256, 4>);
impl<const BITS: usize, const LIMBS: usize> AbiType for Signed<BITS, LIMBS>
where
IntBitCount<BITS>: SupportedInt<Int = Self>,
{
type SolType = sol_data::Int<BITS>;
const ABI: ConstString = append_dec!("int", BITS);
}
// test_type!(int160, "int160", Signed<160, 3>); TODO: audit alloy
test_type!(int256, "int256", Signed<256, 4>);
macro_rules! impl_int {
($bits:literal, $as_arg:expr, $unsigned:ty, $signed:ty) => {
impl AbiType for $unsigned
where
IntBitCount<$bits>: SupportedInt<Uint = Self>,
{
type SolType = sol_data::Uint<$bits>;
const ABI: ConstString = append_dec!("uint", $bits);
}
impl AbiType for $signed
where
IntBitCount<$bits>: SupportedInt<Int = Self>,
{
type SolType = sol_data::Int<$bits>;
const ABI: ConstString = append_dec!("int", $bits);
}
test_type!($unsigned, format!("u{}", $as_arg), $unsigned);
test_type!($signed, $as_arg, $signed);
};
}
impl_int!(8, "int8", u8, i8);
impl_int!(16, "int16", u16, i16);
impl_int!(32, "int32", u32, i32);
impl_int!(64, "int64", u64, i64);
impl_int!(128, "int128", u128, i128);
macro_rules! impl_alloy {
($rust_type:ty, $sol_type:ident $(<$generic:tt>)?, $signature:literal) => {
impl AbiType for $rust_type {
type SolType = sol_data::$sol_type $(<$generic>)*;
const ABI: ConstString = ConstString::new($signature);
}
test_type!($signature, $signature, $rust_type);
};
}
impl_alloy!(bool, Bool, "bool");
impl_alloy!(Address, Address, "address");
impl AbiType for String {
type SolType = sol_data::String;
const ABI: ConstString = ConstString::new("string");
const EXPORT_ABI_ARG: ConstString = append!(Self::ABI, " calldata");
const EXPORT_ABI_RET: ConstString = append!(Self::ABI, " memory");
}
impl<T: AbiType> AbiType for Vec<T> {
type SolType = sol_data::Array<T::SolType>;
const ABI: ConstString = append!(T::ABI, "[]");
const EXPORT_ABI_ARG: ConstString = Self::EXPORT_ABI_RET; // vectors are never calldata
const EXPORT_ABI_RET: ConstString = append!(T::ABI, "[] memory");
const CAN_BE_CALLDATA: bool = false;
}
test_type!(vec_of_u8s, "uint8[] memory", Vec<u8>);
test_type!(
vec_of_u256s,
"uint256[] memory",
Vec<alloy_primitives::U256>
);
test_type!(vec_of_bytes, "bytes[] memory", Vec<super::Bytes>);
test_type!(vec_of_fixed_bytes, "bytes18[] memory", Vec<FixedBytes<18>>);
impl<T: AbiType, const N: usize> AbiType for [T; N] {
type SolType = sol_data::FixedArray<T::SolType, N>;
const ABI: ConstString = T::ABI
.concat(ConstString::new("["))
.concat(ConstString::from_decimal_number(N))
.concat(ConstString::new("]"));
const EXPORT_ABI_ARG: ConstString = Self::ABI.concat(ConstString::select(
T::CAN_BE_CALLDATA,
" calldata",
" memory",
));
const EXPORT_ABI_RET: ConstString = append!(Self::ABI, " memory");
const CAN_BE_CALLDATA: bool = T::CAN_BE_CALLDATA;
}
test_type!(array_of_bools, "bool[5] calldata", [bool; 5]);
test_type!(array_of_nested_u32s, "uint32[2][4] calldata", [[u32; 2]; 4]);
test_type!(
array_of_fixed_bytes,
"bytes32[] memory",
Vec<FixedBytes<32>>
);
impl AbiType for () {
type SolType = ();
const ABI: ConstString = ConstString::new("()");
}
test_type!(empty_tuple, "()", ());
macro_rules! impl_tuple {
() => {};
($first:ident $(, $rest:ident)*) => {
impl<$first: AbiType $(, $rest: AbiType)*> AbiType for ( $first $(, $rest)* , ) {
type SolType = ( $first::SolType $(, $rest::SolType)* , );
const ABI: ConstString = ConstString::new("(")
.concat($first::ABI)
$(
.concat(ConstString::new(","))
.concat($rest::ABI)
)*
.concat(ConstString::new(")"));
const EXPORT_ABI_ARG: ConstString = ConstString::new("(")
.concat($first::EXPORT_ABI_ARG)
$(
.concat(ConstString::new(", "))
.concat($rest::EXPORT_ABI_ARG)
)*
.concat(ConstString::new(")"));
const EXPORT_ABI_RET: ConstString = ConstString::new("(")
.concat($first::EXPORT_ABI_RET)
$(
.concat(ConstString::new(", "))
.concat($rest::EXPORT_ABI_RET)
)*
.concat(ConstString::new(")"));
const CAN_BE_CALLDATA: bool = false;
}
impl_tuple! { $($rest),* }
};
}
impl_tuple!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X);
test_type!(tuple_of_single_u8, "(uint8)", (u8,));
test_type!(tuple_of_single_u256, "(uint256)", (alloy_primitives::U256,));
test_type!(tuple_of_two_u8s, "(uint8, uint8)", (u8, u8));
test_type!(
tuple_of_u8_and_u256,
"(uint8, uint256)",
(u8, alloy_primitives::U256)
);
test_type!(
tuple_of_five_types,
"(uint8, uint256[] memory, bytes calldata, bytes2, bool[][8] memory)",
(
u8,
Vec<alloy_primitives::U256>,
super::Bytes,
FixedBytes<2>,
[Vec<bool>; 8],
)
);