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

Improve tests of liquidation_handler contract #589

Merged
15 changes: 15 additions & 0 deletions src/exchange/base_order_handler.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ trait IBaseOrderHandler<TContractState> {
swap_handler_address: ContractAddress,
referral_storage_address: ContractAddress
);

fn only_liquidation_keeper(self: @TContractState);
zarboq marked this conversation as resolved.
Show resolved Hide resolved
}

#[starknet::contract]
Expand All @@ -54,6 +56,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 +160,10 @@ mod BaseOrderHandler {
);
self.data_store.write(IDataStoreDispatcher { contract_address: data_store_address });
self.role_store.write(IRoleStoreDispatcher { contract_address: role_store_address });

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

self
.event_emitter
.write(IEventEmitterDispatcher { contract_address: event_emitter_address });
Expand All @@ -166,6 +176,11 @@ mod BaseOrderHandler {
.referral_storage
.write(IReferralStorageDispatcher { contract_address: referral_storage_address });
}

fn only_liquidation_keeper(self: @ContractState) {
let mut state: RoleModule::ContractState = RoleModule::unsafe_new_contract_state();
IRoleModule::only_liquidation_keeper(@state);
}
}

// *************************************************************************
Expand Down
25 changes: 23 additions & 2 deletions src/exchange/liquidation_handler.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,18 @@ mod LiquidationHandler {

// Core lib imports.
use satoru::exchange::base_order_handler::BaseOrderHandler::{
tevrat-aksoy marked this conversation as resolved.
Show resolved Hide resolved
event_emitter::InternalContractMemberStateTrait, data_store::InternalContractMemberStateImpl
event_emitter::InternalContractMemberStateTrait,
data_store::InternalContractMemberStateImpl,
oracle::InternalContractMemberStateTrait as OracleStateTrait,
};
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::{IRoleModuleDispatcher, IRoleModuleDispatcherTrait};

use satoru::data::{
data_store::{IDataStoreSafeDispatcher, IDataStoreSafeDispatcherTrait, DataStore},
keys::execute_order_feature_disabled_key
Expand All @@ -71,6 +75,8 @@ mod LiquidationHandler {
use satoru::feature::feature_utils::validate_feature;
use satoru::exchange::order_handler::{IOrderHandler, OrderHandler};
use satoru::utils::starknet_utils;
use satoru::utils::global_reentrancy_guard;


// *************************************************************************
// STORAGE
Expand Down Expand Up @@ -131,9 +137,21 @@ 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());

state_base.only_liquidation_keeper();

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 +175,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 @@ -147,7 +147,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
Loading