Skip to content

Commit

Permalink
refactor(lpp): Get rid of an extra Result<_>
Browse files Browse the repository at this point in the history
  • Loading branch information
Gancho Manev committed May 26, 2023
1 parent 94fdcdb commit 44ee671
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 21 deletions.
3 changes: 2 additions & 1 deletion contracts/lease/src/lease/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,8 @@ mod tests {
}

fn repay(&mut self, by: Timestamp, repayment: Coin<Lpn>) -> lpp::error::Result<()> {
self.loan.repay(by, repayment).map(|_| ())
self.loan.repay(by, repayment);
Ok(())
}

fn annual_interest_rate(&self) -> Percent {
Expand Down
3 changes: 2 additions & 1 deletion contracts/lease/src/loan/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -982,7 +982,8 @@ mod tests {
}

fn repay(&mut self, by: Timestamp, repayment: Coin<Lpn>) -> LppResult<()> {
self.loan.repay(by, repayment).map(|_| ())
self.loan.repay(by, repayment);
Ok(())
}

fn annual_interest_rate(&self) -> Percent {
Expand Down
10 changes: 5 additions & 5 deletions contracts/lpp/src/loan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ where
interest_period.interest(self.principal_due)
}

pub fn repay(&mut self, by: Timestamp, repayment: Coin<Lpn>) -> Result<RepayShares<Lpn>> {
pub fn repay(&mut self, by: Timestamp, repayment: Coin<Lpn>) -> RepayShares<Lpn> {
let (due_period, interest_change) =
InterestPeriod::with_interest(self.annual_interest_rate)
.from(self.interest_paid)
Expand All @@ -65,11 +65,11 @@ where
self.principal_due -= principal_paid;
self.interest_paid = due_period.start();

Ok(RepayShares {
RepayShares {
interest: interest_paid,
principal: principal_paid,
excess,
})
}
}
}

Expand Down Expand Up @@ -172,7 +172,7 @@ mod test {
assert_eq!(interest, 100u128.into());

// partial repay
let payment = loan.repay(time, 600u128.into()).expect("should repay");
let payment = loan.repay(time, 600u128.into());
assert_eq!(payment.interest, 100u128.into());
assert_eq!(payment.principal, 500u128.into());
assert_eq!(payment.excess, 0u128.into());
Expand All @@ -184,7 +184,7 @@ mod test {
Loan::load(deps.as_ref().storage, addr.clone()).expect("should load loan");

// repay with excess, should close the loan
let payment = loan.repay(time, 600u128.into()).expect("should repay");
let payment = loan.repay(time, 600u128.into());
assert_eq!(payment.interest, 0u128.into());
assert_eq!(payment.principal, 500u128.into());
assert_eq!(payment.excess, 100u128.into());
Expand Down
2 changes: 1 addition & 1 deletion contracts/lpp/src/lpp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ where
) -> Result<Coin<LPN>> {
let mut loan = Loan::load(deps.storage, lease_addr.clone())?;
let loan_annual_interest_rate = loan.annual_interest_rate;
let payment = loan.repay(env.block.time, repay_amount)?;
let payment = loan.repay(env.block.time, repay_amount);
Loan::save(deps.storage, lease_addr, loan)?;

self.total
Expand Down
3 changes: 1 addition & 2 deletions contracts/lpp/src/stub/lender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ use sdk::cosmwasm_std::{Addr, QuerierWrapper, Reply};
use crate::{
error::{ContractError, Result},
msg::{ExecuteMsg, LoanResponse, QueryMsg, QueryQuoteResponse},
stub::LppBatch,
};

use super::LppRef;
use super::{LppBatch, LppRef};

pub trait LppLender<Lpn>
where
Expand Down
2 changes: 1 addition & 1 deletion contracts/lpp/src/stub/loan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ where
}

fn repay(&mut self, by: Timestamp, repayment: Coin<Lpn>) -> Result<()> {
self.loan.repay(by, repayment)?;
self.loan.repay(by, repayment);
self.batch
.schedule_execute_wasm_no_reply(
&self.lpp_ref.addr,
Expand Down
10 changes: 0 additions & 10 deletions contracts/lpp/src/stub/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ pub mod loan;

pub trait Lpp<Lpn>
where
Self: Into<LppBatch<LppRef>>,
Lpn: Currency,
{
fn lpp_balance(&self) -> Result<LppBalanceResponse<Lpn>>;
Expand Down Expand Up @@ -270,12 +269,3 @@ pub struct LppBatch<Ref> {
pub lpp_ref: Ref,
pub batch: Batch,
}

impl<'a, C> From<LppStub<'a, C>> for LppBatch<LppRef> {
fn from(stub: LppStub<'a, C>) -> Self {
Self {
lpp_ref: stub.lpp_ref,
batch: Batch::default(),
}
}
}

0 comments on commit 44ee671

Please sign in to comment.