Skip to content

Commit

Permalink
chore: clean up some storage reads that used try_from_val()
Browse files Browse the repository at this point in the history
  • Loading branch information
Teolhyn committed Sep 5, 2024
1 parent b52d6a2 commit 3381a88
Showing 1 changed file with 3 additions and 16 deletions.
19 changes: 3 additions & 16 deletions contracts/loan_manager/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ use crate::storage_types::{
Loan, LoansDataKey, DAY_IN_LEDGERS, POSITIONS_BUMP_AMOUNT, POSITIONS_LIFETIME_THRESHOLD,
};

use soroban_sdk::{
contract, contractimpl, vec, Address, Env, IntoVal, String, Symbol, TryFromVal, Val, Vec,
};
use soroban_sdk::{contract, contractimpl, vec, Address, Env, IntoVal, String, Symbol, Val, Vec};

mod loan_pool {
soroban_sdk::contractimport!(
Expand Down Expand Up @@ -124,20 +122,16 @@ impl LoansTrait for LoansContract {
let current_ledger = e.ledger().sequence();

let key: LoansDataKey = LoansDataKey::LastUpdated;
let previous_ledger_val: Val = e
let previous_ledger: u32 = e
.storage()
.persistent()
.get(&key)
.unwrap_or(current_ledger.into_val(&e)); // If there is no previous ledger, use current.
let previous_ledger: u32 =
u32::try_from_val(&e, &previous_ledger_val).expect("Failed to convert Val to u32");

let ledgers_since_update: u32 = current_ledger - previous_ledger; // Currently unused but is a placeholder for interest calculations. Now time is handled.
let ledger_ratio: i128 =
(i128::from(ledgers_since_update) * DECIMAL) / (i128::from(DAY_IN_LEDGERS * 365));

// Update current ledger as the new 'last time'

// Iterate over loans and add interest to capital borrowed.
// In the same iteration add the amount to the liabilities of the lending pool.
// First, lets retrieve the list of addresses with loans
Expand All @@ -149,14 +143,7 @@ impl LoansTrait for LoansContract {
for user in addresses.iter() {
let key = (Symbol::new(&e, "Loan"), user.clone());

let loan_val: Val = e
.storage()
.persistent()
.get::<(Symbol, Address), Val>(&key)
.unwrap();

let mut loan: Loan =
Loan::try_from_val(&e, &loan_val).expect("Failed to convert Val to Loan");
let mut loan: Loan = e.storage().persistent().get(&key).unwrap();

let borrowed: i128 = loan.borrowed_amount;

Expand Down

0 comments on commit 3381a88

Please sign in to comment.