use crate::prelude::*;
#[derive(serde::Serialize, serde::Deserialize, Clone, Debug, PartialEq, Eq, Copy, Default)]
pub struct CollateralAndUsd {
collateral: Collateral,
usd: Usd,
}
impl CollateralAndUsd {
pub fn new(collateral: Collateral, price_point: &PricePoint) -> Self {
CollateralAndUsd {
collateral,
usd: price_point.collateral_to_usd(collateral),
}
}
pub fn from_pair(collateral: Collateral, usd: Usd) -> Self {
CollateralAndUsd { collateral, usd }
}
pub fn collateral(&self) -> Collateral {
self.collateral
}
pub fn usd(&self) -> Usd {
self.usd
}
pub fn checked_add_assign(
&mut self,
collateral: Collateral,
price_point: &PricePoint,
) -> Result<()> {
self.collateral = self.collateral.checked_add(collateral)?;
self.usd = self
.usd
.checked_add(price_point.collateral_to_usd(collateral))?;
Ok(())
}
}
#[derive(serde::Serialize, serde::Deserialize, Clone, Debug, PartialEq, Eq, Copy, Default)]
pub struct SignedCollateralAndUsd {
collateral: Signed<Collateral>,
usd: Signed<Usd>,
}
impl SignedCollateralAndUsd {
pub fn new(collateral: Signed<Collateral>, price_point: &PricePoint) -> Self {
SignedCollateralAndUsd {
collateral,
usd: collateral.map(|x| price_point.collateral_to_usd(x)),
}
}
pub fn collateral(&self) -> Signed<Collateral> {
self.collateral
}
pub fn usd(&self) -> Signed<Usd> {
self.usd
}
pub fn checked_add_assign(
&mut self,
collateral: Signed<Collateral>,
price_point: &PricePoint,
) -> Result<()> {
self.collateral = self.collateral.checked_add(collateral)?;
self.usd = self
.usd
.checked_add(collateral.map(|x| price_point.collateral_to_usd(x)))?;
Ok(())
}
}