diff --git a/src/exchange/liquidation_handler.cairo b/src/exchange/liquidation_handler.cairo index 1fa65563..ae9106ae 100644 --- a/src/exchange/liquidation_handler.cairo +++ b/src/exchange/liquidation_handler.cairo @@ -6,7 +6,7 @@ // Core lib imports. use core::traits::Into; -use starknet::ContractAddress; +use starknet::{ContractAddress, ClassHash}; // Local imports. use satoru::oracle::oracle_utils::SetPricesParams; @@ -43,6 +43,7 @@ mod LiquidationHandler { use starknet::{ContractAddress, get_caller_address, get_contract_address, ClassHash}; + use debug::PrintTrait; // Local imports. use super::ILiquidationHandler; @@ -77,9 +78,10 @@ mod LiquidationHandler { use satoru::exchange::base_order_handler::BaseOrderHandler::{ event_emitter::InternalContractMemberStateTrait, data_store::InternalContractMemberStateImpl, - order_utils::InternalContractMemberStateTrait as OrderUtilsTrait, + order_utils_lib::InternalContractMemberStateTrait as OrderUtilsTrait, oracle::InternalContractMemberStateTrait as OracleStateTrait, }; + use satoru::order::order_utils::IOrderUtilsDispatcherTrait; // ************************************************************************* // STORAGE @@ -126,9 +128,9 @@ mod LiquidationHandler { swap_handler_address, referral_storage_address, order_utils_class_hash, - increase_order_utils_class_hash: ClassHash, - decrease_order_utils_class_hash: ClassHash, - swap_order_utils_class_hash: ClassHash, + increase_order_utils_class_hash, + decrease_order_utils_class_hash, + swap_order_utils_class_hash, ); let mut state: RoleModule::ContractState = RoleModule::unsafe_new_contract_state(); IRoleModule::initialize(ref state, role_store_address,); @@ -150,7 +152,7 @@ mod LiquidationHandler { is_long: bool, oracle_params: SetPricesParams ) { - let mut state_base: BaseOrderHandler::ContractState = + let mut state_base = BaseOrderHandler::unsafe_new_contract_state(); //retrieve BaseOrderHandler state global_reentrancy_guard::non_reentrant_before(state_base.data_store.read()); @@ -189,7 +191,8 @@ mod LiquidationHandler { params.contracts.data_store, execute_order_feature_disabled_key(get_contract_address(), params.order.order_type) ); - state_base.execute_order_utils(params); + 'pass everything'.print(); + state_base.order_utils_lib.read().execute_order_utils(params); // with_oracle_prices_after(state_base.oracle.read()); global_reentrancy_guard::non_reentrant_after(state_base.data_store.read()); diff --git a/src/lib.cairo b/src/lib.cairo index 23f43a48..019715f3 100644 --- a/src/lib.cairo +++ b/src/lib.cairo @@ -70,7 +70,7 @@ mod exchange { mod deposit_handler; mod error; mod exchange_utils; - // mod liquidation_handler; + mod liquidation_handler; mod order_handler; mod withdrawal_handler; } diff --git a/src/position/decrease_position_utils.cairo b/src/position/decrease_position_utils.cairo index 98f38e0a..f54a495f 100644 --- a/src/position/decrease_position_utils.cairo +++ b/src/position/decrease_position_utils.cairo @@ -60,8 +60,6 @@ fn decrease_position(mut params: UpdatePositionParams) -> DecreasePositionResult market_utils::get_cached_token_price( params.order.initial_collateral_token, params.market, cache.prices ); - params.position.size_in_usd = params.position.size_in_tokens - * cache.collateral_token_price.min; //TODO remove // cap the order size to the position size if (params.order.size_delta_usd > params.position.size_in_usd) { diff --git a/tests/integration/test_long_integration.cairo b/tests/integration/test_long_integration.cairo index b3152128..a1ea928c 100644 --- a/tests/integration/test_long_integration.cairo +++ b/tests/integration/test_long_integration.cairo @@ -52,6 +52,9 @@ use satoru::price::price::{Price, PriceTrait}; use satoru::position::position_utils; use satoru::withdrawal::withdrawal_utils; +use satoru::exchange::liquidation_handler::{ + ILiquidationHandlerDispatcher, ILiquidationHandlerDispatcherTrait +}; use satoru::order::order::{Order, OrderType, SecondaryOrderType, DecreasePositionSwapType}; use satoru::order::order_vault::{IOrderVaultDispatcher, IOrderVaultDispatcherTrait}; use satoru::order::base_order_utils::{CreateOrderParams}; @@ -469,2150 +472,2150 @@ const INITIAL_TOKENS_MINTED: felt252 = 1000; // teardown(data_store, market_factory); // } -#[test] -fn test_long_demo_market_integration() { - // ********************************************************************************************* - // * SETUP * - // ********************************************************************************************* - let ( - caller_address, - market_factory_address, - role_store_address, - data_store_address, - market_token_class_hash, - market_factory, - role_store, - data_store, - event_emitter, - exchange_router, - deposit_handler, - deposit_vault, - oracle, - order_handler, - order_vault, - reader, - referal_storage, - withdrawal_handler, - withdrawal_vault, - ) = - setup(); - - // ********************************************************************************************* - // * TEST LOGIC * - // ********************************************************************************************* +// #[test] +// fn test_long_demo_market_integration() { +// // ********************************************************************************************* +// // * SETUP * +// // ********************************************************************************************* +// let ( +// caller_address, +// market_factory_address, +// role_store_address, +// data_store_address, +// market_token_class_hash, +// market_factory, +// role_store, +// data_store, +// event_emitter, +// exchange_router, +// deposit_handler, +// deposit_vault, +// oracle, +// order_handler, +// order_vault, +// reader, +// referal_storage, +// withdrawal_handler, +// withdrawal_vault, +// ) = +// setup(); - // Create a market. - let market = data_store.get_market(create_market(market_factory)); +// // ********************************************************************************************* +// // * TEST LOGIC * +// // ********************************************************************************************* - // Set params in data_store - data_store.set_address(keys::fee_token(), market.index_token); - data_store.set_u256(keys::max_swap_path_length(), 5); +// // Create a market. +// let market = data_store.get_market(create_market(market_factory)); - // Set max pool amount. - data_store - .set_u256( - keys::max_pool_amount_key(market.market_token, market.long_token), - 5000000000000000000000000000000000000000000 //500 000 ETH - ); - data_store - .set_u256( - keys::max_pool_amount_key(market.market_token, market.short_token), - 2500000000000000000000000000000000000000000000 //250 000 000 USDC - ); +// // Set params in data_store +// data_store.set_address(keys::fee_token(), market.index_token); +// data_store.set_u256(keys::max_swap_path_length(), 5); - let factor_for_deposits: felt252 = keys::max_pnl_factor_for_deposits(); - data_store - .set_u256( - keys::max_pnl_factor_key(factor_for_deposits, market.market_token, true), - 50000000000000000000000000000000000000000000000 - ); - let factor_for_withdrawal: felt252 = keys::max_pnl_factor_for_withdrawals(); - data_store - .set_u256( - keys::max_pnl_factor_key(factor_for_withdrawal, market.market_token, true), - 50000000000000000000000000000000000000000000000 - ); +// // Set max pool amount. +// data_store +// .set_u256( +// keys::max_pool_amount_key(market.market_token, market.long_token), +// 5000000000000000000000000000000000000000000 //500 000 ETH +// ); +// data_store +// .set_u256( +// keys::max_pool_amount_key(market.market_token, market.short_token), +// 2500000000000000000000000000000000000000000000 //250 000 000 USDC +// ); - oracle.set_primary_prices(market.long_token, 5000); - oracle.set_primary_prices(market.short_token, 1); +// let factor_for_deposits: felt252 = keys::max_pnl_factor_for_deposits(); +// data_store +// .set_u256( +// keys::max_pnl_factor_key(factor_for_deposits, market.market_token, true), +// 50000000000000000000000000000000000000000000000 +// ); +// let factor_for_withdrawal: felt252 = keys::max_pnl_factor_for_withdrawals(); +// data_store +// .set_u256( +// keys::max_pnl_factor_key(factor_for_withdrawal, market.market_token, true), +// 50000000000000000000000000000000000000000000000 +// ); - 'fill the pool'.print(); - // Fill the pool. - IERC20Dispatcher { contract_address: market.long_token } - .mint(market.market_token, 50000000000000000000000000000000000000); // 5 ETH - IERC20Dispatcher { contract_address: market.short_token } - .mint(market.market_token, 25000000000000000000000000000000000000000); // 25000 USDC - 'filled pool 1'.print(); +// oracle.set_primary_prices(market.long_token, 5000); +// oracle.set_primary_prices(market.short_token, 1); - IERC20Dispatcher { contract_address: market.long_token } - .mint(caller_address, 9999999999999000000); // 9.999 ETH - IERC20Dispatcher { contract_address: market.short_token } - .mint(caller_address, 49999999999999999000000); // 49.999 UDC - 'filled account'.print(); +// 'fill the pool'.print(); +// // Fill the pool. +// IERC20Dispatcher { contract_address: market.long_token } +// .mint(market.market_token, 50000000000000000000000000000000000000); // 5 ETH +// IERC20Dispatcher { contract_address: market.short_token } +// .mint(market.market_token, 25000000000000000000000000000000000000000); // 25000 USDC +// 'filled pool 1'.print(); - // INITIAL LONG TOKEN IN POOL : 5 ETH - // INITIAL SHORT TOKEN IN POOL : 25000 USDC +// IERC20Dispatcher { contract_address: market.long_token } +// .mint(caller_address, 9999999999999000000); // 9.999 ETH +// IERC20Dispatcher { contract_address: market.short_token } +// .mint(caller_address, 49999999999999999000000); // 49.999 UDC +// 'filled account'.print(); - // TODO Check why we don't need to set pool_amount_key - // // Set pool amount in data_store. - // let mut key = keys::pool_amount_key(market.market_token, contract_address_const::<'ETH'>()); +// // INITIAL LONG TOKEN IN POOL : 5 ETH +// // INITIAL SHORT TOKEN IN POOL : 25000 USDC - let balance_deposit_vault_before = IERC20Dispatcher { contract_address: market.short_token } - .balance_of(deposit_vault.contract_address); - let balance_caller_ETH = IERC20Dispatcher { contract_address: market.long_token } - .balance_of(caller_address); - let balance_caller_USDC = IERC20Dispatcher { contract_address: market.short_token } - .balance_of(caller_address); +// // TODO Check why we don't need to set pool_amount_key +// // // Set pool amount in data_store. +// // let mut key = keys::pool_amount_key(market.market_token, contract_address_const::<'ETH'>()); - assert(balance_deposit_vault_before == 0, 'balance deposit should be 0'); - assert(balance_caller_ETH == 10000000000000000000, 'balanc ETH should be 10 ETH'); - assert(balance_caller_USDC == 50000000000000000000000, 'USDC be 50 000 USDC'); +// let balance_deposit_vault_before = IERC20Dispatcher { contract_address: market.short_token } +// .balance_of(deposit_vault.contract_address); +// let balance_caller_ETH = IERC20Dispatcher { contract_address: market.long_token } +// .balance_of(caller_address); +// let balance_caller_USDC = IERC20Dispatcher { contract_address: market.short_token } +// .balance_of(caller_address); - // Send token to deposit in the deposit vault (this should be in a multi call with create_deposit) - 'get balances'.print(); - // start_prank(market.long_token, caller_address); - // IERC20Dispatcher { contract_address: market.long_token } - // .transfer(deposit_vault.contract_address, 5000000000000000000); // 5 ETH +// assert(balance_deposit_vault_before == 0, 'balance deposit should be 0'); +// assert(balance_caller_ETH == 10000000000000000000, 'balanc ETH should be 10 ETH'); +// assert(balance_caller_USDC == 50000000000000000000000, 'USDC be 50 000 USDC'); - // start_prank(market.short_token, caller_address); - // IERC20Dispatcher { contract_address: market.short_token } - // .transfer(deposit_vault.contract_address, 25000000000000000000000); // 25000 USDC - // 'make transfer'.print(); +// // Send token to deposit in the deposit vault (this should be in a multi call with create_deposit) +// 'get balances'.print(); +// // start_prank(market.long_token, caller_address); +// // IERC20Dispatcher { contract_address: market.long_token } +// // .transfer(deposit_vault.contract_address, 5000000000000000000); // 5 ETH - IERC20Dispatcher { contract_address: market.long_token } - .mint(deposit_vault.contract_address, 50000000000000000000000000000); // 50 000 000 000 - IERC20Dispatcher { contract_address: market.short_token } - .mint(deposit_vault.contract_address, 50000000000000000000000000000); // 50 000 000 000 - // Create Deposit +// // start_prank(market.short_token, caller_address); +// // IERC20Dispatcher { contract_address: market.short_token } +// // .transfer(deposit_vault.contract_address, 25000000000000000000000); // 25000 USDC +// // 'make transfer'.print(); - let addresss_zero: ContractAddress = 0.try_into().unwrap(); +// IERC20Dispatcher { contract_address: market.long_token } +// .mint(deposit_vault.contract_address, 50000000000000000000000000000); // 50 000 000 000 +// IERC20Dispatcher { contract_address: market.short_token } +// .mint(deposit_vault.contract_address, 50000000000000000000000000000); // 50 000 000 000 +// // Create Deposit - let params = CreateDepositParams { - receiver: caller_address, - callback_contract: addresss_zero, - ui_fee_receiver: addresss_zero, - market: market.market_token, - initial_long_token: market.long_token, - initial_short_token: market.short_token, - long_token_swap_path: Array32Trait::::span32(@array![]), - short_token_swap_path: Array32Trait::::span32(@array![]), - min_market_tokens: 0, - execution_fee: 0, - callback_gas_limit: 0, - }; - 'create deposit'.print(); +// let addresss_zero: ContractAddress = 0.try_into().unwrap(); - start_roll(deposit_handler.contract_address, 1910); - let key = deposit_handler.create_deposit(caller_address, params); - let first_deposit = data_store.get_deposit(key); +// let params = CreateDepositParams { +// receiver: caller_address, +// callback_contract: addresss_zero, +// ui_fee_receiver: addresss_zero, +// market: market.market_token, +// initial_long_token: market.long_token, +// initial_short_token: market.short_token, +// long_token_swap_path: Array32Trait::::span32(@array![]), +// short_token_swap_path: Array32Trait::::span32(@array![]), +// min_market_tokens: 0, +// execution_fee: 0, +// callback_gas_limit: 0, +// }; +// 'create deposit'.print(); - 'created deposit'.print(); +// start_roll(deposit_handler.contract_address, 1910); +// let key = deposit_handler.create_deposit(caller_address, params); +// let first_deposit = data_store.get_deposit(key); - assert(first_deposit.account == caller_address, 'Wrong account depositer'); - assert(first_deposit.receiver == caller_address, 'Wrong account receiver'); - assert(first_deposit.initial_long_token == market.long_token, 'Wrong initial long token'); - assert( - first_deposit.initial_long_token_amount == 50000000000000000000000000000, - 'Wrong initial long token amount' - ); - assert( - first_deposit.initial_short_token_amount == 50000000000000000000000000000, - 'Wrong init short token amount' - ); +// 'created deposit'.print(); - let price_params = SetPricesParams { // TODO - signer_info: 1, - tokens: array![contract_address_const::<'ETH'>(), contract_address_const::<'USDC'>()], - compacted_min_oracle_block_numbers: array![1900, 1900], - compacted_max_oracle_block_numbers: array![1910, 1910], - compacted_oracle_timestamps: array![9999, 9999], - compacted_decimals: array![18, 18], - compacted_min_prices: array![4294967346000000], // 50000000, 1000000 compacted - compacted_min_prices_indexes: array![0], - compacted_max_prices: array![4294967346000000], // 50000000, 1000000 compacted - compacted_max_prices_indexes: array![0], - signatures: array![ - array!['signatures1', 'signatures2'].span(), array!['signatures1', 'signatures2'].span() - ], - price_feed_tokens: array![] - }; +// assert(first_deposit.account == caller_address, 'Wrong account depositer'); +// assert(first_deposit.receiver == caller_address, 'Wrong account receiver'); +// assert(first_deposit.initial_long_token == market.long_token, 'Wrong initial long token'); +// assert( +// first_deposit.initial_long_token_amount == 50000000000000000000000000000, +// 'Wrong initial long token amount' +// ); +// assert( +// first_deposit.initial_short_token_amount == 50000000000000000000000000000, +// 'Wrong init short token amount' +// ); - start_prank(role_store.contract_address, caller_address); +// let price_params = SetPricesParams { // TODO +// signer_info: 1, +// tokens: array![contract_address_const::<'ETH'>(), contract_address_const::<'USDC'>()], +// compacted_min_oracle_block_numbers: array![1900, 1900], +// compacted_max_oracle_block_numbers: array![1910, 1910], +// compacted_oracle_timestamps: array![9999, 9999], +// compacted_decimals: array![18, 18], +// compacted_min_prices: array![4294967346000000], // 50000000, 1000000 compacted +// compacted_min_prices_indexes: array![0], +// compacted_max_prices: array![4294967346000000], // 50000000, 1000000 compacted +// compacted_max_prices_indexes: array![0], +// signatures: array![ +// array!['signatures1', 'signatures2'].span(), array!['signatures1', 'signatures2'].span() +// ], +// price_feed_tokens: array![] +// }; - role_store.grant_role(caller_address, role::ORDER_KEEPER); - role_store.grant_role(caller_address, role::ROLE_ADMIN); - role_store.grant_role(caller_address, role::CONTROLLER); - role_store.grant_role(caller_address, role::MARKET_KEEPER); +// start_prank(role_store.contract_address, caller_address); - 'execute deposit'.print(); +// role_store.grant_role(caller_address, role::ORDER_KEEPER); +// role_store.grant_role(caller_address, role::ROLE_ADMIN); +// role_store.grant_role(caller_address, role::CONTROLLER); +// role_store.grant_role(caller_address, role::MARKET_KEEPER); - // Execute Deposit - start_roll(deposit_handler.contract_address, 1915); - deposit_handler.execute_deposit(key, price_params); +// 'execute deposit'.print(); - 'executed deposit'.print(); +// // Execute Deposit +// start_roll(deposit_handler.contract_address, 1915); +// deposit_handler.execute_deposit(key, price_params); - // let pool_value_info = market_utils::get_pool_value_info( - // data_store, - // market, - // Price { min: 2000, max: 2000 }, - // Price { min: 2000, max: 2000 }, - // Price { min: 2000, max: 2000 }, - // keys::max_pnl_factor_for_deposits(), - // true, - // ); +// 'executed deposit'.print(); - // assert(pool_value_info.pool_value.mag == 42000000000000000000000, 'wrong pool value amount'); - // assert(pool_value_info.long_token_amount == 6000000000000000000, 'wrong long token amount'); - // assert(pool_value_info.short_token_amount == 30000000000000000000000, 'wrong short token amount'); +// // let pool_value_info = market_utils::get_pool_value_info( +// // data_store, +// // market, +// // Price { min: 2000, max: 2000 }, +// // Price { min: 2000, max: 2000 }, +// // Price { min: 2000, max: 2000 }, +// // keys::max_pnl_factor_for_deposits(), +// // true, +// // ); - let not_deposit = data_store.get_deposit(key); - let default_deposit: Deposit = Default::default(); - assert(not_deposit == default_deposit, 'Still existing deposit'); +// // assert(pool_value_info.pool_value.mag == 42000000000000000000000, 'wrong pool value amount'); +// // assert(pool_value_info.long_token_amount == 6000000000000000000, 'wrong long token amount'); +// // assert(pool_value_info.short_token_amount == 30000000000000000000000, 'wrong short token amount'); - let market_token_dispatcher = IMarketTokenDispatcher { contract_address: market.market_token }; - let balance_market_token = market_token_dispatcher.balance_of(caller_address); +// let not_deposit = data_store.get_deposit(key); +// let default_deposit: Deposit = Default::default(); +// assert(not_deposit == default_deposit, 'Still existing deposit'); - assert(balance_market_token != 0, 'should receive market token'); +// let market_token_dispatcher = IMarketTokenDispatcher { contract_address: market.market_token }; +// let balance_market_token = market_token_dispatcher.balance_of(caller_address); - let balance_deposit_vault_after = IERC20Dispatcher { contract_address: market.short_token } - .balance_of(deposit_vault.contract_address); +// assert(balance_market_token != 0, 'should receive market token'); - // let pool_value_info = market_utils::get_pool_value_info( - // data_store, - // market, - // Price { min: 5000, max: 5000, }, - // Price { min: 5000, max: 5000, }, - // Price { min: 1, max: 1, }, - // keys::max_pnl_factor_for_deposits(), - // true, - // ); +// let balance_deposit_vault_after = IERC20Dispatcher { contract_address: market.short_token } +// .balance_of(deposit_vault.contract_address); - // pool_value_info.pool_value.mag.print(); // 10000 000000000000000000 - // pool_value_info.long_token_amount.print(); // 5 000000000000000000 - // pool_value_info.short_token_amount.print(); // 25000 000000000000000000 +// // let pool_value_info = market_utils::get_pool_value_info( +// // data_store, +// // market, +// // Price { min: 5000, max: 5000, }, +// // Price { min: 5000, max: 5000, }, +// // Price { min: 1, max: 1, }, +// // keys::max_pnl_factor_for_deposits(), +// // true, +// // ); - // ************************************* TEST LONG ********************************************* +// // pool_value_info.pool_value.mag.print(); // 10000 000000000000000000 +// // pool_value_info.long_token_amount.print(); // 5 000000000000000000 +// // pool_value_info.short_token_amount.print(); // 25000 000000000000000000 - 'Begining of LONG TEST'.print(); +// // ************************************* TEST LONG ********************************************* - let key_open_interest = keys::open_interest_key( - market.market_token, contract_address_const::<'ETH'>(), true - ); - data_store.set_u256(key_open_interest, 1); - let max_key_open_interest = keys::max_open_interest_key(market.market_token, true); - data_store - .set_u256( - max_key_open_interest, 1000000000000000000000000000000000000000000000000000 - ); // 1 000 000 +// 'Begining of LONG TEST'.print(); - // Send token to order_vault in multicall with create_order - start_prank(contract_address_const::<'ETH'>(), caller_address); - IERC20Dispatcher { contract_address: contract_address_const::<'ETH'>() } - .transfer(order_vault.contract_address, 2000000000000000000); // 2ETH +// let key_open_interest = keys::open_interest_key( +// market.market_token, contract_address_const::<'ETH'>(), true +// ); +// data_store.set_u256(key_open_interest, 1); +// let max_key_open_interest = keys::max_open_interest_key(market.market_token, true); +// data_store +// .set_u256( +// max_key_open_interest, 1000000000000000000000000000000000000000000000000000 +// ); // 1 000 000 - 'transfer made'.print(); - // Create order_params Struct - let contract_address = contract_address_const::<0>(); - start_prank(market.market_token, caller_address); - start_prank(market.long_token, caller_address); - let order_params_long = CreateOrderParams { - receiver: caller_address, - callback_contract: contract_address, - ui_fee_receiver: contract_address, - market: market.market_token, - initial_collateral_token: market.long_token, - swap_path: Array32Trait::::span32(@array![]), - size_delta_usd: 10000000000000000000000, - initial_collateral_delta_amount: 2000000000000000000, // 10^18 - trigger_price: 5000, - acceptable_price: 5500, - execution_fee: 0, - callback_gas_limit: 0, - min_output_amount: 0, - order_type: OrderType::MarketIncrease(()), - decrease_position_swap_type: DecreasePositionSwapType::NoSwap(()), - is_long: true, - referral_code: 0 - }; - // Create the swap order. - start_roll(order_handler.contract_address, 1930); - 'try to create prder'.print(); - start_prank(order_handler.contract_address, caller_address); - let key_long = order_handler.create_order(caller_address, order_params_long); - 'long created'.print(); - let got_order_long = data_store.get_order(key_long); - // data_store.set_u256(keys::pool_amount_key(market.market_token, contract_address_const::<'USDC'>()), ); - // data_store.set_u256(keys::pool_amount_key(market.market_token, contract_address_const::<'ETH'>()), 1000000); - // Execute the swap order. +// // Send token to order_vault in multicall with create_order +// start_prank(contract_address_const::<'ETH'>(), caller_address); +// IERC20Dispatcher { contract_address: contract_address_const::<'ETH'>() } +// .transfer(order_vault.contract_address, 2000000000000000000); // 2ETH - data_store - .set_u256( - keys::pool_amount_key(market.market_token, contract_address_const::<'USDC'>()), - 50000000000000000000000000000 - ); - data_store - .set_u256( - keys::pool_amount_key(market.market_token, contract_address_const::<'ETH'>()), - 50000000000000000000000000000 - ); +// 'transfer made'.print(); +// // Create order_params Struct +// let contract_address = contract_address_const::<0>(); +// start_prank(market.market_token, caller_address); +// start_prank(market.long_token, caller_address); +// let order_params_long = CreateOrderParams { +// receiver: caller_address, +// callback_contract: contract_address, +// ui_fee_receiver: contract_address, +// market: market.market_token, +// initial_collateral_token: market.long_token, +// swap_path: Array32Trait::::span32(@array![]), +// size_delta_usd: 10000000000000000000000, +// initial_collateral_delta_amount: 2000000000000000000, // 10^18 +// trigger_price: 5000, +// acceptable_price: 5500, +// execution_fee: 0, +// callback_gas_limit: 0, +// min_output_amount: 0, +// order_type: OrderType::MarketIncrease(()), +// decrease_position_swap_type: DecreasePositionSwapType::NoSwap(()), +// is_long: true, +// referral_code: 0 +// }; +// // Create the swap order. +// start_roll(order_handler.contract_address, 1930); +// 'try to create prder'.print(); +// start_prank(order_handler.contract_address, caller_address); +// let key_long = order_handler.create_order(caller_address, order_params_long); +// 'long created'.print(); +// let got_order_long = data_store.get_order(key_long); +// // data_store.set_u256(keys::pool_amount_key(market.market_token, contract_address_const::<'USDC'>()), ); +// // data_store.set_u256(keys::pool_amount_key(market.market_token, contract_address_const::<'ETH'>()), 1000000); +// // Execute the swap order. - let signatures: Span = array![0].span(); - let set_price_params = SetPricesParams { - signer_info: 2, - tokens: array![contract_address_const::<'ETH'>(), contract_address_const::<'USDC'>()], - compacted_min_oracle_block_numbers: array![1910, 1910], - compacted_max_oracle_block_numbers: array![1920, 1920], - compacted_oracle_timestamps: array![9999, 9999], - compacted_decimals: array![1, 1], - compacted_min_prices: array![2147483648010000], // 500000, 10000 compacted - compacted_min_prices_indexes: array![0], - compacted_max_prices: array![2147483648010000], // 500000, 10000 compacted - compacted_max_prices_indexes: array![0], - signatures: array![ - array!['signatures1', 'signatures2'].span(), array!['signatures1', 'signatures2'].span() - ], - price_feed_tokens: array![] - }; +// data_store +// .set_u256( +// keys::pool_amount_key(market.market_token, contract_address_const::<'USDC'>()), +// 50000000000000000000000000000 +// ); +// data_store +// .set_u256( +// keys::pool_amount_key(market.market_token, contract_address_const::<'ETH'>()), +// 50000000000000000000000000000 +// ); - let keeper_address = contract_address_const::<'keeper'>(); - role_store.grant_role(keeper_address, role::ORDER_KEEPER); +// let signatures: Span = array![0].span(); +// let set_price_params = SetPricesParams { +// signer_info: 2, +// tokens: array![contract_address_const::<'ETH'>(), contract_address_const::<'USDC'>()], +// compacted_min_oracle_block_numbers: array![1910, 1910], +// compacted_max_oracle_block_numbers: array![1920, 1920], +// compacted_oracle_timestamps: array![9999, 9999], +// compacted_decimals: array![1, 1], +// compacted_min_prices: array![2147483648010000], // 500000, 10000 compacted +// compacted_min_prices_indexes: array![0], +// compacted_max_prices: array![2147483648010000], // 500000, 10000 compacted +// compacted_max_prices_indexes: array![0], +// signatures: array![ +// array!['signatures1', 'signatures2'].span(), array!['signatures1', 'signatures2'].span() +// ], +// price_feed_tokens: array![] +// }; - stop_prank(order_handler.contract_address); - start_prank(order_handler.contract_address, keeper_address); - start_roll(order_handler.contract_address, 1935); - // TODO add real signatures check on Oracle Account - order_handler.execute_order_keeper(key_long, set_price_params, keeper_address); - 'long position SUCCEEDED'.print(); - let position_key = data_store.get_account_position_keys(caller_address, 0, 10); +// let keeper_address = contract_address_const::<'keeper'>(); +// role_store.grant_role(keeper_address, role::ORDER_KEEPER); - let position_key_1: felt252 = *position_key.at(0); - let first_position = data_store.get_position(position_key_1); - let market_prices = market_utils::MarketPrices { - index_token_price: Price { min: 8000, max: 8000, }, - long_token_price: Price { min: 8000, max: 8000, }, - short_token_price: Price { min: 1, max: 1, }, - }; - 'size tokens'.print(); - first_position.size_in_tokens.print(); - 'size in usd'.print(); - first_position.size_in_usd.print(); - 'OKAAAAAYYYYYY'.print(); - oracle.set_primary_prices(market.long_token, 6000); - let first_position_after_pump = data_store.get_position(position_key_1); - 'size tokens after pump'.print(); - first_position_after_pump.size_in_tokens.print(); - 'size in usd after pump'.print(); - first_position_after_pump.size_in_usd.print(); +// stop_prank(order_handler.contract_address); +// start_prank(order_handler.contract_address, keeper_address); +// start_roll(order_handler.contract_address, 1935); +// // TODO add real signatures check on Oracle Account +// order_handler.execute_order_keeper(key_long, set_price_params, keeper_address); +// 'long position SUCCEEDED'.print(); +// let position_key = data_store.get_account_position_keys(caller_address, 0, 10); - let position_info = reader - .get_position_info( - data_store, referal_storage, position_key_1, market_prices, 0, contract_address, true - ); - 'pnl'.print(); - position_info.base_pnl_usd.mag.print(); +// let position_key_1: felt252 = *position_key.at(0); +// let first_position = data_store.get_position(position_key_1); +// let market_prices = market_utils::MarketPrices { +// index_token_price: Price { min: 8000, max: 8000, }, +// long_token_price: Price { min: 8000, max: 8000, }, +// short_token_price: Price { min: 1, max: 1, }, +// }; +// 'size tokens'.print(); +// first_position.size_in_tokens.print(); +// 'size in usd'.print(); +// first_position.size_in_usd.print(); +// 'OKAAAAAYYYYYY'.print(); +// oracle.set_primary_prices(market.long_token, 6000); +// let first_position_after_pump = data_store.get_position(position_key_1); +// 'size tokens after pump'.print(); +// first_position_after_pump.size_in_tokens.print(); +// 'size in usd after pump'.print(); +// first_position_after_pump.size_in_usd.print(); - // let second_swap_pool_value_info = market_utils::get_pool_value_info( - // data_store, - // market, - // Price { min: 5000, max: 5000, }, - // Price { min: 5000, max: 5000, }, - // Price { min: 1, max: 1, }, - // keys::max_pnl_factor_for_deposits(), - // true, - // ); - - // second_swap_pool_value_info.pool_value.mag.print(); - // second_swap_pool_value_info.long_token_amount.print(); - // second_swap_pool_value_info.short_token_amount.print(); - // let (position_pnl_usd, uncapped_position_pnl_usd, size_delta_in_tokens) = - // position_utils::get_position_pnl_usd( - // data_store, market, market_prices, first_position, 5000 - // ); - // position_pnl_usd.mag.print(); - - //////////////////////////////////// CLOSING POSITION ////////////////////////////////////// - 'CLOOOOSE POSITION'.print(); - - let balance_of_mkt_before = IERC20Dispatcher { - contract_address: contract_address_const::<'USDC'>() - } - .balance_of(caller_address); - 'balance of mkt before'.print(); - balance_of_mkt_before.print(); - - start_prank(market.market_token, caller_address); - start_prank(market.long_token, caller_address); - let order_params_long_dec = CreateOrderParams { - receiver: caller_address, - callback_contract: contract_address, - ui_fee_receiver: contract_address, - market: market.market_token, - initial_collateral_token: market.long_token, - swap_path: Array32Trait::::span32(@array![market.market_token]), - size_delta_usd: 6000000000000000000000, // 6000 - initial_collateral_delta_amount: 1000000000000000000, // 1 ETH 10^18 - trigger_price: 6000, - acceptable_price: 6000, - execution_fee: 0, - callback_gas_limit: 0, - min_output_amount: 6000000000000000000000, // 6000 - order_type: OrderType::MarketDecrease(()), - decrease_position_swap_type: DecreasePositionSwapType::NoSwap(()), - is_long: true, - referral_code: 0 - }; - // Create the long order. - start_roll(order_handler.contract_address, 1940); - 'try to create order'.print(); - start_prank(order_handler.contract_address, caller_address); - let key_long_dec = order_handler.create_order(caller_address, order_params_long_dec); - 'long decrease created'.print(); - let got_order_long_dec = data_store.get_order(key_long_dec); - // data_store.set_u256(keys::pool_amount_key(market.market_token, contract_address_const::<'USDC'>()), ); - // data_store.set_u256(keys::pool_amount_key(market.market_token, contract_address_const::<'ETH'>()), 1000000); - // Execute the swap order. +// let position_info = reader +// .get_position_info( +// data_store, referal_storage, position_key_1, market_prices, 0, contract_address, true +// ); +// 'pnl'.print(); +// position_info.base_pnl_usd.mag.print(); - let signatures: Span = array![0].span(); - let set_price_params_dec = SetPricesParams { - signer_info: 2, - tokens: array![contract_address_const::<'ETH'>(), contract_address_const::<'USDC'>()], - compacted_min_oracle_block_numbers: array![1910, 1910], - compacted_max_oracle_block_numbers: array![1920, 1920], - compacted_oracle_timestamps: array![9999, 9999], - compacted_decimals: array![1, 1], - compacted_min_prices: array![2147483648010000], // 500000, 10000 compacted - compacted_min_prices_indexes: array![0], - compacted_max_prices: array![2147483648010000], // 500000, 10000 compacted - compacted_max_prices_indexes: array![0], - signatures: array![ - array!['signatures1', 'signatures2'].span(), array!['signatures1', 'signatures2'].span() - ], - price_feed_tokens: array![] - }; +// // let second_swap_pool_value_info = market_utils::get_pool_value_info( +// // data_store, +// // market, +// // Price { min: 5000, max: 5000, }, +// // Price { min: 5000, max: 5000, }, +// // Price { min: 1, max: 1, }, +// // keys::max_pnl_factor_for_deposits(), +// // true, +// // ); + +// // second_swap_pool_value_info.pool_value.mag.print(); +// // second_swap_pool_value_info.long_token_amount.print(); +// // second_swap_pool_value_info.short_token_amount.print(); +// // let (position_pnl_usd, uncapped_position_pnl_usd, size_delta_in_tokens) = +// // position_utils::get_position_pnl_usd( +// // data_store, market, market_prices, first_position, 5000 +// // ); +// // position_pnl_usd.mag.print(); - let keeper_address = contract_address_const::<'keeper'>(); - role_store.grant_role(keeper_address, role::ORDER_KEEPER); +// //////////////////////////////////// CLOSING POSITION ////////////////////////////////////// +// 'CLOOOOSE POSITION'.print(); - stop_prank(order_handler.contract_address); - start_prank(order_handler.contract_address, keeper_address); - start_roll(order_handler.contract_address, 1945); - // TODO add real signatures check on Oracle Account - order_handler.execute_order_keeper(key_long_dec, set_price_params_dec, keeper_address); - 'long pos dec SUCCEEDED'.print(); +// let balance_of_mkt_before = IERC20Dispatcher { +// contract_address: contract_address_const::<'USDC'>() +// } +// .balance_of(caller_address); +// 'balance of mkt before'.print(); +// balance_of_mkt_before.print(); - let first_position_dec = data_store.get_position(position_key_1); +// start_prank(market.market_token, caller_address); +// start_prank(market.long_token, caller_address); +// let order_params_long_dec = CreateOrderParams { +// receiver: caller_address, +// callback_contract: contract_address, +// ui_fee_receiver: contract_address, +// market: market.market_token, +// initial_collateral_token: market.long_token, +// swap_path: Array32Trait::::span32(@array![market.market_token]), +// size_delta_usd: 6000000000000000000000, // 6000 +// initial_collateral_delta_amount: 1000000000000000000, // 1 ETH 10^18 +// trigger_price: 6000, +// acceptable_price: 6000, +// execution_fee: 0, +// callback_gas_limit: 0, +// min_output_amount: 6000000000000000000000, // 6000 +// order_type: OrderType::MarketDecrease(()), +// decrease_position_swap_type: DecreasePositionSwapType::NoSwap(()), +// is_long: true, +// referral_code: 0 +// }; +// // Create the long order. +// start_roll(order_handler.contract_address, 1940); +// 'try to create order'.print(); +// start_prank(order_handler.contract_address, caller_address); +// let key_long_dec = order_handler.create_order(caller_address, order_params_long_dec); +// 'long decrease created'.print(); +// let got_order_long_dec = data_store.get_order(key_long_dec); +// // data_store.set_u256(keys::pool_amount_key(market.market_token, contract_address_const::<'USDC'>()), ); +// // data_store.set_u256(keys::pool_amount_key(market.market_token, contract_address_const::<'ETH'>()), 1000000); +// // Execute the swap order. - 'size tokens before'.print(); - first_position.size_in_tokens.print(); - 'size in usd before'.print(); - first_position.size_in_usd.print(); +// let signatures: Span = array![0].span(); +// let set_price_params_dec = SetPricesParams { +// signer_info: 2, +// tokens: array![contract_address_const::<'ETH'>(), contract_address_const::<'USDC'>()], +// compacted_min_oracle_block_numbers: array![1910, 1910], +// compacted_max_oracle_block_numbers: array![1920, 1920], +// compacted_oracle_timestamps: array![9999, 9999], +// compacted_decimals: array![1, 1], +// compacted_min_prices: array![2147483648010000], // 500000, 10000 compacted +// compacted_min_prices_indexes: array![0], +// compacted_max_prices: array![2147483648010000], // 500000, 10000 compacted +// compacted_max_prices_indexes: array![0], +// signatures: array![ +// array!['signatures1', 'signatures2'].span(), array!['signatures1', 'signatures2'].span() +// ], +// price_feed_tokens: array![] +// }; - 'size tokens'.print(); - first_position_dec.size_in_tokens.print(); - 'size in usd'.print(); - first_position_dec.size_in_usd.print(); +// let keeper_address = contract_address_const::<'keeper'>(); +// role_store.grant_role(keeper_address, role::ORDER_KEEPER); - let balance_of_mkt_after = IERC20Dispatcher { - contract_address: contract_address_const::<'USDC'>() - } - .balance_of(caller_address); - 'balance of mkt after'.print(); - balance_of_mkt_after.print(); +// stop_prank(order_handler.contract_address); +// start_prank(order_handler.contract_address, keeper_address); +// start_roll(order_handler.contract_address, 1945); +// // TODO add real signatures check on Oracle Account +// order_handler.execute_order_keeper(key_long_dec, set_price_params_dec, keeper_address); +// 'long pos dec SUCCEEDED'.print(); - /// close all position - oracle.set_primary_prices(market.long_token, 7000); +// let first_position_dec = data_store.get_position(position_key_1); - start_prank(market.market_token, caller_address); - start_prank(market.long_token, caller_address); - let order_params_long_dec_2 = CreateOrderParams { - receiver: caller_address, - callback_contract: contract_address, - ui_fee_receiver: contract_address, - market: market.market_token, - initial_collateral_token: market.long_token, - swap_path: Array32Trait::::span32(@array![market.market_token]), - size_delta_usd: 7000000000000000000000, // 6000 - initial_collateral_delta_amount: 1000000000000000000, // 1 ETH 10^18 - trigger_price: 7000, - acceptable_price: 7000, - execution_fee: 0, - callback_gas_limit: 0, - min_output_amount: 7000000000000000000000, // 6000 - order_type: OrderType::MarketDecrease(()), - decrease_position_swap_type: DecreasePositionSwapType::NoSwap(()), - is_long: true, - referral_code: 0 - }; - // Create the long order. - start_roll(order_handler.contract_address, 1950); - 'try to create order'.print(); - start_prank(order_handler.contract_address, caller_address); - let key_long_dec_2 = order_handler.create_order(caller_address, order_params_long_dec_2); - 'long decrease created'.print(); - let got_order_long_dec = data_store.get_order(key_long_dec_2); - // data_store.set_u256(keys::pool_amount_key(market.market_token, contract_address_const::<'USDC'>()), ); - // data_store.set_u256(keys::pool_amount_key(market.market_token, contract_address_const::<'ETH'>()), 1000000); - // Execute the swap order. +// 'size tokens before'.print(); +// first_position.size_in_tokens.print(); +// 'size in usd before'.print(); +// first_position.size_in_usd.print(); - let keeper_address = contract_address_const::<'keeper'>(); - role_store.grant_role(keeper_address, role::ORDER_KEEPER); +// 'size tokens'.print(); +// first_position_dec.size_in_tokens.print(); +// 'size in usd'.print(); +// first_position_dec.size_in_usd.print(); - let signatures: Span = array![0].span(); - let set_price_params_dec2 = SetPricesParams { - signer_info: 2, - tokens: array![contract_address_const::<'ETH'>(), contract_address_const::<'USDC'>()], - compacted_min_oracle_block_numbers: array![1910, 1910], - compacted_max_oracle_block_numbers: array![1920, 1920], - compacted_oracle_timestamps: array![9999, 9999], - compacted_decimals: array![1, 1], - compacted_min_prices: array![2147483648010000], // 500000, 10000 compacted - compacted_min_prices_indexes: array![0], - compacted_max_prices: array![2147483648010000], // 500000, 10000 compacted - compacted_max_prices_indexes: array![0], - signatures: array![ - array!['signatures1', 'signatures2'].span(), array!['signatures1', 'signatures2'].span() - ], - price_feed_tokens: array![] - }; +// let balance_of_mkt_after = IERC20Dispatcher { +// contract_address: contract_address_const::<'USDC'>() +// } +// .balance_of(caller_address); +// 'balance of mkt after'.print(); +// balance_of_mkt_after.print(); - stop_prank(order_handler.contract_address); - start_prank(order_handler.contract_address, keeper_address); - start_roll(order_handler.contract_address, 1955); - // TODO add real signatures check on Oracle Account - order_handler.execute_order_keeper(key_long_dec_2, set_price_params_dec2, keeper_address); - 'long pos dec SUCCEEDED'.print(); +// /// close all position +// oracle.set_primary_prices(market.long_token, 7000); - let first_position_dec = data_store.get_position(position_key_1); +// start_prank(market.market_token, caller_address); +// start_prank(market.long_token, caller_address); +// let order_params_long_dec_2 = CreateOrderParams { +// receiver: caller_address, +// callback_contract: contract_address, +// ui_fee_receiver: contract_address, +// market: market.market_token, +// initial_collateral_token: market.long_token, +// swap_path: Array32Trait::::span32(@array![market.market_token]), +// size_delta_usd: 7000000000000000000000, // 6000 +// initial_collateral_delta_amount: 1000000000000000000, // 1 ETH 10^18 +// trigger_price: 7000, +// acceptable_price: 7000, +// execution_fee: 0, +// callback_gas_limit: 0, +// min_output_amount: 7000000000000000000000, // 6000 +// order_type: OrderType::MarketDecrease(()), +// decrease_position_swap_type: DecreasePositionSwapType::NoSwap(()), +// is_long: true, +// referral_code: 0 +// }; +// // Create the long order. +// start_roll(order_handler.contract_address, 1950); +// 'try to create order'.print(); +// start_prank(order_handler.contract_address, caller_address); +// let key_long_dec_2 = order_handler.create_order(caller_address, order_params_long_dec_2); +// 'long decrease created'.print(); +// let got_order_long_dec = data_store.get_order(key_long_dec_2); +// // data_store.set_u256(keys::pool_amount_key(market.market_token, contract_address_const::<'USDC'>()), ); +// // data_store.set_u256(keys::pool_amount_key(market.market_token, contract_address_const::<'ETH'>()), 1000000); +// // Execute the swap order. - 'size tokens before 2'.print(); - first_position.size_in_tokens.print(); - 'size in usd before 2'.print(); - first_position.size_in_usd.print(); +// let keeper_address = contract_address_const::<'keeper'>(); +// role_store.grant_role(keeper_address, role::ORDER_KEEPER); - 'size tokens 2'.print(); - let token_size_dec = first_position_dec.size_in_tokens; - assert(token_size_dec == 0, 'wrong token size'); - 'size in usd 2'.print(); - first_position_dec.size_in_usd.print(); +// let signatures: Span = array![0].span(); +// let set_price_params_dec2 = SetPricesParams { +// signer_info: 2, +// tokens: array![contract_address_const::<'ETH'>(), contract_address_const::<'USDC'>()], +// compacted_min_oracle_block_numbers: array![1910, 1910], +// compacted_max_oracle_block_numbers: array![1920, 1920], +// compacted_oracle_timestamps: array![9999, 9999], +// compacted_decimals: array![1, 1], +// compacted_min_prices: array![2147483648010000], // 500000, 10000 compacted +// compacted_min_prices_indexes: array![0], +// compacted_max_prices: array![2147483648010000], // 500000, 10000 compacted +// compacted_max_prices_indexes: array![0], +// signatures: array![ +// array!['signatures1', 'signatures2'].span(), array!['signatures1', 'signatures2'].span() +// ], +// price_feed_tokens: array![] +// }; - let balance_of_mkt_after = IERC20Dispatcher { - contract_address: contract_address_const::<'USDC'>() - } - .balance_of(caller_address); - 'balance of mkt after 2'.print(); - balance_of_mkt_after.print(); +// stop_prank(order_handler.contract_address); +// start_prank(order_handler.contract_address, keeper_address); +// start_roll(order_handler.contract_address, 1955); +// // TODO add real signatures check on Oracle Account +// order_handler.execute_order_keeper(key_long_dec_2, set_price_params_dec2, keeper_address); +// 'long pos dec SUCCEEDED'.print(); - assert(balance_of_mkt_after == 63000000000000000000000, 'wrong balance final size'); +// let first_position_dec = data_store.get_position(position_key_1); - /// ------ TEST SWAP -------- +// 'size tokens before 2'.print(); +// first_position.size_in_tokens.print(); +// 'size in usd before 2'.print(); +// first_position.size_in_usd.print(); - start_prank(contract_address_const::<'ETH'>(), caller_address); //change to switch swap - // Send token to order_vault in multicall with create_order - IERC20Dispatcher { contract_address: contract_address_const::<'ETH'>() } //change to switch swap - .transfer(order_vault.contract_address, 1000000000000000000); +// 'size tokens 2'.print(); +// let token_size_dec = first_position_dec.size_in_tokens; +// assert(token_size_dec == 0, 'wrong token size'); +// 'size in usd 2'.print(); +// first_position_dec.size_in_usd.print(); - // Create order_params Struct - let contract_address = contract_address_const::<0>(); - start_prank(market.long_token, caller_address); //change to switch swap +// let balance_of_mkt_after = IERC20Dispatcher { +// contract_address: contract_address_const::<'USDC'>() +// } +// .balance_of(caller_address); +// 'balance of mkt after 2'.print(); +// balance_of_mkt_after.print(); - let order_params = CreateOrderParams { - receiver: caller_address, - callback_contract: contract_address, - ui_fee_receiver: contract_address, - market: contract_address, - initial_collateral_token: market.long_token, //change to switch swap - swap_path: Array32Trait::::span32(@array![market.market_token]), - size_delta_usd: 7000000000000000000, - initial_collateral_delta_amount: 1000000000000000000, // 10^18 - trigger_price: 0, - acceptable_price: 0, - execution_fee: 0, - callback_gas_limit: 0, - min_output_amount: 0, - order_type: OrderType::MarketSwap(()), - decrease_position_swap_type: DecreasePositionSwapType::NoSwap(()), - is_long: false, - referral_code: 0 - }; - // Create the swap order. - start_roll(order_handler.contract_address, 1960); - //here we create the order but we do not execute it yet - start_prank(order_handler.contract_address, caller_address); //change to switch swap +// assert(balance_of_mkt_after == 63000000000000000000000, 'wrong balance final size'); - let key = order_handler.create_order(caller_address, order_params); +// /// ------ TEST SWAP -------- - let got_order = data_store.get_order(key); +// start_prank(contract_address_const::<'ETH'>(), caller_address); //change to switch swap +// // Send token to order_vault in multicall with create_order +// IERC20Dispatcher { contract_address: contract_address_const::<'ETH'>() } //change to switch swap +// .transfer(order_vault.contract_address, 1000000000000000000); - // data_store - // .set_u256( - // keys::pool_amount_key(market.market_token, contract_address_const::<'USDC'>()), - // 50000000000000000000000000000 - // ); - // data_store - // .set_u256( - // keys::pool_amount_key(market.market_token, contract_address_const::<'ETH'>()), - // 50000000000000000000000000000 - // ); +// // Create order_params Struct +// let contract_address = contract_address_const::<0>(); +// start_prank(market.long_token, caller_address); //change to switch swap - // Execute the swap order. - let signatures: Span = array![0].span(); - let set_price_params = SetPricesParams { - signer_info: 2, - tokens: array![contract_address_const::<'ETH'>(), contract_address_const::<'USDC'>()], - compacted_min_oracle_block_numbers: array![1910, 1910], - compacted_max_oracle_block_numbers: array![1920, 1920], - compacted_oracle_timestamps: array![9999, 9999], - compacted_decimals: array![1, 1], - compacted_min_prices: array![2147483648010000], // 500000, 10000 compacted - compacted_min_prices_indexes: array![0], - compacted_max_prices: array![2147483648010000], // 500000, 10000 compacted - compacted_max_prices_indexes: array![0], - signatures: array![ - array!['signatures1', 'signatures2'].span(), array!['signatures1', 'signatures2'].span() - ], - price_feed_tokens: array![] - }; +// let order_params = CreateOrderParams { +// receiver: caller_address, +// callback_contract: contract_address, +// ui_fee_receiver: contract_address, +// market: contract_address, +// initial_collateral_token: market.long_token, //change to switch swap +// swap_path: Array32Trait::::span32(@array![market.market_token]), +// size_delta_usd: 7000000000000000000, +// initial_collateral_delta_amount: 1000000000000000000, // 10^18 +// trigger_price: 0, +// acceptable_price: 0, +// execution_fee: 0, +// callback_gas_limit: 0, +// min_output_amount: 0, +// order_type: OrderType::MarketSwap(()), +// decrease_position_swap_type: DecreasePositionSwapType::NoSwap(()), +// is_long: false, +// referral_code: 0 +// }; +// // Create the swap order. +// start_roll(order_handler.contract_address, 1960); +// //here we create the order but we do not execute it yet +// start_prank(order_handler.contract_address, caller_address); //change to switch swap - let keeper_address = contract_address_const::<'keeper'>(); - role_store.grant_role(keeper_address, role::ORDER_KEEPER); +// let key = order_handler.create_order(caller_address, order_params); - stop_prank(order_handler.contract_address); - start_prank(order_handler.contract_address, keeper_address); - start_roll(order_handler.contract_address, 1965); - // TODO add real signatures check on Oracle Account -> Later - order_handler.execute_order_keeper(key, set_price_params, keeper_address); //execute order +// let got_order = data_store.get_order(key); - let balance_of_swap = IERC20Dispatcher { contract_address: contract_address_const::<'USDC'>() } - .balance_of(caller_address); +// // data_store +// // .set_u256( +// // keys::pool_amount_key(market.market_token, contract_address_const::<'USDC'>()), +// // 50000000000000000000000000000 +// // ); +// // data_store +// // .set_u256( +// // keys::pool_amount_key(market.market_token, contract_address_const::<'ETH'>()), +// // 50000000000000000000000000000 +// // ); - assert(balance_of_swap == 70000000000000000000000, 'wrong balance final swap'); - // ********************************************************************************************* - // * TEARDOWN * - // ********************************************************************************************* - teardown(data_store, market_factory); -} +// // Execute the swap order. +// let signatures: Span = array![0].span(); +// let set_price_params = SetPricesParams { +// signer_info: 2, +// tokens: array![contract_address_const::<'ETH'>(), contract_address_const::<'USDC'>()], +// compacted_min_oracle_block_numbers: array![1910, 1910], +// compacted_max_oracle_block_numbers: array![1920, 1920], +// compacted_oracle_timestamps: array![9999, 9999], +// compacted_decimals: array![1, 1], +// compacted_min_prices: array![2147483648010000], // 500000, 10000 compacted +// compacted_min_prices_indexes: array![0], +// compacted_max_prices: array![2147483648010000], // 500000, 10000 compacted +// compacted_max_prices_indexes: array![0], +// signatures: array![ +// array!['signatures1', 'signatures2'].span(), array!['signatures1', 'signatures2'].span() +// ], +// price_feed_tokens: array![] +// }; -#[test] -fn test_long_18_decrease_close_integration() { - // ********************************************************************************************* - // * SETUP * - // ********************************************************************************************* - let ( - caller_address, - market_factory_address, - role_store_address, - data_store_address, - market_token_class_hash, - market_factory, - role_store, - data_store, - event_emitter, - exchange_router, - deposit_handler, - deposit_vault, - oracle, - order_handler, - order_vault, - reader, - referal_storage, - withdrawal_handler, - withdrawal_vault, - ) = - setup(); +// let keeper_address = contract_address_const::<'keeper'>(); +// role_store.grant_role(keeper_address, role::ORDER_KEEPER); - // ********************************************************************************************* - // * TEST LOGIC * - // ********************************************************************************************* +// stop_prank(order_handler.contract_address); +// start_prank(order_handler.contract_address, keeper_address); +// start_roll(order_handler.contract_address, 1965); +// // TODO add real signatures check on Oracle Account -> Later +// order_handler.execute_order_keeper(key, set_price_params, keeper_address); //execute order - // Create a market. - let market = data_store.get_market(create_market(market_factory)); +// let balance_of_swap = IERC20Dispatcher { contract_address: contract_address_const::<'USDC'>() } +// .balance_of(caller_address); - // Set params in data_store - data_store.set_address(keys::fee_token(), market.index_token); - data_store.set_u256(keys::max_swap_path_length(), 5); +// assert(balance_of_swap == 70000000000000000000000, 'wrong balance final swap'); +// // ********************************************************************************************* +// // * TEARDOWN * +// // ********************************************************************************************* +// teardown(data_store, market_factory); +// } - // Set max pool amount. - data_store - .set_u256( - keys::max_pool_amount_key(market.market_token, market.long_token), - 5000000000000000000000000000000000000000000 //500 000 ETH - ); - data_store - .set_u256( - keys::max_pool_amount_key(market.market_token, market.short_token), - 2500000000000000000000000000000000000000000000 //250 000 000 USDC - ); +// #[test] +// fn test_long_18_decrease_close_integration() { +// // ********************************************************************************************* +// // * SETUP * +// // ********************************************************************************************* +// let ( +// caller_address, +// market_factory_address, +// role_store_address, +// data_store_address, +// market_token_class_hash, +// market_factory, +// role_store, +// data_store, +// event_emitter, +// exchange_router, +// deposit_handler, +// deposit_vault, +// oracle, +// order_handler, +// order_vault, +// reader, +// referal_storage, +// withdrawal_handler, +// withdrawal_vault, +// ) = +// setup(); - let factor_for_deposits: felt252 = keys::max_pnl_factor_for_deposits(); - data_store - .set_u256( - keys::max_pnl_factor_key(factor_for_deposits, market.market_token, true), - 50000000000000000000000000000000000000000000000 - ); - let factor_for_withdrawal: felt252 = keys::max_pnl_factor_for_withdrawals(); - data_store - .set_u256( - keys::max_pnl_factor_key(factor_for_withdrawal, market.market_token, true), - 50000000000000000000000000000000000000000000000 - ); +// // ********************************************************************************************* +// // * TEST LOGIC * +// // ********************************************************************************************* - oracle.set_primary_prices(market.long_token, 5000); - oracle.set_primary_prices(market.short_token, 1); +// // Create a market. +// let market = data_store.get_market(create_market(market_factory)); - 'fill the pool'.print(); - // Fill the pool. - IERC20Dispatcher { contract_address: market.long_token } - .mint(market.market_token, 50000000000000000000000000000000000000); // 5 ETH - IERC20Dispatcher { contract_address: market.short_token } - .mint(market.market_token, 25000000000000000000000000000000000000000); // 25000 USDC - 'filled pool 1'.print(); +// // Set params in data_store +// data_store.set_address(keys::fee_token(), market.index_token); +// data_store.set_u256(keys::max_swap_path_length(), 5); - IERC20Dispatcher { contract_address: market.long_token } - .mint(caller_address, 9999999999999000000); // 9.999 ETH - IERC20Dispatcher { contract_address: market.short_token } - .mint(caller_address, 49999999999999999000000); // 49.999 UDC - 'filled account'.print(); +// // Set max pool amount. +// data_store +// .set_u256( +// keys::max_pool_amount_key(market.market_token, market.long_token), +// 5000000000000000000000000000000000000000000 //500 000 ETH +// ); +// data_store +// .set_u256( +// keys::max_pool_amount_key(market.market_token, market.short_token), +// 2500000000000000000000000000000000000000000000 //250 000 000 USDC +// ); - // INITIAL LONG TOKEN IN POOL : 5 ETH - // INITIAL SHORT TOKEN IN POOL : 25000 USDC +// let factor_for_deposits: felt252 = keys::max_pnl_factor_for_deposits(); +// data_store +// .set_u256( +// keys::max_pnl_factor_key(factor_for_deposits, market.market_token, true), +// 50000000000000000000000000000000000000000000000 +// ); +// let factor_for_withdrawal: felt252 = keys::max_pnl_factor_for_withdrawals(); +// data_store +// .set_u256( +// keys::max_pnl_factor_key(factor_for_withdrawal, market.market_token, true), +// 50000000000000000000000000000000000000000000000 +// ); - // TODO Check why we don't need to set pool_amount_key - // // Set pool amount in data_store. - // let mut key = keys::pool_amount_key(market.market_token, contract_address_const::<'ETH'>()); +// oracle.set_primary_prices(market.long_token, 5000); +// oracle.set_primary_prices(market.short_token, 1); - let balance_deposit_vault_before = IERC20Dispatcher { contract_address: market.short_token } - .balance_of(deposit_vault.contract_address); - let balance_caller_ETH = IERC20Dispatcher { contract_address: market.long_token } - .balance_of(caller_address); - let balance_caller_USDC = IERC20Dispatcher { contract_address: market.short_token } - .balance_of(caller_address); +// 'fill the pool'.print(); +// // Fill the pool. +// IERC20Dispatcher { contract_address: market.long_token } +// .mint(market.market_token, 50000000000000000000000000000000000000); // 5 ETH +// IERC20Dispatcher { contract_address: market.short_token } +// .mint(market.market_token, 25000000000000000000000000000000000000000); // 25000 USDC +// 'filled pool 1'.print(); - assert(balance_deposit_vault_before == 0, 'balance deposit should be 0'); - assert(balance_caller_ETH == 10000000000000000000, 'balanc ETH should be 10 ETH'); - assert(balance_caller_USDC == 50000000000000000000000, 'USDC be 50 000 USDC'); +// IERC20Dispatcher { contract_address: market.long_token } +// .mint(caller_address, 9999999999999000000); // 9.999 ETH +// IERC20Dispatcher { contract_address: market.short_token } +// .mint(caller_address, 49999999999999999000000); // 49.999 UDC +// 'filled account'.print(); - // Send token to deposit in the deposit vault (this should be in a multi call with create_deposit) - 'get balances'.print(); - // start_prank(market.long_token, caller_address); - // IERC20Dispatcher { contract_address: market.long_token } - // .transfer(deposit_vault.contract_address, 5000000000000000000); // 5 ETH +// // INITIAL LONG TOKEN IN POOL : 5 ETH +// // INITIAL SHORT TOKEN IN POOL : 25000 USDC - // start_prank(market.short_token, caller_address); - // IERC20Dispatcher { contract_address: market.short_token } - // .transfer(deposit_vault.contract_address, 25000000000000000000000); // 25000 USDC - // 'make transfer'.print(); +// // TODO Check why we don't need to set pool_amount_key +// // // Set pool amount in data_store. +// // let mut key = keys::pool_amount_key(market.market_token, contract_address_const::<'ETH'>()); - IERC20Dispatcher { contract_address: market.long_token } - .mint(deposit_vault.contract_address, 50000000000000000000000000000); // 50 000 000 000 - IERC20Dispatcher { contract_address: market.short_token } - .mint(deposit_vault.contract_address, 50000000000000000000000000000); // 50 000 000 000 - // Create Deposit +// let balance_deposit_vault_before = IERC20Dispatcher { contract_address: market.short_token } +// .balance_of(deposit_vault.contract_address); +// let balance_caller_ETH = IERC20Dispatcher { contract_address: market.long_token } +// .balance_of(caller_address); +// let balance_caller_USDC = IERC20Dispatcher { contract_address: market.short_token } +// .balance_of(caller_address); - let addresss_zero: ContractAddress = 0.try_into().unwrap(); +// assert(balance_deposit_vault_before == 0, 'balance deposit should be 0'); +// assert(balance_caller_ETH == 10000000000000000000, 'balanc ETH should be 10 ETH'); +// assert(balance_caller_USDC == 50000000000000000000000, 'USDC be 50 000 USDC'); - let params = CreateDepositParams { - receiver: caller_address, - callback_contract: addresss_zero, - ui_fee_receiver: addresss_zero, - market: market.market_token, - initial_long_token: market.long_token, - initial_short_token: market.short_token, - long_token_swap_path: Array32Trait::::span32(@array![]), - short_token_swap_path: Array32Trait::::span32(@array![]), - min_market_tokens: 0, - execution_fee: 0, - callback_gas_limit: 0, - }; - 'create deposit'.print(); +// // Send token to deposit in the deposit vault (this should be in a multi call with create_deposit) +// 'get balances'.print(); +// // start_prank(market.long_token, caller_address); +// // IERC20Dispatcher { contract_address: market.long_token } +// // .transfer(deposit_vault.contract_address, 5000000000000000000); // 5 ETH - start_roll(deposit_handler.contract_address, 1910); - let key = deposit_handler.create_deposit(caller_address, params); - let first_deposit = data_store.get_deposit(key); +// // start_prank(market.short_token, caller_address); +// // IERC20Dispatcher { contract_address: market.short_token } +// // .transfer(deposit_vault.contract_address, 25000000000000000000000); // 25000 USDC +// // 'make transfer'.print(); - 'created deposit'.print(); +// IERC20Dispatcher { contract_address: market.long_token } +// .mint(deposit_vault.contract_address, 50000000000000000000000000000); // 50 000 000 000 +// IERC20Dispatcher { contract_address: market.short_token } +// .mint(deposit_vault.contract_address, 50000000000000000000000000000); // 50 000 000 000 +// // Create Deposit - assert(first_deposit.account == caller_address, 'Wrong account depositer'); - assert(first_deposit.receiver == caller_address, 'Wrong account receiver'); - assert(first_deposit.initial_long_token == market.long_token, 'Wrong initial long token'); - assert( - first_deposit.initial_long_token_amount == 50000000000000000000000000000, - 'Wrong initial long token amount' - ); - assert( - first_deposit.initial_short_token_amount == 50000000000000000000000000000, - 'Wrong init short token amount' - ); - - let price_params = SetPricesParams { // TODO - signer_info: 1, - tokens: array![contract_address_const::<'ETH'>(), contract_address_const::<'USDC'>()], - compacted_min_oracle_block_numbers: array![1900, 1900], - compacted_max_oracle_block_numbers: array![1910, 1910], - compacted_oracle_timestamps: array![9999, 9999], - compacted_decimals: array![18, 18], - compacted_min_prices: array![4294967346000000], // 50000000, 1000000 compacted - compacted_min_prices_indexes: array![0], - compacted_max_prices: array![4294967346000000], // 50000000, 1000000 compacted - compacted_max_prices_indexes: array![0], - signatures: array![ - array!['signatures1', 'signatures2'].span(), array!['signatures1', 'signatures2'].span() - ], - price_feed_tokens: array![] - }; +// let addresss_zero: ContractAddress = 0.try_into().unwrap(); - start_prank(role_store.contract_address, caller_address); +// let params = CreateDepositParams { +// receiver: caller_address, +// callback_contract: addresss_zero, +// ui_fee_receiver: addresss_zero, +// market: market.market_token, +// initial_long_token: market.long_token, +// initial_short_token: market.short_token, +// long_token_swap_path: Array32Trait::::span32(@array![]), +// short_token_swap_path: Array32Trait::::span32(@array![]), +// min_market_tokens: 0, +// execution_fee: 0, +// callback_gas_limit: 0, +// }; +// 'create deposit'.print(); - role_store.grant_role(caller_address, role::ORDER_KEEPER); - role_store.grant_role(caller_address, role::ROLE_ADMIN); - role_store.grant_role(caller_address, role::CONTROLLER); - role_store.grant_role(caller_address, role::MARKET_KEEPER); +// start_roll(deposit_handler.contract_address, 1910); +// let key = deposit_handler.create_deposit(caller_address, params); +// let first_deposit = data_store.get_deposit(key); - 'execute deposit'.print(); +// 'created deposit'.print(); - // Execute Deposit - start_roll(deposit_handler.contract_address, 1915); - deposit_handler.execute_deposit(key, price_params); +// assert(first_deposit.account == caller_address, 'Wrong account depositer'); +// assert(first_deposit.receiver == caller_address, 'Wrong account receiver'); +// assert(first_deposit.initial_long_token == market.long_token, 'Wrong initial long token'); +// assert( +// first_deposit.initial_long_token_amount == 50000000000000000000000000000, +// 'Wrong initial long token amount' +// ); +// assert( +// first_deposit.initial_short_token_amount == 50000000000000000000000000000, +// 'Wrong init short token amount' +// ); - 'executed deposit'.print(); +// let price_params = SetPricesParams { // TODO +// signer_info: 1, +// tokens: array![contract_address_const::<'ETH'>(), contract_address_const::<'USDC'>()], +// compacted_min_oracle_block_numbers: array![1900, 1900], +// compacted_max_oracle_block_numbers: array![1910, 1910], +// compacted_oracle_timestamps: array![9999, 9999], +// compacted_decimals: array![18, 18], +// compacted_min_prices: array![4294967346000000], // 50000000, 1000000 compacted +// compacted_min_prices_indexes: array![0], +// compacted_max_prices: array![4294967346000000], // 50000000, 1000000 compacted +// compacted_max_prices_indexes: array![0], +// signatures: array![ +// array!['signatures1', 'signatures2'].span(), array!['signatures1', 'signatures2'].span() +// ], +// price_feed_tokens: array![] +// }; - // let pool_value_info = market_utils::get_pool_value_info( - // data_store, - // market, - // Price { min: 2000, max: 2000 }, - // Price { min: 2000, max: 2000 }, - // Price { min: 2000, max: 2000 }, - // keys::max_pnl_factor_for_deposits(), - // true, - // ); +// start_prank(role_store.contract_address, caller_address); - // assert(pool_value_info.pool_value.mag == 42000000000000000000000, 'wrong pool value amount'); - // assert(pool_value_info.long_token_amount == 6000000000000000000, 'wrong long token amount'); - // assert(pool_value_info.short_token_amount == 30000000000000000000000, 'wrong short token amount'); +// role_store.grant_role(caller_address, role::ORDER_KEEPER); +// role_store.grant_role(caller_address, role::ROLE_ADMIN); +// role_store.grant_role(caller_address, role::CONTROLLER); +// role_store.grant_role(caller_address, role::MARKET_KEEPER); - let not_deposit = data_store.get_deposit(key); - let default_deposit: Deposit = Default::default(); - assert(not_deposit == default_deposit, 'Still existing deposit'); +// 'execute deposit'.print(); - let market_token_dispatcher = IMarketTokenDispatcher { contract_address: market.market_token }; - let balance_market_token = market_token_dispatcher.balance_of(caller_address); +// // Execute Deposit +// start_roll(deposit_handler.contract_address, 1915); +// deposit_handler.execute_deposit(key, price_params); - assert(balance_market_token != 0, 'should receive market token'); +// 'executed deposit'.print(); - let balance_deposit_vault_after = IERC20Dispatcher { contract_address: market.short_token } - .balance_of(deposit_vault.contract_address); +// // let pool_value_info = market_utils::get_pool_value_info( +// // data_store, +// // market, +// // Price { min: 2000, max: 2000 }, +// // Price { min: 2000, max: 2000 }, +// // Price { min: 2000, max: 2000 }, +// // keys::max_pnl_factor_for_deposits(), +// // true, +// // ); - // let pool_value_info = market_utils::get_pool_value_info( - // data_store, - // market, - // Price { min: 5000, max: 5000, }, - // Price { min: 5000, max: 5000, }, - // Price { min: 1, max: 1, }, - // keys::max_pnl_factor_for_deposits(), - // true, - // ); +// // assert(pool_value_info.pool_value.mag == 42000000000000000000000, 'wrong pool value amount'); +// // assert(pool_value_info.long_token_amount == 6000000000000000000, 'wrong long token amount'); +// // assert(pool_value_info.short_token_amount == 30000000000000000000000, 'wrong short token amount'); - // pool_value_info.pool_value.mag.print(); // 10000 000000000000000000 - // pool_value_info.long_token_amount.print(); // 5 000000000000000000 - // pool_value_info.short_token_amount.print(); // 25000 000000000000000000 +// let not_deposit = data_store.get_deposit(key); +// let default_deposit: Deposit = Default::default(); +// assert(not_deposit == default_deposit, 'Still existing deposit'); - // ************************************* TEST LONG ********************************************* +// let market_token_dispatcher = IMarketTokenDispatcher { contract_address: market.market_token }; +// let balance_market_token = market_token_dispatcher.balance_of(caller_address); - 'Begining of LONG TEST'.print(); +// assert(balance_market_token != 0, 'should receive market token'); - let key_open_interest = keys::open_interest_key( - market.market_token, contract_address_const::<'ETH'>(), true - ); - data_store.set_u256(key_open_interest, 1); - let max_key_open_interest = keys::max_open_interest_key(market.market_token, true); - data_store - .set_u256( - max_key_open_interest, 1000000000000000000000000000000000000000000000000000 - ); // 1 000 000 +// let balance_deposit_vault_after = IERC20Dispatcher { contract_address: market.short_token } +// .balance_of(deposit_vault.contract_address); - // Send token to order_vault in multicall with create_order - start_prank(contract_address_const::<'ETH'>(), caller_address); - IERC20Dispatcher { contract_address: contract_address_const::<'ETH'>() } - .transfer(order_vault.contract_address, 2000000000000000000); // 2ETH +// // let pool_value_info = market_utils::get_pool_value_info( +// // data_store, +// // market, +// // Price { min: 5000, max: 5000, }, +// // Price { min: 5000, max: 5000, }, +// // Price { min: 1, max: 1, }, +// // keys::max_pnl_factor_for_deposits(), +// // true, +// // ); - 'transfer made'.print(); - // Create order_params Struct - let contract_address = contract_address_const::<0>(); - start_prank(market.market_token, caller_address); - start_prank(market.long_token, caller_address); - let order_params_long = CreateOrderParams { - receiver: caller_address, - callback_contract: contract_address, - ui_fee_receiver: contract_address, - market: market.market_token, - initial_collateral_token: market.long_token, - swap_path: Array32Trait::::span32(@array![]), - size_delta_usd: 10000000000000000000000, - initial_collateral_delta_amount: 2000000000000000000, // 10^18 - trigger_price: 5000, - acceptable_price: 5500, - execution_fee: 0, - callback_gas_limit: 0, - min_output_amount: 0, - order_type: OrderType::MarketIncrease(()), - decrease_position_swap_type: DecreasePositionSwapType::NoSwap(()), - is_long: true, - referral_code: 0 - }; - // Create the swap order. - start_roll(order_handler.contract_address, 1930); - 'try to create prder'.print(); - start_prank(order_handler.contract_address, caller_address); - let key_long = order_handler.create_order(caller_address, order_params_long); - 'long created'.print(); - let got_order_long = data_store.get_order(key_long); - // data_store.set_u256(keys::pool_amount_key(market.market_token, contract_address_const::<'USDC'>()), ); - // data_store.set_u256(keys::pool_amount_key(market.market_token, contract_address_const::<'ETH'>()), 1000000); - // Execute the swap order. +// // pool_value_info.pool_value.mag.print(); // 10000 000000000000000000 +// // pool_value_info.long_token_amount.print(); // 5 000000000000000000 +// // pool_value_info.short_token_amount.print(); // 25000 000000000000000000 - data_store - .set_u256( - keys::pool_amount_key(market.market_token, contract_address_const::<'USDC'>()), - 50000000000000000000000000000 - ); - data_store - .set_u256( - keys::pool_amount_key(market.market_token, contract_address_const::<'ETH'>()), - 50000000000000000000000000000 - ); +// // ************************************* TEST LONG ********************************************* - let signatures: Span = array![0].span(); - let set_price_params = SetPricesParams { - signer_info: 2, - tokens: array![contract_address_const::<'ETH'>(), contract_address_const::<'USDC'>()], - compacted_min_oracle_block_numbers: array![1910, 1910], - compacted_max_oracle_block_numbers: array![1920, 1920], - compacted_oracle_timestamps: array![9999, 9999], - compacted_decimals: array![1, 1], - compacted_min_prices: array![2147483648010000], // 500000, 10000 compacted - compacted_min_prices_indexes: array![0], - compacted_max_prices: array![2147483648010000], // 500000, 10000 compacted - compacted_max_prices_indexes: array![0], - signatures: array![ - array!['signatures1', 'signatures2'].span(), array!['signatures1', 'signatures2'].span() - ], - price_feed_tokens: array![] - }; +// 'Begining of LONG TEST'.print(); - let keeper_address = contract_address_const::<'keeper'>(); - role_store.grant_role(keeper_address, role::ORDER_KEEPER); +// let key_open_interest = keys::open_interest_key( +// market.market_token, contract_address_const::<'ETH'>(), true +// ); +// data_store.set_u256(key_open_interest, 1); +// let max_key_open_interest = keys::max_open_interest_key(market.market_token, true); +// data_store +// .set_u256( +// max_key_open_interest, 1000000000000000000000000000000000000000000000000000 +// ); // 1 000 000 - stop_prank(order_handler.contract_address); - start_prank(order_handler.contract_address, keeper_address); - start_roll(order_handler.contract_address, 1935); - // TODO add real signatures check on Oracle Account - order_handler.execute_order_keeper(key_long, set_price_params, keeper_address); - 'long position SUCCEEDED'.print(); - let position_key = data_store.get_account_position_keys(caller_address, 0, 10); +// // Send token to order_vault in multicall with create_order +// start_prank(contract_address_const::<'ETH'>(), caller_address); +// IERC20Dispatcher { contract_address: contract_address_const::<'ETH'>() } +// .transfer(order_vault.contract_address, 2000000000000000000); // 2ETH - let position_key_1: felt252 = *position_key.at(0); - let first_position = data_store.get_position(position_key_1); - let market_prices = market_utils::MarketPrices { - index_token_price: Price { min: 8000, max: 8000, }, - long_token_price: Price { min: 8000, max: 8000, }, - short_token_price: Price { min: 1, max: 1, }, - }; - 'size tokens'.print(); - first_position.size_in_tokens.print(); - 'size in usd'.print(); - first_position.size_in_usd.print(); - 'OKAAAAAYYYYYY'.print(); - oracle.set_primary_prices(market.long_token, 6000); - let first_position_after_pump = data_store.get_position(position_key_1); - 'size tokens after pump'.print(); - first_position_after_pump.size_in_tokens.print(); - 'size in usd after pump'.print(); - first_position_after_pump.size_in_usd.print(); +// 'transfer made'.print(); +// // Create order_params Struct +// let contract_address = contract_address_const::<0>(); +// start_prank(market.market_token, caller_address); +// start_prank(market.long_token, caller_address); +// let order_params_long = CreateOrderParams { +// receiver: caller_address, +// callback_contract: contract_address, +// ui_fee_receiver: contract_address, +// market: market.market_token, +// initial_collateral_token: market.long_token, +// swap_path: Array32Trait::::span32(@array![]), +// size_delta_usd: 10000000000000000000000, +// initial_collateral_delta_amount: 2000000000000000000, // 10^18 +// trigger_price: 5000, +// acceptable_price: 5500, +// execution_fee: 0, +// callback_gas_limit: 0, +// min_output_amount: 0, +// order_type: OrderType::MarketIncrease(()), +// decrease_position_swap_type: DecreasePositionSwapType::NoSwap(()), +// is_long: true, +// referral_code: 0 +// }; +// // Create the swap order. +// start_roll(order_handler.contract_address, 1930); +// 'try to create prder'.print(); +// start_prank(order_handler.contract_address, caller_address); +// let key_long = order_handler.create_order(caller_address, order_params_long); +// 'long created'.print(); +// let got_order_long = data_store.get_order(key_long); +// // data_store.set_u256(keys::pool_amount_key(market.market_token, contract_address_const::<'USDC'>()), ); +// // data_store.set_u256(keys::pool_amount_key(market.market_token, contract_address_const::<'ETH'>()), 1000000); +// // Execute the swap order. - let position_info = reader - .get_position_info( - data_store, referal_storage, position_key_1, market_prices, 0, contract_address, true - ); - 'pnl'.print(); - position_info.base_pnl_usd.mag.print(); - - // let second_swap_pool_value_info = market_utils::get_pool_value_info( - // data_store, - // market, - // Price { min: 5000, max: 5000, }, - // Price { min: 5000, max: 5000, }, - // Price { min: 1, max: 1, }, - // keys::max_pnl_factor_for_deposits(), - // true, - // ); - - // second_swap_pool_value_info.pool_value.mag.print(); - // second_swap_pool_value_info.long_token_amount.print(); - // second_swap_pool_value_info.short_token_amount.print(); - // let (position_pnl_usd, uncapped_position_pnl_usd, size_delta_in_tokens) = - // position_utils::get_position_pnl_usd( - // data_store, market, market_prices, first_position, 5000 - // ); - // position_pnl_usd.mag.print(); - - //////////////////////////////////// CLOSING POSITION ////////////////////////////////////// - 'CLOOOOSE POSITION'.print(); - - let balance_of_mkt_before = IERC20Dispatcher { - contract_address: contract_address_const::<'USDC'>() - } - .balance_of(caller_address); - 'balance of mkt before'.print(); - balance_of_mkt_before.print(); - - start_prank(market.market_token, caller_address); - start_prank(market.long_token, caller_address); - let order_params_long_dec = CreateOrderParams { - receiver: caller_address, - callback_contract: contract_address, - ui_fee_receiver: contract_address, - market: market.market_token, - initial_collateral_token: market.long_token, - swap_path: Array32Trait::::span32(@array![market.market_token]), - size_delta_usd: 6000000000000000000000, // 6000 - initial_collateral_delta_amount: 1000000000000000000, // 1 ETH 10^18 - trigger_price: 6000, - acceptable_price: 6000, - execution_fee: 0, - callback_gas_limit: 0, - min_output_amount: 6000000000000000000000, // 6000 - order_type: OrderType::MarketDecrease(()), - decrease_position_swap_type: DecreasePositionSwapType::NoSwap(()), - is_long: true, - referral_code: 0 - }; - // Create the long order. - start_roll(order_handler.contract_address, 1940); - 'try to create order'.print(); - start_prank(order_handler.contract_address, caller_address); - let key_long_dec = order_handler.create_order(caller_address, order_params_long_dec); - 'long decrease created'.print(); - let got_order_long_dec = data_store.get_order(key_long_dec); - // data_store.set_u256(keys::pool_amount_key(market.market_token, contract_address_const::<'USDC'>()), ); - // data_store.set_u256(keys::pool_amount_key(market.market_token, contract_address_const::<'ETH'>()), 1000000); - // Execute the swap order. - - let signatures: Span = array![0].span(); - let set_price_params_dec = SetPricesParams { - signer_info: 2, - tokens: array![contract_address_const::<'ETH'>(), contract_address_const::<'USDC'>()], - compacted_min_oracle_block_numbers: array![1910, 1910], - compacted_max_oracle_block_numbers: array![1920, 1920], - compacted_oracle_timestamps: array![9999, 9999], - compacted_decimals: array![1, 1], - compacted_min_prices: array![2147483648010000], // 500000, 10000 compacted - compacted_min_prices_indexes: array![0], - compacted_max_prices: array![2147483648010000], // 500000, 10000 compacted - compacted_max_prices_indexes: array![0], - signatures: array![ - array!['signatures1', 'signatures2'].span(), array!['signatures1', 'signatures2'].span() - ], - price_feed_tokens: array![] - }; +// data_store +// .set_u256( +// keys::pool_amount_key(market.market_token, contract_address_const::<'USDC'>()), +// 50000000000000000000000000000 +// ); +// data_store +// .set_u256( +// keys::pool_amount_key(market.market_token, contract_address_const::<'ETH'>()), +// 50000000000000000000000000000 +// ); - let keeper_address = contract_address_const::<'keeper'>(); - role_store.grant_role(keeper_address, role::ORDER_KEEPER); +// let signatures: Span = array![0].span(); +// let set_price_params = SetPricesParams { +// signer_info: 2, +// tokens: array![contract_address_const::<'ETH'>(), contract_address_const::<'USDC'>()], +// compacted_min_oracle_block_numbers: array![1910, 1910], +// compacted_max_oracle_block_numbers: array![1920, 1920], +// compacted_oracle_timestamps: array![9999, 9999], +// compacted_decimals: array![1, 1], +// compacted_min_prices: array![2147483648010000], // 500000, 10000 compacted +// compacted_min_prices_indexes: array![0], +// compacted_max_prices: array![2147483648010000], // 500000, 10000 compacted +// compacted_max_prices_indexes: array![0], +// signatures: array![ +// array!['signatures1', 'signatures2'].span(), array!['signatures1', 'signatures2'].span() +// ], +// price_feed_tokens: array![] +// }; - stop_prank(order_handler.contract_address); - start_prank(order_handler.contract_address, keeper_address); - start_roll(order_handler.contract_address, 1945); - // TODO add real signatures check on Oracle Account - order_handler.execute_order_keeper(key_long_dec, set_price_params_dec, keeper_address); - 'long pos dec SUCCEEDED'.print(); +// let keeper_address = contract_address_const::<'keeper'>(); +// role_store.grant_role(keeper_address, role::ORDER_KEEPER); - let first_position_dec = data_store.get_position(position_key_1); +// stop_prank(order_handler.contract_address); +// start_prank(order_handler.contract_address, keeper_address); +// start_roll(order_handler.contract_address, 1935); +// // TODO add real signatures check on Oracle Account +// order_handler.execute_order_keeper(key_long, set_price_params, keeper_address); +// 'long position SUCCEEDED'.print(); +// let position_key = data_store.get_account_position_keys(caller_address, 0, 10); - 'size tokens before'.print(); - first_position.size_in_tokens.print(); - 'size in usd before'.print(); - first_position.size_in_usd.print(); +// let position_key_1: felt252 = *position_key.at(0); +// let first_position = data_store.get_position(position_key_1); +// let market_prices = market_utils::MarketPrices { +// index_token_price: Price { min: 8000, max: 8000, }, +// long_token_price: Price { min: 8000, max: 8000, }, +// short_token_price: Price { min: 1, max: 1, }, +// }; +// 'size tokens'.print(); +// first_position.size_in_tokens.print(); +// 'size in usd'.print(); +// first_position.size_in_usd.print(); +// 'OKAAAAAYYYYYY'.print(); +// oracle.set_primary_prices(market.long_token, 6000); +// let first_position_after_pump = data_store.get_position(position_key_1); +// 'size tokens after pump'.print(); +// first_position_after_pump.size_in_tokens.print(); +// 'size in usd after pump'.print(); +// first_position_after_pump.size_in_usd.print(); - 'size tokens'.print(); - first_position_dec.size_in_tokens.print(); - 'size in usd'.print(); - first_position_dec.size_in_usd.print(); +// let position_info = reader +// .get_position_info( +// data_store, referal_storage, position_key_1, market_prices, 0, contract_address, true +// ); +// 'pnl'.print(); +// position_info.base_pnl_usd.mag.print(); - let balance_of_mkt_after = IERC20Dispatcher { - contract_address: contract_address_const::<'USDC'>() - } - .balance_of(caller_address); - 'balance of mkt after'.print(); - balance_of_mkt_after.print(); +// // let second_swap_pool_value_info = market_utils::get_pool_value_info( +// // data_store, +// // market, +// // Price { min: 5000, max: 5000, }, +// // Price { min: 5000, max: 5000, }, +// // Price { min: 1, max: 1, }, +// // keys::max_pnl_factor_for_deposits(), +// // true, +// // ); + +// // second_swap_pool_value_info.pool_value.mag.print(); +// // second_swap_pool_value_info.long_token_amount.print(); +// // second_swap_pool_value_info.short_token_amount.print(); +// // let (position_pnl_usd, uncapped_position_pnl_usd, size_delta_in_tokens) = +// // position_utils::get_position_pnl_usd( +// // data_store, market, market_prices, first_position, 5000 +// // ); +// // position_pnl_usd.mag.print(); - /// close all position - oracle.set_primary_prices(market.long_token, 7000); +// //////////////////////////////////// CLOSING POSITION ////////////////////////////////////// +// 'CLOOOOSE POSITION'.print(); - start_prank(market.market_token, caller_address); - start_prank(market.long_token, caller_address); - let order_params_long_dec_2 = CreateOrderParams { - receiver: caller_address, - callback_contract: contract_address, - ui_fee_receiver: contract_address, - market: market.market_token, - initial_collateral_token: market.long_token, - swap_path: Array32Trait::::span32(@array![market.market_token]), - size_delta_usd: 7000000000000000000000, // 6000 - initial_collateral_delta_amount: 1000000000000000000, // 1 ETH 10^18 - trigger_price: 7000, - acceptable_price: 7000, - execution_fee: 0, - callback_gas_limit: 0, - min_output_amount: 7000000000000000000000, // 6000 - order_type: OrderType::MarketDecrease(()), - decrease_position_swap_type: DecreasePositionSwapType::NoSwap(()), - is_long: true, - referral_code: 0 - }; - // Create the long order. - start_roll(order_handler.contract_address, 1950); - 'try to create order'.print(); - start_prank(order_handler.contract_address, caller_address); - let key_long_dec_2 = order_handler.create_order(caller_address, order_params_long_dec_2); - 'long decrease created'.print(); - let got_order_long_dec = data_store.get_order(key_long_dec_2); - // data_store.set_u256(keys::pool_amount_key(market.market_token, contract_address_const::<'USDC'>()), ); - // data_store.set_u256(keys::pool_amount_key(market.market_token, contract_address_const::<'ETH'>()), 1000000); - // Execute the swap order. +// let balance_of_mkt_before = IERC20Dispatcher { +// contract_address: contract_address_const::<'USDC'>() +// } +// .balance_of(caller_address); +// 'balance of mkt before'.print(); +// balance_of_mkt_before.print(); - let keeper_address = contract_address_const::<'keeper'>(); - role_store.grant_role(keeper_address, role::ORDER_KEEPER); +// start_prank(market.market_token, caller_address); +// start_prank(market.long_token, caller_address); +// let order_params_long_dec = CreateOrderParams { +// receiver: caller_address, +// callback_contract: contract_address, +// ui_fee_receiver: contract_address, +// market: market.market_token, +// initial_collateral_token: market.long_token, +// swap_path: Array32Trait::::span32(@array![market.market_token]), +// size_delta_usd: 6000000000000000000000, // 6000 +// initial_collateral_delta_amount: 1000000000000000000, // 1 ETH 10^18 +// trigger_price: 6000, +// acceptable_price: 6000, +// execution_fee: 0, +// callback_gas_limit: 0, +// min_output_amount: 6000000000000000000000, // 6000 +// order_type: OrderType::MarketDecrease(()), +// decrease_position_swap_type: DecreasePositionSwapType::NoSwap(()), +// is_long: true, +// referral_code: 0 +// }; +// // Create the long order. +// start_roll(order_handler.contract_address, 1940); +// 'try to create order'.print(); +// start_prank(order_handler.contract_address, caller_address); +// let key_long_dec = order_handler.create_order(caller_address, order_params_long_dec); +// 'long decrease created'.print(); +// let got_order_long_dec = data_store.get_order(key_long_dec); +// // data_store.set_u256(keys::pool_amount_key(market.market_token, contract_address_const::<'USDC'>()), ); +// // data_store.set_u256(keys::pool_amount_key(market.market_token, contract_address_const::<'ETH'>()), 1000000); +// // Execute the swap order. - let signatures: Span = array![0].span(); - let set_price_params_dec2 = SetPricesParams { - signer_info: 2, - tokens: array![contract_address_const::<'ETH'>(), contract_address_const::<'USDC'>()], - compacted_min_oracle_block_numbers: array![1910, 1910], - compacted_max_oracle_block_numbers: array![1920, 1920], - compacted_oracle_timestamps: array![9999, 9999], - compacted_decimals: array![1, 1], - compacted_min_prices: array![2147483648010000], // 500000, 10000 compacted - compacted_min_prices_indexes: array![0], - compacted_max_prices: array![2147483648010000], // 500000, 10000 compacted - compacted_max_prices_indexes: array![0], - signatures: array![ - array!['signatures1', 'signatures2'].span(), array!['signatures1', 'signatures2'].span() - ], - price_feed_tokens: array![] - }; +// let signatures: Span = array![0].span(); +// let set_price_params_dec = SetPricesParams { +// signer_info: 2, +// tokens: array![contract_address_const::<'ETH'>(), contract_address_const::<'USDC'>()], +// compacted_min_oracle_block_numbers: array![1910, 1910], +// compacted_max_oracle_block_numbers: array![1920, 1920], +// compacted_oracle_timestamps: array![9999, 9999], +// compacted_decimals: array![1, 1], +// compacted_min_prices: array![2147483648010000], // 500000, 10000 compacted +// compacted_min_prices_indexes: array![0], +// compacted_max_prices: array![2147483648010000], // 500000, 10000 compacted +// compacted_max_prices_indexes: array![0], +// signatures: array![ +// array!['signatures1', 'signatures2'].span(), array!['signatures1', 'signatures2'].span() +// ], +// price_feed_tokens: array![] +// }; - stop_prank(order_handler.contract_address); - start_prank(order_handler.contract_address, keeper_address); - start_roll(order_handler.contract_address, 1955); - // TODO add real signatures check on Oracle Account - order_handler.execute_order_keeper(key_long_dec_2, set_price_params_dec2, keeper_address); - 'long pos dec SUCCEEDED'.print(); +// let keeper_address = contract_address_const::<'keeper'>(); +// role_store.grant_role(keeper_address, role::ORDER_KEEPER); - let first_position_dec = data_store.get_position(position_key_1); +// stop_prank(order_handler.contract_address); +// start_prank(order_handler.contract_address, keeper_address); +// start_roll(order_handler.contract_address, 1945); +// // TODO add real signatures check on Oracle Account +// order_handler.execute_order_keeper(key_long_dec, set_price_params_dec, keeper_address); +// 'long pos dec SUCCEEDED'.print(); - 'size tokens before 2'.print(); - first_position.size_in_tokens.print(); - 'size in usd before 2'.print(); - first_position.size_in_usd.print(); +// let first_position_dec = data_store.get_position(position_key_1); - 'size tokens 2'.print(); - let token_size_dec = first_position_dec.size_in_tokens; - assert(token_size_dec == 0, 'wrong token size'); - 'size in usd 2'.print(); - first_position_dec.size_in_usd.print(); +// 'size tokens before'.print(); +// first_position.size_in_tokens.print(); +// 'size in usd before'.print(); +// first_position.size_in_usd.print(); - let balance_of_mkt_after = IERC20Dispatcher { - contract_address: contract_address_const::<'USDC'>() - } - .balance_of(caller_address); - 'balance of mkt after 2'.print(); - balance_of_mkt_after.print(); +// 'size tokens'.print(); +// first_position_dec.size_in_tokens.print(); +// 'size in usd'.print(); +// first_position_dec.size_in_usd.print(); - assert(balance_of_mkt_after == 63000000000000000000000, 'wrong balance final size'); +// let balance_of_mkt_after = IERC20Dispatcher { +// contract_address: contract_address_const::<'USDC'>() +// } +// .balance_of(caller_address); +// 'balance of mkt after'.print(); +// balance_of_mkt_after.print(); - // ********************************************************************************************* - // * TEARDOWN * - // ********************************************************************************************* - teardown(data_store, market_factory); -} +// /// close all position +// oracle.set_primary_prices(market.long_token, 7000); -#[test] -fn test_long_18_close_integration() { - // ********************************************************************************************* - // * SETUP * - // ********************************************************************************************* - let ( - caller_address, - market_factory_address, - role_store_address, - data_store_address, - market_token_class_hash, - market_factory, - role_store, - data_store, - event_emitter, - exchange_router, - deposit_handler, - deposit_vault, - oracle, - order_handler, - order_vault, - reader, - referal_storage, - withdrawal_handler, - withdrawal_vault, - ) = - setup(); +// start_prank(market.market_token, caller_address); +// start_prank(market.long_token, caller_address); +// let order_params_long_dec_2 = CreateOrderParams { +// receiver: caller_address, +// callback_contract: contract_address, +// ui_fee_receiver: contract_address, +// market: market.market_token, +// initial_collateral_token: market.long_token, +// swap_path: Array32Trait::::span32(@array![market.market_token]), +// size_delta_usd: 7000000000000000000000, // 6000 +// initial_collateral_delta_amount: 1000000000000000000, // 1 ETH 10^18 +// trigger_price: 7000, +// acceptable_price: 7000, +// execution_fee: 0, +// callback_gas_limit: 0, +// min_output_amount: 7000000000000000000000, // 6000 +// order_type: OrderType::MarketDecrease(()), +// decrease_position_swap_type: DecreasePositionSwapType::NoSwap(()), +// is_long: true, +// referral_code: 0 +// }; +// // Create the long order. +// start_roll(order_handler.contract_address, 1950); +// 'try to create order'.print(); +// start_prank(order_handler.contract_address, caller_address); +// let key_long_dec_2 = order_handler.create_order(caller_address, order_params_long_dec_2); +// 'long decrease created'.print(); +// let got_order_long_dec = data_store.get_order(key_long_dec_2); +// // data_store.set_u256(keys::pool_amount_key(market.market_token, contract_address_const::<'USDC'>()), ); +// // data_store.set_u256(keys::pool_amount_key(market.market_token, contract_address_const::<'ETH'>()), 1000000); +// // Execute the swap order. - // ********************************************************************************************* - // * TEST LOGIC * - // ********************************************************************************************* +// let keeper_address = contract_address_const::<'keeper'>(); +// role_store.grant_role(keeper_address, role::ORDER_KEEPER); - // Create a market. - let market = data_store.get_market(create_market(market_factory)); +// let signatures: Span = array![0].span(); +// let set_price_params_dec2 = SetPricesParams { +// signer_info: 2, +// tokens: array![contract_address_const::<'ETH'>(), contract_address_const::<'USDC'>()], +// compacted_min_oracle_block_numbers: array![1910, 1910], +// compacted_max_oracle_block_numbers: array![1920, 1920], +// compacted_oracle_timestamps: array![9999, 9999], +// compacted_decimals: array![1, 1], +// compacted_min_prices: array![2147483648010000], // 500000, 10000 compacted +// compacted_min_prices_indexes: array![0], +// compacted_max_prices: array![2147483648010000], // 500000, 10000 compacted +// compacted_max_prices_indexes: array![0], +// signatures: array![ +// array!['signatures1', 'signatures2'].span(), array!['signatures1', 'signatures2'].span() +// ], +// price_feed_tokens: array![] +// }; - // Set params in data_store - data_store.set_address(keys::fee_token(), market.index_token); - data_store.set_u256(keys::max_swap_path_length(), 5); +// stop_prank(order_handler.contract_address); +// start_prank(order_handler.contract_address, keeper_address); +// start_roll(order_handler.contract_address, 1955); +// // TODO add real signatures check on Oracle Account +// order_handler.execute_order_keeper(key_long_dec_2, set_price_params_dec2, keeper_address); +// 'long pos dec SUCCEEDED'.print(); - // Set max pool amount. - data_store - .set_u256( - keys::max_pool_amount_key(market.market_token, market.long_token), - 5000000000000000000000000000000000000000000 //500 000 ETH - ); - data_store - .set_u256( - keys::max_pool_amount_key(market.market_token, market.short_token), - 2500000000000000000000000000000000000000000000 //250 000 000 USDC - ); +// let first_position_dec = data_store.get_position(position_key_1); - let factor_for_deposits: felt252 = keys::max_pnl_factor_for_deposits(); - data_store - .set_u256( - keys::max_pnl_factor_key(factor_for_deposits, market.market_token, true), - 50000000000000000000000000000000000000000000000 - ); - let factor_for_withdrawal: felt252 = keys::max_pnl_factor_for_withdrawals(); - data_store - .set_u256( - keys::max_pnl_factor_key(factor_for_withdrawal, market.market_token, true), - 50000000000000000000000000000000000000000000000 - ); +// 'size tokens before 2'.print(); +// first_position.size_in_tokens.print(); +// 'size in usd before 2'.print(); +// first_position.size_in_usd.print(); - oracle.set_primary_prices(market.long_token, 5000); - oracle.set_primary_prices(market.short_token, 1); +// 'size tokens 2'.print(); +// let token_size_dec = first_position_dec.size_in_tokens; +// assert(token_size_dec == 0, 'wrong token size'); +// 'size in usd 2'.print(); +// first_position_dec.size_in_usd.print(); - 'fill the pool'.print(); - // Fill the pool. - IERC20Dispatcher { contract_address: market.long_token } - .mint(market.market_token, 50000000000000000000000000000000000000); // 5 ETH - IERC20Dispatcher { contract_address: market.short_token } - .mint(market.market_token, 25000000000000000000000000000000000000000); // 25000 USDC - 'filled pool 1'.print(); +// let balance_of_mkt_after = IERC20Dispatcher { +// contract_address: contract_address_const::<'USDC'>() +// } +// .balance_of(caller_address); +// 'balance of mkt after 2'.print(); +// balance_of_mkt_after.print(); - IERC20Dispatcher { contract_address: market.long_token } - .mint(caller_address, 9999999999999000000); // 9.999 ETH - IERC20Dispatcher { contract_address: market.short_token } - .mint(caller_address, 49999999999999999000000); // 49.999 UDC - 'filled account'.print(); +// assert(balance_of_mkt_after == 63000000000000000000000, 'wrong balance final size'); - // INITIAL LONG TOKEN IN POOL : 5 ETH - // INITIAL SHORT TOKEN IN POOL : 25000 USDC +// // ********************************************************************************************* +// // * TEARDOWN * +// // ********************************************************************************************* +// teardown(data_store, market_factory); +// } - // TODO Check why we don't need to set pool_amount_key - // // Set pool amount in data_store. - // let mut key = keys::pool_amount_key(market.market_token, contract_address_const::<'ETH'>()); +// #[test] +// fn test_long_18_close_integration() { +// // ********************************************************************************************* +// // * SETUP * +// // ********************************************************************************************* +// let ( +// caller_address, +// market_factory_address, +// role_store_address, +// data_store_address, +// market_token_class_hash, +// market_factory, +// role_store, +// data_store, +// event_emitter, +// exchange_router, +// deposit_handler, +// deposit_vault, +// oracle, +// order_handler, +// order_vault, +// reader, +// referal_storage, +// withdrawal_handler, +// withdrawal_vault, +// ) = +// setup(); - let balance_deposit_vault_before = IERC20Dispatcher { contract_address: market.short_token } - .balance_of(deposit_vault.contract_address); - let balance_caller_ETH = IERC20Dispatcher { contract_address: market.long_token } - .balance_of(caller_address); - let balance_caller_USDC = IERC20Dispatcher { contract_address: market.short_token } - .balance_of(caller_address); +// // ********************************************************************************************* +// // * TEST LOGIC * +// // ********************************************************************************************* - assert(balance_deposit_vault_before == 0, 'balance deposit should be 0'); - assert(balance_caller_ETH == 10000000000000000000, 'balanc ETH should be 10 ETH'); - assert(balance_caller_USDC == 50000000000000000000000, 'USDC be 50 000 USDC'); +// // Create a market. +// let market = data_store.get_market(create_market(market_factory)); - // Send token to deposit in the deposit vault (this should be in a multi call with create_deposit) - 'get balances'.print(); - // start_prank(market.long_token, caller_address); - // IERC20Dispatcher { contract_address: market.long_token } - // .transfer(deposit_vault.contract_address, 5000000000000000000); // 5 ETH +// // Set params in data_store +// data_store.set_address(keys::fee_token(), market.index_token); +// data_store.set_u256(keys::max_swap_path_length(), 5); - // start_prank(market.short_token, caller_address); - // IERC20Dispatcher { contract_address: market.short_token } - // .transfer(deposit_vault.contract_address, 25000000000000000000000); // 25000 USDC - // 'make transfer'.print(); +// // Set max pool amount. +// data_store +// .set_u256( +// keys::max_pool_amount_key(market.market_token, market.long_token), +// 5000000000000000000000000000000000000000000 //500 000 ETH +// ); +// data_store +// .set_u256( +// keys::max_pool_amount_key(market.market_token, market.short_token), +// 2500000000000000000000000000000000000000000000 //250 000 000 USDC +// ); - IERC20Dispatcher { contract_address: market.long_token } - .mint(deposit_vault.contract_address, 50000000000000000000000000000); // 50 000 000 000 - IERC20Dispatcher { contract_address: market.short_token } - .mint(deposit_vault.contract_address, 50000000000000000000000000000); // 50 000 000 000 - // Create Deposit +// let factor_for_deposits: felt252 = keys::max_pnl_factor_for_deposits(); +// data_store +// .set_u256( +// keys::max_pnl_factor_key(factor_for_deposits, market.market_token, true), +// 50000000000000000000000000000000000000000000000 +// ); +// let factor_for_withdrawal: felt252 = keys::max_pnl_factor_for_withdrawals(); +// data_store +// .set_u256( +// keys::max_pnl_factor_key(factor_for_withdrawal, market.market_token, true), +// 50000000000000000000000000000000000000000000000 +// ); - let addresss_zero: ContractAddress = 0.try_into().unwrap(); +// oracle.set_primary_prices(market.long_token, 5000); +// oracle.set_primary_prices(market.short_token, 1); - let params = CreateDepositParams { - receiver: caller_address, - callback_contract: addresss_zero, - ui_fee_receiver: addresss_zero, - market: market.market_token, - initial_long_token: market.long_token, - initial_short_token: market.short_token, - long_token_swap_path: Array32Trait::::span32(@array![]), - short_token_swap_path: Array32Trait::::span32(@array![]), - min_market_tokens: 0, - execution_fee: 0, - callback_gas_limit: 0, - }; - 'create deposit'.print(); +// 'fill the pool'.print(); +// // Fill the pool. +// IERC20Dispatcher { contract_address: market.long_token } +// .mint(market.market_token, 50000000000000000000000000000000000000); // 5 ETH +// IERC20Dispatcher { contract_address: market.short_token } +// .mint(market.market_token, 25000000000000000000000000000000000000000); // 25000 USDC +// 'filled pool 1'.print(); - start_roll(deposit_handler.contract_address, 1910); - let key = deposit_handler.create_deposit(caller_address, params); - let first_deposit = data_store.get_deposit(key); +// IERC20Dispatcher { contract_address: market.long_token } +// .mint(caller_address, 9999999999999000000); // 9.999 ETH +// IERC20Dispatcher { contract_address: market.short_token } +// .mint(caller_address, 49999999999999999000000); // 49.999 UDC +// 'filled account'.print(); - 'created deposit'.print(); +// // INITIAL LONG TOKEN IN POOL : 5 ETH +// // INITIAL SHORT TOKEN IN POOL : 25000 USDC - assert(first_deposit.account == caller_address, 'Wrong account depositer'); - assert(first_deposit.receiver == caller_address, 'Wrong account receiver'); - assert(first_deposit.initial_long_token == market.long_token, 'Wrong initial long token'); - assert( - first_deposit.initial_long_token_amount == 50000000000000000000000000000, - 'Wrong initial long token amount' - ); - assert( - first_deposit.initial_short_token_amount == 50000000000000000000000000000, - 'Wrong init short token amount' - ); +// // TODO Check why we don't need to set pool_amount_key +// // // Set pool amount in data_store. +// // let mut key = keys::pool_amount_key(market.market_token, contract_address_const::<'ETH'>()); - let price_params = SetPricesParams { // TODO - signer_info: 1, - tokens: array![contract_address_const::<'ETH'>(), contract_address_const::<'USDC'>()], - compacted_min_oracle_block_numbers: array![1900, 1900], - compacted_max_oracle_block_numbers: array![1910, 1910], - compacted_oracle_timestamps: array![9999, 9999], - compacted_decimals: array![18, 18], - compacted_min_prices: array![4294967346000000], // 50000000, 1000000 compacted - compacted_min_prices_indexes: array![0], - compacted_max_prices: array![4294967346000000], // 50000000, 1000000 compacted - compacted_max_prices_indexes: array![0], - signatures: array![ - array!['signatures1', 'signatures2'].span(), array!['signatures1', 'signatures2'].span() - ], - price_feed_tokens: array![] - }; - - start_prank(role_store.contract_address, caller_address); +// let balance_deposit_vault_before = IERC20Dispatcher { contract_address: market.short_token } +// .balance_of(deposit_vault.contract_address); +// let balance_caller_ETH = IERC20Dispatcher { contract_address: market.long_token } +// .balance_of(caller_address); +// let balance_caller_USDC = IERC20Dispatcher { contract_address: market.short_token } +// .balance_of(caller_address); - role_store.grant_role(caller_address, role::ORDER_KEEPER); - role_store.grant_role(caller_address, role::ROLE_ADMIN); - role_store.grant_role(caller_address, role::CONTROLLER); - role_store.grant_role(caller_address, role::MARKET_KEEPER); +// assert(balance_deposit_vault_before == 0, 'balance deposit should be 0'); +// assert(balance_caller_ETH == 10000000000000000000, 'balanc ETH should be 10 ETH'); +// assert(balance_caller_USDC == 50000000000000000000000, 'USDC be 50 000 USDC'); - 'execute deposit'.print(); +// // Send token to deposit in the deposit vault (this should be in a multi call with create_deposit) +// 'get balances'.print(); +// // start_prank(market.long_token, caller_address); +// // IERC20Dispatcher { contract_address: market.long_token } +// // .transfer(deposit_vault.contract_address, 5000000000000000000); // 5 ETH - // Execute Deposit - start_roll(deposit_handler.contract_address, 1915); - deposit_handler.execute_deposit(key, price_params); +// // start_prank(market.short_token, caller_address); +// // IERC20Dispatcher { contract_address: market.short_token } +// // .transfer(deposit_vault.contract_address, 25000000000000000000000); // 25000 USDC +// // 'make transfer'.print(); - 'executed deposit'.print(); +// IERC20Dispatcher { contract_address: market.long_token } +// .mint(deposit_vault.contract_address, 50000000000000000000000000000); // 50 000 000 000 +// IERC20Dispatcher { contract_address: market.short_token } +// .mint(deposit_vault.contract_address, 50000000000000000000000000000); // 50 000 000 000 +// // Create Deposit - // let pool_value_info = market_utils::get_pool_value_info( - // data_store, - // market, - // Price { min: 2000, max: 2000 }, - // Price { min: 2000, max: 2000 }, - // Price { min: 2000, max: 2000 }, - // keys::max_pnl_factor_for_deposits(), - // true, - // ); +// let addresss_zero: ContractAddress = 0.try_into().unwrap(); - // assert(pool_value_info.pool_value.mag == 42000000000000000000000, 'wrong pool value amount'); - // assert(pool_value_info.long_token_amount == 6000000000000000000, 'wrong long token amount'); - // assert(pool_value_info.short_token_amount == 30000000000000000000000, 'wrong short token amount'); +// let params = CreateDepositParams { +// receiver: caller_address, +// callback_contract: addresss_zero, +// ui_fee_receiver: addresss_zero, +// market: market.market_token, +// initial_long_token: market.long_token, +// initial_short_token: market.short_token, +// long_token_swap_path: Array32Trait::::span32(@array![]), +// short_token_swap_path: Array32Trait::::span32(@array![]), +// min_market_tokens: 0, +// execution_fee: 0, +// callback_gas_limit: 0, +// }; +// 'create deposit'.print(); - let not_deposit = data_store.get_deposit(key); - let default_deposit: Deposit = Default::default(); - assert(not_deposit == default_deposit, 'Still existing deposit'); +// start_roll(deposit_handler.contract_address, 1910); +// let key = deposit_handler.create_deposit(caller_address, params); +// let first_deposit = data_store.get_deposit(key); - let market_token_dispatcher = IMarketTokenDispatcher { contract_address: market.market_token }; - let balance_market_token = market_token_dispatcher.balance_of(caller_address); +// 'created deposit'.print(); - assert(balance_market_token != 0, 'should receive market token'); +// assert(first_deposit.account == caller_address, 'Wrong account depositer'); +// assert(first_deposit.receiver == caller_address, 'Wrong account receiver'); +// assert(first_deposit.initial_long_token == market.long_token, 'Wrong initial long token'); +// assert( +// first_deposit.initial_long_token_amount == 50000000000000000000000000000, +// 'Wrong initial long token amount' +// ); +// assert( +// first_deposit.initial_short_token_amount == 50000000000000000000000000000, +// 'Wrong init short token amount' +// ); - let balance_deposit_vault_after = IERC20Dispatcher { contract_address: market.short_token } - .balance_of(deposit_vault.contract_address); +// let price_params = SetPricesParams { // TODO +// signer_info: 1, +// tokens: array![contract_address_const::<'ETH'>(), contract_address_const::<'USDC'>()], +// compacted_min_oracle_block_numbers: array![1900, 1900], +// compacted_max_oracle_block_numbers: array![1910, 1910], +// compacted_oracle_timestamps: array![9999, 9999], +// compacted_decimals: array![18, 18], +// compacted_min_prices: array![4294967346000000], // 50000000, 1000000 compacted +// compacted_min_prices_indexes: array![0], +// compacted_max_prices: array![4294967346000000], // 50000000, 1000000 compacted +// compacted_max_prices_indexes: array![0], +// signatures: array![ +// array!['signatures1', 'signatures2'].span(), array!['signatures1', 'signatures2'].span() +// ], +// price_feed_tokens: array![] +// }; - // let pool_value_info = market_utils::get_pool_value_info( - // data_store, - // market, - // Price { min: 5000, max: 5000, }, - // Price { min: 5000, max: 5000, }, - // Price { min: 1, max: 1, }, - // keys::max_pnl_factor_for_deposits(), - // true, - // ); +// start_prank(role_store.contract_address, caller_address); - // pool_value_info.pool_value.mag.print(); // 10000 000000000000000000 - // pool_value_info.long_token_amount.print(); // 5 000000000000000000 - // pool_value_info.short_token_amount.print(); // 25000 000000000000000000 +// role_store.grant_role(caller_address, role::ORDER_KEEPER); +// role_store.grant_role(caller_address, role::ROLE_ADMIN); +// role_store.grant_role(caller_address, role::CONTROLLER); +// role_store.grant_role(caller_address, role::MARKET_KEEPER); - // ************************************* TEST LONG ********************************************* +// 'execute deposit'.print(); - 'Begining of LONG TEST'.print(); +// // Execute Deposit +// start_roll(deposit_handler.contract_address, 1915); +// deposit_handler.execute_deposit(key, price_params); - let key_open_interest = keys::open_interest_key( - market.market_token, contract_address_const::<'ETH'>(), true - ); - data_store.set_u256(key_open_interest, 1); - let max_key_open_interest = keys::max_open_interest_key(market.market_token, true); - data_store - .set_u256( - max_key_open_interest, 1000000000000000000000000000000000000000000000000000 - ); // 1 000 000 +// 'executed deposit'.print(); - // Send token to order_vault in multicall with create_order - start_prank(contract_address_const::<'ETH'>(), caller_address); - IERC20Dispatcher { contract_address: contract_address_const::<'ETH'>() } - .transfer(order_vault.contract_address, 2000000000000000000); // 2ETH +// // let pool_value_info = market_utils::get_pool_value_info( +// // data_store, +// // market, +// // Price { min: 2000, max: 2000 }, +// // Price { min: 2000, max: 2000 }, +// // Price { min: 2000, max: 2000 }, +// // keys::max_pnl_factor_for_deposits(), +// // true, +// // ); - 'transfer made'.print(); - // Create order_params Struct - let contract_address = contract_address_const::<0>(); - start_prank(market.market_token, caller_address); - start_prank(market.long_token, caller_address); - let order_params_long = CreateOrderParams { - receiver: caller_address, - callback_contract: contract_address, - ui_fee_receiver: contract_address, - market: market.market_token, - initial_collateral_token: market.long_token, - swap_path: Array32Trait::::span32(@array![]), - size_delta_usd: 10000000000000000000000, - initial_collateral_delta_amount: 2000000000000000000, // 10^18 - trigger_price: 5000, - acceptable_price: 5500, - execution_fee: 0, - callback_gas_limit: 0, - min_output_amount: 0, - order_type: OrderType::MarketIncrease(()), - decrease_position_swap_type: DecreasePositionSwapType::NoSwap(()), - is_long: true, - referral_code: 0 - }; - // Create the swap order. - start_roll(order_handler.contract_address, 1930); - 'try to create prder'.print(); - start_prank(order_handler.contract_address, caller_address); - let key_long = order_handler.create_order(caller_address, order_params_long); - 'long created'.print(); - let got_order_long = data_store.get_order(key_long); - // data_store.set_u256(keys::pool_amount_key(market.market_token, contract_address_const::<'USDC'>()), ); - // data_store.set_u256(keys::pool_amount_key(market.market_token, contract_address_const::<'ETH'>()), 1000000); - // Execute the swap order. +// // assert(pool_value_info.pool_value.mag == 42000000000000000000000, 'wrong pool value amount'); +// // assert(pool_value_info.long_token_amount == 6000000000000000000, 'wrong long token amount'); +// // assert(pool_value_info.short_token_amount == 30000000000000000000000, 'wrong short token amount'); - data_store - .set_u256( - keys::pool_amount_key(market.market_token, contract_address_const::<'USDC'>()), - 50000000000000000000000000000 - ); - data_store - .set_u256( - keys::pool_amount_key(market.market_token, contract_address_const::<'ETH'>()), - 50000000000000000000000000000 - ); +// let not_deposit = data_store.get_deposit(key); +// let default_deposit: Deposit = Default::default(); +// assert(not_deposit == default_deposit, 'Still existing deposit'); - let signatures: Span = array![0].span(); - let set_price_params = SetPricesParams { - signer_info: 2, - tokens: array![contract_address_const::<'ETH'>(), contract_address_const::<'USDC'>()], - compacted_min_oracle_block_numbers: array![1910, 1910], - compacted_max_oracle_block_numbers: array![1920, 1920], - compacted_oracle_timestamps: array![9999, 9999], - compacted_decimals: array![1, 1], - compacted_min_prices: array![2147483648010000], // 500000, 10000 compacted - compacted_min_prices_indexes: array![0], - compacted_max_prices: array![2147483648010000], // 500000, 10000 compacted - compacted_max_prices_indexes: array![0], - signatures: array![ - array!['signatures1', 'signatures2'].span(), array!['signatures1', 'signatures2'].span() - ], - price_feed_tokens: array![] - }; +// let market_token_dispatcher = IMarketTokenDispatcher { contract_address: market.market_token }; +// let balance_market_token = market_token_dispatcher.balance_of(caller_address); - let keeper_address = contract_address_const::<'keeper'>(); - role_store.grant_role(keeper_address, role::ORDER_KEEPER); +// assert(balance_market_token != 0, 'should receive market token'); - stop_prank(order_handler.contract_address); - start_prank(order_handler.contract_address, keeper_address); - start_roll(order_handler.contract_address, 1935); - // TODO add real signatures check on Oracle Account - order_handler.execute_order_keeper(key_long, set_price_params, keeper_address); - 'long position SUCCEEDED'.print(); - let position_key = data_store.get_account_position_keys(caller_address, 0, 10); +// let balance_deposit_vault_after = IERC20Dispatcher { contract_address: market.short_token } +// .balance_of(deposit_vault.contract_address); - let position_key_1: felt252 = *position_key.at(0); - let first_position = data_store.get_position(position_key_1); - let market_prices = market_utils::MarketPrices { - index_token_price: Price { min: 8000, max: 8000, }, - long_token_price: Price { min: 8000, max: 8000, }, - short_token_price: Price { min: 1, max: 1, }, - }; - 'size tokens'.print(); - first_position.size_in_tokens.print(); - 'size in usd'.print(); - first_position.size_in_usd.print(); - 'OKAAAAAYYYYYY'.print(); - oracle.set_primary_prices(market.long_token, 6000); - let first_position_after_pump = data_store.get_position(position_key_1); - 'size tokens after pump'.print(); - first_position_after_pump.size_in_tokens.print(); - 'size in usd after pump'.print(); - first_position_after_pump.size_in_usd.print(); +// // let pool_value_info = market_utils::get_pool_value_info( +// // data_store, +// // market, +// // Price { min: 5000, max: 5000, }, +// // Price { min: 5000, max: 5000, }, +// // Price { min: 1, max: 1, }, +// // keys::max_pnl_factor_for_deposits(), +// // true, +// // ); - let position_info = reader - .get_position_info( - data_store, referal_storage, position_key_1, market_prices, 0, contract_address, true - ); - 'pnl'.print(); - position_info.base_pnl_usd.mag.print(); +// // pool_value_info.pool_value.mag.print(); // 10000 000000000000000000 +// // pool_value_info.long_token_amount.print(); // 5 000000000000000000 +// // pool_value_info.short_token_amount.print(); // 25000 000000000000000000 - // let second_swap_pool_value_info = market_utils::get_pool_value_info( - // data_store, - // market, - // Price { min: 5000, max: 5000, }, - // Price { min: 5000, max: 5000, }, - // Price { min: 1, max: 1, }, - // keys::max_pnl_factor_for_deposits(), - // true, - // ); +// // ************************************* TEST LONG ********************************************* - // second_swap_pool_value_info.pool_value.mag.print(); - // second_swap_pool_value_info.long_token_amount.print(); - // second_swap_pool_value_info.short_token_amount.print(); - // let (position_pnl_usd, uncapped_position_pnl_usd, size_delta_in_tokens) = - // position_utils::get_position_pnl_usd( - // data_store, market, market_prices, first_position, 5000 - // ); - // position_pnl_usd.mag.print(); +// 'Begining of LONG TEST'.print(); - //////////////////////////////////// CLOSING POSITION ////////////////////////////////////// - 'CLOOOOSE POSITION'.print(); +// let key_open_interest = keys::open_interest_key( +// market.market_token, contract_address_const::<'ETH'>(), true +// ); +// data_store.set_u256(key_open_interest, 1); +// let max_key_open_interest = keys::max_open_interest_key(market.market_token, true); +// data_store +// .set_u256( +// max_key_open_interest, 1000000000000000000000000000000000000000000000000000 +// ); // 1 000 000 - let balance_of_mkt_before = IERC20Dispatcher { - contract_address: contract_address_const::<'USDC'>() - } - .balance_of(caller_address); - 'balance of mkt before'.print(); - balance_of_mkt_before.print(); +// // Send token to order_vault in multicall with create_order +// start_prank(contract_address_const::<'ETH'>(), caller_address); +// IERC20Dispatcher { contract_address: contract_address_const::<'ETH'>() } +// .transfer(order_vault.contract_address, 2000000000000000000); // 2ETH - start_prank(market.market_token, caller_address); - start_prank(market.long_token, caller_address); - let order_params_long_dec = CreateOrderParams { - receiver: caller_address, - callback_contract: contract_address, - ui_fee_receiver: contract_address, - market: market.market_token, - initial_collateral_token: market.long_token, - swap_path: Array32Trait::::span32(@array![market.market_token]), - size_delta_usd: 12000000000000000000000, // 12000 - initial_collateral_delta_amount: 2000000000000000000, // 2 ETH 10^18 - trigger_price: 6000, - acceptable_price: 6000, - execution_fee: 0, - callback_gas_limit: 0, - min_output_amount: 12000000000000000000000, // 12000 - order_type: OrderType::MarketDecrease(()), - decrease_position_swap_type: DecreasePositionSwapType::NoSwap(()), - is_long: true, - referral_code: 0 - }; - // Create the long order. - start_roll(order_handler.contract_address, 1940); - 'try to create order'.print(); - start_prank(order_handler.contract_address, caller_address); - let key_long_dec = order_handler.create_order(caller_address, order_params_long_dec); - 'long decrease created'.print(); - let got_order_long_dec = data_store.get_order(key_long_dec); - // data_store.set_u256(keys::pool_amount_key(market.market_token, contract_address_const::<'USDC'>()), ); - // data_store.set_u256(keys::pool_amount_key(market.market_token, contract_address_const::<'ETH'>()), 1000000); - // Execute the swap order. +// 'transfer made'.print(); +// // Create order_params Struct +// let contract_address = contract_address_const::<0>(); +// start_prank(market.market_token, caller_address); +// start_prank(market.long_token, caller_address); +// let order_params_long = CreateOrderParams { +// receiver: caller_address, +// callback_contract: contract_address, +// ui_fee_receiver: contract_address, +// market: market.market_token, +// initial_collateral_token: market.long_token, +// swap_path: Array32Trait::::span32(@array![]), +// size_delta_usd: 10000000000000000000000, +// initial_collateral_delta_amount: 2000000000000000000, // 10^18 +// trigger_price: 5000, +// acceptable_price: 5500, +// execution_fee: 0, +// callback_gas_limit: 0, +// min_output_amount: 0, +// order_type: OrderType::MarketIncrease(()), +// decrease_position_swap_type: DecreasePositionSwapType::NoSwap(()), +// is_long: true, +// referral_code: 0 +// }; +// // Create the swap order. +// start_roll(order_handler.contract_address, 1930); +// 'try to create prder'.print(); +// start_prank(order_handler.contract_address, caller_address); +// let key_long = order_handler.create_order(caller_address, order_params_long); +// 'long created'.print(); +// let got_order_long = data_store.get_order(key_long); +// // data_store.set_u256(keys::pool_amount_key(market.market_token, contract_address_const::<'USDC'>()), ); +// // data_store.set_u256(keys::pool_amount_key(market.market_token, contract_address_const::<'ETH'>()), 1000000); +// // Execute the swap order. - let signatures: Span = array![0].span(); - let set_price_params_dec = SetPricesParams { - signer_info: 2, - tokens: array![contract_address_const::<'ETH'>(), contract_address_const::<'USDC'>()], - compacted_min_oracle_block_numbers: array![1910, 1910], - compacted_max_oracle_block_numbers: array![1920, 1920], - compacted_oracle_timestamps: array![9999, 9999], - compacted_decimals: array![1, 1], - compacted_min_prices: array![2147483648010000], // 500000, 10000 compacted - compacted_min_prices_indexes: array![0], - compacted_max_prices: array![2147483648010000], // 500000, 10000 compacted - compacted_max_prices_indexes: array![0], - signatures: array![ - array!['signatures1', 'signatures2'].span(), array!['signatures1', 'signatures2'].span() - ], - price_feed_tokens: array![] - }; +// data_store +// .set_u256( +// keys::pool_amount_key(market.market_token, contract_address_const::<'USDC'>()), +// 50000000000000000000000000000 +// ); +// data_store +// .set_u256( +// keys::pool_amount_key(market.market_token, contract_address_const::<'ETH'>()), +// 50000000000000000000000000000 +// ); - let keeper_address = contract_address_const::<'keeper'>(); - role_store.grant_role(keeper_address, role::ORDER_KEEPER); +// let signatures: Span = array![0].span(); +// let set_price_params = SetPricesParams { +// signer_info: 2, +// tokens: array![contract_address_const::<'ETH'>(), contract_address_const::<'USDC'>()], +// compacted_min_oracle_block_numbers: array![1910, 1910], +// compacted_max_oracle_block_numbers: array![1920, 1920], +// compacted_oracle_timestamps: array![9999, 9999], +// compacted_decimals: array![1, 1], +// compacted_min_prices: array![2147483648010000], // 500000, 10000 compacted +// compacted_min_prices_indexes: array![0], +// compacted_max_prices: array![2147483648010000], // 500000, 10000 compacted +// compacted_max_prices_indexes: array![0], +// signatures: array![ +// array!['signatures1', 'signatures2'].span(), array!['signatures1', 'signatures2'].span() +// ], +// price_feed_tokens: array![] +// }; - stop_prank(order_handler.contract_address); - start_prank(order_handler.contract_address, keeper_address); - start_roll(order_handler.contract_address, 1945); - // TODO add real signatures check on Oracle Account - order_handler.execute_order_keeper(key_long_dec, set_price_params_dec, keeper_address); - 'long pos dec SUCCEEDED'.print(); +// let keeper_address = contract_address_const::<'keeper'>(); +// role_store.grant_role(keeper_address, role::ORDER_KEEPER); - let first_position_dec = data_store.get_position(position_key_1); +// stop_prank(order_handler.contract_address); +// start_prank(order_handler.contract_address, keeper_address); +// start_roll(order_handler.contract_address, 1935); +// // TODO add real signatures check on Oracle Account +// order_handler.execute_order_keeper(key_long, set_price_params, keeper_address); +// 'long position SUCCEEDED'.print(); +// let position_key = data_store.get_account_position_keys(caller_address, 0, 10); - 'size tokens before'.print(); - first_position.size_in_tokens.print(); - 'size in usd before'.print(); - first_position.size_in_usd.print(); +// let position_key_1: felt252 = *position_key.at(0); +// let first_position = data_store.get_position(position_key_1); +// let market_prices = market_utils::MarketPrices { +// index_token_price: Price { min: 8000, max: 8000, }, +// long_token_price: Price { min: 8000, max: 8000, }, +// short_token_price: Price { min: 1, max: 1, }, +// }; +// 'size tokens'.print(); +// first_position.size_in_tokens.print(); +// 'size in usd'.print(); +// first_position.size_in_usd.print(); +// 'OKAAAAAYYYYYY'.print(); +// oracle.set_primary_prices(market.long_token, 6000); +// let first_position_after_pump = data_store.get_position(position_key_1); +// 'size tokens after pump'.print(); +// first_position_after_pump.size_in_tokens.print(); +// 'size in usd after pump'.print(); +// first_position_after_pump.size_in_usd.print(); - 'size tokens'.print(); - first_position_dec.size_in_tokens.print(); - 'size in usd'.print(); - first_position_dec.size_in_usd.print(); +// let position_info = reader +// .get_position_info( +// data_store, referal_storage, position_key_1, market_prices, 0, contract_address, true +// ); +// 'pnl'.print(); +// position_info.base_pnl_usd.mag.print(); - let balance_of_mkt_after = IERC20Dispatcher { - contract_address: contract_address_const::<'USDC'>() - } - .balance_of(caller_address); - 'balance of mkt after'.print(); - balance_of_mkt_after.print(); +// // let second_swap_pool_value_info = market_utils::get_pool_value_info( +// // data_store, +// // market, +// // Price { min: 5000, max: 5000, }, +// // Price { min: 5000, max: 5000, }, +// // Price { min: 1, max: 1, }, +// // keys::max_pnl_factor_for_deposits(), +// // true, +// // ); + +// // second_swap_pool_value_info.pool_value.mag.print(); +// // second_swap_pool_value_info.long_token_amount.print(); +// // second_swap_pool_value_info.short_token_amount.print(); +// // let (position_pnl_usd, uncapped_position_pnl_usd, size_delta_in_tokens) = +// // position_utils::get_position_pnl_usd( +// // data_store, market, market_prices, first_position, 5000 +// // ); +// // position_pnl_usd.mag.print(); - // ********************************************************************************************* - // * TEARDOWN * - // ********************************************************************************************* - teardown(data_store, market_factory); -} +// //////////////////////////////////// CLOSING POSITION ////////////////////////////////////// +// 'CLOOOOSE POSITION'.print(); -#[test] -fn test_long_18_takeprofit_integration() { - // ********************************************************************************************* - // * SETUP * - // ********************************************************************************************* - let ( - caller_address, - market_factory_address, - role_store_address, - data_store_address, - market_token_class_hash, - market_factory, - role_store, - data_store, - event_emitter, - exchange_router, - deposit_handler, - deposit_vault, - oracle, - order_handler, - order_vault, - reader, - referal_storage, - withdrawal_handler, - withdrawal_vault, - ) = - setup(); +// let balance_of_mkt_before = IERC20Dispatcher { +// contract_address: contract_address_const::<'USDC'>() +// } +// .balance_of(caller_address); +// 'balance of mkt before'.print(); +// balance_of_mkt_before.print(); - // ********************************************************************************************* - // * TEST LOGIC * - // ********************************************************************************************* +// start_prank(market.market_token, caller_address); +// start_prank(market.long_token, caller_address); +// let order_params_long_dec = CreateOrderParams { +// receiver: caller_address, +// callback_contract: contract_address, +// ui_fee_receiver: contract_address, +// market: market.market_token, +// initial_collateral_token: market.long_token, +// swap_path: Array32Trait::::span32(@array![market.market_token]), +// size_delta_usd: 12000000000000000000000, // 12000 +// initial_collateral_delta_amount: 2000000000000000000, // 2 ETH 10^18 +// trigger_price: 6000, +// acceptable_price: 6000, +// execution_fee: 0, +// callback_gas_limit: 0, +// min_output_amount: 12000000000000000000000, // 12000 +// order_type: OrderType::MarketDecrease(()), +// decrease_position_swap_type: DecreasePositionSwapType::NoSwap(()), +// is_long: true, +// referral_code: 0 +// }; +// // Create the long order. +// start_roll(order_handler.contract_address, 1940); +// 'try to create order'.print(); +// start_prank(order_handler.contract_address, caller_address); +// let key_long_dec = order_handler.create_order(caller_address, order_params_long_dec); +// 'long decrease created'.print(); +// let got_order_long_dec = data_store.get_order(key_long_dec); +// // data_store.set_u256(keys::pool_amount_key(market.market_token, contract_address_const::<'USDC'>()), ); +// // data_store.set_u256(keys::pool_amount_key(market.market_token, contract_address_const::<'ETH'>()), 1000000); +// // Execute the swap order. - // Create a market. - let market = data_store.get_market(create_market(market_factory)); +// let signatures: Span = array![0].span(); +// let set_price_params_dec = SetPricesParams { +// signer_info: 2, +// tokens: array![contract_address_const::<'ETH'>(), contract_address_const::<'USDC'>()], +// compacted_min_oracle_block_numbers: array![1910, 1910], +// compacted_max_oracle_block_numbers: array![1920, 1920], +// compacted_oracle_timestamps: array![9999, 9999], +// compacted_decimals: array![1, 1], +// compacted_min_prices: array![2147483648010000], // 500000, 10000 compacted +// compacted_min_prices_indexes: array![0], +// compacted_max_prices: array![2147483648010000], // 500000, 10000 compacted +// compacted_max_prices_indexes: array![0], +// signatures: array![ +// array!['signatures1', 'signatures2'].span(), array!['signatures1', 'signatures2'].span() +// ], +// price_feed_tokens: array![] +// }; - // Set params in data_store - data_store.set_address(keys::fee_token(), market.index_token); - data_store.set_u256(keys::max_swap_path_length(), 5); +// let keeper_address = contract_address_const::<'keeper'>(); +// role_store.grant_role(keeper_address, role::ORDER_KEEPER); - // Set max pool amount. - data_store - .set_u256( - keys::max_pool_amount_key(market.market_token, market.long_token), - 5000000000000000000000000000000000000000000 //500 000 ETH - ); - data_store - .set_u256( - keys::max_pool_amount_key(market.market_token, market.short_token), - 2500000000000000000000000000000000000000000000 //250 000 000 USDC - ); +// stop_prank(order_handler.contract_address); +// start_prank(order_handler.contract_address, keeper_address); +// start_roll(order_handler.contract_address, 1945); +// // TODO add real signatures check on Oracle Account +// order_handler.execute_order_keeper(key_long_dec, set_price_params_dec, keeper_address); +// 'long pos dec SUCCEEDED'.print(); - let factor_for_deposits: felt252 = keys::max_pnl_factor_for_deposits(); - data_store - .set_u256( - keys::max_pnl_factor_key(factor_for_deposits, market.market_token, true), - 50000000000000000000000000000000000000000000000 - ); - let factor_for_withdrawal: felt252 = keys::max_pnl_factor_for_withdrawals(); - data_store - .set_u256( - keys::max_pnl_factor_key(factor_for_withdrawal, market.market_token, true), - 50000000000000000000000000000000000000000000000 - ); +// let first_position_dec = data_store.get_position(position_key_1); - oracle.set_primary_prices(market.long_token, 5000); - oracle.set_primary_prices(market.short_token, 1); +// 'size tokens before'.print(); +// first_position.size_in_tokens.print(); +// 'size in usd before'.print(); +// first_position.size_in_usd.print(); - 'fill the pool'.print(); - // Fill the pool. - IERC20Dispatcher { contract_address: market.long_token } - .mint(market.market_token, 50000000000000000000000000000000000000); // 5 ETH - IERC20Dispatcher { contract_address: market.short_token } - .mint(market.market_token, 25000000000000000000000000000000000000000); // 25000 USDC - 'filled pool 1'.print(); +// 'size tokens'.print(); +// first_position_dec.size_in_tokens.print(); +// 'size in usd'.print(); +// first_position_dec.size_in_usd.print(); - IERC20Dispatcher { contract_address: market.long_token } - .mint(caller_address, 9999999999999000000); // 9.999 ETH - IERC20Dispatcher { contract_address: market.short_token } - .mint(caller_address, 49999999999999999000000); // 49.999 UDC - 'filled account'.print(); +// let balance_of_mkt_after = IERC20Dispatcher { +// contract_address: contract_address_const::<'USDC'>() +// } +// .balance_of(caller_address); +// 'balance of mkt after'.print(); +// balance_of_mkt_after.print(); - // INITIAL LONG TOKEN IN POOL : 5 ETH - // INITIAL SHORT TOKEN IN POOL : 25000 USDC +// // ********************************************************************************************* +// // * TEARDOWN * +// // ********************************************************************************************* +// teardown(data_store, market_factory); +// } - // TODO Check why we don't need to set pool_amount_key - // // Set pool amount in data_store. - // let mut key = keys::pool_amount_key(market.market_token, contract_address_const::<'ETH'>()); +// #[test] +// fn test_long_18_takeprofit_integration() { +// // ********************************************************************************************* +// // * SETUP * +// // ********************************************************************************************* +// let ( +// caller_address, +// market_factory_address, +// role_store_address, +// data_store_address, +// market_token_class_hash, +// market_factory, +// role_store, +// data_store, +// event_emitter, +// exchange_router, +// deposit_handler, +// deposit_vault, +// oracle, +// order_handler, +// order_vault, +// reader, +// referal_storage, +// withdrawal_handler, +// withdrawal_vault, +// ) = +// setup(); - let balance_deposit_vault_before = IERC20Dispatcher { contract_address: market.short_token } - .balance_of(deposit_vault.contract_address); - let balance_caller_ETH = IERC20Dispatcher { contract_address: market.long_token } - .balance_of(caller_address); - let balance_caller_USDC = IERC20Dispatcher { contract_address: market.short_token } - .balance_of(caller_address); +// // ********************************************************************************************* +// // * TEST LOGIC * +// // ********************************************************************************************* - assert(balance_deposit_vault_before == 0, 'balance deposit should be 0'); - assert(balance_caller_ETH == 10000000000000000000, 'balanc ETH should be 10 ETH'); - assert(balance_caller_USDC == 50000000000000000000000, 'USDC be 50 000 USDC'); +// // Create a market. +// let market = data_store.get_market(create_market(market_factory)); - // Send token to deposit in the deposit vault (this should be in a multi call with create_deposit) - 'get balances'.print(); - // start_prank(market.long_token, caller_address); - // IERC20Dispatcher { contract_address: market.long_token } - // .transfer(deposit_vault.contract_address, 5000000000000000000); // 5 ETH +// // Set params in data_store +// data_store.set_address(keys::fee_token(), market.index_token); +// data_store.set_u256(keys::max_swap_path_length(), 5); - // start_prank(market.short_token, caller_address); - // IERC20Dispatcher { contract_address: market.short_token } - // .transfer(deposit_vault.contract_address, 25000000000000000000000); // 25000 USDC - // 'make transfer'.print(); +// // Set max pool amount. +// data_store +// .set_u256( +// keys::max_pool_amount_key(market.market_token, market.long_token), +// 5000000000000000000000000000000000000000000 //500 000 ETH +// ); +// data_store +// .set_u256( +// keys::max_pool_amount_key(market.market_token, market.short_token), +// 2500000000000000000000000000000000000000000000 //250 000 000 USDC +// ); - IERC20Dispatcher { contract_address: market.long_token } - .mint(deposit_vault.contract_address, 50000000000000000000000000000); // 50 000 000 000 - IERC20Dispatcher { contract_address: market.short_token } - .mint(deposit_vault.contract_address, 50000000000000000000000000000); // 50 000 000 000 - // Create Deposit +// let factor_for_deposits: felt252 = keys::max_pnl_factor_for_deposits(); +// data_store +// .set_u256( +// keys::max_pnl_factor_key(factor_for_deposits, market.market_token, true), +// 50000000000000000000000000000000000000000000000 +// ); +// let factor_for_withdrawal: felt252 = keys::max_pnl_factor_for_withdrawals(); +// data_store +// .set_u256( +// keys::max_pnl_factor_key(factor_for_withdrawal, market.market_token, true), +// 50000000000000000000000000000000000000000000000 +// ); - let addresss_zero: ContractAddress = 0.try_into().unwrap(); +// oracle.set_primary_prices(market.long_token, 5000); +// oracle.set_primary_prices(market.short_token, 1); - let params = CreateDepositParams { - receiver: caller_address, - callback_contract: addresss_zero, - ui_fee_receiver: addresss_zero, - market: market.market_token, - initial_long_token: market.long_token, - initial_short_token: market.short_token, - long_token_swap_path: Array32Trait::::span32(@array![]), - short_token_swap_path: Array32Trait::::span32(@array![]), - min_market_tokens: 0, - execution_fee: 0, - callback_gas_limit: 0, - }; - 'create deposit'.print(); +// 'fill the pool'.print(); +// // Fill the pool. +// IERC20Dispatcher { contract_address: market.long_token } +// .mint(market.market_token, 50000000000000000000000000000000000000); // 5 ETH +// IERC20Dispatcher { contract_address: market.short_token } +// .mint(market.market_token, 25000000000000000000000000000000000000000); // 25000 USDC +// 'filled pool 1'.print(); - start_roll(deposit_handler.contract_address, 1910); - let key = deposit_handler.create_deposit(caller_address, params); - let first_deposit = data_store.get_deposit(key); +// IERC20Dispatcher { contract_address: market.long_token } +// .mint(caller_address, 9999999999999000000); // 9.999 ETH +// IERC20Dispatcher { contract_address: market.short_token } +// .mint(caller_address, 49999999999999999000000); // 49.999 UDC +// 'filled account'.print(); - 'created deposit'.print(); +// // INITIAL LONG TOKEN IN POOL : 5 ETH +// // INITIAL SHORT TOKEN IN POOL : 25000 USDC - assert(first_deposit.account == caller_address, 'Wrong account depositer'); - assert(first_deposit.receiver == caller_address, 'Wrong account receiver'); - assert(first_deposit.initial_long_token == market.long_token, 'Wrong initial long token'); - assert( - first_deposit.initial_long_token_amount == 50000000000000000000000000000, - 'Wrong initial long token amount' - ); - assert( - first_deposit.initial_short_token_amount == 50000000000000000000000000000, - 'Wrong init short token amount' - ); +// // TODO Check why we don't need to set pool_amount_key +// // // Set pool amount in data_store. +// // let mut key = keys::pool_amount_key(market.market_token, contract_address_const::<'ETH'>()); - let price_params = SetPricesParams { // TODO - signer_info: 1, - tokens: array![contract_address_const::<'ETH'>(), contract_address_const::<'USDC'>()], - compacted_min_oracle_block_numbers: array![1900, 1900], - compacted_max_oracle_block_numbers: array![1910, 1910], - compacted_oracle_timestamps: array![9999, 9999], - compacted_decimals: array![18, 18], - compacted_min_prices: array![4294967346000000], // 50000000, 1000000 compacted - compacted_min_prices_indexes: array![0], - compacted_max_prices: array![4294967346000000], // 50000000, 1000000 compacted - compacted_max_prices_indexes: array![0], - signatures: array![ - array!['signatures1', 'signatures2'].span(), array!['signatures1', 'signatures2'].span() - ], - price_feed_tokens: array![] - }; +// let balance_deposit_vault_before = IERC20Dispatcher { contract_address: market.short_token } +// .balance_of(deposit_vault.contract_address); +// let balance_caller_ETH = IERC20Dispatcher { contract_address: market.long_token } +// .balance_of(caller_address); +// let balance_caller_USDC = IERC20Dispatcher { contract_address: market.short_token } +// .balance_of(caller_address); - start_prank(role_store.contract_address, caller_address); +// assert(balance_deposit_vault_before == 0, 'balance deposit should be 0'); +// assert(balance_caller_ETH == 10000000000000000000, 'balanc ETH should be 10 ETH'); +// assert(balance_caller_USDC == 50000000000000000000000, 'USDC be 50 000 USDC'); - role_store.grant_role(caller_address, role::ORDER_KEEPER); - role_store.grant_role(caller_address, role::ROLE_ADMIN); - role_store.grant_role(caller_address, role::CONTROLLER); - role_store.grant_role(caller_address, role::MARKET_KEEPER); +// // Send token to deposit in the deposit vault (this should be in a multi call with create_deposit) +// 'get balances'.print(); +// // start_prank(market.long_token, caller_address); +// // IERC20Dispatcher { contract_address: market.long_token } +// // .transfer(deposit_vault.contract_address, 5000000000000000000); // 5 ETH - 'execute deposit'.print(); +// // start_prank(market.short_token, caller_address); +// // IERC20Dispatcher { contract_address: market.short_token } +// // .transfer(deposit_vault.contract_address, 25000000000000000000000); // 25000 USDC +// // 'make transfer'.print(); - // Execute Deposit - start_roll(deposit_handler.contract_address, 1915); - deposit_handler.execute_deposit(key, price_params); +// IERC20Dispatcher { contract_address: market.long_token } +// .mint(deposit_vault.contract_address, 50000000000000000000000000000); // 50 000 000 000 +// IERC20Dispatcher { contract_address: market.short_token } +// .mint(deposit_vault.contract_address, 50000000000000000000000000000); // 50 000 000 000 +// // Create Deposit - 'executed deposit'.print(); +// let addresss_zero: ContractAddress = 0.try_into().unwrap(); - // let pool_value_info = market_utils::get_pool_value_info( - // data_store, - // market, - // Price { min: 2000, max: 2000 }, - // Price { min: 2000, max: 2000 }, - // Price { min: 2000, max: 2000 }, - // keys::max_pnl_factor_for_deposits(), - // true, - // ); +// let params = CreateDepositParams { +// receiver: caller_address, +// callback_contract: addresss_zero, +// ui_fee_receiver: addresss_zero, +// market: market.market_token, +// initial_long_token: market.long_token, +// initial_short_token: market.short_token, +// long_token_swap_path: Array32Trait::::span32(@array![]), +// short_token_swap_path: Array32Trait::::span32(@array![]), +// min_market_tokens: 0, +// execution_fee: 0, +// callback_gas_limit: 0, +// }; +// 'create deposit'.print(); - // assert(pool_value_info.pool_value.mag == 42000000000000000000000, 'wrong pool value amount'); - // assert(pool_value_info.long_token_amount == 6000000000000000000, 'wrong long token amount'); - // assert(pool_value_info.short_token_amount == 30000000000000000000000, 'wrong short token amount'); +// start_roll(deposit_handler.contract_address, 1910); +// let key = deposit_handler.create_deposit(caller_address, params); +// let first_deposit = data_store.get_deposit(key); - let not_deposit = data_store.get_deposit(key); - let default_deposit: Deposit = Default::default(); - assert(not_deposit == default_deposit, 'Still existing deposit'); +// 'created deposit'.print(); - let market_token_dispatcher = IMarketTokenDispatcher { contract_address: market.market_token }; - let balance_market_token = market_token_dispatcher.balance_of(caller_address); +// assert(first_deposit.account == caller_address, 'Wrong account depositer'); +// assert(first_deposit.receiver == caller_address, 'Wrong account receiver'); +// assert(first_deposit.initial_long_token == market.long_token, 'Wrong initial long token'); +// assert( +// first_deposit.initial_long_token_amount == 50000000000000000000000000000, +// 'Wrong initial long token amount' +// ); +// assert( +// first_deposit.initial_short_token_amount == 50000000000000000000000000000, +// 'Wrong init short token amount' +// ); - assert(balance_market_token != 0, 'should receive market token'); +// let price_params = SetPricesParams { // TODO +// signer_info: 1, +// tokens: array![contract_address_const::<'ETH'>(), contract_address_const::<'USDC'>()], +// compacted_min_oracle_block_numbers: array![1900, 1900], +// compacted_max_oracle_block_numbers: array![1910, 1910], +// compacted_oracle_timestamps: array![9999, 9999], +// compacted_decimals: array![18, 18], +// compacted_min_prices: array![4294967346000000], // 50000000, 1000000 compacted +// compacted_min_prices_indexes: array![0], +// compacted_max_prices: array![4294967346000000], // 50000000, 1000000 compacted +// compacted_max_prices_indexes: array![0], +// signatures: array![ +// array!['signatures1', 'signatures2'].span(), array!['signatures1', 'signatures2'].span() +// ], +// price_feed_tokens: array![] +// }; - let balance_deposit_vault_after = IERC20Dispatcher { contract_address: market.short_token } - .balance_of(deposit_vault.contract_address); +// start_prank(role_store.contract_address, caller_address); - // let pool_value_info = market_utils::get_pool_value_info( - // data_store, - // market, - // Price { min: 5000, max: 5000, }, - // Price { min: 5000, max: 5000, }, - // Price { min: 1, max: 1, }, - // keys::max_pnl_factor_for_deposits(), - // true, - // ); +// role_store.grant_role(caller_address, role::ORDER_KEEPER); +// role_store.grant_role(caller_address, role::ROLE_ADMIN); +// role_store.grant_role(caller_address, role::CONTROLLER); +// role_store.grant_role(caller_address, role::MARKET_KEEPER); + +// 'execute deposit'.print(); + +// // Execute Deposit +// start_roll(deposit_handler.contract_address, 1915); +// deposit_handler.execute_deposit(key, price_params); + +// 'executed deposit'.print(); + +// // let pool_value_info = market_utils::get_pool_value_info( +// // data_store, +// // market, +// // Price { min: 2000, max: 2000 }, +// // Price { min: 2000, max: 2000 }, +// // Price { min: 2000, max: 2000 }, +// // keys::max_pnl_factor_for_deposits(), +// // true, +// // ); + +// // assert(pool_value_info.pool_value.mag == 42000000000000000000000, 'wrong pool value amount'); +// // assert(pool_value_info.long_token_amount == 6000000000000000000, 'wrong long token amount'); +// // assert(pool_value_info.short_token_amount == 30000000000000000000000, 'wrong short token amount'); + +// let not_deposit = data_store.get_deposit(key); +// let default_deposit: Deposit = Default::default(); +// assert(not_deposit == default_deposit, 'Still existing deposit'); + +// let market_token_dispatcher = IMarketTokenDispatcher { contract_address: market.market_token }; +// let balance_market_token = market_token_dispatcher.balance_of(caller_address); + +// assert(balance_market_token != 0, 'should receive market token'); - // pool_value_info.pool_value.mag.print(); // 10000 000000000000000000 - // pool_value_info.long_token_amount.print(); // 5 000000000000000000 - // pool_value_info.short_token_amount.print(); // 25000 000000000000000000 +// let balance_deposit_vault_after = IERC20Dispatcher { contract_address: market.short_token } +// .balance_of(deposit_vault.contract_address); - // ************************************* TEST LONG ********************************************* +// // let pool_value_info = market_utils::get_pool_value_info( +// // data_store, +// // market, +// // Price { min: 5000, max: 5000, }, +// // Price { min: 5000, max: 5000, }, +// // Price { min: 1, max: 1, }, +// // keys::max_pnl_factor_for_deposits(), +// // true, +// // ); - 'Begining of LONG TEST'.print(); +// // pool_value_info.pool_value.mag.print(); // 10000 000000000000000000 +// // pool_value_info.long_token_amount.print(); // 5 000000000000000000 +// // pool_value_info.short_token_amount.print(); // 25000 000000000000000000 - let key_open_interest = keys::open_interest_key( - market.market_token, contract_address_const::<'ETH'>(), true - ); - data_store.set_u256(key_open_interest, 1); - let max_key_open_interest = keys::max_open_interest_key(market.market_token, true); - data_store - .set_u256( - max_key_open_interest, 1000000000000000000000000000000000000000000000000000 - ); // 1 000 000 +// // ************************************* TEST LONG ********************************************* - // Send token to order_vault in multicall with create_order - start_prank(contract_address_const::<'ETH'>(), caller_address); - IERC20Dispatcher { contract_address: contract_address_const::<'ETH'>() } - .transfer(order_vault.contract_address, 2000000000000000000); // 2ETH +// 'Begining of LONG TEST'.print(); - 'transfer made'.print(); - // Create order_params Struct - let contract_address = contract_address_const::<0>(); - start_prank(market.market_token, caller_address); - start_prank(market.long_token, caller_address); - let order_params_long = CreateOrderParams { - receiver: caller_address, - callback_contract: contract_address, - ui_fee_receiver: contract_address, - market: market.market_token, - initial_collateral_token: market.long_token, - swap_path: Array32Trait::::span32(@array![]), - size_delta_usd: 10000000000000000000000, - initial_collateral_delta_amount: 2000000000000000000, // 10^18 - trigger_price: 5000, - acceptable_price: 5500, - execution_fee: 0, - callback_gas_limit: 0, - min_output_amount: 0, - order_type: OrderType::MarketIncrease(()), - decrease_position_swap_type: DecreasePositionSwapType::NoSwap(()), - is_long: true, - referral_code: 0 - }; - // Create the swap order. - start_roll(order_handler.contract_address, 1930); - 'try to create prder'.print(); - start_prank(order_handler.contract_address, caller_address); - let key_long = order_handler.create_order(caller_address, order_params_long); - 'long created'.print(); - let got_order_long = data_store.get_order(key_long); - // data_store.set_u256(keys::pool_amount_key(market.market_token, contract_address_const::<'USDC'>()), ); - // data_store.set_u256(keys::pool_amount_key(market.market_token, contract_address_const::<'ETH'>()), 1000000); - // Execute the swap order. +// let key_open_interest = keys::open_interest_key( +// market.market_token, contract_address_const::<'ETH'>(), true +// ); +// data_store.set_u256(key_open_interest, 1); +// let max_key_open_interest = keys::max_open_interest_key(market.market_token, true); +// data_store +// .set_u256( +// max_key_open_interest, 1000000000000000000000000000000000000000000000000000 +// ); // 1 000 000 - data_store - .set_u256( - keys::pool_amount_key(market.market_token, contract_address_const::<'USDC'>()), - 50000000000000000000000000000 - ); - data_store - .set_u256( - keys::pool_amount_key(market.market_token, contract_address_const::<'ETH'>()), - 50000000000000000000000000000 - ); +// // Send token to order_vault in multicall with create_order +// start_prank(contract_address_const::<'ETH'>(), caller_address); +// IERC20Dispatcher { contract_address: contract_address_const::<'ETH'>() } +// .transfer(order_vault.contract_address, 2000000000000000000); // 2ETH - let signatures: Span = array![0].span(); - let set_price_params = SetPricesParams { - signer_info: 2, - tokens: array![contract_address_const::<'ETH'>(), contract_address_const::<'USDC'>()], - compacted_min_oracle_block_numbers: array![1910, 1910], - compacted_max_oracle_block_numbers: array![1920, 1920], - compacted_oracle_timestamps: array![9999, 9999], - compacted_decimals: array![1, 1], - compacted_min_prices: array![2147483648010000], // 500000, 10000 compacted - compacted_min_prices_indexes: array![0], - compacted_max_prices: array![2147483648010000], // 500000, 10000 compacted - compacted_max_prices_indexes: array![0], - signatures: array![ - array!['signatures1', 'signatures2'].span(), array!['signatures1', 'signatures2'].span() - ], - price_feed_tokens: array![] - }; +// 'transfer made'.print(); +// // Create order_params Struct +// let contract_address = contract_address_const::<0>(); +// start_prank(market.market_token, caller_address); +// start_prank(market.long_token, caller_address); +// let order_params_long = CreateOrderParams { +// receiver: caller_address, +// callback_contract: contract_address, +// ui_fee_receiver: contract_address, +// market: market.market_token, +// initial_collateral_token: market.long_token, +// swap_path: Array32Trait::::span32(@array![]), +// size_delta_usd: 10000000000000000000000, +// initial_collateral_delta_amount: 2000000000000000000, // 10^18 +// trigger_price: 5000, +// acceptable_price: 5500, +// execution_fee: 0, +// callback_gas_limit: 0, +// min_output_amount: 0, +// order_type: OrderType::MarketIncrease(()), +// decrease_position_swap_type: DecreasePositionSwapType::NoSwap(()), +// is_long: true, +// referral_code: 0 +// }; +// // Create the swap order. +// start_roll(order_handler.contract_address, 1930); +// 'try to create prder'.print(); +// start_prank(order_handler.contract_address, caller_address); +// let key_long = order_handler.create_order(caller_address, order_params_long); +// 'long created'.print(); +// let got_order_long = data_store.get_order(key_long); +// // data_store.set_u256(keys::pool_amount_key(market.market_token, contract_address_const::<'USDC'>()), ); +// // data_store.set_u256(keys::pool_amount_key(market.market_token, contract_address_const::<'ETH'>()), 1000000); +// // Execute the swap order. - let keeper_address = contract_address_const::<'keeper'>(); - role_store.grant_role(keeper_address, role::ORDER_KEEPER); +// data_store +// .set_u256( +// keys::pool_amount_key(market.market_token, contract_address_const::<'USDC'>()), +// 50000000000000000000000000000 +// ); +// data_store +// .set_u256( +// keys::pool_amount_key(market.market_token, contract_address_const::<'ETH'>()), +// 50000000000000000000000000000 +// ); - stop_prank(order_handler.contract_address); - start_prank(order_handler.contract_address, keeper_address); - start_roll(order_handler.contract_address, 1935); - // TODO add real signatures check on Oracle Account - order_handler.execute_order_keeper(key_long, set_price_params, keeper_address); - 'long position SUCCEEDED'.print(); - let position_key = data_store.get_account_position_keys(caller_address, 0, 10); +// let signatures: Span = array![0].span(); +// let set_price_params = SetPricesParams { +// signer_info: 2, +// tokens: array![contract_address_const::<'ETH'>(), contract_address_const::<'USDC'>()], +// compacted_min_oracle_block_numbers: array![1910, 1910], +// compacted_max_oracle_block_numbers: array![1920, 1920], +// compacted_oracle_timestamps: array![9999, 9999], +// compacted_decimals: array![1, 1], +// compacted_min_prices: array![2147483648010000], // 500000, 10000 compacted +// compacted_min_prices_indexes: array![0], +// compacted_max_prices: array![2147483648010000], // 500000, 10000 compacted +// compacted_max_prices_indexes: array![0], +// signatures: array![ +// array!['signatures1', 'signatures2'].span(), array!['signatures1', 'signatures2'].span() +// ], +// price_feed_tokens: array![] +// }; - let position_key_1: felt252 = *position_key.at(0); - let first_position = data_store.get_position(position_key_1); - let market_prices = market_utils::MarketPrices { - index_token_price: Price { min: 8000, max: 8000, }, - long_token_price: Price { min: 8000, max: 8000, }, - short_token_price: Price { min: 1, max: 1, }, - }; - 'size tokens'.print(); - first_position.size_in_tokens.print(); - 'size in usd'.print(); - first_position.size_in_usd.print(); - 'OKAAAAAYYYYYY'.print(); - oracle.set_primary_prices(market.long_token, 6000); - let first_position_after_pump = data_store.get_position(position_key_1); - 'size tokens after pump'.print(); - first_position_after_pump.size_in_tokens.print(); - 'size in usd after pump'.print(); - first_position_after_pump.size_in_usd.print(); +// let keeper_address = contract_address_const::<'keeper'>(); +// role_store.grant_role(keeper_address, role::ORDER_KEEPER); - let position_info = reader - .get_position_info( - data_store, referal_storage, position_key_1, market_prices, 0, contract_address, true - ); - 'pnl'.print(); - position_info.base_pnl_usd.mag.print(); +// stop_prank(order_handler.contract_address); +// start_prank(order_handler.contract_address, keeper_address); +// start_roll(order_handler.contract_address, 1935); +// // TODO add real signatures check on Oracle Account +// order_handler.execute_order_keeper(key_long, set_price_params, keeper_address); +// 'long position SUCCEEDED'.print(); +// let position_key = data_store.get_account_position_keys(caller_address, 0, 10); - // let second_swap_pool_value_info = market_utils::get_pool_value_info( - // data_store, - // market, - // Price { min: 5000, max: 5000, }, - // Price { min: 5000, max: 5000, }, - // Price { min: 1, max: 1, }, - // keys::max_pnl_factor_for_deposits(), - // true, - // ); +// let position_key_1: felt252 = *position_key.at(0); +// let first_position = data_store.get_position(position_key_1); +// let market_prices = market_utils::MarketPrices { +// index_token_price: Price { min: 8000, max: 8000, }, +// long_token_price: Price { min: 8000, max: 8000, }, +// short_token_price: Price { min: 1, max: 1, }, +// }; +// 'size tokens'.print(); +// first_position.size_in_tokens.print(); +// 'size in usd'.print(); +// first_position.size_in_usd.print(); +// 'OKAAAAAYYYYYY'.print(); +// oracle.set_primary_prices(market.long_token, 6000); +// let first_position_after_pump = data_store.get_position(position_key_1); +// 'size tokens after pump'.print(); +// first_position_after_pump.size_in_tokens.print(); +// 'size in usd after pump'.print(); +// first_position_after_pump.size_in_usd.print(); - // second_swap_pool_value_info.pool_value.mag.print(); - // second_swap_pool_value_info.long_token_amount.print(); - // second_swap_pool_value_info.short_token_amount.print(); - // let (position_pnl_usd, uncapped_position_pnl_usd, size_delta_in_tokens) = - // position_utils::get_position_pnl_usd( - // data_store, market, market_prices, first_position, 5000 - // ); - // position_pnl_usd.mag.print(); +// let position_info = reader +// .get_position_info( +// data_store, referal_storage, position_key_1, market_prices, 0, contract_address, true +// ); +// 'pnl'.print(); +// position_info.base_pnl_usd.mag.print(); - //////////////////////////////////// TAKE PROFIT TRIGGER ///////////////////////////////// +// // let second_swap_pool_value_info = market_utils::get_pool_value_info( +// // data_store, +// // market, +// // Price { min: 5000, max: 5000, }, +// // Price { min: 5000, max: 5000, }, +// // Price { min: 1, max: 1, }, +// // keys::max_pnl_factor_for_deposits(), +// // true, +// // ); + +// // second_swap_pool_value_info.pool_value.mag.print(); +// // second_swap_pool_value_info.long_token_amount.print(); +// // second_swap_pool_value_info.short_token_amount.print(); +// // let (position_pnl_usd, uncapped_position_pnl_usd, size_delta_in_tokens) = +// // position_utils::get_position_pnl_usd( +// // data_store, market, market_prices, first_position, 5000 +// // ); +// // position_pnl_usd.mag.print(); - 'Take profit start'.print(); - start_prank(market.market_token, caller_address); - start_prank(market.long_token, caller_address); - let order_params_tp = CreateOrderParams { - receiver: caller_address, - callback_contract: contract_address, - ui_fee_receiver: contract_address, - market: market.market_token, - initial_collateral_token: market.long_token, - swap_path: Array32Trait::::span32(@array![market.market_token]), - size_delta_usd: 7000000000000000000000, // 12000 - initial_collateral_delta_amount: 1000000000000000000, // 2 ETH 10^18 - trigger_price: 7000, - acceptable_price: 7000, - execution_fee: 0, - callback_gas_limit: 0, - min_output_amount: 7000000000000000000000, // 12000 - order_type: OrderType::LimitDecrease(()), - decrease_position_swap_type: DecreasePositionSwapType::NoSwap(()), - is_long: true, - referral_code: 0 - }; - // Create the long order. - start_roll(order_handler.contract_address, 1940); - 'create takeprofit order'.print(); - start_prank(order_handler.contract_address, caller_address); - let key_tp = order_handler.create_order(caller_address, order_params_tp); - 'takeprofit created'.print(); - let got_order_tp = data_store.get_order(key_tp); - // data_store.set_u256(keys::pool_amount_key(market.market_token, contract_address_const::<'USDC'>()), ); - // data_store.set_u256(keys::pool_amount_key(market.market_token, contract_address_const::<'ETH'>()), 1000000); - // Execute the swap order. +// //////////////////////////////////// TAKE PROFIT TRIGGER ///////////////////////////////// + +// 'Take profit start'.print(); +// start_prank(market.market_token, caller_address); +// start_prank(market.long_token, caller_address); +// let order_params_tp = CreateOrderParams { +// receiver: caller_address, +// callback_contract: contract_address, +// ui_fee_receiver: contract_address, +// market: market.market_token, +// initial_collateral_token: market.long_token, +// swap_path: Array32Trait::::span32(@array![market.market_token]), +// size_delta_usd: 7000000000000000000000, // 12000 +// initial_collateral_delta_amount: 1000000000000000000, // 2 ETH 10^18 +// trigger_price: 7000, +// acceptable_price: 7000, +// execution_fee: 0, +// callback_gas_limit: 0, +// min_output_amount: 7000000000000000000000, // 12000 +// order_type: OrderType::LimitDecrease(()), +// decrease_position_swap_type: DecreasePositionSwapType::NoSwap(()), +// is_long: true, +// referral_code: 0 +// }; +// // Create the long order. +// start_roll(order_handler.contract_address, 1940); +// 'create takeprofit order'.print(); +// start_prank(order_handler.contract_address, caller_address); +// let key_tp = order_handler.create_order(caller_address, order_params_tp); +// 'takeprofit created'.print(); +// let got_order_tp = data_store.get_order(key_tp); +// // data_store.set_u256(keys::pool_amount_key(market.market_token, contract_address_const::<'USDC'>()), ); +// // data_store.set_u256(keys::pool_amount_key(market.market_token, contract_address_const::<'ETH'>()), 1000000); +// // Execute the swap order. - 'takeproit passed'.print(); - let signatures: Span = array![0].span(); - let set_price_params_dec = SetPricesParams { - signer_info: 2, - tokens: array![contract_address_const::<'ETH'>(), contract_address_const::<'USDC'>()], - compacted_min_oracle_block_numbers: array![1910, 1910], - compacted_max_oracle_block_numbers: array![1920, 1920], - compacted_oracle_timestamps: array![9999, 9999], - compacted_decimals: array![1, 1], - compacted_min_prices: array![2147483648010000], // 500000, 10000 compacted - compacted_min_prices_indexes: array![0], - compacted_max_prices: array![2147483648010000], // 500000, 10000 compacted - compacted_max_prices_indexes: array![0], - signatures: array![ - array!['signatures1', 'signatures2'].span(), array!['signatures1', 'signatures2'].span() - ], - price_feed_tokens: array![] - }; +// 'takeproit passed'.print(); +// let signatures: Span = array![0].span(); +// let set_price_params_dec = SetPricesParams { +// signer_info: 2, +// tokens: array![contract_address_const::<'ETH'>(), contract_address_const::<'USDC'>()], +// compacted_min_oracle_block_numbers: array![1910, 1910], +// compacted_max_oracle_block_numbers: array![1920, 1920], +// compacted_oracle_timestamps: array![9999, 9999], +// compacted_decimals: array![1, 1], +// compacted_min_prices: array![2147483648010000], // 500000, 10000 compacted +// compacted_min_prices_indexes: array![0], +// compacted_max_prices: array![2147483648010000], // 500000, 10000 compacted +// compacted_max_prices_indexes: array![0], +// signatures: array![ +// array!['signatures1', 'signatures2'].span(), array!['signatures1', 'signatures2'].span() +// ], +// price_feed_tokens: array![] +// }; - let keeper_address = contract_address_const::<'keeper'>(); - role_store.grant_role(keeper_address, role::ORDER_KEEPER); +// let keeper_address = contract_address_const::<'keeper'>(); +// role_store.grant_role(keeper_address, role::ORDER_KEEPER); - stop_prank(order_handler.contract_address); - start_prank(order_handler.contract_address, keeper_address); - start_roll(order_handler.contract_address, 1945); - // TODO add real signatures check on Oracle Account - oracle.set_primary_prices(market.long_token, 7000); - order_handler.execute_order_keeper(key_tp, set_price_params_dec, keeper_address); - 'take profit pos dec SUCCEEDED'.print(); +// stop_prank(order_handler.contract_address); +// start_prank(order_handler.contract_address, keeper_address); +// start_roll(order_handler.contract_address, 1945); +// // TODO add real signatures check on Oracle Account +// oracle.set_primary_prices(market.long_token, 7000); +// order_handler.execute_order_keeper(key_tp, set_price_params_dec, keeper_address); +// 'take profit pos dec SUCCEEDED'.print(); - //////////////////////////////////// CLOSING POSITION ////////////////////////////////////// - 'CLOOOOSE POSITION'.print(); +// //////////////////////////////////// CLOSING POSITION ////////////////////////////////////// +// 'CLOOOOSE POSITION'.print(); - let balance_of_mkt_before = IERC20Dispatcher { - contract_address: contract_address_const::<'USDC'>() - } - .balance_of(caller_address); - 'balance of mkt before'.print(); - balance_of_mkt_before.print(); +// let balance_of_mkt_before = IERC20Dispatcher { +// contract_address: contract_address_const::<'USDC'>() +// } +// .balance_of(caller_address); +// 'balance of mkt before'.print(); +// balance_of_mkt_before.print(); - start_prank(market.market_token, caller_address); - start_prank(market.long_token, caller_address); - let order_params_long_dec = CreateOrderParams { - receiver: caller_address, - callback_contract: contract_address, - ui_fee_receiver: contract_address, - market: market.market_token, - initial_collateral_token: market.long_token, - swap_path: Array32Trait::::span32(@array![market.market_token]), - size_delta_usd: 6000000000000000000000, // 12000 - initial_collateral_delta_amount: 1000000000000000000, // 2 ETH 10^18 - trigger_price: 6000, - acceptable_price: 6000, - execution_fee: 0, - callback_gas_limit: 0, - min_output_amount: 6000000000000000000000, // 12000 - order_type: OrderType::MarketDecrease(()), - decrease_position_swap_type: DecreasePositionSwapType::NoSwap(()), - is_long: true, - referral_code: 0 - }; - // Create the long order. - start_roll(order_handler.contract_address, 1940); - 'try to create order'.print(); - start_prank(order_handler.contract_address, caller_address); - let key_long_dec = order_handler.create_order(caller_address, order_params_long_dec); - 'long decrease created'.print(); - let got_order_long_dec = data_store.get_order(key_long_dec); - // data_store.set_u256(keys::pool_amount_key(market.market_token, contract_address_const::<'USDC'>()), ); - // data_store.set_u256(keys::pool_amount_key(market.market_token, contract_address_const::<'ETH'>()), 1000000); - // Execute the swap order. +// start_prank(market.market_token, caller_address); +// start_prank(market.long_token, caller_address); +// let order_params_long_dec = CreateOrderParams { +// receiver: caller_address, +// callback_contract: contract_address, +// ui_fee_receiver: contract_address, +// market: market.market_token, +// initial_collateral_token: market.long_token, +// swap_path: Array32Trait::::span32(@array![market.market_token]), +// size_delta_usd: 6000000000000000000000, // 12000 +// initial_collateral_delta_amount: 1000000000000000000, // 2 ETH 10^18 +// trigger_price: 6000, +// acceptable_price: 6000, +// execution_fee: 0, +// callback_gas_limit: 0, +// min_output_amount: 6000000000000000000000, // 12000 +// order_type: OrderType::MarketDecrease(()), +// decrease_position_swap_type: DecreasePositionSwapType::NoSwap(()), +// is_long: true, +// referral_code: 0 +// }; +// // Create the long order. +// start_roll(order_handler.contract_address, 1940); +// 'try to create order'.print(); +// start_prank(order_handler.contract_address, caller_address); +// let key_long_dec = order_handler.create_order(caller_address, order_params_long_dec); +// 'long decrease created'.print(); +// let got_order_long_dec = data_store.get_order(key_long_dec); +// // data_store.set_u256(keys::pool_amount_key(market.market_token, contract_address_const::<'USDC'>()), ); +// // data_store.set_u256(keys::pool_amount_key(market.market_token, contract_address_const::<'ETH'>()), 1000000); +// // Execute the swap order. - let signatures: Span = array![0].span(); - let set_price_params_dec = SetPricesParams { - signer_info: 2, - tokens: array![contract_address_const::<'ETH'>(), contract_address_const::<'USDC'>()], - compacted_min_oracle_block_numbers: array![1910, 1910], - compacted_max_oracle_block_numbers: array![1920, 1920], - compacted_oracle_timestamps: array![9999, 9999], - compacted_decimals: array![1, 1], - compacted_min_prices: array![2147483648010000], // 500000, 10000 compacted - compacted_min_prices_indexes: array![0], - compacted_max_prices: array![2147483648010000], // 500000, 10000 compacted - compacted_max_prices_indexes: array![0], - signatures: array![ - array!['signatures1', 'signatures2'].span(), array!['signatures1', 'signatures2'].span() - ], - price_feed_tokens: array![] - }; +// let signatures: Span = array![0].span(); +// let set_price_params_dec = SetPricesParams { +// signer_info: 2, +// tokens: array![contract_address_const::<'ETH'>(), contract_address_const::<'USDC'>()], +// compacted_min_oracle_block_numbers: array![1910, 1910], +// compacted_max_oracle_block_numbers: array![1920, 1920], +// compacted_oracle_timestamps: array![9999, 9999], +// compacted_decimals: array![1, 1], +// compacted_min_prices: array![2147483648010000], // 500000, 10000 compacted +// compacted_min_prices_indexes: array![0], +// compacted_max_prices: array![2147483648010000], // 500000, 10000 compacted +// compacted_max_prices_indexes: array![0], +// signatures: array![ +// array!['signatures1', 'signatures2'].span(), array!['signatures1', 'signatures2'].span() +// ], +// price_feed_tokens: array![] +// }; - let keeper_address = contract_address_const::<'keeper'>(); - role_store.grant_role(keeper_address, role::ORDER_KEEPER); +// let keeper_address = contract_address_const::<'keeper'>(); +// role_store.grant_role(keeper_address, role::ORDER_KEEPER); - stop_prank(order_handler.contract_address); - start_prank(order_handler.contract_address, keeper_address); - start_roll(order_handler.contract_address, 1945); - // TODO add real signatures check on Oracle Account - order_handler.execute_order_keeper(key_long_dec, set_price_params_dec, keeper_address); - 'long pos dec SUCCEEDED'.print(); +// stop_prank(order_handler.contract_address); +// start_prank(order_handler.contract_address, keeper_address); +// start_roll(order_handler.contract_address, 1945); +// // TODO add real signatures check on Oracle Account +// order_handler.execute_order_keeper(key_long_dec, set_price_params_dec, keeper_address); +// 'long pos dec SUCCEEDED'.print(); - let first_position_dec = data_store.get_position(position_key_1); +// let first_position_dec = data_store.get_position(position_key_1); - 'size tokens before'.print(); - first_position.size_in_tokens.print(); - 'size in usd before'.print(); - first_position.size_in_usd.print(); +// 'size tokens before'.print(); +// first_position.size_in_tokens.print(); +// 'size in usd before'.print(); +// first_position.size_in_usd.print(); - 'size tokens'.print(); - first_position_dec.size_in_tokens.print(); - 'size in usd'.print(); - first_position_dec.size_in_usd.print(); +// 'size tokens'.print(); +// first_position_dec.size_in_tokens.print(); +// 'size in usd'.print(); +// first_position_dec.size_in_usd.print(); - let balance_of_mkt_after = IERC20Dispatcher { - contract_address: contract_address_const::<'USDC'>() - } - .balance_of(caller_address); - 'balance of mkt after'.print(); - balance_of_mkt_after.print(); +// let balance_of_mkt_after = IERC20Dispatcher { +// contract_address: contract_address_const::<'USDC'>() +// } +// .balance_of(caller_address); +// 'balance of mkt after'.print(); +// balance_of_mkt_after.print(); - // ********************************************************************************************* - // * TEARDOWN * - // ********************************************************************************************* - teardown(data_store, market_factory); -} +// // ********************************************************************************************* +// // * TEARDOWN * +// // ********************************************************************************************* +// teardown(data_store, market_factory); +// } #[test] fn test_long_liquidable_market_integration() { @@ -2639,6 +2642,7 @@ fn test_long_liquidable_market_integration() { referal_storage, withdrawal_handler, withdrawal_vault, + liquidation_handler, ) = setup(); @@ -2990,8 +2994,8 @@ fn test_long_liquidable_market_integration() { assert(is_liquid == false, 'position not liquid'); let market_prices_liquidation = market_utils::MarketPrices { - index_token_price: Price { min: 100, max: 100, }, - long_token_price: Price { min: 100, max: 100, }, + index_token_price: Price { min: 2500, max: 2500, }, + long_token_price: Price { min: 2500, max: 2500, }, short_token_price: Price { min: 1, max: 1, }, }; @@ -3000,6 +3004,37 @@ fn test_long_liquidable_market_integration() { ); assert(is_liquid == true, 'position liquidable'); + oracle.set_primary_prices(market.long_token, 2500); + + let signatures: Span = array![0].span(); + let set_price_params = SetPricesParams { + signer_info: 2, + tokens: array![contract_address_const::<'ETH'>(), contract_address_const::<'USDC'>()], + compacted_min_oracle_block_numbers: array![1910, 1910], + compacted_max_oracle_block_numbers: array![1920, 1920], + compacted_oracle_timestamps: array![9999, 9999], + compacted_decimals: array![1, 1], + compacted_min_prices: array![2147483648010000], // 500000, 10000 compacted + compacted_min_prices_indexes: array![0], + compacted_max_prices: array![2147483648010000], // 500000, 10000 compacted + compacted_max_prices_indexes: array![0], + signatures: array![ + array!['signatures1', 'signatures2'].span(), array!['signatures1', 'signatures2'].span() + ], + price_feed_tokens: array![] + }; + + let keeper_address = contract_address_const::<'keeper'>(); + role_store.grant_role(keeper_address, role::ORDER_KEEPER); + role_store.grant_role(keeper_address, role::LIQUIDATION_KEEPER); + + start_prank(liquidation_handler.contract_address, keeper_address); + start_roll(liquidation_handler.contract_address, 1940); + + liquidation_handler + .execute_liquidation( + caller_address, market.market_token, market.long_token, true, set_price_params + ); // ********************************************************************************************* // * TEARDOWN * @@ -3064,6 +3099,7 @@ fn setup() -> ( IReferralStorageDispatcher, IWithdrawalHandlerDispatcher, IWithdrawalVaultDispatcher, + ILiquidationHandlerDispatcher, ) { let ( caller_address, @@ -3085,6 +3121,7 @@ fn setup() -> ( referal_storage, withdrawal_handler, withdrawal_vault, + liquidation_handler, ) = setup_contracts(); grant_roles_and_prank(caller_address, role_store, data_store, market_factory); @@ -3108,6 +3145,7 @@ fn setup() -> ( referal_storage, withdrawal_handler, withdrawal_vault, + liquidation_handler, ) } @@ -3183,6 +3221,7 @@ fn setup_contracts() -> ( IReferralStorageDispatcher, IWithdrawalHandlerDispatcher, IWithdrawalVaultDispatcher, + ILiquidationHandlerDispatcher, ) { // Deploy the role store contract. let role_store_address = deploy_role_store(); @@ -3302,6 +3341,23 @@ fn setup_contracts() -> ( let withdrawal_vault = IWithdrawalVaultDispatcher { contract_address: withdrawal_vault_address }; + let liquidation_handler_address = deploy_liquidation_handler( + data_store_address, + role_store_address, + event_emitter_address, + order_vault_address, + oracle_address, + swap_handler_address, + referral_storage_address, + order_utils_class_hash, + increase_order_class_hash, + decrease_order_class_hash, + swap_order_class_hash + ); + + let liquidation_handler = ILiquidationHandlerDispatcher { + contract_address: liquidation_handler_address + }; ( contract_address_const::<'caller'>(), market_factory_address, @@ -3322,6 +3378,7 @@ fn setup_contracts() -> ( referal_storage, withdrawal_handler, withdrawal_vault, + liquidation_handler, ) } @@ -3520,6 +3577,39 @@ fn deploy_order_handler( contract.deploy_at(@constructor_calldata, deployed_contract_address).unwrap() } +fn deploy_liquidation_handler( + data_store_address: ContractAddress, + role_store_address: ContractAddress, + event_emitter_address: ContractAddress, + order_vault_address: ContractAddress, + oracle_address: ContractAddress, + swap_handler_address: ContractAddress, + referral_storage_address: ContractAddress, + order_utils_class_hash: ClassHash, + increase_order_class_hash: ClassHash, + decrease_order_class_hash: ClassHash, + swap_order_class_hash: ClassHash +) -> ContractAddress { + let contract = declare('LiquidationHandler'); + let caller_address: ContractAddress = contract_address_const::<'caller'>(); + let deployed_contract_address = contract_address_const::<'liquidation_handler'>(); + start_prank(deployed_contract_address, caller_address); + let constructor_calldata = array![ + data_store_address.into(), + role_store_address.into(), + event_emitter_address.into(), + order_vault_address.into(), + oracle_address.into(), + swap_handler_address.into(), + referral_storage_address.into(), + order_utils_class_hash.into(), + increase_order_class_hash.into(), + decrease_order_class_hash.into(), + swap_order_class_hash.into() + ]; + contract.deploy_at(@constructor_calldata, deployed_contract_address).unwrap() +} + fn deploy_swap_handler_address( role_store_address: ContractAddress, data_store_address: ContractAddress ) -> ContractAddress { diff --git a/tests/lib.cairo b/tests/lib.cairo index 2dcf7a54..33279fb4 100644 --- a/tests/lib.cairo +++ b/tests/lib.cairo @@ -121,9 +121,9 @@ // } mod integration { - mod test_deposit_withdrawal; + // mod test_deposit_withdrawal; mod test_long_integration; - mod test_short_integration; - // mod test_swap_integration; - mod swap_test; +// mod test_short_integration; +// mod test_swap_integration; +// mod swap_test; }