This commit is contained in:
Yana
2023-11-08 16:51:38 +00:00
parent 35e3961f75
commit 4f58a63cb6
3 changed files with 34 additions and 61 deletions
@@ -36,7 +36,7 @@ export const DelegateModal: FCWithChildren<{
header?: string;
buttonText?: string;
rewardInterval: string;
accountBalance?: string;
// accountBalance?: string;
estimatedReward?: number;
profitMarginPercentage?: string | null;
nodeUptimePercentage?: number | null;
@@ -55,7 +55,7 @@ export const DelegateModal: FCWithChildren<{
buttonText,
identityKey: initialIdentityKey,
rewardInterval,
accountBalance,
// accountBalance,
estimatedReward,
denom,
profitMarginPercentage,
@@ -99,39 +99,15 @@ export const DelegateModal: FCWithChildren<{
}
}, [address, getCosmWasmClient]);
const handleCheckStakeSaturation = async (newMixId: number) => {
try {
const newSaturation = decimalToFloatApproximation(
(await getMixnodeStakeSaturation(newMixId)).uncapped_saturation,
);
if (newSaturation && newSaturation > 1) {
const saturationPercentage = Math.round(newSaturation * 100);
return { isOverSaturated: true, saturationPercentage };
}
return { isOverSaturated: false, saturationPercentage: undefined };
} catch (e) {
Console.error('Error fetching the saturation, error:', e);
return { isOverSaturated: false, saturationPercentage: undefined };
}
};
const validate = async () => {
let newValidatedValue = true;
let errorAmountMessage;
let errorIdentityKeyMessage;
const tokenPool = 'balance';
if (!identityKey || !validateKey(identityKey, 32)) {
if (!identityKey) {
newValidatedValue = false;
errorIdentityKeyMessage = undefined;
console.log('!identityKey :>> ', identityKey);
}
if (identityKey && mixId && validateKey(identityKey, 32)) {
const { isOverSaturated, saturationPercentage } = await handleCheckStakeSaturation(mixId);
if (isOverSaturated) {
newValidatedValue = false;
errorIdentityKeyMessage = `This node is over saturated (${saturationPercentage}%), please select another node`;
}
errorIdentityKeyMessage = 'Please enter a valid identity key';
}
if (amount && !(await validateAmount(amount, '0'))) {
@@ -148,10 +124,19 @@ export const DelegateModal: FCWithChildren<{
newValidatedValue = false;
}
if (!mixId) {
newValidatedValue = false;
if (amount) {
const hasEnoughTokens = checkTokenBalance(tokenPool, amount, balance.data || '0');
if (!hasEnoughTokens) {
errorAmountMessage = 'Not enough funds';
newValidatedValue = false;
}
}
// if (!mixId) {
// newValidatedValue = false;
// }
setErrorIdentityKey(errorIdentityKeyMessage);
if (mixIdError && !errorIdentityKeyMessage) {
setErrorIdentityKey(mixIdError);
@@ -168,12 +153,6 @@ export const DelegateModal: FCWithChildren<{
const handleConfirm = async ({ mixId: id, value }: { mixId: number; value: DecCoin }) => {
const tokenPool = 'balance';
const hasEnoughTokens = await checkTokenBalance(tokenPool, value.amount, balance.data || '0');
if (!hasEnoughTokens) {
setErrorAmount('Not enough funds');
return;
}
if (tokenPool === 'balance') {
getFee(simulateDelegateToMixnode, { mixId: id, amount: value });
@@ -4,13 +4,7 @@ import Big from 'big.js';
import { valid } from 'semver';
import { add, format, fromUnixTime } from 'date-fns';
import { DecCoin, isValidRawCoin, MixNodeCostParams } from '@nymproject/types';
import {
getCurrentInterval,
getDefaultMixnodeCostParams,
getLockedCoins,
getSpendableCoins,
userBalance,
} from '../requests';
import { getCurrentInterval, getDefaultMixnodeCostParams, getLockedCoins, getSpendableCoins } from '../requests';
import { Console } from './console';
export type TPoolOption = 'balance' | 'locked';
@@ -24,17 +18,17 @@ export const uNYMtoNYM = (unym: string, rounding = 6) => {
};
};
export const checkHasEnoughFunds = async (balance: string, allocationValue: string): Promise<boolean> => {
try {
// const walletValue = await userBalance();
// export const checkHasEnoughFunds = async (balance: string, allocationValue: string): Promise<boolean> => {
// try {
// // const walletValue = await userBalance();
const remainingBalance = +balance - +allocationValue;
return remainingBalance >= 0;
} catch (e) {
Console.log(e as string);
return false;
}
};
// const remainingBalance = +balance - +allocationValue;
// return remainingBalance >= 0;
// } catch (e) {
// Console.log(e as string);
// return false;
// }
// };
export const checkHasEnoughLockedTokens = async (allocationValue: string) => {
try {
@@ -48,14 +42,15 @@ export const checkHasEnoughLockedTokens = async (allocationValue: string) => {
return false;
};
export const checkTokenBalance = async (tokenPool: TPoolOption, amount: string, balance: string) => {
export const checkTokenBalance = (tokenPool: TPoolOption, amount: string, balance: string) => {
let hasEnoughFunds = false;
if (tokenPool === 'locked') {
hasEnoughFunds = await checkHasEnoughLockedTokens(amount);
}
// if (tokenPool === 'locked') {
// hasEnoughFunds = await checkHasEnoughLockedTokens(amount);
// }
if (tokenPool === 'balance') {
hasEnoughFunds = await checkHasEnoughFunds(balance, amount);
const remainingBalance = +balance - +amount;
hasEnoughFunds = remainingBalance >= 0;
}
return hasEnoughFunds;
+1 -2
View File
@@ -4,7 +4,6 @@ import Select, { SelectChangeEvent } from '@mui/material/Select';
import { Filters } from './Filters/Filters';
import { useIsMobile } from '../hooks/useIsMobile';
import { DelegateModal } from './Delegations/components/DelegateModal';
import { AppContext } from './Delegations/context/main';
import { ChainProvider } from '@cosmos-kit/react';
import { assets, chains } from 'chain-registry';
import { wallets as keplr } from '@cosmos-kit/keplr';
@@ -148,7 +147,7 @@ export const TableToolbar: FCWithChildren<TableToolBarProps> = ({
header="Delegate"
buttonText="Delegate stake"
denom={'nym'} // clientDetails?.display_mix_denom || 'nym'}
accountBalance={'balance?.printable_balance' || 'error reading balance'}
// accountBalance={'balance?.printable_balance' || 'error reading balance'}
rewardInterval="weekly"
hasVestingContract={true}
// {...storybookStyles(theme, isStorybook)}