Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/use delegation rates #741

Merged
merged 3 commits into from
Aug 18, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions contracts/mixnet/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ fn default_initial_state(owner: Addr) -> State {
INITIAL_DEFAULT_EPOCH_LENGTH,
gateway_bond_reward_rate,
),
mixnode_epoch_delegation_reward: calculate_epoch_reward_rate(
INITIAL_DEFAULT_EPOCH_LENGTH,
mixnode_delegation_reward_rate,
),
gateway_epoch_delegation_reward: calculate_epoch_reward_rate(
INITIAL_DEFAULT_EPOCH_LENGTH,
gateway_delegation_reward_rate,
),
}
}

Expand Down
2 changes: 2 additions & 0 deletions contracts/mixnet/src/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,8 @@ mod tests {
},
mixnode_epoch_bond_reward: "1.23".parse().unwrap(),
gateway_epoch_bond_reward: "4.56".parse().unwrap(),
mixnode_epoch_delegation_reward: "7.89".parse().unwrap(),
gateway_epoch_delegation_reward: "0.12".parse().unwrap(),
};

config(deps.as_mut().storage).save(&dummy_state).unwrap();
Expand Down
2 changes: 2 additions & 0 deletions contracts/mixnet/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ pub struct State {
// helper values to avoid having to recalculate them on every single payment operation
pub mixnode_epoch_bond_reward: Decimal, // reward per epoch expressed as a decimal like 0.05
pub gateway_epoch_bond_reward: Decimal, // reward per epoch expressed as a decimal like 0.05
pub mixnode_epoch_delegation_reward: Decimal, // reward per epoch expressed as a decimal like 0.05
pub gateway_epoch_delegation_reward: Decimal, // reward per epoch expressed as a decimal like 0.05
}
42 changes: 40 additions & 2 deletions contracts/mixnet/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,38 @@ pub(crate) fn read_state_params(storage: &dyn Storage) -> StateParams {
config_read(storage).load().unwrap().params
}

pub(crate) fn read_mixnode_epoch_reward_rate(storage: &dyn Storage) -> Decimal {
pub(crate) fn read_mixnode_epoch_bond_reward_rate(storage: &dyn Storage) -> Decimal {
// same justification as in `read_state_params` for the unwrap
config_read(storage)
.load()
.unwrap()
.mixnode_epoch_bond_reward
}

pub(crate) fn read_gateway_epoch_reward_rate(storage: &dyn Storage) -> Decimal {
pub(crate) fn read_gateway_epoch_bond_reward_rate(storage: &dyn Storage) -> Decimal {
// same justification as in `read_state_params` for the unwrap
config_read(storage)
.load()
.unwrap()
.gateway_epoch_bond_reward
}

pub(crate) fn read_mixnode_epoch_delegation_reward_rate(storage: &dyn Storage) -> Decimal {
// same justification as in `read_state_params` for the unwrap
config_read(storage)
.load()
.unwrap()
.mixnode_epoch_delegation_reward
}

pub(crate) fn read_gateway_epoch_delegation_reward_rate(storage: &dyn Storage) -> Decimal {
// same justification as in `read_state_params` for the unwrap
config_read(storage)
.load()
.unwrap()
.gateway_epoch_delegation_reward
}

pub fn layer_distribution(storage: &mut dyn Storage) -> Singleton<LayerDistribution> {
singleton(storage, LAYER_DISTRIBUTION_KEY)
}
Expand Down Expand Up @@ -240,6 +256,17 @@ pub(crate) fn read_mixnode_bond(
Ok(node.bond_amount.amount)
}

// currently not used outside tests
#[cfg(test)]
pub(crate) fn read_mixnode_delegation(
storage: &dyn Storage,
identity: &[u8],
) -> StdResult<cosmwasm_std::Uint128> {
let bucket = mixnodes_read(storage);
let node = bucket.load(identity)?;
Ok(node.total_delegation.amount)
}

// Gateway-related stuff

pub fn gateways(storage: &mut dyn Storage) -> Bucket<GatewayBond> {
Expand Down Expand Up @@ -305,6 +332,17 @@ pub(crate) fn read_gateway_bond(
Ok(node.bond_amount.amount)
}

// currently not used outside tests
#[cfg(test)]
pub(crate) fn read_gateway_delegation(
storage: &dyn Storage,
identity: &[u8],
) -> StdResult<cosmwasm_std::Uint128> {
let bucket = gateways_read(storage);
let node = bucket.load(identity)?;
Ok(node.total_delegation.amount)
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
Loading