Skip to content

Commit

Permalink
feat(inspect): implement DecodedTransactionTrace
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon-Becker committed Dec 10, 2023
1 parent 8b65118 commit cc7c9fd
Show file tree
Hide file tree
Showing 8 changed files with 401 additions and 223 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions common/src/resources/transpose.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ struct TransposeResponse {
}

/// executes a transpose SQL query and returns the response
/// TODO: exponential backoff
async fn call_transpose(query: &str, api_key: &str) -> Option<TransposeResponse> {
// get a new logger
let logger = Logger::default();
Expand Down
2 changes: 2 additions & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ strsim = "0.10.0"
tokio = {version = "1", features = ["full"]}
tui = "0.19"
derive_builder = "0.12.0"
async-convert = "1.0.0"
futures = "0.3.28"
2 changes: 2 additions & 0 deletions core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ pub enum Error {
RpcError(String),
#[error("Error: {0}")]
GenericError(String),
#[error("TransposeError: {0}")]
TransposeError(String),
}
75 changes: 75 additions & 0 deletions core/src/inspect/core/contracts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
use std::collections::{HashMap, HashSet};

use ethers::types::Address;
use futures::future::try_join_all;

use crate::{error::Error, inspect::InspectArgs};
use heimdall_common::{resources::transpose::get_label, utils::hex::ToLowerHex};

#[derive(Debug, Clone)]
pub struct Contracts {
pub contracts: HashMap<Address, String>,
transpose_api_key: Option<String>,
}

impl Contracts {
pub fn new(args: &InspectArgs) -> Self {
Self { contracts: HashMap::new(), transpose_api_key: args.transpose_api_key.clone() }
}

pub async fn add(&mut self, address: Address) -> Result<(), Error> {
// if alias already exists, don't overwrite
if self.contracts.contains_key(&address) {
return Ok(());
}

if let Some(transpose_api_key) = &self.transpose_api_key {
self.contracts.insert(
address,
get_label(&address.to_lower_hex(), transpose_api_key)
.await
.unwrap_or(address.to_lower_hex()),
);
} else {
self.contracts.insert(address, address.to_lower_hex());
}

Ok(())
}

pub async fn extend(&mut self, addresses: HashSet<Address>) -> Result<(), Error> {
// for each address, get the label
if let Some(transpose_api_key) = &self.transpose_api_key {
let handles: Vec<_> = addresses
.clone()
.into_iter()
.map(move |address| {
let transpose_api_key = transpose_api_key.clone();
tokio::spawn(async move {
get_label(&address.to_lower_hex(), &transpose_api_key).await
})
})
.collect();

let labels =
try_join_all(handles).await.map_err(|e| Error::TransposeError(e.to_string()))?;

self.contracts.extend(
addresses
.into_iter()
.zip(labels.into_iter())
.map(|(address, label)| (address, label.unwrap_or(address.to_lower_hex()))),
);
// replace None
} else {
self.contracts
.extend(addresses.into_iter().map(|address| (address, address.to_lower_hex())));
}

Ok(())
}

pub fn get(&self, address: Address) -> Option<&String> {
self.contracts.get(&address)
}
}
1 change: 1 addition & 0 deletions core/src/inspect/core/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub(crate) mod contracts;
pub mod tracing;
Loading

0 comments on commit cc7c9fd

Please sign in to comment.