penumbra_sdk_auction/auction/dutch/actions/
end.rs

1use anyhow::anyhow;
2use penumbra_sdk_asset::{Balance, Value};
3use penumbra_sdk_proto::{core::component::auction::v1 as pb, DomainType};
4use penumbra_sdk_txhash::{EffectHash, EffectingData};
5use serde::{Deserialize, Serialize};
6
7use crate::auction::{id::AuctionId, AuctionNft};
8
9#[derive(Debug, Clone, Serialize, Deserialize)]
10#[serde(
11    try_from = "pb::ActionDutchAuctionEnd",
12    into = "pb::ActionDutchAuctionEnd"
13)]
14pub struct ActionDutchAuctionEnd {
15    pub auction_id: AuctionId,
16}
17
18impl ActionDutchAuctionEnd {
19    /// Compute the value balance for this action
20    ///
21    /// # Diagram
22    ///
23    ///  ┌────────────────────┬──────────────────────┐
24    ///  │      Burn (-)      │       Mint (+)       │
25    ///  ├────────────────────┼──────────────────────┤
26    ///  │ opened auction nft │  closed auction nft  │
27    ///  └────────────────────┴──────────────────────┘
28    pub fn balance(&self) -> Balance {
29        let start_auction = Value {
30            amount: 1u128.into(),
31            asset_id: AuctionNft::new(self.auction_id, 0u64).asset_id(),
32        };
33
34        let end_auction = Value {
35            amount: 1u128.into(),
36            asset_id: AuctionNft::new(self.auction_id, 1u64).asset_id(),
37        };
38
39        Balance::from(end_auction) - Balance::from(start_auction)
40    }
41}
42
43/* Effect hash */
44impl EffectingData for ActionDutchAuctionEnd {
45    fn effect_hash(&self) -> EffectHash {
46        EffectHash::from_proto_effecting_data(&self.to_proto())
47    }
48}
49/* Protobuf impls */
50impl DomainType for ActionDutchAuctionEnd {
51    type Proto = pb::ActionDutchAuctionEnd;
52}
53
54impl From<ActionDutchAuctionEnd> for pb::ActionDutchAuctionEnd {
55    fn from(domain: ActionDutchAuctionEnd) -> Self {
56        pb::ActionDutchAuctionEnd {
57            auction_id: Some(domain.auction_id.into()),
58        }
59    }
60}
61
62impl TryFrom<pb::ActionDutchAuctionEnd> for ActionDutchAuctionEnd {
63    type Error = anyhow::Error;
64
65    fn try_from(msg: pb::ActionDutchAuctionEnd) -> Result<Self, Self::Error> {
66        Ok(ActionDutchAuctionEnd {
67            auction_id: msg
68                .auction_id
69                .ok_or_else(|| anyhow!("ActionDutchAuctionEnd message is missing an auction_id"))?
70                .try_into()?,
71        })
72    }
73}