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
use anyhow::Context;
use penumbra_asset::{Value, STAKING_TOKEN_ASSET_ID};
use penumbra_proto::{penumbra::core::component::community_pool::v1 as pb, DomainType};
use serde::{Deserialize, Serialize};

use crate::params::CommunityPoolParameters;

#[derive(Deserialize, Serialize, Debug, Clone)]
#[serde(try_from = "pb::GenesisContent", into = "pb::GenesisContent")]
pub struct Content {
    /// The initial configuration parameters for the Community Pool component.
    pub community_pool_params: CommunityPoolParameters,
    /// The initial balance of the Community Pool.
    pub initial_balance: Value,
}

impl From<Content> for pb::GenesisContent {
    fn from(genesis: Content) -> Self {
        pb::GenesisContent {
            community_pool_params: Some(genesis.community_pool_params.into()),
            initial_balance: Some(genesis.initial_balance.into()),
        }
    }
}

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

    fn try_from(msg: pb::GenesisContent) -> Result<Self, Self::Error> {
        Ok(Content {
            initial_balance: msg
                .initial_balance
                .context("Initial balance not present in protobuf message")?
                .try_into()?,
            community_pool_params: msg
                .community_pool_params
                .context("Community Pool params not present in protobuf message")?
                .try_into()?,
        })
    }
}

impl DomainType for Content {
    type Proto = pb::GenesisContent;
}

impl Default for Content {
    fn default() -> Self {
        Content {
            community_pool_params: CommunityPoolParameters::default(),
            initial_balance: Value {
                amount: 0u128.into(),
                asset_id: *STAKING_TOKEN_ASSET_ID,
            },
        }
    }
}