get mixnode reward estimation

separarte handleCalculate function into own file

add mix-id to bondedNode state

remove unused imports
This commit is contained in:
fmtabbara
2022-10-04 14:33:08 +01:00
parent 41fa03862e
commit 4584f35a2a
6 changed files with 49 additions and 40 deletions
@@ -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;
+1
View File
@@ -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,
+1
View File
@@ -7,6 +7,7 @@ import { mockSleep } from './utils';
const SLEEP_MS = 1000;
const bondedMixnodeMock: TBondedMixnode = {
id: 1,
name: 'Monster node',
mixId: 1,
identityKey: '7mjM2fYbtN6kxMwp1TrmQ4VwPks3URR5pBgWPWhzT98F',
@@ -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<DefaultInputValues>();
const [stakeSaturation, setStakeSaturation] = useState<string>();
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 = () => {
}
/>
<CardContent>
{console.log(defaultInputValues?.uptime)}
{defaultInputValues && <Inputs onCalculate={handleCalculate} defaultValues={defaultInputValues} />}
{defaultInputValues && <Inputs onCalculate={handleCalculateEstimate} defaultValues={defaultInputValues} />}
</CardContent>
</Card>
<Grid container spacing={3}>
@@ -116,7 +101,7 @@ export const ApyPlayground = () => {
<ResultsTable results={results} />
</Grid>
<Grid item xs={12} md={4}>
<NodeDetails saturation={results.stakeSaturation} selectionProbability={results.selectionProbability} />
<NodeDetails saturation={stakeSaturation} />
</Grid>
</Grid>
</Box>
@@ -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(),
},
};
};
+2 -2
View File
@@ -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<any>('compute_mixnode_reward_estimation', args);
}) => invokeWrapper<RewardEstimationResponse>('compute_mixnode_reward_estimation', args);
export const getMixnodeUptime = async (mixId: number) => invokeWrapper<number>('get_mixnode_uptime', { mixId });