penumbra_sdk_auction/auction/dutch/actions/
view.rs

1use crate::auction::{
2    dutch::{
3        actions::{ActionDutchAuctionSchedule, ActionDutchAuctionWithdraw},
4        asset::Metadata,
5    },
6    id::AuctionId,
7};
8use anyhow::anyhow;
9use penumbra_sdk_asset::ValueView;
10use penumbra_sdk_proto::{core::component::auction::v1 as pb, DomainType};
11use serde::{Deserialize, Serialize};
12
13/* Domain type definitions */
14#[derive(Debug, Clone, Serialize, Deserialize)]
15#[serde(
16    try_from = "pb::ActionDutchAuctionScheduleView",
17    into = "pb::ActionDutchAuctionScheduleView"
18)]
19pub struct ActionDutchAuctionScheduleView {
20    pub action: ActionDutchAuctionSchedule,
21    pub auction_id: AuctionId,
22    pub input_metadata: Option<Metadata>,
23    pub output_metadata: Option<Metadata>,
24}
25
26#[derive(Debug, Clone, Serialize, Deserialize)]
27#[serde(
28    try_from = "pb::ActionDutchAuctionWithdrawView",
29    into = "pb::ActionDutchAuctionWithdrawView"
30)]
31pub struct ActionDutchAuctionWithdrawView {
32    pub action: ActionDutchAuctionWithdraw,
33    // A sequence of values that sum together to the provided
34    // reserves commitment.
35    pub reserves: Vec<ValueView>,
36}
37
38/* Conversion back to an action */
39
40impl From<ActionDutchAuctionScheduleView> for ActionDutchAuctionSchedule {
41    fn from(value: ActionDutchAuctionScheduleView) -> Self {
42        value.action
43    }
44}
45
46impl From<ActionDutchAuctionWithdrawView> for ActionDutchAuctionWithdraw {
47    fn from(value: ActionDutchAuctionWithdrawView) -> Self {
48        value.action
49    }
50}
51
52/* Protobuf impls */
53impl DomainType for ActionDutchAuctionScheduleView {
54    type Proto = pb::ActionDutchAuctionScheduleView;
55}
56
57impl From<ActionDutchAuctionScheduleView> for pb::ActionDutchAuctionScheduleView {
58    fn from(domain: ActionDutchAuctionScheduleView) -> Self {
59        pb::ActionDutchAuctionScheduleView {
60            action: Some(domain.action.into()),
61            auction_id: Some(domain.auction_id.into()),
62            input_metadata: domain.input_metadata.map(Into::into),
63            output_metadata: domain.output_metadata.map(Into::into),
64        }
65    }
66}
67
68impl TryFrom<pb::ActionDutchAuctionScheduleView> for ActionDutchAuctionScheduleView {
69    type Error = anyhow::Error;
70
71    fn try_from(msg: pb::ActionDutchAuctionScheduleView) -> Result<Self, Self::Error> {
72        Ok(ActionDutchAuctionScheduleView {
73            action: msg
74                .action
75                .ok_or_else(|| {
76                    anyhow!("ActionDutchAuctionScheduleView message is missing an action")
77                })?
78                .try_into()?,
79            auction_id: msg
80                .auction_id
81                .ok_or_else(|| {
82                    anyhow!("ActionDutchAuctionScheduleView message is missing an auction_id")
83                })?
84                .try_into()?,
85            input_metadata: msg
86                .input_metadata
87                .map(|input| input.try_into())
88                .transpose()?,
89            output_metadata: msg
90                .output_metadata
91                .map(|output| output.try_into())
92                .transpose()?,
93        })
94    }
95}
96/* Protobuf impls */
97impl DomainType for ActionDutchAuctionWithdrawView {
98    type Proto = pb::ActionDutchAuctionWithdrawView;
99}
100
101impl From<ActionDutchAuctionWithdrawView> for pb::ActionDutchAuctionWithdrawView {
102    fn from(domain: ActionDutchAuctionWithdrawView) -> Self {
103        pb::ActionDutchAuctionWithdrawView {
104            action: Some(domain.action.into()),
105            reserves: domain
106                .reserves
107                .into_iter()
108                .map(Into::into)
109                .collect::<Vec<_>>(),
110        }
111    }
112}
113
114impl TryFrom<pb::ActionDutchAuctionWithdrawView> for ActionDutchAuctionWithdrawView {
115    type Error = anyhow::Error;
116
117    fn try_from(msg: pb::ActionDutchAuctionWithdrawView) -> Result<Self, Self::Error> {
118        Ok(ActionDutchAuctionWithdrawView {
119            action: msg
120                .action
121                .ok_or_else(|| {
122                    anyhow!("ActionDutchAuctionWithdrawView message is missing an action")
123                })?
124                .try_into()?,
125            reserves: msg
126                .reserves
127                .into_iter()
128                .map(TryInto::try_into)
129                .collect::<Result<_, _>>()?,
130        })
131    }
132}