Last active
February 16, 2018 15:21
-
-
Save axelchalon/b1afb020f1fe0995fab6923bcbcbab73 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// [OUTDATED] | |
// EthabiFunction | |
use ethabi; | |
const INTERNAL_ERR: &'static str = "`ethabi_derive` internal error"; | |
#[doc = r" Contract"] | |
pub struct Eip20 {} | |
impl Default for Eip20 { | |
fn default() -> Self { | |
Eip20 {} | |
} | |
} | |
impl Eip20 {} | |
pub mod events { | |
use ethabi; | |
use ethabi::ParseLog; | |
pub struct Approval { | |
event: ethabi::Event, | |
} | |
impl Default for Approval { | |
fn default() -> Self { | |
Approval { | |
event: ethabi::Event { | |
name: "Approval".to_owned(), | |
inputs: vec![ | |
ethabi::EventParam { | |
name: "owner".to_owned(), | |
kind: ethabi::ParamType::Address, | |
indexed: true, | |
}, | |
ethabi::EventParam { | |
name: "spender".to_owned(), | |
kind: ethabi::ParamType::Address, | |
indexed: true, | |
}, | |
ethabi::EventParam { | |
name: "value".to_owned(), | |
kind: ethabi::ParamType::Uint(256usize), | |
indexed: false, | |
}, | |
], | |
anonymous: false, | |
}, | |
} | |
} | |
} | |
impl ParseLog for Approval { | |
type Log = super::logs::Approval; | |
#[doc = r" Parses log."] | |
fn parse_log(&self, log: ethabi::RawLog) -> ethabi::Result<Self::Log> { | |
let mut log = self.event.parse_log(log)?.params.into_iter(); | |
let result = super::logs::Approval { | |
owner: log.next() | |
.expect(super::INTERNAL_ERR) | |
.value | |
.to_address() | |
.expect(super::INTERNAL_ERR), | |
spender: log.next() | |
.expect(super::INTERNAL_ERR) | |
.value | |
.to_address() | |
.expect(super::INTERNAL_ERR), | |
value: log.next() | |
.expect(super::INTERNAL_ERR) | |
.value | |
.to_uint() | |
.expect(super::INTERNAL_ERR), | |
}; | |
Ok(result) | |
} | |
} | |
impl Approval { | |
#[doc = r" Creates topic filter."] | |
pub fn create_filter< | |
T0: Into<ethabi::Topic<ethabi::Address>>, | |
T1: Into<ethabi::Topic<ethabi::Address>>, | |
>( | |
&self, | |
owner: T0, | |
spender: T1, | |
) -> ethabi::TopicFilter { | |
let raw = ethabi::RawTopicFilter { | |
topic0: owner.into().map(|i| ethabi::Token::Address(i)), | |
topic1: spender.into().map(|i| ethabi::Token::Address(i)), | |
..Default::default() | |
}; | |
self.event.create_filter(raw).expect(super::INTERNAL_ERR) | |
} | |
} | |
pub struct Transfer { | |
event: ethabi::Event, | |
} | |
impl Default for Transfer { | |
fn default() -> Self { | |
Transfer { | |
event: ethabi::Event { | |
name: "Transfer".to_owned(), | |
inputs: vec![ | |
ethabi::EventParam { | |
name: "from".to_owned(), | |
kind: ethabi::ParamType::Address, | |
indexed: true, | |
}, | |
ethabi::EventParam { | |
name: "to".to_owned(), | |
kind: ethabi::ParamType::Address, | |
indexed: true, | |
}, | |
ethabi::EventParam { | |
name: "value".to_owned(), | |
kind: ethabi::ParamType::Uint(256usize), | |
indexed: false, | |
}, | |
], | |
anonymous: false, | |
}, | |
} | |
} | |
} | |
impl ParseLog for Transfer { | |
type Log = super::logs::Transfer; | |
#[doc = r" Parses log."] | |
fn parse_log(&self, log: ethabi::RawLog) -> ethabi::Result<Self::Log> { | |
let mut log = self.event.parse_log(log)?.params.into_iter(); | |
let result = super::logs::Transfer { | |
from: log.next() | |
.expect(super::INTERNAL_ERR) | |
.value | |
.to_address() | |
.expect(super::INTERNAL_ERR), | |
to: log.next() | |
.expect(super::INTERNAL_ERR) | |
.value | |
.to_address() | |
.expect(super::INTERNAL_ERR), | |
value: log.next() | |
.expect(super::INTERNAL_ERR) | |
.value | |
.to_uint() | |
.expect(super::INTERNAL_ERR), | |
}; | |
Ok(result) | |
} | |
} | |
impl Transfer { | |
#[doc = r" Creates topic filter."] | |
pub fn create_filter< | |
T0: Into<ethabi::Topic<ethabi::Address>>, | |
T1: Into<ethabi::Topic<ethabi::Address>>, | |
>( | |
&self, | |
from: T0, | |
to: T1, | |
) -> ethabi::TopicFilter { | |
let raw = ethabi::RawTopicFilter { | |
topic0: from.into().map(|i| ethabi::Token::Address(i)), | |
topic1: to.into().map(|i| ethabi::Token::Address(i)), | |
..Default::default() | |
}; | |
self.event.create_filter(raw).expect(super::INTERNAL_ERR) | |
} | |
} | |
} | |
pub mod logs { | |
use ethabi; | |
#[derive(Debug)] | |
pub struct Approval { | |
pub owner: ethabi::Address, | |
pub spender: ethabi::Address, | |
pub value: ethabi::Uint, | |
} | |
#[derive(Debug)] | |
pub struct Transfer { | |
pub from: ethabi::Address, | |
pub to: ethabi::Address, | |
pub value: ethabi::Uint, | |
} | |
} | |
#[doc = r" Contract events"] | |
pub struct Eip20Events {} | |
impl Eip20Events { | |
pub fn approval(&self) -> events::Approval { | |
events::Approval::default() | |
} | |
pub fn transfer(&self) -> events::Transfer { | |
events::Transfer::default() | |
} | |
} | |
impl Eip20 { | |
#[doc = r" Get contract events"] | |
pub fn events(&self) -> Eip20Events { | |
Eip20Events {} | |
} | |
} | |
#[doc = r" Contract functions (for decoding output)"] | |
pub struct Outputs {} | |
impl Outputs { | |
#[doc = r" Returns the output for this contract function converted to native types"] | |
pub fn transfer(&self, output_bytes: &[u8]) -> ethabi::Result<bool> { | |
functions::Transfer::default().decode_output(&output_bytes) | |
} | |
#[doc = r" Returns the output for this contract function converted to native types"] | |
pub fn balance_of(&self, output_bytes: &[u8]) -> ethabi::Result<ethabi::Uint> { | |
functions::BalanceOf::default().decode_output(&output_bytes) | |
} | |
} | |
impl Eip20 { | |
#[doc = r" Gets contract functions (for decoding output)"] | |
pub fn outputs(&self) -> Outputs { | |
Outputs {} | |
} | |
} | |
pub mod functions { | |
use ethabi; | |
pub struct Transfer { | |
function: ethabi::Function, | |
} | |
impl Default for Transfer { | |
fn default() -> Self { | |
Transfer { | |
function: ethabi::Function { | |
name: "transfer".to_owned(), | |
inputs: vec![ | |
ethabi::Param { | |
name: "_to".to_owned(), | |
kind: ethabi::ParamType::Address, | |
}, | |
ethabi::Param { | |
name: "_value".to_owned(), | |
kind: ethabi::ParamType::Uint(256usize), | |
}, | |
], | |
outputs: vec![ | |
ethabi::Param { | |
name: "success".to_owned(), | |
kind: ethabi::ParamType::Bool, | |
}, | |
], | |
constant: false, | |
}, | |
} | |
} | |
} | |
impl Transfer { | |
#[allow(unused_variables)] | |
pub fn decode_output(&self, output: &[u8]) -> ethabi::Result<bool> { | |
let out = self.function | |
.decode_output(output)? | |
.into_iter() | |
.next() | |
.expect(super::INTERNAL_ERR); | |
Ok(out.to_bool().expect(super::INTERNAL_ERR)) | |
} | |
pub fn encode_input(&self, tokens: &[ethabi::Token]) -> ethabi::Result<ethabi::Bytes> { | |
self.function.encode_input(tokens) | |
} | |
} | |
pub struct BalanceOf { | |
function: ethabi::Function, | |
} | |
impl Default for BalanceOf { | |
fn default() -> Self { | |
BalanceOf { | |
function: ethabi::Function { | |
name: "balanceOf".to_owned(), | |
inputs: vec![ | |
ethabi::Param { | |
name: "_owner".to_owned(), | |
kind: ethabi::ParamType::Address, | |
}, | |
], | |
outputs: vec![ | |
ethabi::Param { | |
name: "balance".to_owned(), | |
kind: ethabi::ParamType::Uint(256usize), | |
}, | |
], | |
constant: true, | |
}, | |
} | |
} | |
} | |
impl BalanceOf { | |
#[allow(unused_variables)] | |
pub fn decode_output(&self, output: &[u8]) -> ethabi::Result<ethabi::Uint> { | |
let out = self.function | |
.decode_output(output)? | |
.into_iter() | |
.next() | |
.expect(super::INTERNAL_ERR); | |
Ok(out.to_uint().expect(super::INTERNAL_ERR)) | |
} | |
pub fn encode_input(&self, tokens: &[ethabi::Token]) -> ethabi::Result<ethabi::Bytes> { | |
self.function.encode_input(tokens) | |
} | |
} | |
} | |
#[doc = r" Contract function with already defined input values"] | |
pub struct TransferWithInput { | |
encoded_input: ethabi::Bytes, | |
} | |
impl ethabi::EthabiFunction for TransferWithInput { | |
type Output = bool; | |
#[doc = r" Returns the previously set function arguments encoded as bytes"] | |
fn encoded(&self) -> ethabi::Bytes { | |
self.encoded_input.clone() | |
} | |
fn output(&self, output_bytes: ethabi::Bytes) -> ethabi::Result<Self::Output> { | |
functions::Transfer::default().decode_output(&output_bytes) | |
} | |
} | |
impl TransferWithInput { | |
#[doc(hidden)] | |
pub fn from_tokens(v: Vec<ethabi::Token>) -> Self { | |
let encoded_input: ethabi::Bytes = functions::Transfer::default().encode_input(&v).expect( | |
INTERNAL_ERR, | |
); | |
TransferWithInput { encoded_input: encoded_input } | |
} | |
} | |
#[doc = r" Contract function with already defined input values"] | |
pub struct BalanceOfWithInput { | |
encoded_input: ethabi::Bytes, | |
} | |
impl ethabi::EthabiFunction for BalanceOfWithInput { | |
type Output = ethabi::Uint; | |
#[doc = r" Returns the previously set function arguments encoded as bytes"] | |
fn encoded(&self) -> ethabi::Bytes { | |
self.encoded_input.clone() | |
} | |
fn output(&self, output_bytes: ethabi::Bytes) -> ethabi::Result<Self::Output> { | |
functions::BalanceOf::default().decode_output(&output_bytes) | |
} | |
} | |
impl BalanceOfWithInput { | |
#[doc(hidden)] | |
pub fn from_tokens(v: Vec<ethabi::Token>) -> Self { | |
let encoded_input: ethabi::Bytes = functions::BalanceOf::default() | |
.encode_input(&v) | |
.expect(INTERNAL_ERR); | |
BalanceOfWithInput { encoded_input: encoded_input } | |
} | |
} | |
#[doc = r" Contract functions (for encoding input, making calls, transactions)"] | |
pub struct Eip20Functions {} | |
impl Eip20Functions { | |
#[doc = r" Sets the input (arguments) for this contract function"] | |
pub fn transfer<T0: Into<ethabi::Address>, T1: Into<ethabi::Uint>>( | |
&self, | |
to: T0, | |
value: T1, | |
) -> TransferWithInput { | |
let v: Vec<ethabi::Token> = vec![ | |
ethabi::Token::Address(to.into()), | |
ethabi::Token::Uint(value.into()), | |
]; | |
TransferWithInput::from_tokens(v) | |
} | |
#[doc = r" Sets the input (arguments) for this contract function"] | |
pub fn balance_of<T0: Into<ethabi::Address>>(&self, owner: T0) -> BalanceOfWithInput { | |
let v: Vec<ethabi::Token> = vec![ethabi::Token::Address(owner.into())]; | |
BalanceOfWithInput::from_tokens(v) | |
} | |
} | |
impl Eip20 { | |
#[doc = r" Gets contract functions (for encoding input, making calls, transactions)"] | |
pub fn functions(&self) -> Eip20Functions { | |
Eip20Functions {} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment