Skip to content

Commit

Permalink
cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmytro-Shashkevych committed Nov 15, 2023
1 parent a501cb2 commit 0bf0f19
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 100 deletions.
104 changes: 52 additions & 52 deletions contracts/lending/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,24 +337,24 @@ impl LendingContract {
user_borrow_amount_with_interest as i128,
borrowed_token_decimals,
)
.mul(Decimal::from_i128_with_scale(
user_borrowing_info.average_interest_rate as i128,
INTEREST_RATE_DECIMALS,
))
.add(
Decimal::from_i128_with_scale(amount as i128, borrowed_token_decimals).mul(
Decimal::from_i128_with_scale(
current_interest_rate as i128,
INTEREST_RATE_DECIMALS,
),
.mul(Decimal::from_i128_with_scale(
user_borrowing_info.average_interest_rate as i128,
INTEREST_RATE_DECIMALS,
))
.add(
Decimal::from_i128_with_scale(amount as i128, borrowed_token_decimals).mul(
Decimal::from_i128_with_scale(
current_interest_rate as i128,
INTEREST_RATE_DECIMALS,
),
))
.div(Decimal::from_i128_with_scale(
new_user_borrow_amount as i128,
borrowed_token_decimals,
))
.to_u128_with_decimals(INTEREST_RATE_DECIMALS)
.unwrap();
),
))
.div(Decimal::from_i128_with_scale(
new_user_borrow_amount as i128,
borrowed_token_decimals,
))
.to_u128_with_decimals(INTEREST_RATE_DECIMALS)
.unwrap();

