Skip to content

Commit

Permalink
feat(inspect): impl TryFrom<Log> for DecodedLog
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon-Becker committed Dec 11, 2023
1 parent 806aca0 commit 43d4b24
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
18 changes: 18 additions & 0 deletions common/src/ether/signatures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ pub trait ResolveSelector {
#[async_trait]
impl ResolveSelector for ResolvedError {
async fn resolve(selector: &str) -> Option<Vec<Self>> {
// normalize selector
let selector = match selector.strip_prefix("0x") {
Some(selector) => selector,
None => selector,
};

debug_max!("resolving error selector {}", &selector);

// get cached results
Expand Down Expand Up @@ -119,6 +125,12 @@ impl ResolveSelector for ResolvedError {
#[async_trait]
impl ResolveSelector for ResolvedLog {
async fn resolve(selector: &str) -> Option<Vec<Self>> {
// normalize selector
let selector = match selector.strip_prefix("0x") {
Some(selector) => selector,
None => selector,
};

debug_max!("resolving event selector {}", &selector);

// get cached results
Expand Down Expand Up @@ -198,6 +210,12 @@ impl ResolveSelector for ResolvedLog {
#[async_trait]
impl ResolveSelector for ResolvedFunction {
async fn resolve(selector: &str) -> Option<Vec<Self>> {
// normalize selector
let selector = match selector.strip_prefix("0x") {
Some(selector) => selector,
None => selector,
};

debug_max!("resolving event selector {}", &selector);

// get cached results
Expand Down
1 change: 1 addition & 0 deletions core/src/inspect/core/contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub struct Contracts {
transpose_api_key: Option<String>,
}

#[allow(dead_code)]
impl Contracts {
pub fn new(args: &InspectArgs) -> Self {
Self { contracts: HashMap::new(), transpose_api_key: args.transpose_api_key.clone() }
Expand Down
29 changes: 28 additions & 1 deletion core/src/inspect/core/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@

use async_convert::{async_trait, TryFrom};
use ethers::types::{Address, Bytes, Log, H256, U256, U64};
use heimdall_common::{
debug_max,
ether::signatures::{ResolveSelector, ResolvedLog},
utils::hex::ToLowerHex,
};
use serde::{Deserialize, Serialize};

/// Represents a decoded log
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, PartialOrd)]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct DecodedLog {
/// H160. the contract that emitted the log
pub address: Address,
Expand All @@ -19,6 +24,11 @@ pub struct DecodedLog {
/// Data
pub data: Bytes,

/// Resolved Event
#[serde(rename = "resolvedEvent")]
#[serde(skip_serializing_if = "Option::is_none")]
pub resolved_event: Option<ResolvedLog>,

/// Block Hash
#[serde(rename = "blockHash")]
#[serde(skip_serializing_if = "Option::is_none")]
Expand Down Expand Up @@ -66,6 +76,22 @@ impl TryFrom<Log> for DecodedLog {
type Error = crate::error::Error;

async fn try_from(value: Log) -> Result<Self, Self::Error> {
let signature = match value.topics.first() {
Some(topic) => {
let topic = topic.to_lower_hex();
Some(topic)
}
None => None,
};

let resolved_logs = match signature {
Some(signature) => {
debug_max!("resolving signature: {}", signature.to_string().to_lowercase());
ResolvedLog::resolve(&signature).await.unwrap_or(Vec::new())
}
None => Vec::new(),
};

Ok(Self {
address: value.address,
topics: value.topics,
Expand All @@ -78,6 +104,7 @@ impl TryFrom<Log> for DecodedLog {
transaction_log_index: value.transaction_log_index,
log_type: value.log_type,
removed: value.removed,
resolved_event: resolved_logs.first().cloned(),
})
}
}

0 comments on commit 43d4b24

Please sign in to comment.