Skip to content

Commit

Permalink
added unsold liquidity tests
Browse files Browse the repository at this point in the history
  • Loading branch information
0xDegenDeveloper committed Jul 5, 2024
1 parent 8a57a17 commit ea73476
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 4 deletions.
7 changes: 7 additions & 0 deletions src/contracts/vault.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ trait IVault<TContractState> {
self: @TContractState, liquidity_provider: ContractAddress, round_id: u256
) -> u256;

// Get the amount of unsold liquidity for a round
fn get_unsold_liquidity(self: @TContractState, round_id: u256) -> u256;

/// Writes ///

/// State transition
Expand Down Expand Up @@ -360,6 +363,10 @@ mod Vault {
self.round_addresses.read(option_round_id)
}

fn get_unsold_liquidity(self: @ContractState, round_id: u256) -> u256 {
self.unsold_liquidity.read(round_id)
}

/// Liquidity

// For liquidity providers
Expand Down
1 change: 1 addition & 0 deletions src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ mod contracts {
mod market_aggregator;
mod lp_token;
}

#[cfg(test)]
mod tests;
Original file line number Diff line number Diff line change
@@ -1,6 +1,176 @@
// Test unsold liquidity getter
// Test unsold liquiidty is correct
// Check sanity checks
// Test unsold becomes unlocked
use pitch_lake_starknet::{
tests::{
utils::{
helpers::{
accelerators::{
accelerate_to_auctioning, accelerate_to_running, accelerate_to_running_custom,
timeskip_and_end_auction, accelerate_to_auctioning_custom,
},
setup::{setup_facade, setup_test_auctioning_bidders},
general_helpers::{
create_array_linear, create_array_gradient, create_array_gradient_reverse,
get_portion_of_amount
},
},
lib::{
test_accounts::{
option_bidder_buyer_1, option_bidder_buyer_2, option_bidder_buyer_3,
option_bidder_buyer_4, option_bidders_get, liquidity_providers_get,
},
variables::{decimals},
},
facades::{
vault_facade::{VaultFacade, VaultFacadeTrait},
option_round_facade::{OptionRoundFacade, OptionRoundFacadeTrait, OptionRoundParams},
},
},
}
};
use starknet::testing::{set_block_timestamp, set_contract_address};


// Test unsold liquidity is 0 before auction end
#[test]
#[available_gas(50000000)]
fn test_unsold_liquidity_0_before_auction_end() {
let (mut vault, _) = setup_facade();
let total_options_available = accelerate_to_auctioning(ref vault);

// Place bids but not end auction
let mut current_round = vault.get_current_round();
let bid_amount = total_options_available;
let bid_price = current_round.get_reserve_price();
current_round.place_bid(bid_amount, bid_price, option_bidder_buyer_1());

// Check that unsold liquidity is 0 before auction end
let unsold_liq = vault.get_unsold_liquidity(current_round.get_round_id());
assert(unsold_liq == 0, 'should be 0 pre auction end');
}

// Test unsold liquidity is 0 if all options sell
#[test]
#[available_gas(50000000)]
fn test_unsold_liquidity_0_if_all_options_sell() {
let (mut vault, _) = setup_facade();
let mut current_round = vault.get_current_round();
accelerate_to_auctioning(ref vault);
accelerate_to_running(ref vault);

// Check that unsold liquidity is 0 if all options sell
let unsold_liq = vault.get_unsold_liquidity(current_round.get_round_id());
assert(unsold_liq == 0, 'should be 0');
}

// Test unsold liquidity is correct
#[test]
#[available_gas(50000000)]
fn test_unsold_liquidity_1() {
let (mut vault, _) = setup_facade();
let mut current_round = vault.get_current_round();
let options_available = accelerate_to_auctioning(ref vault);

// Get liquidity locked before auction ends
let total_locked_before = vault.get_total_locked_balance();

let liquidity_providers = liquidity_providers_get(2);
let bid_amounts = array![options_available / 3, options_available / 3];
let bid_prices = create_array_linear(current_round.get_reserve_price(), bid_amounts.len());
accelerate_to_running_custom(
ref vault, liquidity_providers.span(), bid_amounts.span(), bid_prices.span()
);

// Check 1/3 of the total locked liquidity is unsold
let expected_unsold_liq = total_locked_before / 3;
let unsold_liq = vault.get_unsold_liquidity(current_round.get_round_id());
assert(unsold_liq == expected_unsold_liq, 'unsold liq wrong');
}

