fix(proc-macro): adjust to rest of contract fixes

This commit is contained in:
Georgios Konstantopoulos 2020-06-02 14:56:09 +03:00
parent c18e52f918
commit 701e442f94
No known key found for this signature in database
GPG Key ID: FA607837CD26EDBC
6 changed files with 13 additions and 201 deletions

View File

@ -81,12 +81,12 @@ impl Context {
#struct_decl #struct_decl
impl<'a, P: JsonRpcClient, N: Network, S: Signer> #name<'a, P, N, S> { impl<'a, P: JsonRpcClient, S: Signer> #name<'a, P, S> {
/// Creates a new contract instance with the specified `ethers` /// Creates a new contract instance with the specified `ethers`
/// client at the given `Address`. The contract derefs to a `ethers::Contract` /// client at the given `Address`. The contract derefs to a `ethers::Contract`
/// object /// object
pub fn new<T: Into<Address>>(address: T, client: &'a Client<'a, P, N, S>) -> Self { pub fn new<T: Into<Address>>(address: T, client: &'a Client<P, S>) -> Self {
let contract = Contract::new(client, &ABI, address.into()); let contract = Contract::new(address.into(), &ABI, client);
Self(contract) Self(contract)
} }

View File

@ -15,7 +15,7 @@ pub(crate) fn imports() -> TokenStream {
}, },
contract::{Contract, ContractCall, Event, Lazy}, contract::{Contract, ContractCall, Event, Lazy},
signers::{Client, Signer}, signers::{Client, Signer},
providers::{JsonRpcClient, networks::Network}, providers::JsonRpcClient,
}; };
} }
} }
@ -31,17 +31,17 @@ pub(crate) fn struct_declaration(cx: &Context) -> TokenStream {
// Struct declaration // Struct declaration
#[derive(Clone)] #[derive(Clone)]
pub struct #name<'a, P, N, S>(Contract<'a, P, N, S>); pub struct #name<'a, P, S>(Contract<'a, P, S>);
// Deref to the inner contract in order to access more specific functions functions // Deref to the inner contract in order to access more specific functions functions
impl<'a, P, N, S> std::ops::Deref for #name<'a, P, N, S> { impl<'a, P, S> std::ops::Deref for #name<'a, P, S> {
type Target = Contract<'a, P, N, S>; type Target = Contract<'a, P, S>;
fn deref(&self) -> &Self::Target { &self.0 } fn deref(&self) -> &Self::Target { &self.0 }
} }
impl<'a, P: JsonRpcClient, N: Network, S: Signer> std::fmt::Debug for #name<'a, P, N, S> { impl<'a, P: JsonRpcClient, S: Signer> std::fmt::Debug for #name<'a, P, S> {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.debug_tuple(stringify!(#name)) f.debug_tuple(stringify!(#name))
.field(&self.address()) .field(&self.address())

View File

@ -56,7 +56,7 @@ fn expand_filter(event: &Event) -> Result<TokenStream> {
Ok(quote! { Ok(quote! {
#doc #doc
pub fn #name<'b>(&'a self) -> Event<'a, 'b, P, N, #result> where 'a: 'b, { pub fn #name(&self) -> Event<P, #result> {
self.0.event(#ev_name).expect("event not found (this should never happen)") self.0.event(#ev_name).expect("event not found (this should never happen)")
} }
}) })
@ -319,10 +319,7 @@ mod tests {
assert_quote!(expand_filter(&event).unwrap(), { assert_quote!(expand_filter(&event).unwrap(), {
#[doc = "Gets the contract's `Transfer` event"] #[doc = "Gets the contract's `Transfer` event"]
pub fn transfer_filter<'b>(&'a self) -> Event<'a, 'b, P, N, TransferFilter> pub fn transfer_filter(&self) -> Event<P, TransferFilter> {
where
'a: 'b,
{
self.0 self.0
.event("Transfer") .event("Transfer")
.expect("event not found (this should never happen)") .expect("event not found (this should never happen)")

View File

@ -41,9 +41,9 @@ fn expand_function(function: &Function, alias: Option<Ident>) -> Result<TokenStr
let outputs = expand_fn_outputs(&function.outputs)?; let outputs = expand_fn_outputs(&function.outputs)?;
let result = if function.constant { let result = if function.constant {
quote! { ContractCall<'a, P, N, S, #outputs> } quote! { ContractCall<'a, P, S, #outputs> }
} else { } else {
quote! { ContractCall<'a, P, N, S, H256> } quote! { ContractCall<'a, P, S, H256> }
}; };
let arg = expand_inputs_call_arg(&function.inputs); let arg = expand_inputs_call_arg(&function.inputs);

View File

@ -1,185 +0,0 @@
pub use erc20token_mod::ERC20Token;
mod erc20token_mod {
use ethers_contract::{
abi::{Abi, Detokenize, InvalidOutputType, Token, Tokenizable},
providers::JsonRpcClient,
signers::{Client, Signer},
types::*,
Contract, Lazy,
};
static ABI: Lazy<Abi> = Lazy::new(|| {
serde_json :: from_str ( "[{\"constant\":true,\"inputs\":[],\"name\":\"name\",\"outputs\":[{\"name\":\"name\",\"type\":\"string\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"symbol\",\"outputs\":[{\"name\":\"symbol\",\"type\":\"string\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"decimals\",\"outputs\":[{\"name\":\"decimals\",\"type\":\"uint8\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"spender\",\"type\":\"address\"},{\"name\":\"value\",\"type\":\"uint256\"}],\"name\":\"approve\",\"outputs\":[{\"name\":\"success\",\"type\":\"bool\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"totalSupply\",\"outputs\":[{\"name\":\"totalSupply\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"from\",\"type\":\"address\"},{\"name\":\"to\",\"type\":\"address\"},{\"name\":\"value\",\"type\":\"uint256\"}],\"name\":\"transferFrom\",\"outputs\":[{\"name\":\"success\",\"type\":\"bool\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"who\",\"type\":\"address\"}],\"name\":\"balanceOf\",\"outputs\":[{\"name\":\"balance\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"to\",\"type\":\"address\"},{\"name\":\"value\",\"type\":\"uint256\"}],\"name\":\"transfer\",\"outputs\":[{\"name\":\"success\",\"type\":\"bool\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"owner\",\"type\":\"address\"},{\"name\":\"spender\",\"type\":\"address\"}],\"name\":\"allowance\",\"outputs\":[{\"name\":\"remaining\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"name\":\"owner\",\"type\":\"address\"},{\"indexed\":true,\"name\":\"spender\",\"type\":\"address\"},{\"indexed\":false,\"name\":\"value\",\"type\":\"uint256\"}],\"name\":\"Approval\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"name\":\"from\",\"type\":\"address\"},{\"indexed\":true,\"name\":\"to\",\"type\":\"address\"},{\"indexed\":false,\"name\":\"value\",\"type\":\"uint256\"}],\"name\":\"Transfer\",\"type\":\"event\"}]" ) . expect ( "invalid abi" )
});
#[derive(Clone)]
pub struct ERC20Token<'a, S, P>(Contract<'a, S, P>);
impl<'a, S, P> std::ops::Deref for ERC20Token<'a, S, P> {
type Target = Contract<'a, S, P>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<'a, S: Signer, P: JsonRpcClient> std::fmt::Debug for ERC20Token<'a, S, P> {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.debug_tuple(stringify!(ERC20Token))
.field(&self.address())
.finish()
}
}
impl<'a, S: Signer, P: JsonRpcClient> ERC20Token<'a, S, P> {
#[doc = r" Creates a new contract instance with the specified `ethers`"]
#[doc = r" client at the given `Address`. The contract derefs to a `ethers::Contract`"]
#[doc = r" object"]
pub fn new<T: Into<Address>>(address: T, client: &'a Client<'a, S, P>) -> Self {
let contract = Contract::new(client, ABI.clone(), address.into());
Self(contract)
}
#[doc = "Calls the contract's balanceOf function"]
pub fn balance_of(&self, who: Address) -> Sender<'a, S, P, U256> {
self.0
.method([112, 160, 130, 49], (who,))
.expect("method not found (this should never happen)")
}
#[doc = "Calls the contract's transfer function"]
pub fn transfer(&self, to: Address, value: U256) -> Sender<'a, S, P, H256> {
self.0
.method([169, 5, 156, 187], (to, value))
.expect("method not found (this should never happen)")
}
#[doc = "Calls the contract's approve function"]
pub fn approve(&self, spender: Address, value: U256) -> Sender<'a, S, P, H256> {
self.0
.method([9, 94, 167, 179], (spender, value))
.expect("method not found (this should never happen)")
}
#[doc = "Calls the contract's name function"]
pub fn name(&self) -> Sender<'a, S, P, String> {
self.0
.method([6, 253, 222, 3], ())
.expect("method not found (this should never happen)")
}
#[doc = "Calls the contract's decimals function"]
pub fn decimals(&self) -> Sender<'a, S, P, u8> {
self.0
.method([49, 60, 229, 103], ())
.expect("method not found (this should never happen)")
}
#[doc = "Calls the contract's symbol function"]
pub fn symbol(&self) -> Sender<'a, S, P, String> {
self.0
.method([149, 216, 155, 65], ())
.expect("method not found (this should never happen)")
}
#[doc = "Calls the contract's totalSupply function"]
pub fn total_supply(&self) -> Sender<'a, S, P, U256> {
self.0
.method([24, 22, 13, 221], ())
.expect("method not found (this should never happen)")
}
#[doc = "Calls the contract's transferFrom function"]
pub fn transfer_from(
&self,
from: Address,
to: Address,
value: U256,
) -> Sender<'a, S, P, H256> {
self.0
.method([35, 184, 114, 221], (from, to, value))
.expect("method not found (this should never happen)")
}
#[doc = "Calls the contract's allowance function"]
pub fn allowance(&self, owner: Address, spender: Address) -> Sender<'a, S, P, U256> {
self.0
.method([221, 98, 237, 62], (owner, spender))
.expect("method not found (this should never happen)")
}
}
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct Transfer {
pub from: Address,
pub to: Address,
pub value: U256,
}
impl Transfer {
#[doc = r" Retrieves the signature for the event this data corresponds to."]
#[doc = r" This signature is the Keccak-256 hash of the ABI signature of"]
#[doc = r" this event."]
pub const fn signature() -> H256 {
H256([
221, 242, 82, 173, 27, 226, 200, 155, 105, 194, 176, 104, 252, 55, 141, 170, 149,
43, 167, 241, 99, 196, 161, 22, 40, 245, 90, 77, 245, 35, 179, 239,
])
}
#[doc = r" Retrieves the ABI signature for the event this data corresponds"]
#[doc = r" to. For this event the value should always be:"]
#[doc = r""]
#[doc = "`Transfer(address,address,uint256)`"]
pub const fn abi_signature() -> &'static str {
"Transfer(address,address,uint256)"
}
}
impl Detokenize for Transfer {
fn from_tokens(tokens: Vec<Token>) -> Result<Self, InvalidOutputType> {
if tokens.len() != 3 {
return Err(InvalidOutputType(format!(
"Expected {} tokens, got {}: {:?}",
3,
tokens.len(),
tokens
)));
}
#[allow(unused_mut)]
let mut tokens = tokens.into_iter();
let from = Address::from_token(tokens.next().expect("this should never happen"))?;
let to = Address::from_token(tokens.next().expect("this should never happen"))?;
let value = U256::from_token(tokens.next().expect("this should never happen"))?;
Ok(Transfer { from, to, value })
}
}
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct Approval {
pub owner: Address,
pub spender: Address,
pub value: U256,
}
impl Approval {
#[doc = r" Retrieves the signature for the event this data corresponds to."]
#[doc = r" This signature is the Keccak-256 hash of the ABI signature of"]
#[doc = r" this event."]
pub const fn signature() -> H256 {
H256([
140, 91, 225, 229, 235, 236, 125, 91, 209, 79, 113, 66, 125, 30, 132, 243, 221, 3,
20, 192, 247, 178, 41, 30, 91, 32, 10, 200, 199, 195, 185, 37,
])
}
#[doc = r" Retrieves the ABI signature for the event this data corresponds"]
#[doc = r" to. For this event the value should always be:"]
#[doc = r""]
#[doc = "`Approval(address,address,uint256)`"]
pub const fn abi_signature() -> &'static str {
"Approval(address,address,uint256)"
}
}
impl Detokenize for Approval {
fn from_tokens(tokens: Vec<Token>) -> Result<Self, InvalidOutputType> {
if tokens.len() != 3 {
return Err(InvalidOutputType(format!(
"Expected {} tokens, got {}: {:?}",
3,
tokens.len(),
tokens
)));
}
#[allow(unused_mut)]
let mut tokens = tokens.into_iter();
let owner = Address::from_token(tokens.next().expect("this should never happen"))?;
let spender = Address::from_token(tokens.next().expect("this should never happen"))?;
let value = U256::from_token(tokens.next().expect("this should never happen"))?;
Ok(Approval {
owner,
spender,
value,
})
}
}
}
fn main() {}

View File

@ -47,7 +47,7 @@ async fn main() -> Result<()> {
let addr = contract.address(); let addr = contract.address();
// 9. instantiate the contract // 9. instantiate the contract
let contract = SimpleContract::new(*addr, &client); let contract = SimpleContract::new(addr, &client);
// 10. call the `setValue` method // 10. call the `setValue` method
let _tx_hash = contract.set_value("hi".to_owned()).send().await?; let _tx_hash = contract.set_value("hi".to_owned()).send().await?;