Skip to content

Commit

Permalink
refactor: Create storage.rs for loan_manager
Browse files Browse the repository at this point in the history
  • Loading branch information
kovipu committed Feb 16, 2025
1 parent 55e8a60 commit 371a582
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 44 deletions.
29 changes: 15 additions & 14 deletions contracts/loan_manager/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use crate::error::LoanManagerError;
use crate::oracle::{self, Asset};
use crate::positions;
use crate::storage_types::{
Loan, LoansDataKey, POSITIONS_BUMP_AMOUNT, POSITIONS_LIFETIME_THRESHOLD,
use crate::storage::{
self, Loan, LoanManagerDataKey, POSITIONS_BUMP_AMOUNT, POSITIONS_LIFETIME_THRESHOLD,
};

use soroban_sdk::{
Expand All @@ -27,11 +26,13 @@ struct LoanManager;
impl LoanManager {
/// Set the admin that's allowed to upgrade the wasm.
pub fn initialize(e: Env, admin: Address) -> Result<(), LoanManagerError> {
if e.storage().persistent().has(&LoansDataKey::Admin) {
if e.storage().persistent().has(&LoanManagerDataKey::Admin) {
return Err(LoanManagerError::AlreadyInitialized);
}

e.storage().persistent().set(&LoansDataKey::Admin, &admin);
e.storage()
.persistent()
.set(&LoanManagerDataKey::Admin, &admin);
e.events()
.publish((symbol_short!("admin"), symbol_short!("added")), admin);
Ok(())
Expand All @@ -52,7 +53,7 @@ impl LoanManager {
.with_current_contract(salt)
.deploy_v2(wasm_hash, ());

let admin: Option<Address> = e.storage().persistent().get(&LoansDataKey::Admin);
let admin: Option<Address> = e.storage().persistent().get(&LoanManagerDataKey::Admin);

if let Some(admin) = admin {
admin.require_auth();
Expand All @@ -61,14 +62,14 @@ impl LoanManager {
let mut pool_addresses: Vec<Address> = e
.storage()
.persistent()
.get(&LoansDataKey::PoolAddresses)
.get(&LoanManagerDataKey::PoolAddresses)
.unwrap_or(vec![&e]);
pool_addresses.push_back(deployed_address.clone());
e.storage()
.persistent()
.set(&LoansDataKey::PoolAddresses, &pool_addresses);
.set(&LoanManagerDataKey::PoolAddresses, &pool_addresses);
e.events().publish(
(LoansDataKey::PoolAddresses, symbol_short!("added")),
(LoanManagerDataKey::PoolAddresses, symbol_short!("added")),
&deployed_address,
);

Expand Down Expand Up @@ -100,12 +101,12 @@ impl LoanManager {
let admin: Address = e
.storage()
.persistent()
.get(&LoansDataKey::Admin)
.get(&LoanManagerDataKey::Admin)
.ok_or(LoanManagerError::AdminNotFound)?;
admin.require_auth();
e.storage()
.persistent()
.get(&LoansDataKey::PoolAddresses)
.get(&LoanManagerDataKey::PoolAddresses)
.unwrap_or(vec![&e])
.iter()
.for_each(|pool| {
Expand All @@ -132,7 +133,7 @@ impl LoanManager {

if e.storage()
.persistent()
.has(&LoansDataKey::Loan(user.clone()))
.has(&LoanManagerDataKey::Loan(user.clone()))
{
return Err(LoanManagerError::LoanAlreadyExists);
}
Expand Down Expand Up @@ -178,7 +179,7 @@ impl LoanManager {
last_accrual: borrow_pool_client.get_accrual(),
};

positions::init_loan(&e, user.clone(), loan);
storage::write_loan(&e, user.clone(), loan);

Ok(())
}
Expand Down Expand Up @@ -313,7 +314,7 @@ impl LoanManager {
}

pub fn get_loan(e: &Env, addr: Address) -> Loan {
if let Some(loan) = positions::read_positions(e, addr) {
if let Some(loan) = storage::read_loan(e, addr) {
loan
} else {
panic!() // TODO: It should be panic_with_error or something and give out detailed error.
Expand Down
3 changes: 1 addition & 2 deletions contracts/loan_manager/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@
mod contract;
mod error;
mod oracle;
mod positions;
mod storage_types;
mod storage;
28 changes: 0 additions & 28 deletions contracts/loan_manager/src/positions.rs

This file was deleted.

52 changes: 52 additions & 0 deletions contracts/loan_manager/src/storage.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use soroban_sdk::{contracttype, Address, Env};

/* Ledger Thresholds */

pub(crate) const DAY_IN_LEDGERS: u32 = 17280; // if ledger takes 5 seconds

pub(crate) const POSITIONS_BUMP_AMOUNT: u32 = 30 * DAY_IN_LEDGERS;
pub(crate) const POSITIONS_LIFETIME_THRESHOLD: u32 = POSITIONS_BUMP_AMOUNT - DAY_IN_LEDGERS;

/* Storage Types */
#[derive(Clone)]
#[contracttype]
pub struct Loan {
pub borrower: Address,
pub borrowed_amount: i128,
pub borrowed_from: Address,
pub collateral_amount: i128,
pub collateral_from: Address,
pub health_factor: i128,
pub unpaid_interest: i128,
pub last_accrual: i128,
}

#[derive(Clone)]
#[contracttype]
pub enum LoanManagerDataKey {
Admin,
PoolAddresses,
// Users positions in the pool
Loan(Address),
LastUpdated,
}

pub fn write_loan(e: &Env, addr: Address, loan: Loan) {
let key = LoanManagerDataKey::Loan(addr);

e.storage().persistent().set(&key, &loan);

e.storage()
.persistent()
.extend_ttl(&key, POSITIONS_LIFETIME_THRESHOLD, POSITIONS_BUMP_AMOUNT);

e.events().publish(("Loan", "created"), key);
}

pub fn read_loan(e: &Env, addr: Address) -> Option<Loan> {
let key = LoanManagerDataKey::Loan(addr);

let value: Option<Loan> = e.storage().persistent().get(&key)?;

value
}

0 comments on commit 371a582

Please sign in to comment.