2020-06-22 08:44:08 +00:00
|
|
|
use super::{util, Context};
|
2020-05-26 18:57:59 +00:00
|
|
|
|
2021-10-10 08:31:34 +00:00
|
|
|
use proc_macro2::TokenStream;
|
2020-05-26 18:57:59 +00:00
|
|
|
use quote::quote;
|
|
|
|
|
2021-11-05 13:00:01 +00:00
|
|
|
use ethers_core::macros::{ethers_contract_crate, ethers_core_crate, ethers_providers_crate};
|
2021-08-28 18:53:01 +00:00
|
|
|
|
2020-06-22 08:44:08 +00:00
|
|
|
pub(crate) fn imports(name: &str) -> TokenStream {
|
|
|
|
let doc = util::expand_doc(&format!("{} was auto-generated with ethers-rs Abigen. More information at: https://github.com/gakonst/ethers-rs", name));
|
|
|
|
|
2021-08-28 18:53:01 +00:00
|
|
|
let ethers_core = ethers_core_crate();
|
|
|
|
let ethers_providers = ethers_providers_crate();
|
|
|
|
let ethers_contract = ethers_contract_crate();
|
|
|
|
|
2020-05-26 18:57:59 +00:00
|
|
|
quote! {
|
2021-08-06 12:47:57 +00:00
|
|
|
#![allow(clippy::enum_variant_names)]
|
2020-06-17 06:38:04 +00:00
|
|
|
#![allow(dead_code)]
|
2021-10-08 14:14:16 +00:00
|
|
|
#![allow(clippy::type_complexity)]
|
2020-06-17 06:38:04 +00:00
|
|
|
#![allow(unused_imports)]
|
2020-06-22 08:44:08 +00:00
|
|
|
#doc
|
|
|
|
|
|
|
|
use std::sync::Arc;
|
2021-08-28 18:53:01 +00:00
|
|
|
use #ethers_core::{
|
|
|
|
abi::{Abi, Token, Detokenize, InvalidOutputType, Tokenizable},
|
|
|
|
types::*, // import all the types so that we can codegen for everything
|
2020-05-26 18:57:59 +00:00
|
|
|
};
|
2021-08-28 18:53:01 +00:00
|
|
|
use #ethers_contract::{Contract, builders::{ContractCall, Event}, Lazy};
|
|
|
|
use #ethers_providers::Middleware;
|
2020-05-26 18:57:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-16 19:37:19 +00:00
|
|
|
/// Generates the static `Abi` constants and the contract struct
|
2020-06-02 21:10:46 +00:00
|
|
|
pub(crate) fn struct_declaration(cx: &Context, abi_name: &proc_macro2::Ident) -> TokenStream {
|
2022-02-24 20:09:08 +00:00
|
|
|
let name = &cx.contract_ident;
|
2020-05-26 18:57:59 +00:00
|
|
|
let abi = &cx.abi_str;
|
|
|
|
|
2021-08-28 18:53:01 +00:00
|
|
|
let ethers_core = ethers_core_crate();
|
|
|
|
let ethers_providers = ethers_providers_crate();
|
|
|
|
let ethers_contract = ethers_contract_crate();
|
|
|
|
|
2020-10-29 07:48:24 +00:00
|
|
|
let abi_parse = if !cx.human_readable {
|
|
|
|
quote! {
|
2021-08-28 18:53:01 +00:00
|
|
|
pub static #abi_name: #ethers_contract::Lazy<#ethers_core::abi::Abi> = #ethers_contract::Lazy::new(|| serde_json::from_str(#abi)
|
2020-10-29 07:48:24 +00:00
|
|
|
.expect("invalid abi"));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
quote! {
|
2021-08-28 18:53:01 +00:00
|
|
|
pub static #abi_name: #ethers_contract::Lazy<#ethers_core::abi::Abi> = #ethers_contract::Lazy::new(|| #ethers_core::abi::parse_abi_str(#abi)
|
2021-03-16 19:37:19 +00:00
|
|
|
.expect("invalid abi"));
|
2020-10-29 07:48:24 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-05-26 18:57:59 +00:00
|
|
|
quote! {
|
|
|
|
// Inline ABI declaration
|
2020-10-29 07:48:24 +00:00
|
|
|
#abi_parse
|
2020-05-26 18:57:59 +00:00
|
|
|
|
|
|
|
// Struct declaration
|
|
|
|
#[derive(Clone)]
|
2021-08-28 18:53:01 +00:00
|
|
|
pub struct #name<M>(#ethers_contract::Contract<M>);
|
2020-05-26 18:57:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
// Deref to the inner contract in order to access more specific functions functions
|
2020-09-24 21:33:09 +00:00
|
|
|
impl<M> std::ops::Deref for #name<M> {
|
2021-08-28 18:53:01 +00:00
|
|
|
type Target = #ethers_contract::Contract<M>;
|
2020-05-26 18:57:59 +00:00
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target { &self.0 }
|
|
|
|
}
|
|
|
|
|
2021-08-28 18:53:01 +00:00
|
|
|
impl<M: #ethers_providers::Middleware> std::fmt::Debug for #name<M> {
|
2020-05-26 18:57:59 +00:00
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
|
|
|
f.debug_tuple(stringify!(#name))
|
|
|
|
.field(&self.address())
|
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|