Skip to content

Commit

Permalink
Merge pull request #140 from Hydrogen-Labs/fixes-recommended-changes-…
Browse files Browse the repository at this point in the history
…first-pass

Fixes recommended changes first pass
  • Loading branch information
diyahir authored Oct 22, 2024
2 parents 54cac4d + 7e8cb03 commit 01a9593
Show file tree
Hide file tree
Showing 79 changed files with 2,533 additions and 898 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ format: ## Format the code
-------Deployer Scripts-------:

deploy: ## Run the deployment script for core contracts
@cd deploy-scripts && RPC=$(RPC) SECRET=$(SECRET) cargo run deploy
@forc build && cd deploy-scripts && RPC=$(RPC) SECRET=$(SECRET) cargo run deploy

add-asset: ## Run the script to add assets to the protocol
@cd deploy-scripts && RPC=$(RPC) SECRET=$(SECRET) cargo run add-asset
Expand Down
22 changes: 17 additions & 5 deletions contracts/active-pool-contract/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ use std::{
hash::Hash,
};

configurable {
/// Initializer identity
INITIALIZER: Identity = Identity::Address(Address::zero()),
}

storage {
borrow_operations_contract: Identity = Identity::Address(Address::zero()),
stability_pool_contract: Identity = Identity::Address(Address::zero()),
Expand All @@ -37,6 +42,11 @@ impl ActivePool for Contract {
default_pool: ContractId,
protocol_manager: Identity,
) {
require(
msg_sender()
.unwrap() == INITIALIZER,
"Active Pool: Caller is not initializer",
);
require(
storage
.is_initialized
Expand Down Expand Up @@ -68,9 +78,11 @@ impl ActivePool for Contract {
#[storage(read, write)]
fn send_asset(address: Identity, amount: u64, asset_id: AssetId) {
require_caller_is_bo_or_tm_or_sp_or_pm();
let new_amount = storage.asset_amount.get(asset_id).read() - amount;
storage.asset_amount.insert(asset_id, new_amount);
transfer(address, asset_id, amount);
if amount > 0 {
let new_amount = storage.asset_amount.get(asset_id).read() - amount;
storage.asset_amount.insert(asset_id, new_amount);
transfer(address, asset_id, amount);
}
}
// Increase the USDF debt for a given asset
#[storage(read, write)]
Expand Down Expand Up @@ -105,7 +117,7 @@ impl ActivePool for Contract {
fn recieve() {
require_caller_is_borrow_operations_or_default_pool();
let asset_id = msg_asset_id();
require_is_asset_id(asset_id);
require_is_valid_asset_id(asset_id);
let new_amount = storage.asset_amount.get(asset_id).try_read().unwrap_or(0) + msg_amount();
storage.asset_amount.insert(asset_id, new_amount);
}
Expand All @@ -121,7 +133,7 @@ impl ActivePool for Contract {
}
// --- Helper functions ---
#[storage(read)]
fn require_is_asset_id(asset_id: AssetId) {
fn require_is_valid_asset_id(asset_id: AssetId) {
let valid_asset_id = storage.valid_asset_ids.get(asset_id).try_read().unwrap_or(false);
require(valid_asset_id, "Active Pool: Asset ID is not correct");
}
Expand Down
1 change: 1 addition & 0 deletions contracts/borrow-operations-contract/Forc.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ name = "borrow-operations-contract"

[dependencies]
libraries = { path = "../../libraries" }
standards = { git = "https://github.com/FuelLabs/sway-standards", tag = "v0.6.1" }
12 changes: 6 additions & 6 deletions contracts/borrow-operations-contract/src/data_structures.sw
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub struct AssetContracts {
pub oracle: ContractId,
}

pub struct LocalVariables_OpenTrove {
pub struct LocalVariablesOpenTrove {
pub price: u64,
pub usdf_fee: u64,
pub net_debt: u64,
Expand All @@ -15,9 +15,9 @@ pub struct LocalVariables_OpenTrove {
pub array_index: u64,
}

impl LocalVariables_OpenTrove {
impl LocalVariablesOpenTrove {
pub fn new() -> Self {
LocalVariables_OpenTrove {
LocalVariablesOpenTrove {
price: 0,
usdf_fee: 0,
net_debt: 0,
Expand All @@ -29,7 +29,7 @@ impl LocalVariables_OpenTrove {
}
}

pub struct LocalVariables_AdjustTrove {
pub struct LocalVariablesAdjustTrove {
pub price: u64,
pub coll_change: u64,
pub net_debt_change: u64,
Expand All @@ -45,9 +45,9 @@ pub struct LocalVariables_AdjustTrove {
pub stake: u64,
}

impl LocalVariables_AdjustTrove {
impl LocalVariablesAdjustTrove {
pub fn new() -> Self {
LocalVariables_AdjustTrove {
LocalVariablesAdjustTrove {
price: 0,
coll_change: 0,
net_debt_change: 0,
Expand Down
48 changes: 30 additions & 18 deletions contracts/borrow-operations-contract/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ contract;

mod data_structures;

use ::data_structures::{AssetContracts, LocalVariables_AdjustTrove, LocalVariables_OpenTrove};
use standards::{src3::SRC3,};
use ::data_structures::{AssetContracts, LocalVariablesAdjustTrove, LocalVariablesOpenTrove};
use libraries::trove_manager_interface::data_structures::Status;
use libraries::active_pool_interface::ActivePool;
use libraries::token_interface::Token;
use libraries::usdf_token_interface::USDFToken;
use libraries::trove_manager_interface::TroveManager;
use libraries::sorted_troves_interface::SortedTroves;
use libraries::fpt_staking_interface::FPTStaking;
Expand All @@ -35,6 +35,12 @@ use std::{
},
hash::*,
};

configurable {
/// Initializer identity
INITIALIZER: Identity = Identity::Address(Address::zero()),
}

storage {
asset_contracts: StorageMap<AssetId, AssetContracts> = StorageMap::<AssetId, AssetContracts> {},
valid_asset_ids: StorageMap<AssetId, bool> = StorageMap::<AssetId, bool> {},
Expand All @@ -61,6 +67,11 @@ impl BorrowOperations for Contract {
active_pool_contract: ContractId,
sorted_troves_contract: ContractId,
) {
require(
msg_sender()
.unwrap() == INITIALIZER,
"Borrow Operations: Caller is not initializer",
);
require(
!storage
.is_initialized
Expand All @@ -77,7 +88,7 @@ impl BorrowOperations for Contract {
storage.sorted_troves_contract.write(sorted_troves_contract);
storage
.usdf_asset_id
.write(get_default_asset_id(usdf_contract));
.write(AssetId::new(usdf_contract, SubId::zero()));
storage.pauser.write(msg_sender().unwrap());
storage.is_initialized.write(true);
}
Expand All @@ -98,7 +109,7 @@ impl BorrowOperations for Contract {
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();
let mut vars = LocalVariablesOpenTrove::new();
let sender = msg_sender().unwrap();
vars.net_debt = usdf_amount;
vars.price = oracle.get_price();
Expand Down Expand Up @@ -172,7 +183,6 @@ impl BorrowOperations for Contract {
lower_hint: Identity,
asset_contract: AssetId,
) {
require_is_not_paused();
internal_adjust_trove(
msg_sender()
.unwrap(),
Expand Down Expand Up @@ -250,6 +260,7 @@ impl BorrowOperations for Contract {
);
active_pool.send_asset(borrower, coll, asset_contract);
if (debt < msg_amount()) {
require_valid_usdf_id(msg_asset_id());
let excess_usdf_returned = msg_amount() - debt;
transfer(borrower, usdf_asset_id, excess_usdf_returned);
}
Expand Down Expand Up @@ -306,14 +317,18 @@ fn internal_trigger_borrowing_fee(
usdf_contract: ContractId,
fpt_staking_contract: ContractId,
) -> u64 {
let usdf = abi(USDFToken, usdf_contract.bits());
let usdf = abi(SRC3, usdf_contract.bits());
let fpt_staking = abi(FPTStaking, fpt_staking_contract.bits());
let usdf_fee = fm_compute_borrow_fee(usdf_amount);

//increase fpt staking rewards
fpt_staking.increase_f_usdf(usdf_fee);
// Mint usdf to fpt staking contract
usdf.mint(usdf_fee, Identity::ContractId(fpt_staking_contract));
usdf.mint(
Identity::ContractId(fpt_staking_contract),
Some(SubId::zero()),
usdf_fee,
);

return usdf_fee
}
Expand Down Expand Up @@ -346,7 +361,7 @@ fn internal_adjust_trove(
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();
let mut vars = LocalVariables_AdjustTrove::new();
let mut vars = LocalVariablesAdjustTrove::new();
if is_debt_increase {
require_is_not_paused();
require_non_zero_debt_change(usdf_change);
Expand Down Expand Up @@ -387,10 +402,6 @@ fn internal_adjust_trove(

if !is_debt_increase && usdf_change > 0 {
require_at_least_min_net_debt(vars.debt - vars.net_debt_change);
require(
msg_amount() >= vars.net_debt_change,
"Borrow Operations: caller does not have enough balance to repay debt",
);
}

let new_position_res = internal_update_trove_from_adjustment(
Expand Down Expand Up @@ -479,7 +490,7 @@ fn require_non_zero_adjustment(asset_amount: u64, coll_withdrawl: u64, usdf_chan
}
fn require_at_least_min_net_debt(_net_debt: u64) {
require(
_net_debt > MIN_NET_DEBT,
_net_debt >= MIN_NET_DEBT,
"Borrow Operations: net debt must be greater than 0",
);
}
Expand All @@ -491,7 +502,7 @@ fn require_non_zero_debt_change(debt_change: u64) {
}
fn require_at_least_mcr(icr: u64) {
require(
icr > MCR,
icr >= MCR,
"Borrow Operations: Minimum collateral ratio not met",
);
}
Expand Down Expand Up @@ -529,9 +540,9 @@ fn internal_withdraw_usdf(
asset_contract: AssetId,
) {
let active_pool = abi(ActivePool, active_pool_contract.bits());
let usdf = abi(USDFToken, usdf_contract.bits());
let usdf = abi(SRC3, usdf_contract.bits());
active_pool.increase_usdf_debt(net_debt_increase, asset_contract);
usdf.mint(amount, recipient);
usdf.mint(recipient, Some(SubId::zero()), amount);
}
fn internal_get_coll_change(coll_recieved: u64, requested_coll_withdrawn: u64) -> (u64, bool) {
if (coll_recieved != 0) {
Expand Down Expand Up @@ -638,12 +649,13 @@ fn internal_repay_usdf(
asset_contract: AssetId,
) {
let active_pool = abi(ActivePool, active_pool_contract.bits());
let usdf = abi(USDFToken, usdf_contract.bits());
let usdf = abi(SRC3, usdf_contract.bits());
usdf
.burn {
coins: usdf_amount,
asset_id: storage.usdf_asset_id.read().bits(),
}();
}(SubId::zero(), usdf_amount);

active_pool.decrease_usdf_debt(usdf_amount, asset_contract);
}
#[storage(read)]
Expand Down
2 changes: 2 additions & 0 deletions contracts/borrow-operations-contract/tests/failure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ async fn fails_reduce_debt_under_min_usdf_required() {
&contracts.sorted_troves,
&contracts.asset_contracts[0].trove_manager,
&contracts.active_pool,
&contracts.default_pool,
300 * PRECISION,
Identity::Address(Address::zeroed()),
Identity::Address(Address::zeroed()),
Expand Down Expand Up @@ -541,6 +542,7 @@ async fn fails_incorrect_token_as_collateral_or_repayment() {
&contracts.sorted_troves,
&contracts.asset_contracts[0].trove_manager,
&contracts.active_pool,
&contracts.default_pool,
1 * PRECISION,
Identity::Address(Address::zeroed()),
Identity::Address(Address::zeroed()),
Expand Down
1 change: 1 addition & 0 deletions contracts/borrow-operations-contract/tests/pausing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ async fn test_paused_operations() {
&contracts.sorted_troves,
&contracts.asset_contracts[0].trove_manager,
&contracts.active_pool,
&contracts.default_pool,
repay_amount,
Identity::Address(Address::zeroed()),
Identity::Address(Address::zeroed()),
Expand Down
1 change: 1 addition & 0 deletions contracts/borrow-operations-contract/tests/success.rs
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,7 @@ async fn proper_decrease_debt() {
&contracts.sorted_troves,
&contracts.asset_contracts[0].trove_manager,
&contracts.active_pool,
&contracts.default_pool,
repay_amount,
Identity::Address(Address::zeroed()),
Identity::Address(Address::zeroed()),
Expand Down
10 changes: 10 additions & 0 deletions contracts/coll-surplus-pool-contract/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ use std::{
},
hash::Hash,
};
configurable {
/// Initializer identity
INITIALIZER: Identity = Identity::Address(Address::zero()),
}

storage {
protocol_manager: Identity = Identity::Address(Address::zero()),
borrow_operations_contract: ContractId = ContractId::zero(),
Expand All @@ -34,6 +39,11 @@ impl CollSurplusPool for Contract {
borrow_operations_contract: ContractId,
protocol_manager: Identity,
) {
require(
msg_sender()
.unwrap() == INITIALIZER,
"CollSurplusPool: Caller is not initializer",
);
require(
storage
.is_initialized
Expand Down
5 changes: 3 additions & 2 deletions contracts/coll-surplus-pool-contract/tests/harness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use test_utils::{
coll_surplus_pool::{coll_surplus_pool_abi, CollSurplusPool},
oracle::oracle_abi,
pyth_oracle::{
pyth_oracle_abi, pyth_price_feed, pyth_price_feed_with_time, PYTH_TIMESTAMP,
pyth_oracle_abi, pyth_price_feed, pyth_price_feed_with_time, PYTH_PRECISION,
PYTH_TIMESTAMP,
},
token::token_abi,
trove_manager::trove_manager_abi,
Expand Down Expand Up @@ -95,7 +96,7 @@ async fn test_collateral_surplus_workflow_after_liquidation() {
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_with_time(1, PYTH_TIMESTAMP + 1),
pyth_price_feed_with_time(1, PYTH_TIMESTAMP + 1, PYTH_PRECISION.into()),
)
.await;

Expand Down
2 changes: 2 additions & 0 deletions contracts/community-issuance-contract/Forc.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ name = "community-issuance-contract"

[dependencies]
libraries = { path = "../../libraries" }
standards = { git = "https://github.com/FuelLabs/sway-standards", tag = "v0.6.1" }
sway_libs = { git = "https://github.com/FuelLabs/sway-libs", tag = "v0.23.1" }
Loading

0 comments on commit 01a9593

Please sign in to comment.