Skip to content

Commit

Permalink
feat(log_data_indexes): Refacto + Added serializable_dict to Satoru's…
Browse files Browse the repository at this point in the history
… book
  • Loading branch information
akhercha committed Nov 1, 2023
1 parent 5ebed4f commit fab23c7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 33 deletions.
2 changes: 2 additions & 0 deletions book/src/smart-contracts-architecture/utils-module.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
58 changes: 25 additions & 33 deletions src/event/event_utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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<felt252>, this is the value that will
/// be used to recognized a separation between two dicts.
Expand Down Expand Up @@ -132,51 +132,43 @@ impl LogDataImpl of LogDataTrait {

/// Deserialize all the sub-dicts serialized into a LogData
fn deserialize(ref serialized: Span<felt252>) -> Option<LogData> {
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::<ContractAddress>::deserialize(ref serialized_dict)
// Deserialize all dicts one by one
let mut serialized_dict = get_next_dict_serialized(ref serialized);
let address_dict = SerializableFelt252DictTrait::<ContractAddress>::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::<u128>::deserialize(ref serialized_dict)
let mut serialized_dict = get_next_dict_serialized(ref serialized);
let uint_dict = SerializableFelt252DictTrait::<u128>::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::<i128>::deserialize(ref serialized_dict)
let mut serialized_dict = get_next_dict_serialized(ref serialized);
let int_dict = SerializableFelt252DictTrait::<i128>::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::<bool>::deserialize(ref serialized_dict)
let mut serialized_dict = get_next_dict_serialized(ref serialized);
let bool_dict = SerializableFelt252DictTrait::<bool>::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::<felt252>::deserialize(ref serialized_dict)
let mut serialized_dict = get_next_dict_serialized(ref serialized);
let felt252_dict = SerializableFelt252DictTrait::<felt252>::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::<felt252>::deserialize(ref serialized_dict)
let mut serialized_dict = get_next_dict_serialized(ref serialized);
let string_dict = SerializableFelt252DictTrait::<felt252>::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)
}
}
Expand All @@ -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<felt252>) -> Span<felt252> {
fn get_next_dict_serialized(ref serialized: Span<felt252>) -> Span<felt252> {
let mut dict_data: Array<felt252> = array![];
loop {
match serialized.pop_front() {
Expand Down

0 comments on commit fab23c7

Please sign in to comment.