Skip to content

Commit

Permalink
new lazy functions in tx_prelude
Browse files Browse the repository at this point in the history
  • Loading branch information
brentstone committed Dec 9, 2022
1 parent 58dd251 commit affbf70
Showing 1 changed file with 90 additions and 2 deletions.
92 changes: 90 additions & 2 deletions tx_prelude/src/proof_of_stake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ use namada_proof_of_stake::storage::{
Unbonds, ValidatorConsensusKeys, ValidatorDeltas, ValidatorSets,
};
use namada_proof_of_stake::types::{CommissionRates, ValidatorStates};
use namada_proof_of_stake::{
become_validator_new, bond_tokens_new,
change_validator_commission_rate_new, read_pos_params, unbond_tokens_new,
withdraw_tokens_new,
};
pub use namada_proof_of_stake::{
bond_tokens_new, epoched, parameters, types, PosActions as PosWrite,
PosReadOnly as PosRead,
epoched, parameters, types, PosActions as PosWrite, PosReadOnly as PosRead,
};
use rust_decimal::Decimal;

Expand Down Expand Up @@ -79,6 +83,25 @@ impl Ctx {
)
}

/// NEW: Unbond self-bonded tokens from a validator when `source` is `None`
/// or equal to the `validator` address, or unbond delegated tokens from
/// the `source` to the `validator`.
pub fn unbond_tokens_new(
&mut self,
source: Option<&Address>,
validator: &Address,
amount: token::Amount,
) -> TxResult {
let current_epoch = self.get_block_epoch()?;
unbond_tokens_new(
self,
source,
validator,
token::Change::from(amount),
current_epoch,
)
}

/// Withdraw unbonded tokens from a self-bond to a validator when `source`
/// is `None` or equal to the `validator` address, or withdraw unbonded
/// tokens delegated to the `validator` to the `source`.
Expand All @@ -96,6 +119,18 @@ impl Ctx {
)
}

/// NEW: Withdraw unbonded tokens from a self-bond to a validator when
/// `source` is `None` or equal to the `validator` address, or withdraw
/// unbonded tokens delegated to the `validator` to the `source`.
pub fn withdraw_tokens_new(
&mut self,
source: Option<&Address>,
validator: &Address,
) -> EnvResult<token::Amount> {
let current_epoch = self.get_block_epoch()?;
withdraw_tokens_new(self, source, validator, current_epoch)
}

/// Change validator commission rate.
pub fn change_validator_commission_rate(
&mut self,
Expand All @@ -111,6 +146,21 @@ impl Ctx {
)
}

/// NEW: Change validator commission rate.
pub fn change_validator_commission_rate_new(
&mut self,
validator: &Address,
rate: &Decimal,
) -> TxResult {
let current_epoch = self.get_block_epoch()?;
change_validator_commission_rate_new(
self,
validator,
*rate,
current_epoch,
)
}

/// Attempt to initialize a validator account. On success, returns the
/// initialized validator account's address.
pub fn init_validator(
Expand Down Expand Up @@ -145,6 +195,44 @@ impl Ctx {

Ok(validator_address)
}

/// NEW: Attempt to initialize a validator account. On success, returns the
/// initialized validator account's address.
pub fn init_validator_new(
&mut self,
InitValidator {
account_key,
consensus_key,
protocol_key,
dkg_key,
commission_rate,
max_commission_rate_change,
validator_vp_code,
}: InitValidator,
) -> EnvResult<Address> {
let current_epoch = self.get_block_epoch()?;
// Init validator account
let validator_address = self.init_account(&validator_vp_code)?;
let pk_key = key::pk_key(&validator_address);
self.write(&pk_key, &account_key)?;
let protocol_pk_key = key::protocol_pk_key(&validator_address);
self.write(&protocol_pk_key, &protocol_key)?;
let dkg_pk_key = key::dkg_session_keys::dkg_pk_key(&validator_address);
self.write(&dkg_pk_key, &dkg_key)?;

let params = read_pos_params(self)?;
become_validator_new(
self,
&params,
&validator_address,
&consensus_key,
current_epoch,
commission_rate,
max_commission_rate_change,
)?;

Ok(validator_address)
}
}

namada_proof_of_stake::impl_pos_read_only! {
Expand Down

0 comments on commit affbf70

Please sign in to comment.