diff --git a/ethers-contract/ethers-contract-abigen/src/multi.rs b/ethers-contract/ethers-contract-abigen/src/multi.rs index 6a4af2e26..85de450e1 100644 --- a/ethers-contract/ethers-contract-abigen/src/multi.rs +++ b/ethers-contract/ethers-contract-abigen/src/multi.rs @@ -144,7 +144,11 @@ impl MultiAbigen { /// Build the contract bindings and prepare for writing pub fn build(self) -> Result { let format = self.abigens.iter().any(|gen| gen.format); - Ok(MultiBindings { expansion: MultiExpansion::from_abigen(self.abigens)?.expand(), format }) + Ok(MultiBindings { + expansion: MultiExpansion::from_abigen(self.abigens)?.expand(), + format, + dependencies: vec![], + }) } } @@ -299,7 +303,12 @@ impl MultiExpansionResult { } /// Converts this result into [`MultiBindingsInner`] - fn into_bindings(mut self, single_file: bool, format: bool) -> MultiBindingsInner { + fn into_bindings( + mut self, + single_file: bool, + format: bool, + dependencies: Vec, + ) -> MultiBindingsInner { self.set_shared_import_path(single_file); let Self { contracts, shared_types, root, .. } = self; let bindings = contracts @@ -333,7 +342,7 @@ impl MultiExpansionResult { None }; - MultiBindingsInner { root, bindings, shared_types } + MultiBindingsInner { root, bindings, shared_types, dependencies } } } @@ -366,6 +375,7 @@ impl MultiExpansionResult { pub struct MultiBindings { expansion: MultiExpansionResult, format: bool, + dependencies: Vec, } impl MultiBindings { @@ -396,8 +406,19 @@ impl MultiBindings { self } + /// Specify a set of dependencies to use for the generated crate. + /// + /// By default, this is empty and only the `ethers` dependency is added. + pub fn dependencies( + mut self, + dependencies: impl IntoIterator>, + ) -> Self { + self.dependencies = dependencies.into_iter().map(|dep| dep.into()).collect(); + self + } + fn into_inner(self, single_file: bool) -> MultiBindingsInner { - self.expansion.into_bindings(single_file, self.format) + self.expansion.into_bindings(single_file, self.format, self.dependencies) } /// Generates all the bindings and writes them to the given module @@ -581,6 +602,9 @@ struct MultiBindingsInner { bindings: BTreeMap, /// contains the content of the shared types if any shared_types: Option, + /// Dependencies other than `ethers-rs` to add to the `Cargo.toml` for bindings generated as a + /// crate. + dependencies: Vec, } // deref allows for inspection without modification @@ -611,6 +635,9 @@ impl MultiBindingsInner { writeln!(toml)?; writeln!(toml, "[dependencies]")?; writeln!(toml, r#"{crate_version}"#)?; + for dependency in self.dependencies.clone() { + writeln!(toml, "{}", dependency)?; + } Ok(toml) }