Struct ethers_contract::Abigen
source · pub struct Abigen { /* private fields */ }
abigen
only.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) -> Abigen
pub fn new_raw(contract_name: Ident, abi_source: Source) -> Abigen
Creates a new builder with the given contract name Ident and ABI source.
sourcepub fn from_file(path: impl AsRef<Path>) -> Result<Abigen, Report>
pub fn from_file(path: impl AsRef<Path>) -> Result<Abigen, Report>
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) -> Abigen
pub fn add_event_alias<S1, S2>(self, signature: S1, alias: S2) -> Abigen
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) -> Abigen
pub fn add_method_alias<S1, S2>(self, signature: S1, alias: S2) -> Abigen
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) -> Abigen
pub fn add_error_alias<S1, S2>(self, signature: S1, alias: S2) -> Abigen
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>(self, derive: S) -> Result<Abigen, Report>
pub fn add_derive<S>(self, derive: S) -> Result<Abigen, Report>
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) -> Abigen
pub fn format(self, format: bool) -> Abigen
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) -> Abigen
pub fn emit_cargo_directives(self, emit_cargo_directives: bool) -> Abigen
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, Report>
pub fn generate(self) -> Result<ContractBindings, Report>
Generates the contract bindings.
sourcepub fn expand(self) -> Result<(ExpandedContract, Context), Report>
pub fn expand(self) -> Result<(ExpandedContract, Context), Report>
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.