From e5e2e8e32d51bcddb2a252c98b898fb8297a690e Mon Sep 17 00:00:00 2001 From: "nikanor.goreglyad" Date: Mon, 29 Jul 2024 09:03:42 +0200 Subject: [PATCH] add tests for src/amm_core/oracles/agg.cairo::get_current_price --- src/amm_core/oracles/agg.cairo | 98 ++++++++++++++++++++++++++++++++-- tests/test_set_balances.cairo | 8 ++- 2 files changed, 100 insertions(+), 6 deletions(-) diff --git a/src/amm_core/oracles/agg.cairo b/src/amm_core/oracles/agg.cairo index 22a5ad6..986122b 100644 --- a/src/amm_core/oracles/agg.cairo +++ b/src/amm_core/oracles/agg.cairo @@ -1,9 +1,10 @@ mod OracleAgg { use starknet::ContractAddress; + use starknet::get_block_info; + use cubit::f128::types::fixed::{Fixed}; use carmine_protocol::types::basic::{Timestamp}; - use carmine_protocol::amm_core::oracles::pragma::Pragma::{ get_pragma_median_price, get_pragma_terminal_price }; @@ -11,9 +12,6 @@ mod OracleAgg { use carmine_protocol::amm_core::state::State::read_latest_oracle_price; use carmine_protocol::amm_core::state::State::write_latest_oracle_price; - use starknet::get_block_info; - - // @notice Returns current spot price for given ticker (quote and base token) // @param quote_token_addr: Address of quote token in given ticker // @param base_token_addr: Address of base token in given ticker @@ -31,7 +29,6 @@ mod OracleAgg { last_price } else { let price_pragma = get_pragma_median_price(quote_token_addr, base_token_addr); - write_latest_oracle_price( base_token_addr, quote_token_addr, @@ -55,3 +52,94 @@ mod OracleAgg { price_pragma } } + +// Tests -------------------------------------------------------------------------------------------------------------- + +#[cfg(test)] +mod tests { + use debug::PrintTrait; + + use starknet::{ContractAddress, get_block_info}; + use cubit::f128::types::fixed::{Fixed, FixedTrait}; + use carmine_protocol::testing::setup::deploy_setup; + use carmine_protocol::amm_core::oracles::agg::OracleAgg; + use carmine_protocol::amm_core::state::State; + use carmine_protocol::amm_core::oracles::pragma::Pragma::PRAGMA_ORACLE_ADDRESS; + use carmine_protocol::amm_core::oracles::pragma::PragmaUtils::{PragmaPricesResponse}; + use carmine_protocol::amm_core::state::State::{write_latest_oracle_price, read_latest_oracle_price}; + use snforge_std::{start_prank, stop_prank, start_mock_call, stop_mock_call, start_warp, stop_warp}; + + fn assert_eq(a: Fixed, b: Fixed, err_msg: felt252) { + assert(a == b, err_msg); + } + + #[test] + fn test_get_current_price() { + let (ctx, dsps) = deploy_setup(); + // Setup: Create mock addresses + let quote_token_addr = starknet::contract_address_const::<0x1>(); + let base_token_addr = starknet::contract_address_const::<0x2>(); + + // Test case 1: When last_price_block_num == curr_block + write_latest_oracle_price( + base_token_addr, + quote_token_addr, + FixedTrait::from_unscaled_felt(1000), + 2000 + ); + + let result1 = OracleAgg::get_current_price(quote_token_addr, base_token_addr); + assert(result1 == FixedTrait::from_unscaled_felt(1000), 'Wrong price when block matches'); + + // Test case 2: When last_price_block_num != curr_block + write_latest_oracle_price( + base_token_addr, + quote_token_addr, + FixedTrait::from_unscaled_felt(1000), + 1999 + ); + + // Mock Pragma oracle call + start_mock_call( + PRAGMA_ORACLE_ADDRESS.try_into().unwrap(), + 'get_data', + PragmaPricesResponse { + price: 150000000000, // 1500 with 8 decimals + decimals: 8, + last_updated_timestamp: 1000000000, + num_sources_aggregated: 3, + expiration_timestamp: Option::None(()) + } + ); + + let result2 = OracleAgg::get_current_price(quote_token_addr, base_token_addr); + assert(result2 == FixedTrait::from_unscaled_felt(1500), 'Wrong price from Pragma'); + + // Test case 3: When last_price_block_num != curr_block, price in state is updated + write_latest_oracle_price( + base_token_addr, + quote_token_addr, + FixedTrait::from_unscaled_felt(1000), + 1999 + ); + + // Mock Pragma oracle call + start_mock_call( + PRAGMA_ORACLE_ADDRESS.try_into().unwrap(), + 'get_data', + PragmaPricesResponse { + price: 150000000000, // 1500 with 8 decimals + decimals: 8, + last_updated_timestamp: 1000000000, + num_sources_aggregated: 3, + expiration_timestamp: Option::None(()) + } + ); + + let current_price = OracleAgg::get_current_price(quote_token_addr, base_token_addr); + let (new_price_in_state, last_block_in_state) = read_latest_oracle_price(quote_token_addr, base_token_addr); + new_price_in_state.print(); + last_block_in_state.print(); + assert(new_price_in_state == FixedTrait::from_unscaled_felt(1500), 'Price in state was not updated.'); + } +} diff --git a/tests/test_set_balances.cairo b/tests/test_set_balances.cairo index 4cb6816..40cd193 100644 --- a/tests/test_set_balances.cairo +++ b/tests/test_set_balances.cairo @@ -18,7 +18,7 @@ use carmine_protocol::amm_interface::{IAMMDispatcher, IAMMDispatcherTrait}; use carmine_protocol::testing::setup::{Ctx, Dispatchers}; use carmine_protocol::tokens::my_token::{MyToken, IMyTokenDispatcher, IMyTokenDispatcherTrait}; - +use carmine_protocol::amm_core::state::State::write_latest_oracle_price; #[test] #[should_panic(expected: ('Cant set lpool < locked',))] @@ -69,6 +69,12 @@ fn test_set_balance() { /////////////////////////////////////////////////// // Open a short trade /////////////////////////////////////////////////// + // write_latest_oracle_price( + // ctx.eth_address, + // ctx.usdc_address, + // FixedTrait::ZERO(), + // 0_u64 + // ); start_mock_call( PRAGMA_ORACLE_ADDRESS.try_into().unwrap(),