diff --git a/nym-wallet/src-tauri/src/operations/mixnet/bond.rs b/nym-wallet/src-tauri/src/operations/mixnet/bond.rs index 5807fb7270..6564ebca86 100644 --- a/nym-wallet/src-tauri/src/operations/mixnet/bond.rs +++ b/nym-wallet/src-tauri/src/operations/mixnet/bond.rs @@ -11,6 +11,7 @@ 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; @@ -341,3 +342,18 @@ pub async fn get_mix_node_description( .json() .await?) } + +#[tauri::command] +pub async fn get_mixnode_uptime( + mix_id: NodeId, + state: tauri::State<'_, WalletState>, +) -> Result { + log::info!(">>> Get mixnode uptime"); + + let guard = state.read().await; + let client = guard.current_client()?; + let uptime = client.validator_api.get_mixnode_avg_uptime(mix_id).await?; + + log::info!(">>> Uptime response: {}", uptime.avg_uptime); + Ok(uptime.avg_uptime) +} diff --git a/nym-wallet/src/components/RewardsPlayground/Inputs.tsx b/nym-wallet/src/components/RewardsPlayground/Inputs.tsx index f0e5c80880..a288d3f930 100644 --- a/nym-wallet/src/components/RewardsPlayground/Inputs.tsx +++ b/nym-wallet/src/components/RewardsPlayground/Inputs.tsx @@ -2,18 +2,19 @@ import React from 'react'; import { yupResolver } from '@hookform/resolvers/yup'; import { Button, Grid, TextField, Typography } from '@mui/material'; import { useForm } from 'react-hook-form'; +import { DefaultInputValues } from 'src/pages/bonding/node-settings/apy-playground'; import { inputValidationSchema } from './inputsValidationSchema'; -import { useBondingContext } from 'src/context'; export type InputFields = { label: string; - name: 'profitMargin' | 'uptime' | 'bond' | 'delegations' | 'operatorCost'; + name: 'profitMargin' | 'uptime' | 'bond' | 'delegations' | 'operatorCost' | 'uptime'; isPercentage?: boolean; }[]; -export type calculateArgs = { +export type CalculateArgs = { bond: string; delegations: string; + uptime: string; }; const inputFields: InputFields = [ @@ -24,11 +25,15 @@ const inputFields: InputFields = [ { label: 'Uptime', name: 'uptime', isPercentage: true }, ]; -export const Inputs = ({ onCalculate }: { onCalculate: (args: calculateArgs) => Promise }) => { - const { bondedNode } = useBondingContext(); - - const handleCalculate = (args: calculateArgs) => { - onCalculate({ bond: args.bond, delegations: args.delegations }); +export const Inputs = ({ + onCalculate, + defaultValues, +}: { + onCalculate: (args: CalculateArgs) => Promise; + defaultValues: DefaultInputValues | undefined; +}) => { + const handleCalculate = async (args: CalculateArgs) => { + onCalculate({ bond: args.bond, delegations: args.delegations, uptime: args.uptime }); }; const { @@ -37,19 +42,13 @@ export const Inputs = ({ onCalculate }: { onCalculate: (args: calculateArgs) => formState: { errors }, } = useForm({ resolver: yupResolver(inputValidationSchema), - defaultValues: { - profitMargin: bondedNode?.profitMargin || '', - uptime: 100, - bond: bondedNode?.bond.amount || '', - delegations: '', - operatorCost: '', - }, + defaultValues, }); return ( {inputFields.map((field) => ( - + ( Stake saturation - {saturation}% + {saturation || '- '}% diff --git a/nym-wallet/src/context/bonding.tsx b/nym-wallet/src/context/bonding.tsx index 7225360456..b8f65bfed7 100644 --- a/nym-wallet/src/context/bonding.tsx +++ b/nym-wallet/src/context/bonding.tsx @@ -157,11 +157,14 @@ export const BondingContextProvider = ({ children }: { children?: React.ReactNod } = { status: 'not_found', stakeSaturation: '0', + uptime: 0, }; try { const statusResponse = await getMixnodeStatus(mixId); + const uptime = await getMixnodeUptime(mixId); additionalDetails.status = statusResponse.status; + additionalDetails.uptime = uptime; } catch (e) { Console.log('getMixnodeStatus fails', e); } @@ -279,6 +282,7 @@ export const BondingContextProvider = ({ children }: { children?: React.ReactNod delegators: rewarding_details.unique_delegations, proxy: bond_information.proxy, operatorRewards, + uptime, status, stakeSaturation, operatorCost: decCoinToDisplay(rewarding_details.cost_params.interval_operating_cost), diff --git a/nym-wallet/src/pages/bonding/node-settings/NodeSettings.tsx b/nym-wallet/src/pages/bonding/node-settings/NodeSettings.tsx index da50bc2d2d..fd1f020e79 100644 --- a/nym-wallet/src/pages/bonding/node-settings/NodeSettings.tsx +++ b/nym-wallet/src/pages/bonding/node-settings/NodeSettings.tsx @@ -5,6 +5,7 @@ import { Box, Typography, Stack, IconButton, Divider } from '@mui/material'; import { Close } from '@mui/icons-material'; import { useTheme } from '@mui/material/styles'; import { ConfirmationDetailProps, ConfirmationDetailsModal } from 'src/components/Bonding/modals/ConfirmationModal'; +import { TestNode } from 'src/pages/bonding/node-settings/test-my-node'; import { Node as NodeIcon } from 'src/svg-icons/node'; import { LoadingModal } from 'src/components/Modals/LoadingModal'; import { NymCard } from 'src/components'; 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 a30d0e141d..f06f0ef964 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,31 +1,59 @@ -import React, { useState } from 'react'; +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 } from 'src/requests'; +import { computeMixnodeRewardEstimation, getDelegationSummary } from 'src/requests'; import { NodeDetails } from 'src/components/RewardsPlayground/NodeDetail'; -import { Inputs, calculateArgs } from 'src/components/RewardsPlayground/Inputs'; -import { useBondingContext } from 'src/context'; -import { useDelegationContext } from 'src/context/delegations'; +import { Inputs, CalculateArgs } from 'src/components/RewardsPlayground/Inputs'; +import { TBondedMixnode, useBondingContext } from 'src/context'; +import { isMixnode } from 'src/types'; +import { SelectionChance } from '@nymproject/types'; const MAJOR_AMOUNT_FOR_CALCS = 1000; +export type DefaultInputValues = { + profitMargin: string; + uptime: string; + bond: string; + delegations: string; + operatorCost: string; +}; + export const ApyPlayground = () => { const { bondedNode } = useBondingContext(); - const { totalDelegations } = useDelegationContext(); - - console.log(totalDelegations); const [results, setResults] = useState({ total: { daily: '-', monthly: '-', yearly: '-' }, operator: { daily: '-', monthly: '-', yearly: '-' }, delegator: { daily: '-', monthly: '-', yearly: '-' }, + stakeSaturation: '', + selectionProbability: 'Low' as SelectionChance, }); - const handleCalculate = async ({ bond, delegations }: calculateArgs) => { + const [defaultInputValues, setDefaultInputValues] = useState(); + + const initialiseInputs = async (node: TBondedMixnode) => { + const delegations = await getDelegationSummary(); + + setDefaultInputValues({ + profitMargin: node.profitMargin, + uptime: (node.uptime || 0).toString(), + bond: node.bond.amount || '', + delegations: delegations.total_delegations.amount, + operatorCost: '', + }); + }; + + useEffect(() => { + if (bondedNode && isMixnode(bondedNode)) { + initialiseInputs(bondedNode); + } + }, []); + + const handleCalculate = async ({ bond, delegations, uptime }: CalculateArgs) => { try { const res = await computeMixnodeRewardEstimation({ - identity: 'HsnGQDiTL9hfY4ZkCBWoVFDdVDQWXKaK9ojXSkmUT44z', - uptime: 10, + identity: bondedNode?.identityKey || '', + uptime: parseInt(uptime, 10), isActive: true, pledgeAmount: Math.floor(+bond * 1_000_000), totalDelegation: Math.floor(+delegations * 1_000_000), @@ -53,6 +81,8 @@ export const ApyPlayground = () => { monthly: (delegatorReward * 30).toString(), yearly: (delegatorReward * 365).toString(), }, + stakeSaturation: '0', + selectionProbability: 'High', }); } catch (e) { console.log(e); @@ -77,7 +107,8 @@ export const ApyPlayground = () => { } /> - + {console.log(defaultInputValues?.uptime)} + {defaultInputValues && } @@ -85,7 +116,7 @@ export const ApyPlayground = () => { - + diff --git a/nym-wallet/src/requests/queries.ts b/nym-wallet/src/requests/queries.ts index 8eb5ade3ab..2167f69ce4 100644 --- a/nym-wallet/src/requests/queries.ts +++ b/nym-wallet/src/requests/queries.ts @@ -59,3 +59,5 @@ export const computeMixnodeRewardEstimation = async (args: { pledgeAmount: number; totalDelegation: number; }) => invokeWrapper('compute_mixnode_reward_estimation', args); + +export const getMixnodeUptime = async (mixId: number) => invokeWrapper('get_mixnode_uptime', { mixId });