From 3b52c2fc7eb0568d9b0c3e72c6346a56888e3fdb Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Fri, 11 Nov 2022 23:25:31 +0100 Subject: [PATCH] fix: improve overloaded param diff matching (#1853) --- .../ethers-contract-abigen/src/contract/methods.rs | 14 ++++++++++++-- ethers-contract/tests/it/abigen.rs | 13 +++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/ethers-contract/ethers-contract-abigen/src/contract/methods.rs b/ethers-contract/ethers-contract-abigen/src/contract/methods.rs index cfda0b00..57e16d19 100644 --- a/ethers-contract/ethers-contract-abigen/src/contract/methods.rs +++ b/ethers-contract/ethers-contract-abigen/src/contract/methods.rs @@ -1,4 +1,4 @@ -use std::collections::{btree_map::Entry, BTreeMap, HashMap}; +use std::collections::{btree_map::Entry, BTreeMap, HashMap, HashSet}; use super::{types, util, Context}; use crate::{ @@ -534,11 +534,21 @@ impl Context { } // compare each overloaded function with the `first_fun` for (idx, overloaded_fun) in functions.into_iter().skip(1) { + // keep track of matched params + let mut already_matched_param_diff = HashSet::new(); // attempt to find diff in the input arguments let mut diff = Vec::new(); let mut same_params = true; for (idx, i1) in overloaded_fun.inputs.iter().enumerate() { - if first_fun.inputs.iter().all(|i2| i1 != i2) { + // Find the first param that differs and hasn't already been matched as diff + if let Some((pos, _)) = first_fun + .inputs + .iter() + .enumerate() + .filter(|(pos, _)| !already_matched_param_diff.contains(pos)) + .find(|(_, i2)| i1 != *i2) + { + already_matched_param_diff.insert(pos); diff.push(i1); same_params = false; } else { diff --git a/ethers-contract/tests/it/abigen.rs b/ethers-contract/tests/it/abigen.rs index f38f9538..41706d01 100644 --- a/ethers-contract/tests/it/abigen.rs +++ b/ethers-contract/tests/it/abigen.rs @@ -737,3 +737,16 @@ fn can_generate_event_with_structs() { assert_eq!("MyEvent((uint256,uint256),uint256)", MyEventFilter::abi_signature()); assert_event::(); } + +#[test] +fn can_handle_overloaded_function_with_array() { + abigen!( + Test, + r#"[ + serializeString(string calldata, string calldata, string calldata) external returns (string memory) + serializeString(string calldata, string calldata, string[] calldata) external returns (string memory) + serializeBool(string calldata, string calldata, bool) external returns (string memory) + serializeBool(string calldata, string calldata, bool[] calldata) external returns (string memory) + ]"#, + ); +}