Skip to content

Commit

Permalink
feat(log_data_indexes): Typo + Don't serialize too early
Browse files Browse the repository at this point in the history
  • Loading branch information
akhercha committed Oct 31, 2023
1 parent 6ba0e9a commit a9bb259
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 39 deletions.
48 changes: 22 additions & 26 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 @@ -91,103 +91,99 @@ fn get_saved_callback_contract(
/// # Arguments
/// * `key` - They key of the deposit.
/// * `deposit` - The deposit that was executed.
/// * `serialized_log_data` - The log data.
fn after_deposit_execution(key: felt252, deposit: Deposit, serialized_log_data: Array<felt252>) {
/// * `log_data.serialize_into()` - The log data.
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, serialized_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.
/// * `serialized_log_data` - The log data.
fn after_deposit_cancellation(key: felt252, deposit: Deposit, serialized_log_data: Array<felt252>) {
/// * `log_data.serialize_into()` - The log data.
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, serialized_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.
/// * `serialized_log_data` - The log data.
fn after_withdrawal_execution(
key: felt252, withdrawal: Withdrawal, serialized_log_data: Array<felt252>
) {
/// * `log_data.serialize_into()` - The log data.
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, serialized_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.
/// * `serialized_log_data` - The log data.
fn after_withdrawal_cancellation(
key: felt252, withdrawal: Withdrawal, serialized_log_data: Array<felt252>
) {
/// * `log_data.serialize_into()` - The log data.
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, serialized_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.
/// * `serialized_log_data` - The log data.
fn after_order_execution(key: felt252, order: Order, serialized_log_data: Array<felt252>) {
/// * `log_data.serialize_into()` - The log data.
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, serialized_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.
/// * `serialized_log_data` - The log data.
fn after_order_cancellation(key: felt252, order: Order, serialized_log_data: Array<felt252>) {
/// * `log_data.serialize_into()` - The log data.
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, serialized_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.
/// * `serialized_log_data` - The log data.
fn after_order_frozen(key: felt252, order: Order, serialized_log_data: Array<felt252>) {
/// * `log_data.serialize_into()` - The log data.
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, serialized_log_data)
dispatcher.after_order_frozen(key, order, log_data.serialize_into())
}

/// Validates that the given address is a contract.
Expand Down
3 changes: 2 additions & 1 deletion src/deposit/deposit_utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ fn cancel_deposit(

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

after_deposit_cancellation(key, deposit, array![]);
let mut log_data: LogData = Default::default();
after_deposit_cancellation(key, deposit, log_data);

gas_utils::pay_execution_fee_deposit(
data_store,
Expand Down
2 changes: 1 addition & 1 deletion src/deposit/execute_deposit_utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ fn execute_deposit(params: ExecuteDepositParams) {

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.serialize_into());
after_deposit_execution(params.key, deposit, event_data);

pay_execution_fee_deposit(
params.data_store,
Expand Down
15 changes: 8 additions & 7 deletions src/event/event_utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,14 @@ const END_OF_DICT: felt252 = '______';
impl LogDataImpl of LogDataTrait {
/// Serializes all the sub-dicts of LogData & append all of them into a new felt252 array
fn serialize(ref self: LogData, ref output: Array<felt252>) {
let mut serialized_dicts: Array<Array<felt252>> = array![];
serialized_dicts.append(self.address_dict.serialize_into());
serialized_dicts.append(self.uint_dict.serialize_into());
serialized_dicts.append(self.int_dict.serialize_into());
serialized_dicts.append(self.bool_dict.serialize_into());
serialized_dicts.append(self.felt252_dict.serialize_into());
serialized_dicts.append(self.string_dict.serialize_into());
let mut serialized_dicts: Array<Array<felt252>> = array![
self.address_dict.serialize_into(),
self.uint_dict.serialize_into(),
self.int_dict.serialize_into(),
self.bool_dict.serialize_into(),
self.felt252_dict.serialize_into(),
self.string_dict.serialize_into()
];
let mut span_arrays = serialized_dicts.span();
loop {
match span_arrays.pop_front() {
Expand Down
6 changes: 3 additions & 3 deletions src/order/order_utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ fn execute_order(params: ExecuteOrderParams) {

params.contracts.event_emitter.emit_order_executed(params.key, params.secondary_order_type);

callback_utils::after_order_execution(params.key, params.order, event_data.serialize_into());
callback_utils::after_order_execution(params.key, params.order, event_data);

// the order.executionFee for liquidation / adl orders is zero
// gas costs for liquidations / adl is subsidised by the treasury
Expand Down Expand Up @@ -271,7 +271,7 @@ fn cancel_order(
event_emitter.emit_order_cancelled(key, reason, reason_bytes.span());

let mut event_data: LogData = Default::default();
callback_utils::after_order_cancellation(key, order, event_data.serialize_into());
callback_utils::after_order_cancellation(key, order, event_data);

gas_utils::pay_execution_fee_order(
data_store,
Expand Down Expand Up @@ -324,7 +324,7 @@ fn freeze_order(
event_emitter.emit_order_frozen(key, reason, reason_bytes.span());

let mut event_data: LogData = Default::default();
callback_utils::after_order_frozen(key, order, event_data.serialize_into());
callback_utils::after_order_frozen(key, order, event_data);

gas_utils::pay_execution_fee_order(
data_store, event_emitter, order_vault, execution_fee, starting_gas, keeper, order.account
Expand Down
2 changes: 1 addition & 1 deletion tests/callback/test_callback_utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,6 @@ fn given_normal_conditions_when_callback_contract_functions_then_works() {
deposit.callback_contract = callback_mock.contract_address;

assert(callback_mock.get_counter() == 1, 'should be 1');
after_deposit_execution(42, deposit, log_data.serialize_into());
after_deposit_execution(42, deposit, log_data);
assert(callback_mock.get_counter() == 2, 'should be 2');
}

0 comments on commit a9bb259

Please sign in to comment.