Skip to main content

fedimint_mintv2_client/
ecash.rs

1use fedimint_core::Amount;
2use fedimint_core::config::FederationId;
3use fedimint_core::encoding::{Decodable, Encodable};
4
5use crate::SpendableNote;
6
7#[derive(Clone, Debug, Encodable, Decodable)]
8pub struct ECash(Vec<ECashField>);
9
10#[derive(Clone, Debug, Decodable, Encodable)]
11enum ECashField {
12    Mint(FederationId),
13    Note(SpendableNote),
14    #[encodable_default]
15    Default {
16        variant: u64,
17        bytes: Vec<u8>,
18    },
19}
20
21impl ECash {
22    pub fn new(mint: FederationId, notes: Vec<SpendableNote>) -> Self {
23        Self(
24            std::iter::once(ECashField::Mint(mint))
25                .chain(notes.into_iter().map(ECashField::Note))
26                .collect(),
27        )
28    }
29
30    pub fn amount(&self) -> Amount {
31        self.0
32            .iter()
33            .filter_map(|field| match field {
34                ECashField::Note(note) => Some(note.amount()),
35                _ => None,
36            })
37            .sum()
38    }
39
40    pub fn mint(&self) -> Option<FederationId> {
41        self.0.iter().find_map(|field| match field {
42            ECashField::Mint(mint) => Some(*mint),
43            _ => None,
44        })
45    }
46
47    pub fn notes(&self) -> Vec<SpendableNote> {
48        self.0
49            .iter()
50            .filter_map(|field| match field {
51                ECashField::Note(note) => Some(note.clone()),
52                _ => None,
53            })
54            .collect()
55    }
56}