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
This commit is contained in:
parent
cf06259a0b
commit
f98eb76597
@ -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<SimplifiedPalletBrokerConfigRecord>();
|
||||
|
||||
useEffect((): void => {
|
||||
config &&
|
||||
!!config && !!config.toJSON() &&
|
||||
setState(
|
||||
parseConfig(config)
|
||||
);
|
||||
|
||||
44
packages/react-hooks/src/useBrokerSalesInfo.ts
Normal file
44
packages/react-hooks/src/useBrokerSalesInfo.ts
Normal file
@ -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<PalletBrokerSaleInfoRecord>(isApiReady && api.query.broker.saleInfo);
|
||||
|
||||
const [state, setState] = useState<SimplifiedPalletBrokerSaleInfoRecord | undefined>();
|
||||
|
||||
useEffect((): void => {
|
||||
!!record && !!record.toJSON() &&
|
||||
setState(
|
||||
parsePalletBrokerSaleInfoRecord(record)
|
||||
);
|
||||
}, [record]);
|
||||
|
||||
return state;
|
||||
}
|
||||
|
||||
export const useBrokerSalesInfo = createNamedHook('useBrokerSalesInfo', useBrokerSalesInfoImpl);
|
||||
@ -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<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);
|
||||
Loading…
Reference in New Issue
Block a user