1use crate::{2 abi::{AbiRead, AbiWrite},3 types::*,4};56use super::{AbiReader, AbiWriter};7use hex_literal::hex;8use primitive_types::{H160, U256};9use concat_idents::concat_idents;1011macro_rules! test_impl {12 ($name:ident, $type:ty, $function_identifier:expr, $decoded_data:expr, $encoded_data:expr) => {13 concat_idents!(test_name = encode_decode_, $name {14 #[test]15 fn test_name() {16 let function_identifier: u32 = $function_identifier;17 let decoded_data = $decoded_data;18 let encoded_data = $encoded_data;1920 let (call, mut decoder) = AbiReader::new_call(encoded_data).unwrap();21 assert_eq!(call, u32::to_be_bytes(function_identifier));22 let data = <$type>::abi_read(&mut decoder).unwrap();23 assert_eq!(data, decoded_data);2425 let mut writer = AbiWriter::new_call(function_identifier);26 decoded_data.abi_write(&mut writer);27 let ed = writer.finish();28 similar_asserts::assert_eq!(encoded_data, ed.as_slice());29 }30 });31 };32}3334macro_rules! test_impl_uint {35 ($type:ident) => {36 test_impl!(37 $type,38 $type,39 0xdeadbeef,40 255 as $type,41 &hex!(42 "43 deadbeef44 00000000000000000000000000000000000000000000000000000000000000ff45 "46 )47 );48 };49}5051test_impl_uint!(uint8);52test_impl_uint!(uint32);53test_impl_uint!(uint128);5455test_impl!(56 uint256,57 uint256,58 0xdeadbeef,59 U256([255, 0, 0, 0]),60 &hex!(61 "62 deadbeef63 00000000000000000000000000000000000000000000000000000000000000ff64 "65 )66);6768test_impl!(69 vec_tuple_address_uint256,70 Vec<(address, uint256)>,71 0x1ACF2D55,72 vec![73 (74 H160(hex!("2D2FF76104B7BACB2E8F6731D5BFC184EBECDDBC")),75 U256([10, 0, 0, 0]),76 ),77 (78 H160(hex!("AB8E3D9134955566483B11E6825C9223B6737B10")),79 U256([20, 0, 0, 0]),80 ),81 (82 H160(hex!("8C582BDF2953046705FC56F189385255EFC1BE18")),83 U256([30, 0, 0, 0]),84 ),85 ],86 &hex!(87 "88 1ACF2D5589 0000000000000000000000000000000000000000000000000000000000000020 // offset of (address, uint256)[]90 0000000000000000000000000000000000000000000000000000000000000003 // length of (address, uint256)[]9192 0000000000000000000000002D2FF76104B7BACB2E8F6731D5BFC184EBECDDBC // address93 000000000000000000000000000000000000000000000000000000000000000A // uint2569495 000000000000000000000000AB8E3D9134955566483B11E6825C9223B6737B10 // address96 0000000000000000000000000000000000000000000000000000000000000014 // uint2569798 0000000000000000000000008C582BDF2953046705FC56F189385255EFC1BE18 // address99 000000000000000000000000000000000000000000000000000000000000001E // uint256100 "101 )102);103104test_impl!(105 vec_tuple_uint256_string,106 Vec<(uint256, string)>,107 0xdeadbeef,108 vec![109 (1.into(), "Test URI 0".to_string()),110 (11.into(), "Test URI 1".to_string()),111 (12.into(), "Test URI 2".to_string()),112 ],113 &hex!(114 "115 deadbeef116 0000000000000000000000000000000000000000000000000000000000000020 // offset of (uint256, string)[]117 0000000000000000000000000000000000000000000000000000000000000003 // length of (uint256, string)[]118119 0000000000000000000000000000000000000000000000000000000000000060 // offset of first elem120 00000000000000000000000000000000000000000000000000000000000000e0 // offset of second elem121 0000000000000000000000000000000000000000000000000000000000000160 // offset of third elem122123 0000000000000000000000000000000000000000000000000000000000000001 // first token id? #60124 0000000000000000000000000000000000000000000000000000000000000040 // offset of string125 000000000000000000000000000000000000000000000000000000000000000a // size of string126 5465737420555249203000000000000000000000000000000000000000000000 // string127128 000000000000000000000000000000000000000000000000000000000000000b // second token id? Why ==11? #e0129 0000000000000000000000000000000000000000000000000000000000000040 // offset of string130 000000000000000000000000000000000000000000000000000000000000000a // size of string131 5465737420555249203100000000000000000000000000000000000000000000 // string132133 000000000000000000000000000000000000000000000000000000000000000c // third token id? Why ==12? #160134 0000000000000000000000000000000000000000000000000000000000000040 // offset of string135 000000000000000000000000000000000000000000000000000000000000000a // size of string136 5465737420555249203200000000000000000000000000000000000000000000 // string137 "138 )139);140141#[test]142fn dynamic_after_static() {143 let mut encoder = AbiWriter::new();144 encoder.bool(&true);145 encoder.string("test");146 let encoded = encoder.finish();147148 let mut encoder = AbiWriter::new();149 encoder.bool(&true);150 151 encoder.uint32(&(32 * 2));152 153 encoder.uint32(&4);154 encoder.write_padright(&[b't', b'e', b's', b't']);155 let alternative_encoded = encoder.finish();156157 assert_eq!(encoded, alternative_encoded);158159 let mut decoder = AbiReader::new(&encoded);160 assert!(decoder.bool().unwrap());161 assert_eq!(decoder.string().unwrap(), "test");162}163164#[test]165fn mint_sample() {166 let (call, mut decoder) = AbiReader::new_call(&hex!(167 "168 50bb4e7f169 000000000000000000000000ad2c0954693c2b5404b7e50967d3481bea432374170 0000000000000000000000000000000000000000000000000000000000000001171 0000000000000000000000000000000000000000000000000000000000000060172 0000000000000000000000000000000000000000000000000000000000000008173 5465737420555249000000000000000000000000000000000000000000000000174 "175 ))176 .unwrap();177 assert_eq!(call, u32::to_be_bytes(0x50bb4e7f));178 assert_eq!(179 format!("{:?}", decoder.address().unwrap()),180 "0xad2c0954693c2b5404b7e50967d3481bea432374"181 );182 assert_eq!(decoder.uint32().unwrap(), 1);183 assert_eq!(decoder.string().unwrap(), "Test URI");184}185186#[test]187fn parse_vec_with_dynamic_type() {188 let decoded_data = (189 0x36543006,190 vec![191 (1.into(), "Test URI 0".to_string()),192 (11.into(), "Test URI 1".to_string()),193 (12.into(), "Test URI 2".to_string()),194 ],195 );196197 let encoded_data = &hex!(198 "199 36543006200 00000000000000000000000053744e6da587ba10b32a2554d2efdcd985bc27a3 // address201 0000000000000000000000000000000000000000000000000000000000000040 // offset of (uint256, string)[]202 0000000000000000000000000000000000000000000000000000000000000003 // length of (uint256, string)[]203204 0000000000000000000000000000000000000000000000000000000000000060 // offset of first elem205 00000000000000000000000000000000000000000000000000000000000000e0 // offset of second elem206 0000000000000000000000000000000000000000000000000000000000000160 // offset of third elem207208 0000000000000000000000000000000000000000000000000000000000000001 // first token id? #60209 0000000000000000000000000000000000000000000000000000000000000040 // offset of string210 000000000000000000000000000000000000000000000000000000000000000a // size of string211 5465737420555249203000000000000000000000000000000000000000000000 // string212213 000000000000000000000000000000000000000000000000000000000000000b // second token id? Why ==11? #e0214 0000000000000000000000000000000000000000000000000000000000000040 // offset of string215 000000000000000000000000000000000000000000000000000000000000000a // size of string216 5465737420555249203100000000000000000000000000000000000000000000 // string217218 000000000000000000000000000000000000000000000000000000000000000c // third token id? Why ==12? #160219 0000000000000000000000000000000000000000000000000000000000000040 // offset of string220 000000000000000000000000000000000000000000000000000000000000000a // size of string221 5465737420555249203200000000000000000000000000000000000000000000 // string222 "223 );224225 let (call, mut decoder) = AbiReader::new_call(encoded_data).unwrap();226 assert_eq!(call, u32::to_be_bytes(decoded_data.0));227 let address = decoder.address().unwrap();228 let data = <Vec<(uint256, string)>>::abi_read(&mut decoder).unwrap();229 assert_eq!(data, decoded_data.1);230231 let mut writer = AbiWriter::new_call(decoded_data.0);232 address.abi_write(&mut writer);233 decoded_data.1.abi_write(&mut writer);234 let ed = writer.finish();235 similar_asserts::assert_eq!(encoded_data, ed.as_slice());236}237238test_impl!(239 vec_tuple_string_bytes,240 Vec<(string, bytes)>,241 0xdeadbeef,242 vec![243 (244 "Test URI 0".to_string(),245 bytes(vec![246 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,247 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,248 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,249 0x11, 0x11, 0x11, 0x11, 0x11, 0x11250 ])251 ),252 (253 "Test URI 1".to_string(),254 bytes(vec![255 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,256 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,257 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,258 0x22, 0x22, 0x22, 0x22, 0x22259 ])260 ),261 ("Test URI 2".to_string(), bytes(vec![0x33, 0x33])),262 ],263 &hex!(264 "265 deadbeef266 0000000000000000000000000000000000000000000000000000000000000020267 0000000000000000000000000000000000000000000000000000000000000003268 269 0000000000000000000000000000000000000000000000000000000000000060270 0000000000000000000000000000000000000000000000000000000000000140271 0000000000000000000000000000000000000000000000000000000000000220272273 0000000000000000000000000000000000000000000000000000000000000040274 0000000000000000000000000000000000000000000000000000000000000080275 000000000000000000000000000000000000000000000000000000000000000a276 5465737420555249203000000000000000000000000000000000000000000000277 0000000000000000000000000000000000000000000000000000000000000030278 1111111111111111111111111111111111111111111111111111111111111111279 1111111111111111111111111111111100000000000000000000000000000000280281 0000000000000000000000000000000000000000000000000000000000000040282 0000000000000000000000000000000000000000000000000000000000000080283 000000000000000000000000000000000000000000000000000000000000000a284 5465737420555249203100000000000000000000000000000000000000000000285 000000000000000000000000000000000000000000000000000000000000002f286 2222222222222222222222222222222222222222222222222222222222222222287 2222222222222222222222222222220000000000000000000000000000000000288289 0000000000000000000000000000000000000000000000000000000000000040290 0000000000000000000000000000000000000000000000000000000000000080291 000000000000000000000000000000000000000000000000000000000000000a292 5465737420555249203200000000000000000000000000000000000000000000293 0000000000000000000000000000000000000000000000000000000000000002294 3333000000000000000000000000000000000000000000000000000000000000295 "296 )297);