From f98eb7659763388ad725cf54d46f9a722c3f76ba Mon Sep 17 00:00:00 2001 From: Daria <2395326+piggydoughnut@users.noreply.github.com> Date: Wed, 18 Sep 2024 21:40:20 +0800 Subject: [PATCH] Fix: updated parsing for brokerConfig and brokerSalesInfo (#10926) * updated parsing for brokerConfig and brokerSalesInfo * renamed file * updated parsing for brokerConfig and brokerSalesInfo * better type conversion for BN * BN type change * lint --- packages/react-hooks/src/useBrokerConfig.ts | 22 ++++----- .../react-hooks/src/useBrokerSalesInfo.ts | 44 +++++++++++++++++ .../react-hooks/src/useBrokerSalesInfo.tsx | 49 ------------------- 3 files changed, 53 insertions(+), 62 deletions(-) create mode 100644 packages/react-hooks/src/useBrokerSalesInfo.ts delete mode 100644 packages/react-hooks/src/useBrokerSalesInfo.tsx diff --git a/packages/react-hooks/src/useBrokerConfig.ts b/packages/react-hooks/src/useBrokerConfig.ts index 1d46ae934ad2..3e37eb65874a 100644 --- a/packages/react-hooks/src/useBrokerConfig.ts +++ b/packages/react-hooks/src/useBrokerConfig.ts @@ -8,20 +8,16 @@ 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()) + 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 }; } @@ -33,7 +29,7 @@ function useBrokerConfigImpl () { const [state, setState] = useState(); useEffect((): void => { - config && + !!config && !!config.toJSON() && setState( parseConfig(config) ); diff --git a/packages/react-hooks/src/useBrokerSalesInfo.ts b/packages/react-hooks/src/useBrokerSalesInfo.ts new file mode 100644 index 000000000000..e9b10c41c6cd --- /dev/null +++ b/packages/react-hooks/src/useBrokerSalesInfo.ts @@ -0,0 +1,44 @@ +// 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 { BN } from '@polkadot/util'; + +function parsePalletBrokerSaleInfoRecord (record: PalletBrokerSaleInfoRecord): SimplifiedPalletBrokerSaleInfoRecord { + 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) + }; +} + +function useBrokerSalesInfoImpl () { + const { api, isApiReady } = useApi(); + + const record = useCall(isApiReady && api.query.broker.saleInfo); + + const [state, setState] = useState(); + + useEffect((): void => { + !!record && !!record.toJSON() && + setState( + parsePalletBrokerSaleInfoRecord(record) + ); + }, [record]); + + return state; +} + +export const useBrokerSalesInfo = createNamedHook('useBrokerSalesInfo', useBrokerSalesInfoImpl); diff --git a/packages/react-hooks/src/useBrokerSalesInfo.tsx b/packages/react-hooks/src/useBrokerSalesInfo.tsx deleted file mode 100644 index 1f841ee91367..000000000000 --- a/packages/react-hooks/src/useBrokerSalesInfo.tsx +++ /dev/null @@ -1,49 +0,0 @@ -// 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(isApiReady && api.query.broker.saleInfo); - - const [state, setState] = useState(); - - useEffect((): void => { - saleInfo && - setState( - parsePalletBrokerSaleInfoRecord(saleInfo) - ); - }, [saleInfo]); - - return state; -} - -export const useBrokerSalesInfo = createNamedHook('useBrokerSalesInfo', useBrokerSalesInfoImpl);