2020-06-11 08:40:07 +00:00
|
|
|
//! This module implements extensions to the [`ethabi`](https://docs.rs/ethabi) API.
|
2020-05-28 14:53:22 +00:00
|
|
|
// Adapted from [Gnosis' ethcontract](https://github.com/gnosis/ethcontract-rs/blob/master/common/src/abiext.rs)
|
2020-05-31 16:01:34 +00:00
|
|
|
use crate::{types::Selector, utils::id};
|
2020-05-25 10:05:00 +00:00
|
|
|
|
2020-05-25 15:35:38 +00:00
|
|
|
pub use ethabi::Contract as Abi;
|
|
|
|
pub use ethabi::*;
|
|
|
|
|
2020-05-26 10:37:17 +00:00
|
|
|
mod tokens;
|
2020-05-26 11:00:56 +00:00
|
|
|
pub use tokens::{Detokenize, InvalidOutputType, Tokenizable, TokenizableItem, Tokenize};
|
2020-05-25 10:05:00 +00:00
|
|
|
|
2021-03-15 11:49:28 +00:00
|
|
|
pub mod struct_def;
|
|
|
|
pub use struct_def::SolStruct;
|
|
|
|
|
|
|
|
mod error;
|
|
|
|
pub use error::ParseError;
|
|
|
|
|
2020-10-29 07:48:24 +00:00
|
|
|
mod human_readable;
|
2021-03-15 11:49:28 +00:00
|
|
|
pub use human_readable::{parse as parse_abi, AbiParser};
|
2020-10-29 07:48:24 +00:00
|
|
|
|
2020-05-25 10:05:00 +00:00
|
|
|
/// Extension trait for `ethabi::Function`.
|
|
|
|
pub trait FunctionExt {
|
|
|
|
/// Compute the method signature in the standard ABI format. This does not
|
|
|
|
/// include the output types.
|
|
|
|
fn abi_signature(&self) -> String;
|
|
|
|
|
|
|
|
/// Compute the Keccak256 function selector used by contract ABIs.
|
|
|
|
fn selector(&self) -> Selector;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FunctionExt for Function {
|
|
|
|
fn abi_signature(&self) -> String {
|
|
|
|
let mut full_signature = self.signature();
|
|
|
|
if let Some(colon) = full_signature.find(':') {
|
|
|
|
full_signature.truncate(colon);
|
|
|
|
}
|
|
|
|
|
|
|
|
full_signature
|
|
|
|
}
|
|
|
|
|
|
|
|
fn selector(&self) -> Selector {
|
|
|
|
id(self.abi_signature())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Extension trait for `ethabi::Event`.
|
|
|
|
pub trait EventExt {
|
|
|
|
/// Compute the event signature in human-readable format. The `keccak256`
|
|
|
|
/// hash of this value is the actual event signature that is used as topic0
|
|
|
|
/// in the transaction logs.
|
|
|
|
fn abi_signature(&self) -> String;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl EventExt for Event {
|
|
|
|
fn abi_signature(&self) -> String {
|
|
|
|
format!(
|
|
|
|
"{}({}){}",
|
|
|
|
self.name,
|
|
|
|
self.inputs
|
|
|
|
.iter()
|
|
|
|
.map(|input| input.kind.to_string())
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.join(","),
|
|
|
|
if self.anonymous { " anonymous" } else { "" },
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn format_function_signature() {
|
|
|
|
for (f, expected) in &[
|
|
|
|
(
|
2020-06-16 12:08:42 +00:00
|
|
|
r#"{"name":"foo","inputs":[],"outputs":[], "stateMutability": "nonpayable"}"#,
|
|
|
|
"foo()",
|
|
|
|
),
|
|
|
|
(
|
|
|
|
r#"{"name":"bar","inputs":[{"name":"a","type":"uint256"},{"name":"b","type":"bool"}],"outputs":[], "stateMutability": "nonpayable"}"#,
|
2020-05-25 10:05:00 +00:00
|
|
|
"bar(uint256,bool)",
|
|
|
|
),
|
|
|
|
(
|
2020-06-16 12:08:42 +00:00
|
|
|
r#"{"name":"baz","inputs":[{"name":"a","type":"uint256"}],"outputs":[{"name":"b","type":"bool"}], "stateMutability": "nonpayable"}"#,
|
2020-05-25 10:05:00 +00:00
|
|
|
"baz(uint256)",
|
|
|
|
),
|
|
|
|
(
|
2020-06-16 12:08:42 +00:00
|
|
|
r#"{"name":"bax","inputs":[],"outputs":[{"name":"a","type":"uint256"},{"name":"b","type":"bool"}], "stateMutability": "nonpayable"}"#,
|
2020-05-25 10:05:00 +00:00
|
|
|
"bax()",
|
|
|
|
),
|
|
|
|
] {
|
|
|
|
let function: Function = serde_json::from_str(f).expect("invalid function JSON");
|
|
|
|
let signature = function.abi_signature();
|
|
|
|
assert_eq!(signature, *expected);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn format_event_signature() {
|
|
|
|
for (e, expected) in &[
|
|
|
|
(r#"{"name":"foo","inputs":[],"anonymous":false}"#, "foo()"),
|
|
|
|
(
|
|
|
|
r#"{"name":"bar","inputs":[{"name":"a","type":"uint256"},{"name":"b","type":"bool"}],"anonymous":false}"#,
|
|
|
|
"bar(uint256,bool)",
|
|
|
|
),
|
|
|
|
(
|
|
|
|
r#"{"name":"baz","inputs":[{"name":"a","type":"uint256"}],"anonymous":true}"#,
|
|
|
|
"baz(uint256) anonymous",
|
|
|
|
),
|
|
|
|
(
|
|
|
|
r#"{"name":"bax","inputs":[],"anonymous":true}"#,
|
|
|
|
"bax() anonymous",
|
|
|
|
),
|
|
|
|
] {
|
|
|
|
let event: Event = serde_json::from_str(e).expect("invalid event JSON");
|
|
|
|
let signature = event.abi_signature();
|
|
|
|
assert_eq!(signature, *expected);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|