penumbra_sdk_distributions/component/
view.rs

1use async_trait::async_trait;
2
3use crate::{component::state_key, params::DistributionsParameters};
4use anyhow::Result;
5use cnidarium::{StateRead, StateWrite};
6use penumbra_sdk_num::Amount;
7use penumbra_sdk_proto::{StateReadProto, StateWriteProto};
8
9#[async_trait]
10pub trait StateReadExt: StateRead {
11    /// Gets the distributions module chain parameters from the JMT.
12    async fn get_distributions_params(&self) -> Result<DistributionsParameters> {
13        self.get(state_key::distributions_parameters())
14            .await?
15            .ok_or_else(|| anyhow::anyhow!("Missing DistributionsParameters"))
16    }
17
18    // Get the total amount of staking tokens issued for this epoch.
19    fn get_staking_token_issuance_for_epoch(&self) -> Option<Amount> {
20        self.object_get(&state_key::staking_token_issuance_for_epoch())
21    }
22
23    // Get the total amount of LQT rewards issued for this epoch.
24    async fn get_lqt_reward_issuance_for_epoch(&self, epoch_index: u64) -> Option<Amount> {
25        let key = state_key::lqt::v1::budget::for_epoch(epoch_index);
26
27        self.nonverifiable_get(&key).await.unwrap_or_else(|_| {
28            tracing::error!("LQT issuance does not exist for epoch");
29            None
30        })
31    }
32}
33
34impl<T: StateRead + ?Sized> StateReadExt for T {}
35#[async_trait]
36
37pub trait StateWriteExt: StateWrite + StateReadExt {
38    /// Set the total amount of staking tokens issued for this epoch.
39    fn set_staking_token_issuance_for_epoch(&mut self, issuance: Amount) {
40        self.object_put(state_key::staking_token_issuance_for_epoch(), issuance);
41    }
42
43    /// Set the Distributions parameters in the JMT.
44    fn put_distributions_params(&mut self, params: DistributionsParameters) {
45        self.put(state_key::distributions_parameters().into(), params)
46    }
47
48    /// Set the total amount of LQT rewards issued for this epoch.
49    fn set_lqt_reward_issuance_for_epoch(&mut self, epoch_index: u64, issuance: Amount) {
50        self.nonverifiable_put(
51            state_key::lqt::v1::budget::for_epoch(epoch_index).into(),
52            issuance,
53        );
54    }
55}
56impl<T: StateWrite + ?Sized> StateWriteExt for T {}