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

Commit

Permalink
feat: generate bytecode static
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored and gakonst committed Mar 17, 2022
1 parent c93a16f commit 36a94fd
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
15 changes: 12 additions & 3 deletions ethers-contract/ethers-contract-abigen/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,13 @@ impl Context {
let name = &self.contract_ident;
let name_mod =
util::ident(&format!("{}_mod", self.contract_ident.to_string().to_lowercase()));

let abi_name = super::util::safe_ident(&format!("{}_ABI", name.to_string().to_uppercase()));
let abi_name = self.inline_abi_ident();

// 0. Imports
let imports = common::imports(&name.to_string());

// 1. Declare Contract struct
let struct_decl = common::struct_declaration(self, &abi_name);
let struct_decl = common::struct_declaration(self);

// 2. Declare events structs & impl FromTokens for each event
let events_decl = self.events_declaration()?;
Expand Down Expand Up @@ -247,6 +246,16 @@ impl Context {
&self.contract_name
}

/// name of the `Lazy` that stores the ABI
pub(crate) fn inline_abi_ident(&self) -> Ident {
util::safe_ident(&format!("{}_ABI", self.contract_ident.to_string().to_uppercase()))
}

/// name of the `Lazy` that stores the Bytecode
pub(crate) fn inline_bytecode_ident(&self) -> Ident {
util::safe_ident(&format!("{}_BYTECODE", self.contract_ident.to_string().to_uppercase()))
}

/// The internal abi struct mapping table
pub fn internal_structs(&self) -> &InternalStructs {
&self.internal_structs
Expand Down
18 changes: 17 additions & 1 deletion ethers-contract/ethers-contract-abigen/src/contract/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ pub(crate) fn imports(name: &str) -> TokenStream {
}

/// Generates the static `Abi` constants and the contract struct
pub(crate) fn struct_declaration(cx: &Context, abi_name: &proc_macro2::Ident) -> TokenStream {
pub(crate) fn struct_declaration(cx: &Context) -> TokenStream {
let name = &cx.contract_ident;
let abi = &cx.abi_str;

let abi_name = cx.inline_abi_ident();

let ethers_core = ethers_core_crate();
let ethers_providers = ethers_providers_crate();
let ethers_contract = ethers_contract_crate();
Expand All @@ -50,10 +52,24 @@ pub(crate) fn struct_declaration(cx: &Context, abi_name: &proc_macro2::Ident) ->
}
};

let bytecode = if let Some(ref bytecode) = cx.contract_bytecode {
let bytecode_name = cx.inline_bytecode_ident();
let hex_bytecode = format!("{}", bytecode);
quote! {
/// Bytecode of the #name contract
pub static #bytecode_name: #ethers_contract::Lazy<#ethers_core::types::Bytes> = #ethers_contract::Lazy::new(|| #hex_bytecode.parse()
.expect("invalid bytecode"));
}
} else {
quote! {}
};

quote! {
// Inline ABI declaration
#abi_parse

#bytecode

// Struct declaration
#[derive(Clone)]
pub struct #name<M>(#ethers_contract::Contract<M>);
Expand Down

0 comments on commit 36a94fd

Please sign in to comment.