@polkadot/util#extractTime TypeScript Examples
The following examples show how to use
@polkadot/util#extractTime.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: useBlockTime.ts From crust-apps with Apache License 2.0 | 6 votes |
export function useBlockTime (blocks = BN_ONE, apiOverride?: ApiPromise): Result {
const { t } = useTranslation();
const { api } = useApi();
return useMemo(
(): Result => {
const a = apiOverride || api;
const blockTime = (
a.consts.babe?.expectedBlockTime ||
a.consts.difficulty?.targetBlockTime ||
a.consts.timestamp?.minimumPeriod.muln(2) ||
DEFAULT_TIME
);
const value = blockTime.mul(blocks).toNumber();
const prefix = value < 0 ? '+' : '';
const time = extractTime(Math.abs(value));
const { days, hours, minutes, seconds } = time;
const timeStr = [
days ? (days > 1) ? t<string>('{{days}} days', { replace: { days } }) : t<string>('1 day') : null,
hours ? (hours > 1) ? t<string>('{{hours}} hrs', { replace: { hours } }) : t<string>('1 hr') : null,
minutes ? (minutes > 1) ? t<string>('{{minutes}} mins', { replace: { minutes } }) : t<string>('1 min') : null,
seconds ? (seconds > 1) ? t<string>('{{seconds}} s', { replace: { seconds } }) : t<string>('1 s') : null
]
.filter((value): value is string => !!value)
.slice(0, 2)
.join(' ');
return [
blockTime.toNumber(),
`${prefix}${timeStr}`,
time
];
},
[api, apiOverride, blocks, t]
);
}
Example #2
Source File: useBlockTime.ts From contracts-ui with GNU General Public License v3.0 | 6 votes |
useBlockTime = (
blocks: number | BN = BN_ONE,
apiOverride?: ApiPromise | null
): Result => {
const { api } = useApi();
return useMemo((): Result => {
const a = apiOverride || api;
const blockTime = blockTimeMs(a);
const value = blockTime.mul(bnToBn(blocks)).toNumber();
const time = extractTime(Math.abs(value));
const { days, hours, minutes, seconds } = time;
const timeStr = [
days ? (days > 1 ? `${days} days` : '1 day') : null,
hours ? (hours > 1 ? `${hours} hours` : '1 hr') : null,
minutes ? (minutes > 1 ? `${minutes} mins` : '1 min') : null,
seconds ? (seconds > 1 ? `${seconds} s` : '1 s') : null,
]
.filter((s): s is string => !!s)
.slice(0, 2)
.join(' ');
return [blockTime.toNumber(), `${value < 0 ? '+' : ''}${timeStr}`, time];
}, [api, apiOverride, blocks]);
}
Example #3
Source File: useBlockTime.ts From subscan-multisig-react with Apache License 2.0 | 6 votes |
export function useBlockTime(blocks = BN_ONE, apiOverride?: ApiPromise | null): Result {
const { t } = useTranslation();
const { api } = useApi();
// eslint-disable-next-line
return useMemo((): Result => {
const a = apiOverride || api;
const blockTime =
a.consts.babe?.expectedBlockTime ||
a.consts.difficulty?.targetBlockTime ||
// eslint-disable-next-line no-magic-numbers
(a.consts.timestamp?.minimumPeriod as unknown as BN).muln(2) ||
DEFAULT_TIME;
const value = (blockTime as unknown as BN).mul(blocks).toNumber();
const prefix = value < 0 ? '+' : '';
const time = extractTime(Math.abs(value));
const { days, hours, minutes, seconds } = time;
const timeStr = [
days ? (days > 1 ? t<string>('{{days}} days', { replace: { days } }) : t<string>('1 day')) : null,
hours ? (hours > 1 ? t<string>('{{hours}} hrs', { replace: { hours } }) : t<string>('1 hr')) : null,
minutes ? (minutes > 1 ? t<string>('{{minutes}} mins', { replace: { minutes } }) : t<string>('1 min')) : null,
seconds ? (seconds > 1 ? t<string>('{{seconds}} s', { replace: { seconds } }) : t<string>('1 s')) : null,
]
// eslint-disable-next-line @typescript-eslint/no-shadow
.filter((value): value is string => !!value)
// eslint-disable-next-line no-magic-numbers
.slice(0, 2)
.join(' ');
return [(blockTime as unknown as BN).toNumber(), `${prefix}${timeStr}`, time];
}, [api, apiOverride, blocks, t]);
}
Example #4
Source File: mockHooks.ts From crust-apps with Apache License 2.0 | 5 votes |
mockHooks = {
blockTime: [50, '', extractTime(1)],
members: defaultMembers,
treasury: defaultTreasury
}