penumbra_sdk_dex/swap_claim/
view.rs

1use penumbra_sdk_proto::{penumbra::core::component::dex::v1 as pbd, DomainType};
2use penumbra_sdk_shielded_pool::NoteView;
3use penumbra_sdk_txhash::TransactionId;
4use serde::{Deserialize, Serialize};
5
6use super::SwapClaim;
7
8#[derive(Clone, Debug, Serialize, Deserialize)]
9#[serde(try_from = "pbd::SwapClaimView", into = "pbd::SwapClaimView")]
10#[allow(clippy::large_enum_variant)]
11pub enum SwapClaimView {
12    Visible {
13        swap_claim: SwapClaim,
14        output_1: NoteView,
15        output_2: NoteView,
16        swap_tx: Option<TransactionId>,
17    },
18    Opaque {
19        swap_claim: SwapClaim,
20    },
21}
22
23impl DomainType for SwapClaimView {
24    type Proto = pbd::SwapClaimView;
25}
26
27impl TryFrom<pbd::SwapClaimView> for SwapClaimView {
28    type Error = anyhow::Error;
29
30    fn try_from(v: pbd::SwapClaimView) -> Result<Self, Self::Error> {
31        match v
32            .swap_claim_view
33            .ok_or_else(|| anyhow::anyhow!("missing swap field"))?
34        {
35            pbd::swap_claim_view::SwapClaimView::Visible(x) => Ok(SwapClaimView::Visible {
36                swap_claim: x
37                    .swap_claim
38                    .ok_or_else(|| anyhow::anyhow!("missing swap claim field"))?
39                    .try_into()?,
40                output_1: x
41                    .output_1
42                    .ok_or_else(|| anyhow::anyhow!("missing output_1 field"))?
43                    .try_into()?,
44                output_2: x
45                    .output_2
46                    .ok_or_else(|| anyhow::anyhow!("missing output_2 field"))?
47                    .try_into()?,
48                swap_tx: x.swap_tx.map(TryInto::try_into).transpose()?,
49            }),
50            pbd::swap_claim_view::SwapClaimView::Opaque(x) => Ok(SwapClaimView::Opaque {
51                swap_claim: x
52                    .swap_claim
53                    .ok_or_else(|| anyhow::anyhow!("missing swap claim field"))?
54                    .try_into()?,
55            }),
56        }
57    }
58}
59
60impl From<SwapClaimView> for pbd::SwapClaimView {
61    fn from(v: SwapClaimView) -> Self {
62        use pbd::swap_claim_view as scv;
63        match v {
64            SwapClaimView::Visible {
65                swap_claim,
66                output_1,
67                output_2,
68                swap_tx,
69            } => Self {
70                swap_claim_view: Some(scv::SwapClaimView::Visible(scv::Visible {
71                    swap_claim: Some(swap_claim.into()),
72                    output_1: Some(output_1.into()),
73                    output_2: Some(output_2.into()),
74                    swap_tx: swap_tx.map(Into::into),
75                })),
76            },
77            SwapClaimView::Opaque { swap_claim } => Self {
78                swap_claim_view: Some(scv::SwapClaimView::Opaque(scv::Opaque {
79                    swap_claim: Some(swap_claim.into()),
80                })),
81            },
82        }
83    }
84}
85
86impl From<SwapClaimView> for SwapClaim {
87    fn from(v: SwapClaimView) -> Self {
88        match v {
89            SwapClaimView::Visible {
90                swap_claim,
91                output_1: _,
92                output_2: _,
93                swap_tx: _,
94            } => swap_claim,
95            SwapClaimView::Opaque { swap_claim } => swap_claim,
96        }
97    }
98}