Skip to main content

canic_core/cdk/spec/standards/
xrc.rs

1//! Module: cdk::spec::standards::xrc
2//!
3//! Responsibility: XRC Candid DTOs used by Canic callers.
4//! Does not own: exchange-rate policy, pricing decisions, or cycle payment.
5//! Boundary: mirrors the external XRC `get_exchange_rate` surface.
6
7use crate::cdk::spec::prelude::*;
8
9//
10// AssetClass
11// XRC asset class discriminator.
12//
13
14#[derive(CandidType, Clone, Copy, Debug, Deserialize, Eq, PartialEq)]
15pub enum AssetClass {
16    Cryptocurrency,
17    FiatCurrency,
18}
19
20//
21// Asset
22// XRC asset descriptor used in exchange rate queries.
23//
24
25#[derive(CandidType, Clone, Debug, Deserialize)]
26pub struct Asset {
27    pub symbol: String,
28    pub class: AssetClass,
29}
30
31//
32// GetExchangeRateRequest
33// Request payload for XRC `get_exchange_rate`.
34//
35
36#[derive(CandidType, Clone, Debug, Deserialize)]
37pub struct GetExchangeRateRequest {
38    pub base_asset: Asset,
39    pub quote_asset: Asset,
40    pub timestamp: Option<u64>,
41}
42
43//
44// ExchangeRateMetadata
45// Metadata attached to a returned exchange rate.
46//
47
48#[derive(CandidType, Clone, Debug, Deserialize)]
49pub struct ExchangeRateMetadata {
50    pub decimals: u32,
51    pub base_asset_num_queried_sources: u32,
52    pub base_asset_num_received_rates: u32,
53    pub quote_asset_num_queried_sources: u32,
54    pub quote_asset_num_received_rates: u32,
55    pub standard_deviation: u64,
56    pub forex_timestamp: Option<u64>,
57}
58
59//
60// ExchangeRate
61// Returned exchange rate record.
62//
63
64#[derive(CandidType, Clone, Debug, Deserialize)]
65pub struct ExchangeRate {
66    pub rate: u64,
67    pub metadata: ExchangeRateMetadata,
68}
69
70//
71// ExchangeRateError
72// Error variants returned by XRC.
73//
74
75#[derive(CandidType, Clone, Debug, Deserialize)]
76pub enum ExchangeRateError {
77    AnonymousPrincipalNotAllowed,
78    CryptoQuoteAssetNotFound,
79    FailedToAcceptCycles,
80    ForexBaseAssetNotFound,
81    CryptoBaseAssetNotFound,
82    StablecoinRateTooFewRates,
83    ForexAssetsNotFound,
84    InconsistentRatesReceived,
85    RateLimited,
86    StablecoinRateZeroRate,
87    Other(String),
88    NotEnoughCycles,
89    ForexInvalidTimestamp,
90    NotEnoughRates,
91    ForexQuoteAssetNotFound,
92    StablecoinRateNotFound,
93}
94
95//
96// GetExchangeRateResult
97// Result envelope for XRC `get_exchange_rate`.
98//
99
100#[derive(CandidType, Clone, Debug, Deserialize)]
101pub enum GetExchangeRateResult {
102    Ok(ExchangeRate),
103    Err(ExchangeRateError),
104}