Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Commit

Permalink
fix(abigen): remove redundant index adjustment for many overloads (#1419
Browse files Browse the repository at this point in the history
)
  • Loading branch information
mattsse authored Jun 27, 2022
1 parent 6c89311 commit f17f900
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
18 changes: 4 additions & 14 deletions ethers-contract/ethers-contract-abigen/src/contract/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,20 +414,10 @@ impl Context {
let mut functions = functions.iter().enumerate().collect::<Vec<_>>();
functions.sort_by(|(_, f1), (_, f2)| f1.inputs.len().cmp(&f2.inputs.len()));

// the `functions` are now mapped with their index according as defined in the ABI, but
// we always want the zero arg function (`log()`) to be `log0`, even if it defined after
// an overloaded function like `log(address)`
if num_functions > NAME_ALIASING_OVERLOADED_FUNCTIONS_CAP {
// lots of overloads, so we set `log()` to index 0, and shift all fun
for (idx, _) in &mut functions[1..] {
*idx += 1;
}
functions[0].0 = 0;
} else {
// for few overloads we stick entirely to the input len order
for (idx, (f_idx, _)) in functions.iter_mut().enumerate() {
*f_idx = idx;
}
// the `functions` are now mapped with their index as defined in the ABI, but
// we always want the zero arg function (`log()`) to be `log0`
for (idx, (f_idx, _)) in functions.iter_mut().enumerate() {
*f_idx = idx;
}

// the first function will be the function with the least amount of inputs, like log()
Expand Down
25 changes: 25 additions & 0 deletions ethers-contract/tests/abigen.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![cfg(feature = "abigen")]
#![allow(unused)]
//! Test cases to validate the `abigen!` macro
use ethers_contract::{abigen, EthCall, EthEvent};
use ethers_core::{
Expand Down Expand Up @@ -601,3 +602,27 @@ fn can_gen_seaport() {
);
assert_eq!(hex::encode(FulfillAdvancedOrderCall::selector()), "e7acab24");
}

#[test]
fn can_generate_to_string_overload() {
abigen!(
ToString,
r#"[
toString(bytes)
toString(address)
toString(uint256)
toString(int256)
toString(bytes32)
toString(bool)
]"#
);

match ToStringCalls::ToString0(ToString0Call(Default::default())) {
ToStringCalls::ToString0(_) => {}
ToStringCalls::ToString1(_) => {}
ToStringCalls::ToString2(_) => {}
ToStringCalls::ToString3(_) => {}
ToStringCalls::ToString4(_) => {}
ToStringCalls::ToString5(_) => {}
};
}

0 comments on commit f17f900

Please sign in to comment.