Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Use indexes for LogData #539

Merged
merged 44 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from 43 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
d39fff6
feat(log_data_indexes): Added SerializableFelt252Dict
akhercha Oct 18, 2023
9130137
feat(log_data_indexes): Added docstring for SerializableFelt252Dict
akhercha Oct 18, 2023
9e462e1
feat(log_data_indexes): Draft
akhercha Oct 18, 2023
9b1597d
feat(log_data_indexes): Added TODO
akhercha Oct 18, 2023
2764875
feat(log_data_indexes): get->Option<T> instead of get->T
akhercha Oct 18, 2023
a393b8b
feat(log_data_indexes): Test almost working
akhercha Oct 18, 2023
3754f74
feat(log_data_indexes): Serializing OK but still errs
akhercha Oct 18, 2023
20e10cf
feat(log_data_indexes): Revert event_utils & trying different things …
akhercha Oct 18, 2023
bd37409
feat(log_data_indexes): OrderedDict tries
akhercha Oct 24, 2023
63595ef
feat(log_data_indexes): Compiles
akhercha Oct 24, 2023
199d9c9
feat(log_data_indexes): Added i128
akhercha Oct 24, 2023
7c5d19c
feat(log_data_indexes): Added felt252
akhercha Oct 24, 2023
936ebdd
feat(log_data_indexes): Added TODOs
akhercha Oct 24, 2023
b09d65b
feat(log_data_indexes): Renamed structs + address_items impl
akhercha Oct 25, 2023
9999822
feat(log_data_indexes): Generic impl
akhercha Oct 25, 2023
61185c2
feat(log_data_indexes): fmt
akhercha Oct 25, 2023
cd89257
feat(log_data_indexes): Trigger CI to check semgrep
akhercha Oct 25, 2023
43ea52d
Merge branch 'main' into feat/log_data_indexes
akhercha Oct 25, 2023
728c5dc
feat(log_data_indexes): Renaming + Started unit tests
akhercha Oct 25, 2023
83d5b29
Merge branch 'feat/log_data_indexes' of github.com:akhercha/satoru in…
akhercha Oct 25, 2023
38d5c81
feat(log_data_indexes): Merge
akhercha Oct 25, 2023
15daea3
feat(log_data_indexes): Update SerializableDict with custom i128
akhercha Oct 25, 2023
5e72d6c
feat(log_data_indexes): Checkpoint; units break
akhercha Oct 25, 2023
a81153c
feat(log_data_indexes): Replaced SerializableDict's Array by Span
akhercha Oct 27, 2023
4af231e
feat(log_data_indexes): Utilities fn
akhercha Oct 27, 2023
74850d1
feat(log_data_indexes): Adding unit tests for debug
akhercha Oct 28, 2023
b3b71cd
feat(log_data_indexes): TODO: investigate serialization fail
akhercha Oct 28, 2023
ffd003d
feat(log_data_indexes): update
akhercha Oct 28, 2023
1c86f27
feat(log_data_indexes): Using a custom (de)serialization
akhercha Oct 30, 2023
4663984
feat(log_data_indexes): TODO: implement custom_(de)serialize for LogData
akhercha Oct 30, 2023
8beccb5
feat(log_data_indexes): Added serialization for LogData
akhercha Oct 30, 2023
f5984d3
feat(log_data_indexes): Dict working
akhercha Oct 30, 2023
bd9aa00
feat(log_data_indexes): Removed sandbox file
akhercha Oct 30, 2023
bf951d5
feat(log_data_indexes): TODO: deserialize + tests
akhercha Oct 30, 2023
365cf9c
feat(log_data_indexes): Implement deserialize; need tests
akhercha Oct 30, 2023
1601e2c
feat(log_data_indexes): Added tests for log_data serialization
akhercha Oct 30, 2023
6ba0e9a
Merge branch 'main' into feat/log_data_indexes
Sk8erboi84 Oct 31, 2023
a9bb259
feat(log_data_indexes): Typo + Don't serialize too early
akhercha Oct 31, 2023
cf4119a
feat(log_data_indexes): Docstrings
akhercha Oct 31, 2023
5ebed4f
feat(log_data_indexes): Updated failed docstring
akhercha Oct 31, 2023
fab23c7
feat(log_data_indexes): Refacto + Added serializable_dict to Satoru's…
akhercha Nov 1, 2023
3458b48
feat(log_data_indexes): Removed Copy Trait for SerializableDict
akhercha Nov 1, 2023
ea32b23
feat(log_data_indexes): Added .cairo ext
akhercha Nov 1, 2023
ca4c126
Merge branch 'main' into feat/log_data_indexes
Sk8erboi84 Nov 1, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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