ghost-portal/packages/page-coretime/src/utils.ts
Daria 069b032213
Relay Chain Coretime UI bugs (#11043)
* showing multiple core rows for parachains, changed end dates for non extended chains, correct renew status

* mobile table

* only showing renewals eligibility during the interlude

* removed extra tag

* styled component $property
2024-10-29 07:12:17 -04:00

68 lines
2.2 KiB
TypeScript

// Copyright 2017-2024 @polkadot/app-coretime authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { CoreTimeConsts, type LegacyLease, type Reservation } from '@polkadot/react-hooks/types';
import { BN } from '@polkadot/util';
import { CoreTimeTypes } from './types.js';
export const FirstCycleStart: Record<string, number> = {
kusama: 285768,
polkadot: 282525
};
function formatDate (date: Date) {
const day = date.getDate();
const month = date.toLocaleString('default', { month: 'short' });
const year = date.getFullYear();
return `${day} ${month} ${year}`;
}
/**
* blockTime = 6000 ms
* BlocksPerTimeslice = 80
* Default Regoin = 5040 timeslices
* TargetBlock = TargetTimeslice * BlocksPerTimeslice
* Block Time Difference = |TargetBlock - latest Block| * blockTime
*
* Estimate timestamp =
* if targetBlock is before the latestBlock
* now minus block time difference
* else
* now plus block time difference
*/
export const estimateTime = (targetTimeslice: string | number, latestBlock: number): string | null => {
if (!latestBlock || !targetTimeslice) {
console.error('Invalid input: one or more inputs are missing');
return null;
}
const now = new Date().getTime();
try {
const blockTime = new BN(CoreTimeConsts.BlockTime); // Average block time in milliseconds (6 seconds)
const timeSlice = new BN(CoreTimeConsts.BlocksPerTimeslice);
const latestBlockBN = new BN(latestBlock);
const timestampBN = new BN(now);
const targetBlockBN = new BN(targetTimeslice).mul(timeSlice);
const blockTimeDifference = targetBlockBN.sub(latestBlockBN).mul(blockTime);
const estTimestamp = timestampBN.add(blockTimeDifference);
return formatDate(new Date(estTimestamp.toNumber()));
} catch (error) {
console.error('Error in calculation:', error);
return null;
}
};
export const getOccupancyType = (lease: LegacyLease | undefined, reservation: Reservation | undefined, isPool: boolean): CoreTimeTypes => {
if (isPool) {
return CoreTimeTypes['On Demand'];
}
return reservation ? CoreTimeTypes.Reservation : lease ? CoreTimeTypes.Lease : CoreTimeTypes['Bulk Coretime'];
};