-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: time handling for interest #17
Changes from all commits
df76a87
486ae1e
0d6c099
068e0c5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,4 +15,5 @@ pub enum LoansDataKey { | |
// Users positions in the pool | ||
Loan(Address), | ||
Addresses, | ||
LastUpdated, | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,7 @@ pub trait LoanPoolTrait { | |
fn initialize(e: Env, token: Address); | ||
|
||
// Deposits token. Also, mints pool shares for the "user" Identifier. | ||
fn deposit(e: Env, user: Address, amount: i128); | ||
fn deposit(e: Env, user: Address, amount: i128) -> i128; | ||
|
||
// Transfers share tokens back, burns them and gives corresponding amount of tokens back to user. Returns amount of tokens withdrawn | ||
fn withdraw(e: Env, user: Address, share_amount: i128) -> (i128, i128); | ||
|
@@ -46,7 +46,7 @@ impl LoanPoolTrait for LoanPoolContract { | |
pool::write_available_balance(&e, 0); | ||
} | ||
|
||
fn deposit(e: Env, user: Address, amount: i128) { | ||
fn deposit(e: Env, user: Address, amount: i128) -> i128 { | ||
user.require_auth(); // Depositor needs to authorize the deposit | ||
assert!(amount > 0, "Amount must be positive!"); | ||
|
||
|
@@ -56,16 +56,19 @@ impl LoanPoolTrait for LoanPoolContract { | |
let client = token::Client::new(&e, &pool::read_token(&e)); | ||
client.transfer(&user, &e.current_contract_address(), &amount); | ||
|
||
// TODO: Increase AvailableBalance | ||
// TODO: Increase TotalShares | ||
// TODO: Increase TotalBalance | ||
// TODO: these need to be replaced with increase rather than write so that it wont overwrite the values. | ||
pool::write_available_balance(&e, amount); | ||
pool::write_total_shares(&e, amount); | ||
pool::write_total_balance(&e, amount); | ||
|
||
// Increase users position in pool as they deposit | ||
// as this is deposit amount is added to receivables and | ||
// liabilities & collateral stays intact | ||
let liabilities: i128 = 0; // temp test param | ||
let collateral: i128 = 0; // temp test param | ||
positions::increase_positions(&e, user.clone(), amount, liabilities, collateral); | ||
|
||
amount | ||
} | ||
|
||
fn withdraw(e: Env, user: Address, amount: i128) -> (i128, i128) { | ||
|
@@ -109,7 +112,7 @@ impl LoanPoolTrait for LoanPoolContract { | |
*/ | ||
let address = String::from_str( | ||
&e, | ||
"CCR7ARWZN4WODMEWVTRCMPPJJQKE2MBKUPJBSYWCDEOT3OLBPAPEGLPH", | ||
"CB6MHNR6FJMQHJZDWOKAU4KESR4OARLPZ4RMN57R55P2QUBH4QJENHLY", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tää oli kaiketi väliaikanen ratkasu? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Joo tämä juuri pitäisi korjata 'Store loan manager’s ID for liquidity pool' tiketissä. |
||
); | ||
let contract: Address = Address::from_string(&address); | ||
contract.require_auth(); | ||
|
@@ -164,3 +167,116 @@ impl LoanPoolTrait for LoanPoolContract { | |
pool::read_total_balance(&e) | ||
} | ||
} | ||
|
||
#[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 good_deposit() { | ||
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 = 100; | ||
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); | ||
|
||
assert_eq!(result, amount_i); | ||
|
||
// Add assertions to validate expected behavior | ||
} | ||
|
||
#[test] | ||
fn good_withdraw() { | ||
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 = 100; | ||
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); | ||
|
||
assert_eq!(result, amount_i); | ||
|
||
let withdraw_args = vec![&e, user.to_val(), amount]; | ||
let withdraw_result: (i128, i128) = | ||
e.invoke_contract(&contract_id, &Symbol::new(&e, "withdraw"), withdraw_args); | ||
|
||
assert_eq!(withdraw_result, (amount_i, amount_i)); | ||
} | ||
|
||
#[test] | ||
#[should_panic(expected = "Amount can not be greater than receivables!")] | ||
fn withdraw_more_than_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 = 100; | ||
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); | ||
|
||
assert_eq!(result, amount_i); | ||
|
||
let amount_i_2: i128 = 200; | ||
let amount_2: Val = amount_i_2.into_val(&e); | ||
|
||
let withdraw_args = vec![&e, user.to_val(), amount_2]; | ||
let _withdraw_result: (i128, i128) = | ||
e.invoke_contract(&contract_id, &Symbol::new(&e, "withdraw"), withdraw_args); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Solidit testit 👏 |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Googlasin mitä eroa on
unwrap_or
jaunwrap_or_else
välillä koska en tiennyt. Ei tässä kohtaa väliä kumpaa käyttää, mutta jos toi else-haara olisi kallista laskea, niinunwrap_or_else
olisi helppo tapa optimoida.https://stackoverflow.com/a/56726618