Skip to content

Commit

Permalink
feat: copy inner composite for struct array keys
Browse files Browse the repository at this point in the history
  • Loading branch information
MartianGreed committed Oct 22, 2024
1 parent c3857b1 commit 450599a
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
37 changes: 37 additions & 0 deletions crates/parser/src/abi/parser.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use starknet::core::types::contract::{AbiEntry, AbiEvent, SierraClass, TypedAbiEvent};
use starknet::core::types::requests::TraceTransactionRequestRef;
use std::collections::HashMap;

use crate::tokens::{Array, Composite, CompositeInner, CompositeType, CoreBasic, Function, Token};
Expand Down Expand Up @@ -372,6 +373,21 @@ impl AbiParser {
.clone();
}
}
if let Token::Array(ref mut inner_array) = inner.token {
if let Token::Composite(ref mut inner_composite) = *inner_array.inner {
if inner_composite.r#type == CompositeType::Unknown {
*inner_array.inner = *Box::new(
filtered.get(&inner_composite.type_path)
.unwrap_or_else(|| panic!("In composite {} the inner token type for {} is expected to exist: {}",
name,
&inner.name,
&inner_composite.type_path
))
.clone()
);
}
}
}
}
}
}
Expand Down Expand Up @@ -425,4 +441,25 @@ mod tests {
assert_eq!(s.inners[1].name, "b");
assert_eq!(s.inners[2].name, "c");
}

#[test]
fn test_dojo_starter_direction_available_abi() {
let abi = AbiParser::tokens_from_abi_string(
include_str!("../../test_data/dojo_starter-directions_available.abi.json"),
&HashMap::new(),
)
.unwrap();

assert_eq!(abi.structs.len(), 1);
let s = abi.structs[0].to_composite().unwrap();
if let Token::Array(a) = &s.inners[1].token {
let inner_array = a.inner.to_composite().unwrap();
assert_eq!(5, inner_array.inners.len());
// Check that copy was properly done
let src_enum = abi.enums[0].to_composite().unwrap();
assert_eq!(inner_array, src_enum);
} else {
panic!("Expected array");
}
}
}
42 changes: 42 additions & 0 deletions crates/parser/test_data/dojo_starter-directions_available.abi.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
[
{
"type": "enum",
"name": "dojo_starter::models::Direction",
"variants": [
{
"name": "None",
"type": "()"
},
{
"name": "Left",
"type": "()"
},
{
"name": "Right",
"type": "()"
},
{
"name": "Up",
"type": "()"
},
{
"name": "Down",
"type": "()"
}
]
},
{
"type": "struct",
"name": "dojo_starter::models::DirectionsAvailable",
"members": [
{
"name": "player",
"type": "core::starknet::contract_address::ContractAddress"
},
{
"name": "directions",
"type": "core::array::Array::<dojo_starter::models::Direction>"
}
]
}
]

0 comments on commit 450599a

Please sign in to comment.