penumbra_sdk_auction/auction/dutch/actions/
end.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
use anyhow::anyhow;
use penumbra_sdk_asset::{Balance, Value};
use penumbra_sdk_proto::{core::component::auction::v1 as pb, DomainType};
use penumbra_sdk_txhash::{EffectHash, EffectingData};
use serde::{Deserialize, Serialize};

use crate::auction::{id::AuctionId, AuctionNft};

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(
    try_from = "pb::ActionDutchAuctionEnd",
    into = "pb::ActionDutchAuctionEnd"
)]
pub struct ActionDutchAuctionEnd {
    pub auction_id: AuctionId,
}

impl ActionDutchAuctionEnd {
    /// Compute the value balance for this action
    ///
    /// # Diagram
    ///
    ///  ┌────────────────────┬──────────────────────┐
    ///  │      Burn (-)      │       Mint (+)       │
    ///  ├────────────────────┼──────────────────────┤
    ///  │ opened auction nft │  closed auction nft  │
    ///  └────────────────────┴──────────────────────┘
    pub fn balance(&self) -> Balance {
        let start_auction = Value {
            amount: 1u128.into(),
            asset_id: AuctionNft::new(self.auction_id, 0u64).asset_id(),
        };

        let end_auction = Value {
            amount: 1u128.into(),
            asset_id: AuctionNft::new(self.auction_id, 1u64).asset_id(),
        };

        Balance::from(end_auction) - Balance::from(start_auction)
    }
}

/* Effect hash */
impl EffectingData for ActionDutchAuctionEnd {
    fn effect_hash(&self) -> EffectHash {
        EffectHash::from_proto_effecting_data(&self.to_proto())
    }
}
/* Protobuf impls */
impl DomainType for ActionDutchAuctionEnd {
    type Proto = pb::ActionDutchAuctionEnd;
}

impl From<ActionDutchAuctionEnd> for pb::ActionDutchAuctionEnd {
    fn from(domain: ActionDutchAuctionEnd) -> Self {
        pb::ActionDutchAuctionEnd {
            auction_id: Some(domain.auction_id.into()),
        }
    }
}

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

    fn try_from(msg: pb::ActionDutchAuctionEnd) -> Result<Self, Self::Error> {
        Ok(ActionDutchAuctionEnd {
            auction_id: msg
                .auction_id
                .ok_or_else(|| anyhow!("ActionDutchAuctionEnd message is missing an auction_id"))?
                .try_into()?,
        })
    }
}