Struct ethers_contract_abigen::Abigen
source · pub struct Abigen { /* private fields */ }
Expand description
Programmatically generate type-safe Rust bindings for an Ethereum smart contract from its ABI.
For all the supported ABI sources, see Source.
To generate bindings for multiple contracts at once, see MultiAbigen
.
To generate bindings at compile time, see the abigen! macro, or use in a build.rs
file.
§Example
Running the code below will generate a file called token.rs
containing the bindings inside,
which exports an ERC20Token
struct, along with all its events.
use ethers_contract_abigen::Abigen;
Abigen::new("ERC20Token", "./abi.json")?.generate()?.write_to_file("token.rs")?;
Implementations§
source§impl Abigen
impl Abigen
sourcepub fn new_raw(contract_name: Ident, abi_source: Source) -> Self
pub fn new_raw(contract_name: Ident, abi_source: Source) -> Self
Creates a new builder with the given contract name Ident and ABI source.
sourcepub fn from_file(path: impl AsRef<Path>) -> Result<Self>
pub fn from_file(path: impl AsRef<Path>) -> Result<Self>
Attempts to load a new builder from an ABI JSON file at the specific path.
sourcepub fn add_event_alias<S1, S2>(self, signature: S1, alias: S2) -> Self
pub fn add_event_alias<S1, S2>(self, signature: S1, alias: S2) -> Self
Manually adds a solidity event alias to specify what the event struct and function name will be in Rust.
For events without an alias, the PascalCase
event name will be used.
sourcepub fn add_method_alias<S1, S2>(self, signature: S1, alias: S2) -> Self
pub fn add_method_alias<S1, S2>(self, signature: S1, alias: S2) -> Self
Add a Solidity method error alias to specify the generated method name.
For methods without an alias, the snake_case
method name will be used.
sourcepub fn add_error_alias<S1, S2>(self, signature: S1, alias: S2) -> Self
pub fn add_error_alias<S1, S2>(self, signature: S1, alias: S2) -> Self
Add a Solidity custom error alias to specify the generated struct’s name.
For errors without an alias, the PascalCase
error name will be used.
sourcepub fn add_derive<S: AsRef<str>>(self, derive: S) -> Result<Self>
pub fn add_derive<S: AsRef<str>>(self, derive: S) -> Result<Self>
Add a custom derive to the derives for all structs and enums.
For example, this makes it possible to derive serde::Serialize and serde::Deserialize.
sourcepub fn format(self, format: bool) -> Self
pub fn format(self, format: bool) -> Self
Specify whether to format the code or not. True by default.
This will use prettyplease
, so the resulting formatted code will not be affected by
the local rustfmt
version or config.
sourcepub fn emit_cargo_directives(self, emit_cargo_directives: bool) -> Self
pub fn emit_cargo_directives(self, emit_cargo_directives: bool) -> Self
Specify whether to print cargo build script directives if the source is a path. By default, this is true only when executing inside of a build script.
sourcepub fn generate(self) -> Result<ContractBindings>
pub fn generate(self) -> Result<ContractBindings>
Generates the contract bindings.
sourcepub fn expand(self) -> Result<(ExpandedContract, Context)>
pub fn expand(self) -> Result<(ExpandedContract, Context)>
Expands the Abigen
and returns the ExpandedContract
that holds all tokens and the
Context
that holds the state used during expansion.
source§impl Abigen
impl Abigen
sourcepub fn source_mut(&mut self) -> &mut Source
pub fn source_mut(&mut self) -> &mut Source
Returns a mutable reference to the contract’s ABI source.
sourcepub fn method_aliases(&self) -> &HashMap<String, String>
pub fn method_aliases(&self) -> &HashMap<String, String>
Returns a reference to the contract’s method aliases.
sourcepub fn method_aliases_mut(&mut self) -> &mut HashMap<String, String>
pub fn method_aliases_mut(&mut self) -> &mut HashMap<String, String>
Returns a mutable reference to the contract’s method aliases.
sourcepub fn event_aliases(&self) -> &HashMap<String, String>
pub fn event_aliases(&self) -> &HashMap<String, String>
Returns a reference to the contract’s event aliases.
sourcepub fn error_aliases_mut(&mut self) -> &mut HashMap<String, String>
pub fn error_aliases_mut(&mut self) -> &mut HashMap<String, String>
Returns a mutable reference to the contract’s event aliases.
sourcepub fn derives_mut(&mut self) -> &mut Vec<Path>
pub fn derives_mut(&mut self) -> &mut Vec<Path>
Returns a mutable reference to the contract’s derives.