Skip to main content

canic_cdk/spec/standards/
xrc.rs

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