// Test unsold liquidity moves from locked to unlocked
#[test]
#[available_gas(50000000)]
fn test_unsold_liquidity_moves_from_locked_to_unlocked() {
let (mut vault, _) = setup_facade();
let mut current_round = vault.get_current_round();
let options_available = accelerate_to_auctioning(ref vault);

// Get liquidity locked before auction ends
let (total_locked_before, total_unlocked_before) = vault
.get_total_locked_and_unlocked_balance();

let liquidity_providers = liquidity_providers_get(2);
let bid_amounts = array![options_available / 3, options_available / 3];
let bid_prices = create_array_linear(current_round.get_reserve_price(), bid_amounts.len());
accelerate_to_running_custom(
ref vault, liquidity_providers.span(), bid_amounts.span(), bid_prices.span()
);

// Check unsold moves from locked to unlocked
let unsold_liq = vault.get_unsold_liquidity(current_round.get_round_id());
assert(unsold_liq > 0, 'unsold liq shd not be 0');
let (total_locked_after, total_unlocked_after) = vault.get_total_locked_and_unlocked_balance();
assert(total_locked_after == total_locked_before - unsold_liq, 'locked balance after fail');
assert(
total_unlocked_after == total_unlocked_before + unsold_liq, 'unlocked balance after fail'
);
}

// Test unsold liquidity adds to liquidity provider's unlocked balance
#[test]
#[available_gas(50000000)]
fn test_unsold_liquidity_is_unlocked_for_liquidity_providers() {
let (mut vault, _) = setup_facade();
let mut current_round = vault.get_current_round();
let number_of_lps = 3;
let mut liquidity_providers = liquidity_providers_get(number_of_lps).span();
let deposit_amounts = create_array_linear(20 * decimals(), number_of_lps).span();
let options_available = accelerate_to_auctioning_custom(
ref vault, liquidity_providers, deposit_amounts
);

// Get liquidity providers locked and unlocked balances before auction end
let mut locked_and_unlocked_balances_before: Span<(u256, u256)> = vault
.get_lp_locked_and_unlocked_balances(liquidity_providers)
.span();

// Bid for 1/2 the options
let option_bidders = array![option_bidder_buyer_1()].span();
let bid_amounts = array![options_available / 2].span();
let bid_prices = array![current_round.get_reserve_price()].span();
accelerate_to_running_custom(
ref vault, option_bidders, bid_amounts, bid_prices
);

// Check each LP's unlocked balance increments as expected
let unsold_liq = vault.get_unsold_liquidity(current_round.get_round_id());
let mut expected_unsold_liq_shares = get_portion_of_amount(deposit_amounts, unsold_liq).span();
let mut locked_and_unlocked_balances_after: Span<(u256, u256)> = vault
.get_lp_locked_and_unlocked_balances(liquidity_providers)
.span();
assert(unsold_liq > 0, 'unsold liq shd not be 0');
loop {
match liquidity_providers.pop_front() {
Option::Some(_) => {
let (locked_balance_before, unlocked_balance_before) =
locked_and_unlocked_balances_before
.pop_front()
.unwrap();
let (locked_balance_after, unlocked_balance_after) =
locked_and_unlocked_balances_after
.pop_front()
.unwrap();
let share_of_unsold_liq = expected_unsold_liq_shares.pop_front().unwrap();

assert(
*locked_balance_after == *locked_balance_before - *share_of_unsold_liq,
'lp locked wrong'
);
assert(
*unlocked_balance_after == *unlocked_balance_before + *share_of_unsold_liq,
'lp unlocked wrong'
);
},
Option::None => { break (); }
}
}
}

4 changes: 4 additions & 0 deletions src/tests/utils/facades/vault_facade.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,10 @@ impl VaultFacadeImpl of VaultFacadeTrait {
(self.get_current_round(), self.get_next_round())
}

fn get_unsold_liquidity(ref self: VaultFacade, round_id: u256) -> u256 {
self.vault_dispatcher.get_unsold_liquidity(round_id)
}

/// Liquidity

// For LPs
Expand Down

0 comments on commit ea73476

Please sign in to comment.