Skip to content

Commit

Permalink
Merge pull request #125 from Hydrogen-Labs/cleaning-code
Browse files Browse the repository at this point in the history
Cleaning code
  • Loading branch information
diyahir authored Sep 6, 2024
2 parents 91ef4d5 + 335ca7c commit d5d409f
Show file tree
Hide file tree
Showing 38 changed files with 423 additions and 286 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ target
*.DS_Store
*.ENV
*.lock
*.env
!*/.sample.env
*types/*
*/contracts.json
22 changes: 15 additions & 7 deletions contracts/active-pool-contract/src/main.sw
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
contract;

// The Active Pool holds the collateral assets and USDF debt (but not USDF tokens) for all active troves.
//
// When a trove is liquidated, its collateral assets and USDF debt are transferred from the Active Pool
// to either the Stability Pool, the Default Pool, or both, depending on the liquidation conditions.
//
// This contract supports multiple collateral assets, each managed separately.
use libraries::active_pool_interface::ActivePool;
use std::{
asset::transfer,
Expand All @@ -11,17 +16,17 @@ use std::{
msg_amount,
},
hash::Hash,
logging::log,
};

storage {
borrow_operations_contract: Identity = Identity::Address(Address::zero()),
stability_pool_contract: Identity = Identity::Address(Address::zero()),
default_pool_contract: ContractId = ContractId::zero(),
protocol_manager_contract: Identity = Identity::Address(Address::zero()),
asset_amount: StorageMap<AssetId, u64> = StorageMap::<AssetId, u64> {},
usdf_debt_amount: StorageMap<AssetId, u64> = StorageMap::<AssetId, u64> {},
valid_asset_ids: StorageMap<AssetId, bool> = StorageMap::<AssetId, bool> {},
valid_trove_managers: StorageMap<Identity, bool> = StorageMap::<Identity, bool> {},
asset_amount: StorageMap<AssetId, u64> = StorageMap::<AssetId, u64> {}, // Asset amount in the active pool
usdf_debt_amount: StorageMap<AssetId, u64> = StorageMap::<AssetId, u64> {}, // USDF debt in the active pool
valid_asset_ids: StorageMap<AssetId, bool> = StorageMap::<AssetId, bool> {}, // Valid asset ids
valid_trove_managers: StorageMap<Identity, bool> = StorageMap::<Identity, bool> {}, // Valid trove managers, one for each asset managed
is_initialized: bool = false,
}
impl ActivePool for Contract {
Expand All @@ -36,7 +41,7 @@ impl ActivePool for Contract {
storage
.is_initialized
.read() == false,
"Already initialized",
"Active Pool: Already initialized",
);
storage.borrow_operations_contract.write(borrow_operations);
storage.stability_pool_contract.write(stability_pool);
Expand Down Expand Up @@ -75,18 +80,21 @@ impl ActivePool for Contract {
storage.asset_amount.insert(asset_id, new_amount);
transfer(address, asset_id, amount);
}
// Increase the USDF debt for a given asset
#[storage(read, write)]
fn increase_usdf_debt(amount: u64, asset_id: AssetId) {
require_caller_is_bo_or_tm();
let new_debt = storage.usdf_debt_amount.get(asset_id).try_read().unwrap_or(0) + amount;
storage.usdf_debt_amount.insert(asset_id, new_debt);
}
// Decrease the USDF debt for a given asset
#[storage(read, write)]
fn decrease_usdf_debt(amount: u64, asset_id: AssetId) {
require_caller_is_bo_or_tm_or_sp_or_pm();
let new_debt = storage.usdf_debt_amount.get(asset_id).read() - amount;
storage.usdf_debt_amount.insert(asset_id, new_debt);
}
// Send the collateral asset to the Default Pool
#[storage(read, write)]
fn send_asset_to_default_pool(amount: u64, asset_id: AssetId) {
require_caller_is_bo_or_tm_or_sp_or_pm();
Expand Down
10 changes: 5 additions & 5 deletions contracts/borrow-operations-contract/src/data_structures.sw
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
library;

pub struct AssetContracts {
pub trove_manager: ContractId,
pub oracle: ContractId,
}

pub struct LocalVariables_OpenTrove {
pub price: u64,
pub usdf_fee: u64,
Expand All @@ -11,11 +16,6 @@ pub struct LocalVariables_OpenTrove {
pub array_index: u64,
}

pub struct AssetContracts {
pub trove_manager: ContractId,
pub oracle: ContractId,
}

impl LocalVariables_OpenTrove {
pub fn new() -> Self {
LocalVariables_OpenTrove {
Expand Down
21 changes: 20 additions & 1 deletion contracts/borrow-operations-contract/src/main.sw
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
contract;
// This contract, BorrowOperations, manages borrowing operations for the USDF stablecoin system.
// It handles opening and adjusting troves, which are collateralized debt positions.
// The contract interacts with various other components of the system
//
// Key functionalities include:
// - Opening new troves
// - Adjusting existing troves (adding/removing collateral, borrowing/repaying USDF)
// - Closing troves
// - Managing multiple collateral asset types
// - Enforcing system parameters and stability conditions

mod data_structures;

use ::data_structures::{AssetContracts, LocalVariables_AdjustTrove, LocalVariables_OpenTrove};

use libraries::trove_manager_interface::data_structures::Status;
use libraries::active_pool_interface::ActivePool;
use libraries::token_interface::Token;
Expand Down Expand Up @@ -85,6 +94,7 @@ impl BorrowOperations for Contract {
.insert(asset_contract, asset_contracts);
}
// --- Borrower Trove Operations ---
// Open a new trove by borrowing USDF
#[storage(read, write), payable]
fn open_trove(usdf_amount: u64, upper_hint: Identity, lower_hint: Identity) {
require_valid_asset_id();
Expand Down Expand Up @@ -127,6 +137,7 @@ impl BorrowOperations for Contract {
asset_contract,
);
}
// Add collateral to an existing trove
#[storage(read, write), payable]
fn add_coll(upper_hint: Identity, lower_hint: Identity) {
require_valid_asset_id();
Expand All @@ -142,6 +153,7 @@ impl BorrowOperations for Contract {
msg_asset_id(),
);
}
// Withdraw collateral from an existing trove
#[storage(read, write)]
fn withdraw_coll(
amount: u64,
Expand All @@ -161,6 +173,7 @@ impl BorrowOperations for Contract {
asset_contract,
);
}
// Withdraw USDF from an existing trove
#[storage(read, write)]
fn withdraw_usdf(
amount: u64,
Expand All @@ -180,6 +193,7 @@ impl BorrowOperations for Contract {
asset_contract,
);
}
// Repay USDF for an existing trove
#[storage(read, write), payable]
fn repay_usdf(
upper_hint: Identity,
Expand All @@ -199,6 +213,7 @@ impl BorrowOperations for Contract {
asset_contract,
);
}
// Close an existing trove
#[storage(read, write), payable]
fn close_trove(asset_contract: AssetId) {
let asset_contracts_cache = storage.asset_contracts.get(asset_contract).read();
Expand Down Expand Up @@ -233,6 +248,7 @@ impl BorrowOperations for Contract {
transfer(borrower, storage.usdf_asset_id.read(), excess_usdf_returned);
}
}
// Claim collateral from liquidations
#[storage(read)]
fn claim_collateral(asset: AssetId) {
let coll_surplus = abi(CollSurplusPool, storage.coll_surplus_pool_contract.read().bits());
Expand All @@ -243,6 +259,9 @@ impl BorrowOperations for Contract {
return storage.usdf_asset_id.read();
}
}

// --- Internal Functions ---

#[storage(read)]
fn internal_trigger_borrowing_fee(
usdf_amount: u64,
Expand Down
48 changes: 24 additions & 24 deletions contracts/borrow-operations-contract/tests/failure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ async fn fails_open_two_troves_of_same_coll_type() {
&contracts.active_pool,
col_amount,
debt_amount,
Identity::Address([0; 32].into()),
Identity::Address([0; 32].into()),
Identity::Address(Address::zeroed()),
Identity::Address(Address::zeroed()),
)
.await
.unwrap();
Expand All @@ -71,8 +71,8 @@ async fn fails_open_two_troves_of_same_coll_type() {
&contracts.active_pool,
col_amount,
debt_amount,
Identity::Address([0; 32].into()),
Identity::Address([0; 32].into()),
Identity::Address(Address::zeroed()),
Identity::Address(Address::zeroed()),
)
.await
.is_err();
Expand Down Expand Up @@ -211,8 +211,8 @@ async fn fails_open_trove_under_minimum_collateral_ratio() {
&contracts.active_pool,
coll_amount,
debt_amount,
Identity::Address([0; 32].into()),
Identity::Address([0; 32].into()),
Identity::Address(Address::zeroed()),
Identity::Address(Address::zeroed()),
)
.await
.is_err();
Expand Down Expand Up @@ -257,8 +257,8 @@ async fn fails_open_trove_under_min_usdf_required() {
&contracts.active_pool,
coll_amount,
debt_amount,
Identity::Address([0; 32].into()),
Identity::Address([0; 32].into()),
Identity::Address(Address::zeroed()),
Identity::Address(Address::zeroed()),
)
.await
.is_err();
Expand Down Expand Up @@ -303,8 +303,8 @@ async fn fails_reduce_debt_under_min_usdf_required() {
&contracts.active_pool,
coll_amount,
debt_amount,
Identity::Address([0; 32].into()),
Identity::Address([0; 32].into()),
Identity::Address(Address::zeroed()),
Identity::Address(Address::zeroed()),
)
.await
.unwrap();
Expand All @@ -323,8 +323,8 @@ async fn fails_reduce_debt_under_min_usdf_required() {
&contracts.asset_contracts[0].trove_manager,
&contracts.active_pool,
300 * PRECISION,
Identity::Address([0; 32].into()),
Identity::Address([0; 32].into()),
Identity::Address(Address::zeroed()),
Identity::Address(Address::zeroed()),
)
.await
.is_err();
Expand Down Expand Up @@ -370,8 +370,8 @@ async fn fails_decrease_collateral_under_mcr() {
&contracts.active_pool,
coll_amount,
debt_amount,
Identity::Address([0; 32].into()),
Identity::Address([0; 32].into()),
Identity::Address(Address::zeroed()),
Identity::Address(Address::zeroed()),
)
.await
.unwrap();
Expand All @@ -386,8 +386,8 @@ async fn fails_decrease_collateral_under_mcr() {
&contracts.asset_contracts[0].trove_manager,
&contracts.active_pool,
600 * PRECISION,
Identity::Address([0; 32].into()),
Identity::Address([0; 32].into()),
Identity::Address(Address::zeroed()),
Identity::Address(Address::zeroed()),
)
.await
.is_err();
Expand Down Expand Up @@ -448,8 +448,8 @@ async fn fails_incorrect_token_as_collateral_or_repayment() {
&contracts.active_pool,
1_200 * PRECISION,
600 * PRECISION,
Identity::Address([0; 32].into()),
Identity::Address([0; 32].into()),
Identity::Address(Address::zeroed()),
Identity::Address(Address::zeroed()),
)
.await
.is_err();
Expand All @@ -473,8 +473,8 @@ async fn fails_incorrect_token_as_collateral_or_repayment() {
&contracts.active_pool,
1_200 * PRECISION,
600 * PRECISION,
Identity::Address([0; 32].into()),
Identity::Address([0; 32].into()),
Identity::Address(Address::zeroed()),
Identity::Address(Address::zeroed()),
)
.await
.unwrap();
Expand All @@ -490,8 +490,8 @@ async fn fails_incorrect_token_as_collateral_or_repayment() {
&contracts.asset_contracts[0].trove_manager,
&contracts.active_pool,
1 * PRECISION,
Identity::Address([0; 32].into()),
Identity::Address([0; 32].into()),
Identity::Address(Address::zeroed()),
Identity::Address(Address::zeroed()),
)
.await
.is_err();
Expand Down Expand Up @@ -531,8 +531,8 @@ async fn fails_incorrect_token_as_collateral_or_repayment() {
&contracts.asset_contracts[0].trove_manager,
&contracts.active_pool,
1 * PRECISION,
Identity::Address([0; 32].into()),
Identity::Address([0; 32].into()),
Identity::Address(Address::zeroed()),
Identity::Address(Address::zeroed()),
)
.await
.is_err();
Expand Down
Loading

0 comments on commit d5d409f

Please sign in to comment.