1use crate::{2 custom_signature::SignatureUnit,3 execution::{Result, ResultWithPostInfo, WithPostDispatchInfo},4 make_signature, sealed,5 types::*,6};7use super::{traits::*, ABI_ALIGNMENT, AbiReader, AbiWriter};8use primitive_types::{U256, H160};910#[cfg(not(feature = "std"))]11use alloc::vec::Vec;1213macro_rules! impl_abi_type {14 ($ty:ty, $name:ident, $dynamic:literal) => {15 impl sealed::CanBePlacedInVec for $ty {}1617 impl AbiType for $ty {18 const SIGNATURE: SignatureUnit = make_signature!(new fixed(stringify!($name)));1920 fn is_dynamic() -> bool {21 $dynamic22 }2324 fn size() -> usize {25 ABI_ALIGNMENT26 }27 }28 };29}3031macro_rules! impl_abi_readable {32 ($ty:ty, $method:ident) => {33 impl AbiRead for $ty {34 fn abi_read(reader: &mut AbiReader) -> Result<$ty> {35 reader.$method()36 }37 }38 };39}4041macro_rules! impl_abi_writeable {42 ($ty:ty, $method:ident) => {43 impl AbiWrite for $ty {44 fn abi_write(&self, writer: &mut AbiWriter) {45 writer.$method(&self)46 }47 }48 };49}5051macro_rules! impl_abi {52 ($ty:ty, $method:ident, $dynamic:literal) => {53 impl_abi_type!($ty, $method, $dynamic);54 impl_abi_readable!($ty, $method);55 impl_abi_writeable!($ty, $method);56 };57}5859impl_abi!(bool, bool, false);60impl_abi!(u8, uint8, false);61impl_abi!(u32, uint32, false);62impl_abi!(u64, uint64, false);63impl_abi!(u128, uint128, false);64impl_abi!(U256, uint256, false);65impl_abi!(H160, address, false);66impl_abi!(string, string, true);6768impl_abi_writeable!(&str, string);6970impl_abi_type!(bytes, bytes, true);7172impl AbiRead for bytes {73 fn abi_read(reader: &mut AbiReader) -> Result<bytes> {74 Ok(bytes(reader.bytes()?))75 }76}7778impl AbiWrite for bytes {79 fn abi_write(&self, writer: &mut AbiWriter) {80 writer.bytes(self.0.as_slice())81 }82}8384impl_abi_type!(bytes4, bytes4, false);85impl AbiRead for bytes4 {86 fn abi_read(reader: &mut AbiReader) -> Result<bytes4> {87 reader.bytes4()88 }89}9091impl<T: AbiType + AbiRead + sealed::CanBePlacedInVec> AbiRead for Vec<T> {92 fn abi_read(reader: &mut AbiReader) -> Result<Vec<T>> {93 let mut sub = reader.subresult(None)?;94 let size = sub.uint32()? as usize;95 sub.subresult_offset = sub.offset;96 let is_dynamic = <T as AbiType>::is_dynamic();97 let mut out = Vec::with_capacity(size);98 for _ in 0..size {99 out.push(<T as AbiRead>::abi_read(&mut sub)?);100 if !is_dynamic {101 sub.bytes_read(<T as AbiType>::size());102 };103 }104 Ok(out)105 }106}107108impl<T: AbiType> AbiType for Vec<T> {109 const SIGNATURE: SignatureUnit = make_signature!(new nameof(T::SIGNATURE) fixed("[]"));110111 fn is_dynamic() -> bool {112 true113 }114115 fn size() -> usize {116 ABI_ALIGNMENT117 }118}119120impl sealed::CanBePlacedInVec for Property {}121122impl AbiType for Property {123 const SIGNATURE: SignatureUnit = make_signature!(new fixed("(string,bytes)"));124125 fn is_dynamic() -> bool {126 string::is_dynamic() || bytes::is_dynamic()127 }128129 fn size() -> usize {130 <string as AbiType>::size() + <bytes as AbiType>::size()131 }132}133134impl AbiRead for Property {135 fn abi_read(reader: &mut AbiReader) -> Result<Property> {136 let size = if !Property::is_dynamic() {137 Some(<Property as AbiType>::size())138 } else {139 None140 };141 let mut subresult = reader.subresult(size)?;142 let key = <string>::abi_read(&mut subresult)?;143 let value = <bytes>::abi_read(&mut subresult)?;144145 Ok(Property { key, value })146 }147}148149impl AbiWrite for Property {150 fn abi_write<'a>(&'a self, writer: &mut AbiWriter) {151 (self.key.clone(), self.value.clone()).abi_write(writer);152 }153}154155impl<T: AbiWrite + AbiType> AbiWrite for Vec<T> {156 fn abi_write(&self, writer: &mut AbiWriter) {157 let is_dynamic = T::is_dynamic();158 let mut sub = if is_dynamic {159 AbiWriter::new_dynamic(is_dynamic)160 } else {161 AbiWriter::new()162 };163164 165 (self.len() as u32).abi_write(&mut sub);166167 for item in self {168 item.abi_write(&mut sub);169 }170 writer.write_subresult(sub);171 }172}173174impl AbiWrite for () {175 fn abi_write(&self, _writer: &mut AbiWriter) {}176}177178179180181182impl<T: AbiWrite> AbiWrite for ResultWithPostInfo<T> {183 fn abi_write(&self, _writer: &mut AbiWriter) {184 debug_assert!(false, "shouldn't be called, see comment")185 }186 fn to_result(&self) -> ResultWithPostInfo<AbiWriter> {187 match self {188 Ok(v) => Ok(WithPostDispatchInfo {189 post_info: v.post_info.clone(),190 data: {191 let mut out = AbiWriter::new();192 v.data.abi_write(&mut out);193 out194 },195 }),196 Err(e) => Err(e.clone()),197 }198 }199}200201macro_rules! impl_tuples {202 ($($ident:ident)+) => {203 impl<$($ident: AbiType,)+> AbiType for ($($ident,)+)204 where205 $(206 $ident: AbiType,207 )+208 {209 const SIGNATURE: SignatureUnit = make_signature!(210 new fixed("(")211 $(nameof(<$ident>::SIGNATURE) fixed(","))+212 shift_left(1)213 fixed(")")214 );215216 fn is_dynamic() -> bool {217 false218 $(219 || <$ident>::is_dynamic()220 )*221 }222223 fn size() -> usize {224 0 $(+ <$ident>::size())+225 }226 }227228 impl<$($ident),+> sealed::CanBePlacedInVec for ($($ident,)+) {}229230 impl<$($ident),+> AbiRead for ($($ident,)+)231 where232 Self: AbiType,233 $($ident: AbiRead + AbiType,)+234 {235 fn abi_read(reader: &mut AbiReader) -> Result<($($ident,)+)> {236 let is_dynamic = <Self>::is_dynamic();237 let size = if !is_dynamic { Some(<Self>::size()) } else { None };238 let mut subresult = reader.subresult(size)?;239 Ok((240 $({241 let value = <$ident>::abi_read(&mut subresult)?;242 if !is_dynamic {subresult.bytes_read(<$ident as AbiType>::size())};243 value244 },)+245 ))246 }247 }248249 #[allow(non_snake_case)]250 impl<$($ident),+> AbiWrite for ($($ident,)+)251 where252 $($ident: AbiWrite + AbiType,)+253 {254 fn abi_write(&self, writer: &mut AbiWriter) {255 let ($($ident,)+) = self;256 if <Self as AbiType>::is_dynamic() {257 let mut sub = AbiWriter::new();258 $($ident.abi_write(&mut sub);)+259 writer.write_subresult(sub);260 } else {261 $($ident.abi_write(writer);)+262 }263 }264 }265 };266}267268impl_tuples! {A}269impl_tuples! {A B}270impl_tuples! {A B C}271impl_tuples! {A B C D}272impl_tuples! {A B C D E}273impl_tuples! {A B C D E F}274impl_tuples! {A B C D E F G}275impl_tuples! {A B C D E F G H}276impl_tuples! {A B C D E F G H I}277impl_tuples! {A B C D E F G H I J}