Files
nym/explorer-nextjs/app/utils/currency.ts
T
Mark Sinclair 15c3012199 explorer-api: add nym node endpoints + UI to show nym-nodes and account balances (#5183)
* explorer-api: add nym node endpoints + UI to show nym-nodes and account balances

* explorer-api: add endpoints to get operator rewards
explorer-ui: show delegations on nym-nodes, show operator rewards, bug fixes

* explorer-ui: change summary screen to only show nym-node stats

* explorer-api: add unstable routes to get legacy mixnodes and gateways from the contract instead of the Nym API
explorer-ui: adapt front-end to show less information in legacy nodes with plain bond types

* explorer-ui: fix up source of legacy mixnode data

* explorer-ui: add more account page null and undefined checks

* explorer-ui: filter out null gateway versions

* explorer-ui: sanitise gateway versions

* explorer-ui: add more guards on the balance parts to check that greater than 0

* explorer-api: make /tmp/unstable/gateways endpoint compatible with the current Harbour Master API

* explorer-ui: fix typo

* cargo fmt

* Add node-id, total stake and links to nodes list

---------

Co-authored-by: Mark Sinclair <mmsinclair@users.noreply.github.com>
Co-authored-by: Jędrzej Stuczyński <jedrzej.stuczynski@gmail.com>
2024-12-05 08:17:30 +00:00

111 lines
3.1 KiB
TypeScript

import { printableCoin } from '@nymproject/nym-validator-client';
import Big from 'big.js';
import { DecCoin, isValidRawCoin } from '@nymproject/types';
const DENOM = process.env.CURRENCY_DENOM || 'unym';
const DENOM_STAKING = process.env.CURRENCY_STAKING_DENOM || 'unyx';
export const toDisplay = (val: string | number | Big, dp = 4) => {
let displayValue;
try {
displayValue = Big(val).toFixed(dp);
} catch (e: any) {
console.warn(`${displayValue} not a valid decimal number: ${e}`);
}
return displayValue;
};
export const currencyToString = ({ amount, dp, denom = DENOM }: { amount: string; dp?: number; denom?: string }) => {
if (!dp) {
printableCoin({
amount,
denom,
});
}
const [printableAmount, printableDenom] = printableCoin({
amount,
denom,
}).split(/\s+/);
return `${toDisplay(printableAmount, dp)} ${printableDenom}`;
};
function addThousandsSeparator(value: string) {
return value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, " ");
}
export const humanReadableCurrencyToString = ({ amount, denom }: { amount: string, denom: string }) => {
const str = currencyToString({ amount, denom, dp: 2 });
const parts = str.split('.');
return [addThousandsSeparator(parts[0]), parts[1]].join('.');
}
export const stakingCurrencyToString = (amount: string, denom: string = DENOM_STAKING) =>
printableCoin({
amount,
denom,
});
/**
* Converts a decimal number to a pretty representation
* with fixed decimal places.
*
* @param val - a decimal number of string form
* @param dp - number of decimal places (4 by default ie. 0.0000)
* @returns A prettyfied decimal number
*/
/**
* Converts a decimal number of μNYM (micro NYM) to NYM.
*
* @param unym - a decimal number of μNYM
* @param dp - number of decimal places (4 by default ie. 0.0000)
* @returns The corresponding decimal number in NYM
*/
export const unymToNym = (unym: string | number | Big, dp = 4) => {
let nym;
try {
nym = Big(unym).div(1_000_000).toFixed(dp);
} catch (e: any) {
console.warn(`${unym} not a valid decimal number: ${e}`);
}
return nym;
};
export const validateAmount = async (
majorAmountAsString: DecCoin['amount'],
minimumAmountAsString: DecCoin['amount'],
): Promise<boolean> => {
// tests basic coin value requirements, like no more than 6 decimal places, value lower than total supply, etc
if (!Number(majorAmountAsString)) {
return false;
}
if (!isValidRawCoin(majorAmountAsString)) {
return false;
}
const majorValueFloat = parseInt(majorAmountAsString, Number(10));
return majorValueFloat >= parseInt(minimumAmountAsString, Number(10));
};
/**
* Takes a DecCoin and prettify its amount to a representation
* with fixed decimal places.
*
* @param coin - a DecCoin
* @param dp - number of decimal places to apply to amount (4 by default ie. 0.0000)
* @returns A DecCoin with prettified amount
*/
export const decCoinToDisplay = (coin: DecCoin, dp = 4) => {
const displayCoin = { ...coin };
try {
displayCoin.amount = Big(coin.amount).toFixed(dp);
} catch (e: any) {
console.warn(`${coin.amount} not a valid decimal number: ${e}`);
}
return displayCoin;
};