penumbra_sdk_fee/
component.rs

1mod fee_pay;
2pub mod rpc;
3mod view;
4
5use std::sync::Arc;
6
7use crate::{event::EventBlockFees, genesis, Fee};
8use async_trait::async_trait;
9use cnidarium::StateWrite;
10use cnidarium_component::Component;
11use penumbra_sdk_proto::state::StateWriteProto as _;
12use penumbra_sdk_proto::DomainType as _;
13use tendermint::abci;
14use tracing::instrument;
15
16pub use fee_pay::FeePay;
17pub use view::{StateReadExt, StateWriteExt};
18
19// Fee component
20pub struct FeeComponent {}
21
22#[async_trait]
23impl Component for FeeComponent {
24    type AppState = genesis::Content;
25
26    #[instrument(name = "fee", skip(state, app_state))]
27    async fn init_chain<S: StateWrite>(mut state: S, app_state: Option<&Self::AppState>) {
28        match app_state {
29            Some(genesis) => {
30                state.put_fee_params(genesis.fee_params.clone());
31            }
32            None => { /* perform upgrade specific check */ }
33        }
34    }
35
36    #[instrument(name = "fee", skip(_state, _begin_block))]
37    async fn begin_block<S: StateWrite + 'static>(
38        _state: &mut Arc<S>,
39        _begin_block: &abci::request::BeginBlock,
40    ) {
41    }
42
43    #[instrument(name = "fee", skip(state, _end_block))]
44    async fn end_block<S: StateWrite + 'static>(
45        state: &mut Arc<S>,
46        _end_block: &abci::request::EndBlock,
47    ) {
48        let state_ref = Arc::get_mut(state).expect("unique ref in end_block");
49        // Grab the total fees and use them to emit an event.
50        let fees = state_ref.accumulated_base_fees_and_tips();
51
52        let (swapped_base, swapped_tip) = fees
53            .get(&penumbra_sdk_asset::STAKING_TOKEN_ASSET_ID)
54            .cloned()
55            .unwrap_or_default();
56
57        let swapped_total = swapped_base + swapped_tip;
58
59        state_ref.record_proto(
60            EventBlockFees {
61                swapped_fee_total: Fee::from_staking_token_amount(swapped_total),
62                swapped_base_fee_total: Fee::from_staking_token_amount(swapped_base),
63                swapped_tip_total: Fee::from_staking_token_amount(swapped_tip),
64            }
65            .to_proto(),
66        );
67    }
68
69    #[instrument(name = "fee", skip(_state))]
70    async fn end_epoch<S: StateWrite + 'static>(_state: &mut Arc<S>) -> anyhow::Result<()> {
71        Ok(())
72    }
73}