Skip to content

Commit

Permalink
Improve tests of liquidation_handler contract (keep-starknet-strange#589
Browse files Browse the repository at this point in the history
)

* add liq_handler missing modifiers

* add mock signers

* add new tests

* fix imports
  • Loading branch information
tevrat-aksoy authored Nov 23, 2023
1 parent 8c0b778 commit 59b2735
Show file tree
Hide file tree
Showing 6 changed files with 637 additions and 152 deletions.
5 changes: 5 additions & 0 deletions src/exchange/base_order_handler.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ mod BaseOrderHandler {
// Local imports.
use super::IBaseOrderHandler;
use satoru::role::role_store::{IRoleStoreDispatcher, IRoleStoreDispatcherTrait};
use satoru::role::role_module::{
IRoleModuleDispatcher, IRoleModuleDispatcherTrait, RoleModule, IRoleModule
};

use satoru::data::data_store::{IDataStoreDispatcher, IDataStoreDispatcherTrait};
use satoru::event::event_emitter::{IEventEmitterDispatcher, IEventEmitterDispatcherTrait};
use satoru::oracle::{
Expand Down Expand Up @@ -154,6 +158,7 @@ mod BaseOrderHandler {
);
self.data_store.write(IDataStoreDispatcher { contract_address: data_store_address });
self.role_store.write(IRoleStoreDispatcher { contract_address: role_store_address });

self
.event_emitter
.write(IEventEmitterDispatcher { contract_address: event_emitter_address });
Expand Down
43 changes: 35 additions & 8 deletions src/exchange/liquidation_handler.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ mod LiquidationHandler {
// *************************************************************************

// Core lib imports.
use satoru::exchange::base_order_handler::BaseOrderHandler::{
event_emitter::InternalContractMemberStateTrait, data_store::InternalContractMemberStateImpl
};

use starknet::{ContractAddress, get_caller_address, get_contract_address};


// Local imports.
use super::ILiquidationHandler;
use satoru::role::role_store::{IRoleStoreSafeDispatcher, IRoleStoreSafeDispatcherTrait};
use satoru::role::role_module::{RoleModule, IRoleModule};

use satoru::data::{
data_store::{IDataStoreSafeDispatcher, IDataStoreSafeDispatcherTrait, DataStore},
keys::execute_order_feature_disabled_key
Expand All @@ -65,12 +65,20 @@ mod LiquidationHandler {
};
use satoru::swap::swap_handler::{ISwapHandlerDispatcher, ISwapHandlerDispatcherTrait};
use satoru::market::market::Market;
use satoru::exchange::base_order_handler::{IBaseOrderHandler, BaseOrderHandler};
use satoru::exchange::{
order_handler::{IOrderHandler, OrderHandler},
base_order_handler::{IBaseOrderHandler, BaseOrderHandler}
};


use satoru::liquidation::liquidation_utils::create_liquidation_order;
use satoru::exchange::order_handler;
use satoru::feature::feature_utils::validate_feature;
use satoru::exchange::order_handler::{IOrderHandler, OrderHandler};
use satoru::utils::starknet_utils;
use satoru::utils::{starknet_utils, global_reentrancy_guard};
use satoru::exchange::base_order_handler::BaseOrderHandler::{
event_emitter::InternalContractMemberStateTrait,
data_store::InternalContractMemberStateImpl,
oracle::InternalContractMemberStateTrait as OracleStateTrait,
};

// *************************************************************************
// STORAGE
Expand Down Expand Up @@ -113,6 +121,9 @@ mod LiquidationHandler {
swap_handler_address,
referral_storage_address
);

let mut state: RoleModule::ContractState = RoleModule::unsafe_new_contract_state();
IRoleModule::initialize(ref state, role_store_address,);
}


Expand All @@ -131,9 +142,22 @@ mod LiquidationHandler {
is_long: bool,
oracle_params: SetPricesParams
) {
let starting_gas: u128 = starknet_utils::sn_gasleft(array![100]);
let mut state_base: BaseOrderHandler::ContractState =
BaseOrderHandler::unsafe_new_contract_state(); //retrieve BaseOrderHandler state
global_reentrancy_guard::non_reentrant_before(state_base.data_store.read());

let mut role_state: RoleModule::ContractState = RoleModule::unsafe_new_contract_state();
IRoleModule::only_liquidation_keeper(@role_state);

with_oracle_prices_before(
state_base.oracle.read(),
state_base.data_store.read(),
state_base.event_emitter.read(),
@oracle_params
);

let starting_gas: u128 = starknet_utils::sn_gasleft(array![100]);

let key: felt252 = create_liquidation_order(
state_base.data_store.read(),
state_base.event_emitter.read(),
Expand All @@ -157,6 +181,9 @@ mod LiquidationHandler {
execute_order_feature_disabled_key(get_contract_address(), params.order.order_type)
);
order_utils::execute_order(params);
with_oracle_prices_after(state_base.oracle.read());

global_reentrancy_guard::non_reentrant_after(state_base.data_store.read());
}
}
}
2 changes: 1 addition & 1 deletion src/gas/gas_utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ fn pay_execution_fee_order(

// 63/64 gas is forwarded to external calls, reduce the startingGas to account for this
let reduced_starting_gas = starting_gas - sn_gasleft(array![100]) / 63;
let gas_used = reduced_starting_gas - sn_gasleft(array![100]);
let gas_used = reduced_starting_gas - sn_gasleft(array![0]);

// each external call forwards 63/64 of the remaining gas
let mut execution_fee_for_keeper = adjust_gas_usage(data_store, gas_used)
Expand Down
1 change: 1 addition & 0 deletions src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ mod mock {
mod error;
mod governable;
mod referral_storage;
mod mock_account;
}

// `oracle` contains functions related to oracles used by Satoru.
Expand Down
85 changes: 85 additions & 0 deletions src/mock/mock_account.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
//! Mock Account for testing.

#[starknet::contract]
mod MockAccount {
// *************************************************************************
// IMPORTS
// *************************************************************************

// Core lib imports.
use core::zeroable::Zeroable;
use starknet::{get_caller_address, ContractAddress};
use result::ResultTrait;

// Local imports.
use satoru::oracle::{
interfaces::account::{IAccount, IAccountDispatcher, IAccountDispatcherTrait}
};


// *************************************************************************
// STORAGE
// *************************************************************************
#[storage]
struct Storage {
owner: felt252,
}

// *************************************************************************
// EXTERNAL FUNCTIONS
// *************************************************************************
#[external(v0)]
impl MockAccount of IAccount<ContractState> {
fn __validate_declare__(self: @ContractState, class_hash: felt252) -> felt252 {
1
}
fn __validate_deploy__(
self: @ContractState,
class_hash: felt252,
contract_address_salt: felt252,
owner: felt252,
guardian: felt252
) -> felt252 {
1
}

fn change_owner(
ref self: ContractState, new_owner: felt252, signature_r: felt252, signature_s: felt252
) {
self.owner.write(new_owner);
}
fn change_guardian(ref self: ContractState, new_guardian: felt252) {}


fn change_guardian_backup(ref self: ContractState, new_guardian_backup: felt252) {}


fn trigger_escape_owner(ref self: ContractState, new_owner: felt252) {}

fn trigger_escape_guardian(ref self: ContractState, new_guardian: felt252) {}

fn escape_owner(ref self: ContractState) {}

fn escape_guardian(ref self: ContractState) {}

fn cancel_escape(ref self: ContractState) {}
fn get_owner(self: @ContractState) -> felt252 {
self.owner.read()
}
fn get_guardian(self: @ContractState) -> felt252 {
1
}
fn get_guardian_backup(self: @ContractState) -> felt252 {
1
}
fn get_name(self: @ContractState) -> felt252 {
1
}
fn get_guardian_escape_attempts(self: @ContractState) -> u32 {
1
}
fn get_owner_escape_attempts(self: @ContractState) -> u32 {
1
}
}
}
Loading

0 comments on commit 59b2735

Please sign in to comment.