Skip to content

Commit

Permalink
Fix: UI Coretime type fix (polkadot-js#10933)
Browse files Browse the repository at this point in the history
* added Option type as return type from api requests for brokerConfig and brokerSalesInfo

* extra check for value
  • Loading branch information
piggydoughnut authored Sep 19, 2024
1 parent 2644915 commit 6740d28
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 27 deletions.
29 changes: 16 additions & 13 deletions packages/react-hooks/src/useBrokerConfig.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,40 @@
// Copyright 2017-2024 @polkadot/react-hooks authors & contributors
// SPDX-License-Identifier: Apache-2.0

import type { Option } from '@polkadot/types';
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';

function parseConfig (config: PalletBrokerConfigRecord): SimplifiedPalletBrokerConfigRecord {
function extractInfo (config: Option<PalletBrokerConfigRecord>): SimplifiedPalletBrokerConfigRecord {
const c = config.unwrap();

return {
advanceNotice: config.advanceNotice.toNumber(),
contributionTimeout: config.contributionTimeout.toNumber(),
idealBulkProportion: config.idealBulkProportion,
interludeLength: config.interludeLength.toNumber(),
leadinLength: config.leadinLength.toNumber(),
limitCoresOffered: config.limitCoresOffered.isSome ? config.limitCoresOffered.unwrap().toNumber() : 0,
regionLength: config.regionLength.toNumber(),
renewalBump: config.renewalBump
advanceNotice: c.advanceNotice?.toNumber(),
contributionTimeout: c.contributionTimeout?.toNumber(),
idealBulkProportion: c.idealBulkProportion,
interludeLength: c.interludeLength?.toNumber(),
leadinLength: c.leadinLength?.toNumber(),
limitCoresOffered: c.limitCoresOffered?.isSome ? c.limitCoresOffered?.unwrap().toNumber() : 0,
regionLength: c.regionLength?.toNumber(),
renewalBump: c.renewalBump
};
}

function useBrokerConfigImpl () {
const { api, isApiReady } = useApi();

const config = useCall<PalletBrokerConfigRecord>(isApiReady && api.query.broker.configuration);
const config = useCall<Option<PalletBrokerConfigRecord>>(isApiReady && api.query.broker.configuration);

const [state, setState] = useState<SimplifiedPalletBrokerConfigRecord>();
const [state, setState] = useState<SimplifiedPalletBrokerConfigRecord | undefined>();

useEffect((): void => {
!!config && !!config.toJSON() &&
!!config && !!config.isSome && !!config.toJSON() &&
setState(
parseConfig(config)
extractInfo(config)
);
}, [config]);

Expand Down
31 changes: 17 additions & 14 deletions packages/react-hooks/src/useBrokerSalesInfo.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2017-2024 @polkadot/react-hooks authors & contributors
// SPDX-License-Identifier: Apache-2.0

import type { Option } from '@polkadot/types';
import type { PalletBrokerSaleInfoRecord } from '@polkadot/types/lookup';
import type { PalletBrokerSaleInfoRecord as SimplifiedPalletBrokerSaleInfoRecord } from './types.js';

Expand All @@ -9,32 +10,34 @@ import { useEffect, useState } from 'react';
import { createNamedHook, useApi, useCall } from '@polkadot/react-hooks';
import { BN } from '@polkadot/util';

function parsePalletBrokerSaleInfoRecord (record: PalletBrokerSaleInfoRecord): SimplifiedPalletBrokerSaleInfoRecord {
function extractInfo (record: Option<PalletBrokerSaleInfoRecord>): SimplifiedPalletBrokerSaleInfoRecord {
const v = record.unwrap();

return {
coresOffered: record.coresOffered.toNumber(),
coresSold: record.coresSold.toNumber(),
endPrice: record.endPrice,
firstCore: record.firstCore.toNumber(),
idealCoresSold: record.idealCoresSold.toNumber(),
leadinLength: record.leadinLength.toNumber(),
regionBegin: record.regionBegin.toNumber(),
regionEnd: record.regionEnd.toNumber(),
saleStart: record.saleStart.toNumber(),
selloutPrice: record.selloutPrice.isSome ? record.selloutPrice.unwrap() : new BN(0)
coresOffered: v.coresOffered?.toNumber(),
coresSold: v.coresSold?.toNumber(),
endPrice: v.endPrice,
firstCore: v.firstCore?.toNumber(),
idealCoresSold: v.idealCoresSold?.toNumber(),
leadinLength: v.leadinLength?.toNumber(),
regionBegin: v.regionBegin?.toNumber(),
regionEnd: v.regionEnd?.toNumber(),
saleStart: v.saleStart?.toNumber(),
selloutPrice: v.selloutPrice?.isSome ? v.selloutPrice?.unwrap() : new BN(0)
};
}

function useBrokerSalesInfoImpl () {
const { api, isApiReady } = useApi();

const record = useCall<PalletBrokerSaleInfoRecord>(isApiReady && api.query.broker.saleInfo);
const record = useCall<Option<PalletBrokerSaleInfoRecord>>(isApiReady && api.query.broker.saleInfo);

const [state, setState] = useState<SimplifiedPalletBrokerSaleInfoRecord | undefined>();

useEffect((): void => {
!!record && !!record.toJSON() &&
!!record && !!record.isSome && !!record.toJSON() &&
setState(
parsePalletBrokerSaleInfoRecord(record)
extractInfo(record)
);
}, [record]);

Expand Down

0 comments on commit 6740d28

Please sign in to comment.