Skip to content

Commit

Permalink
fix: clarify full liquidation logic, minor optimizations, and fill in…
Browse files Browse the repository at this point in the history
… missing test cases
  • Loading branch information
mootz12 committed Dec 27, 2024
1 parent 6228c7a commit 9aa733b
Show file tree
Hide file tree
Showing 4 changed files with 375 additions and 52 deletions.
129 changes: 96 additions & 33 deletions pool/src/auctions/auction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub struct AuctionData {
pub block: u32,
}

/// Create an new auction. Stores the resulting auction to the ledger to begin on the next block.
/// Create a new auction. Stores the resulting auction to the ledger to begin on the next block.
///
/// Returns the AuctionData object created
///
Expand All @@ -82,38 +82,15 @@ pub fn create_auction(
lot: &Vec<Address>,
percent: u32,
) -> AuctionData {
match AuctionType::from_u32(e, auction_type) {
AuctionType::UserLiquidation => {
let auction_data = create_user_liq_auction_data(e, user, bid, lot, percent);
storage::set_auction(
e,
&(AuctionType::UserLiquidation as u32),
user,
&auction_data,
);
auction_data
}
AuctionType::BadDebtAuction => {
let auction_data = create_bad_debt_auction_data(e, user, bid, lot, percent);
storage::set_auction(
e,
&(AuctionType::BadDebtAuction as u32),
&user,
&auction_data,
);
auction_data
}
AuctionType::InterestAuction => {
let auction_data = create_interest_auction_data(e, user, bid, lot, percent);
storage::set_auction(
e,
&(AuctionType::InterestAuction as u32),
&user,
&auction_data,
);
auction_data
}
}
// panics if auction_type parameter is not valid
let auction_type_enum = AuctionType::from_u32(e, auction_type);
let auction_data = match auction_type_enum {
AuctionType::UserLiquidation => create_user_liq_auction_data(e, user, bid, lot, percent),
AuctionType::BadDebtAuction => create_bad_debt_auction_data(e, user, bid, lot, percent),
AuctionType::InterestAuction => create_interest_auction_data(e, user, bid, lot, percent),
};
storage::set_auction(e, &auction_type, user, &auction_data);
auction_data
}

/// Delete a liquidation auction if the user being liquidated
Expand Down Expand Up @@ -889,6 +866,92 @@ mod tests {
});
}

#[test]
#[should_panic(expected = "Error(Contract, #1200)")]
fn test_create_auction_invalid_type() {
let e = Env::default();
e.mock_all_auths();
e.budget().reset_unlimited(); // setup exhausts budget

e.ledger().set(LedgerInfo {
timestamp: 12345,
protocol_version: 22,
sequence_number: 50,
network_id: Default::default(),
base_reserve: 10,
min_temp_entry_ttl: 10,
min_persistent_entry_ttl: 10,
max_entry_ttl: 3110400,
});

let bombadil = Address::generate(&e);

let pool_address = create_pool(&e);
let (usdc_id, _) = testutils::create_token_contract(&e, &bombadil);
let (blnd_id, _) = testutils::create_blnd_token(&e, &pool_address, &bombadil);

let (backstop_token_id, _) = create_comet_lp_pool(&e, &bombadil, &blnd_id, &usdc_id);
let (backstop_address, backstop_client) = testutils::create_backstop(&e);
testutils::setup_backstop(
&e,
&pool_address,
&backstop_address,
&backstop_token_id,
&usdc_id,
&blnd_id,
);
backstop_client.deposit(&bombadil, &pool_address, &(50 * SCALAR_7));
backstop_client.update_tkn_val();
let (oracle_id, oracle_client) = testutils::create_mock_oracle(&e);

let (underlying_0, _) = testutils::create_token_contract(&e, &bombadil);
let (mut reserve_config_0, mut reserve_data_0) = testutils::default_reserve_meta();
reserve_data_0.last_time = 12345;
reserve_data_0.backstop_credit = 200_0000000;
reserve_data_0.b_supply = 1000_0000000;
reserve_data_0.d_supply = 750_0000000;
reserve_config_0.index = 0;
testutils::create_reserve(
&e,
&pool_address,
&underlying_0,
&reserve_config_0,
&reserve_data_0,
);

oracle_client.set_data(
&bombadil,
&Asset::Other(Symbol::new(&e, "USD")),
&vec![
&e,
Asset::Stellar(underlying_0.clone()),
Asset::Stellar(usdc_id),
],
&7,
&300,
);
oracle_client.set_price_stable(&vec![&e, 2_0000000, 1_0000000]);

let pool_config = PoolConfig {
oracle: oracle_id,
bstop_rate: 0_1000000,
status: 0,
max_positions: 4,
};
e.as_contract(&pool_address, || {
storage::set_pool_config(&e, &pool_config);

create_auction(
&e,
3,
&backstop_address,
&vec![&e, backstop_token_id],
&vec![&e, underlying_0],
100,
);
});
}

#[test]
fn test_delete_user_liquidation() {
let e = Env::default();
Expand Down
2 changes: 1 addition & 1 deletion pool/src/auctions/backstop_interest_auction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub fn create_interest_auction_data(
percent: u32,
) -> AuctionData {
let backstop = storage::get_backstop(e);
if user.clone() != backstop {
if user != &backstop {
panic_with_error!(e, PoolError::BadRequest);
}
if percent != 100 {
Expand Down
2 changes: 1 addition & 1 deletion pool/src/auctions/bad_debt_auction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub fn create_bad_debt_auction_data(
percent: u32,
) -> AuctionData {
let backstop = storage::get_backstop(e);
if user.clone() != backstop {
if user != &backstop {
panic_with_error!(e, PoolError::BadRequest);
}
if percent != 100 {
Expand Down
Loading

0 comments on commit 9aa733b

Please sign in to comment.