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

Fallback mechanism for oracle priority #118

Merged
merged 18 commits into from
Aug 26, 2024
2 changes: 2 additions & 0 deletions Forc.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
[workspace]
members = [
"./contracts/mock-pyth-contract",
"./contracts/mock-redstone-contract",
"./contracts/mock-oracle-contract",
"./contracts/multi-trove-getter-contract",
"./contracts/stability-pool-contract",
Expand Down
6 changes: 3 additions & 3 deletions contracts/borrow-operations-contract/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use libraries::trove_manager_interface::TroveManager;
use libraries::sorted_troves_interface::SortedTroves;
use libraries::fpt_staking_interface::FPTStaking;
use libraries::coll_surplus_pool_interface::CollSurplusPool;
use libraries::mock_oracle_interface::MockOracle;
use libraries::mock_oracle_interface::Oracle;
Braqzen marked this conversation as resolved.
Show resolved Hide resolved
use libraries::borrow_operations_interface::BorrowOperations;
use libraries::fluid_math::*;
use std::{
Expand Down Expand Up @@ -94,7 +94,7 @@ impl BorrowOperations for Contract {
let fpt_staking_contract = storage.fpt_staking_contract.read();
let active_pool_contract = storage.active_pool_contract.read();
let sorted_troves_contract = storage.sorted_troves_contract.read();
let oracle = abi(MockOracle, asset_contracts.oracle.bits());
let oracle = abi(Oracle, asset_contracts.oracle.bits());
let trove_manager = abi(TroveManager, asset_contracts.trove_manager.bits());
let sorted_troves = abi(SortedTroves, sorted_troves_contract.bits());
let mut vars = LocalVariables_OpenTrove::new();
Expand Down Expand Up @@ -274,7 +274,7 @@ fn internal_adjust_trove(
let fpt_staking_contract_cache = storage.fpt_staking_contract.read();
let active_pool_contract_cache = storage.active_pool_contract.read();
let sorted_troves_contract_cache = storage.sorted_troves_contract.read();
let oracle = abi(MockOracle, asset_contracts_cache.oracle.bits());
let oracle = abi(Oracle, asset_contracts_cache.oracle.bits());
let trove_manager = abi(TroveManager, asset_contracts_cache.trove_manager.bits());
let sorted_troves = abi(SortedTroves, sorted_troves_contract_cache.bits());
let price = oracle.get_price();
Expand Down
117 changes: 113 additions & 4 deletions contracts/borrow-operations-contract/tests/failure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@ use fuels::{prelude::*, types::Identity};

use test_utils::{
data_structures::PRECISION,
interfaces::borrow_operations::borrow_operations_abi,
interfaces::sorted_troves::sorted_troves_abi,
interfaces::{active_pool::active_pool_abi, token::token_abi},
interfaces::{trove_manager::trove_manager_abi, usdf_token::usdf_token_abi},
interfaces::{
active_pool::active_pool_abi,
borrow_operations::borrow_operations_abi,
oracle::oracle_abi,
pyth_oracle::{pyth_oracle_abi, pyth_price_feed, PYTH_TIMESTAMP},
redstone_oracle::{redstone_oracle_abi, redstone_price_feed},
sorted_troves::sorted_troves_abi,
token::token_abi,
trove_manager::trove_manager_abi,
usdf_token::usdf_token_abi,
},
setup::common::{deploy_token, deploy_usdf_token, setup_protocol},
utils::{calculate_icr, with_min_borrow_fee},
};
Expand All @@ -26,9 +33,29 @@ async fn fails_open_two_troves_of_same_coll_type() {
let col_amount = 1_200 * PRECISION;
let debt_amount = 600 * 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),
)
.await;

redstone_oracle_abi::write_prices(
&contracts.asset_contracts[0].mock_redstone_oracle,
redstone_price_feed(vec![1]),
)
.await;
redstone_oracle_abi::set_timestamp(
&contracts.asset_contracts[0].mock_redstone_oracle,
PYTH_TIMESTAMP,
)
.await;

borrow_operations_abi::open_trove(
&contracts.borrow_operations,
&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,
Expand All @@ -46,6 +73,8 @@ async fn fails_open_two_troves_of_same_coll_type() {
let res = borrow_operations_abi::open_trove(
&contracts.borrow_operations,
&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,
Expand Down Expand Up @@ -184,6 +213,8 @@ async fn fails_open_trove_under_minimum_collateral_ratio() {
let res = borrow_operations_abi::open_trove(
&contracts.borrow_operations,
&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,
Expand Down Expand Up @@ -219,9 +250,17 @@ async fn fails_open_trove_under_min_usdf_required() {
let debt_amount = 400 * PRECISION;
// 100 USDF < 500 USDF

pyth_oracle_abi::update_price_feeds(
&contracts.asset_contracts[0].mock_pyth_oracle,
pyth_price_feed(1),
)
.await;

let res = borrow_operations_abi::open_trove(
&contracts.borrow_operations,
&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,
Expand Down Expand Up @@ -256,9 +295,29 @@ async fn fails_reduce_debt_under_min_usdf_required() {
let coll_amount = 1_200 * PRECISION;
let debt_amount = 600 * 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),
)
.await;

redstone_oracle_abi::write_prices(
&contracts.asset_contracts[0].mock_redstone_oracle,
redstone_price_feed(vec![1]),
)
.await;
redstone_oracle_abi::set_timestamp(
&contracts.asset_contracts[0].mock_redstone_oracle,
PYTH_TIMESTAMP,
)
.await;

borrow_operations_abi::open_trove(
&contracts.borrow_operations,
&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,
Expand All @@ -279,6 +338,8 @@ async fn fails_reduce_debt_under_min_usdf_required() {
let res = borrow_operations_abi::repay_usdf(
&contracts.borrow_operations,
&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.sorted_troves,
Expand Down Expand Up @@ -312,9 +373,29 @@ async fn fails_decrease_collateral_under_mcr() {
let coll_amount = 1_200 * PRECISION;
let debt_amount = 600 * 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),
)
.await;

redstone_oracle_abi::write_prices(
&contracts.asset_contracts[0].mock_redstone_oracle,
redstone_price_feed(vec![1]),
)
.await;
redstone_oracle_abi::set_timestamp(
&contracts.asset_contracts[0].mock_redstone_oracle,
PYTH_TIMESTAMP,
)
.await;

borrow_operations_abi::open_trove(
&contracts.borrow_operations,
&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,
Expand All @@ -332,6 +413,8 @@ async fn fails_decrease_collateral_under_mcr() {
let res = borrow_operations_abi::withdraw_coll(
&contracts.borrow_operations,
&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.sorted_troves,
&contracts.asset_contracts[0].trove_manager,
Expand Down Expand Up @@ -379,9 +462,29 @@ async fn fails_incorrect_token_as_collateral_or_repayment() {
)
.await;

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),
)
.await;

redstone_oracle_abi::write_prices(
&contracts.asset_contracts[0].mock_redstone_oracle,
redstone_price_feed(vec![1]),
)
.await;
redstone_oracle_abi::set_timestamp(
&contracts.asset_contracts[0].mock_redstone_oracle,
PYTH_TIMESTAMP,
)
.await;

let res = borrow_operations_abi::open_trove(
&contracts.borrow_operations,
&contracts.asset_contracts[0].oracle,
&contracts.asset_contracts[0].mock_pyth_oracle,
&contracts.asset_contracts[0].mock_redstone_oracle,
&mock_fake_token,
&contracts.usdf,
&contracts.fpt_staking,
Expand All @@ -405,6 +508,8 @@ async fn fails_incorrect_token_as_collateral_or_repayment() {
borrow_operations_abi::open_trove(
&contracts.borrow_operations,
&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,
Expand All @@ -422,6 +527,8 @@ async fn fails_incorrect_token_as_collateral_or_repayment() {
let res = borrow_operations_abi::add_coll(
&contracts.borrow_operations,
&contracts.asset_contracts[0].oracle,
&contracts.asset_contracts[0].mock_pyth_oracle,
&contracts.asset_contracts[0].mock_redstone_oracle,
&mock_fake_token,
&contracts.usdf,
&contracts.sorted_troves,
Expand Down Expand Up @@ -461,6 +568,8 @@ async fn fails_incorrect_token_as_collateral_or_repayment() {
let res = borrow_operations_abi::repay_usdf(
&contracts.borrow_operations,
&contracts.asset_contracts[0].oracle,
&contracts.asset_contracts[0].mock_pyth_oracle,
&contracts.asset_contracts[0].mock_redstone_oracle,
&contracts.asset_contracts[0].asset,
&fake_usdf_token,
&contracts.sorted_troves,
Expand Down
Loading