ethers-rs/crates/ethers-contract/ethers-contract-abigen/src/contract/common.rs

51 lines
1.6 KiB
Rust
Raw Normal View History

2020-05-26 18:57:59 +00:00
use super::Context;
use ethers_types::Address;
use proc_macro2::{Literal, TokenStream};
use quote::quote;
pub(crate) fn imports() -> TokenStream {
quote! {
// TODO: Can we make this context aware so that it imports either ethers_contract
// or ethers::contract?
2020-05-26 18:57:59 +00:00
use ethers_contract::{
abi::{Abi, Token, Detokenize, InvalidOutputType, Tokenizable},
Contract, ContractCall, Event, Lazy,
signers::{Client, Signer},
2020-05-26 18:57:59 +00:00
types::*, // import all the types so that we can codegen for everything
2020-05-27 15:43:43 +00:00
providers::{JsonRpcClient, networks::Network},
2020-05-26 18:57:59 +00:00
};
}
}
pub(crate) fn struct_declaration(cx: &Context) -> TokenStream {
let name = &cx.contract_name;
let abi = &cx.abi_str;
quote! {
// Inline ABI declaration
static ABI: Lazy<Abi> = Lazy::new(|| serde_json::from_str(#abi)
.expect("invalid abi"));
// Struct declaration
#[derive(Clone)]
2020-05-27 15:43:43 +00:00
pub struct #name<'a, P, N, S>(Contract<'a, P, N, S>);
2020-05-26 18:57:59 +00:00
// Deref to the inner contract in order to access more specific functions functions
2020-05-27 15:43:43 +00:00
impl<'a, P, N, S> std::ops::Deref for #name<'a, P, N, S> {
type Target = Contract<'a, P, N, S>;
2020-05-26 18:57:59 +00:00
fn deref(&self) -> &Self::Target { &self.0 }
}
2020-05-27 15:43:43 +00:00
impl<'a, P: JsonRpcClient, N: Network, S: Signer> std::fmt::Debug for #name<'a, P, N, S> {
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()
}
}
}
}