penumbra_governance/proposal_withdraw/
action.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
74
75
use serde::{Deserialize, Serialize};

use penumbra_asset::{Balance, Value};
use penumbra_num::Amount;
use penumbra_proto::{penumbra::core::component::governance::v1 as pb, DomainType};
use penumbra_txhash::{EffectHash, EffectingData};

use crate::ProposalNft;

/// A withdrawal of a proposal.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(try_from = "pb::ProposalWithdraw", into = "pb::ProposalWithdraw")]
pub struct ProposalWithdraw {
    /// The proposal ID to withdraw.
    pub proposal: u64,
    // The reason the proposal was withdrawn.
    pub reason: String,
}

impl EffectingData for ProposalWithdraw {
    fn effect_hash(&self) -> EffectHash {
        EffectHash::from_proto_effecting_data(&self.to_proto())
    }
}

impl From<ProposalWithdraw> for pb::ProposalWithdraw {
    fn from(value: ProposalWithdraw) -> pb::ProposalWithdraw {
        pb::ProposalWithdraw {
            proposal: value.proposal,
            reason: value.reason,
        }
    }
}

impl ProposalWithdraw {
    /// Compute a commitment to the value contributed to a transaction by this proposal withdrawal.
    pub fn balance(&self) -> Balance {
        let voting_proposal_nft = self.voting_proposal_nft_value();
        let withdrawn_proposal_nft = self.withdrawn_proposal_nft();

        // Proposal withdrawals consume the submitted proposal and produce a withdrawn proposal:
        Balance::from(withdrawn_proposal_nft) - Balance::from(voting_proposal_nft)
    }

    /// Returns the [`Value`] of the proposal NFT.
    fn voting_proposal_nft_value(&self) -> Value {
        Value {
            amount: Amount::from(1u64),
            asset_id: ProposalNft::deposit(self.proposal).denom().into(),
        }
    }

    /// Returns a withdrawal NFT.
    fn withdrawn_proposal_nft(&self) -> Value {
        Value {
            amount: Amount::from(1u64),
            asset_id: ProposalNft::unbonding_deposit(self.proposal).denom().into(),
        }
    }
}

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

    fn try_from(msg: pb::ProposalWithdraw) -> Result<Self, Self::Error> {
        Ok(ProposalWithdraw {
            proposal: msg.proposal,
            reason: msg.reason,
        })
    }
}

impl DomainType for ProposalWithdraw {
    type Proto = pb::ProposalWithdraw;
}