Skip to content

Commit

Permalink
Contract cleanup (#153)
Browse files Browse the repository at this point in the history
* remove old file

* re-order lib.cairo

* remove old function

* added a couple tests

- test only vault can call OptionRound::update_round_params()

- added test that both Vault & OptionRound update_round_parmas function as expected
  • Loading branch information
0xDegenDeveloper authored Jul 24, 2024
1 parent 9bd2639 commit 17550db
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 32 deletions.
24 changes: 13 additions & 11 deletions src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
mod types;

mod library {
mod eth;
mod red_black_tree;
mod utils;
}


mod vault {
mod interface;
mod contract;
Expand All @@ -14,20 +23,13 @@ mod market_aggregator {
mod types;
}

mod library {
mod eth;
mod red_black_tree;
mod utils;
}

mod types;

#[cfg(test)]
mod tests;


// @note Refactor these into their own modules
mod contracts {
mod pitch_lake;
mod lp_token;
}

#[cfg(test)]
mod tests;

Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use pitch_lake_starknet::{
facades::{
vault_facade::{VaultFacade, VaultFacadeTrait},
option_round_facade::{OptionRoundFacade, OptionRoundFacadeTrait, OptionRoundParams},
market_aggregator_facade::{MarketAggregatorFacade, MarketAggregatorFacadeTrait},
},
},
}
Expand Down Expand Up @@ -75,3 +76,76 @@ fn test_only_vault_can_settle_option_round() {
current_round.settle_option_round_expect_error(0x123, err);
}


#[test]
#[available_gas(50000000)]
fn test_only_vault_can_update_round_params() {
let (mut vault, _) = setup_facade();
let mut round = vault.get_current_round();

set_contract_address(not_vault());
round.update_round_params_expect_error(err);
}

#[test]
#[available_gas(50000000)]
fn test_update_round_params_on_round() {
let (mut vault, _) = setup_facade();
let mut round = vault.get_current_round();

let reserve_price0 = round.get_reserve_price();
let cap_level0 = round.get_cap_level();
let strike_price0 = round.get_strike_price();

set_contract_address(vault.contract_address());
round.update_params(1, 2, 3);

let reserve_price = round.get_reserve_price();
let cap_level = round.get_cap_level();
let strike_price = round.get_strike_price();

assert_eq!(reserve_price, 1);
assert_eq!(cap_level, 2);
assert_eq!(strike_price, 3);
assert(reserve_price != reserve_price0, 'reserve price did not change');
assert(cap_level != cap_level0, 'cap level did not change');
assert(strike_price != strike_price0, 'strike price did not change');
}

#[test]
#[available_gas(50000000)]
fn test_update_round_params_on_vault() {
let (mut vault, _) = setup_facade();
let mut round = vault.get_current_round();

let reserve_price0 = round.get_reserve_price();
let cap_level0 = round.get_cap_level();
let strike_price0 = round.get_strike_price();

// Mock values on mk agg
let mk_agg = vault.get_market_aggregator_facade();
let from = round.get_auction_start_date();
let to = round.get_option_settlement_date();

let new_reserve_price = 1;
let new_cap_level = 2;
let new_strike_price = 3;

mk_agg.set_reserve_price_for_time_period(from, to, new_reserve_price);
mk_agg.set_cap_level_for_time_period(from, to, new_cap_level);
mk_agg.set_strike_price_for_time_period(from, to, new_strike_price);

vault.update_round_params();

let reserve_price = round.get_reserve_price();
let cap_level = round.get_cap_level();
let strike_price = round.get_strike_price();

assert_eq!(reserve_price, new_reserve_price);
assert_eq!(cap_level, new_cap_level);
assert_eq!(strike_price, new_strike_price);
assert(reserve_price != reserve_price0, 'reserve price did not change');
assert(cap_level != cap_level0, 'cap level did not change');
assert(strike_price != strike_price0, 'strike price did not change');
}

7 changes: 7 additions & 0 deletions src/tests/utils/facades/option_round_facade.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ impl OptionRoundFacadeImpl of OptionRoundFacadeTrait {
vault.update_round_params();
}

#[feature("safe_dispatcher")]
fn update_round_params_expect_error(ref self: OptionRoundFacade, error: felt252) {
let safe = self.get_safe_dispatcher();
safe.update_round_params(1_u256, 2_128, 3_u256).expect_err(error);
}


// Mock values of the option round and start the auction
fn setup_mock_auction(
ref self: OptionRoundFacade,
Expand Down
1 change: 0 additions & 1 deletion src/tests/utils/mocks/mock_market_aggregator.cairo

This file was deleted.

20 changes: 0 additions & 20 deletions src/vault/contract.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -756,25 +756,5 @@ mod Vault {
Option::None => 0
}
}

fn calculate_total_options_available(
self: @ContractState, starting_liquidity: u256, cap_level: u16
) -> u256 {
let current_round_id = self.current_option_round_id();
let current_round = self.get_round_dispatcher(current_round_id);
//Calculate total options accordingly
let strike = current_round.get_strike_price();
//println!("strike: {}", strike);
//println!("cl: {}", cap_level);
let cap = (strike * cap_level.into()) / 10000;
let total_options = starting_liquidity / cap;

//println!("starting liquidity: {}", starting_liquidity);
//println!("cap: {}", cap);
//println!("total options: {}", total_options);

//(starting_liquidity * cap_level.into()) / 10000
total_options
}
}
}

0 comments on commit 17550db

Please sign in to comment.