Copy use fuels::{prelude::*, types::Identity};
use test_utils::data_structures::{ContractInstance, PRECISION};
use test_utils::interfaces::oracle::oracle_abi;
use test_utils::interfaces::protocol_manager::ProtocolManager;
use test_utils::interfaces::pyth_oracle::PYTH_TIMESTAMP;
use test_utils::{
interfaces::{
borrow_operations::{borrow_operations_abi, BorrowOperations},
protocol_manager::protocol_manager_abi,
pyth_oracle::{pyth_oracle_abi, pyth_price_feed,pyth_price_no_precision_with_time},
token::token_abi,
trove_manager::{trove_manager_abi, trove_manager_utils, Status},
},
setup::common::setup_protocol,
};
#[tokio::test]
async fn can_not_redeem_when_all_asset_mcr_less_than_1_35() {
let (contracts, _admin, mut wallets) = setup_protocol(5, true, false).await;
let healthy_wallet1 = wallets.pop().unwrap();
let healthy_wallet2 = wallets.pop().unwrap();
let healthy_wallet3 = wallets.pop().unwrap();
let balance: u64 = 12_000 * PRECISION;
token_abi::mint_to_id(
&contracts.asset_contracts[0].asset,
balance,
Identity::Address(healthy_wallet1.address().into()),
)
.await;
token_abi::mint_to_id(
&contracts.asset_contracts[0].asset,
balance,
Identity::Address(healthy_wallet2.address().into()),
)
.await;
token_abi::mint_to_id(
&contracts.asset_contracts[0].asset,
balance,
Identity::Address(healthy_wallet3.address().into()),
)
.await;
let borrow_operations_healthy_wallet1 = ContractInstance::new(
BorrowOperations::new(
contracts.borrow_operations.contract.contract_id().clone(),
healthy_wallet1.clone(),
),
contracts.borrow_operations.implementation_id.clone(),
);
let coll1 = 12_000 * PRECISION;
let debt1 = 6_000 * PRECISION;
oracle_abi::set_debug_timestamp(&contracts.asset_contracts[0].oracle, PYTH_TIMESTAMP).await;
pyth_oracle_abi::update_price_feeds(
&contracts.asset_contracts[0].mock_pyth_oracle,
// pyth_price_feed(1),
pyth_price_no_precision_with_time(1 * PRECISION, PYTH_TIMESTAMP),
)
.await;
borrow_operations_abi::open_trove(
&borrow_operations_healthy_wallet1,
&contracts.asset_contracts[0].oracle,
&contracts.asset_contracts[0].mock_pyth_oracle,
&contracts.asset_contracts[0].mock_redstone_oracle,
&contracts.asset_contracts[0].asset,
&contracts.usdf,
&contracts.fpt_staking,
&contracts.sorted_troves,
&contracts.asset_contracts[0].trove_manager,
&contracts.active_pool,
coll1,
debt1,
Identity::Address(Address::zeroed()),
Identity::Address(Address::zeroed()),
)
.await
.unwrap();
let borrow_operations_healthy_wallet2 = ContractInstance::new(
BorrowOperations::new(
contracts.borrow_operations.contract.contract_id().clone(),
healthy_wallet2.clone(),
),
contracts.borrow_operations.implementation_id.clone(),
);
let coll2: u64 = 12_000 * PRECISION;
let debt2: u64 = 6_000 * PRECISION;
borrow_operations_abi::open_trove(
&borrow_operations_healthy_wallet2,
&contracts.asset_contracts[0].oracle,
&contracts.asset_contracts[0].mock_pyth_oracle,
&contracts.asset_contracts[0].mock_redstone_oracle,
&contracts.asset_contracts[0].asset,
&contracts.usdf,
&contracts.fpt_staking,
&contracts.sorted_troves,
&contracts.asset_contracts[0].trove_manager,
&contracts.active_pool,
coll2,
debt2,
Identity::Address(Address::zeroed()),
Identity::Address(Address::zeroed()),
)
.await
.unwrap();
let borrow_operations_healthy_wallet3 = ContractInstance::new(
BorrowOperations::new(
contracts.borrow_operations.contract.contract_id().clone(),
healthy_wallet3.clone(),
),
contracts.borrow_operations.implementation_id.clone(),
);
let coll3: u64 = 12_000 * PRECISION;
let debt3: u64 = 6_000 * PRECISION;
borrow_operations_abi::open_trove(
&borrow_operations_healthy_wallet3,
&contracts.asset_contracts[0].oracle,
&contracts.asset_contracts[0].mock_pyth_oracle,
&contracts.asset_contracts[0].mock_redstone_oracle,
&contracts.asset_contracts[0].asset,
&contracts.usdf,
&contracts.fpt_staking,
&contracts.sorted_troves,
&contracts.asset_contracts[0].trove_manager,
&contracts.active_pool,
coll3,
debt3,
Identity::Address(Address::zeroed()),
Identity::Address(Address::zeroed()),
)
.await
.unwrap();
// Troves 12 / 6 = 2
oracle_abi::set_debug_timestamp(&contracts.asset_contracts[0].oracle, PYTH_TIMESTAMP + 1).await;
pyth_oracle_abi::update_price_feeds(
&contracts.asset_contracts[0].mock_pyth_oracle,
// pyth_price_feed(1),
pyth_price_no_precision_with_time(6 * PRECISION / 10 , PYTH_TIMESTAMP + 1), // 1 -> 0.6
)
.await;
// Troves 12*0.6 / 6 = 1.2 < MCR = 1.35
let redemption_amount: u64 = 6_000 * PRECISION;
let protocol_manager_health1 = ContractInstance::new(
ProtocolManager::new(
contracts.protocol_manager.contract.contract_id().clone(),
healthy_wallet1.clone(),
),
contracts.protocol_manager.implementation_id,
);
oracle_abi::set_debug_timestamp(&contracts.asset_contracts[1].oracle, PYTH_TIMESTAMP).await;
pyth_oracle_abi::update_price_feeds(
&contracts.asset_contracts[1].mock_pyth_oracle,
pyth_price_feed(1),
)
.await;
// The transaction was reverted because a zero address was inserted.
protocol_manager_abi::redeem_collateral(
&protocol_manager_health1,
redemption_amount,
10,
0,
None,
None,
&contracts.usdf,
&contracts.fpt_staking,
&contracts.coll_surplus_pool,
&contracts.default_pool,
&contracts.active_pool,
&contracts.sorted_troves,
&contracts.asset_contracts,
)
.await;
}