Skip to content

Commit

Permalink
chore: added frame for unit test to loan manager
Browse files Browse the repository at this point in the history
  • Loading branch information
Teolhyn committed Aug 21, 2024
1 parent 7c2c499 commit aa9e64c
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 24 deletions.
77 changes: 55 additions & 22 deletions contracts/loan_manager/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::oracle::{self, Asset};
use crate::positions;
use crate::storage_types::{LoansDataKey, POSITIONS_BUMP_AMOUNT, POSITIONS_LIFETIME_THRESHOLD};
use crate::oracle::{self, Asset};

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

#[allow(dead_code)]
Expand Down Expand Up @@ -162,24 +162,57 @@ impl LoansTrait for LoansContract {
token_amount: i128,
token_collateral: Address,
token_collateral_amount: i128,
) -> i128 {
let reflector_contract: oracle::Client = oracle::Client::new(&e, &reflector_contract_id);

// get the price and calculate the value of the collateral
let collateral_asset: Asset = Asset::Other(Symbol::new(&e, "XLM"));

let collateral_asset_price: oracle::PriceData = reflector_contract.lastprice(&collateral_asset).unwrap();
let collateral_value: i128 = collateral_asset_price.price * token_collateral_amount;

// get the price and calculate the value of the borrowed asset
let borrowed_asset: Asset = Asset::Other(Symbol::new(&e, "USDC"));

let asset_price: oracle::PriceData = reflector_contract.lastprice(&borrowed_asset).unwrap();
let borrowed_value: i128 = asset_price.price * token_amount;

let health_ratio: i128 = collateral_value * 10000000_i128 / borrowed_value;

health_ratio
}
) -> i128 {
let reflector_contract: oracle::Client = oracle::Client::new(&e, &reflector_contract_id);

// get the price and calculate the value of the collateral
let collateral_asset: Asset = Asset::Other(Symbol::new(&e, "BTC"));

let collateral_asset_price: oracle::PriceData =
reflector_contract.lastprice(&collateral_asset).unwrap();
let collateral_value: i128 = collateral_asset_price.price * token_collateral_amount;

// get the price and calculate the value of the borrowed asset
let borrowed_asset: Asset = Asset::Other(Symbol::new(&e, "ETH"));

}
let asset_price: oracle::PriceData = reflector_contract.lastprice(&borrowed_asset).unwrap();
let borrowed_value: i128 = asset_price.price * token_amount;

let health_ratio: i128 = collateral_value * 10000000_i128 / borrowed_value;

health_ratio
}
}
#[cfg(test)]
mod tests {
use super::*; // This imports LoanPoolContract and everything else from the parent module
use soroban_sdk::{
testutils::Address as _,
token::{Client as TokenClient, StellarAssetClient},
vec, Env, IntoVal,
};

#[test]
fn create_loan() {
let e: Env = Env::default();
e.mock_all_auths();

let admin: Address = Address::generate(&e);
let token_contract_id = e.register_stellar_asset_contract(admin.clone());
let stellar_asset = StellarAssetClient::new(&e, &token_contract_id);
let token = TokenClient::new(&e, &token_contract_id);

let admin2: Address = Address::generate(&e);
let token_contract_id2 = e.register_stellar_asset_contract(admin2.clone());
let stellar_asset2 = StellarAssetClient::new(&e, &token_contract_id2);
let collateral_token = TokenClient::new(&e, &token_contract_id);

let user = Address::generate(&e);
stellar_asset.mint(&user, &1000);
stellar_asset2.mint(&user, &1000);
assert_eq!(token.balance(&user), 1000);
assert_eq!(collateral_token.balance(&user), 1000);

//TODO: study how to create loan_pools for testing as one can not initialize without it. for now this should pass but it doesn't test anything
}
}
2 changes: 1 addition & 1 deletion contracts/loan_manager/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![no_std]

mod contract;
mod oracle;
mod positions;
mod storage_types;
mod oracle;
2 changes: 1 addition & 1 deletion contracts/loan_manager/src/oracle.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
use soroban_sdk::contractimport;

contractimport!(file = "../../target/wasm32-unknown-unknown/release/reflector_oracle.wasm");
contractimport!(file = "../../target/wasm32-unknown-unknown/release/reflector_oracle.wasm");
28 changes: 28 additions & 0 deletions contracts/loan_pool/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,34 @@ mod tests {
assert_eq!(withdraw_result, (amount_i, amount_i));
}

#[test]
#[should_panic]
fn deposit_more_than_account_balance() {
let e: Env = Env::default();
e.mock_all_auths();

let admin: Address = Address::generate(&e);
let token_contract_id = e.register_stellar_asset_contract(admin.clone());
let stellar_asset = StellarAssetClient::new(&e, &token_contract_id);
let token = TokenClient::new(&e, &token_contract_id);

let user = Address::generate(&e);
stellar_asset.mint(&user, &1000);
assert_eq!(token.balance(&user), 1000);

let contract_id = e.register_contract(None, LoanPoolContract);
let amount_i: i128 = 2000;
let amount: Val = amount_i.into_val(&e);

let args: soroban_sdk::Vec<Val> = vec![&e, user.to_val(), amount];
let init_args: soroban_sdk::Vec<Val> = vec![&e, token_contract_id.to_val()];

let _init_result: () =
e.invoke_contract(&contract_id, &Symbol::new(&e, "initialize"), init_args);

let _result: i128 = e.invoke_contract(&contract_id, &Symbol::new(&e, "deposit"), args);
}

#[test]
#[should_panic(expected = "Amount can not be greater than receivables!")]
fn withdraw_more_than_balance() {
Expand Down

0 comments on commit aa9e64c

Please sign in to comment.