ensure functions do not clash with event names

This commit is contained in:
Georgios Konstantopoulos 2020-05-31 21:46:49 +03:00
parent 5170b7ec9f
commit c71c46ff56
No known key found for this signature in database
GPG Key ID: FA607837CD26EDBC
4 changed files with 23 additions and 16 deletions

View File

@ -8,11 +8,13 @@ pub(crate) fn imports() -> TokenStream {
quote! { quote! {
// TODO: Can we make this context aware so that it imports either ethers_contract // TODO: Can we make this context aware so that it imports either ethers_contract
// or ethers::contract? // or ethers::contract?
use ethers_contract::{ use ethers::{
core::{
abi::{Abi, Token, Detokenize, InvalidOutputType, Tokenizable}, abi::{Abi, Token, Detokenize, InvalidOutputType, Tokenizable},
Contract, ContractCall, Event, Lazy,
signers::{Client, Signer},
types::*, // import all the types so that we can codegen for everything types::*, // import all the types so that we can codegen for everything
},
contract::{Contract, ContractCall, Event, Lazy},
signers::{Client, Signer},
providers::{JsonRpcClient, networks::Network}, providers::{JsonRpcClient, networks::Network},
}; };
} }

View File

@ -44,9 +44,13 @@ impl Context {
/// Expands into a single method for contracting an event stream. /// Expands into a single method for contracting an event stream.
fn expand_filter(event: &Event) -> Result<TokenStream> { fn expand_filter(event: &Event) -> Result<TokenStream> {
let name = util::safe_ident(&event.name.to_snake_case()); // append `filter` to disambiguate with potentially conflicting
// function names
let name = util::safe_ident(&format!("{}_filter", event.name.to_snake_case()));
// let result = util::ident(&event.name.to_pascal_case());
let result = expand_struct_name(event);
let ev_name = Literal::string(&event.name); let ev_name = Literal::string(&event.name);
let result = util::ident(&event.name.to_pascal_case());
let doc = util::expand_doc(&format!("Gets the contract's `{}` event", event.name)); let doc = util::expand_doc(&format!("Gets the contract's `{}` event", event.name));
Ok(quote! { Ok(quote! {
@ -139,7 +143,8 @@ fn expand_event(event: &Event, event_derives: &[Path]) -> Result<TokenStream> {
/// Expands an ABI event into an identifier for its event data type. /// Expands an ABI event into an identifier for its event data type.
fn expand_struct_name(event: &Event) -> TokenStream { fn expand_struct_name(event: &Event) -> TokenStream {
let event_name = util::ident(&event.name.to_pascal_case()); let name = format!("{}Filter", event.name.to_pascal_case());
let event_name = util::ident(&name);
quote! { #event_name } quote! { #event_name }
} }
@ -314,7 +319,7 @@ mod tests {
assert_quote!(expand_filter(&event).unwrap(), { assert_quote!(expand_filter(&event).unwrap(), {
#[doc = "Gets the contract's `Transfer` event"] #[doc = "Gets the contract's `Transfer` event"]
pub fn transfer<'b>(&'a self) -> Event<'a, 'b, P, N, Transfer> pub fn transfer_filter<'b>(&'a self) -> Event<'a, 'b, P, N, TransferFilter>
where where
'a: 'b, 'a: 'b,
{ {
@ -389,12 +394,12 @@ mod tests {
let (definition, construction) = expand_data_struct(&name, &params); let (definition, construction) = expand_data_struct(&name, &params);
assert_quote!(definition, { assert_quote!(definition, {
struct Foo { struct FooFilter {
pub a: bool, pub a: bool,
pub p1: Address, pub p1: Address,
} }
}); });
assert_quote!(construction, { Foo { a, p1 } }); assert_quote!(construction, { FooFilter { a, p1 } });
} }
#[test] #[test]
@ -421,9 +426,9 @@ mod tests {
let (definition, construction) = expand_data_tuple(&name, &params); let (definition, construction) = expand_data_tuple(&name, &params);
assert_quote!(definition, { assert_quote!(definition, {
struct Foo(pub bool, pub Address); struct FooFilter(pub bool, pub Address);
}); });
assert_quote!(construction, { Foo(p0, p1) }); assert_quote!(construction, { FooFilter(p0, p1) });
} }
// #[test] // #[test]

View File

@ -53,7 +53,7 @@ async fn main() -> Result<()> {
let _tx_hash = contract.set_value("hi".to_owned()).send().await?; let _tx_hash = contract.set_value("hi".to_owned()).send().await?;
// 11. get all events // 11. get all events
let logs = contract.value_changed().from_block(0u64).query().await?; let logs = contract.value_changed_filter().from_block(0u64).query().await?;
// 12. get the new value // 12. get the new value
let value = contract.get_value().call().await?; let value = contract.get_value().call().await?;

View File

@ -45,7 +45,7 @@ pub mod contract {
/// network type and override the provider's ENS address with the `ens` method. /// network type and override the provider's ENS address with the `ens` method.
/// ///
/// ```rust /// ```rust
/// use ethers_providers::{HttpProvider, networks::Any}; /// use ethers::providers::{HttpProvider, networks::Any};
/// use std::convert::TryFrom; /// use std::convert::TryFrom;
/// use tokio::runtime::Runtime; /// use tokio::runtime::Runtime;
/// ///
@ -68,7 +68,7 @@ pub mod contract {
/// follows: /// follows:
/// ///
/// ```rust /// ```rust
/// # use ethers_providers::{HttpProvider, networks::Mainnet}; /// # use ethers::providers::{HttpProvider, networks::Mainnet};
/// # use std::convert::TryFrom; /// # use std::convert::TryFrom;
/// # use tokio::runtime::Runtime; /// # use tokio::runtime::Runtime;
/// # let provider = HttpProvider::<Mainnet>::try_from( /// # let provider = HttpProvider::<Mainnet>::try_from(
@ -147,7 +147,7 @@ pub mod signers {
/// signing the hash of the result. /// signing the hash of the result.
/// ///
/// ```rust /// ```rust
/// use ethers_core::types::{PrivateKey, Address}; /// use ethers::core::types::{PrivateKey, Address};
/// ///
/// let message = "Some data"; /// let message = "Some data";
/// let key = PrivateKey::new(&mut rand::thread_rng()); /// let key = PrivateKey::new(&mut rand::thread_rng());