Skip to content

Commit

Permalink
feat: Use indexes for LogData (#539)
Browse files Browse the repository at this point in the history
* feat(log_data_indexes): Added SerializableFelt252Dict

* feat(log_data_indexes): Added docstring for SerializableFelt252Dict

* feat(log_data_indexes): Draft

* feat(log_data_indexes): Added TODO

* feat(log_data_indexes): get->Option<T> instead of get->T

* feat(log_data_indexes): Test almost working

* feat(log_data_indexes): Serializing OK but still errs

* feat(log_data_indexes): Revert event_utils & trying different things in SerializableFeltDict

* feat(log_data_indexes): OrderedDict tries

* feat(log_data_indexes): Compiles

* feat(log_data_indexes): Added i128

* feat(log_data_indexes): Added felt252

* feat(log_data_indexes): Added TODOs

* feat(log_data_indexes): Renamed structs + address_items impl

* feat(log_data_indexes): Generic impl

* feat(log_data_indexes): fmt

* feat(log_data_indexes): Trigger CI to check semgrep

* feat(log_data_indexes): Renaming + Started unit tests

* feat(log_data_indexes): Merge

* feat(log_data_indexes): Update SerializableDict with custom i128

* feat(log_data_indexes): Checkpoint; units break

* feat(log_data_indexes): Replaced SerializableDict's Array by Span

* feat(log_data_indexes): Utilities fn

* feat(log_data_indexes): Adding unit tests for debug

* feat(log_data_indexes): TODO: investigate serialization fail

* feat(log_data_indexes): update

* feat(log_data_indexes): Using a custom (de)serialization

* feat(log_data_indexes): TODO: implement custom_(de)serialize for LogData

* feat(log_data_indexes): Added serialization for LogData

* feat(log_data_indexes): Dict working

* feat(log_data_indexes): Removed sandbox file

* feat(log_data_indexes): TODO: deserialize + tests

* feat(log_data_indexes): Implement deserialize; need tests

* feat(log_data_indexes): Added tests for log_data serialization

* feat(log_data_indexes): Typo + Don't serialize too early

* feat(log_data_indexes): Docstrings

* feat(log_data_indexes): Updated failed docstring

* feat(log_data_indexes): Refacto + Added serializable_dict to Satoru's book

* feat(log_data_indexes): Removed Copy Trait for SerializableDict

* feat(log_data_indexes): Added .cairo ext

---------

Co-authored-by: Michel <[email protected]>
  • Loading branch information
akhercha and Sk8erboi84 authored Nov 1, 2023
1 parent 4047c6d commit 282539e
Show file tree
Hide file tree
Showing 18 changed files with 846 additions and 347 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.cairo](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
30 changes: 15 additions & 15 deletions src/callback/callback_utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use starknet::ContractAddress;
// Local imports.
use satoru::data::data_store::{IDataStoreDispatcher, IDataStoreDispatcherTrait};
use satoru::data::keys;
use satoru::event::event_utils::LogData;
use satoru::event::event_utils::{LogData, LogDataTrait};
use satoru::order::order::Order;
use satoru::deposit::deposit::Deposit;
use satoru::withdrawal::withdrawal::Withdrawal;
Expand Down Expand Up @@ -92,98 +92,98 @@ fn get_saved_callback_contract(
/// * `key` - They key of the deposit.
/// * `deposit` - The deposit that was executed.
/// * `log_data` - The log data.
fn after_deposit_execution(key: felt252, deposit: Deposit, log_data: LogData) {
fn after_deposit_execution(key: felt252, deposit: Deposit, mut log_data: LogData) {
if !is_valid_callback_contract(deposit.callback_contract) {
return;
}
let dispatcher = IDepositCallbackReceiverDispatcher {
contract_address: deposit.callback_contract
};
dispatcher.after_deposit_execution(key, deposit, log_data)
dispatcher.after_deposit_execution(key, deposit, log_data.serialize_into())
}

/// Called after a deposit cancellation.
/// # Arguments
/// * `key` - They key of the deposit.
/// * `deposit` - The deposit that was cancelled.
/// * `log_data` - The log data.
fn after_deposit_cancellation(key: felt252, deposit: Deposit, log_data: LogData) {
fn after_deposit_cancellation(key: felt252, deposit: Deposit, mut log_data: LogData) {
if !is_valid_callback_contract(deposit.callback_contract) {
return;
}
let dispatcher = IDepositCallbackReceiverDispatcher {
contract_address: deposit.callback_contract
};
dispatcher.after_deposit_cancellation(key, deposit, log_data)
dispatcher.after_deposit_cancellation(key, deposit, log_data.serialize_into())
}

/// Called after a withdrawal execution.
/// # Arguments
/// * `key` - They key of the withdrawal.
/// * `withdrawal` - The withdrawal that was executed.
/// * `log_data` - The log data.
fn after_withdrawal_execution(key: felt252, withdrawal: Withdrawal, log_data: LogData) {
fn after_withdrawal_execution(key: felt252, withdrawal: Withdrawal, mut log_data: LogData) {
if !is_valid_callback_contract(withdrawal.callback_contract) {
return;
}
let dispatcher = IWithdrawalCallbackReceiverDispatcher {
contract_address: withdrawal.callback_contract
};
dispatcher.after_withdrawal_execution(key, withdrawal, log_data)
dispatcher.after_withdrawal_execution(key, withdrawal, log_data.serialize_into())
}

/// Called after an withdrawal cancellation.
/// # Arguments
/// * `key` - They key of the withdrawal.
/// * `withdrawal` - The withdrawal that was cancelled.
/// * `log_data` - The log data.
fn after_withdrawal_cancellation(key: felt252, withdrawal: Withdrawal, log_data: LogData) {
fn after_withdrawal_cancellation(key: felt252, withdrawal: Withdrawal, mut log_data: LogData) {
if !is_valid_callback_contract(withdrawal.callback_contract) {
return;
}
let dispatcher = IWithdrawalCallbackReceiverDispatcher {
contract_address: withdrawal.callback_contract
};
dispatcher.after_withdrawal_cancellation(key, withdrawal, log_data)
dispatcher.after_withdrawal_cancellation(key, withdrawal, log_data.serialize_into())
}

/// Called after an order execution.
/// # Arguments
/// * `key` - They key of the order.
/// * `order` - The order that was executed.
/// * `log_data` - The log data.
fn after_order_execution(key: felt252, order: Order, log_data: LogData) {
fn after_order_execution(key: felt252, order: Order, mut log_data: LogData) {
if !is_valid_callback_contract(order.callback_contract) {
return;
}
let dispatcher = IOrderCallbackReceiverDispatcher { contract_address: order.callback_contract };
dispatcher.after_order_execution(key, order, log_data)
dispatcher.after_order_execution(key, order, log_data.serialize_into())
}

/// Called after an order cancellation.
/// # Arguments
/// * `key` - They key of the order.
/// * `order` - The order that was cancelled.
/// * `log_data` - The log data.
fn after_order_cancellation(key: felt252, order: Order, log_data: LogData) {
fn after_order_cancellation(key: felt252, order: Order, mut log_data: LogData) {
if !is_valid_callback_contract(order.callback_contract) {
return;
}
let dispatcher = IOrderCallbackReceiverDispatcher { contract_address: order.callback_contract };
dispatcher.after_order_cancellation(key, order, log_data)
dispatcher.after_order_cancellation(key, order, log_data.serialize_into())
}

/// Called after an order cancellation.
/// # Arguments
/// * `key` - They key of the order.
/// * `order` - The order that was frozen.
/// * `log_data` - The log data.
fn after_order_frozen(key: felt252, order: Order, log_data: LogData) {
fn after_order_frozen(key: felt252, order: Order, mut log_data: LogData) {
if !is_valid_callback_contract(order.callback_contract) {
return;
}
let dispatcher = IOrderCallbackReceiverDispatcher { contract_address: order.callback_contract };
dispatcher.after_order_frozen(key, order, log_data)
dispatcher.after_order_frozen(key, order, log_data.serialize_into())
}

/// Validates that the given address is a contract.
Expand Down
4 changes: 2 additions & 2 deletions src/callback/deposit_callback_receiver/interface.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ trait IDepositCallbackReceiver<TContractState> {
/// * `event_data` - The event log data.
/// * `deposit` - The deposit that was executed.
fn after_deposit_execution(
ref self: TContractState, key: felt252, deposit: Deposit, log_data: LogData,
ref self: TContractState, key: felt252, deposit: Deposit, log_data: Array<felt252>,
);

/// Called after a deposit cancellation.
Expand All @@ -22,6 +22,6 @@ trait IDepositCallbackReceiver<TContractState> {
/// * `event_data` - The event log data.
/// * `deposit` - The deposit that was cancelled.
fn after_deposit_cancellation(
ref self: TContractState, key: felt252, deposit: Deposit, log_data: LogData,
ref self: TContractState, key: felt252, deposit: Deposit, log_data: Array<felt252>,
);
}
4 changes: 2 additions & 2 deletions src/callback/mocks.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ mod CallbackMock {
#[external(v0)]
impl IDepositCallbackReceiverImpl of IDepositCallbackReceiver<ContractState> {
fn after_deposit_execution(
ref self: ContractState, key: felt252, deposit: Deposit, log_data: LogData,
ref self: ContractState, key: felt252, deposit: Deposit, log_data: Array<felt252>,
) {
self.counter.write(self.get_counter() + 1);
}

fn after_deposit_cancellation(
ref self: ContractState, key: felt252, deposit: Deposit, log_data: LogData,
ref self: ContractState, key: felt252, deposit: Deposit, log_data: Array<felt252>,
) {
self.counter.write(self.get_counter() + 1);
}
Expand Down
8 changes: 5 additions & 3 deletions src/callback/order_callback_receiver/interface.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ trait IOrderCallbackReceiver<TContractState> {
/// * `order` - The order that was executed.
/// * `log_data` - The log data.
fn after_order_execution(
ref self: TContractState, key: felt252, order: Order, log_data: LogData
ref self: TContractState, key: felt252, order: Order, log_data: Array<felt252>
);

/// Called after an order cancellation.
Expand All @@ -22,13 +22,15 @@ trait IOrderCallbackReceiver<TContractState> {
/// * `order` - The order that was cancelled.
/// * `log_data` - The log data.
fn after_order_cancellation(
ref self: TContractState, key: felt252, order: Order, log_data: LogData
ref self: TContractState, key: felt252, order: Order, log_data: Array<felt252>
);

/// Called after an order cancellation.
/// # Arguments
/// * `key` - They key of the order.
/// * `order` - The order that was frozen.
/// * `log_data` - The log data.
fn after_order_frozen(ref self: TContractState, key: felt252, order: Order, log_data: LogData);
fn after_order_frozen(
ref self: TContractState, key: felt252, order: Order, log_data: Array<felt252>
);
}
4 changes: 2 additions & 2 deletions src/callback/withdrawal_callback_receiver/interface.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ trait IWithdrawalCallbackReceiver<TContractState> {
/// * `log_data` - The log data.
// TODO uncomment withdrawal when available
fn after_withdrawal_execution(
ref self: TContractState, key: felt252, withdrawal: Withdrawal, log_data: LogData,
ref self: TContractState, key: felt252, withdrawal: Withdrawal, log_data: Array<felt252>,
);

/// Called after an withdrawal cancellation.
Expand All @@ -23,6 +23,6 @@ trait IWithdrawalCallbackReceiver<TContractState> {
/// * `withdrawal` - The withdrawal that was cancelled.
/// * `log_data` - The log data.
fn after_withdrawal_cancellation(
ref self: TContractState, key: felt252, withdrawal: Withdrawal, log_data: LogData,
ref self: TContractState, key: felt252, withdrawal: Withdrawal, log_data: Array<felt252>,
);
}
2 changes: 1 addition & 1 deletion src/deposit/deposit_utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ fn cancel_deposit(

event_emitter.emit_deposit_cancelled(key, reason, reason_bytes.span());

let log_data: LogData = Default::default();
let mut log_data: LogData = Default::default();
after_deposit_cancellation(key, deposit, log_data);

gas_utils::pay_execution_fee_deposit(
Expand Down
10 changes: 6 additions & 4 deletions src/deposit/execute_deposit_utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ use satoru::deposit::{
deposit_vault::{IDepositVaultDispatcher, IDepositVaultDispatcherTrait}, error::DepositError
};
use satoru::event::event_emitter::{IEventEmitterDispatcher, IEventEmitterDispatcherTrait};
use satoru::event::event_utils::{LogData, set_item_uint_items, UintItems};
use satoru::event::event_utils::{
LogData, LogDataTrait, Felt252IntoU128, ContractAddressDictValue, I128252DictValue
};
use satoru::utils::serializable_dict::{SerializableFelt252Dict, SerializableFelt252DictTrait};
use satoru::fee::fee_utils;
use satoru::gas::gas_utils::pay_execution_fee_deposit;
use satoru::market::{
Expand Down Expand Up @@ -237,9 +240,8 @@ fn execute_deposit(params: ExecuteDepositParams) {
cache.received_market_tokens,
);

let event_data: LogData = Default::default();
let mut uint_items: UintItems = Default::default();
set_item_uint_items(uint_items, 0, 'received_market_tokens', cache.received_market_tokens);
let mut event_data: LogData = Default::default();
event_data.uint_dict.insert_single('received_market_tokens', cache.received_market_tokens);
after_deposit_execution(params.key, deposit, event_data);

pay_execution_fee_deposit(
Expand Down
Loading

0 comments on commit 282539e

Please sign in to comment.