Skip to main content

pm_types/
entry.rs

1use miden_client::{Felt, Word};
2
3use crate::pair::Pair;
4#[derive(Debug, Clone)]
5pub struct Entry {
6    pub pair: Pair,
7    // TODO(akhercha): We may prefer a u128 for more precision.
8    // This can probably done by storing a Price(low, high) struct with two u64s.
9    // We can remove the "pair" field for that? Since it's possible to find it using the mapping?
10    pub price: u64,
11    pub decimals: u32,
12    pub timestamp: u64,
13}
14
15impl TryInto<Word> for Entry {
16    type Error = anyhow::Error;
17
18    fn try_into(self) -> Result<Word, Self::Error> {
19        Ok([
20            Felt::try_from(self.pair)?,
21            Felt::new(self.price),
22            Felt::new(self.decimals as u64),
23            Felt::new(self.timestamp),
24        ])
25    }
26}
27
28impl From<Word> for Entry {
29    fn from(word: Word) -> Self {
30        let [pair_felt, price_felt, decimals_felt, timestamp_felt] = word;
31
32        // Convert pair from Felt
33        let pair = Pair::from(pair_felt);
34
35        // Extract other fields
36        let price = price_felt.as_int();
37        let decimals = decimals_felt.as_int() as u32;
38        let timestamp = timestamp_felt.as_int();
39
40        Entry {
41            pair,
42            price,
43            decimals,
44            timestamp,
45        }
46    }
47}