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

test: BaseOrderHandler tests preparation #509

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 5 additions & 3 deletions src/tests_lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,17 @@ fn setup_oracle_and_store() -> (
let role_store = IRoleStoreDispatcher { contract_address: role_store_address };
let data_store_address = deploy_data_store(role_store_address);
let data_store = IDataStoreDispatcher { contract_address: data_store_address };
start_prank(role_store_address, caller_address);
role_store.grant_role(caller_address, role::CONTROLLER);
start_prank(data_store_address, caller_address);
let (event_emitter_address, event_emitter) = setup_event_emitter();
let oracle_store_address = deploy_oracle_store(role_store_address, event_emitter_address);
let oracle_address = deploy_oracle(
role_store_address, oracle_store_address, contract_address_const::<'pragma'>()
);
let oracle = IOracleDispatcher { contract_address: oracle_address };

start_prank(role_store_address, caller_address);
role_store.grant_role(caller_address, role::CONTROLLER);
start_prank(data_store_address, caller_address);

(caller_address, role_store, data_store, event_emitter, oracle)
}

Expand Down
259 changes: 171 additions & 88 deletions tests/exchange/test_base_order_handler.cairo
Original file line number Diff line number Diff line change
@@ -1,29 +1,168 @@
//! Test file for `src/exchange/base_order_handler.cairo`.

// *************************************************************************
// IMPORTS
// *************************************************************************
// Core lib imports.
use starknet::{
ContractAddress, get_caller_address, Felt252TryIntoContractAddress, contract_address_const
};
use snforge_std::{declare, start_prank, stop_prank, ContractClassTrait};
use snforge_std::{declare, start_prank, stop_prank, start_mock_call, ContractClassTrait};
use traits::Default;
use poseidon::poseidon_hash_span;

use satoru::event::event_emitter::{IEventEmitterDispatcher, IEventEmitterDispatcherTrait};
use satoru::exchange::withdrawal_handler::{
IWithdrawalHandlerDispatcher, IWithdrawalHandlerDispatcherTrait
};
use satoru::withdrawal::withdrawal_vault::{
IWithdrawalVaultDispatcher, IWithdrawalVaultDispatcherTrait
};
use satoru::fee::fee_handler::{IFeeHandlerDispatcher, IFeeHandlerDispatcherTrait};
// Local imports.
use satoru::role::role;
use satoru::tests_lib;

