penumbra_sdk_dex/
candlestick.rs

1use anyhow::Result;
2use serde::{Deserialize, Serialize};
3
4use penumbra_sdk_proto::{core::component::dex::v1 as pb, DomainType};
5
6#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
7#[serde(try_from = "pb::CandlestickData", into = "pb::CandlestickData")]
8pub struct CandlestickData {
9    /// The block height of the candlestick data.
10    pub height: u64,
11    /// The first observed price during the block execution.
12    pub open: f64,
13    /// The last observed price during the block execution.
14    pub close: f64,
15    /// The highest observed price during the block execution.
16    pub high: f64,
17    /// The lowest observed price during the block execution.
18    pub low: f64,
19    /// The volume that traded "directly", during individual position executions.
20    pub direct_volume: f64,
21    /// The volume that traded as part of swaps, which could have traversed multiple routes.
22    pub swap_volume: f64,
23}
24
25impl DomainType for CandlestickData {
26    type Proto = pb::CandlestickData;
27}
28
29impl From<CandlestickData> for pb::CandlestickData {
30    fn from(cd: CandlestickData) -> Self {
31        Self {
32            height: cd.height,
33            open: cd.open,
34            close: cd.close,
35            high: cd.high,
36            low: cd.low,
37            direct_volume: cd.direct_volume,
38            swap_volume: cd.swap_volume,
39        }
40    }
41}
42
43impl TryFrom<pb::CandlestickData> for CandlestickData {
44    type Error = anyhow::Error;
45    fn try_from(cd: pb::CandlestickData) -> Result<Self, Self::Error> {
46        Ok(Self {
47            height: cd.height,
48            open: cd.open,
49            close: cd.close,
50            high: cd.high,
51            low: cd.low,
52            direct_volume: cd.direct_volume,
53            swap_volume: cd.swap_volume,
54        })
55    }
56}