Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/event field updates #161

Merged
merged 16 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/library/utils.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use pitch_lake_starknet::types::{VaultType, Consts::{BPS, PRECISION}};
use pitch_lake_starknet::types::{VaultType, Consts::BPS};

fn min<T, +PartialEq<T>, +PartialOrd<T>, +Drop<T>, +Copy<T>>(a: T, b: T) -> T {
match a < b {
Expand Down
41 changes: 0 additions & 41 deletions src/option_round/contract.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -943,47 +943,6 @@ mod OptionRound {
self.erc20._burn(owner, amount);
}


// fn inspect_options_for
// #Params
// @bidder:ContractAddress, targetAddress
fn inspect_options_for(
self: @ContractState, bidder: ContractAddress
) -> (Array<Bid>, Array<Bid>, felt252) {
let mut refundable_bids: Array<Bid> = array![];
let mut tokenizable_bids: Array<Bid> = array![];
let mut partial_bid: felt252 = 0;

//If state is open or auctioning, return defaults

let state = self.get_state();
if (state == OptionRoundState::Open || state == OptionRoundState::Auctioning) {
return (tokenizable_bids, refundable_bids, partial_bid);
}
let nonce = self.get_bidding_nonce_for(bidder);
let mut i = 0;
while i < nonce {
let bid_id: felt252 = self.create_bid_id(bidder, i);
let clearing_bid_id: felt252 = self.bids_tree.clearing_bid.read();
// If bidder's bid is the clearing bid, it could be partially sold
if (bid_id == clearing_bid_id) {
partial_bid = bid_id;
} else {
let bid: Bid = self.bids_tree._find(bid_id);
let clearing_bid: Bid = self.bids_tree._find(clearing_bid_id);

if (bid < clearing_bid) {
refundable_bids.append(bid);
} else {
tokenizable_bids.append(bid);
}
}
i += 1;
};

(tokenizable_bids, refundable_bids, partial_bid)
}

// Get bid outcomes
fn calculate_bid_outcome_for(
self: @ContractState, bidder: ContractAddress
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ fn test_unsold_liquidity_moves_from_locked_to_unlocked() {

// Test unsold liquidity adds to liquidity provider's unlocked balance
#[test]
#[available_gas(50000000)]
#[available_gas(80000000)]
fn test_unsold_liquidity_is_unlocked_for_liquidity_providers() {
let (mut vault, _) = setup_facade();
let mut current_round = vault.get_current_round();
Expand Down
12 changes: 6 additions & 6 deletions src/tests/utils/facades/sanity_checks.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -95,18 +95,18 @@ fn tokenize_options(
fn deposit(
ref vault: VaultFacade, liquidity_provider: ContractAddress, unlocked_amount: u256
) -> u256 {
let expected_unlocked_amount = vault.get_lp_unlocked_balance(liquidity_provider);
let storage_unlocked_amount = vault.get_lp_unlocked_balance(liquidity_provider);

assert(unlocked_amount == expected_unlocked_amount, 'Deposit sanity check fail');
expected_unlocked_amount
assert_eq!(unlocked_amount, storage_unlocked_amount);
storage_unlocked_amount
}

fn withdraw(
ref vault: VaultFacade, liquidity_provider: ContractAddress, unlocked_amount: u256
) -> u256 {
let expected_unlocked_amount = vault.get_lp_unlocked_balance(liquidity_provider);
assert(unlocked_amount == expected_unlocked_amount, 'Withdraw sanity check fail');
expected_unlocked_amount
let unlocked_amount_in_storage = vault.get_lp_unlocked_balance(liquidity_provider);
assert_eq!(unlocked_amount, unlocked_amount_in_storage);
unlocked_amount_in_storage
}

fn claim_queued_liquidity(ref vault: VaultFacade, queued_amount: u256, expected: u256) -> u256 {
Expand Down
37 changes: 13 additions & 24 deletions src/tests/utils/facades/vault_facade.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,18 @@ impl VaultFacadeImpl of VaultFacadeTrait {
sanity_checks::withdraw(ref self, liquidity_provider, updated_unlocked_position)
}

fn queue_withdrawal(ref self: VaultFacade, liquidity_provider: ContractAddress, amount: u256) {
fn queue_withdrawal(ref self: VaultFacade, liquidity_provider: ContractAddress, bps: u16) {
set_contract_address(liquidity_provider);
self.vault_dispatcher.queue_withdrawal(amount);
self.vault_dispatcher.queue_withdrawal(bps);
}

#[feature("safe_dispatcher")]
fn queue_withdrawal_expect_error(
ref self: VaultFacade, liquidity_provider: ContractAddress, amount: u256, error: felt252,
ref self: VaultFacade, liquidity_provider: ContractAddress, bps: u16, error: felt252,
) {
set_contract_address(liquidity_provider);
let safe_vault = self.get_safe_dispatcher();
safe_vault.queue_withdrawal(amount).expect_err(error);
safe_vault.queue_withdrawal(bps).expect_err(error);
}


Expand All @@ -129,13 +129,13 @@ impl VaultFacadeImpl of VaultFacadeTrait {
fn queue_multiple_withdrawals(
ref self: VaultFacade,
mut liquidity_providers: Span<ContractAddress>,
mut amounts: Span<u256>
mut bps_multi: Span<u16>
) {
loop {
match liquidity_providers.pop_front() {
Option::Some(lp) => {
let amount = amounts.pop_front().unwrap();
self.queue_withdrawal(*lp, *amount);
let bps = bps_multi.pop_front().unwrap();
self.queue_withdrawal(*lp, *bps);
},
Option::None => { break (); }
};
Expand Down Expand Up @@ -291,12 +291,6 @@ impl VaultFacadeImpl of VaultFacadeTrait {

// For LPs

fn get_lp_premiums_collected(
ref self: VaultFacade, liquidity_provider: ContractAddress, round_id: u256
) -> u256 {
self.vault_dispatcher.get_premiums_collected(liquidity_provider, round_id)
}

fn get_lp_locked_balance(ref self: VaultFacade, liquidity_provider: ContractAddress) -> u256 {
self.vault_dispatcher.get_lp_locked_balance(liquidity_provider)
}
Expand All @@ -317,28 +311,23 @@ impl VaultFacadeImpl of VaultFacadeTrait {
balances
}

fn get_lp_queued_balance(
ref self: VaultFacade, liquidity_provider: ContractAddress, round_id: u256
) -> u256 {
self.vault_dispatcher.get_lp_queued_balance(liquidity_provider, round_id)
fn get_lp_queued_bps(ref self: VaultFacade, liquidity_provider: ContractAddress) -> u16 {
self.vault_dispatcher.get_lp_queued_bps(liquidity_provider)
}

fn get_lp_stashed_balance(ref self: VaultFacade, liquidity_provider: ContractAddress) -> u256 {
self.vault_dispatcher.get_lp_stashed_balance(liquidity_provider)
}


fn get_lp_queued_balances(
ref self: VaultFacade,
mut liquidity_providers: Span<ContractAddress>,
mut round_ids: Span<u256>
) -> Array<u256> {
fn get_lp_queued_bps_multi(
ref self: VaultFacade, mut liquidity_providers: Span<ContractAddress>,
) -> Array<u16> {
let mut balances = array![];
loop {
match liquidity_providers.pop_front() {
Option::Some(liquidity_provider) => {
let round_id = round_ids.pop_front().unwrap();
let balance = self.get_lp_queued_balance(*liquidity_provider, *round_id);
let balance = self.get_lp_queued_bps(*liquidity_provider);
balances.append(balance);
},
Option::None => { break (); }
Expand Down
51 changes: 37 additions & 14 deletions src/tests/utils/helpers/event_helpers.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -223,12 +223,29 @@ fn assert_event_transfer(

// Test OptionRoundCreated event emits correctly
fn assert_event_option_round_deployed(
contract: ContractAddress, round_id: u256, address: ContractAddress,
contract: ContractAddress,
round_id: u256,
address: ContractAddress,
reserve_price: u256,
strike_price: u256,
cap_level: u128,
auction_start_date: u64,
auction_end_date: u64,
option_settlement_date: u64,
) {
match pop_log::<Vault::Event>(contract) {
Option::Some(e) => {
let expected = Vault::Event::OptionRoundDeployed(
Vault::OptionRoundDeployed { round_id, address, }
Vault::OptionRoundDeployed {
round_id,
address,
reserve_price,
strike_price,
cap_level,
auction_start_date,
auction_end_date,
option_settlement_date
}
);
assert_events_equal(e, expected);
},
Expand All @@ -240,13 +257,16 @@ fn assert_event_option_round_deployed(
fn assert_event_vault_deposit(
vault: ContractAddress,
account: ContractAddress,
position_balance_before: u256,
position_balance_after: u256,
amount: u256,
account_unlocked_balance_now: u256,
vault_unlocked_balance_now: u256,
) {
match pop_log::<Vault::Event>(vault) {
Option::Some(e) => {
let expected = Vault::Event::Deposit(
Vault::Deposit { account, position_balance_before, position_balance_after }
Vault::Deposit {
account, amount, account_unlocked_balance_now, vault_unlocked_balance_now
}
);
assert_events_equal(e, expected);
},
Expand All @@ -258,13 +278,16 @@ fn assert_event_vault_deposit(
fn assert_event_vault_withdrawal(
vault: ContractAddress,
account: ContractAddress,
position_balance_before: u256,
position_balance_after: u256,
amount: u256,
account_unlocked_balance_now: u256,
vault_unlocked_balance_now: u256,
) {
match pop_log::<Vault::Event>(vault) {
Option::Some(e) => {
let expected = Vault::Event::Withdrawal(
Vault::Withdrawal { account, position_balance_before, position_balance_after }
Vault::Withdrawal {
account, amount, account_unlocked_balance_now, vault_unlocked_balance_now
}
);

assert_events_equal(e, expected);
Expand All @@ -275,12 +298,12 @@ fn assert_event_vault_withdrawal(

// Test collect queued liquidity event emits correctly
fn assert_event_queued_liquidity_collected(
vault: ContractAddress, account: ContractAddress, stashed_amount: u256,
vault: ContractAddress, account: ContractAddress, amount: u256, vault_stashed_balance_now: u256,
) {
match pop_log::<Vault::Event>(vault) {
Option::Some(e) => {
let expected = Vault::Event::QueuedLiquidityCollected(
Vault::QueuedLiquidityCollected { account, stashed_amount }
Vault::QueuedLiquidityCollected { account, amount, vault_stashed_balance_now }
);

assert_events_equal(e, expected);
Expand All @@ -293,15 +316,15 @@ fn assert_event_queued_liquidity_collected(
fn assert_event_withdrawal_queued(
vault: ContractAddress,
account: ContractAddress,
round_id: u256,
previous_amount_queued: u256,
new_amount_queued: u256
bps: u16,
account_queued_amount_now: u256,
vault_queued_amount_now: u256
) {
match pop_log::<Vault::Event>(vault) {
Option::Some(e) => {
let expected = Vault::Event::WithdrawalQueued(
Vault::WithdrawalQueued {
account, round_id, previous_amount_queued, new_amount_queued
account, bps, account_queued_amount_now, vault_queued_amount_now
}
);

Expand Down
26 changes: 21 additions & 5 deletions src/tests/vault/liquidity_providers/deposit_tests.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ use debug::PrintTrait;
fn test_deposit_events() {
let (mut vault, _) = setup_facade();
let mut liquidity_providers = liquidity_providers_get(3).span();
let deposit_amounts = array![25 * decimals(), 50 * decimals(), 100 * decimals()].span();
let mut deposit_amounts = array![25 * decimals(), 50 * decimals(), 100 * decimals()].span();

// Unlocked balances before deposit
let mut unlocked_balances_before = vault.get_lp_unlocked_balances(liquidity_providers);
let mut vault_unlocked_balance_before = vault.get_total_unlocked_balance();

// Deposit into the vault
let mut unlocked_balances_after = vault.deposit_multiple(deposit_amounts, liquidity_providers);
Expand All @@ -66,13 +66,15 @@ fn test_deposit_events() {
loop {
match liquidity_providers.pop_front() {
Option::Some(liquidity_provider) => {
let unlocked_balance_before = unlocked_balances_before.pop_front().unwrap();
let deposit_amount = *deposit_amounts.pop_front().unwrap();
let unlocked_balance_after = unlocked_balances_after.pop_front().unwrap();
vault_unlocked_balance_before += deposit_amount;
assert_event_vault_deposit(
vault.contract_address(),
*liquidity_provider,
unlocked_balance_before,
unlocked_balance_after
deposit_amount,
unlocked_balance_after,
vault_unlocked_balance_before
);
},
Option::None => { break (); }
Expand Down Expand Up @@ -124,6 +126,20 @@ fn test_depositing_to_vault_eth_transfer() {
}
}

#[test]
#[available_gas(50000000)]
#[should_panic(expected: ('u256_sub Overflow', 'ENTRYPOINT_FAILED', 'ENTRYPOINT_FAILED'))]
fn test_depositing_to_vault_no_approval() {
let (mut vault, eth) = setup_facade();
let mut liquidity_provider = liquidity_provider_1();
let mut deposit_amount = 50 * decimals();

set_contract_address(liquidity_provider);
eth.approve(vault.contract_address(), 0);

vault.deposit(deposit_amount, liquidity_provider);
}

// Test deposits always go to the vault's unlocked pool, regardless of the state of the current round
#[test]
#[available_gas(90000000)]
Expand Down
21 changes: 10 additions & 11 deletions src/tests/vault/liquidity_providers/withdraw_tests.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -75,30 +75,32 @@ fn test_withdrawing_more_than_unlocked_balance_fails() {
fn test_withdrawal_events() {
let (mut vault, _) = setup_facade();
let mut liquidity_providers = liquidity_providers_get(3).span();
let deposit_amounts = array![25 * decimals(), 50 * decimals(), 100 * decimals()].span();
let mut deposit_amounts = array![25 * decimals(), 50 * decimals(), 100 * decimals()].span();

// Unlocked balances before withdrawal
let mut lp_unlocked_balances_before = vault
.deposit_multiple(deposit_amounts, liquidity_providers);
// Unlocked balance before withdrawals
vault.deposit_multiple(deposit_amounts, liquidity_providers);

// Clear deposit events from log
clear_event_logs(array![vault.contract_address()]);

// Withdraw from the vault
let mut vault_unlocked_balance_before = vault.get_total_unlocked_balance();
let mut lp_unlocked_balances_after = vault
.withdraw_multiple(deposit_amounts, liquidity_providers);

// Check event emissions
loop {
match liquidity_providers.pop_front() {
Option::Some(liquidity_provider) => {
let unlocked_amount_before = lp_unlocked_balances_before.pop_front().unwrap();
let withdraw_amount = *deposit_amounts.pop_front().unwrap();
let unlocked_amount_after = lp_unlocked_balances_after.pop_front().unwrap();
vault_unlocked_balance_before -= withdraw_amount;
assert_event_vault_withdrawal(
vault.contract_address(),
*liquidity_provider,
unlocked_amount_before,
unlocked_amount_after
withdraw_amount,
unlocked_amount_after, // account unlocked balance before the withdraw
vault_unlocked_balance_before, // vault unlocked balance after the withdraw
);
},
Option::None => { break (); }
Expand Down Expand Up @@ -131,9 +133,7 @@ fn test_withdrawing_from_vault_eth_transfer() {
let vault_balance_after = eth.balance_of(vault.contract_address());

// Check vault eth balance
assert(
vault_balance_after == vault_balance_before - total_withdrawals, 'vault eth balance wrong'
);
assert_eq!(vault_balance_after, vault_balance_before - total_withdrawals);

// Check liquidity provider eth balances
loop {
Expand Down Expand Up @@ -176,7 +176,6 @@ fn test_withdrawing_always_come_from_unlocked_pool() {
accelerate_to_running(ref vault);
let unlocked_amount_before = vault.get_lp_unlocked_balance(liquidity_provider);
let unlocked_amount_after = vault.withdraw(withdraw_amount, liquidity_provider);

assert(
unlocked_amount_after == unlocked_amount_before - withdraw_amount, 'unlocked amount 2 wrong'
);
Expand Down
Loading
Loading