Skip to content

Commit

Permalink
Implement auction logic in tree
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruv035 committed Jul 6, 2024
1 parent 2de2e0d commit 38111aa
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 15 deletions.
11 changes: 9 additions & 2 deletions src/contracts/option_round.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ mod OptionRound {
use starknet::{ContractAddress, get_caller_address, get_contract_address, get_block_timestamp};
use pitch_lake_starknet::contracts::{
market_aggregator::{IMarketAggregatorDispatcher, IMarketAggregatorDispatcherTrait},
utils::{red_black_tree::rb_tree_component, utils::{min, max}},
utils::{red_black_tree::{rb_tree_component,ClearingPriceReturn}, utils::{min, max}},
vault::{Vault::VaultType, IVaultDispatcher, IVaultDispatcherTrait},
option_round::IOptionRound
};
Expand Down Expand Up @@ -933,7 +933,14 @@ mod OptionRound {

fn update_clearing_price(ref self:ContractState){
let clearing_price = self.bids_tree.find_clearing_price(self.total_options_available.read());
self.clearing_price.write(clearing_price);
match clearing_price.unwrap(){
ClearingPriceReturn::clearing_price(value)=>{
self.clearing_price.write(value);
},
ClearingPriceReturn::remaining_options(value)=>{
self.total_options_sold.write(self.total_options_available.read()-value);
}
}
}

// End the auction and calculate the clearing price and total options sold
Expand Down
33 changes: 20 additions & 13 deletions src/contracts/utils/red_black_tree.cairo
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use pitch_lake_starknet::contracts::option_round::OptionRound::Bid;
use pitch_lake_starknet::contracts::{utils::red_black_tree::rb_tree_component::ClearingPriceReturn,
option_round::OptionRound::Bid};
#[starknet::interface]
trait IRBTree<TContractState> {
fn insert(ref self: TContractState, value: Bid);
fn find(ref self: TContractState, value: Bid) -> felt252;
fn find_clearing_price(ref self: TContractState, total_options_available: u256) -> u256;
fn find_clearing_price(ref self: TContractState, total_options_available: u256) -> Option<ClearingPriceReturn>;
fn delete(ref self: TContractState, value: Bid);
fn get_root(self: @TContractState) -> felt252;
fn traverse_postorder(ref self: TContractState);
Expand Down Expand Up @@ -42,17 +43,26 @@ pub mod rb_tree_component {
color: bool,
}

#[derive(Copy, Drop, Serde, PartialEq,)]
enum TraverseReturn {
remaining_options: u256,
clearing_price_felt: felt252,
}

#[derive(Copy, Drop, Serde, PartialEq,)]
enum ClearingPriceReturn {
remaining_options: u256,
clearing_price: u256,
}

#[event]
#[derive(Drop, starknet::Event, PartialEq)]
pub enum Event {
InsertEvent: InsertEvent
}

#[derive(Copy, Drop, Serde, PartialEq,)]
enum TraverseReturn {
remaining_options: u256,
clearing_price_felt: felt252,
}


#[derive(Drop, starknet::Event, PartialEq)]
struct InsertEvent {
node: Node,
Expand Down Expand Up @@ -81,15 +91,15 @@ pub mod rb_tree_component {

fn find_clearing_price(
ref self: ComponentState<TContractState>, total_options_available: u256
) -> u256 {
) -> Option<ClearingPriceReturn> {
let result = self
.traverse_postorder_total_from_node(self.root.read(), total_options_available);
match result.unwrap() {
TraverseReturn::clearing_price_felt(node_id) => {
let clearing_bid: Bid = self.bid_details.read(node_id);
clearing_bid.price
Option::Some(ClearingPriceReturn::clearing_price(clearing_bid.price))
},
TraverseReturn::remaining_options(_) => { 0 }
TraverseReturn::remaining_options(value) => { Option::Some(ClearingPriceReturn::remaining_options(value)) }
}
}

Expand Down Expand Up @@ -142,9 +152,6 @@ pub mod rb_tree_component {
self.traverse_postorder_from_node(current_node.left);
}

fn find_clearing_price() -> felt252 {
'asd'
}
fn traverse_postorder_total_from_node(
ref self: ComponentState<TContractState>,
current_id: felt252,
Expand Down

0 comments on commit 38111aa

Please sign in to comment.