From 6740d2886490d1abae61879afee4ef474e8e89f6 Mon Sep 17 00:00:00 2001 From: Daria <2395326+piggydoughnut@users.noreply.github.com> Date: Thu, 19 Sep 2024 21:58:55 +0800 Subject: [PATCH] Fix: UI Coretime type fix (#10933) * added Option type as return type from api requests for brokerConfig and brokerSalesInfo * extra check for value --- packages/react-hooks/src/useBrokerConfig.ts | 29 +++++++++-------- .../react-hooks/src/useBrokerSalesInfo.ts | 31 ++++++++++--------- 2 files changed, 33 insertions(+), 27 deletions(-) diff --git a/packages/react-hooks/src/useBrokerConfig.ts b/packages/react-hooks/src/useBrokerConfig.ts index 3e37eb65874a..61423945bf7f 100644 --- a/packages/react-hooks/src/useBrokerConfig.ts +++ b/packages/react-hooks/src/useBrokerConfig.ts @@ -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 { PalletBrokerConfigRecord } from '@polkadot/types/lookup'; import type { PalletBrokerConfigRecord as SimplifiedPalletBrokerConfigRecord } from './types.js'; @@ -8,30 +9,32 @@ import { useEffect, useState } from 'react'; import { createNamedHook, useApi, useCall } from '@polkadot/react-hooks'; -function parseConfig (config: PalletBrokerConfigRecord): SimplifiedPalletBrokerConfigRecord { +function extractInfo (config: Option): 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(isApiReady && api.query.broker.configuration); + const config = useCall>(isApiReady && api.query.broker.configuration); - const [state, setState] = useState(); + const [state, setState] = useState(); useEffect((): void => { - !!config && !!config.toJSON() && + !!config && !!config.isSome && !!config.toJSON() && setState( - parseConfig(config) + extractInfo(config) ); }, [config]); diff --git a/packages/react-hooks/src/useBrokerSalesInfo.ts b/packages/react-hooks/src/useBrokerSalesInfo.ts index e9b10c41c6cd..fcc67e517e23 100644 --- a/packages/react-hooks/src/useBrokerSalesInfo.ts +++ b/packages/react-hooks/src/useBrokerSalesInfo.ts @@ -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'; @@ -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): 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(isApiReady && api.query.broker.saleInfo); + const record = useCall>(isApiReady && api.query.broker.saleInfo); const [state, setState] = useState(); useEffect((): void => { - !!record && !!record.toJSON() && + !!record && !!record.isSome && !!record.toJSON() && setState( - parsePalletBrokerSaleInfoRecord(record) + extractInfo(record) ); }, [record]);