Skip to content

Commit

Permalink
chore: user cannot approve twice
Browse files Browse the repository at this point in the history
  • Loading branch information
thevaibhav-dixit committed Sep 4, 2024
1 parent b1f7bcf commit bf0de6a
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
58 changes: 57 additions & 1 deletion core/src/loan/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,13 +376,32 @@ impl Loan {
}
}

pub(super) fn has_user_previously_approved(&self, user_id: UserId) -> bool {
for event in self.events.iter() {
match event {
LoanEvent::ApprovalAdded {
approving_user_id, ..
} => {
if user_id == *approving_user_id {
return true;
}
}
_ => continue,
}
}
false
}

pub(super) fn add_approval(
&mut self,
approving_user_id: UserId,
approving_user_roles: HashSet<Role>,
audit_info: AuditInfo,
price: PriceOfOneBTC,
) -> Result<Option<LoanApproval>, LoanError> {
if self.has_user_previously_approved(approving_user_id) {
return Err(LoanError::UserCannotApproveTwice);
}
if self.is_approved() {
return Err(LoanError::AlreadyApproved);
}
Expand All @@ -391,7 +410,6 @@ impl Loan {
return Err(LoanError::NoCollateral);
}

// move below logic to terms ?
let current_cvl = self.cvl(price);
let margin_call_cvl = self.terms.margin_call_cvl;

Expand Down Expand Up @@ -1701,4 +1719,42 @@ mod test {

assert!(!loan.approval_threshold_met());
}

#[test]
fn same_user_cannot_approve_twice() {
let mut loan = Loan::try_from(init_events()).unwrap();
let loan_collateral_update = loan
.initiate_collateral_update(Satoshis::from(10000))
.unwrap();
loan.confirm_collateral_update(
loan_collateral_update,
Utc::now(),
dummy_audit_info(),
default_price(),
default_upgrade_buffer_cvl_pct(),
);

let user_id = UserId::new();

let first_approval = loan.add_approval(
user_id,
bank_manager_role(),
dummy_audit_info(),
default_price(),
);

assert!(first_approval.is_ok());

let second_approval = loan.add_approval(
user_id,
bank_manager_role(),
dummy_audit_info(),
default_price(),
);

assert!(matches!(
second_approval,
Err(LoanError::UserCannotApproveTwice)
));
}
}
2 changes: 2 additions & 0 deletions core/src/loan/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ pub enum LoanError {
AlreadyCompleted,
#[error("LoanError - AlreadyApproved")]
AlreadyApproved,
#[error("LoanError - UserCannotApproveTwice")]
UserCannotApproveTwice,
#[error("LoanError - PaymentExceedsOutstandingLoanAmount: {0} > {1}")]
PaymentExceedsOutstandingLoanAmount(UsdCents, UsdCents),
#[error("LoanError - UnexpectedZeroPrincipalAmount: totalAmount({0}), interestAmount({1})")]
Expand Down

0 comments on commit bf0de6a

Please sign in to comment.