Skip to content

Commit

Permalink
fix: resolve topic_id packing issues
Browse files Browse the repository at this point in the history
  • Loading branch information
joshstevens19 committed Oct 15, 2024
1 parent b69ad8d commit 49e73af
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
41 changes: 31 additions & 10 deletions core/src/abi.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{collections::HashSet, fs, iter::Map, path::Path};
use std::{collections::HashSet, fs, path::Path};

use ethers::{
types::{ValueOrArray, H256},
Expand Down Expand Up @@ -178,12 +178,33 @@ pub enum ReadAbiError {

impl ABIItem {
pub fn format_event_signature(&self) -> Result<String, ParamTypeError> {
let formatted_inputs = self
.inputs
.iter()
.map(|component| component.format_param_type())
.collect::<Result<Vec<_>, _>>()?;
Ok(formatted_inputs.join(","))
let name = &self.name;
let params = self.inputs.iter()
.map(Self::format_param_type)
.collect::<Result<Vec<_>, _>>()?
.join(",");

Ok(format!("{}({})", name, params))
}

fn format_param_type(input: &ABIInput) -> Result<String, ParamTypeError> {
let base_type = input.type_.split('[').next().unwrap_or(&input.type_);
let array_suffix = input.type_.strip_prefix(base_type).unwrap_or("");

let type_str = match base_type {
"tuple" => {
let inner = input.components.as_ref()
.ok_or(ParamTypeError::MissingComponents)?
.iter()
.map(Self::format_param_type)
.collect::<Result<Vec<_>, _>>()?
.join(",");
format!("({})", inner)
},
_ => base_type.to_string(),
};

Ok(format!("{}{}", type_str, array_suffix))
}

pub fn extract_event_names_and_signatures_from_abi(
Expand All @@ -193,6 +214,7 @@ impl ABIItem {
for item in abi_json.into_iter() {
if item.type_ == "event" {
let signature = item.format_event_signature()?;
// println!("signature {}", signature);
events.push(EventInfo::new(item, signature));
}
}
Expand Down Expand Up @@ -275,13 +297,12 @@ impl EventInfo {
}

pub fn topic_id(&self) -> H256 {
let event_signature = format!("{}({})", self.name, self.signature);
let event_signature = self.signature.clone();
H256::from_slice(&keccak256(event_signature))
}

pub fn topic_id_as_hex_string(&self) -> String {
let event_signature = format!("{}({})", self.name, self.signature);
Map::collect(keccak256(event_signature).iter().map(|byte| format!("{:02x}", byte)))
format!("0x{:x}", self.topic_id())
}

pub fn struct_result(&self) -> &str {
Expand Down
2 changes: 1 addition & 1 deletion core/src/generator/events_bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ fn build_pub_contract_fn(
}
Some(value) => match value {
ValueOrArray::Value(address) => {
let address = format!("{}", address);
let address = format!("{:?}", address);
Code::new(format!(
r#"pub fn {contract_name}_contract(network: &str) -> {abi_gen_name}<Arc<Provider<RetryClient<Http>>>> {{
let address: Address = "{address}".parse().expect("Invalid address");
Expand Down
1 change: 1 addition & 0 deletions documentation/docs/pages/docs/changelog.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ github branch - https://github.com/joshstevens19/rindexer/tree/release/0.10.0
-------------------------------------------------
- fix: dependency events not being applied to the correct contract
- fix: resolve defining environment variables in contract address fields in the yaml
- fix: resolve topic_id packing issues

## 0.9.0-beta - 19th September 2024

Expand Down

0 comments on commit 49e73af

Please sign in to comment.