use satoru::role::role_store::{IRoleStoreDispatcher, IRoleStoreDispatcherTrait};
use satoru::data::data_store::{IDataStoreDispatcher, IDataStoreDispatcherTrait};
use satoru::data::keys::{
claim_fee_amount_key, claim_ui_fee_amount_key, claim_ui_fee_amount_for_account_key
use satoru::event::event_emitter::{IEventEmitterDispatcher, IEventEmitterDispatcherTrait};
use satoru::bank::strict_bank::{IStrictBankDispatcher, IStrictBankDispatcherTrait};
use satoru::order::order::{Order, OrderType, SecondaryOrderType, DecreasePositionSwapType};
use satoru::order::order_vault::{IOrderVaultDispatcher, IOrderVaultDispatcherTrait};
use satoru::oracle::oracle_store::{IOracleStoreDispatcher, IOracleStoreDispatcherTrait};
use satoru::oracle::oracle::{IOracleDispatcher, IOracleDispatcherTrait};
use satoru::oracle::oracle_utils::SetPricesParams;
use satoru::swap::swap_handler::{ISwapHandlerDispatcher, ISwapHandlerDispatcherTrait};
use satoru::mock::referral_storage::{IReferralStorageDispatcher, IReferralStorageDispatcherTrait};
use satoru::exchange::base_order_handler::{
IBaseOrderHandlerDispatcher, IBaseOrderHandlerDispatcherTrait
};
use satoru::oracle::oracle_utils::{SetPricesParams, SimulatePricesParams};
use satoru::role::role_store::{IRoleStoreDispatcher, IRoleStoreDispatcherTrait};
use satoru::role::role;
use satoru::withdrawal::withdrawal_utils::CreateWithdrawalParams;
use satoru::withdrawal::withdrawal::Withdrawal;
use traits::Default;
use satoru::utils::starknet_utils;
use satoru::utils::span32::{Span32, Array32Trait};

// *********************************************************************************************
// * TEST LOGIC *
// *********************************************************************************************
#[test]
#[should_panic(expected: ('already_initialized',))]
fn given_already_intialized_when_initialize_then_fails() {
let (_, role_store, data_store, base_order_handler) = setup();
let dummy_address: ContractAddress = 0x202.try_into().unwrap();
base_order_handler
.initialize(
dummy_address,
dummy_address,
dummy_address,
dummy_address,
dummy_address,
dummy_address,
dummy_address,
);
tests_lib::teardown(data_store.contract_address);
}

/// TODO: implement unit tests here when BaseOrderHandler is implemented
#[test]
#[should_panic(expected: ('NOT IMPLEMENTED YET',))]
fn given_normal_conditions_then_TODO_work() {
assert(true == false, 'NOT IMPLEMENTED YET');
}

// *********************************************************************************************
// * MOCKS *
// *********************************************************************************************
fn mock_key() -> felt252 {
poseidon_hash_span(array!['my', 'amazing', 'key'].span())
}

fn mock_set_prices_params() -> SetPricesParams {
SetPricesParams {
signer_info: 1,
tokens: array![
contract_address_const::<'ETH'>(),
contract_address_const::<'USDC'>(),
contract_address_const::<'DAI'>()
],
compacted_min_oracle_block_numbers: array![0, 0, 0],
compacted_max_oracle_block_numbers: array![6400, 6400, 6400],
compacted_oracle_timestamps: array![0, 0, 0],
compacted_decimals: array![18, 18, 18],
compacted_min_prices: array![0, 0, 0],
compacted_min_prices_indexes: array![1, 2, 3],
compacted_max_prices: array![0, 0, 0],
compacted_max_prices_indexes: array![1, 2, 3],
signatures: array![1, 2, 3],
price_feed_tokens: array![
contract_address_const::<'ETH'>(),
contract_address_const::<'USDC'>(),
contract_address_const::<'DAI'>()
]
}
}

fn mock_swap_path() -> Span32<ContractAddress> {
array![contract_address_const::<'swap_path_0'>(), contract_address_const::<'swap_path_1'>()]
.span32()
}

fn mock_order(key: felt252) -> Order {
let swap_path: Span32<ContractAddress> = mock_swap_path();
Order {
key,
order_type: OrderType::StopLossDecrease,
decrease_position_swap_type: DecreasePositionSwapType::SwapPnlTokenToCollateralToken(()),
account: contract_address_const::<'account'>(),
receiver: contract_address_const::<'receiver'>(),
callback_contract: contract_address_const::<'callback_contract'>(),
ui_fee_receiver: contract_address_const::<'ui_fee_receiver'>(),
market: contract_address_const::<'market'>(),
initial_collateral_token: contract_address_const::<'initial_collateral_token'>(),
swap_path,
size_delta_usd: 1000,
initial_collateral_delta_amount: 500,
trigger_price: 2000,
acceptable_price: 2500,
execution_fee: 100,
callback_gas_limit: 300000,
min_output_amount: 100,
updated_at_block: 0,
is_long: true,
is_frozen: false,
}
}

// *********************************************************************************************
// * SETUP *
// *********************************************************************************************
/// Utility function to setup the test environment.
///
/// # Returns
///
/// * `ContractAddress` - The address of the caller.
/// * `IRoleStoreDispatcher` - The role store dispatcher.
/// * `IDataStoreDispatcher` - The data store dispatcher.
/// * `IBaseOrderHandlerDispatcher` - The base order handler dispatcher.
fn setup() -> (
ContractAddress, IRoleStoreDispatcher, IDataStoreDispatcher, IBaseOrderHandlerDispatcher
) {
let (caller_address, role_store, data_store, event_emitter, oracle) =
tests_lib::setup_oracle_and_store();

// TODO test when all functions called within get_execute_order_params are implemented
let order_vault_address = deploy_order_vault(
data_store.contract_address, role_store.contract_address
);
let swap_handler_address = deploy_swap_handler(role_store.contract_address);
let referral_storage_address = deploy_referral_storage(event_emitter.contract_address);

let base_order_handler_address = deploy_base_order_handler(
data_store.contract_address,
role_store.contract_address,
event_emitter.contract_address,
order_vault_address,
oracle.contract_address,
swap_handler_address,
referral_storage_address
);
let base_order_handler = IBaseOrderHandlerDispatcher {
contract_address: base_order_handler_address
};

(caller_address, role_store, data_store, base_order_handler)
}

/// Utility function to deploy a `BaseOrderhandler` contract and return its address.
fn deploy_base_order_handler(
data_store_address: ContractAddress,
role_store_address: ContractAddress,
Expand All @@ -46,81 +185,25 @@ fn deploy_base_order_handler(
contract.deploy(@constructor_calldata).unwrap()
}

fn deploy_oracle(
oracle_store_address: ContractAddress, role_store_address: ContractAddress
) -> ContractAddress {
let contract = declare('Oracle');
let constructor_calldata = array![role_store_address.into(), oracle_store_address.into()];
contract.deploy(@constructor_calldata).unwrap()
}

fn deploy_oracle_store(
role_store_address: ContractAddress, event_emitter_address: ContractAddress
/// Utility function to deploy an `OrderVault` contract and return its address.
fn deploy_order_vault(
data_store_address: ContractAddress, role_store_address: ContractAddress,
) -> ContractAddress {
let contract = declare('OracleStore');
let constructor_calldata = array![role_store_address.into(), event_emitter_address.into()];
contract.deploy(@constructor_calldata).unwrap()
}

fn deploy_order_vault(order_vault_address: ContractAddress) -> ContractAddress {
let contract = declare('OrderVault');
let constructor_calldata = array![order_vault_address.into()];
let mut constructor_calldata = array![];
constructor_calldata.append(data_store_address.into());
constructor_calldata.append(role_store_address.into());
contract.deploy(@constructor_calldata).unwrap()
}

fn deploy_strict_bank(
data_store_address: ContractAddress, role_store_address: ContractAddress
) -> ContractAddress {
let contract = declare('StrictBank');
let constructor_calldata = array![data_store_address.into(), role_store_address.into()];
contract.deploy(@constructor_calldata).unwrap()
}

fn deploy_data_store(role_store_address: ContractAddress) -> ContractAddress {
let contract = declare('DataStore');
let constructor_calldata = array![role_store_address.into()];
contract.deploy(@constructor_calldata).unwrap()
/// Utility function to deploy a `SwapHandler` contract and return its address.
fn deploy_swap_handler(role_store_address: ContractAddress) -> ContractAddress {
let contract = declare('SwapHandler');
contract.deploy(@array![role_store_address.into()]).unwrap()
}

fn deploy_role_store() -> ContractAddress {
let contract = declare('RoleStore');
contract.deploy(@array![]).unwrap()
}

fn deploy_event_emitter() -> ContractAddress {
let contract = declare('EventEmitter');
contract.deploy(@array![]).unwrap()
}

fn setup() -> (
ContractAddress, IDataStoreDispatcher, IEventEmitterDispatcher, IWithdrawalHandlerDispatcher
) {
let caller_address: ContractAddress = 0x101.try_into().unwrap();
let order_keeper: ContractAddress = 0x2233.try_into().unwrap();
let role_store_address = deploy_role_store();
let role_store = IRoleStoreDispatcher { contract_address: role_store_address };
let data_store_address = deploy_data_store(role_store_address);
let data_store = IDataStoreDispatcher { contract_address: data_store_address };
let event_emitter_address = deploy_event_emitter();
let event_emitter = IEventEmitterDispatcher { contract_address: event_emitter_address };
let strict_bank_address = deploy_strict_bank(data_store_address, role_store_address);
let order_vault_address = deploy_order_vault(strict_bank_address);
let oracle_store_address = deploy_oracle_store(role_store_address, event_emitter_address);
let oracle_address = deploy_oracle(oracle_store_address, role_store_address);
let withdrawal_handler_address = deploy_withdrawal_handler(
data_store_address,
role_store_address,
event_emitter_address,
withdrawal_vault_address,
oracle_address
);

let withdrawal_handler = IWithdrawalHandlerDispatcher {
contract_address: withdrawal_handler_address
};
start_prank(role_store_address, caller_address);
role_store.grant_role(caller_address, role::CONTROLLER);
role_store.grant_role(order_keeper, role::ORDER_KEEPER);
start_prank(data_store_address, caller_address);
(caller_address, data_store, event_emitter, withdrawal_handler)
/// Utility function to deploy a `ReferralStorage` contract and return its address.
fn deploy_referral_storage(event_emitter_address: ContractAddress) -> ContractAddress {
let contract = declare('ReferralStorage');
contract.deploy(@array![event_emitter_address.into()]).unwrap()
}
1 change: 1 addition & 0 deletions tests/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ mod exchange {
mod test_liquidation_handler;
mod test_withdrawal_handler;
mod test_deposit_handler;
mod test_base_order_handler;
}
mod feature {
mod test_feature_utils;
Expand Down
Loading