Add Query endpoints for calculating rewards (#1152)

This commit is contained in:
Drazen Urch
2022-03-18 15:44:26 +01:00
committed by GitHub
parent f24d6e224d
commit 63dd26ca1b
11 changed files with 154 additions and 5 deletions
@@ -248,6 +248,28 @@ impl<C> Client<C> {
Ok(self.nymd.get_contract_settings().await?)
}
pub async fn get_operator_rewards(&self, address: String) -> Result<u128, ValidatorClientError>
where
C: CosmWasmClient + Sync,
{
Ok(self.nymd.get_operator_rewards(address).await?.u128())
}
pub async fn get_delegator_rewards(
&self,
address: String,
mix_identity: IdentityKey,
) -> Result<u128, ValidatorClientError>
where
C: CosmWasmClient + Sync,
{
Ok(self
.nymd
.get_delegator_rewards(address, mix_identity)
.await?
.u128())
}
pub async fn get_current_epoch(&self) -> Result<Option<Interval>, ValidatorClientError>
where
C: CosmWasmClient + Sync,
@@ -290,6 +290,33 @@ impl<C> NymdClient<C> {
.await
}
pub async fn get_operator_rewards(&self, address: String) -> Result<Uint128, NymdError>
where
C: CosmWasmClient + Sync,
{
let request = QueryMsg::QueryOperatorReward { address };
self.client
.query_contract_smart(self.mixnet_contract_address()?, &request)
.await
}
pub async fn get_delegator_rewards(
&self,
address: String,
mix_identity: IdentityKey,
) -> Result<Uint128, NymdError>
where
C: CosmWasmClient + Sync,
{
let request = QueryMsg::QueryDelegatorReward {
address,
mix_identity,
};
self.client
.query_contract_smart(self.mixnet_contract_address()?, &request)
.await
}
pub async fn get_current_epoch(&self) -> Result<Option<Interval>, NymdError>
where
C: CosmWasmClient + Sync,
@@ -169,6 +169,13 @@ pub enum QueryMsg {
GetRewardedSetRefreshBlocks {},
GetCurrentEpoch {},
GetEpochsInInterval {},
QueryOperatorReward {
address: String,
},
QueryDelegatorReward {
address: String,
mix_identity: IdentityKey,
},
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
+11
View File
@@ -359,6 +359,17 @@ pub fn query(deps: Deps<'_>, env: Env, msg: QueryMsg) -> Result<QueryResponse, C
}
QueryMsg::GetEpochsInInterval {} => to_binary(&crate::constants::EPOCHS_IN_INTERVAL),
QueryMsg::GetCurrentEpoch {} => to_binary(&query_current_epoch(deps.storage)?),
QueryMsg::QueryOperatorReward { address } => to_binary(
&crate::rewards::queries::query_operator_reward(deps, address)?,
),
QueryMsg::QueryDelegatorReward {
address,
mix_identity,
} => to_binary(&crate::rewards::queries::query_delegator_reward(
deps,
address,
mix_identity,
)?),
};
Ok(query_res?)
+1 -1
View File
@@ -54,7 +54,7 @@ pub(crate) fn mixnodes<'a>(
}
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub(crate) struct StoredMixnodeBond {
pub struct StoredMixnodeBond {
pub pledge_amount: Coin,
pub owner: Addr,
pub layer: Layer,
+31
View File
@@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
use super::storage;
use crate::error::ContractError;
use cosmwasm_std::Uint128;
use cosmwasm_std::{Deps, StdResult};
use mixnet_contract_common::{IdentityKey, MixnodeRewardingStatusResponse};
@@ -24,6 +25,36 @@ pub(crate) fn query_rewarding_status(
Ok(MixnodeRewardingStatusResponse { status })
}
pub fn query_operator_reward(deps: Deps, owner: String) -> Result<Uint128, ContractError> {
let owner_address = deps.api.addr_validate(&owner)?;
let bond = match crate::mixnodes::storage::mixnodes()
.idx
.owner
.item(deps.storage, owner_address.clone())?
{
Some(record) => record.1,
None => {
// Return if bond does not exist
return Ok(Uint128::zero());
}
};
super::transactions::calculate_operator_reward(deps.storage, &owner_address, &bond)
}
pub fn query_delegator_reward(
deps: Deps,
owner: String,
mix_identity: IdentityKey,
) -> Result<Uint128, ContractError> {
let owner_address = deps.api.addr_validate(&owner)?;
super::transactions::calculate_delegator_reward(
deps.storage,
owner_address.as_str(),
&mix_identity,
)
}
#[cfg(test)]
pub(crate) mod tests {
use super::*;
+2 -2
View File
@@ -90,7 +90,7 @@ pub fn _try_compound_operator_reward(
Ok(reward)
}
fn calculate_operator_reward(
pub fn calculate_operator_reward(
storage: &dyn Storage,
owner: &Addr,
bond: &StoredMixnodeBond,
@@ -233,7 +233,7 @@ pub fn _try_compound_delegator_reward(
// TODO: Test
// + last_reward_claimed_height is updated
// + last_reward_claimed height is correctly used
fn calculate_delegator_reward(
pub fn calculate_delegator_reward(
storage: &dyn Storage,
owner_address: &str,
mix_identity: &str,
+2
View File
@@ -45,11 +45,13 @@ fn main() {
mixnet::bond::bond_gateway,
mixnet::bond::bond_mixnode,
mixnet::bond::gateway_bond_details,
mixnet::bond::get_operator_rewards,
mixnet::bond::mixnode_bond_details,
mixnet::bond::unbond_gateway,
mixnet::bond::unbond_mixnode,
mixnet::bond::update_mixnode,
mixnet::delegate::delegate_to_mixnode,
mixnet::delegate::get_delegator_rewards,
mixnet::delegate::get_reverse_mix_delegations_paged,
mixnet::delegate::undelegate_from_mixnode,
mixnet::send::send,
@@ -3,6 +3,7 @@ use crate::error::BackendError;
use crate::nymd_client;
use crate::state::State;
use crate::{Gateway, MixNode};
use cosmwasm_std::Uint128;
use mixnet_contract_common::{GatewayBond, MixNodeBond};
use std::sync::Arc;
use tokio::sync::RwLock;
@@ -83,3 +84,11 @@ pub async fn gateway_bond_details(
let bond = client.nymd.owns_gateway(client.nymd.address()).await?;
Ok(bond)
}
#[tauri::command]
pub async fn get_operator_rewards(
address: String,
state: tauri::State<'_, Arc<RwLock<State>>>,
) -> Result<Uint128, BackendError> {
Ok(nymd_client!(state).get_operator_rewards(address).await?)
}
@@ -3,8 +3,8 @@ use crate::error::BackendError;
use crate::nymd_client;
use crate::state::State;
use crate::utils::DelegationResult;
use cosmwasm_std::Coin as CosmWasmCoin;
use mixnet_contract_common::PagedDelegatorDelegationsResponse;
use cosmwasm_std::{Coin as CosmWasmCoin, Uint128};
use mixnet_contract_common::{IdentityKey, PagedDelegatorDelegationsResponse};
use std::sync::Arc;
use tokio::sync::RwLock;
@@ -51,3 +51,16 @@ pub async fn get_reverse_mix_delegations_paged(
.await?,
)
}
#[tauri::command]
pub async fn get_delegator_rewards(
address: String,
mix_identity: IdentityKey,
state: tauri::State<'_, Arc<RwLock<State>>>,
) -> Result<Uint128, BackendError> {
Ok(
nymd_client!(state)
.get_delegator_rewards(address, mix_identity)
.await?,
)
}
+27
View File
@@ -140,6 +140,33 @@ impl<C> Client<C> {
self.0.read().await.get_contract_settings().await
}
#[allow(dead_code)]
pub(crate) async fn get_operator_rewards(
&self,
address: String,
) -> Result<u128, ValidatorClientError>
where
C: CosmWasmClient + Sync,
{
self.0.read().await.get_operator_rewards(address).await
}
#[allow(dead_code)]
pub(crate) async fn get_delegator_rewards(
&self,
address: String,
mix_identity: IdentityKey,
) -> Result<u128, ValidatorClientError>
where
C: CosmWasmClient + Sync,
{
self.0
.read()
.await
.get_delegator_rewards(address, mix_identity)
.await
}
pub(crate) async fn get_current_epoch(&self) -> Result<Option<Interval>, ValidatorClientError>
where
C: CosmWasmClient + Sync,