Skip to content

Commit

Permalink
Rename to DelegationDistribution for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
Mauro Lacy committed Jan 27, 2025
1 parent d58cbf7 commit 0711c85
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
6 changes: 3 additions & 3 deletions contracts/btc-staking/src/staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use hex::ToHex;

use crate::error::ContractError;
use crate::state::config::{ADMIN, CONFIG, PARAMS};
use crate::state::delegations::{delegations, Delegation};
use crate::state::delegations::{delegations, DelegationDistribution};
use crate::state::staking::{
fps, BtcDelegation, DelegatorUnbondingInfo, FinalityProviderState, ACTIVATED_HEIGHT,
BTC_DELEGATIONS, DELEGATION_FPS, FPS, FP_DELEGATIONS,
Expand Down Expand Up @@ -485,7 +485,7 @@ pub fn handle_withdraw_rewards(

pub fn withdraw_delegation_reward(
deps: DepsMut,
delegation: &mut Delegation,
delegation: &mut DelegationDistribution,
fp_pubkey_hex: &str,
) -> Result<Uint128, ContractError> {
// Load FP state
Expand All @@ -502,7 +502,7 @@ pub fn withdraw_delegation_reward(

/// Calculates reward for the delegation and the corresponding FP distribution.
pub(crate) fn calculate_reward(
delegation: &Delegation,
delegation: &DelegationDistribution,
fp_state: &FinalityProviderState,
) -> Result<Uint128, ContractError> {
let points = fp_state.points_per_stake * Uint256::from(delegation.stake);
Expand Down
22 changes: 12 additions & 10 deletions contracts/btc-staking/src/state/delegations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use cw_storage_plus::{Index, IndexList, IndexedMap, KeyDeserialize, MultiIndex};
/// Single delegation related information - entry per `(staking hash, finality provider public key)`
/// pair, including distribution alignment
#[cw_serde]
pub struct Delegation {
pub struct DelegationDistribution {
/// The delegator's canonical address
pub staker_addr: CanonicalAddr,
/// How many satoshis the user stakes in this delegation
Expand All @@ -20,21 +20,23 @@ pub struct Delegation {
pub struct DelegationIndexes<'a> {
// Delegations by finality provider's public key and staking hash.
// Last type param defines the pk deserialization type
pub rev: MultiIndex<'a, (String, Vec<u8>), Delegation, (Vec<u8>, String)>,
pub rev: MultiIndex<'a, (String, Vec<u8>), DelegationDistribution, (Vec<u8>, String)>,
// Delegations by staker's (raw, canonical) address and finality provider's public key.
// Last type param defines the pk deserialization type
pub staker: MultiIndex<'a, (Vec<u8>, String), Delegation, (Vec<u8>, String)>,
pub staker: MultiIndex<'a, (Vec<u8>, String), DelegationDistribution, (Vec<u8>, String)>,
}

impl<'a> IndexList<Delegation> for DelegationIndexes<'a> {
fn get_indexes(&'_ self) -> Box<dyn Iterator<Item = &'_ dyn Index<Delegation>> + '_> {
let v: Vec<&dyn Index<Delegation>> = vec![&self.rev, &self.staker];
impl<'a> IndexList<DelegationDistribution> for DelegationIndexes<'a> {
fn get_indexes(
&'_ self,
) -> Box<dyn Iterator<Item = &'_ dyn Index<DelegationDistribution>> + '_> {
let v: Vec<&dyn Index<DelegationDistribution>> = vec![&self.rev, &self.staker];
Box::new(v.into_iter())
}
}

pub struct Delegations<'a> {
pub delegation: IndexedMap<(&'a [u8], &'a str), Delegation, DelegationIndexes<'a>>,
pub delegation: IndexedMap<(&'a [u8], &'a str), DelegationDistribution, DelegationIndexes<'a>>,
}

impl<'a> Delegations<'a> {
Expand Down Expand Up @@ -89,7 +91,7 @@ impl<'a> Delegations<'a> {
)),
None => {
// Distribution alignment
let delegation = Delegation {
let delegation = DelegationDistribution {
staker_addr: staker_canonical_addr.clone(),
stake: delegation_stake,
withdrawn_funds: Uint128::zero(),
Expand Down Expand Up @@ -129,7 +131,7 @@ impl<'a> Delegations<'a> {
&self,
storage: &dyn Storage,
fp: &str,
) -> StdResult<Vec<(Vec<u8>, Delegation)>> {
) -> StdResult<Vec<(Vec<u8>, DelegationDistribution)>> {
self.delegation
.idx
.rev
Expand All @@ -139,7 +141,7 @@ impl<'a> Delegations<'a> {
let ((hash, _), del) = item?;
Ok((hash, del))
})
.collect::<StdResult<Vec<(Vec<u8>, Delegation)>>>()
.collect::<StdResult<Vec<(Vec<u8>, DelegationDistribution)>>>()
}
}

Expand Down

0 comments on commit 0711c85

Please sign in to comment.