Skip to content

Commit

Permalink
feat/rb-tree-contract
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruv035 committed Jul 7, 2024
1 parent 60f5f6b commit 95caa62
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
13 changes: 8 additions & 5 deletions src/contracts/option_round.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ trait IOptionRound<TContractState> {
// Gets the amount that an option buyer can exercise with their option balance
fn get_payout_balance_for(self: @TContractState, option_buyer: ContractAddress) -> u256;

fn get_option_balance_for(self: @TContractState, option_buyer: ContractAddress) -> u256;
fn get_option_balance_for(ref self: TContractState, option_buyer: ContractAddress) -> u256;


/// Other
Expand Down Expand Up @@ -263,6 +263,8 @@ mod OptionRound {
total_options_sold: u256,
// The clearing price of the auction (the price each option sells for)
clearing_price: u256,
// Bid id for the last bid to be partially or fully filled
clearing_bid:felt252,
// The auction start date
auction_start_date: u64,
// The auction end date
Expand Down Expand Up @@ -641,8 +643,8 @@ mod OptionRound {
100
}

fn get_option_balance_for(self: @ContractState, option_buyer: ContractAddress) -> u256 {
100
fn get_option_balance_for(ref self: ContractState, option_buyer: ContractAddress) -> u256 {
self.bids_tree.find_options_for(option_buyer)
}

fn get_round_id(self: @ContractState) -> u256 {
Expand Down Expand Up @@ -961,13 +963,14 @@ mod OptionRound {
let total_options_available = self.total_options_available.read();
let clearing_price = self.bids_tree.find_clearing_price(total_options_available);
match clearing_price.unwrap() {
ClearingPriceReturn::clearing_price(value) => {
ClearingPriceReturn::ClearedParams((value,bid_id)) => {
self.clearing_price.write(value);
self.clearing_bid.write(bid_id);
if (self.total_options_sold.read() != total_options_available) {
self.total_options_sold.write(total_options_available);
}
},
ClearingPriceReturn::remaining_options(value) => {
ClearingPriceReturn::RemainingOptions(value) => {
self.total_options_sold.write(self.total_options_available.read() - value);
}
}
Expand Down
33 changes: 23 additions & 10 deletions src/contracts/utils/red_black_tree.cairo
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
use pitch_lake_starknet::contracts::{utils::red_black_tree::rb_tree_component::ClearingPriceReturn,
option_round::OptionRound::Bid};
use pitch_lake_starknet::contracts::{
utils::red_black_tree::rb_tree_component::ClearingPriceReturn, option_round::OptionRound::Bid
};
use starknet::ContractAddress;
#[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) -> Option<ClearingPriceReturn>;
fn find_clearing_price(
ref self: TContractState, total_options_available: u256
) -> Option<ClearingPriceReturn>;
fn find_options_for(ref self: TContractState, bidder: ContractAddress) -> u256;
fn delete(ref self: TContractState, value: Bid);
fn get_root(self: @TContractState) -> felt252;
fn traverse_postorder(ref self: TContractState);
Expand All @@ -20,7 +25,7 @@ const RED: bool = true;
#[starknet::component]
pub mod rb_tree_component {
use pitch_lake_starknet::contracts::utils::red_black_tree::IRBTree;
use super::{BLACK, RED, Bid};
use super::{BLACK, RED, Bid, ContractAddress};
use core::{array::ArrayTrait, option::OptionTrait, traits::{IndexView, TryInto}};


Expand Down Expand Up @@ -51,8 +56,8 @@ pub mod rb_tree_component {

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

#[event]
Expand All @@ -61,8 +66,6 @@ pub mod rb_tree_component {
InsertEvent: InsertEvent
}



#[derive(Drop, starknet::Event, PartialEq)]
struct InsertEvent {
node: Node,
Expand Down Expand Up @@ -97,12 +100,21 @@ pub mod rb_tree_component {
match result.unwrap() {
TraverseReturn::clearing_price_felt(node_id) => {
let clearing_bid: Bid = self.bid_details.read(node_id);
Option::Some(ClearingPriceReturn::clearing_price(clearing_bid.price))
Option::Some(ClearingPriceReturn::ClearedParams((clearing_bid.price, node_id)))
},
TraverseReturn::remaining_options(value) => { Option::Some(ClearingPriceReturn::remaining_options(value)) }
TraverseReturn::remaining_options(value) => {
Option::Some(ClearingPriceReturn::RemainingOptions(value))
}
}
}

fn find_options_for(
ref self: ComponentState<TContractState>, bidder: ContractAddress
) -> u256 {
self.traverse_postorder_calculate_options();
1
}


fn delete(ref self: ComponentState<TContractState>, value: Bid) {
let node_to_delete_id = self.find_node(self.root.read(), value);
Expand Down Expand Up @@ -152,6 +164,7 @@ pub mod rb_tree_component {
self.traverse_postorder_from_node(current_node.left);
}

fn traverse_postorder_calculate_options(ref self: ComponentState<TContractState>) {}
fn traverse_postorder_total_from_node(
ref self: ComponentState<TContractState>,
current_id: felt252,
Expand Down

0 comments on commit 95caa62

Please sign in to comment.