Skip to content

Commit

Permalink
fix: support contract JSON ABI with callpaths (#1354)
Browse files Browse the repository at this point in the history
* Support contract ABI with callpaths

* Add explanation blurb
  • Loading branch information
deekerno authored Sep 19, 2023
1 parent d31fd1a commit 46dea91
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 110 deletions.
25 changes: 25 additions & 0 deletions packages/fuel-indexer-macros/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,31 @@ pub fn can_derive_id(field_set: &HashSet<String>, field_name: &str) -> bool {
&& field_name != IdCol::to_lowercase_str()
}

/// Strip the call path from the type field of a `TypeDeclaration`.
///
/// It is possible that the type field for a `TypeDeclaration` contains a
/// fully-qualified path (e.g. `std::address::Address` as opposed to `Address`).
/// Path separators are not allowed to be used as part of an identifier, so this
/// function removes the qualifying path while keeping the type keyword.
pub fn strip_callpath_from_type_field(mut typ: TypeDeclaration) -> TypeDeclaration {
if is_non_decodable_type(&typ) {
return typ;
}

let mut s = typ.type_field.split_whitespace();
typ.type_field =
if let (Some(keyword), Some(fully_qualified_type_path)) = (s.next(), s.last()) {
if let Some(slug) = fully_qualified_type_path.split("::").last() {
[keyword, slug].join(" ")
} else {
unreachable!("All types should be formed with a keyword and call path")
}
} else {
typ.type_field
};
typ
}

/// Simply represents a value for a generic type.
#[derive(Debug)]
pub enum GenericType {
Expand Down
7 changes: 6 additions & 1 deletion packages/fuel-indexer-macros/src/indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,12 @@ fn process_fn_items(
let mut abi_dispatchers = Vec::new();

let funcs = abi.clone().functions;
let abi_types = abi.clone().types;
let abi_types: Vec<TypeDeclaration> = abi
.clone()
.types
.iter()
.map(|t| strip_callpath_from_type_field(t.clone()))
.collect();
let abi_log_types = abi.clone().logged_types.unwrap_or_default();
let abi_msg_types = abi.clone().messages_types.unwrap_or_default();
let fuel_types = FUEL_PRIMITIVES
Expand Down
Loading

0 comments on commit 46dea91

Please sign in to comment.