use super::{AnalyticsRequest, StablecoinId, StablecoinScope, TvlScope};
use crate::{
domain::{
ExactDecimal, Timestamp,
market::{Identifier, Label, NonnegativeDecimal, UtcDateTime, unique},
},
error::{Error, ValidationError},
};
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ProtocolTvl {
protocol: Identifier,
usd: NonnegativeDecimal,
}
impl ProtocolTvl {
#[must_use]
pub const fn new(protocol: Identifier, usd: NonnegativeDecimal) -> Self {
Self { protocol, usd }
}
#[must_use]
pub const fn protocol(&self) -> &Identifier {
&self.protocol
}
#[must_use]
pub const fn usd(&self) -> &NonnegativeDecimal {
&self.usd
}
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct UsdPoint {
date: Timestamp,
usd: NonnegativeDecimal,
}
impl UsdPoint {
#[must_use]
pub const fn new(date: Timestamp, usd: NonnegativeDecimal) -> Self {
Self { date, usd }
}
#[must_use]
pub const fn date(&self) -> &Timestamp {
&self.date
}
#[must_use]
pub const fn usd(&self) -> &NonnegativeDecimal {
&self.usd
}
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct BreakdownValue {
label: Label,
value: NonnegativeDecimal,
}
impl BreakdownValue {
#[must_use]
pub const fn new(label: Label, value: NonnegativeDecimal) -> Self {
Self { label, value }
}
#[must_use]
pub const fn label(&self) -> &Label {
&self.label
}
#[must_use]
pub const fn value(&self) -> &NonnegativeDecimal {
&self.value
}
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct YieldRates {
#[serde(deserialize_with = "crate::domain::deserialize_optional")]
apy: Option<ExactDecimal>,
#[serde(deserialize_with = "crate::domain::deserialize_optional")]
base: Option<ExactDecimal>,
#[serde(deserialize_with = "crate::domain::deserialize_optional")]
reward: Option<ExactDecimal>,
}
impl YieldRates {
#[must_use]
pub const fn new(
apy: Option<ExactDecimal>,
base: Option<ExactDecimal>,
reward: Option<ExactDecimal>,
) -> Self {
Self { apy, base, reward }
}
#[must_use]
pub const fn apy(&self) -> &Option<ExactDecimal> {
&self.apy
}
#[must_use]
pub const fn base(&self) -> &Option<ExactDecimal> {
&self.base
}
#[must_use]
pub const fn reward(&self) -> &Option<ExactDecimal> {
&self.reward
}
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct YieldPool {
pool: Identifier,
chain: Label,
project: Identifier,
symbol: YieldSymbol,
tvl_usd: NonnegativeDecimal,
rates: YieldRates,
}
impl YieldPool {
#[must_use]
pub const fn new(
pool: Identifier,
chain: Label,
project: Identifier,
symbol: YieldSymbol,
tvl_usd: NonnegativeDecimal,
rates: YieldRates,
) -> Self {
Self {
pool,
chain,
project,
symbol,
tvl_usd,
rates,
}
}
#[must_use]
pub const fn pool(&self) -> &Identifier {
&self.pool
}
#[must_use]
pub const fn chain(&self) -> &Label {
&self.chain
}
#[must_use]
pub const fn project(&self) -> &Identifier {
&self.project
}
#[must_use]
pub const fn symbol(&self) -> &YieldSymbol {
&self.symbol
}
#[must_use]
pub const fn tvl_usd(&self) -> &NonnegativeDecimal {
&self.tvl_usd
}
#[must_use]
pub const fn rates(&self) -> &YieldRates {
&self.rates
}
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct YieldPoint {
date: UtcDateTime,
tvl_usd: NonnegativeDecimal,
rates: YieldRates,
}
impl YieldPoint {
#[must_use]
pub const fn new(date: UtcDateTime, tvl_usd: NonnegativeDecimal, rates: YieldRates) -> Self {
Self {
date,
tvl_usd,
rates,
}
}
#[must_use]
pub const fn date(&self) -> &UtcDateTime {
&self.date
}
#[must_use]
pub const fn tvl_usd(&self) -> &NonnegativeDecimal {
&self.tvl_usd
}
#[must_use]
pub const fn rates(&self) -> &YieldRates {
&self.rates
}
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AnalyticsTotals {
#[serde(deserialize_with = "crate::domain::deserialize_optional")]
day: Option<NonnegativeDecimal>,
#[serde(deserialize_with = "crate::domain::deserialize_optional")]
week: Option<NonnegativeDecimal>,
#[serde(deserialize_with = "crate::domain::deserialize_optional")]
all_time: Option<NonnegativeDecimal>,
}
impl AnalyticsTotals {
#[must_use]
pub const fn new(
day: Option<NonnegativeDecimal>,
week: Option<NonnegativeDecimal>,
all_time: Option<NonnegativeDecimal>,
) -> Self {
Self {
day,
week,
all_time,
}
}
#[must_use]
pub const fn day(&self) -> &Option<NonnegativeDecimal> {
&self.day
}
#[must_use]
pub const fn week(&self) -> &Option<NonnegativeDecimal> {
&self.week
}
#[must_use]
pub const fn all_time(&self) -> &Option<NonnegativeDecimal> {
&self.all_time
}
}
fn bounded(count: usize) -> Result<(), Error> {
if count > 100_000 {
Err(ValidationError::InvalidMarketRecord.into())
} else {
Ok(())
}
}
fn ordered(points: &[UsdPoint]) -> Result<(), Error> {
bounded(points.len())?;
if points.windows(2).any(|p| p[0].date() >= p[1].date()) {
return Err(ValidationError::InvalidMarketRecord.into());
}
Ok(())
}
fn breakdowns(values: &[BreakdownValue]) -> Result<(), Error> {
bounded(values.len())?;
if !unique(values.iter().map(|v| v.label().as_str())) {
return Err(ValidationError::InvalidMarketRecord.into());
}
Ok(())
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(try_from = "ProtocolFields")]
pub struct ProtocolHistory {
protocol: Identifier,
provider_id: Label,
name: Label,
points: Vec<UsdPoint>,
current_breakdowns: Vec<BreakdownValue>,
}
impl ProtocolHistory {
pub fn new(
protocol: Identifier,
provider_id: Label,
name: Label,
points: Vec<UsdPoint>,
current_breakdowns: Vec<BreakdownValue>,
) -> Result<Self, Error> {
ordered(&points)?;
breakdowns(¤t_breakdowns)?;
Ok(Self {
protocol,
provider_id,
name,
points,
current_breakdowns,
})
}
#[must_use]
pub const fn protocol(&self) -> &Identifier {
&self.protocol
}
#[must_use]
pub const fn provider_id(&self) -> &Label {
&self.provider_id
}
#[must_use]
pub const fn name(&self) -> &Label {
&self.name
}
#[must_use]
pub fn points(&self) -> &[UsdPoint] {
&self.points
}
#[must_use]
pub fn current_breakdowns(&self) -> &[BreakdownValue] {
&self.current_breakdowns
}
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct ProtocolFields {
protocol: Identifier,
provider_id: Label,
name: Label,
points: Vec<UsdPoint>,
current_breakdowns: Vec<BreakdownValue>,
}
impl TryFrom<ProtocolFields> for ProtocolHistory {
type Error = Error;
fn try_from(v: ProtocolFields) -> Result<Self, Error> {
Self::new(
v.protocol,
v.provider_id,
v.name,
v.points,
v.current_breakdowns,
)
}
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(try_from = "TvlFields")]
pub struct TvlHistory {
scope: TvlScope,
points: Vec<UsdPoint>,
}
impl TvlHistory {
pub fn new(scope: TvlScope, points: Vec<UsdPoint>) -> Result<Self, Error> {
ordered(&points)?;
Ok(Self { scope, points })
}
#[must_use]
pub const fn scope(&self) -> &TvlScope {
&self.scope
}
#[must_use]
pub fn points(&self) -> &[UsdPoint] {
&self.points
}
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct TvlFields {
scope: TvlScope,
points: Vec<UsdPoint>,
}
impl TryFrom<TvlFields> for TvlHistory {
type Error = Error;
fn try_from(v: TvlFields) -> Result<Self, Error> {
Self::new(v.scope, v.points)
}
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(try_from = "YieldPoolsFields")]
pub struct YieldPools {
pools: Vec<YieldPool>,
}
impl YieldPools {
pub fn new(pools: Vec<YieldPool>) -> Result<Self, Error> {
bounded(pools.len())?;
if !unique(pools.iter().map(YieldPool::pool)) {
return Err(ValidationError::InvalidMarketRecord.into());
}
Ok(Self { pools })
}
#[must_use]
pub fn pools(&self) -> &[YieldPool] {
&self.pools
}
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct YieldPoolsFields {
pools: Vec<YieldPool>,
}
impl TryFrom<YieldPoolsFields> for YieldPools {
type Error = Error;
fn try_from(v: YieldPoolsFields) -> Result<Self, Error> {
Self::new(v.pools)
}
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(try_from = "YieldHistoryFields")]
pub struct YieldHistory {
pool: Identifier,
points: Vec<YieldPoint>,
}
impl YieldHistory {
pub fn new(pool: Identifier, points: Vec<YieldPoint>) -> Result<Self, Error> {
bounded(points.len())?;
if points
.windows(2)
.any(|p| p[0].date().chronological_key() >= p[1].date().chronological_key())
{
return Err(ValidationError::InvalidMarketRecord.into());
}
Ok(Self { pool, points })
}
#[must_use]
pub const fn pool(&self) -> &Identifier {
&self.pool
}
#[must_use]
pub fn points(&self) -> &[YieldPoint] {
&self.points
}
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct YieldHistoryFields {
pool: Identifier,
points: Vec<YieldPoint>,
}
impl TryFrom<YieldHistoryFields> for YieldHistory {
type Error = Error;
fn try_from(v: YieldHistoryFields) -> Result<Self, Error> {
Self::new(v.pool, v.points)
}
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(try_from = "StablecoinFields")]
pub struct Stablecoin {
id: StablecoinId,
name: Label,
symbol: Label,
peg_type: Label,
circulating: Vec<BreakdownValue>,
price_usd: Option<NonnegativeDecimal>,
chains: Vec<Label>,
}
impl Stablecoin {
pub fn new(
id: StablecoinId,
name: Label,
symbol: Label,
peg_type: Label,
circulating: Vec<BreakdownValue>,
price_usd: Option<NonnegativeDecimal>,
chains: Vec<Label>,
) -> Result<Self, Error> {
breakdowns(&circulating)?;
bounded(chains.len())?;
if !unique(chains.iter().map(Label::as_str)) {
return Err(ValidationError::InvalidMarketRecord.into());
}
Ok(Self {
id,
name,
symbol,
peg_type,
circulating,
price_usd,
chains,
})
}
#[must_use]
pub const fn id(&self) -> StablecoinId {
self.id
}
#[must_use]
pub const fn name(&self) -> &Label {
&self.name
}
#[must_use]
pub const fn symbol(&self) -> &Label {
&self.symbol
}
#[must_use]
pub const fn peg_type(&self) -> &Label {
&self.peg_type
}
#[must_use]
pub fn circulating(&self) -> &[BreakdownValue] {
&self.circulating
}
#[must_use]
pub const fn price_usd(&self) -> &Option<NonnegativeDecimal> {
&self.price_usd
}
#[must_use]
pub fn chains(&self) -> &[Label] {
&self.chains
}
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct StablecoinFields {
id: StablecoinId,
name: Label,
symbol: Label,
peg_type: Label,
circulating: Vec<BreakdownValue>,
#[serde(deserialize_with = "crate::domain::deserialize_optional")]
price_usd: Option<NonnegativeDecimal>,
chains: Vec<Label>,
}
impl TryFrom<StablecoinFields> for Stablecoin {
type Error = Error;
fn try_from(v: StablecoinFields) -> Result<Self, Error> {
Self::new(
v.id,
v.name,
v.symbol,
v.peg_type,
v.circulating,
v.price_usd,
v.chains,
)
}
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(try_from = "StablecoinsFields")]
pub struct Stablecoins {
assets: Vec<Stablecoin>,
}
impl Stablecoins {
pub fn new(assets: Vec<Stablecoin>) -> Result<Self, Error> {
bounded(assets.len())?;
if !unique(assets.iter().map(Stablecoin::id)) {
return Err(ValidationError::InvalidMarketRecord.into());
}
Ok(Self { assets })
}
#[must_use]
pub fn assets(&self) -> &[Stablecoin] {
&self.assets
}
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct StablecoinsFields {
assets: Vec<Stablecoin>,
}
impl TryFrom<StablecoinsFields> for Stablecoins {
type Error = Error;
fn try_from(v: StablecoinsFields) -> Result<Self, Error> {
Self::new(v.assets)
}
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(try_from = "StablecoinPointFields")]
pub struct StablecoinPoint {
date: Timestamp,
circulating: Vec<BreakdownValue>,
circulating_usd: Vec<BreakdownValue>,
}
impl StablecoinPoint {
pub fn new(
date: Timestamp,
circulating: Vec<BreakdownValue>,
circulating_usd: Vec<BreakdownValue>,
) -> Result<Self, Error> {
breakdowns(&circulating)?;
breakdowns(&circulating_usd)?;
Ok(Self {
date,
circulating,
circulating_usd,
})
}
#[must_use]
pub const fn date(&self) -> Timestamp {
self.date
}
#[must_use]
pub fn circulating(&self) -> &[BreakdownValue] {
&self.circulating
}
#[must_use]
pub fn circulating_usd(&self) -> &[BreakdownValue] {
&self.circulating_usd
}
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct StablecoinPointFields {
date: Timestamp,
circulating: Vec<BreakdownValue>,
circulating_usd: Vec<BreakdownValue>,
}
impl TryFrom<StablecoinPointFields> for StablecoinPoint {
type Error = Error;
fn try_from(v: StablecoinPointFields) -> Result<Self, Error> {
Self::new(v.date, v.circulating, v.circulating_usd)
}
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(try_from = "StablecoinHistoryFields")]
pub struct StablecoinHistory {
scope: StablecoinScope,
points: Vec<StablecoinPoint>,
}
impl StablecoinHistory {
pub fn new(scope: StablecoinScope, points: Vec<StablecoinPoint>) -> Result<Self, Error> {
bounded(points.len())?;
if points.windows(2).any(|p| p[0].date() >= p[1].date()) {
return Err(ValidationError::InvalidMarketRecord.into());
}
Ok(Self { scope, points })
}
#[must_use]
pub const fn scope(&self) -> &StablecoinScope {
&self.scope
}
#[must_use]
pub fn points(&self) -> &[StablecoinPoint] {
&self.points
}
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct StablecoinHistoryFields {
scope: StablecoinScope,
points: Vec<StablecoinPoint>,
}
impl TryFrom<StablecoinHistoryFields> for StablecoinHistory {
type Error = Error;
fn try_from(v: StablecoinHistoryFields) -> Result<Self, Error> {
Self::new(v.scope, v.points)
}
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(try_from = "AnalyticsFields")]
pub struct Analytics {
request: AnalyticsRequest,
name: Label,
totals: AnalyticsTotals,
daily: Vec<UsdPoint>,
}
impl Analytics {
pub fn new(
request: AnalyticsRequest,
name: Label,
totals: AnalyticsTotals,
daily: Vec<UsdPoint>,
) -> Result<Self, Error> {
ordered(&daily)?;
Ok(Self {
request,
name,
totals,
daily,
})
}
#[must_use]
pub const fn request(&self) -> &AnalyticsRequest {
&self.request
}
#[must_use]
pub const fn name(&self) -> &Label {
&self.name
}
#[must_use]
pub const fn totals(&self) -> &AnalyticsTotals {
&self.totals
}
#[must_use]
pub fn daily(&self) -> &[UsdPoint] {
&self.daily
}
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct AnalyticsFields {
request: AnalyticsRequest,
name: Label,
totals: AnalyticsTotals,
daily: Vec<UsdPoint>,
}
impl TryFrom<AnalyticsFields> for Analytics {
type Error = Error;
fn try_from(v: AnalyticsFields) -> Result<Self, Error> {
Self::new(v.request, v.name, v.totals, v.daily)
}
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(try_from = "String", into = "String")]
pub struct YieldSymbol(String);
impl YieldSymbol {
pub fn new(value: &str) -> Result<Self, Error> {
if value.is_empty() || value.len() > 512 {
return Err(ValidationError::InvalidMarketRecord.into());
}
Ok(Self(value.to_owned()))
}
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
}
impl TryFrom<String> for YieldSymbol {
type Error = Error;
fn try_from(value: String) -> Result<Self, Error> {
Self::new(&value)
}
}
impl From<YieldSymbol> for String {
fn from(value: YieldSymbol) -> Self {
value.0
}
}