penumbra_sdk_auction/component/
auction_store.rs

1use anyhow::Result;
2use async_trait::async_trait;
3use cnidarium::StateRead;
4use pbjson_types::Any;
5use penumbra_sdk_proto::core::component::auction::v1 as pb;
6use penumbra_sdk_proto::DomainType;
7use penumbra_sdk_proto::Name;
8use penumbra_sdk_proto::StateReadProto;
9
10use crate::{
11    auction::{dutch::DutchAuction, id::AuctionId},
12    state_key,
13};
14
15/// Provide access to internal auction data.
16#[async_trait]
17pub trait AuctionStoreRead: StateRead {
18    /// Returns whether the supplied `auction_id` exists in the chain state.
19    async fn auction_id_exists(&self, auction_id: AuctionId) -> bool {
20        self.get_raw_auction(auction_id).await.is_some()
21    }
22
23    /// Fetch a [`DutchAuction`] from storage, returning `None` if none
24    /// were found with the provided identifier.
25    ///
26    /// # Errors
27    /// This method returns an error if the auction state associated with the
28    /// specified `auction_id` is *not* of type `DutchAuction`.
29    async fn get_dutch_auction_by_id(&self, auction_id: AuctionId) -> Result<Option<DutchAuction>> {
30        let Some(any_auction) = self.get_raw_auction(auction_id).await else {
31            return Ok(None);
32        };
33
34        let dutch_auction_type_str = pb::DutchAuction::type_url();
35
36        anyhow::ensure!(
37            any_auction.type_url == dutch_auction_type_str,
38            "error deserializing auction state, expected type to be {}, but got: {}",
39            dutch_auction_type_str,
40            any_auction.type_url
41        );
42
43        Ok(Some(DutchAuction::decode(any_auction.value.as_ref())?))
44    }
45
46    /// Returns raw auction data if found under the specified `auction_id`,
47    /// and `None` otherwise
48    async fn get_raw_auction(&self, auction_id: AuctionId) -> Option<Any> {
49        self.get_proto(&state_key::auction_store::by_id(auction_id))
50            .await
51            .expect("no storage errors")
52    }
53}
54
55impl<T: StateRead + ?Sized> AuctionStoreRead for T {}
56
57#[cfg(test)]
58mod tests {}