Skip to content

Commit

Permalink
Merge branch 'main' into centrifuge/anemoy-pool-currency
Browse files Browse the repository at this point in the history
  • Loading branch information
NunoAlexandre authored Sep 23, 2023
2 parents bbb1cb7 + d007468 commit 4ef3398
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 3 deletions.
4 changes: 4 additions & 0 deletions pallets/loans/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -947,6 +947,10 @@ pub mod pallet {
.position(|(id, _)| *id == loan_id)
.ok_or(Error::<T>::LoanNotActiveOrNotFound)?;

PortfolioValuation::<T>::try_mutate(pool_id, |portfolio| {
portfolio.remove_elem(loan_id)
})?;

Ok((
active_loans.swap_remove(index).1,
active_loans.len().ensure_into()?,
Expand Down
59 changes: 56 additions & 3 deletions pallets/loans/src/types/portfolio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,20 @@ where
self.last_updated
}

pub fn value_of(&self, id: ElemId) -> Option<&Balance> {
pub fn value_of(&self, id: ElemId) -> Option<Balance> {
self.values
.iter()
.find(|(elem_id, _)| *elem_id == id)
.map(|(_, balance)| balance)
.map(|(_, balance)| *balance)
}

pub fn insert_elem(&mut self, id: ElemId, pv: Balance) -> DispatchResult {
self.values
.try_push((id, pv))
.map_err(|_| DispatchError::Other("Max portfolio size reached"))?;

Ok(self.value.ensure_add_assign(pv)?)
self.value.ensure_add_assign(pv)?;
Ok(())
}

pub fn update_elem(&mut self, id: ElemId, new_pv: Balance) -> DispatchResult {
Expand All @@ -110,6 +111,18 @@ where

Ok(())
}

pub fn remove_elem(&mut self, elem_id: ElemId) -> DispatchResult {
let index = self
.values
.iter()
.position(|(id, _)| *id == elem_id)
.ok_or(DispatchError::CannotLookup)?;

let (_, pv) = self.values.swap_remove(index);
self.value.ensure_sub_assign(pv)?;
Ok(())
}
}

/// Type that builds a PortfolioValuation with the current instant.
Expand All @@ -136,3 +149,43 @@ pub enum PortfolioValuationUpdateType {
/// Portfolio Valuation was updated inexactly based on loan status changes
Inexact,
}

#[cfg(test)]
mod tests {
use frame_support::assert_ok;
use sp_core::ConstU32;

use super::*;

#[test]
fn general_usage() {
let mut portfolio = PortfolioValuation::<u128, u64, ConstU32<3>>::new(10);

assert_ok!(portfolio.insert_elem(1, 100));
assert_ok!(portfolio.insert_elem(2, 200));
assert_ok!(portfolio.insert_elem(3, 300));

assert_eq!(portfolio.value(), 600);

// Increase
assert_ok!(portfolio.update_elem(1, 300));
assert_eq!(portfolio.value(), 800);

// Do not change
assert_ok!(portfolio.update_elem(2, 200));
assert_eq!(portfolio.value(), 800);

// Decrease
assert_ok!(portfolio.update_elem(3, 100));
assert_eq!(portfolio.value(), 600);

assert_eq!(portfolio.value_of(1), Some(300));
assert_eq!(portfolio.value_of(2), Some(200));
assert_eq!(portfolio.value_of(3), Some(100));

assert_ok!(portfolio.remove_elem(1));
assert_ok!(portfolio.remove_elem(2));
assert_ok!(portfolio.remove_elem(3));
assert_eq!(portfolio.value(), 0);
}
}

0 comments on commit 4ef3398

Please sign in to comment.