penumbra_sdk_auction/auction/dutch/actions/
view.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
use crate::auction::{
    dutch::{
        actions::{ActionDutchAuctionSchedule, ActionDutchAuctionWithdraw},
        asset::Metadata,
    },
    id::AuctionId,
};
use anyhow::anyhow;
use penumbra_sdk_asset::ValueView;
use penumbra_sdk_proto::{core::component::auction::v1 as pb, DomainType};
use serde::{Deserialize, Serialize};

/* Domain type definitions */
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(
    try_from = "pb::ActionDutchAuctionScheduleView",
    into = "pb::ActionDutchAuctionScheduleView"
)]
pub struct ActionDutchAuctionScheduleView {
    pub action: ActionDutchAuctionSchedule,
    pub auction_id: AuctionId,
    pub input_metadata: Option<Metadata>,
    pub output_metadata: Option<Metadata>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(
    try_from = "pb::ActionDutchAuctionWithdrawView",
    into = "pb::ActionDutchAuctionWithdrawView"
)]
pub struct ActionDutchAuctionWithdrawView {
    pub action: ActionDutchAuctionWithdraw,
    // A sequence of values that sum together to the provided
    // reserves commitment.
    pub reserves: Vec<ValueView>,
}

/* Conversion back to an action */

impl From<ActionDutchAuctionScheduleView> for ActionDutchAuctionSchedule {
    fn from(value: ActionDutchAuctionScheduleView) -> Self {
        value.action
    }
}

impl From<ActionDutchAuctionWithdrawView> for ActionDutchAuctionWithdraw {
    fn from(value: ActionDutchAuctionWithdrawView) -> Self {
        value.action
    }
}

/* Protobuf impls */
impl DomainType for ActionDutchAuctionScheduleView {
    type Proto = pb::ActionDutchAuctionScheduleView;
}

impl From<ActionDutchAuctionScheduleView> for pb::ActionDutchAuctionScheduleView {
    fn from(domain: ActionDutchAuctionScheduleView) -> Self {
        pb::ActionDutchAuctionScheduleView {
            action: Some(domain.action.into()),
            auction_id: Some(domain.auction_id.into()),
            input_metadata: domain.input_metadata.map(Into::into),
            output_metadata: domain.output_metadata.map(Into::into),
        }
    }
}

impl TryFrom<pb::ActionDutchAuctionScheduleView> for ActionDutchAuctionScheduleView {
    type Error = anyhow::Error;

    fn try_from(msg: pb::ActionDutchAuctionScheduleView) -> Result<Self, Self::Error> {
        Ok(ActionDutchAuctionScheduleView {
            action: msg
                .action
                .ok_or_else(|| {
                    anyhow!("ActionDutchAuctionScheduleView message is missing an action")
                })?
                .try_into()?,
            auction_id: msg
                .auction_id
                .ok_or_else(|| {
                    anyhow!("ActionDutchAuctionScheduleView message is missing an auction_id")
                })?
                .try_into()?,
            input_metadata: msg
                .input_metadata
                .map(|input| input.try_into())
                .transpose()?,
            output_metadata: msg
                .output_metadata
                .map(|output| output.try_into())
                .transpose()?,
        })
    }
}
/* Protobuf impls */
impl DomainType for ActionDutchAuctionWithdrawView {
    type Proto = pb::ActionDutchAuctionWithdrawView;
}

impl From<ActionDutchAuctionWithdrawView> for pb::ActionDutchAuctionWithdrawView {
    fn from(domain: ActionDutchAuctionWithdrawView) -> Self {
        pb::ActionDutchAuctionWithdrawView {
            action: Some(domain.action.into()),
            reserves: domain
                .reserves
                .into_iter()
                .map(Into::into)
                .collect::<Vec<_>>(),
        }
    }
}

impl TryFrom<pb::ActionDutchAuctionWithdrawView> for ActionDutchAuctionWithdrawView {
    type Error = anyhow::Error;

    fn try_from(msg: pb::ActionDutchAuctionWithdrawView) -> Result<Self, Self::Error> {
        Ok(ActionDutchAuctionWithdrawView {
            action: msg
                .action
                .ok_or_else(|| {
                    anyhow!("ActionDutchAuctionWithdrawView message is missing an action")
                })?
                .try_into()?,
            reserves: msg
                .reserves
                .into_iter()
                .map(TryInto::try_into)
                .collect::<Result<_, _>>()?,
        })
    }
}