2021-08-28 21:06:29 +00:00
|
|
|
#![cfg(feature = "abigen")]
|
2021-03-16 19:37:19 +00:00
|
|
|
//! Test cases to validate the `abigen!` macro
|
|
|
|
use ethers_contract::{abigen, EthEvent};
|
2021-10-02 14:34:01 +00:00
|
|
|
use ethers_core::abi::{Address, Tokenizable};
|
|
|
|
use ethers_providers::Provider;
|
|
|
|
use std::sync::Arc;
|
2021-03-16 19:37:19 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_gen_human_readable() {
|
|
|
|
abigen!(
|
|
|
|
SimpleContract,
|
|
|
|
r#"[
|
|
|
|
event ValueChanged(address indexed author, string oldValue, string newValue)
|
|
|
|
]"#,
|
|
|
|
event_derives(serde::Deserialize, serde::Serialize)
|
|
|
|
);
|
|
|
|
assert_eq!("ValueChanged", ValueChangedFilter::name());
|
|
|
|
assert_eq!(
|
|
|
|
"ValueChanged(address,string,string)",
|
|
|
|
ValueChangedFilter::abi_signature()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_gen_structs_readable() {
|
|
|
|
abigen!(
|
|
|
|
SimpleContract,
|
|
|
|
r#"[
|
|
|
|
struct Value {address addr; string value;}
|
|
|
|
struct Addresses {address[] addr; string s;}
|
|
|
|
event ValueChanged(Value indexed old, Value newValue, Addresses _a)
|
|
|
|
]"#,
|
|
|
|
event_derives(serde::Deserialize, serde::Serialize)
|
|
|
|
);
|
|
|
|
let value = Addresses {
|
|
|
|
addr: vec!["eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee".parse().unwrap()],
|
|
|
|
s: "hello".to_string(),
|
|
|
|
};
|
|
|
|
let token = value.clone().into_token();
|
|
|
|
assert_eq!(value, Addresses::from_token(token).unwrap());
|
|
|
|
|
|
|
|
assert_eq!("ValueChanged", ValueChangedFilter::name());
|
|
|
|
assert_eq!(
|
|
|
|
"ValueChanged((address,string),(address,string),(address[],string))",
|
|
|
|
ValueChangedFilter::abi_signature()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-08-16 07:29:44 +00:00
|
|
|
#[test]
|
|
|
|
fn can_gen_structs_with_arrays_readable() {
|
|
|
|
abigen!(
|
|
|
|
SimpleContract,
|
|
|
|
r#"[
|
|
|
|
struct Value {address addr; string value;}
|
|
|
|
struct Addresses {address[] addr; string s;}
|
|
|
|
event ValueChanged(Value indexed old, Value newValue, Addresses[] _a)
|
|
|
|
]"#,
|
|
|
|
event_derives(serde::Deserialize, serde::Serialize)
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
"ValueChanged((address,string),(address,string),(address[],string)[])",
|
|
|
|
ValueChangedFilter::abi_signature()
|
|
|
|
);
|
|
|
|
}
|
2021-03-16 19:37:19 +00:00
|
|
|
|
2021-08-16 07:29:44 +00:00
|
|
|
fn assert_tokenizeable<T: Tokenizable>() {}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_generate_internal_structs() {
|
|
|
|
abigen!(
|
|
|
|
VerifierContract,
|
|
|
|
"ethers-contract/tests/solidity-contracts/verifier_abi.json",
|
|
|
|
event_derives(serde::Deserialize, serde::Serialize)
|
|
|
|
);
|
|
|
|
assert_tokenizeable::<VerifyingKey>();
|
|
|
|
assert_tokenizeable::<G1Point>();
|
|
|
|
assert_tokenizeable::<G2Point>();
|
|
|
|
}
|
2021-10-02 14:34:01 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_gen_human_readable_with_structs() {
|
|
|
|
abigen!(
|
|
|
|
SimpleContract,
|
|
|
|
r#"[
|
|
|
|
struct Foo { uint256 x; }
|
|
|
|
function foo(Foo memory x)
|
|
|
|
]"#,
|
|
|
|
event_derives(serde::Deserialize, serde::Serialize)
|
|
|
|
);
|
|
|
|
assert_tokenizeable::<Foo>();
|
|
|
|
|
|
|
|
let (client, _mock) = Provider::mocked();
|
|
|
|
let contract = SimpleContract::new(Address::default(), Arc::new(client));
|
2021-10-10 08:31:34 +00:00
|
|
|
let f = Foo { x: 100u64.into() };
|
|
|
|
let _ = contract.foo(f);
|
2021-10-02 14:34:01 +00:00
|
|
|
}
|