Skip to content

Commit

Permalink
Tests and Bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruv035 committed Jul 7, 2024
1 parent 05adaac commit 8223a7b
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 110 deletions.
68 changes: 35 additions & 33 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,ClearingPriceReturn}, 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 @@ -480,9 +480,9 @@ mod OptionRound {
OptionRoundAlreadySettled,
OptionSettlementDateNotReached,
// Placing bids
BidBelowReservePrice,BidAmountZero,
BidBelowReservePrice,
BidAmountZero,
BiddingWhileNotAuctioning,

// Editing bids
BidCannotBeDecreased: felt252,
}
Expand Down Expand Up @@ -707,9 +707,9 @@ mod OptionRound {
return Result::Err(OptionRoundError::AuctionAlreadyStarted);
}

let StartAuctionParams { total_options_available,
let StartAuctionParams { total_options_available: _,
starting_liquidity,
reserve_price,
reserve_price: _,
cap_level,
strike_price } =
params;
Expand All @@ -722,12 +722,12 @@ mod OptionRound {
}

// Set auction params
self.reserve_price.write(1);//HardCoded for tests
self.reserve_price.write(1); //HardCoded for tests
self.cap_level.write(cap_level);
self.strike_price.write(strike_price);
// Set starting liquidity & total options available
self.starting_liquidity.write(starting_liquidity);
self.total_options_available.write(5);//HardCoded for tests
self.total_options_available.write(5); //HardCoded for tests

// Update state to Auctioning
self.state.write(OptionRoundState::Auctioning);
Expand All @@ -744,7 +744,7 @@ mod OptionRound {
);

// Return the total options available
Result::Ok(5)//HardCoded for tests
Result::Ok(5) //HardCoded for tests
}

