diff --git a/pool/src/auctions/auction.rs b/pool/src/auctions/auction.rs index cc5e6ba2..8ec87f85 100644 --- a/pool/src/auctions/auction.rs +++ b/pool/src/auctions/auction.rs @@ -274,7 +274,7 @@ mod tests { use crate::{ pool::Positions, storage::PoolConfig, - testutils::{self, create_pool}, + testutils::{self, create_comet_lp_pool, create_pool}, }; use super::*; @@ -441,15 +441,20 @@ mod tests { let pool_address = create_pool(&e); let (usdc_id, _) = testutils::create_usdc_token(&e, &pool_address, &bombadil); - let (backstop_address, _backstop_client) = testutils::create_backstop(&e); + 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, - &Address::generate(&e), - &Address::generate(&e), - &Address::generate(&e), + &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); diff --git a/pool/src/auctions/backstop_interest_auction.rs b/pool/src/auctions/backstop_interest_auction.rs index f0ea8601..f21b709f 100644 --- a/pool/src/auctions/backstop_interest_auction.rs +++ b/pool/src/auctions/backstop_interest_auction.rs @@ -251,7 +251,7 @@ mod tests { e.as_contract(&pool_address, || { storage::set_pool_config(&e, &pool_config); - let result = create_interest_auction_data(&e, &backstop_address, &vec![&e]); + create_interest_auction_data(&e, &backstop_address, &vec![&e]); }); } @@ -275,11 +275,10 @@ mod tests { let bombadil = Address::generate(&e); let pool_address = create_pool(&e); - let (usdc_id, usdc_client) = testutils::create_usdc_token(&e, &pool_address, &bombadil); - let (blnd_id, blnd_client) = testutils::create_blnd_token(&e, &pool_address, &bombadil); + let (usdc_id, _) = testutils::create_usdc_token(&e, &pool_address, &bombadil); + let (blnd_id, _) = testutils::create_blnd_token(&e, &pool_address, &bombadil); - let (backstop_token_id, backstop_token_client) = - create_comet_lp_pool(&e, &bombadil, &blnd_id, &usdc_id); + 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, @@ -393,11 +392,10 @@ mod tests { let bombadil = Address::generate(&e); let pool_address = create_pool(&e); - let (usdc_id, usdc_client) = testutils::create_usdc_token(&e, &pool_address, &bombadil); - let (blnd_id, blnd_client) = testutils::create_blnd_token(&e, &pool_address, &bombadil); + let (usdc_id, _) = testutils::create_usdc_token(&e, &pool_address, &bombadil); + let (blnd_id, _) = testutils::create_blnd_token(&e, &pool_address, &bombadil); - let (backstop_token_id, backstop_token_client) = - create_comet_lp_pool(&e, &bombadil, &blnd_id, &usdc_id); + 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, @@ -498,132 +496,134 @@ mod tests { }); } - // #[test] - // fn test_fill_interest_auction() { - // let e = Env::default(); - // e.mock_all_auths_allowing_non_root_auth(); - // e.budget().reset_unlimited(); - - // e.ledger().set(LedgerInfo { - // timestamp: 12345, - // protocol_version: 20, - // sequence_number: 301, - // network_id: Default::default(), - // base_reserve: 10, - // min_temp_entry_ttl: 10, - // min_persistent_entry_ttl: 10, - // max_entry_ttl: 2000000, - // }); - - // let bombadil = Address::generate(&e); - // let samwise = Address::generate(&e); - - // let pool_address = create_pool(&e); - - // let (usdc_id, usdc_client) = testutils::create_usdc_token(&e, &pool_address, &bombadil); - // let (blnd_id, blnd_client) = testutils::create_blnd_token(&e, &pool_address, &bombadil); - - // let (backstop_token_id, backstop_token_client) = - // create_comet_lp_pool(&e, &bombadil, &blnd_id, &usdc_id); - // blnd_client.mint(&samwise, &10_000_0000000); - // usdc_client.mint(&samwise, &250_0000000); - // let exp_ledger = e.ledger().sequence() + 100; - // blnd_client.approve(&bombadil, &backstop_token_id, &2_000_0000000, &exp_ledger); - // usdc_client.approve(&bombadil, &backstop_token_id, &2_000_0000000, &exp_ledger); - // backstop_token_client.join_pool( - // &(500 * SCALAR_7), - // &vec![&e, 10_000_0000000, 250_0000000], - // &samwise, - // ); - // 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 (underlying_0, underlying_0_client) = testutils::create_token_contract(&e, &bombadil); - // let (mut reserve_config_0, mut reserve_data_0) = testutils::default_reserve_meta(); - // reserve_data_0.b_rate = 1_100_000_000; - // reserve_data_0.b_supply = 200_000_0000000; - // reserve_data_0.d_supply = 100_000_0000000; - // reserve_data_0.last_time = 12345; - // reserve_data_0.backstop_credit = 100_0000000; - // reserve_config_0.index = 0; - // testutils::create_reserve( - // &e, - // &pool_address, - // &underlying_0, - // &reserve_config_0, - // &reserve_data_0, - // ); - // underlying_0_client.mint(&pool_address, &1_000_0000000); - - // let (underlying_1, underlying_1_client) = testutils::create_token_contract(&e, &bombadil); - // let (mut reserve_config_1, mut reserve_data_1) = testutils::default_reserve_meta(); - // reserve_data_1.b_rate = 1_100_000_000; - // reserve_data_0.b_supply = 10_000_0000000; - // reserve_data_0.b_supply = 7_000_0000000; - // reserve_data_1.last_time = 12345; - // reserve_data_1.backstop_credit = 30_0000000; - // reserve_config_1.index = 1; - // testutils::create_reserve( - // &e, - // &pool_address, - // &underlying_1, - // &reserve_config_1, - // &reserve_data_1, - // ); - // underlying_1_client.mint(&pool_address, &1_000_0000000); - - // let pool_config = PoolConfig { - // oracle: Address::generate(&e), - // bstop_rate: 0_1000000, - // status: 0, - // max_positions: 4, - // }; - // let mut auction_data = AuctionData { - // bid: map![&e, (usdc_id.clone(), 95_0000000)], - // lot: map![ - // &e, - // (underlying_0.clone(), 100_0000000), - // (underlying_1.clone(), 25_0000000) - // ], - // block: 51, - // }; - // usdc_client.mint(&samwise, &100_0000000); - // e.as_contract(&pool_address, || { - // e.mock_all_auths_allowing_non_root_auth(); - // storage::set_auction( - // &e, - // &(AuctionType::InterestAuction as u32), - // &backstop_address, - // &auction_data, - // ); - // storage::set_pool_config(&e, &pool_config); - // storage::set_backstop(&e, &backstop_address); - // storage::set_usdc_token(&e, &usdc_id); - - // let mut pool = Pool::load(&e); - // fill_interest_auction(&e, &mut pool, &mut auction_data, &samwise); - // pool.store_cached_reserves(&e); - - // assert_eq!(usdc_client.balance(&samwise), 5_0000000); - // assert_eq!(usdc_client.balance(&backstop_address), 95_0000000); - // assert_eq!(underlying_0_client.balance(&samwise), 100_0000000); - // assert_eq!(underlying_1_client.balance(&samwise), 25_0000000); - // // verify only filled backstop credits get deducted from total - // let reserve_0_data = storage::get_res_data(&e, &underlying_0); - // assert_eq!(reserve_0_data.backstop_credit, 0); - // let reserve_1_data = storage::get_res_data(&e, &underlying_1); - // assert_eq!(reserve_1_data.backstop_credit, 5_0000000); - // }); - // } + #[test] + fn test_fill_interest_auction() { + let e = Env::default(); + e.mock_all_auths_allowing_non_root_auth(); + e.budget().reset_unlimited(); + + e.ledger().set(LedgerInfo { + timestamp: 12345, + protocol_version: 20, + sequence_number: 301, + network_id: Default::default(), + base_reserve: 10, + min_temp_entry_ttl: 10, + min_persistent_entry_ttl: 10, + max_entry_ttl: 2000000, + }); + + let bombadil = Address::generate(&e); + let samwise = Address::generate(&e); + + let pool_address = create_pool(&e); + + let (usdc_id, usdc_client) = testutils::create_usdc_token(&e, &pool_address, &bombadil); + let (blnd_id, blnd_client) = testutils::create_blnd_token(&e, &pool_address, &bombadil); + + let (backstop_token_id, backstop_token_client) = + create_comet_lp_pool(&e, &bombadil, &blnd_id, &usdc_id); + blnd_client.mint(&samwise, &10_000_0000000); + usdc_client.mint(&samwise, &250_0000000); + let exp_ledger = e.ledger().sequence() + 100; + blnd_client.approve(&bombadil, &backstop_token_id, &2_000_0000000, &exp_ledger); + usdc_client.approve(&bombadil, &backstop_token_id, &2_000_0000000, &exp_ledger); + backstop_token_client.join_pool( + &(100 * SCALAR_7), + &vec![&e, 10_000_0000000, 250_0000000], + &samwise, + ); + 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 (underlying_0, underlying_0_client) = testutils::create_token_contract(&e, &bombadil); + let (mut reserve_config_0, mut reserve_data_0) = testutils::default_reserve_meta(); + reserve_data_0.b_rate = 1_100_000_000; + reserve_data_0.b_supply = 200_000_0000000; + reserve_data_0.d_supply = 100_000_0000000; + reserve_data_0.last_time = 12345; + reserve_data_0.backstop_credit = 100_0000000; + reserve_config_0.index = 0; + testutils::create_reserve( + &e, + &pool_address, + &underlying_0, + &reserve_config_0, + &reserve_data_0, + ); + underlying_0_client.mint(&pool_address, &1_000_0000000); + + let (underlying_1, underlying_1_client) = testutils::create_token_contract(&e, &bombadil); + let (mut reserve_config_1, mut reserve_data_1) = testutils::default_reserve_meta(); + reserve_data_1.b_rate = 1_100_000_000; + reserve_data_0.b_supply = 10_000_0000000; + reserve_data_0.b_supply = 7_000_0000000; + reserve_data_1.last_time = 12345; + reserve_data_1.backstop_credit = 30_0000000; + reserve_config_1.index = 1; + testutils::create_reserve( + &e, + &pool_address, + &underlying_1, + &reserve_config_1, + &reserve_data_1, + ); + underlying_1_client.mint(&pool_address, &1_000_0000000); + + let pool_config = PoolConfig { + oracle: Address::generate(&e), + bstop_rate: 0_1000000, + status: 0, + max_positions: 4, + }; + let mut auction_data = AuctionData { + bid: map![&e, (backstop_token_id.clone(), 75_0000000)], + lot: map![ + &e, + (underlying_0.clone(), 100_0000000), + (underlying_1.clone(), 25_0000000) + ], + block: 51, + }; + e.as_contract(&pool_address, || { + e.mock_all_auths_allowing_non_root_auth(); + storage::set_auction( + &e, + &(AuctionType::InterestAuction as u32), + &backstop_address, + &auction_data, + ); + storage::set_pool_config(&e, &pool_config); + storage::set_backstop(&e, &backstop_address); + storage::set_usdc_token(&e, &usdc_id); + let mut pool = Pool::load(&e); + let backstop_token_balance_pre_fill = backstop_token_client.balance(&backstop_address); + fill_interest_auction(&e, &mut pool, &mut auction_data, &samwise); + pool.store_cached_reserves(&e); + + assert_eq!(backstop_token_client.balance(&samwise), 25_0000000); + assert_eq!( + backstop_token_client.balance(&backstop_address), + backstop_token_balance_pre_fill + 75_0000000 + ); + assert_eq!(underlying_0_client.balance(&samwise), 100_0000000); + assert_eq!(underlying_1_client.balance(&samwise), 25_0000000); + // verify only filled backstop credits get deducted from total + let reserve_0_data = storage::get_res_data(&e, &underlying_0); + assert_eq!(reserve_0_data.backstop_credit, 0); + let reserve_1_data = storage::get_res_data(&e, &underlying_1); + assert_eq!(reserve_1_data.backstop_credit, 5_0000000); + }); + } #[test] #[should_panic(expected = "Error(Contract, #1200)")] diff --git a/pool/src/contract.rs b/pool/src/contract.rs index 515faab3..ab9761c4 100644 --- a/pool/src/contract.rs +++ b/pool/src/contract.rs @@ -412,8 +412,10 @@ impl Pool for PoolContract { storage::extend_instance(&e); let auction_data = auctions::create_bad_debt_auction(&e); - e.events() - .publish((Symbol::new(&e, "new_auction"), 1), auction_data.clone()); + e.events().publish( + (Symbol::new(&e, "new_auction"), 1 as u32), + auction_data.clone(), + ); auction_data } @@ -422,8 +424,10 @@ impl Pool for PoolContract { storage::extend_instance(&e); let auction_data = auctions::create_interest_auction(&e, &assets); - e.events() - .publish((Symbol::new(&e, "new_auction"), 2), auction_data.clone()); + e.events().publish( + (Symbol::new(&e, "new_auction"), 2 as u32), + auction_data.clone(), + ); auction_data } diff --git a/pool/src/pool/actions.rs b/pool/src/pool/actions.rs index 0f60cce7..4bafadb0 100644 --- a/pool/src/pool/actions.rs +++ b/pool/src/pool/actions.rs @@ -320,8 +320,9 @@ pub fn build_actions_from_request( mod tests { use crate::{ + constants::SCALAR_7, storage::{self, PoolConfig}, - testutils::{self, create_pool}, + testutils::{self, create_comet_lp_pool, create_pool}, AuctionData, AuctionType, Positions, }; @@ -1348,7 +1349,7 @@ mod tests { e.ledger().set(LedgerInfo { timestamp: 12345, protocol_version: 20, - sequence_number: 51 + 200, + sequence_number: 51 + 250, network_id: Default::default(), base_reserve: 10, min_temp_entry_ttl: 10, @@ -1361,15 +1362,31 @@ mod tests { let pool_address = create_pool(&e); let (usdc_id, usdc_client) = testutils::create_usdc_token(&e, &pool_address, &bombadil); - let (backstop_address, _backstop_client) = testutils::create_backstop(&e); + let (blnd_id, blnd_client) = testutils::create_blnd_token(&e, &pool_address, &bombadil); + + let (backstop_token_id, backstop_token_client) = + create_comet_lp_pool(&e, &bombadil, &blnd_id, &usdc_id); + let (backstop_address, backstop_client) = testutils::create_backstop(&e); + blnd_client.mint(&samwise, &10_000_0000000); + usdc_client.mint(&samwise, &250_0000000); + let exp_ledger = e.ledger().sequence() + 100; + blnd_client.approve(&bombadil, &backstop_token_id, &2_000_0000000, &exp_ledger); + usdc_client.approve(&bombadil, &backstop_token_id, &2_000_0000000, &exp_ledger); + backstop_token_client.join_pool( + &(100 * SCALAR_7), + &vec![&e, 10_000_0000000, 250_0000000], + &samwise, + ); testutils::setup_backstop( &e, &pool_address, &backstop_address, - &Address::generate(&e), + &backstop_token_id, &usdc_id, - &Address::generate(&e), + &blnd_id, ); + backstop_client.deposit(&bombadil, &pool_address, &(50 * SCALAR_7)); + backstop_client.update_tkn_val(); let (underlying_0, underlying_0_client) = testutils::create_token_contract(&e, &bombadil); let (mut reserve_config_0, mut reserve_data_0) = testutils::default_reserve_meta(); @@ -1420,7 +1437,7 @@ mod tests { max_positions: 2, }; let auction_data = AuctionData { - bid: map![&e, (usdc_id.clone(), 952_0000000)], + bid: map![&e, (backstop_token_id.clone(), 100_0000000)], lot: map![ &e, (underlying_0.clone(), 100_0000000), @@ -1428,7 +1445,6 @@ mod tests { ], block: 51, }; - usdc_client.mint(&samwise, &952_0000000); e.as_contract(&pool_address, || { e.mock_all_auths_allowing_non_root_auth(); @@ -1439,6 +1455,7 @@ mod tests { &backstop_address, &auction_data, ); + storage::set_backstop(&e, &backstop_address); let mut pool = Pool::load(&e); @@ -1450,14 +1467,17 @@ mod tests { amount: 100, }, ]; + let pre_fill_backstop_token_balance = backstop_token_client.balance(&backstop_address); let (actions, _, health_check) = build_actions_from_request(&e, &mut pool, &samwise, requests); - assert_eq!(usdc_client.balance(&samwise), 0); - assert_eq!(usdc_client.balance(&backstop_address), 952_0000000); + assert_eq!(backstop_token_client.balance(&samwise), 25_0000000); + assert_eq!( + backstop_token_client.balance(&backstop_address), + pre_fill_backstop_token_balance + 75_0000000 + ); assert_eq!(underlying_0_client.balance(&samwise), 100_0000000); assert_eq!(underlying_1_client.balance(&samwise), 25_0000000); - assert_eq!(usdc_client.balance(&samwise), 0); assert_eq!(health_check, false); assert_eq!( storage::has_auction( diff --git a/test-suites/tests/test_liquidation.rs b/test-suites/tests/test_liquidation.rs index 67c1b4eb..975a2b5a 100644 --- a/test-suites/tests/test_liquidation.rs +++ b/test-suites/tests/test_liquidation.rs @@ -1,9 +1,6 @@ #![cfg(test)] use cast::i128; -use pool::{ - PoolDataKey, Positions, Request, RequestType, ReserveConfig, ReserveData, - ReserveEmissionMetadata, -}; +use pool::{PoolDataKey, Positions, Request, RequestType, ReserveConfig, ReserveData}; use soroban_fixed_point_math::FixedPoint; use soroban_sdk::{ testutils::{Address as AddressTestTrait, Events}, @@ -12,7 +9,6 @@ use soroban_sdk::{ use test_suites::{ assertions::assert_approx_eq_abs, create_fixture_with_data, - pool::default_reserve_metadata, test_fixture::{TokenIndex, SCALAR_7}, }; @@ -175,11 +171,9 @@ fn test_liquidations() { .lot .get_unchecked(fixture.tokens[TokenIndex::WETH].address.clone()); assert_approx_eq_abs(weth_interest_lot_amount, 0_002671545, 5000); - let usdc_donate_bid_amount = auction_data - .bid - .get_unchecked(fixture.tokens[TokenIndex::USDC].address.clone()); + let lp_donate_bid_amount = auction_data.bid.get_unchecked(fixture.lp.address.clone()); //NOTE: bid STABLE amount is seven decimals whereas reserve(and lot) STABLE has 6 decomals - assert_approx_eq_abs(usdc_donate_bid_amount, 392_1769961, SCALAR_7); + assert_approx_eq_abs(lp_donate_bid_amount, 313_7415968, SCALAR_7); assert_eq!(auction_data.block, 151); let liq_pct = 30; let events = fixture.env.events().all(); @@ -190,7 +184,7 @@ fn test_liquidations() { &fixture.env, ( pool_fixture.pool.address.clone(), - (Symbol::new(&fixture.env, "new_auction"), 2).into_val(&fixture.env), + (Symbol::new(&fixture.env, "new_auction"), 2 as u32).into_val(&fixture.env), auction_data.into_val(&fixture.env) ) ] @@ -199,7 +193,6 @@ fn test_liquidations() { let auction_data = pool_fixture .pool .new_liquidation_auction(&samwise, &liq_pct); - let usdc_bid_amount = auction_data .bid .get_unchecked(fixture.tokens[TokenIndex::STABLE].address.clone()); @@ -780,7 +773,7 @@ fn test_liquidations() { .withdraw(&frodo, &pool_fixture.pool.address, &original_deposit); assert_approx_eq_abs( fixture.lp.balance(&frodo) - pre_withdraw_frodo_bstp, - original_deposit - 717_1043530 - 4302_6261190, + original_deposit - 717_1043530 - 4302_6261190 + 313_7415968, SCALAR_7, ); fixture