From 742db63767c0f555caad0c90349b610d39b751c7 Mon Sep 17 00:00:00 2001 From: Yaroslav Bolyukin Date: Wed, 01 Sep 2021 12:57:50 +0000 Subject: [PATCH] fix: each tuple should be decoded via subresult --- --- a/crates/evm-coder/src/abi.rs +++ b/crates/evm-coder/src/abi.rs @@ -211,6 +211,10 @@ self.memory(value.as_bytes()) } + pub fn bytes(&mut self, value: &[u8]) { + self.memory(value) + } + pub fn finish(mut self) -> Vec { for (static_offset, part) in self.dynamic_part { let part_offset = self.static_part.len(); @@ -261,7 +265,14 @@ Self: AbiRead, { fn abi_read(&mut self) -> Result> { - todo!() + let mut sub = self.subresult()?; + let size = sub.read_usize()?; + sub.subresult_offset = sub.offset; + let mut out = Vec::with_capacity(size); + for _ in 0..size { + out.push(>::abi_read(&mut sub)?); + } + Ok(out) } } @@ -273,8 +284,9 @@ $(Self: AbiRead<$ident>),+ { fn abi_read(&mut self) -> Result<($($ident,)+)> { + let mut subresult = self.subresult()?; Ok(( - $(>::abi_read(self)?,)+ + $(>::abi_read(&mut subresult)?,)+ )) } } @@ -318,6 +330,11 @@ writer.string(self) } } +impl AbiWrite for &Vec { + fn abi_write(&self, writer: &mut AbiWriter) { + writer.bytes(self) + } +} impl AbiWrite for () { fn abi_write(&self, _writer: &mut AbiWriter) {} @@ -353,6 +370,11 @@ #[cfg(test)] pub mod test { + use crate::{ + abi::AbiRead, + types::{string, uint256}, + }; + use super::{AbiReader, AbiWriter}; use hex_literal::hex; @@ -400,4 +422,48 @@ assert_eq!(decoder.uint32().unwrap(), 1); assert_eq!(decoder.string().unwrap(), "Test URI"); } + + #[test] + fn mint_bulk() { + let (call, mut decoder) = AbiReader::new_call(&hex!( + " + 36543006 + 00000000000000000000000053744e6da587ba10b32a2554d2efdcd985bc27a3 // address + 0000000000000000000000000000000000000000000000000000000000000040 // offset of (uint256, string)[] + 0000000000000000000000000000000000000000000000000000000000000003 // length of (uint256, string)[] + + 0000000000000000000000000000000000000000000000000000000000000060 // offset of first elem + 00000000000000000000000000000000000000000000000000000000000000e0 // offset of second elem + 0000000000000000000000000000000000000000000000000000000000000160 // offset of third elem + + 0000000000000000000000000000000000000000000000000000000000000001 // first token id? #60 + 0000000000000000000000000000000000000000000000000000000000000040 // offset of string + 000000000000000000000000000000000000000000000000000000000000000a // size of string + 5465737420555249203000000000000000000000000000000000000000000000 // string + + 000000000000000000000000000000000000000000000000000000000000000b // second token id? Why ==11? #e0 + 0000000000000000000000000000000000000000000000000000000000000040 // offset of string + 000000000000000000000000000000000000000000000000000000000000000a // size of string + 5465737420555249203100000000000000000000000000000000000000000000 // string + + 000000000000000000000000000000000000000000000000000000000000000c // third token id? Why ==12? #160 + 0000000000000000000000000000000000000000000000000000000000000040 // offset of string + 000000000000000000000000000000000000000000000000000000000000000a // size of string + 5465737420555249203200000000000000000000000000000000000000000000 // string + " + )) + .unwrap(); + assert_eq!(call, 0x36543006); + let _ = decoder.address().unwrap(); + let data = + as AbiRead>>::abi_read(&mut decoder).unwrap(); + assert_eq!( + data, + vec![ + (1.into(), "Test URI 0".to_string()), + (11.into(), "Test URI 1".to_string()), + (12.into(), "Test URI 2".to_string()) + ] + ); + } } -- gitstuff