// updating user borrowing info
let new_user_borrowing_info: UserBorrowingInfo = UserBorrowingInfo {
Expand All @@ -368,19 +368,19 @@ impl LendingContract {
let expected_annual_interest_income: u128 = total_borrow_data
.expected_annual_interest_income
- Decimal::from_i128_with_scale(
user_borrowing_info.borrowed_amount as i128,
borrowed_token_decimals,
)
user_borrowing_info.borrowed_amount as i128,
borrowed_token_decimals,
)
.mul(Decimal::from_i128_with_scale(
(user_borrowing_info.average_interest_rate / HUNDRED) as i128,
INTEREST_RATE_DECIMALS,
))
.to_u128_with_decimals(INTEREST_RATE_DECIMALS)
.unwrap()
+ Decimal::from_i128_with_scale(
new_user_borrow_amount as i128,
borrowed_token_decimals,
)
new_user_borrow_amount as i128,
borrowed_token_decimals,
)
.mul(Decimal::from_i128_with_scale(
(average_interest_rate / HUNDRED) as i128,
INTEREST_RATE_DECIMALS,
Expand All @@ -394,9 +394,9 @@ impl LendingContract {

let total_average_interest_rate = HUNDRED
* Decimal::from_i128_with_scale(
expected_annual_interest_income as i128,
INTEREST_RATE_DECIMALS,
)
expected_annual_interest_income as i128,
INTEREST_RATE_DECIMALS,
)
.div(Decimal::from_i128_with_scale(
total_borrowed_amount as i128,
borrowed_token_decimals,
Expand Down Expand Up @@ -563,22 +563,22 @@ impl LendingContract {

let expected_annual_interest_income = total_borrow_data.expected_annual_interest_income
+ Decimal::from_i128_with_scale(
(user_borrow_amount_with_interest - user_borrowing_info.borrowed_amount) as i128,
repay_token_decimals,
)
(user_borrow_amount_with_interest - user_borrowing_info.borrowed_amount) as i128,
repay_token_decimals,
)
.mul(Decimal::from_i128_with_scale(
(user_borrowing_info.average_interest_rate / HUNDRED) as i128,
INTEREST_RATE_DECIMALS,
))
.to_u128_with_decimals(INTEREST_RATE_DECIMALS)
.unwrap()
- Decimal::from_i128_with_scale((repay_amount) as i128, repay_token_decimals)
.mul(Decimal::from_i128_with_scale(
(user_borrowing_info.average_interest_rate / HUNDRED) as i128,
INTEREST_RATE_DECIMALS,
))
.to_u128_with_decimals(INTEREST_RATE_DECIMALS)
.unwrap();
.mul(Decimal::from_i128_with_scale(
(user_borrowing_info.average_interest_rate / HUNDRED) as i128,
INTEREST_RATE_DECIMALS,
))
.to_u128_with_decimals(INTEREST_RATE_DECIMALS)
.unwrap();

let total_borrowed_amount: u128 = total_borrow_data.total_borrowed_amount
+ user_borrow_amount_with_interest
Expand All @@ -589,9 +589,9 @@ impl LendingContract {
if total_borrowed_amount != 0u128 {
total_average_interest_rate = HUNDRED
* Decimal::from_i128_with_scale(
expected_annual_interest_income as i128,
INTEREST_RATE_DECIMALS,
)
expected_annual_interest_income as i128,
INTEREST_RATE_DECIMALS,
)
.div(Decimal::from_i128_with_scale(
total_borrowed_amount as i128,
repay_token_decimals,
Expand Down Expand Up @@ -720,9 +720,9 @@ impl LendingContract {
let expected_annual_interest_income = total_borrow_data
.expected_annual_interest_income
- Decimal::from_i128_with_scale(
(user_borrowing_info.borrowed_amount) as i128,
token_decimals,
)
(user_borrowing_info.borrowed_amount) as i128,
token_decimals,
)
.mul(Decimal::from_i128_with_scale(
(user_borrowing_info.average_interest_rate / HUNDRED) as i128,
INTEREST_RATE_DECIMALS,
Expand All @@ -737,9 +737,9 @@ impl LendingContract {
if total_borrowed_amount != 0u128 {
total_average_interest_rate = HUNDRED
* Decimal::from_i128_with_scale(
expected_annual_interest_income as i128,
INTEREST_RATE_DECIMALS,
)
expected_annual_interest_income as i128,
INTEREST_RATE_DECIMALS,
)
.div(Decimal::from_i128_with_scale(
total_borrowed_amount as i128,
token_decimals,
Expand Down Expand Up @@ -795,12 +795,12 @@ impl LendingContract {
new_liquidator_token_balance as i128,
token_decimals,
)
.div(Decimal::from_i128_with_scale(
mm_token_price as i128,
token_decimals,
))
.to_u128_with_decimals(token_decimals)
.unwrap();
.div(Decimal::from_i128_with_scale(
mm_token_price as i128,
token_decimals,
))
.to_u128_with_decimals(token_decimals)
.unwrap();

let mut liquidator_mm_balance_map: Map<Symbol, u128> = env
.storage()
Expand Down Expand Up @@ -922,7 +922,7 @@ impl LendingContract {
);
}

pub fn set_token_interest_rate_params(
pub fn set_token_interest_rate_params(
env: Env,
denom: Symbol,
min_interest_rate: u128,
Expand Down Expand Up @@ -1007,5 +1007,5 @@ impl LendingContract {
// }
}

mod storage;
mod test;
mod storage;
49 changes: 22 additions & 27 deletions contracts/lending/src/storage.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
#![allow(dead_code)]

use core::ops::{Mul, Div};
use rust_decimal::{Decimal, MathematicalOps};
use core::ops::{Div, Mul};
use rust_decimal::prelude::ToPrimitive;
use rust_decimal::{Decimal, MathematicalOps};

use soroban_sdk::{
contracttype, map, symbol_short, token, Address, Env, Map, Symbol, Vec,
};
use soroban_sdk::{contracttype, map, symbol_short, token, Address, Env, Map, Symbol, Vec};

pub(crate) const DAY_IN_LEDGERS: u32 = 17280;
pub(crate) const WEEK_BUMP_AMOUNT: u32 = 7 * DAY_IN_LEDGERS;
Expand All @@ -25,7 +23,6 @@ pub(crate) const YEAR_IN_SECONDS: u128 = 31536000; // 365 days

pub(crate) const USD_DECIMALS: u32 = 8;


pub trait DecimalExt {
fn to_u128_with_decimals(&self, decimals: u32) -> Result<u128, rust_decimal::Error>;
}
Expand Down Expand Up @@ -56,7 +53,6 @@ impl DecimalExt for Decimal {
}
}


#[contracttype]
#[derive(Clone)]
pub enum DataKey {
Expand All @@ -82,7 +78,7 @@ pub enum DataKey {
// Map price for denom
UserDepositAsCollateral(Address),
// Map of bool per denom
UserBorrowingInfo(Address), // Map UserBorrowingInfo per denom
UserBorrowingInfo(Address), // Map UserBorrowingInfo per denom
}

#[contracttype]
Expand Down Expand Up @@ -150,7 +146,6 @@ pub struct TokenInterestRateModelParams {
pub optimal_utilization_ratio: u128,
}


pub fn has_admin(e: &Env) -> bool {
let key = DataKey::Admin;
e.storage().persistent().has(&key)
Expand Down Expand Up @@ -252,11 +247,11 @@ pub fn get_interest_rate(env: Env, denom: Symbol) -> u128 {
if utilization_rate <= optimal_utilization_ratio {
min_interest_rate
+ utilization_rate * (safe_borrow_max_rate - min_interest_rate)
/ optimal_utilization_ratio
/ optimal_utilization_ratio
} else {
safe_borrow_max_rate
+ rate_growth_factor * (utilization_rate - optimal_utilization_ratio)
/ (HUNDRED_PERCENT - optimal_utilization_ratio)
/ (HUNDRED_PERCENT - optimal_utilization_ratio)
}
}

Expand Down Expand Up @@ -460,13 +455,13 @@ pub fn get_liquidity_rate(env: Env, denom: Symbol) -> u128 {
expected_annual_interest_income as i128,
INTEREST_RATE_DECIMALS,
)
.mul(Decimal::from_i128_with_scale(HUNDRED as i128, 0u32))
.div(Decimal::from_i128_with_scale(
reserves_by_token as i128,
token_decimals,
))
.to_u128_with_decimals(INTEREST_RATE_DECIMALS)
.unwrap();
.mul(Decimal::from_i128_with_scale(HUNDRED as i128, 0u32))
.div(Decimal::from_i128_with_scale(
reserves_by_token as i128,
token_decimals,
))
.to_u128_with_decimals(INTEREST_RATE_DECIMALS)
.unwrap();

liquidity_rate
}
Expand All @@ -490,9 +485,9 @@ pub fn get_current_liquidity_index_ln(env: Env, denom: Symbol) -> u128 {
.checked_sub(liquidity_index_last_update)
.unwrap_or_default()) as u128
* Decimal::from_i128_with_scale(
(liquidity_rate / HUNDRED + INTEREST_RATE_MULTIPLIER) as i128,
INTEREST_RATE_DECIMALS,
)
(liquidity_rate / HUNDRED + INTEREST_RATE_MULTIPLIER) as i128,
INTEREST_RATE_DECIMALS,
)
.ln()
.to_u128_with_decimals(INTEREST_RATE_DECIMALS)
.unwrap()
Expand Down Expand Up @@ -652,9 +647,9 @@ pub fn get_available_to_borrow(env: Env, user: Address, denom: Symbol) -> u128 {
(max_allowed_borrow_amount_usd - sum_user_borrow_balance_usd) as i128,
USD_DECIMALS,
)
.div(Decimal::from_i128_with_scale(price as i128, USD_DECIMALS))
.to_u128_with_decimals(token_decimals)
.unwrap();
.div(Decimal::from_i128_with_scale(price as i128, USD_DECIMALS))
.to_u128_with_decimals(token_decimals)
.unwrap();

let token_liquidity = get_available_liquidity_by_token(env.clone(), denom.clone());

Expand Down Expand Up @@ -695,9 +690,9 @@ pub fn get_available_to_redeem(env: Env, user: Address, denom: Symbol) -> u128 {
(sum_collateral_balance_usd - required_collateral_balance_usd) as i128,
USD_DECIMALS,
)
.div(Decimal::from_i128_with_scale(price as i128, USD_DECIMALS))
.to_u128_with_decimals(token_decimals)
.unwrap();
.div(Decimal::from_i128_with_scale(price as i128, USD_DECIMALS))
.to_u128_with_decimals(token_decimals)
.unwrap();

if available_to_redeem > user_token_balance {
available_to_redeem = user_token_balance;
Expand Down
17 changes: 10 additions & 7 deletions contracts/lending/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ use super::{LendingContract, LendingContractClient};
use crate::storage::*;
use std::println;

use soroban_sdk::arbitrary::std::dbg;
use soroban_sdk::testutils::{Address as _, Events, Ledger, LedgerInfo};
use soroban_sdk::token::Interface;
use soroban_sdk::{symbol_short, token, Address, Env, IntoVal, String, Symbol};
use soroban_sdk::arbitrary::std::dbg;
use token::Client;

mod token_contract {
Expand Down Expand Up @@ -160,7 +160,8 @@ pub fn success_deposit_of_one_token_setup() -> (LendingContractClient<'static>,
(contract_client, admin, user1)
}

pub fn success_deposit_of_diff_token_with_prices() -> (Env, LendingContractClient<'static>, Address, Address) {
pub fn success_deposit_of_diff_token_with_prices(
) -> (Env, LendingContractClient<'static>, Address, Address) {
const TOKENS_DECIMALS: u32 = 18;

const INIT_BALANCE_ETH: u128 = 1000 * 10u128.pow(TOKENS_DECIMALS);
Expand Down Expand Up @@ -301,7 +302,8 @@ pub fn success_deposit_of_diff_token_with_prices() -> (Env, LendingContractClien
(env, contract_client, admin, user1)
}

pub fn success_deposit_as_collateral_of_diff_token_with_prices() -> (Env, LendingContractClient<'static>, Address, Address) {
pub fn success_deposit_as_collateral_of_diff_token_with_prices(
) -> (Env, LendingContractClient<'static>, Address, Address) {
let (env, contract_client, admin, user) = success_deposit_of_diff_token_with_prices();

contract_client.toggle_collateral_setting(&user, &symbol_short!("eth"));
Expand Down Expand Up @@ -658,7 +660,8 @@ fn test_successful_deposits_of_one_token() {
contract_client.get_total_borrow_data(&symbol_short!("eth"));
println!("Total borrow data: {:?}", total_borrow_data);

let reserves_by_token: u128 = contract_client.get_total_reserves_by_token(&symbol_short!("eth"));
let reserves_by_token: u128 =
contract_client.get_total_reserves_by_token(&symbol_short!("eth"));
println!("Total Reserves for Eth : {:?}", reserves_by_token);

user_deposited_balance = contract_client.get_deposit(&user1, &symbol_short!("eth"));
Expand Down Expand Up @@ -716,7 +719,7 @@ fn test_get_liquidity_rate() {
let get_liquidity_rate_xlm: u128 = contract_client.get_liquidity_rate(&symbol_short!("xlm"));

assert_eq!(get_liquidity_rate_xlm, 1153846153846153846); // ~1.154%
assert_eq!(get_liquidity_rate_eth,0 );
assert_eq!(get_liquidity_rate_eth, 0);
}

#[test]
Expand Down Expand Up @@ -1090,7 +1093,6 @@ fn test_success_repay_by_parts() {
// assert_eq!(liquidator_deposit_amount_xlm, 300000000000000000000); // 300 XLM
// }


#[test]
fn test_full_borrow() {
const TOKENS_DECIMALS: u32 = 18;
Expand All @@ -1108,7 +1110,8 @@ fn test_full_borrow() {
ledger_info.timestamp = 10000;
env.ledger().set(ledger_info.clone());

let user_deposited_balance_eth: u128 = contract_client.get_deposit(&user, &symbol_short!("eth"));
let user_deposited_balance_eth: u128 =
contract_client.get_deposit(&user, &symbol_short!("eth"));

assert_eq!(user_deposited_balance_eth, 200_000000000000000000); // 200 ETH

Expand Down
Loading

0 comments on commit 0bf0f19

Please sign in to comment.