1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
use frame_support::{storage::bounded_btree_map::BoundedBTreeMap, RuntimeDebugNoBound};
use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
use scale_info::TypeInfo;
use sp_runtime::{ArithmeticError, DispatchError};

use crate::{
	entities::pricing::external::ExternalAmount,
	pallet::{Config, Error},
	types::RepaidAmount,
	PriceOf,
};

#[derive(Encode, Decode, Clone, PartialEq, Eq, TypeInfo, RuntimeDebugNoBound, MaxEncodedLen)]
#[scale_info(skip_type_params(T))]
pub enum PrincipalInput<T: Config> {
	Internal(T::Balance),
	External(ExternalAmount<T>),
}

impl<T: Config> PrincipalInput<T> {
	pub fn balance(&self) -> Result<T::Balance, ArithmeticError> {
		match self {
			Self::Internal(amount) => Ok(*amount),
			Self::External(external) => external.balance(),
		}
	}

	pub fn internal(&self) -> Result<T::Balance, DispatchError> {
		match self {
			Self::Internal(amount) => Ok(*amount),
			Self::External(_) => Err(Error::<T>::MismatchedPricingMethod.into()),
		}
	}

	pub fn external(&self) -> Result<ExternalAmount<T>, DispatchError> {
		match self {
			Self::Internal(_) => Err(Error::<T>::MismatchedPricingMethod.into()),
			Self::External(principal) => Ok(principal.clone()),
		}
	}
}

#[derive(Encode, Decode, Clone, PartialEq, Eq, TypeInfo, RuntimeDebugNoBound, MaxEncodedLen)]
#[scale_info(skip_type_params(T))]
pub struct RepaidInput<T: Config> {
	pub principal: PrincipalInput<T>,
	pub interest: T::Balance,
	pub unscheduled: T::Balance,
}

impl<T: Config> RepaidInput<T> {
	pub fn repaid_amount(&self) -> Result<RepaidAmount<T::Balance>, ArithmeticError> {
		Ok(RepaidAmount {
			principal: self.principal.balance()?,
			interest: self.interest,
			unscheduled: self.unscheduled,
		})
	}
}

#[derive(Encode, Decode, Clone, PartialEq, Eq, TypeInfo, RuntimeDebugNoBound, MaxEncodedLen)]
#[scale_info(skip_type_params(T))]
pub enum PriceCollectionInput<T: Config> {
	Empty,
	Custom(BoundedBTreeMap<T::PriceId, PriceOf<T>, T::MaxActiveLoansPerPool>),
	FromRegistry,
}