Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion loan_manager/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1812,7 +1812,7 @@ impl LoanManager {

// Validate collateral covers new amount (collateral must be >= loan amount)
if loan.collateral_amount < new_amount {
return Err(LoanError::InsufficientScore);
return Err(LoanError::InsufficientCollateral);
}

// Settle all accrued interest and late fees up to now.
Expand Down
40 changes: 40 additions & 0 deletions loan_manager/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2715,6 +2715,46 @@ fn test_refinance_loan_fails_when_score_drops_below_minimum() {
assert_eq!(manager.get_loan(&loan_id).status, LoanStatus::Approved);
}

#[test]
fn test_refinance_loan_fails_with_insufficient_collateral() {
let env = Env::default();
env.mock_all_auths_allowing_non_root_auth();

let (manager, nft_client, pool_client, token_id, _admin) = setup_test(&env);
let borrower = Address::generate(&env);

let history_hash = soroban_sdk::BytesN::from_array(&env, &[0u8; 32]);
nft_client.mint(
&borrower,
&600,
&history_hash,
&String::from_str(&env, "ipfs://QmTest"),
&None,
);

let stellar_token = StellarAssetClient::new(&env, &token_id);
stellar_token.mint(&pool_client, &50_000);

let loan_id = manager.request_loan(&borrower, &1_000, &17_280);
manager.approve_loan(&loan_id);

// Set collateral to 500, which is less than the new_amount of 2_000.
stellar_token.mint(&manager.address, &500);
env.as_contract(&manager.address, || {
let key = DataKey::Loan(loan_id);
let mut loan: Loan = env.storage().persistent().get(&key).unwrap();
loan.collateral_amount = 500;
env.storage().persistent().set(&key, &loan);
});

// Attempting to refinance up to 2_000 must fail with InsufficientCollateral,
// not InsufficientScore.
let result = manager.try_refinance_loan(&loan_id, &2_000, &17_280);
assert_eq!(result, Err(Ok(LoanError::InsufficientCollateral)));
// Loan must remain unchanged.
assert_eq!(manager.get_loan(&loan_id).status, LoanStatus::Approved);
}

// ── Pause functionality tests ──────────────────────────────────────────────

#[test]
Expand Down
Loading