diff --git a/book/src/smart-contracts-architecture/utils-module.md b/book/src/smart-contracts-architecture/utils-module.md index d497077a..c908f7f4 100644 --- a/book/src/smart-contracts-architecture/utils-module.md +++ b/book/src/smart-contracts-architecture/utils-module.md @@ -32,6 +32,8 @@ It contains the following files: - [precision.cairo](https://github.com/keep-starknet-strange/satoru/blob/main/src/utils/precision.cairo): This offers utility functions for detailed math and changing units, helping with accurate calculations and conversions between different measures, like from float to wei, applying factors, and managing rounding in the Satoru Starknet smart contract environment. +- [serializable_dict](https://github.com/keep-starknet-strange/satoru/blob/main/src/utils/serializable_dict.cairo): This file defines the SerializableFelt252Dict structure that allows us to use a Felt252Dict and serialize/deserialize it. + - [span32.cairo](https://github.com/keep-starknet-strange/satoru/blob/main/src/utils/span32.cairo): Provides utility functions for managing and manipulating fixed-size arrays (span32). A wrapper around Span type with a maximum size of 32. Used to prevent size overflow when storing Span. - [starknet_utils.cairo](https://github.com/keep-starknet-strange/satoru/blob/main/src/utils/starknet_utils.cairo): This puts in place fake utilities to mimic Starknet environment features, like `gasleft` and `tx.gasprice`, in the Satoru Starknet smart contract environment. These functions give back set values based on the given parameters, allowing a way to mimic Starknet gas actions during testing and development. diff --git a/src/event/event_utils.cairo b/src/event/event_utils.cairo index a3bf1e23..f2df87d3 100644 --- a/src/event/event_utils.cairo +++ b/src/event/event_utils.cairo @@ -81,7 +81,7 @@ struct LogData { } /// Number of dicts presents in LogData -const NUMBER_OF_DICTS: usize = 6; +const DICTS_IN_LOGDATA: usize = 6; /// When serializing dicts into a unique Array, this is the value that will /// be used to recognized a separation between two dicts. @@ -132,51 +132,43 @@ impl LogDataImpl of LogDataTrait { /// Deserialize all the sub-dicts serialized into a LogData fn deserialize(ref serialized: Span) -> Option { - let mut log_data: LogData = Default::default(); - - // There should be the right amount of dictionnaries serializeds - if serialized.occurrences_of(END_OF_DICT) != NUMBER_OF_DICTS { - return Option::None(()); + // There should be the right amount of dictionnaries serialized + if serialized.occurrences_of(END_OF_DICT) != DICTS_IN_LOGDATA { + panic_with_felt252('serialized format error'); } - // deserialize address_dict - let mut serialized_dict = get_dict_serialized(ref serialized); - log_data - .address_dict = - SerializableFelt252DictTrait::::deserialize(ref serialized_dict) + // Deserialize all dicts one by one + let mut serialized_dict = get_next_dict_serialized(ref serialized); + let address_dict = SerializableFelt252DictTrait::::deserialize( + ref serialized_dict + ) .expect('deserialize err address'); - // deserialize uint_dict - let mut serialized_dict = get_dict_serialized(ref serialized); - log_data - .uint_dict = SerializableFelt252DictTrait::::deserialize(ref serialized_dict) + let mut serialized_dict = get_next_dict_serialized(ref serialized); + let uint_dict = SerializableFelt252DictTrait::::deserialize(ref serialized_dict) .expect('deserialize err uint'); - // deserialize int_dict - let mut serialized_dict = get_dict_serialized(ref serialized); - log_data - .int_dict = SerializableFelt252DictTrait::::deserialize(ref serialized_dict) + let mut serialized_dict = get_next_dict_serialized(ref serialized); + let int_dict = SerializableFelt252DictTrait::::deserialize(ref serialized_dict) .expect('deserialize err int'); - // deserialize bool_dict - let mut serialized_dict = get_dict_serialized(ref serialized); - log_data - .bool_dict = SerializableFelt252DictTrait::::deserialize(ref serialized_dict) + let mut serialized_dict = get_next_dict_serialized(ref serialized); + let bool_dict = SerializableFelt252DictTrait::::deserialize(ref serialized_dict) .expect('deserialize err bool'); - // deserialize felt252_dict - let mut serialized_dict = get_dict_serialized(ref serialized); - log_data - .felt252_dict = - SerializableFelt252DictTrait::::deserialize(ref serialized_dict) + let mut serialized_dict = get_next_dict_serialized(ref serialized); + let felt252_dict = SerializableFelt252DictTrait::::deserialize(ref serialized_dict) .expect('deserialize err felt252'); - // deserialize string_dict - let mut serialized_dict = get_dict_serialized(ref serialized); - log_data - .string_dict = SerializableFelt252DictTrait::::deserialize(ref serialized_dict) + let mut serialized_dict = get_next_dict_serialized(ref serialized); + let string_dict = SerializableFelt252DictTrait::::deserialize(ref serialized_dict) .expect('deserialize err string'); + // Create the LogData struct with every dicts + let log_data: LogData = LogData { + address_dict, uint_dict, int_dict, bool_dict, felt252_dict, string_dict + }; + Option::Some(log_data) } } @@ -188,7 +180,7 @@ impl LogDataImpl of LogDataTrait { /// Pop every elements from the span until the next occurences of END_OF_DICT or /// the end of the Span and return those values in a Span. -fn get_dict_serialized(ref serialized: Span) -> Span { +fn get_next_dict_serialized(ref serialized: Span) -> Span { let mut dict_data: Array = array![]; loop { match serialized.pop_front() {