diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 780897f0..b9880eec 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -15,7 +15,7 @@ jobs: scarb-version: "2.3.1" - uses: foundry-rs/setup-snfoundry@v2 with: - starknet-foundry-version: 0.9.1 + starknet-foundry-version: 0.10.1 - name: Run cairo tests run: snforge test # - name: Set up Scarb diff --git a/Scarb.lock b/Scarb.lock index 2c57af1a..c913aead 100644 --- a/Scarb.lock +++ b/Scarb.lock @@ -49,4 +49,4 @@ dependencies = [ [[package]] name = "snforge_std" version = "0.1.0" -source = "git+https://github.com/foundry-rs/starknet-foundry.git?tag=v0.9.1#da085bd11e1b151d0592f43917136560d9b70d37" +source = "git+https://github.com/foundry-rs/starknet-foundry.git?tag=v0.10.1#da085bd11e1b151d0592f43917136560d9b70d37" diff --git a/Scarb.toml b/Scarb.toml index 658010c8..60b0d17d 100644 --- a/Scarb.toml +++ b/Scarb.toml @@ -23,7 +23,7 @@ alexandria_data_structures = { git = "https://github.com/keep-starknet-strange/a alexandria_math = { git = "https://github.com/keep-starknet-strange/alexandria.git", tag = "cairo-v2.3.0-rc0" } alexandria_storage = { git = "https://github.com/keep-starknet-strange/alexandria.git", tag = "cairo-v2.3.0-rc0" } alexandria_sorting = { git = "https://github.com/keep-starknet-strange/alexandria.git", tag = "cairo-v2.3.0-rc0" } -snforge_std = { git = "https://github.com/foundry-rs/starknet-foundry.git", tag = "v0.9.1" } +snforge_std = { git = "https://github.com/foundry-rs/starknet-foundry.git", tag = "v0.10.1" } [tool.snforge] diff --git a/book/src/continuous-integration/workflows.md b/book/src/continuous-integration/workflows.md index 7a2836ad..77fd8d64 100644 --- a/book/src/continuous-integration/workflows.md +++ b/book/src/continuous-integration/workflows.md @@ -206,7 +206,7 @@ jobs: - uses: actions/checkout@v3 - uses: foundry-rs/setup-snfoundry@v1 with: - starknet-foundry-version: 0.9.1 + starknet-foundry-version: 0.10.1 - uses: software-mansion/setup-scarb@v1 with: scarb-version: "0.7.0" diff --git a/tests/position/test_position_utils.cairo b/tests/position/test_position_utils.cairo index 6b238394..3bd7a1c7 100644 --- a/tests/position/test_position_utils.cairo +++ b/tests/position/test_position_utils.cairo @@ -26,7 +26,10 @@ use satoru::market::market_token::{IMarketTokenDispatcher, IMarketTokenDispatche use satoru::data::keys; use satoru::role::role; use satoru::price::price::{Price, PriceTrait}; -use satoru::position::{position::Position, position_utils::UpdatePositionParams, position_utils}; +use satoru::position::{ + position::Position, position_utils::UpdatePositionParams, + position_utils::WillPositionCollateralBeSufficientValues, position_utils +}; use satoru::tests_lib::{setup, setup_event_emitter, teardown}; use satoru::mock::referral_storage::{IReferralStorageDispatcher, IReferralStorageDispatcherTrait}; use satoru::pricing::position_pricing_utils::{PositionFees, PositionReferralFees}; @@ -37,7 +40,7 @@ use satoru::order::{ use satoru::oracle::oracle::{IOracleDispatcher, IOracleDispatcherTrait}; use satoru::swap::swap_handler::{ISwapHandlerDispatcher, ISwapHandlerDispatcherTrait}; use satoru::order::base_order_utils::ExecuteOrderParamsContracts; -use satoru::utils::i128::{i128}; +use satoru::utils::i128::{i128, i128_new}; #[test] fn given_normal_conditions_when_get_position_key_then_works() { // @@ -679,11 +682,69 @@ fn given_valid_referral_when_handling_then_referral_successfully_processed() { assert(affiliate_reward_value == 30, 'Invalide affiliate reward value') } -//TODO -// #[test] -// fn test_will_position_collateral_be_sufficient() { -//} + +#[test] +fn test_will_position_collateral_be_sufficient() { + // Setup + let (caller_address, role_store, data_store) = setup(); + + let market_token: ContractAddress = contract_address_const::<'market_token'>(); + let long_token: ContractAddress = contract_address_const::<'long_token'>(); + let short_token: ContractAddress = contract_address_const::<'short_token'>(); + let market: Market = Market { market_token, index_token: long_token, long_token, short_token }; + + let long_token_price = Price { min: 100, max: 110 }; + let index_token_price = Price { min: 100, max: 110 }; + let short_token_price = Price { min: 100, max: 110 }; + + // setting long interest greater than te position size in USD... + let open_interest_key = keys::open_interest_key(market_token, long_token, true); + data_store.set_u128(open_interest_key, 15000); + + // setting a min collateral factor for the market + let min_collateral_factor_key = keys::min_collateral_factor_key(market_token); + data_store.set_u128(min_collateral_factor_key, 10_000_000_000_000_000_000); + + let prices: MarketPrices = MarketPrices { + index_token_price: index_token_price, + long_token_price: long_token_price, + short_token_price: short_token_price + }; + + let values: WillPositionCollateralBeSufficientValues = + WillPositionCollateralBeSufficientValues { + position_size_in_usd: 1000, + position_collateral_amount: 50, + realized_pnl_usd: i128_new(10, true), + open_interest_delta: i128_new(5, true), + }; + + // invoke the function with scenario where collateral will be sufficient + let (will_be_sufficient, remaining_collateral_usd) = + position_utils::will_position_collateral_be_sufficient( + data_store, market, prices, long_token, true, values + ); + assert(will_be_sufficient, 'collateral supposed sufficient'); + assert(remaining_collateral_usd == i128_new(4990, false), 'eq 4990'); + + let values: WillPositionCollateralBeSufficientValues = + WillPositionCollateralBeSufficientValues { + position_size_in_usd: 1000, + position_collateral_amount: 5, + realized_pnl_usd: i128_new(410, true), + open_interest_delta: i128_new(5, true), + }; + + // invoke the function with scenario where collateral will be insufficient + let (will_be_sufficient, remaining_collateral_usd) = + position_utils::will_position_collateral_be_sufficient( + data_store, market, prices, long_token, true, values + ); + // assert that the function returns that the collateral is not sufficient + assert(!will_be_sufficient, 'collateral should insufficient'); + assert(remaining_collateral_usd == i128_new(90, false), 'eq 90'); +} //TODO // #[test] // fn test_update_funding_and_borrowing_state() {