use ethers::core::types::{H160, H256, I256, U128, U256}; use ethers_contract::{EthAbiType, EthEvent}; use ethers_core::abi::Tokenizable; use ethers_core::types::Address; #[derive(Debug, Clone, PartialEq, EthAbiType)] struct ValueChanged { old_author: Address, new_author: Address, old_value: String, new_value: String, } #[derive(Debug, Clone, PartialEq, EthAbiType)] struct ValueChangedWrapper { inner: ValueChanged, msg: String, } #[derive(Debug, Clone, PartialEq, EthAbiType)] struct ValueChangedTuple(Address, Address, String, String); #[derive(Debug, Clone, PartialEq, EthAbiType)] struct ValueChangedTupleWrapper(ValueChangedTuple, String); #[test] fn can_detokenize_struct() { let value = ValueChanged { old_author: "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee".parse().unwrap(), new_author: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa".parse().unwrap(), old_value: "50".to_string(), new_value: "100".to_string(), }; let token = value.clone().into_token(); assert_eq!(value, ValueChanged::from_token(token).unwrap()); } #[test] fn can_detokenize_nested_structs() { let value = ValueChangedWrapper { inner: ValueChanged { old_author: "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee".parse().unwrap(), new_author: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa".parse().unwrap(), old_value: "50".to_string(), new_value: "100".to_string(), }, msg: "hello world".to_string(), }; let token = value.clone().into_token(); assert_eq!(value, ValueChangedWrapper::from_token(token).unwrap()); } #[test] fn can_detokenize_tuple_struct() { let value = ValueChangedTuple( "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee".parse().unwrap(), "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa".parse().unwrap(), "50".to_string(), "100".to_string(), ); let token = value.clone().into_token(); assert_eq!(value, ValueChangedTuple::from_token(token).unwrap()); } #[test] fn can_detokenize_nested_tuple_struct() { let value = ValueChangedTupleWrapper( ValueChangedTuple( "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee".parse().unwrap(), "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa".parse().unwrap(), "50".to_string(), "100".to_string(), ), "hello world".to_string(), ); let token = value.clone().into_token(); assert_eq!(value, ValueChangedTupleWrapper::from_token(token).unwrap()); } #[test] fn can_derive_eth_event() { #[derive(Debug, Clone, PartialEq, EthEvent)] struct ValueChangedEvent { old_author: Address, new_author: Address, old_value: String, new_value: String, } let value = ValueChangedEvent { old_author: "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee".parse().unwrap(), new_author: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa".parse().unwrap(), old_value: "50".to_string(), new_value: "100".to_string(), }; assert_eq!("ValueChangedEvent", ValueChangedEvent::name()); assert_eq!( "ValueChangedEvent(address,address,string,string)", ValueChangedEvent::abi_signature() ); let token = value.clone().into_token(); assert_eq!(value, ValueChangedEvent::from_token(token).unwrap()); } #[test] fn can_set_eth_event_name_attribute() { #[derive(Debug, PartialEq, EthEvent)] #[ethevent(name = "MyEvent")] struct ValueChangedEvent { old_author: Address, new_author: Address, old_value: String, new_value: String, } assert_eq!("MyEvent", ValueChangedEvent::name()); assert_eq!( "MyEvent(address,address,string,string)", ValueChangedEvent::abi_signature() ); } #[test] fn can_detect_various_event_abi_types() { #[derive(Debug, PartialEq, EthEvent)] struct ValueChangedEvent { old_author: Address, s: String, h1: H256, i256: I256, u256: U256, b: bool, v: Vec
, bs: Vec