From 4584f35a2aeb9ebf49f1b89f7707b9ec33bb6644 Mon Sep 17 00:00:00 2001 From: fmtabbara Date: Tue, 4 Oct 2022 14:33:08 +0100 Subject: [PATCH] get mixnode reward estimation separarte handleCalculate function into own file add mix-id to bondedNode state remove unused imports --- .../src-tauri/src/operations/mixnet/bond.rs | 1 - nym-wallet/src/context/bonding.tsx | 1 + nym-wallet/src/context/mocks/bonding.tsx | 1 + .../node-settings/apy-playground/index.tsx | 59 +++++++------------ .../node-settings/apy-playground/utils.tsx | 23 ++++++++ nym-wallet/src/requests/queries.ts | 4 +- 6 files changed, 49 insertions(+), 40 deletions(-) create mode 100644 nym-wallet/src/pages/bonding/node-settings/apy-playground/utils.tsx diff --git a/nym-wallet/src-tauri/src/operations/mixnet/bond.rs b/nym-wallet/src-tauri/src/operations/mixnet/bond.rs index 6564ebca86..bff31c28ef 100644 --- a/nym-wallet/src-tauri/src/operations/mixnet/bond.rs +++ b/nym-wallet/src-tauri/src/operations/mixnet/bond.rs @@ -11,7 +11,6 @@ use nym_types::mixnode::{MixNodeCostParams, MixNodeDetails}; use nym_types::transaction::TransactionExecuteResult; use serde::{Deserialize, Serialize}; use std::time::Duration; -use validator_client::models::UptimeResponse; use validator_client::nymd::traits::{MixnetQueryClient, MixnetSigningClient}; use validator_client::nymd::Fee; diff --git a/nym-wallet/src/context/bonding.tsx b/nym-wallet/src/context/bonding.tsx index b8f65bfed7..a00a15d3ac 100644 --- a/nym-wallet/src/context/bonding.tsx +++ b/nym-wallet/src/context/bonding.tsx @@ -270,6 +270,7 @@ export const BondingContextProvider = ({ children }: { children?: React.ReactNod ); const routingScore = await getAvgUptime(); setBondedNode({ + id: data.bond_information.id, name: nodeDescription?.name, mixId: mix_id, identityKey: bond_information.mix_node.identity_key, diff --git a/nym-wallet/src/context/mocks/bonding.tsx b/nym-wallet/src/context/mocks/bonding.tsx index 95e770850b..fa5b31fb47 100644 --- a/nym-wallet/src/context/mocks/bonding.tsx +++ b/nym-wallet/src/context/mocks/bonding.tsx @@ -7,6 +7,7 @@ import { mockSleep } from './utils'; const SLEEP_MS = 1000; const bondedMixnodeMock: TBondedMixnode = { + id: 1, name: 'Monster node', mixId: 1, identityKey: '7mjM2fYbtN6kxMwp1TrmQ4VwPks3URR5pBgWPWhzT98F', diff --git a/nym-wallet/src/pages/bonding/node-settings/apy-playground/index.tsx b/nym-wallet/src/pages/bonding/node-settings/apy-playground/index.tsx index f06f0ef964..886fbf0f59 100644 --- a/nym-wallet/src/pages/bonding/node-settings/apy-playground/index.tsx +++ b/nym-wallet/src/pages/bonding/node-settings/apy-playground/index.tsx @@ -1,12 +1,12 @@ import React, { useState, useEffect } from 'react'; import { Box, Card, CardContent, CardHeader, Grid, Typography } from '@mui/material'; import { ResultsTable } from 'src/components/RewardsPlayground/ResultsTable'; -import { computeMixnodeRewardEstimation, getDelegationSummary } from 'src/requests'; +import { computeMixnodeRewardEstimation, getDelegationSummary, getMixnodeRewardEstimation } from 'src/requests'; import { NodeDetails } from 'src/components/RewardsPlayground/NodeDetail'; import { Inputs, CalculateArgs } from 'src/components/RewardsPlayground/Inputs'; import { TBondedMixnode, useBondingContext } from 'src/context'; import { isMixnode } from 'src/types'; -import { SelectionChance } from '@nymproject/types'; +import { handleCalculate } from './utils'; const MAJOR_AMOUNT_FOR_CALCS = 1000; @@ -25,65 +25,51 @@ export const ApyPlayground = () => { total: { daily: '-', monthly: '-', yearly: '-' }, operator: { daily: '-', monthly: '-', yearly: '-' }, delegator: { daily: '-', monthly: '-', yearly: '-' }, - stakeSaturation: '', - selectionProbability: 'Low' as SelectionChance, }); const [defaultInputValues, setDefaultInputValues] = useState(); + const [stakeSaturation, setStakeSaturation] = useState(); - const initialiseInputs = async (node: TBondedMixnode) => { + const initialise = async (node: TBondedMixnode) => { const delegations = await getDelegationSummary(); + const res = await getMixnodeRewardEstimation(node.id); + setResults(handleCalculate(res.estimation.operator, res.estimation.delegates, res.estimation.total_node_reward)); + setStakeSaturation(node.stakeSaturation); setDefaultInputValues({ profitMargin: node.profitMargin, uptime: (node.uptime || 0).toString(), bond: node.bond.amount || '', delegations: delegations.total_delegations.amount, - operatorCost: '', + operatorCost: Math.floor(res.estimation.operating_cost / 1_000_000).toString(), }); }; useEffect(() => { if (bondedNode && isMixnode(bondedNode)) { - initialiseInputs(bondedNode); + initialise(bondedNode); } }, []); - const handleCalculate = async ({ bond, delegations, uptime }: CalculateArgs) => { + const handleCalculateEstimate = async ({ bond, delegations, uptime }: CalculateArgs) => { try { - const res = await computeMixnodeRewardEstimation({ - identity: bondedNode?.identityKey || '', - uptime: parseInt(uptime, 10), + const estimatedRewards = await computeMixnodeRewardEstimation({ + identity: bondedNode!.identityKey, + performance: (parseInt(uptime, 10) / 100).toString(), isActive: true, pledgeAmount: Math.floor(+bond * 1_000_000), totalDelegation: Math.floor(+delegations * 1_000_000), }); - const operatorReward = (res.estimated_operator_reward / 1_000_000) * 24; // epoch_reward * 1 epoch_per_hour * 24 hours - const delegatorsReward = (res.estimated_delegators_reward / 1_000_000) * 24; + const estimationResult = handleCalculate( + estimatedRewards.estimation.delegates, + estimatedRewards.estimation.operator, + estimatedRewards.estimation.total_node_reward, + ); - const operatorRewardScaled = MAJOR_AMOUNT_FOR_CALCS * (operatorReward / 0); - const delegatorReward = MAJOR_AMOUNT_FOR_CALCS * (delegatorsReward / 0); + setStakeSaturation('0'); - setResults({ - total: { - daily: (operatorRewardScaled + delegatorReward).toString(), - monthly: ((operatorRewardScaled + delegatorReward) * 30).toString(), - yearly: ((operatorRewardScaled + delegatorReward) * 365).toString(), - }, - operator: { - daily: operatorRewardScaled.toString(), - monthly: (operatorRewardScaled * 30).toString(), - yearly: (operatorRewardScaled * 365).toString(), - }, - delegator: { - daily: delegatorReward.toString(), - monthly: (delegatorReward * 30).toString(), - yearly: (delegatorReward * 365).toString(), - }, - stakeSaturation: '0', - selectionProbability: 'High', - }); + setResults(estimationResult); } catch (e) { console.log(e); } @@ -107,8 +93,7 @@ export const ApyPlayground = () => { } /> - {console.log(defaultInputValues?.uptime)} - {defaultInputValues && } + {defaultInputValues && } @@ -116,7 +101,7 @@ export const ApyPlayground = () => { - + diff --git a/nym-wallet/src/pages/bonding/node-settings/apy-playground/utils.tsx b/nym-wallet/src/pages/bonding/node-settings/apy-playground/utils.tsx new file mode 100644 index 0000000000..ef8fa604dc --- /dev/null +++ b/nym-wallet/src/pages/bonding/node-settings/apy-playground/utils.tsx @@ -0,0 +1,23 @@ +export const handleCalculate = (operatorReward: number, delegatorsReward: number, totalReward: number) => { + const dailyOperatorReward = (operatorReward / 1_000_000) * 24; // epoch_reward * 1 epoch_per_hour * 24 hours + const dailyDelegatorReward = (delegatorsReward / 1_000_000) * 24; + const dailyTotal = (totalReward / 1_000_000) * 24; + console.log({ dailyOperatorReward }); + return { + total: { + daily: Math.round(dailyTotal).toString(), + monthly: Math.round(dailyTotal * 30).toString(), + yearly: Math.round(dailyTotal * 365).toString(), + }, + operator: { + daily: Math.round(dailyOperatorReward).toString(), + monthly: Math.round(dailyOperatorReward * 30).toString(), + yearly: Math.round(dailyOperatorReward * 365).toString(), + }, + delegator: { + daily: Math.round(dailyDelegatorReward).toString(), + monthly: Math.round(dailyDelegatorReward * 30).toString(), + yearly: Math.round(dailyDelegatorReward * 365).toString(), + }, + }; +}; diff --git a/nym-wallet/src/requests/queries.ts b/nym-wallet/src/requests/queries.ts index 2167f69ce4..6daf91ea55 100644 --- a/nym-wallet/src/requests/queries.ts +++ b/nym-wallet/src/requests/queries.ts @@ -54,10 +54,10 @@ export const getGatewayReport = async (identity: string) => export const computeMixnodeRewardEstimation = async (args: { identity: string; - uptime: number; + performance: string; isActive: boolean; pledgeAmount: number; totalDelegation: number; -}) => invokeWrapper('compute_mixnode_reward_estimation', args); +}) => invokeWrapper('compute_mixnode_reward_estimation', args); export const getMixnodeUptime = async (mixId: number) => invokeWrapper('get_mixnode_uptime', { mixId });