-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(torii-core): store update member (#2182)
* feat(torii-core): store update member * feat; add set_model_member for updating specific member * chore: format * chore: update log to include member nname * fmt * refactor: clean code & add is event message * fmt * chore * feat: emit the store member update from the world * fix: ensure event messages are emitted with the correct selector * fix: cairo fmt * fix: fix test typo * wip: * fix: simple types model memebr update * chore; pass memmber type to func * chore: clean code * fmt * wip * fix: upsert to avoid constraint CHECK failing * fmt * fix: figure out way to fix upsert issue to reuse func * refactor: refactor recursive set to handle store update member * fix: fix world typo and merge * fix: struct from ty as mutable * fix: compilable torii * feat: wrap up store update member * fmt * fix fmt * fmt * cairo fmt * fix: update example to set array with new model API * fix: handle arrays * fix: correctly handle arrays * chore * fix: test --------- Co-authored-by: glihm <dev@glihm.net>
- Loading branch information
Showing
26 changed files
with
564 additions
and
82 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
crates/dojo-lang/src/manifest_test_data/compiler_cairo/manifests/dev/base/dojo-world.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
kind = "Class" | ||
class_hash = "0x4c90da98d2bad157dec1d7b9b9c8c5861828a7ec1b323425d84fa6c3071303f" | ||
original_class_hash = "0x4c90da98d2bad157dec1d7b9b9c8c5861828a7ec1b323425d84fa6c3071303f" | ||
class_hash = "0x2178527e9556d1aa21d3c2961d28f9114fcfed81b4c3674ed591c50ce46cc9d" | ||
original_class_hash = "0x2178527e9556d1aa21d3c2961d28f9114fcfed81b4c3674ed591c50ce46cc9d" | ||
abi = "manifests/dev/base/abis/dojo-world.json" | ||
tag = "dojo-world" | ||
manifest_name = "dojo-world" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
crates/torii/core/src/processors/store_update_member.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
use anyhow::{Context, Error, Result}; | ||
use async_trait::async_trait; | ||
use dojo_world::contracts::model::ModelReader; | ||
use dojo_world::contracts::naming; | ||
use dojo_world::contracts::world::WorldContractReader; | ||
use num_traits::ToPrimitive; | ||
use starknet::core::types::{Event, TransactionReceiptWithBlockInfo}; | ||
use starknet::core::utils::get_selector_from_name; | ||
use starknet::providers::Provider; | ||
use tracing::{info, warn}; | ||
|
||
use super::EventProcessor; | ||
use crate::processors::{ENTITY_ID_INDEX, MODEL_INDEX}; | ||
use crate::sql::Sql; | ||
|
||
pub(crate) const LOG_TARGET: &str = "torii_core::processors::store_update_member"; | ||
|
||
const MEMBER_INDEX: usize = 2; | ||
|
||
#[derive(Default, Debug)] | ||
pub struct StoreUpdateMemberProcessor; | ||
|
||
#[async_trait] | ||
impl<P> EventProcessor<P> for StoreUpdateMemberProcessor | ||
where | ||
P: Provider + Send + Sync + std::fmt::Debug, | ||
{ | ||
fn event_key(&self) -> String { | ||
"StoreUpdateMember".to_string() | ||
} | ||
|
||
fn validate(&self, event: &Event) -> bool { | ||
if event.keys.len() > 1 { | ||
info!( | ||
target: LOG_TARGET, | ||
event_key = %<StoreUpdateMemberProcessor as EventProcessor<P>>::event_key(self), | ||
invalid_keys = %<StoreUpdateMemberProcessor as EventProcessor<P>>::event_keys_as_string(self, event), | ||
"Invalid event keys." | ||
); | ||
return false; | ||
} | ||
true | ||
} | ||
|
||
async fn process( | ||
&self, | ||
_world: &WorldContractReader<P>, | ||
db: &mut Sql, | ||
_block_number: u64, | ||
block_timestamp: u64, | ||
_transaction_receipt: &TransactionReceiptWithBlockInfo, | ||
event_id: &str, | ||
event: &Event, | ||
) -> Result<(), Error> { | ||
let selector = event.data[MODEL_INDEX]; | ||
let entity_id = event.data[ENTITY_ID_INDEX]; | ||
let member_selector = event.data[MEMBER_INDEX]; | ||
|
||
let model = db.model(selector).await?; | ||
let schema = model.schema().await?; | ||
|
||
let mut member = schema | ||
.as_struct() | ||
.expect("model schema must be a struct") | ||
.children | ||
.iter() | ||
.find(|c| { | ||
get_selector_from_name(&c.name).expect("invalid selector for member name") | ||
== member_selector | ||
}) | ||
.context("member not found")? | ||
.clone(); | ||
|
||
info!( | ||
target: LOG_TARGET, | ||
name = %model.name(), | ||
entity_id = format!("{:#x}", entity_id), | ||
member = %member.name, | ||
"Store update member.", | ||
); | ||
|
||
let values_start = MEMBER_INDEX + 1; | ||
let values_end: usize = | ||
values_start + event.data[values_start].to_usize().context("invalid usize")?; | ||
|
||
// Skip the length to only get the values as they will be deserialized. | ||
let mut values = event.data[values_start + 1..=values_end].to_vec(); | ||
|
||
let tag = naming::get_tag(model.namespace(), model.name()); | ||
|
||
if !db.does_entity_exist(tag.clone(), entity_id).await? { | ||
warn!( | ||
target: LOG_TARGET, | ||
tag, | ||
entity_id = format!("{:#x}", entity_id), | ||
"Entity not found, must be set before updating a member.", | ||
); | ||
|
||
return Ok(()); | ||
} | ||
|
||
member.ty.deserialize(&mut values)?; | ||
|
||
db.set_model_member(&schema.name(), entity_id, false, &member, event_id, block_timestamp) | ||
.await?; | ||
Ok(()) | ||
} | ||
} |
Oops, something went wrong.