Skip to content

Commit

Permalink
Naive conversion - it compiles 🤷
Browse files Browse the repository at this point in the history
  • Loading branch information
Victor-N-Suadicani committed Jun 13, 2024
1 parent a85bc12 commit 7d4c36d
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,46 @@ impl AccountStakingInfo {
}
}

/// The status of a cooldown. When stake is removed from a baker or delegator
/// (from protocol version 7) it first enters the pre-pre-cooldown state.
/// The next time the stake snaphot is taken (at the epoch transition before
/// a payday) it enters the pre-cooldown state. At the subsequent payday, it
/// enters the cooldown state. At the payday after the end of the cooldown
/// period, the stake is finally released.
#[derive(SerdeSerialize, SerdeDeserialize, Debug, PartialEq)]
pub enum CooldownStatus {
/// The amount is in cooldown and will expire at the specified time,
/// becoming available at the subsequent pay day.
Cooldown,

/// The amount will enter cooldown at the next pay day. The specified
/// end time is projected to be the end of the cooldown period,
/// but the actual end time will be determined at the payday,
/// and may be different if the global cooldown period changes.
PreCooldown,

/// The amount will enter pre-cooldown at the next snapshot epoch (i.e.
/// the epoch transition before a pay day transition). As with
/// pre-cooldown, the specified end time is projected, but the
/// actual end time will be determined later.
PrePreCooldown,
}

#[derive(SerdeSerialize, SerdeDeserialize, Debug, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct Cooldown {
/// The time in milliseconds since the Unix epoch when the cooldown period
/// ends.
pub end_time: Timestamp,

/// The amount that is in cooldown and set to be released at the end of the
/// cooldown period.
pub amount: Amount,

/// The status of the cooldown.
pub status: CooldownStatus,
}

#[derive(SerdeSerialize, SerdeDeserialize, Debug, PartialEq)]
#[serde(rename_all = "camelCase")]
/// Account information exposed via the node's API. This is always the state of
Expand Down Expand Up @@ -327,6 +367,18 @@ pub struct AccountInfo {
pub account_stake: Option<AccountStakingInfo>,
/// Canonical address of the account.
pub account_address: AccountAddress,

/// The stake on the account that is in cooldown.
/// There can be multiple amounts in cooldown that expire at different
/// times.
pub cooldowns: Vec<Cooldown>,

/// The available (unencrypted) balance of the account (i.e. that can be
/// transferred or used to pay for transactions). This is the balance
/// minus the locked amount. The locked amount is the maximum of the
/// amount in the release schedule and the total amount that is actively
/// staked or in cooldown (inactive stake).
pub available_balance: Amount,
}

impl From<&AccountInfo> for AccountAccessStructure {
Expand Down
33 changes: 33 additions & 0 deletions src/v2/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use concordium_base::{
},
updates,
};
use cooldown::CooldownStatus;
use std::collections::{BTreeMap, BTreeSet};

fn consume<A: Deserial>(bytes: &[u8]) -> Result<A, tonic::Status> {
Expand Down Expand Up @@ -899,6 +900,28 @@ impl TryFrom<DelegatorRewardPeriodInfo> for super::types::DelegatorRewardPeriodI
}
}

impl From<CooldownStatus> for super::types::CooldownStatus {
fn from(cds: CooldownStatus) -> Self {
match cds {
CooldownStatus::Cooldown => Self::Cooldown,
CooldownStatus::PreCooldown => Self::PreCooldown,
CooldownStatus::PrePreCooldown => Self::PrePreCooldown,
}
}
}

impl TryFrom<Cooldown> for super::types::Cooldown {
type Error = tonic::Status;

fn try_from(cd: Cooldown) -> Result<Self, Self::Error> {
Ok(Self {
status: cd.status().into(),
end_time: cd.end_time.require()?.into(),
amount: cd.amount.require()?.into(),
})
}
}

impl TryFrom<AccountInfo> for super::types::AccountInfo {
type Error = tonic::Status;

Expand All @@ -914,6 +937,8 @@ impl TryFrom<AccountInfo> for super::types::AccountInfo {
index,
stake,
address,
cooldowns,
available_balance,
} = value;
let account_nonce = sequence_number.require()?.into();
let account_amount = amount.require()?.into();
Expand All @@ -927,6 +952,12 @@ impl TryFrom<AccountInfo> for super::types::AccountInfo {
None => None,
};
let account_address = address.require()?.try_into()?;
let mut cds = Vec::with_capacity(cooldowns.len());
for cooldown in cooldowns.into_iter() {
cds.push(cooldown.try_into()?)
}
let cooldowns = cds;
let available_balance = available_balance.require()?.into();
Ok(Self {
account_nonce,
account_amount,
Expand All @@ -947,6 +978,8 @@ impl TryFrom<AccountInfo> for super::types::AccountInfo {
account_index,
account_stake,
account_address,
cooldowns,
available_balance,
})
}
}
Expand Down

0 comments on commit 7d4c36d

Please sign in to comment.