updated broker pallets calls to be more general, added the estimated bulk price value for overtime ui tab (#10923)
This commit is contained in:
parent
a983d211f0
commit
12d9b3a53e
@ -8,7 +8,7 @@ import React from 'react';
|
||||
|
||||
import { CardSummary, styled, SummaryBox, UsageBar } from '@polkadot/react-components';
|
||||
import { defaultHighlight } from '@polkadot/react-components/styles';
|
||||
import { useApi, useBrokerStatus, useCurrentPrice, useRenewalBump } from '@polkadot/react-hooks';
|
||||
import { useApi, useBrokerStatus } from '@polkadot/react-hooks';
|
||||
|
||||
import { useTranslation } from '../translate.js';
|
||||
import { getStats } from '../utils.js';
|
||||
@ -41,8 +41,6 @@ interface Props {
|
||||
function Summary ({ workloadInfos }: Props): React.ReactElement {
|
||||
const { t } = useTranslation();
|
||||
const { api, apiEndpoint } = useApi();
|
||||
const renewalBump = useRenewalBump();
|
||||
const currentPrice = useCurrentPrice();
|
||||
const totalCores = useBrokerStatus('coreCount');
|
||||
const uiHighlight = apiEndpoint?.ui.color || defaultHighlight;
|
||||
const { idles, pools, tasks }: statsType = React.useMemo(() => getStats(totalCores, workloadInfos), [totalCores, workloadInfos]);
|
||||
@ -66,10 +64,7 @@ function Summary ({ workloadInfos }: Props): React.ReactElement {
|
||||
<RegionLength />
|
||||
</CardSummary>
|
||||
<CardSummary label={t('estimated bulk price')}>
|
||||
<RenewalPrice
|
||||
currentPrice={currentPrice}
|
||||
renewalBump={renewalBump}
|
||||
/>
|
||||
<RenewalPrice />
|
||||
</CardSummary>
|
||||
</StyledDiv>
|
||||
</>
|
||||
|
||||
@ -3,6 +3,7 @@
|
||||
|
||||
import React from 'react';
|
||||
|
||||
import { useBrokerSalesInfo } from '@polkadot/react-hooks';
|
||||
import { formatBalance } from '@polkadot/util';
|
||||
|
||||
interface Props {
|
||||
@ -12,18 +13,13 @@ interface Props {
|
||||
currentPrice?: string;
|
||||
}
|
||||
|
||||
function RenewalPrice ({ currentPrice, renewalBump }: Props): React.ReactElement<Props> | null {
|
||||
const percentage = renewalBump ? Number.parseInt(renewalBump) / 1000000000 : 0;
|
||||
|
||||
const price = currentPrice ? Number.parseInt(currentPrice) : 0;
|
||||
|
||||
const renewalPrice = price * percentage + price;
|
||||
function RenewalPrice (): React.ReactElement<Props> | null {
|
||||
const salesInfo = useBrokerSalesInfo();
|
||||
|
||||
return (
|
||||
<div className='ui--balance-value'>
|
||||
{formatBalance(renewalPrice) || '-'}
|
||||
{formatBalance(salesInfo?.endPrice) || '-'}
|
||||
</div>
|
||||
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@ -22,8 +22,10 @@ export { useBlockEvents } from './useBlockEvents.js';
|
||||
export { useBlockInterval } from './useBlockInterval.js';
|
||||
export { useBlocksPerDays } from './useBlocksPerDays.js';
|
||||
export { useBlockTime } from './useBlockTime.js';
|
||||
export { useBrokerConfig } from './useBrokerConfig.js';
|
||||
export { useBrokerLeases } from './useBrokerLeases.js';
|
||||
export { useBrokerReservations } from './useBrokerReservations.js';
|
||||
export { useBrokerSalesInfo } from './useBrokerSalesInfo.js';
|
||||
export { useBrokerStatus } from './useBrokerStatus.js';
|
||||
export { useCacheKey } from './useCacheKey.js';
|
||||
export { useCall } from './useCall.js';
|
||||
@ -31,7 +33,6 @@ export { useCallMulti } from './useCallMulti.js';
|
||||
export { useCollectiveInstance } from './useCollectiveInstance.js';
|
||||
export { useCollectiveMembers } from './useCollectiveMembers.js';
|
||||
export { useCoreDescriptor } from './useCoreDescriptor.js';
|
||||
export { useCurrentPrice } from './useCurrentPrice.js';
|
||||
export { useDebounce } from './useDebounce.js';
|
||||
export { useDelegations } from './useDelegations.js';
|
||||
export { useDeriveAccountFlags } from './useDeriveAccountFlags.js';
|
||||
@ -74,7 +75,6 @@ export { useQueue } from './useQueue.js';
|
||||
export { useQueueStatus } from './useQueueStatus.js';
|
||||
export { useRegions } from './useRegions.js';
|
||||
export { useRegistrars } from './useRegistrars.js';
|
||||
export { useRenewalBump } from './useRenewalBump.js';
|
||||
export { useSavedFlags } from './useSavedFlags.js';
|
||||
export { useScroll } from './useScroll.js';
|
||||
export { useStakingInfo } from './useStakingInfo.js';
|
||||
|
||||
@ -272,3 +272,27 @@ export interface LegacyLease {
|
||||
until: number,
|
||||
task: string
|
||||
}
|
||||
|
||||
export interface PalletBrokerSaleInfoRecord {
|
||||
saleStart: number;
|
||||
leadinLength: number;
|
||||
endPrice: BN;
|
||||
regionBegin: number;
|
||||
regionEnd: number;
|
||||
idealCoresSold: number;
|
||||
coresOffered: number;
|
||||
firstCore: number;
|
||||
selloutPrice: BN;
|
||||
coresSold: number;
|
||||
}
|
||||
|
||||
export interface PalletBrokerConfigRecord {
|
||||
advanceNotice: number;
|
||||
interludeLength: number;
|
||||
leadinLength: number;
|
||||
regionLength: number;
|
||||
idealBulkProportion: BN;
|
||||
limitCoresOffered: number;
|
||||
renewalBump: BN;
|
||||
contributionTimeout: number;
|
||||
}
|
||||
|
||||
45
packages/react-hooks/src/useBrokerConfig.ts
Normal file
45
packages/react-hooks/src/useBrokerConfig.ts
Normal file
@ -0,0 +1,45 @@
|
||||
// Copyright 2017-2024 @polkadot/react-hooks authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { PalletBrokerConfigRecord } from '@polkadot/types/lookup';
|
||||
import type { PalletBrokerConfigRecord as SimplifiedPalletBrokerConfigRecord } from './types.js';
|
||||
|
||||
import { useEffect, useState } from 'react';
|
||||
|
||||
import { createNamedHook, useApi, useCall } from '@polkadot/react-hooks';
|
||||
|
||||
import { stringToBN } from './utils/dataProcessing.js';
|
||||
|
||||
function parseConfig (config: PalletBrokerConfigRecord): SimplifiedPalletBrokerConfigRecord {
|
||||
const c = config.toJSON();
|
||||
|
||||
return {
|
||||
advanceNotice: c.advanceNotice as number,
|
||||
contributionTimeout: c.contributionTimeout as number,
|
||||
idealBulkProportion: stringToBN(c.idealBulkProportion?.toString()),
|
||||
interludeLength: c.interludeLength as number,
|
||||
leadinLength: c.leadinLength as number,
|
||||
limitCoresOffered: c.limitCoresOffered as number,
|
||||
regionLength: c.regionLength as number,
|
||||
renewalBump: stringToBN(c.renewalBump?.toString())
|
||||
};
|
||||
}
|
||||
|
||||
function useBrokerConfigImpl () {
|
||||
const { api, isApiReady } = useApi();
|
||||
|
||||
const config = useCall<PalletBrokerConfigRecord>(isApiReady && api.query.broker.configuration);
|
||||
|
||||
const [state, setState] = useState<SimplifiedPalletBrokerConfigRecord>();
|
||||
|
||||
useEffect((): void => {
|
||||
config &&
|
||||
setState(
|
||||
parseConfig(config)
|
||||
);
|
||||
}, [config]);
|
||||
|
||||
return state;
|
||||
}
|
||||
|
||||
export const useBrokerConfig = createNamedHook('useBrokerConfig', useBrokerConfigImpl);
|
||||
49
packages/react-hooks/src/useBrokerSalesInfo.tsx
Normal file
49
packages/react-hooks/src/useBrokerSalesInfo.tsx
Normal file
@ -0,0 +1,49 @@
|
||||
// Copyright 2017-2024 @polkadot/react-hooks authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { PalletBrokerSaleInfoRecord } from '@polkadot/types/lookup';
|
||||
import type { PalletBrokerSaleInfoRecord as SimplifiedPalletBrokerSaleInfoRecord } from './types.js';
|
||||
|
||||
import { useEffect, useState } from 'react';
|
||||
|
||||
import { createNamedHook, useApi, useCall } from '@polkadot/react-hooks';
|
||||
|
||||
import { stringToBN } from './utils/dataProcessing.js';
|
||||
|
||||
function parsePalletBrokerSaleInfoRecord (
|
||||
record: PalletBrokerSaleInfoRecord
|
||||
): SimplifiedPalletBrokerSaleInfoRecord {
|
||||
const rec = record.toJSON();
|
||||
|
||||
return {
|
||||
coresOffered: rec.coresOffered as number,
|
||||
coresSold: rec.coresSold as number,
|
||||
endPrice: stringToBN(rec.endPrice?.toString()),
|
||||
firstCore: rec.firstCore as number,
|
||||
idealCoresSold: rec.idealCoresSold as number,
|
||||
leadinLength: rec.leadinLength as number,
|
||||
regionBegin: rec.regionBegin as number,
|
||||
regionEnd: rec.regionEnd as number,
|
||||
saleStart: rec.saleStart as number,
|
||||
selloutPrice: stringToBN(rec.selloutPrice?.toString())
|
||||
};
|
||||
}
|
||||
|
||||
function useBrokerSalesInfoImpl () {
|
||||
const { api, isApiReady } = useApi();
|
||||
|
||||
const saleInfo = useCall<PalletBrokerSaleInfoRecord>(isApiReady && api.query.broker.saleInfo);
|
||||
|
||||
const [state, setState] = useState<SimplifiedPalletBrokerSaleInfoRecord | undefined>();
|
||||
|
||||
useEffect((): void => {
|
||||
saleInfo &&
|
||||
setState(
|
||||
parsePalletBrokerSaleInfoRecord(saleInfo)
|
||||
);
|
||||
}, [saleInfo]);
|
||||
|
||||
return state;
|
||||
}
|
||||
|
||||
export const useBrokerSalesInfo = createNamedHook('useBrokerSalesInfo', useBrokerSalesInfoImpl);
|
||||
@ -1,31 +0,0 @@
|
||||
// Copyright 2017-2024 @polkadot/react-hooks authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { PalletBrokerSaleInfoRecord } from '@polkadot/types/lookup';
|
||||
|
||||
import { useEffect, useState } from 'react';
|
||||
|
||||
import { createNamedHook, useApi, useCall } from '@polkadot/react-hooks';
|
||||
|
||||
function extractCurrentPrice (saleInfo: PalletBrokerSaleInfoRecord) {
|
||||
return saleInfo.toJSON()?.price?.toString();
|
||||
}
|
||||
|
||||
function useCurrentPriceImpl () {
|
||||
const { api, isApiReady } = useApi();
|
||||
|
||||
const saleInfo = useCall<PalletBrokerSaleInfoRecord>(isApiReady && api.query.broker.saleInfo);
|
||||
|
||||
const [state, setState] = useState<string | undefined>();
|
||||
|
||||
useEffect((): void => {
|
||||
saleInfo &&
|
||||
setState(
|
||||
extractCurrentPrice(saleInfo)
|
||||
);
|
||||
}, [saleInfo]);
|
||||
|
||||
return state;
|
||||
}
|
||||
|
||||
export const useCurrentPrice = createNamedHook('useCurrentPrice', useCurrentPriceImpl);
|
||||
@ -1,31 +0,0 @@
|
||||
// Copyright 2017-2024 @polkadot/react-hooks authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { PalletBrokerConfigRecord } from '@polkadot/types/lookup';
|
||||
|
||||
import { useEffect, useState } from 'react';
|
||||
|
||||
import { createNamedHook, useApi, useCall } from '@polkadot/react-hooks';
|
||||
|
||||
function extractRenewalBump (config: PalletBrokerConfigRecord) {
|
||||
return config.toJSON()?.renewalBump?.toString();
|
||||
}
|
||||
|
||||
function useRenewalBumpImpl () {
|
||||
const { api, isApiReady } = useApi();
|
||||
|
||||
const config = useCall<PalletBrokerConfigRecord>(isApiReady && api.query.broker.configuration);
|
||||
|
||||
const [state, setState] = useState<string | undefined>();
|
||||
|
||||
useEffect((): void => {
|
||||
config &&
|
||||
setState(
|
||||
extractRenewalBump(config)
|
||||
);
|
||||
}, [config]);
|
||||
|
||||
return state;
|
||||
}
|
||||
|
||||
export const useRenewalBump = createNamedHook('useRenewalBump', useRenewalBumpImpl);
|
||||
@ -3,6 +3,8 @@
|
||||
|
||||
import type { PalletBrokerScheduleItem } from '@polkadot/types/lookup';
|
||||
|
||||
import { BN } from '@polkadot/util';
|
||||
|
||||
export function hexToBin (hex: string): string {
|
||||
return parseInt(hex, 16).toString(2);
|
||||
}
|
||||
@ -23,3 +25,17 @@ export function processHexMask (mask: PalletBrokerScheduleItem['mask'] | undefin
|
||||
|
||||
return buffArr;
|
||||
}
|
||||
|
||||
export function stringToBN (value: string | undefined): BN {
|
||||
if (!value) {
|
||||
return new BN(0);
|
||||
}
|
||||
|
||||
const sanitized = value.trim().replace(/,/g, '');
|
||||
|
||||
if (!/^\d+$/.test(sanitized)) {
|
||||
throw new Error(`Invalid input for BN conversion: "${sanitized}"`);
|
||||
}
|
||||
|
||||
return new BN(sanitized);
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user