12345678910111213141516171819#![allow(dead_code)]2021#[cfg(not(feature = "std"))]22use alloc::vec::Vec;23use evm_core::ExitError;24use primitive_types::{H160, U256};2526use crate::{27 execution::{Error, ResultWithPostInfo, WithPostDispatchInfo},28 types::*,29};30use crate::execution::Result;3132const ABI_ALIGNMENT: usize = 32;3334trait TypeHelper {35 36 fn is_dynamic() -> bool;3738 39 fn size() -> usize;40}414243#[derive(Clone)]44pub struct AbiReader<'i> {45 buf: &'i [u8],46 subresult_offset: usize,47 offset: usize,48}49impl<'i> AbiReader<'i> {50 51 pub fn new(buf: &'i [u8]) -> Self {52 Self {53 buf,54 subresult_offset: 0,55 offset: 0,56 }57 }58 59 pub fn new_call(buf: &'i [u8]) -> Result<(bytes4, Self)> {60 if buf.len() < 4 {61 return Err(Error::Error(ExitError::OutOfOffset));62 }63 let mut method_id = [0; 4];64 method_id.copy_from_slice(&buf[0..4]);6566 Ok((67 method_id,68 Self {69 buf,70 subresult_offset: 4,71 offset: 4,72 },73 ))74 }7576 fn read_pad<const S: usize>(77 buf: &[u8],78 offset: usize,79 pad_start: usize,80 pad_size: usize,81 block_start: usize,82 block_size: usize,83 ) -> Result<[u8; S]> {84 if buf.len() - offset < ABI_ALIGNMENT {85 return Err(Error::Error(ExitError::OutOfOffset));86 }87 let mut block = [0; S];88 let is_pad_zeroed = buf[pad_start..pad_size].iter().all(|&v| v == 0);89 if !is_pad_zeroed {90 return Err(Error::Error(ExitError::InvalidRange));91 }92 block.copy_from_slice(&buf[block_start..block_size]);93 Ok(block)94 }9596 fn read_padleft<const S: usize>(&mut self) -> Result<[u8; S]> {97 let offset = self.offset;98 self.offset += ABI_ALIGNMENT;99 Self::read_pad(100 self.buf,101 offset,102 offset,103 offset + ABI_ALIGNMENT - S,104 offset + ABI_ALIGNMENT - S,105 offset + ABI_ALIGNMENT,106 )107 }108109 fn read_padright<const S: usize>(&mut self) -> Result<[u8; S]> {110 let offset = self.offset;111 self.offset += ABI_ALIGNMENT;112 Self::read_pad(113 self.buf,114 offset,115 offset + S,116 offset + ABI_ALIGNMENT,117 offset,118 offset + S,119 )120 }121122 123 pub fn address(&mut self) -> Result<H160> {124 Ok(H160(self.read_padleft()?))125 }126127 128 pub fn bool(&mut self) -> Result<bool> {129 let data: [u8; 1] = self.read_padleft()?;130 match data[0] {131 0 => Ok(false),132 1 => Ok(true),133 _ => Err(Error::Error(ExitError::InvalidRange)),134 }135 }136137 138 pub fn bytes4(&mut self) -> Result<[u8; 4]> {139 self.read_padright()140 }141142 143 pub fn bytes(&mut self) -> Result<Vec<u8>> {144 let mut subresult = self.subresult(None)?;145 let length = subresult.uint32()? as usize;146 if subresult.buf.len() < subresult.offset + length {147 return Err(Error::Error(ExitError::OutOfOffset));148 }149 Ok(subresult.buf[subresult.offset..subresult.offset + length].into())150 }151152 153 pub fn string(&mut self) -> Result<string> {154 string::from_utf8(self.bytes()?).map_err(|_| Error::Error(ExitError::InvalidRange))155 }156157 158 pub fn uint8(&mut self) -> Result<u8> {159 Ok(self.read_padleft::<1>()?[0])160 }161162 163 pub fn uint32(&mut self) -> Result<u32> {164 Ok(u32::from_be_bytes(self.read_padleft()?))165 }166167 168 pub fn uint128(&mut self) -> Result<u128> {169 Ok(u128::from_be_bytes(self.read_padleft()?))170 }171172 173 pub fn uint256(&mut self) -> Result<U256> {174 let buf: [u8; 32] = self.read_padleft()?;175 Ok(U256::from_big_endian(&buf))176 }177178 179 pub fn uint64(&mut self) -> Result<u64> {180 Ok(u64::from_be_bytes(self.read_padleft()?))181 }182183 184 #[deprecated = "dangerous, as usize may have different width in wasm and native execution"]185 pub fn read_usize(&mut self) -> Result<usize> {186 Ok(usize::from_be_bytes(self.read_padleft()?))187 }188189 190 191 fn subresult(&mut self, size: Option<usize>) -> Result<AbiReader<'i>> {192 let subresult_offset = self.subresult_offset;193 let offset = if let Some(size) = size {194 self.offset += size;195 self.subresult_offset += size;196 0197 } else {198 self.uint32()? as usize199 };200201 if offset + self.subresult_offset > self.buf.len() {202 return Err(Error::Error(ExitError::InvalidRange));203 }204205 let new_offset = offset + subresult_offset;206 Ok(AbiReader {207 buf: self.buf,208 subresult_offset: new_offset,209 offset: new_offset,210 })211 }212213 214 pub fn is_finished(&self) -> bool {215 self.buf.len() == self.offset216 }217}218219220#[derive(Default)]221pub struct AbiWriter {222 static_part: Vec<u8>,223 dynamic_part: Vec<(usize, AbiWriter)>,224 had_call: bool,225 is_dynamic: bool,226}227impl AbiWriter {228 229 pub fn new() -> Self {230 Self::default()231 }232233 234 pub fn new_dynamic(is_dynamic: bool) -> Self {235 Self {236 is_dynamic,237 ..Default::default()238 }239 }240 241 pub fn new_call(method_id: u32) -> Self {242 let mut val = Self::new();243 val.static_part.extend(&method_id.to_be_bytes());244 val.had_call = true;245 val246 }247248 fn write_padleft(&mut self, block: &[u8]) {249 assert!(block.len() <= ABI_ALIGNMENT);250 self.static_part251 .extend(&[0; ABI_ALIGNMENT][0..ABI_ALIGNMENT - block.len()]);252 self.static_part.extend(block);253 }254255 fn write_padright(&mut self, block: &[u8]) {256 assert!(block.len() <= ABI_ALIGNMENT);257 self.static_part.extend(block);258 self.static_part259 .extend(&[0; ABI_ALIGNMENT][0..ABI_ALIGNMENT - block.len()]);260 }261262 263 pub fn address(&mut self, address: &H160) {264 self.write_padleft(&address.0)265 }266267 268 pub fn bool(&mut self, value: &bool) {269 self.write_padleft(&[if *value { 1 } else { 0 }])270 }271272 273 pub fn uint8(&mut self, value: &u8) {274 self.write_padleft(&[*value])275 }276277 278 pub fn uint32(&mut self, value: &u32) {279 self.write_padleft(&u32::to_be_bytes(*value))280 }281282 283 pub fn uint128(&mut self, value: &u128) {284 self.write_padleft(&u128::to_be_bytes(*value))285 }286287 288 pub fn uint256(&mut self, value: &U256) {289 let mut out = [0; 32];290 value.to_big_endian(&mut out);291 self.write_padleft(&out)292 }293294 295 #[deprecated = "dangerous, as usize may have different width in wasm and native execution"]296 pub fn write_usize(&mut self, value: &usize) {297 self.write_padleft(&usize::to_be_bytes(*value))298 }299300 301 pub fn write_subresult(&mut self, result: Self) {302 self.dynamic_part.push((self.static_part.len(), result));303 304 self.write_padleft(&[]);305 }306307 fn memory(&mut self, value: &[u8]) {308 let mut sub = Self::new();309 sub.uint32(&(value.len() as u32));310 for chunk in value.chunks(ABI_ALIGNMENT) {311 sub.write_padright(chunk);312 }313 self.write_subresult(sub);314 }315316 317 pub fn string(&mut self, value: &str) {318 self.memory(value.as_bytes())319 }320321 322 pub fn bytes(&mut self, value: &[u8]) {323 self.memory(value)324 }325326 327 pub fn finish(mut self) -> Vec<u8> {328 for (static_offset, part) in self.dynamic_part {329 let part_offset = self.static_part.len()330 - if self.had_call { 4 } else { 0 }331 - if self.is_dynamic { ABI_ALIGNMENT } else { 0 };332333 let encoded_dynamic_offset = usize::to_be_bytes(part_offset);334 let start = static_offset + ABI_ALIGNMENT - encoded_dynamic_offset.len();335 let stop = static_offset + ABI_ALIGNMENT;336 self.static_part[start..stop].copy_from_slice(&encoded_dynamic_offset);337 self.static_part.extend(part.finish())338 }339 self.static_part340 }341}342343344345346347348pub trait AbiRead<T> {349 350 fn abi_read(&mut self) -> Result<T>;351}352353macro_rules! impl_abi_readable {354 ($ty:ty, $method:ident, $dynamic:literal) => {355 impl TypeHelper for $ty {356 fn is_dynamic() -> bool {357 $dynamic358 }359360 fn size() -> usize {361 ABI_ALIGNMENT362 }363 }364 impl AbiRead<$ty> for AbiReader<'_> {365 fn abi_read(&mut self) -> Result<$ty> {366 self.$method()367 }368 }369 };370}371372impl_abi_readable!(bool, bool, false);373impl_abi_readable!(uint8, uint8, false);374impl_abi_readable!(uint32, uint32, false);375impl_abi_readable!(uint64, uint64, false);376impl_abi_readable!(uint128, uint128, false);377impl_abi_readable!(uint256, uint256, false);378impl_abi_readable!(bytes4, bytes4, false);379impl_abi_readable!(address, address, false);380impl_abi_readable!(string, string, true);381382383impl TypeHelper for bytes {384 fn is_dynamic() -> bool {385 true386 }387 fn size() -> usize {388 ABI_ALIGNMENT389 }390}391impl AbiRead<bytes> for AbiReader<'_> {392 fn abi_read(&mut self) -> Result<bytes> {393 Ok(bytes(self.bytes()?))394 }395}396397mod sealed {398 399 pub trait CanBePlacedInVec {}400}401402impl sealed::CanBePlacedInVec for U256 {}403impl sealed::CanBePlacedInVec for string {}404impl sealed::CanBePlacedInVec for H160 {}405impl sealed::CanBePlacedInVec for EthCrossAccount {}406407impl<R: sealed::CanBePlacedInVec> AbiRead<Vec<R>> for AbiReader<'_>408where409 Self: AbiRead<R>,410{411 fn abi_read(&mut self) -> Result<Vec<R>> {412 let mut sub = self.subresult(None)?;413 let size = sub.uint32()? as usize;414 sub.subresult_offset = sub.offset;415 let mut out = Vec::with_capacity(size);416 for _ in 0..size {417 out.push(<Self as AbiRead<R>>::abi_read(&mut sub)?);418 }419 Ok(out)420 }421}422423impl TypeHelper for EthCrossAccount {424 fn is_dynamic() -> bool {425 address::is_dynamic() || uint256::is_dynamic()426 }427}428429impl AbiRead<EthCrossAccount> for AbiReader<'_> {430 fn abi_read(&mut self) -> Result<EthCrossAccount> {431 let size = if !EthCrossAccount::is_dynamic() {432 Some(<Self as AbiRead<EthCrossAccount>>::size())433 } else {434 None435 };436 let mut subresult = self.subresult(size)?;437 let eth = <Self as AbiRead<address>>::abi_read(&mut subresult)?;438 let sub = <Self as AbiRead<uint256>>::abi_read(&mut subresult)?;439440 Ok(EthCrossAccount { eth: eth, sub: sub })441 }442443 fn size() -> usize {444 <Self as AbiRead<address>>::size() + <Self as AbiRead<uint256>>::size()445 }446}447448impl AbiWrite for &EthCrossAccount {449 fn abi_write(&self, writer: &mut AbiWriter) {450 self.eth.abi_write(writer);451 self.sub.abi_write(writer);452 }453}454455impl TypeHelper for EthCrossAccount {456 fn is_dynamic() -> bool {457 address::is_dynamic() || uint256::is_dynamic()458 }459}460461impl AbiRead<EthCrossAccount> for AbiReader<'_> {462 fn abi_read(&mut self) -> Result<EthCrossAccount> {463 let size = if !EthCrossAccount::is_dynamic() {464 Some(<Self as AbiRead<EthCrossAccount>>::size())465 } else {466 None467 };468 let mut subresult = self.subresult(size)?;469 let eth = <Self as AbiRead<address>>::abi_read(&mut subresult)?;470 let sub = <Self as AbiRead<uint256>>::abi_read(&mut subresult)?;471472 Ok(EthCrossAccount { eth: eth, sub: sub })473 }474475 fn size() -> usize {476 <Self as AbiRead<address>>::size() + <Self as AbiRead<uint256>>::size()477 }478}479480impl AbiWrite for &EthCrossAccount {481 fn abi_write(&self, writer: &mut AbiWriter) {482 self.eth.abi_write(writer);483 self.sub.abi_write(writer);484 }485}486487macro_rules! impl_tuples {488 ($($ident:ident)+) => {489 impl<$($ident: TypeHelper,)+> TypeHelper for ($($ident,)+)490 where491 $(492 $ident: TypeHelper,493 )+494 {495 fn is_dynamic() -> bool {496 false497 $(498 || <$ident>::is_dynamic()499 )*500 }501502 fn size() -> usize {503 0 $(+ <$ident>::size())+504 }505 }506 impl<$($ident),+> sealed::CanBePlacedInVec for ($($ident,)+) {}507 impl<$($ident),+> AbiRead<($($ident,)+)> for AbiReader<'_>508 where509 $(510 Self: AbiRead<$ident>,511 )+512 ($($ident,)+): TypeHelper,513 {514 fn abi_read(&mut self) -> Result<($($ident,)+)> {515 let size = if !<($($ident,)+)>::is_dynamic() { Some(<($($ident,)+)>::size()) } else { None };516 let mut subresult = self.subresult(size)?;517 Ok((518 $(<Self as AbiRead<$ident>>::abi_read(&mut subresult)?,)+519 ))520 }521 }522 #[allow(non_snake_case)]523 impl<$($ident),+> AbiWrite for ($($ident,)+)524 where525 $($ident: AbiWrite,)+526 {527 fn abi_write(&self, writer: &mut AbiWriter) {528 let ($($ident,)+) = self;529 if writer.is_dynamic {530 let mut sub = AbiWriter::new();531 $($ident.abi_write(&mut sub);)+532 writer.write_subresult(sub);533 } else {534 $($ident.abi_write(writer);)+535 }536 }537 }538 };539}540541impl_tuples! {A}542impl_tuples! {A B}543impl_tuples! {A B C}544impl_tuples! {A B C D}545impl_tuples! {A B C D E}546impl_tuples! {A B C D E F}547impl_tuples! {A B C D E F G}548impl_tuples! {A B C D E F G H}549impl_tuples! {A B C D E F G H I}550impl_tuples! {A B C D E F G H I J}551552553554pub trait AbiWrite {555 556 fn abi_write(&self, writer: &mut AbiWriter);557 558 559 fn to_result(&self) -> ResultWithPostInfo<AbiWriter> {560 let mut writer = AbiWriter::new();561 self.abi_write(&mut writer);562 Ok(writer.into())563 }564}565566567568569570impl<T: AbiWrite> AbiWrite for ResultWithPostInfo<T> {571 fn abi_write(&self, _writer: &mut AbiWriter) {572 debug_assert!(false, "shouldn't be called, see comment")573 }574 fn to_result(&self) -> ResultWithPostInfo<AbiWriter> {575 match self {576 Ok(v) => Ok(WithPostDispatchInfo {577 post_info: v.post_info.clone(),578 data: {579 let mut out = AbiWriter::new();580 v.data.abi_write(&mut out);581 out582 },583 }),584 Err(e) => Err(e.clone()),585 }586 }587}588589macro_rules! impl_abi_writeable {590 ($ty:ty, $method:ident) => {591 impl AbiWrite for $ty {592 fn abi_write(&self, writer: &mut AbiWriter) {593 writer.$method(&self)594 }595 }596 };597}598599impl_abi_writeable!(u8, uint8);600impl_abi_writeable!(u32, uint32);601impl_abi_writeable!(u128, uint128);602impl_abi_writeable!(U256, uint256);603impl_abi_writeable!(H160, address);604impl_abi_writeable!(bool, bool);605impl_abi_writeable!(&str, string);606607impl AbiWrite for string {608 fn abi_write(&self, writer: &mut AbiWriter) {609 writer.string(self)610 }611}612613impl AbiWrite for bytes {614 fn abi_write(&self, writer: &mut AbiWriter) {615 writer.bytes(self.0.as_slice())616 }617}618619impl<T: AbiWrite + TypeHelper> AbiWrite for Vec<T> {620 fn abi_write(&self, writer: &mut AbiWriter) {621 let is_dynamic = T::is_dynamic();622 let mut sub = if is_dynamic {623 AbiWriter::new_dynamic(is_dynamic)624 } else {625 AbiWriter::new()626 };627628 629 (self.len() as u32).abi_write(&mut sub);630631 for item in self {632 item.abi_write(&mut sub);633 }634 writer.write_subresult(sub);635 }636}637638impl AbiWrite for () {639 fn abi_write(&self, _writer: &mut AbiWriter) {}640}641642643#[deprecated]644#[macro_export]645macro_rules! abi_decode {646 ($reader:expr, $($name:ident: $typ:ident),+ $(,)?) => {647 $(648 let $name = $reader.$typ()?;649 )+650 }651}652653654#[deprecated]655#[macro_export]656macro_rules! abi_encode {657 ($($typ:ident($value:expr)),* $(,)?) => {{658 #[allow(unused_mut)]659 let mut writer = ::evm_coder::abi::AbiWriter::new();660 $(661 writer.$typ($value);662 )*663 writer664 }};665 (call $val:expr; $($typ:ident($value:expr)),* $(,)?) => {{666 #[allow(unused_mut)]667 let mut writer = ::evm_coder::abi::AbiWriter::new_call($val);668 $(669 writer.$typ($value);670 )*671 writer672 }}673}674675#[cfg(test)]676pub mod test {677 use crate::{678 abi::{AbiRead, AbiWrite},679 types::*,680 };681682 use super::{AbiReader, AbiWriter};683 use hex_literal::hex;684 use primitive_types::{H160, U256};685 use concat_idents::concat_idents;686687 macro_rules! test_impl {688 ($name:ident, $type:ty, $function_identifier:expr, $decoded_data:expr, $encoded_data:expr) => {689 concat_idents!(test_name = encode_decode_, $name {690 #[test]691 fn test_name() {692 let function_identifier: u32 = $function_identifier;693 let decoded_data = $decoded_data;694 let encoded_data = $encoded_data;695696 let (call, mut decoder) = AbiReader::new_call(encoded_data).unwrap();697 assert_eq!(call, u32::to_be_bytes(function_identifier));698 let data = <AbiReader<'_> as AbiRead<$type>>::abi_read(&mut decoder).unwrap();699 assert_eq!(data, decoded_data);700701 let mut writer = AbiWriter::new_call(function_identifier);702 decoded_data.abi_write(&mut writer);703 let ed = writer.finish();704 similar_asserts::assert_eq!(encoded_data, ed.as_slice());705 }706 });707 };708 }709710 macro_rules! test_impl_uint {711 ($type:ident) => {712 test_impl!(713 $type,714 $type,715 0xdeadbeef,716 255 as $type,717 &hex!(718 "719 deadbeef720 00000000000000000000000000000000000000000000000000000000000000ff721 "722 )723 );724 };725 }726727 test_impl_uint!(uint8);728 test_impl_uint!(uint32);729 test_impl_uint!(uint128);730731 test_impl!(732 uint256,733 uint256,734 0xdeadbeef,735 U256([255, 0, 0, 0]),736 &hex!(737 "738 deadbeef739 00000000000000000000000000000000000000000000000000000000000000ff740 "741 )742 );743744 test_impl!(745 vec_tuple_address_uint256,746 Vec<(address, uint256)>,747 0x1ACF2D55,748 vec![749 (750 H160(hex!("2D2FF76104B7BACB2E8F6731D5BFC184EBECDDBC")),751 U256([10, 0, 0, 0]),752 ),753 (754 H160(hex!("AB8E3D9134955566483B11E6825C9223B6737B10")),755 U256([20, 0, 0, 0]),756 ),757 (758 H160(hex!("8C582BDF2953046705FC56F189385255EFC1BE18")),759 U256([30, 0, 0, 0]),760 ),761 ],762 &hex!(763 "764 1ACF2D55765 0000000000000000000000000000000000000000000000000000000000000020 // offset of (address, uint256)[]766 0000000000000000000000000000000000000000000000000000000000000003 // length of (address, uint256)[]767 768 0000000000000000000000002D2FF76104B7BACB2E8F6731D5BFC184EBECDDBC // address769 000000000000000000000000000000000000000000000000000000000000000A // uint256770 771 000000000000000000000000AB8E3D9134955566483B11E6825C9223B6737B10 // address772 0000000000000000000000000000000000000000000000000000000000000014 // uint256773 774 0000000000000000000000008C582BDF2953046705FC56F189385255EFC1BE18 // address775 000000000000000000000000000000000000000000000000000000000000001E // uint256776 "777 )778 );779780 test_impl!(781 vec_tuple_uint256_string,782 Vec<(uint256, string)>,783 0xdeadbeef,784 vec![785 (1.into(), "Test URI 0".to_string()),786 (11.into(), "Test URI 1".to_string()),787 (12.into(), "Test URI 2".to_string()),788 ],789 &hex!(790 "791 deadbeef792 0000000000000000000000000000000000000000000000000000000000000020 // offset of (uint256, string)[]793 0000000000000000000000000000000000000000000000000000000000000003 // length of (uint256, string)[]794795 0000000000000000000000000000000000000000000000000000000000000060 // offset of first elem796 00000000000000000000000000000000000000000000000000000000000000e0 // offset of second elem797 0000000000000000000000000000000000000000000000000000000000000160 // offset of third elem798799 0000000000000000000000000000000000000000000000000000000000000001 // first token id? #60800 0000000000000000000000000000000000000000000000000000000000000040 // offset of string801 000000000000000000000000000000000000000000000000000000000000000a // size of string802 5465737420555249203000000000000000000000000000000000000000000000 // string803804 000000000000000000000000000000000000000000000000000000000000000b // second token id? Why ==11? #e0805 0000000000000000000000000000000000000000000000000000000000000040 // offset of string806 000000000000000000000000000000000000000000000000000000000000000a // size of string807 5465737420555249203100000000000000000000000000000000000000000000 // string808809 000000000000000000000000000000000000000000000000000000000000000c // third token id? Why ==12? #160810 0000000000000000000000000000000000000000000000000000000000000040 // offset of string811 000000000000000000000000000000000000000000000000000000000000000a // size of string812 5465737420555249203200000000000000000000000000000000000000000000 // string813 "814 )815 );816817 #[test]818 fn dynamic_after_static() {819 let mut encoder = AbiWriter::new();820 encoder.bool(&true);821 encoder.string("test");822 let encoded = encoder.finish();823824 let mut encoder = AbiWriter::new();825 encoder.bool(&true);826 827 encoder.uint32(&(32 * 2));828 829 encoder.uint32(&4);830 encoder.write_padright(&[b't', b'e', b's', b't']);831 let alternative_encoded = encoder.finish();832833 assert_eq!(encoded, alternative_encoded);834835 let mut decoder = AbiReader::new(&encoded);836 assert!(decoder.bool().unwrap());837 assert_eq!(decoder.string().unwrap(), "test");838 }839840 #[test]841 fn mint_sample() {842 let (call, mut decoder) = AbiReader::new_call(&hex!(843 "844 50bb4e7f845 000000000000000000000000ad2c0954693c2b5404b7e50967d3481bea432374846 0000000000000000000000000000000000000000000000000000000000000001847 0000000000000000000000000000000000000000000000000000000000000060848 0000000000000000000000000000000000000000000000000000000000000008849 5465737420555249000000000000000000000000000000000000000000000000850 "851 ))852 .unwrap();853 assert_eq!(call, u32::to_be_bytes(0x50bb4e7f));854 assert_eq!(855 format!("{:?}", decoder.address().unwrap()),856 "0xad2c0954693c2b5404b7e50967d3481bea432374"857 );858 assert_eq!(decoder.uint32().unwrap(), 1);859 assert_eq!(decoder.string().unwrap(), "Test URI");860 }861862 #[test]863 fn parse_vec_with_dynamic_type() {864 let decoded_data = (865 0x36543006,866 vec![867 (1.into(), "Test URI 0".to_string()),868 (11.into(), "Test URI 1".to_string()),869 (12.into(), "Test URI 2".to_string()),870 ],871 );872873 let encoded_data = &hex!(874 "875 36543006876 00000000000000000000000053744e6da587ba10b32a2554d2efdcd985bc27a3 // address877 0000000000000000000000000000000000000000000000000000000000000040 // offset of (uint256, string)[]878 0000000000000000000000000000000000000000000000000000000000000003 // length of (uint256, string)[]879880 0000000000000000000000000000000000000000000000000000000000000060 // offset of first elem881 00000000000000000000000000000000000000000000000000000000000000e0 // offset of second elem882 0000000000000000000000000000000000000000000000000000000000000160 // offset of third elem883884 0000000000000000000000000000000000000000000000000000000000000001 // first token id? #60885 0000000000000000000000000000000000000000000000000000000000000040 // offset of string886 000000000000000000000000000000000000000000000000000000000000000a // size of string887 5465737420555249203000000000000000000000000000000000000000000000 // string888889 000000000000000000000000000000000000000000000000000000000000000b // second token id? Why ==11? #e0890 0000000000000000000000000000000000000000000000000000000000000040 // offset of string891 000000000000000000000000000000000000000000000000000000000000000a // size of string892 5465737420555249203100000000000000000000000000000000000000000000 // string893894 000000000000000000000000000000000000000000000000000000000000000c // third token id? Why ==12? #160895 0000000000000000000000000000000000000000000000000000000000000040 // offset of string896 000000000000000000000000000000000000000000000000000000000000000a // size of string897 5465737420555249203200000000000000000000000000000000000000000000 // string898 "899 );900901 let (call, mut decoder) = AbiReader::new_call(encoded_data).unwrap();902 assert_eq!(call, u32::to_be_bytes(decoded_data.0));903 let address = decoder.address().unwrap();904 let data =905 <AbiReader<'_> as AbiRead<Vec<(uint256, string)>>>::abi_read(&mut decoder).unwrap();906 assert_eq!(data, decoded_data.1);907908 let mut writer = AbiWriter::new_call(decoded_data.0);909 address.abi_write(&mut writer);910 decoded_data.1.abi_write(&mut writer);911 let ed = writer.finish();912 similar_asserts::assert_eq!(encoded_data, ed.as_slice());913 }914}