fn end_auction(ref self: ContractState) -> Result<(u256, u256), OptionRoundError> {
Expand Down Expand Up @@ -832,25 +832,36 @@ mod OptionRound {
ref self: ContractState, amount: u256, price: u256
) -> Result<Bid, OptionRoundError> {
//Check state of the OptionRound

println!("AMOUNT PRICE {} {}",amount,price);
let bidder = get_caller_address();
let eth_dispatcher = self.get_eth_dispatcher();
if (self.get_state() != OptionRoundState::Auctioning) {
self
.emit(
Event::AuctionRejectedBid(AuctionRejectedBid { account: bidder, amount, price })
);
return Result::Err(OptionRoundError::BiddingWhileNotAuctioning);

}

//Bid amount zero
if(amount==0){
println!("AMOUNT 0");
if (amount == 0) {
self
.emit(
Event::AuctionRejectedBid(AuctionRejectedBid { account: bidder, amount, price })
);
return Result::Err(OptionRoundError::BidAmountZero);
}
//Bid below reserve price

if (price < self.get_reserve_price()) {
println!("price {}\n reserve_price{}",price,self.reserve_price.read());
self
.emit(
Event::AuctionRejectedBid(AuctionRejectedBid { account: bidder, amount, price })
);
return Result::Err(OptionRoundError::BidBelowReservePrice);
}
let bidder = get_caller_address();

let nonce = self.bidder_nonces.read(bidder);

let bid = Bid {
Expand All @@ -862,27 +873,18 @@ mod OptionRound {
price: price,
valid: true
};
println!("INNER 1");
self.bids_tree.insert(bid);
println!("INNER 2");
self.bidder_nonces.write(bidder, nonce + 1);
println!("INNER 3");

//Update Clearing Price
self.update_clearing_price();
println!("INNER 4{}{}",amount,price);

let total:u256=amount*price;
let felt:felt252 = get_contract_address().into();
println!("TOTAL 5{}",felt);
//Transfer Eth
eth_dispatcher.transfer_from(get_contract_address(), bidder, 1);
println!("INNER 1");
eth_dispatcher.transfer_from(bidder, get_contract_address(), 1);
self
.emit(
Event::AuctionAcceptedBid(AuctionAcceptedBid { account: bidder, amount, price })
);
println!("INNER 16");
Result::Ok(bid)
}

Expand All @@ -909,16 +911,16 @@ mod OptionRound {
}
new_bid.amount = amount;
new_bid.price = price;
let difference = new_bid.amount*new_bid.price-old_bid.amount*old_bid.price;
let difference = new_bid.amount * new_bid.price - old_bid.amount * old_bid.price;

self.bids_tree.delete(old_bid);

self.bids_tree.insert(new_bid);

self.update_clearing_price();

let eth_dispatcher = self.get_eth_dispatcher();
eth_dispatcher.transfer_from(get_caller_address(),get_contract_address(),difference);
eth_dispatcher.transfer_from(get_caller_address(), get_contract_address(), difference);
Result::Ok(new_bid)
}
fn refund_unused_bids(
Expand Down Expand Up @@ -949,18 +951,18 @@ mod OptionRound {
get_caller_address() == self.vault_address.read()
}

fn update_clearing_price(ref self:ContractState){
fn update_clearing_price(ref self: ContractState) {
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)=>{
match clearing_price.unwrap() {
ClearingPriceReturn::clearing_price(value) => {
self.clearing_price.write(value);
if(self.total_options_sold.read()!=total_options_available){
if (self.total_options_sold.read() != total_options_available) {
self.total_options_sold.write(total_options_available);
}
},
ClearingPriceReturn::remaining_options(value)=>{
self.total_options_sold.write(self.total_options_available.read()-value);
ClearingPriceReturn::remaining_options(value) => {
self.total_options_sold.write(self.total_options_available.read() - value);
}
}
}
Expand Down
44 changes: 26 additions & 18 deletions src/tests/option_round/option_buyers/bidding_tests.cairo
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use core::array::ArrayTrait;
use starknet::{
ClassHash, ContractAddress, contract_address_const, deploy_syscall,
Felt252TryIntoContractAddress, get_contract_address, get_block_timestamp,
Expand All @@ -16,7 +17,7 @@ use pitch_lake_starknet::{
IVaultDispatcher, IVaultSafeDispatcher, IVaultDispatcherTrait, Vault,
IVaultSafeDispatcherTrait
},
option_round::{IOptionRoundDispatcher, IOptionRoundDispatcherTrait},
option_round::{IOptionRoundDispatcher, IOptionRoundDispatcherTrait,OptionRoundState},
},
tests::{
utils::{
Expand Down Expand Up @@ -110,7 +111,7 @@ fn test_bid_price_below_reserve_fails() {

// Test bidding before auction starts fails
#[test]
#[available_gas(50000000)]
#[available_gas(500000000)]
fn test_bid_before_auction_starts_failure() {
let (mut vault, _) = setup_facade();
accelerate_to_auctioning(ref vault);
Expand Down Expand Up @@ -138,29 +139,35 @@ fn test_bid_before_auction_starts_failure() {

// Test bidding after auction ends fails
#[test]
#[available_gas(50000000)]
#[available_gas(5000000000)]
fn test_bid_after_auction_ends_failure() {
let (mut vault, _) = setup_facade();
println!("1");

println!("HERE1");
accelerate_to_auctioning(ref vault);
println!("2");
println!("HERE2");
accelerate_to_running(ref vault);
println!("3");
println!("HERE3");
accelerate_to_settled(ref vault, 0);
println!("4");
println!("HERE4");
accelerate_to_auctioning(ref vault);
println!("5");
println!("HERE5");
accelerate_to_running(ref vault);
println!("HERE");
println!("HERE6");

// Try to place bid after auction ends
println!("HERE");
let (mut round2, _round3) = vault.get_current_and_next_rounds();
let bidder = option_bidder_buyer_1();
let bid_price = round2.get_reserve_price();
let bid_amount = round2.get_total_options_available();

let state = round2.get_state();
if(state==OptionRoundState::Running){
println!("Correct");
}
// Check bid rejected event
clear_event_logs(array![round2.contract_address()]);
println!("HERES");
match round2.place_bid_raw(bid_amount, bid_price, option_bidder_buyer_1()) {
Result::Ok(_) => { panic!("Bid should have failed"); },
Result::Err(_) => {
Expand All @@ -174,7 +181,7 @@ fn test_bid_after_auction_ends_failure() {

// Test bidding after auction end date fail (if end_auction() is not called first)
#[test]
#[available_gas(50000000)]
#[available_gas(500000000)]
fn test_bid_after_auction_end_failure_2() {
let (mut vault, _) = setup_facade();
accelerate_to_auctioning(ref vault);
Expand Down Expand Up @@ -209,7 +216,7 @@ fn test_bid_after_auction_end_failure_2() {

// Test bid accepted events
#[test]
#[available_gas(50000000)]
#[available_gas(500000000)]
fn test_bid_accepted_events() {
let (mut vault_facade, _) = setup_facade();
let options_available = accelerate_to_auctioning(ref vault_facade);
Expand Down Expand Up @@ -242,7 +249,7 @@ fn test_bid_accepted_events() {

// Test bidding transfers eth from bidder to round
#[test]
#[available_gas(50000000)]
#[available_gas(500000000)]
fn test_bid_eth_transfer() {
let (mut vault_facade, eth) = setup_facade();
let options_available = accelerate_to_auctioning(ref vault_facade);
Expand Down Expand Up @@ -289,7 +296,7 @@ fn test_bid_eth_transfer() {

// Test bidding updates bid nonce
#[test]
#[available_gas(50000000)]
#[available_gas(500000000)]
fn test_bidding_updates_bid_nonce() {
let (mut vault_facade, _) = setup_facade();
let options_available = accelerate_to_auctioning(ref vault_facade);
Expand All @@ -312,15 +319,16 @@ fn test_bidding_updates_bid_nonce() {

// Test failed bids do not update bid nonce
#[test]
#[available_gas(50000000)]
#[available_gas(500000000)]
fn test_failed_bid_nonce_unchanged() {
let (mut vault_facade, _) = setup_facade();
let options_available = accelerate_to_auctioning(ref vault_facade);
let mut current_round = vault_facade.get_current_round();
let reserve_price = current_round.get_reserve_price();

// Bid parameters
let bidder = option_bidder_buyer_1();
let bidder = *option_bidders_get(1)[0];

let bid_price = reserve_price;
let mut bid_amount = options_available;

Expand All @@ -329,7 +337,7 @@ fn test_failed_bid_nonce_unchanged() {
let nonce_before = current_round.get_bidding_nonce_for(bidder);
if (i % 2 == 1) {
//Failed bid in alternate rounds and check nonce update
current_round.place_bid(bid_amount, bid_price - 1, bidder);
current_round.place_bid_raw(bid_amount, bid_price - 1, bidder);
let nonce_after = current_round.get_bidding_nonce_for(bidder);
assert(nonce_before == nonce_after, 'Nonce Mismatch');
} else {
Expand All @@ -342,7 +350,7 @@ fn test_failed_bid_nonce_unchanged() {

// Test bid hashes match expected hash
#[test]
#[available_gas(50000000)]
#[available_gas(500000000)]
fn test_place_bid_id() {
let (mut vault_facade, _) = setup_facade();
let options_available = accelerate_to_auctioning(ref vault_facade);
Expand Down
17 changes: 10 additions & 7 deletions src/tests/utils/facades/option_round_facade.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use pitch_lake_starknet::{
},
tests::{
utils::{
helpers::general_helpers::{assert_two_arrays_equal_length, get_erc20_balance},
helpers::{setup::eth_supply_and_approve_all_bidders,general_helpers::{assert_two_arrays_equal_length, get_erc20_balance}},
lib::{test_accounts::{vault_manager, bystander}, structs::{OptionRoundParams}},
facades::sanity_checks,
}
Expand Down Expand Up @@ -61,10 +61,17 @@ impl OptionRoundFacadeImpl of OptionRoundFacadeTrait {
let res = self
.option_round_dispatcher
.settle_option_round(SettleOptionRoundParams { settlement_price });
match res {
let res = match res {
Result::Ok(total_payout) => sanity_checks::settle_option_round(ref self, total_payout),
Result::Err(e) => panic(array![e.into()]),
}
};

//Get next round id and approvals for next round
let vault_address= self.vault_address();
let vault_dispatcher = IVaultDispatcher{contract_address:vault_address};
let next_round_address= vault_dispatcher.get_option_round_address(self.get_round_id()+1);
eth_supply_and_approve_all_bidders(next_round_address,vault_dispatcher.eth_address());
res
}


Expand Down Expand Up @@ -101,7 +108,6 @@ impl OptionRoundFacadeImpl of OptionRoundFacadeTrait {
let bid_amount = amounts.pop_front().unwrap();
let bid_price = prices.pop_front().unwrap();
// Make bid
println!("ABCDEF {} {}",bid_amount,bid_price);
let res = self.place_bid(*bid_amount, *bid_price, *bidder);
// Append result
results.append(res);
Expand All @@ -121,9 +127,6 @@ impl OptionRoundFacadeImpl of OptionRoundFacadeTrait {
option_bidder_buyer: ContractAddress,
) -> Result<Bid, OptionRoundError> {
set_contract_address(option_bidder_buyer);
let add:felt252 = self.contract_address().into();
println!("FELT{}",add);

self.option_round_dispatcher.place_bid(amount, price)
}

Expand Down
Loading

0 comments on commit 8223a7b

Please sign in to comment.