Skip to content

Commit

Permalink
add tests for src/amm_core/oracles/agg.cairo::get_current_price
Browse files Browse the repository at this point in the history
  • Loading branch information
nikanor.goreglyad committed Jul 29, 2024
1 parent fa33021 commit e5e2e8e
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 6 deletions.
98 changes: 93 additions & 5 deletions src/amm_core/oracles/agg.cairo
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
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
};

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
Expand All @@ -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,
Expand All @@ -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.');
}
}
8 changes: 7 additions & 1 deletion tests/test_set_balances.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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',))]
Expand Down Expand Up @@ -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(),
Expand Down

0 comments on commit e5e2e8e

Please sign in to comment.