penumbra_stake/
delegate.rsuse penumbra_asset::{Balance, Value, STAKING_TOKEN_ASSET_ID};
use penumbra_num::Amount;
use penumbra_proto::{penumbra::core::component::stake::v1 as pb, DomainType};
use penumbra_txhash::{EffectHash, EffectingData};
use serde::{Deserialize, Serialize};
use crate::{DelegationToken, IdentityKey};
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(try_from = "pb::Delegate", into = "pb::Delegate")]
pub struct Delegate {
pub validator_identity: IdentityKey,
pub epoch_index: u64,
pub unbonded_amount: Amount,
pub delegation_amount: Amount,
}
impl EffectingData for Delegate {
fn effect_hash(&self) -> EffectHash {
EffectHash::from_proto_effecting_data(&self.to_proto())
}
}
impl Delegate {
pub fn balance(&self) -> Balance {
let stake: Balance = self.unbonded_value().into();
let delegation: Balance = self.delegation_value().into();
delegation - stake
}
pub fn delegation_value(&self) -> Value {
Value {
amount: self.delegation_amount,
asset_id: DelegationToken::new(self.validator_identity.clone()).id(),
}
}
pub fn unbonded_value(&self) -> Value {
Value {
amount: self.unbonded_amount,
asset_id: STAKING_TOKEN_ASSET_ID.clone(),
}
}
}
impl DomainType for Delegate {
type Proto = pb::Delegate;
}
impl From<Delegate> for pb::Delegate {
fn from(d: Delegate) -> Self {
pb::Delegate {
validator_identity: Some(d.validator_identity.into()),
epoch_index: d.epoch_index,
unbonded_amount: Some(d.unbonded_amount.into()),
delegation_amount: Some(d.delegation_amount.into()),
}
}
}
impl TryFrom<pb::Delegate> for Delegate {
type Error = anyhow::Error;
fn try_from(d: pb::Delegate) -> Result<Self, Self::Error> {
Ok(Self {
validator_identity: d
.validator_identity
.ok_or_else(|| anyhow::anyhow!("missing validator identity"))?
.try_into()?,
epoch_index: d.epoch_index,
unbonded_amount: d
.unbonded_amount
.ok_or_else(|| anyhow::anyhow!("missing unbonded amount"))?
.try_into()?,
delegation_amount: d
.delegation_amount
.ok_or_else(|| anyhow::anyhow!("missing delegation amount"))?
.try_into()?,
})
}
}