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
Merged
18 changes: 9 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[workspace]
resolver = "2"
members = [
"./contracts/mock-oracle-contract",
"./contracts/oracle-contract",
"./contracts/stability-pool-contract",
"./contracts/trove-manager-contract",
"./contracts/vesting-contract",
Expand Down
4 changes: 3 additions & 1 deletion Forc.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
[workspace]
members = [
"./contracts/mock-oracle-contract",
"./contracts/mock-pyth-contract",
"./contracts/mock-redstone-contract",
"./contracts/oracle-contract",
"./contracts/multi-trove-getter-contract",
"./contracts/stability-pool-contract",
"./contracts/trove-manager-contract",
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ directory.
| [`sorted-troves`](contracts/sorted-troves-contract) | Manages location of troves in the Linked list format | $$\color{green}{90/100}$$ |
| Asset Specific Contracts |
| [`token`](contracts/token-contract) | FRC-20 to use in local tests made by Sway Gang | $$\color{green}{90/100}$$ |
| [`mock-oracle`](contracts/mock-oracle-contract) | Oracle for on-chain data | $$\color{orange}{60/100}$$ |
| [`oracle`](contracts/oracle-contract) | Oracle for on-chain data | $$\color{orange}{60/100}$$ |
| [`trove-manager`](contracts/trove-manager-contract) | Manages liquidations, redemptions, and user troves in the Linked list format | $$\color{orange}{85/100}$$ |
| FPT Contracts |
| [`FPT-vesting`](contracts/vesting-contract) | Manages $FPT vesting schedules | $$\color{green}{85/100}$$ |
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::oracle_interface::Oracle;
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
72 changes: 68 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,16 @@ 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},
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 +32,18 @@ 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;

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 +61,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 +201,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 +238,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 +283,18 @@ 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;

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 +315,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 +350,18 @@ 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;

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 +379,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 +428,18 @@ 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;

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 +463,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 +482,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 +523,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