From 9ea6b2dafa7d3c985cd908e1e43b2473af130d14 Mon Sep 17 00:00:00 2001 From: sparqet <37338401+sparqet@users.noreply.github.com> Date: Wed, 3 Jul 2024 13:33:52 +0200 Subject: [PATCH 1/2] Feat/is liquidable func (#689) * refactor integration tests * add is_position_liquidable in reader * fix coding style --- src/reader/reader.cairo | 29 +++++++++++++++++++ tests/integration/test_long_integration.cairo | 21 ++++++++------ 2 files changed, 41 insertions(+), 9 deletions(-) diff --git a/src/reader/reader.cairo b/src/reader/reader.cairo index 489e8a1b..3530cc78 100644 --- a/src/reader/reader.cairo +++ b/src/reader/reader.cairo @@ -425,6 +425,16 @@ trait IReader { is_long: bool, prices: MarketPrices ) -> (u64, bool, i256, u256); + + fn is_position_liquidable( + self: @TContractState, + data_store: IDataStoreDispatcher, + referral_storage: IReferralStorageDispatcher, + position: Position, + market: Market, + prices: MarketPrices, + should_validate_min_collateral_usd: bool, + ) -> (bool, felt252); } #[starknet::contract] @@ -856,6 +866,25 @@ mod Reader { ); (latest_adl_block, should_enabled_ald, pnl_to_pool_factor, max_pnl_factor) } + + fn is_position_liquidable( + self: @ContractState, + data_store: IDataStoreDispatcher, + referral_storage: IReferralStorageDispatcher, + position: Position, + market: Market, + prices: MarketPrices, + should_validate_min_collateral_usd: bool + ) -> (bool, felt252) { + position_utils::is_position_liquiditable( + data_store, + referral_storage, + position, + market, + prices, + should_validate_min_collateral_usd + ) + } } } diff --git a/tests/integration/test_long_integration.cairo b/tests/integration/test_long_integration.cairo index 91a65112..0dba739e 100644 --- a/tests/integration/test_long_integration.cairo +++ b/tests/integration/test_long_integration.cairo @@ -3713,9 +3713,10 @@ fn test_long_liquidation() { short_token_price: Price { min: 1, max: 1, }, }; - let (is_liquiditable, reason) = position_utils::is_position_liquiditable( - data_store, referal_storage, first_position, market, market_prices, false - ); + let (is_liquiditable, reason) = reader + .is_position_liquidable( + data_store, referal_storage, first_position, market, market_prices, true + ); assert(is_liquiditable == true, 'Position is liquidable'); @@ -3727,9 +3728,10 @@ fn test_long_liquidation() { short_token_price: Price { min: 1, max: 1, }, }; - let (is_liquiditable, reason) = position_utils::is_position_liquiditable( - data_store, referal_storage, first_position, market, market_prices, false - ); + let (is_liquiditable, reason) = reader + .is_position_liquidable( + data_store, referal_storage, first_position, market, market_prices, true + ); assert(is_liquiditable == false, 'Position is not liquidable'); @@ -4637,9 +4639,10 @@ fn test_long_leverage_liquidation() { short_token_price: Price { min: 1, max: 1, }, }; - let (is_liquiditable, reason) = position_utils::is_position_liquiditable( - data_store, referal_storage, first_position, market, market_prices, false - ); + let (is_liquiditable, reason) = reader + .is_position_liquidable( + data_store, referal_storage, first_position, market, market_prices, true + ); // position x10 leverage is liquidable at 3000$, position x1 leverage is not liquidable at 3000$ assert(is_liquiditable == true, 'Position is liquidable'); From a2fb657a2661cf42ac79a226ce142c5100023c12 Mon Sep 17 00:00:00 2001 From: sparqet <37338401+sparqet@users.noreply.github.com> Date: Wed, 3 Jul 2024 15:12:31 +0200 Subject: [PATCH 2/2] Refactor/deposit function (#690) * refactor integration tests * add is_position_liquidable in reader * fix coding style * refactor deposit setup tests --- src/callback/mocks.cairo | 2 +- src/lib.cairo | 5 +- src/test_utils/deposit_setup.cairo | 368 ++++ src/{ => test_utils}/tests_lib.cairo | 1 - tests/integration/swap_test.cairo | 172 +- tests/integration/test_long_integration.cairo | 1518 +---------------- .../integration/test_short_integration.cairo | 208 +-- tests/integration/test_swap_integration.cairo | 2 +- 8 files changed, 442 insertions(+), 1834 deletions(-) create mode 100644 src/test_utils/deposit_setup.cairo rename src/{ => test_utils}/tests_lib.cairo (99%) diff --git a/src/callback/mocks.cairo b/src/callback/mocks.cairo index edbc01a5..a8457361 100644 --- a/src/callback/mocks.cairo +++ b/src/callback/mocks.cairo @@ -1,7 +1,7 @@ use starknet::ContractAddress; use snforge_std::{declare, ContractClassTrait}; -use satoru::tests_lib::{setup, teardown, deploy_event_emitter}; +use satoru::test_utils::tests_lib::{setup, teardown, deploy_event_emitter}; #[starknet::interface] trait ICallbackMock { diff --git a/src/lib.cairo b/src/lib.cairo index 019715f3..aa1fb618 100644 --- a/src/lib.cairo +++ b/src/lib.cairo @@ -244,7 +244,10 @@ mod token { mod token_utils; } -mod tests_lib; +mod test_utils { + mod deposit_setup; + mod tests_lib; +} // `withdrawal` contains withdrawal management functions mod withdrawal { diff --git a/src/test_utils/deposit_setup.cairo b/src/test_utils/deposit_setup.cairo new file mode 100644 index 00000000..84194717 --- /dev/null +++ b/src/test_utils/deposit_setup.cairo @@ -0,0 +1,368 @@ +// ************************************************************************* +// IMPORTS +// ************************************************************************* + +// Core lib imports. + +use result::ResultTrait; +use debug::PrintTrait; +use traits::{TryInto, Into}; +use starknet::{ + ContractAddress, get_caller_address, Felt252TryIntoContractAddress, contract_address_const, + ClassHash, +}; +use snforge_std::{declare, start_prank, stop_prank, start_roll, ContractClassTrait, ContractClass}; + + +// Local imports. +use satoru::data::data_store::{IDataStoreDispatcher, IDataStoreDispatcherTrait}; +use satoru::role::role_store::{IRoleStoreDispatcher, IRoleStoreDispatcherTrait}; +use satoru::order::order_utils::{IOrderUtilsDispatcher, IOrderUtilsDispatcherTrait}; +use satoru::market::market_factory::{IMarketFactoryDispatcher, IMarketFactoryDispatcherTrait}; +use satoru::event::event_emitter::{IEventEmitterDispatcher, IEventEmitterDispatcherTrait}; +use satoru::deposit::deposit_vault::{IDepositVaultDispatcher, IDepositVaultDispatcherTrait}; +use satoru::deposit::deposit::Deposit; +use satoru::withdrawal::withdrawal::Withdrawal; + +use satoru::exchange::withdrawal_handler::{ + IWithdrawalHandlerDispatcher, IWithdrawalHandlerDispatcherTrait +}; +use satoru::exchange::deposit_handler::{IDepositHandlerDispatcher, IDepositHandlerDispatcherTrait}; +use satoru::router::exchange_router::{IExchangeRouterDispatcher, IExchangeRouterDispatcherTrait}; +use satoru::mock::referral_storage::{IReferralStorageDispatcher, IReferralStorageDispatcherTrait}; +use satoru::reader::reader::{IReaderDispatcher, IReaderDispatcherTrait}; +use satoru::market::market::{Market, UniqueIdMarket}; +use satoru::market::market_token::{IMarketTokenDispatcher, IMarketTokenDispatcherTrait}; +use satoru::role::role; +use satoru::oracle::oracle_utils::SetPricesParams; +use satoru::test_utils::tests_lib; +use satoru::deposit::deposit_utils::CreateDepositParams; +use satoru::utils::span32::{Span32, DefaultSpan32, Array32Trait}; +use satoru::deposit::deposit_utils; +use satoru::bank::bank::{IBankDispatcherTrait, IBankDispatcher}; +use satoru::bank::strict_bank::{IStrictBankDispatcher, IStrictBankDispatcherTrait}; +use satoru::token::erc20::interface::{IERC20Dispatcher, IERC20DispatcherTrait}; +use satoru::oracle::oracle::{IOracleDispatcher, IOracleDispatcherTrait}; +use satoru::withdrawal::withdrawal_vault::{ + IWithdrawalVaultDispatcher, IWithdrawalVaultDispatcherTrait +}; +use satoru::data::keys; +use satoru::market::market_utils; +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}; +use satoru::oracle::oracle_store::{IOracleStoreDispatcher, IOracleStoreDispatcherTrait}; +use satoru::swap::swap_handler::{ISwapHandlerDispatcher, ISwapHandlerDispatcherTrait}; +use satoru::market::{market::{UniqueIdMarketImpl},}; +use satoru::exchange::order_handler::{ + OrderHandler, IOrderHandlerDispatcher, IOrderHandlerDispatcherTrait +}; +use satoru::test_utils::tests_lib::{setup, create_market, teardown}; + +fn deposit_setup( + long_token_amount: u256, short_token_amount: u256 +) -> ( + // This caller address will be used with `start_prank` cheatcode to mock the caller address., + ContractAddress, + // Address of the `MarketFactory` contract. + ContractAddress, + // Address of the `RoleStore` contract. + ContractAddress, + // Address of the `DataStore` contract. + ContractAddress, + // The `MarketToken` class hash for the factory. + ContractClass, + // Interface to interact with the `MarketFactory` contract. + IMarketFactoryDispatcher, + // Interface to interact with the `RoleStore` contract. + IRoleStoreDispatcher, + // Interface to interact with the `DataStore` contract. + IDataStoreDispatcher, + // Interface to interact with the `EventEmitter` contract. + IEventEmitterDispatcher, + // Interface to interact with the `ExchangeRouter` contract. + IExchangeRouterDispatcher, + // Interface to interact with the `DepositHandler` contract. + IDepositHandlerDispatcher, + // Interface to interact with the `DepositHandler` contract. + IDepositVaultDispatcher, + IOracleDispatcher, + IOrderHandlerDispatcher, + IOrderVaultDispatcher, + IReaderDispatcher, + IReferralStorageDispatcher, + IWithdrawalHandlerDispatcher, + IWithdrawalVaultDispatcher, + ILiquidationHandlerDispatcher, + Market, +) { + // ********************************************************************************************* + // * 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, + liquidation_handler, + ) = + setup(); + + // ********************************************************************************************* + // * TEST LOGIC * + // ********************************************************************************************* + + // Create a market. + let market = data_store.get_market(create_market(market_factory)); + + // 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); + + // 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 + ); + + // Long setups + + 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 + ); + data_store.set_u256(keys::reserve_factor_key(market.market_token, true), 1000000000000000000); + data_store + .set_u256( + keys::open_interest_reserve_factor_key(market.market_token, true), 1000000000000000000 + ); + + // Short 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, false), + 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, false), + 50000000000000000000000000000000000000000000000 + ); + data_store.set_u256(keys::reserve_factor_key(market.market_token, false), 1000000000000000000); + data_store + .set_u256( + keys::open_interest_reserve_factor_key(market.market_token, false), 1000000000000000000 + ); + + data_store.set_bool('REENTRANCY_GUARD_STATUS', false); + + '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(); + + 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(); + + // 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); + + 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'); + + // Send token to deposit in the deposit vault (this should be in a multi call with create_deposit) + start_prank(market.long_token, caller_address); + start_prank(market.short_token, caller_address); + IERC20Dispatcher { contract_address: market.long_token } + .approve(caller_address, long_token_amount); + IERC20Dispatcher { contract_address: market.short_token } + .approve(caller_address, short_token_amount); + + IERC20Dispatcher { contract_address: market.long_token } + .mint(caller_address, long_token_amount); // 20 ETH + IERC20Dispatcher { contract_address: market.short_token } + .mint(caller_address, short_token_amount); // 100 000 USDC + + // role_store.grant_role(exchange_router.contract_address, role::ROUTER_PLUGIN); + // role_store.grant_role(caller_address, role::ROUTER_PLUGIN); + + exchange_router + .send_tokens(market.long_token, deposit_vault.contract_address, long_token_amount); + exchange_router + .send_tokens(market.short_token, deposit_vault.contract_address, short_token_amount); + + stop_prank(market.long_token); + stop_prank(market.short_token); + + // Create Deposit + + let addresss_zero: ContractAddress = 0.try_into().unwrap(); + + 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(); + + start_roll(deposit_handler.contract_address, 1910); + let key = deposit_handler.create_deposit(caller_address, params); + let first_deposit = data_store.get_deposit(key); + + 'created deposit'.print(); + + 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 == long_token_amount, + 'Wrong initial long token amount' + ); + assert( + first_deposit.initial_short_token_amount == short_token_amount, + 'Wrong init short token amount' + ); + + let price_params = SetPricesParams { + signer_info: 0, + 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![4000, 1], // 500000, 10000 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); + + role_store.grant_role(caller_address, role::ORDER_KEEPER); + role_store.grant_role(caller_address, role::ROLE_ADMIN); + role_store.grant_role(exchange_router.contract_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'); + + let balance_deposit_vault_after = IERC20Dispatcher { contract_address: market.short_token } + .balance_of(deposit_vault.contract_address); + + ( + 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, + liquidation_handler, + market + ) +} diff --git a/src/tests_lib.cairo b/src/test_utils/tests_lib.cairo similarity index 99% rename from src/tests_lib.cairo rename to src/test_utils/tests_lib.cairo index 168b8e6d..31f96bdc 100644 --- a/src/tests_lib.cairo +++ b/src/test_utils/tests_lib.cairo @@ -35,7 +35,6 @@ use satoru::market::market::{Market, UniqueIdMarket}; use satoru::market::market_token::{IMarketTokenDispatcher, IMarketTokenDispatcherTrait}; use satoru::role::role; use satoru::oracle::oracle_utils::SetPricesParams; -use satoru::tests_lib; use satoru::deposit::deposit_utils::CreateDepositParams; use satoru::utils::span32::{Span32, DefaultSpan32, Array32Trait}; use satoru::deposit::deposit_utils; diff --git a/tests/integration/swap_test.cairo b/tests/integration/swap_test.cairo index f9278bb9..e09f24d8 100644 --- a/tests/integration/swap_test.cairo +++ b/tests/integration/swap_test.cairo @@ -35,7 +35,7 @@ use satoru::market::market::{Market, UniqueIdMarket}; use satoru::market::market_token::{IMarketTokenDispatcher, IMarketTokenDispatcherTrait}; use satoru::role::role; use satoru::oracle::oracle_utils::SetPricesParams; -use satoru::tests_lib; +use satoru::test_utils::tests_lib; use satoru::deposit::deposit_utils::CreateDepositParams; use satoru::utils::span32::{Span32, DefaultSpan32, Array32Trait}; use satoru::deposit::deposit_utils; @@ -64,7 +64,7 @@ use satoru::market::{market::{UniqueIdMarketImpl},}; use satoru::exchange::order_handler::{ OrderHandler, IOrderHandlerDispatcher, IOrderHandlerDispatcherTrait }; -use satoru::tests_lib::{setup, create_market, teardown}; +use satoru::test_utils::{tests_lib::{setup, create_market, teardown}, deposit_setup::deposit_setup}; const INITIAL_TOKENS_MINTED: felt252 = 1000; #[test] @@ -90,180 +90,20 @@ fn test_swap_market() { withdrawal_handler, withdrawal_vault, liquidation_handler, + market, ) = - setup(); - - // ********************************************************************************************* - // * TEST LOGIC * - // ********************************************************************************************* + deposit_setup( + 20000000000000000000, 100000000000000000000000 + ); - // Create a market. - let market = data_store.get_market(create_market(market_factory)); - - // 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); - - // 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 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 - ); - data_store.set_u256(keys::reserve_factor_key(market.market_token, true), 1000000000000000000); - data_store - .set_u256( - keys::open_interest_reserve_factor_key(market.market_token, true), 1000000000000000000 - ); - - data_store.set_bool('REENTRANCY_GUARD_STATUS', false); - - '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(); - - 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(); - - // 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); - 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'); - // Send token to deposit in the deposit vault (this should be in a multi call with create_deposit) - start_prank(market.long_token, caller_address); - start_prank(market.short_token, caller_address); - IERC20Dispatcher { contract_address: market.long_token } - .approve(caller_address, 20000000000000000000); - IERC20Dispatcher { contract_address: market.short_token } - .approve(caller_address, 100000000000000000000000); - - IERC20Dispatcher { contract_address: market.long_token } - .mint(caller_address, 20000000000000000000); // 20 ETH - IERC20Dispatcher { contract_address: market.short_token } - .mint(caller_address, 100000000000000000000000); // 100 000 USDC - - // role_store.grant_role(exchange_router.contract_address, role::ROUTER_PLUGIN); - // role_store.grant_role(caller_address, role::ROUTER_PLUGIN); - - exchange_router - .send_tokens(market.long_token, deposit_vault.contract_address, 20000000000000000000); - exchange_router - .send_tokens(market.short_token, deposit_vault.contract_address, 100000000000000000000000); - - stop_prank(market.long_token); - stop_prank(market.short_token); - - // Create Deposit - let addresss_zero: ContractAddress = 0.try_into().unwrap(); - 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(); - - start_roll(deposit_handler.contract_address, 1910); - let key = deposit_handler.create_deposit(caller_address, params); - let first_deposit = data_store.get_deposit(key); - - 'created deposit'.print(); - - 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 == 20000000000000000000, - 'Wrong initial long token amount' - ); - assert( - first_deposit.initial_short_token_amount == 100000000000000000000000, - 'Wrong init short token amount' - ); - - let price_params = SetPricesParams { - signer_info: 0, - 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![5000, 1], // 500000, 10000 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); - - role_store.grant_role(caller_address, role::ORDER_KEEPER); - role_store.grant_role(caller_address, role::ROLE_ADMIN); - role_store.grant_role(exchange_router.contract_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 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'); - let pool_value_info = market_utils::get_pool_value_info( data_store, market, diff --git a/tests/integration/test_long_integration.cairo b/tests/integration/test_long_integration.cairo index 0dba739e..a9690e60 100644 --- a/tests/integration/test_long_integration.cairo +++ b/tests/integration/test_long_integration.cairo @@ -35,7 +35,7 @@ use satoru::market::market::{Market, UniqueIdMarket}; use satoru::market::market_token::{IMarketTokenDispatcher, IMarketTokenDispatcherTrait}; use satoru::role::role; use satoru::oracle::oracle_utils::SetPricesParams; -use satoru::tests_lib; +use satoru::test_utils::tests_lib; use satoru::deposit::deposit_utils::CreateDepositParams; use satoru::utils::span32::{Span32, DefaultSpan32, Array32Trait}; use satoru::deposit::deposit_utils; @@ -64,9 +64,9 @@ use satoru::market::{market::{UniqueIdMarketImpl},}; use satoru::exchange::order_handler::{ OrderHandler, IOrderHandlerDispatcher, IOrderHandlerDispatcherTrait }; -use satoru::tests_lib::{setup, create_market, teardown}; -const INITIAL_TOKENS_MINTED: felt252 = 1000; - +use satoru::test_utils::{ + tests_lib::{setup, create_market, teardown}, deposit_setup::{deposit_setup} +}; #[test] fn test_long_increase_decrease_close() { @@ -94,203 +94,20 @@ fn test_long_increase_decrease_close() { withdrawal_handler, withdrawal_vault, liquidation_handler, + market, ) = - setup(); - - // ********************************************************************************************* - // * TEST LOGIC * - // ********************************************************************************************* - - // Create a market. - let market = data_store.get_market(create_market(market_factory)); - - // 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); - - // 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 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 - ); - data_store.set_u256(keys::reserve_factor_key(market.market_token, true), 1000000000000000000); - data_store - .set_u256( - keys::open_interest_reserve_factor_key(market.market_token, true), 1000000000000000000 - ); - - data_store.set_bool('REENTRANCY_GUARD_STATUS', false); - - '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(); - - 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(); - - // INITIAL LONG TOKEN IN POOL : 5 ETH - // INITIAL SHORT TOKEN IN POOL : 25000 USDC + deposit_setup( + 50000000000000000000000000000, 50000000000000000000000000000 + ); - 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); - 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'); - // Send token to deposit in the deposit vault (this should be in a multi call with create_deposit) - start_prank(market.long_token, caller_address); - start_prank(market.short_token, caller_address); - IERC20Dispatcher { contract_address: market.long_token } - .approve(caller_address, 50000000000000000000000000000); - IERC20Dispatcher { contract_address: market.short_token } - .approve(caller_address, 50000000000000000000000000000); - - IERC20Dispatcher { contract_address: market.long_token } - .mint(caller_address, 50000000000000000000000000000); // 20 ETH - IERC20Dispatcher { contract_address: market.short_token } - .mint(caller_address, 50000000000000000000000000000); // 100 000 USDC - - // role_store.grant_role(exchange_router.contract_address, role::ROUTER_PLUGIN); - // role_store.grant_role(caller_address, role::ROUTER_PLUGIN); - - exchange_router - .send_tokens( - market.long_token, deposit_vault.contract_address, 50000000000000000000000000000 - ); - exchange_router - .send_tokens( - market.short_token, deposit_vault.contract_address, 50000000000000000000000000000 - ); - - stop_prank(market.long_token); - stop_prank(market.short_token); - - // Create Deposit - - let addresss_zero: ContractAddress = 0.try_into().unwrap(); - - 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(); - - start_roll(deposit_handler.contract_address, 1910); - let key = deposit_handler.create_deposit(caller_address, params); - let first_deposit = data_store.get_deposit(key); - - 'created deposit'.print(); - - 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 { - signer_info: 0, - 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![4000, 1], // 500000, 10000 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); - - role_store.grant_role(caller_address, role::ORDER_KEEPER); - role_store.grant_role(caller_address, role::ROLE_ADMIN); - role_store.grant_role(exchange_router.contract_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'); - - 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, @@ -745,203 +562,20 @@ fn test_takeprofit_long() { withdrawal_handler, withdrawal_vault, liquidation_handler, + market, ) = - setup(); - - // ********************************************************************************************* - // * TEST LOGIC * - // ********************************************************************************************* - - // Create a market. - let market = data_store.get_market(create_market(market_factory)); - - // 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); - - // 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 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 - ); - data_store.set_u256(keys::reserve_factor_key(market.market_token, true), 1000000000000000000); - data_store - .set_u256( - keys::open_interest_reserve_factor_key(market.market_token, true), 1000000000000000000 - ); - - data_store.set_bool('REENTRANCY_GUARD_STATUS', false); - - '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(); - - 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(); - - // INITIAL LONG TOKEN IN POOL : 5 ETH - // INITIAL SHORT TOKEN IN POOL : 25000 USDC + deposit_setup( + 50000000000000000000000000000, 50000000000000000000000000000 + ); - 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); - 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'); - // Send token to deposit in the deposit vault (this should be in a multi call with create_deposit) - start_prank(market.long_token, caller_address); - start_prank(market.short_token, caller_address); - IERC20Dispatcher { contract_address: market.long_token } - .approve(caller_address, 50000000000000000000000000000); - IERC20Dispatcher { contract_address: market.short_token } - .approve(caller_address, 50000000000000000000000000000); - - IERC20Dispatcher { contract_address: market.long_token } - .mint(caller_address, 50000000000000000000000000000); // 20 ETH - IERC20Dispatcher { contract_address: market.short_token } - .mint(caller_address, 50000000000000000000000000000); // 100 000 USDC - - // role_store.grant_role(exchange_router.contract_address, role::ROUTER_PLUGIN); - // role_store.grant_role(caller_address, role::ROUTER_PLUGIN); - - exchange_router - .send_tokens( - market.long_token, deposit_vault.contract_address, 50000000000000000000000000000 - ); - exchange_router - .send_tokens( - market.short_token, deposit_vault.contract_address, 50000000000000000000000000000 - ); - - stop_prank(market.long_token); - stop_prank(market.short_token); - - // Create Deposit - - let addresss_zero: ContractAddress = 0.try_into().unwrap(); - - 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(); - - start_roll(deposit_handler.contract_address, 1910); - let key = deposit_handler.create_deposit(caller_address, params); - let first_deposit = data_store.get_deposit(key); - - 'created deposit'.print(); - - 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 { - signer_info: 0, - 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![4000, 1], // 500000, 10000 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); - - role_store.grant_role(caller_address, role::ORDER_KEEPER); - role_store.grant_role(caller_address, role::ROLE_ADMIN); - role_store.grant_role(exchange_router.contract_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'); - - 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, @@ -1400,203 +1034,20 @@ fn test_takeprofit_long_increase_fails() { withdrawal_handler, withdrawal_vault, liquidation_handler, + market, ) = - setup(); - - // ********************************************************************************************* - // * TEST LOGIC * - // ********************************************************************************************* - - // Create a market. - let market = data_store.get_market(create_market(market_factory)); - - // 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); - - // 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 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 - ); - data_store.set_u256(keys::reserve_factor_key(market.market_token, true), 1000000000000000000); - data_store - .set_u256( - keys::open_interest_reserve_factor_key(market.market_token, true), 1000000000000000000 - ); - - data_store.set_bool('REENTRANCY_GUARD_STATUS', false); - - '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(); - - 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(); - - // INITIAL LONG TOKEN IN POOL : 5 ETH - // INITIAL SHORT TOKEN IN POOL : 25000 USDC + deposit_setup( + 50000000000000000000000000000, 50000000000000000000000000000 + ); - 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); - 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'); - // Send token to deposit in the deposit vault (this should be in a multi call with create_deposit) - start_prank(market.long_token, caller_address); - start_prank(market.short_token, caller_address); - IERC20Dispatcher { contract_address: market.long_token } - .approve(caller_address, 50000000000000000000000000000); - IERC20Dispatcher { contract_address: market.short_token } - .approve(caller_address, 50000000000000000000000000000); - - IERC20Dispatcher { contract_address: market.long_token } - .mint(caller_address, 50000000000000000000000000000); // 20 ETH - IERC20Dispatcher { contract_address: market.short_token } - .mint(caller_address, 50000000000000000000000000000); // 100 000 USDC - - // role_store.grant_role(exchange_router.contract_address, role::ROUTER_PLUGIN); - // role_store.grant_role(caller_address, role::ROUTER_PLUGIN); - - exchange_router - .send_tokens( - market.long_token, deposit_vault.contract_address, 50000000000000000000000000000 - ); - exchange_router - .send_tokens( - market.short_token, deposit_vault.contract_address, 50000000000000000000000000000 - ); - - stop_prank(market.long_token); - stop_prank(market.short_token); - - // Create Deposit - - let addresss_zero: ContractAddress = 0.try_into().unwrap(); - - 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(); - - start_roll(deposit_handler.contract_address, 1910); - let key = deposit_handler.create_deposit(caller_address, params); - let first_deposit = data_store.get_deposit(key); - - 'created deposit'.print(); - - 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 { - signer_info: 0, - 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![4000, 1], // 500000, 10000 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); - - role_store.grant_role(caller_address, role::ORDER_KEEPER); - role_store.grant_role(caller_address, role::ROLE_ADMIN); - role_store.grant_role(exchange_router.contract_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'); - - 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, @@ -2055,203 +1506,20 @@ fn test_takeprofit_long_decrease_fails() { withdrawal_handler, withdrawal_vault, liquidation_handler, + market, ) = - setup(); + deposit_setup( + 50000000000000000000000000000, 50000000000000000000000000000 + ); - // ********************************************************************************************* - // * TEST LOGIC * - // ********************************************************************************************* + 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); - // Create a market. - let market = data_store.get_market(create_market(market_factory)); - - // 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); - - // 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 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 - ); - data_store.set_u256(keys::reserve_factor_key(market.market_token, true), 1000000000000000000); - data_store - .set_u256( - keys::open_interest_reserve_factor_key(market.market_token, true), 1000000000000000000 - ); - - data_store.set_bool('REENTRANCY_GUARD_STATUS', false); - - '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(); - - 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(); - - // 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); - - 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'); - // Send token to deposit in the deposit vault (this should be in a multi call with create_deposit) - start_prank(market.long_token, caller_address); - start_prank(market.short_token, caller_address); - IERC20Dispatcher { contract_address: market.long_token } - .approve(caller_address, 50000000000000000000000000000); - IERC20Dispatcher { contract_address: market.short_token } - .approve(caller_address, 50000000000000000000000000000); - - IERC20Dispatcher { contract_address: market.long_token } - .mint(caller_address, 50000000000000000000000000000); // 20 ETH - IERC20Dispatcher { contract_address: market.short_token } - .mint(caller_address, 50000000000000000000000000000); // 100 000 USDC - - // role_store.grant_role(exchange_router.contract_address, role::ROUTER_PLUGIN); - // role_store.grant_role(caller_address, role::ROUTER_PLUGIN); - - exchange_router - .send_tokens( - market.long_token, deposit_vault.contract_address, 50000000000000000000000000000 - ); - exchange_router - .send_tokens( - market.short_token, deposit_vault.contract_address, 50000000000000000000000000000 - ); - - stop_prank(market.long_token); - stop_prank(market.short_token); - - // Create Deposit - - let addresss_zero: ContractAddress = 0.try_into().unwrap(); - - 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(); - - start_roll(deposit_handler.contract_address, 1910); - let key = deposit_handler.create_deposit(caller_address, params); - let first_deposit = data_store.get_deposit(key); - - 'created deposit'.print(); - - 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 { - signer_info: 0, - 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![4000, 1], // 500000, 10000 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); - - role_store.grant_role(caller_address, role::ORDER_KEEPER); - role_store.grant_role(caller_address, role::ROLE_ADMIN); - role_store.grant_role(exchange_router.contract_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'); - - 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, @@ -2710,203 +1978,20 @@ fn test_takeprofit_long_close_fails() { withdrawal_handler, withdrawal_vault, liquidation_handler, + market, ) = - setup(); - - // ********************************************************************************************* - // * TEST LOGIC * - // ********************************************************************************************* - - // Create a market. - let market = data_store.get_market(create_market(market_factory)); - - // 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); - - // 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 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 - ); - data_store.set_u256(keys::reserve_factor_key(market.market_token, true), 1000000000000000000); - data_store - .set_u256( - keys::open_interest_reserve_factor_key(market.market_token, true), 1000000000000000000 - ); - - data_store.set_bool('REENTRANCY_GUARD_STATUS', false); - - '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(); - - 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(); - - // INITIAL LONG TOKEN IN POOL : 5 ETH - // INITIAL SHORT TOKEN IN POOL : 25000 USDC + deposit_setup( + 50000000000000000000000000000, 50000000000000000000000000000 + ); - 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); - 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'); - // Send token to deposit in the deposit vault (this should be in a multi call with create_deposit) - start_prank(market.long_token, caller_address); - start_prank(market.short_token, caller_address); - IERC20Dispatcher { contract_address: market.long_token } - .approve(caller_address, 50000000000000000000000000000); - IERC20Dispatcher { contract_address: market.short_token } - .approve(caller_address, 50000000000000000000000000000); - - IERC20Dispatcher { contract_address: market.long_token } - .mint(caller_address, 50000000000000000000000000000); // 20 ETH - IERC20Dispatcher { contract_address: market.short_token } - .mint(caller_address, 50000000000000000000000000000); // 100 000 USDC - - // role_store.grant_role(exchange_router.contract_address, role::ROUTER_PLUGIN); - // role_store.grant_role(caller_address, role::ROUTER_PLUGIN); - - exchange_router - .send_tokens( - market.long_token, deposit_vault.contract_address, 50000000000000000000000000000 - ); - exchange_router - .send_tokens( - market.short_token, deposit_vault.contract_address, 50000000000000000000000000000 - ); - - stop_prank(market.long_token); - stop_prank(market.short_token); - - // Create Deposit - - let addresss_zero: ContractAddress = 0.try_into().unwrap(); - - 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(); - - start_roll(deposit_handler.contract_address, 1910); - let key = deposit_handler.create_deposit(caller_address, params); - let first_deposit = data_store.get_deposit(key); - - 'created deposit'.print(); - - 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 { - signer_info: 0, - 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![4000, 1], // 500000, 10000 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); - - role_store.grant_role(caller_address, role::ORDER_KEEPER); - role_store.grant_role(caller_address, role::ROLE_ADMIN); - role_store.grant_role(exchange_router.contract_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'); - - 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, @@ -3364,189 +2449,20 @@ fn test_long_liquidation() { withdrawal_handler, withdrawal_vault, liquidation_handler, + market, ) = - setup(); - - // ********************************************************************************************* - // * TEST LOGIC * - // ********************************************************************************************* - - // Create a market. - let market = data_store.get_market(create_market(market_factory)); - - // 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); - - // 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 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 - ); - data_store.set_u256(keys::reserve_factor_key(market.market_token, true), 1000000000000000000); - data_store - .set_u256( - keys::open_interest_reserve_factor_key(market.market_token, true), 1000000000000000000 - ); - - data_store.set_bool('REENTRANCY_GUARD_STATUS', false); - - '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(); - - 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(); - - // INITIAL LONG TOKEN IN POOL : 5 ETH - // INITIAL SHORT TOKEN IN POOL : 25000 USDC + deposit_setup( + 50000000000000000000000000000, 50000000000000000000000000000 + ); - 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); - 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'); - // Send token to deposit in the deposit vault (this should be in a multi call with create_deposit) - start_prank(market.long_token, caller_address); - start_prank(market.short_token, caller_address); - IERC20Dispatcher { contract_address: market.long_token } - .approve(caller_address, 50000000000000000000000000000); - IERC20Dispatcher { contract_address: market.short_token } - .approve(caller_address, 50000000000000000000000000000); - - IERC20Dispatcher { contract_address: market.long_token } - .mint(caller_address, 50000000000000000000000000000); // 20 ETH - IERC20Dispatcher { contract_address: market.short_token } - .mint(caller_address, 50000000000000000000000000000); // 100 000 USDC - - // role_store.grant_role(exchange_router.contract_address, role::ROUTER_PLUGIN); - // role_store.grant_role(caller_address, role::ROUTER_PLUGIN); - - exchange_router - .send_tokens( - market.long_token, deposit_vault.contract_address, 50000000000000000000000000000 - ); - exchange_router - .send_tokens( - market.short_token, deposit_vault.contract_address, 50000000000000000000000000000 - ); - - stop_prank(market.long_token); - stop_prank(market.short_token); - - // Create Deposit - - let addresss_zero: ContractAddress = 0.try_into().unwrap(); - - 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(); - - start_roll(deposit_handler.contract_address, 1910); - let key = deposit_handler.create_deposit(caller_address, params); - let first_deposit = data_store.get_deposit(key); - - 'created deposit'.print(); - - 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 { - signer_info: 0, - 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![4000, 1], // 500000, 10000 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); - - role_store.grant_role(caller_address, role::ORDER_KEEPER); - role_store.grant_role(caller_address, role::ROLE_ADMIN); - role_store.grant_role(exchange_router.contract_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 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'); - - 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, @@ -3811,8 +2727,6 @@ fn test_long_liquidation() { teardown(data_store, market_factory); } -///////////////////////////// LEVERAGE TESTS /////////////////////////////////// - #[test] fn test_long_leverage_positif_close() { // ********************************************************************************************* @@ -3839,203 +2753,20 @@ fn test_long_leverage_positif_close() { withdrawal_handler, withdrawal_vault, liquidation_handler, + market, ) = - setup(); - - // ********************************************************************************************* - // * TEST LOGIC * - // ********************************************************************************************* - - // Create a market. - let market = data_store.get_market(create_market(market_factory)); - - // 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); - - // 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 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 - ); - data_store.set_u256(keys::reserve_factor_key(market.market_token, true), 1000000000000000000); - data_store - .set_u256( - keys::open_interest_reserve_factor_key(market.market_token, true), 1000000000000000000 - ); - - data_store.set_bool('REENTRANCY_GUARD_STATUS', false); - - '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(); - - 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(); - - // INITIAL LONG TOKEN IN POOL : 5 ETH - // INITIAL SHORT TOKEN IN POOL : 25000 USDC + deposit_setup( + 50000000000000000000000000000, 50000000000000000000000000000 + ); - 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); - 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'); - // Send token to deposit in the deposit vault (this should be in a multi call with create_deposit) - start_prank(market.long_token, caller_address); - start_prank(market.short_token, caller_address); - IERC20Dispatcher { contract_address: market.long_token } - .approve(caller_address, 50000000000000000000000000000); - IERC20Dispatcher { contract_address: market.short_token } - .approve(caller_address, 50000000000000000000000000000); - - IERC20Dispatcher { contract_address: market.long_token } - .mint(caller_address, 50000000000000000000000000000); // 20 ETH - IERC20Dispatcher { contract_address: market.short_token } - .mint(caller_address, 50000000000000000000000000000); // 100 000 USDC - - // role_store.grant_role(exchange_router.contract_address, role::ROUTER_PLUGIN); - // role_store.grant_role(caller_address, role::ROUTER_PLUGIN); - - exchange_router - .send_tokens( - market.long_token, deposit_vault.contract_address, 50000000000000000000000000000 - ); - exchange_router - .send_tokens( - market.short_token, deposit_vault.contract_address, 50000000000000000000000000000 - ); - - stop_prank(market.long_token); - stop_prank(market.short_token); - - // Create Deposit - - let addresss_zero: ContractAddress = 0.try_into().unwrap(); - - 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(); - - start_roll(deposit_handler.contract_address, 1910); - let key = deposit_handler.create_deposit(caller_address, params); - let first_deposit = data_store.get_deposit(key); - - 'created deposit'.print(); - - 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 { - signer_info: 0, - 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![4000, 1], // 500000, 10000 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); - - role_store.grant_role(caller_address, role::ORDER_KEEPER); - role_store.grant_role(caller_address, role::ROLE_ADMIN); - role_store.grant_role(exchange_router.contract_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'); - - 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, @@ -4290,189 +3021,20 @@ fn test_long_leverage_liquidation() { withdrawal_handler, withdrawal_vault, liquidation_handler, + market, ) = - setup(); - - // ********************************************************************************************* - // * TEST LOGIC * - // ********************************************************************************************* - - // Create a market. - let market = data_store.get_market(create_market(market_factory)); - - // 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); - - // 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 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 - ); - data_store.set_u256(keys::reserve_factor_key(market.market_token, true), 1000000000000000000); - data_store - .set_u256( - keys::open_interest_reserve_factor_key(market.market_token, true), 1000000000000000000 - ); - - data_store.set_bool('REENTRANCY_GUARD_STATUS', false); - - '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(); - - 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(); - - // INITIAL LONG TOKEN IN POOL : 5 ETH - // INITIAL SHORT TOKEN IN POOL : 25000 USDC + deposit_setup( + 50000000000000000000000000000, 50000000000000000000000000000 + ); - 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); - 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'); - // Send token to deposit in the deposit vault (this should be in a multi call with create_deposit) - start_prank(market.long_token, caller_address); - start_prank(market.short_token, caller_address); - IERC20Dispatcher { contract_address: market.long_token } - .approve(caller_address, 50000000000000000000000000000); - IERC20Dispatcher { contract_address: market.short_token } - .approve(caller_address, 50000000000000000000000000000); - - IERC20Dispatcher { contract_address: market.long_token } - .mint(caller_address, 50000000000000000000000000000); // 20 ETH - IERC20Dispatcher { contract_address: market.short_token } - .mint(caller_address, 50000000000000000000000000000); // 100 000 USDC - - // role_store.grant_role(exchange_router.contract_address, role::ROUTER_PLUGIN); - // role_store.grant_role(caller_address, role::ROUTER_PLUGIN); - - exchange_router - .send_tokens( - market.long_token, deposit_vault.contract_address, 50000000000000000000000000000 - ); - exchange_router - .send_tokens( - market.short_token, deposit_vault.contract_address, 50000000000000000000000000000 - ); - - stop_prank(market.long_token); - stop_prank(market.short_token); - - // Create Deposit - - let addresss_zero: ContractAddress = 0.try_into().unwrap(); - - 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(); - - start_roll(deposit_handler.contract_address, 1910); - let key = deposit_handler.create_deposit(caller_address, params); - let first_deposit = data_store.get_deposit(key); - - 'created deposit'.print(); - - 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 { - signer_info: 0, - 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![4000, 1], // 500000, 10000 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); - - role_store.grant_role(caller_address, role::ORDER_KEEPER); - role_store.grant_role(caller_address, role::ROLE_ADMIN); - role_store.grant_role(exchange_router.contract_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 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'); - - 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, diff --git a/tests/integration/test_short_integration.cairo b/tests/integration/test_short_integration.cairo index e56d29cf..dd8b3dcc 100644 --- a/tests/integration/test_short_integration.cairo +++ b/tests/integration/test_short_integration.cairo @@ -35,7 +35,7 @@ use satoru::market::market::{Market, UniqueIdMarket}; use satoru::market::market_token::{IMarketTokenDispatcher, IMarketTokenDispatcherTrait}; use satoru::role::role; use satoru::oracle::oracle_utils::SetPricesParams; -use satoru::tests_lib; +use satoru::test_utils::tests_lib; use satoru::deposit::deposit_utils::CreateDepositParams; use satoru::utils::span32::{Span32, DefaultSpan32, Array32Trait}; use satoru::deposit::deposit_utils; @@ -64,7 +64,7 @@ use satoru::market::{market::{UniqueIdMarketImpl},}; use satoru::exchange::order_handler::{ OrderHandler, IOrderHandlerDispatcher, IOrderHandlerDispatcherTrait }; -use satoru::tests_lib::{setup, create_market, teardown}; +use satoru::test_utils::{tests_lib::{setup, create_market, teardown}, deposit_setup::deposit_setup}; const INITIAL_TOKENS_MINTED: felt252 = 1000; #[test] @@ -93,202 +93,41 @@ fn test_short_increase_decrease_close() { withdrawal_handler, withdrawal_vault, liquidation_handler, + market, ) = - setup(); - - // ********************************************************************************************* - // * TEST LOGIC * - // ********************************************************************************************* - - // Create a market. - let market = data_store.get_market(create_market(market_factory)); - - // 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); - - // 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 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, false), - 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, false), - 50000000000000000000000000000000000000000000000 - ); - data_store.set_u256(keys::reserve_factor_key(market.market_token, false), 1000000000000000000); - data_store - .set_u256( - keys::open_interest_reserve_factor_key(market.market_token, false), 1000000000000000000 - ); - - data_store.set_bool('REENTRANCY_GUARD_STATUS', false); - - '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(); - - 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(); - - // INITIAL LONG TOKEN IN POOL : 5 ETH - // INITIAL SHORT TOKEN IN POOL : 25000 USDC + deposit_setup( + 50000000000000000000000000000, 50000000000000000000000000000 + ); - 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); - 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'); - // Send token to deposit in the deposit vault (this should be in a multi call with create_deposit) - start_prank(market.long_token, caller_address); - start_prank(market.short_token, caller_address); - IERC20Dispatcher { contract_address: market.long_token } - .approve(caller_address, 50000000000000000000000000000); - IERC20Dispatcher { contract_address: market.short_token } - .approve(caller_address, 50000000000000000000000000000); - - IERC20Dispatcher { contract_address: market.long_token } - .mint(caller_address, 50000000000000000000000000000); // 20 ETH - IERC20Dispatcher { contract_address: market.short_token } - .mint(caller_address, 50000000000000000000000000000); // 100 000 USDC - - // role_store.grant_role(exchange_router.contract_address, role::ROUTER_PLUGIN); - // role_store.grant_role(caller_address, role::ROUTER_PLUGIN); - - exchange_router - .send_tokens( - market.long_token, deposit_vault.contract_address, 50000000000000000000000000000 - ); - exchange_router - .send_tokens( - market.short_token, deposit_vault.contract_address, 50000000000000000000000000000 - ); - - stop_prank(market.long_token); - stop_prank(market.short_token); - - // Create Deposit - - let addresss_zero: ContractAddress = 0.try_into().unwrap(); - - 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(); - - start_roll(deposit_handler.contract_address, 1910); - let key = deposit_handler.create_deposit(caller_address, params); - let first_deposit = data_store.get_deposit(key); - - 'created deposit'.print(); + let pool_value_info = market_utils::get_pool_value_info( + data_store, + market, + Price { min: 3500, max: 3500, }, + Price { min: 3500, max: 3500, }, + Price { min: 1, max: 1, }, + keys::max_pnl_factor_for_deposits(), + true, + ); - 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' + pool_value_info.pool_value.mag == 175050000000000000000000000000000, 'wrong pool value 1' ); assert( - first_deposit.initial_short_token_amount == 50000000000000000000000000000, - 'Wrong init short token amount' + pool_value_info.long_token_amount == 50000000000000000000000000000, + 'wrong long token amount 1' + ); + assert( + pool_value_info.short_token_amount == 50000000000000000000000000000, + 'wrong short token amount 1' ); - - let price_params = SetPricesParams { - signer_info: 0, - 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![4000, 1], // 500000, 10000 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); - - role_store.grant_role(caller_address, role::ORDER_KEEPER); - role_store.grant_role(caller_address, role::ROLE_ADMIN); - role_store.grant_role(exchange_router.contract_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 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'); - - 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: 5000, max: 5000, }, - // Price { min: 5000, max: 5000, }, - // Price { min: 1, max: 1, }, - // keys::max_pnl_factor_for_deposits(), - // true, - // ); - - // 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 // ************************************* TEST SHORT ********************************************* @@ -309,7 +148,6 @@ fn test_short_increase_decrease_close() { let balance_caller_USDC = IERC20Dispatcher { contract_address: market.short_token } .balance_of(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'); @@ -355,7 +193,6 @@ fn test_short_increase_decrease_close() { let balance_caller_USDC = IERC20Dispatcher { contract_address: market.short_token } .balance_of(caller_address); - assert(balance_deposit_vault_before == 0, 'balance deposit should be 0'); assert(balance_caller_ETH == 10000000000000000000, 'balanc ETH caller 10 ETH'); assert(balance_caller_USDC == 43000000000000000000000, 'USDC be 43 000 USDC'); @@ -422,7 +259,6 @@ fn test_short_increase_decrease_close() { let balance_caller_USDC = IERC20Dispatcher { contract_address: market.short_token } .balance_of(caller_address); - assert(balance_deposit_vault_before == 0, 'balance deposit should be 0'); assert(balance_caller_ETH == 10000000000000000000, 'balanc ETH caller 10 ETH'); assert(balance_caller_USDC == 43000000000000000000000, 'USDC caller 43000 USDC'); diff --git a/tests/integration/test_swap_integration.cairo b/tests/integration/test_swap_integration.cairo index 6cbc685f..71c97a38 100644 --- a/tests/integration/test_swap_integration.cairo +++ b/tests/integration/test_swap_integration.cairo @@ -34,7 +34,7 @@ use satoru::market::market::{Market, UniqueIdMarket}; use satoru::market::market_token::{IMarketTokenDispatcher, IMarketTokenDispatcherTrait}; use satoru::role::role; use satoru::oracle::oracle_utils::SetPricesParams; -use satoru::tests_lib; +use satoru::test_utils::tests_lib; use satoru::deposit::deposit_utils::CreateDepositParams; use satoru::utils::span32::{Span32, DefaultSpan32, Array32Trait}; use satoru::deposit::deposit_utils;