diff --git a/package.json b/package.json index 791533ba..aa590430 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,7 @@ "@types/humanize-duration": "^3.27.3", "@vercel/analytics": "^1.2.2", "clsx": "^1.1.1", - "coretime-utils": "^0.2.8", + "coretime-utils": "^0.3.2", "date-fns": "^3.3.1", "decimal.js": "^10.4.3", "humanize-duration": "^3.31.0", @@ -67,4 +67,4 @@ "typescript": "^4.7.4", "webpack": "^5.81.0" } -} +} \ No newline at end of file diff --git a/src/components/Modals/Interlace/index.tsx b/src/components/Modals/Interlace/index.tsx index ffdb0af0..73a60729 100644 --- a/src/components/Modals/Interlace/index.tsx +++ b/src/components/Modals/Interlace/index.tsx @@ -10,7 +10,7 @@ import { Typography, useTheme, } from '@mui/material'; -import { CoreMask } from 'coretime-utils'; +import { maskFromBin, maskFromChunk, maskToBin } from 'coretime-utils'; import { useEffect, useState } from 'react'; import { ProgressButton, SimpleRegionCard } from '@/components/Elements'; @@ -45,7 +45,7 @@ export const InterlaceModal = ({ const { toastError, toastSuccess, toastInfo } = useToast(); const { fetchRegions } = useRegions(); - const currentMask = regionMetadata.region.getMask().toBin(); + const currentMask = maskToBin(regionMetadata.region.getMask()); // Represents the first active bit in the bitmap. const oneStart = currentMask.indexOf('1'); // Represents the last active bit in the bitmap. @@ -55,12 +55,12 @@ export const InterlaceModal = ({ const [working, setWorking] = useState(false); const [position, setPosition] = useState(oneStart); - const newMask = CoreMask.fromChunk(oneStart, position + 1).toBin(); + const newMask = maskToBin(maskFromChunk(oneStart, position + 1)); const onInterlace = async () => { if (!api || !activeAccount || !activeSigner) return; - const mask = CoreMask.fromBin(newMask).toRawHex(); + const mask = maskFromBin(newMask); const txInterlace = api.tx.broker.interlace( regionMetadata.region.getOnChainRegionId(), diff --git a/src/contexts/regions/index.tsx b/src/contexts/regions/index.tsx index 27458187..b8950732 100644 --- a/src/contexts/regions/index.tsx +++ b/src/contexts/regions/index.tsx @@ -1,4 +1,9 @@ -import { CoreIndex, CoreMask, Region, RegionId } from 'coretime-utils'; +import { + CoreIndex, + getEncodedRegionId, + Region, + RegionId, +} from 'coretime-utils'; import React, { createContext, useCallback, @@ -58,7 +63,7 @@ const RegionDataProvider = ({ children }: Props) => { const [loading, setLoading] = useState(false); const _getTaskFromWorkloadId = useCallback( - async (core: CoreIndex, mask: CoreMask): Promise => { + async (core: CoreIndex, mask: string): Promise => { const task = await fetchRegionWorkload(core, mask); return task; }, @@ -86,7 +91,10 @@ const RegionDataProvider = ({ children }: Props) => { ) continue; - const rawId = region.getEncodedRegionId(coretimeApi).toString(); + const rawId = getEncodedRegionId( + region.getRegionId(), + coretimeApi + ).toString(); const location = RegionLocation.CORETIME_CHAIN; const name = @@ -108,7 +116,7 @@ const RegionDataProvider = ({ children }: Props) => { _regions.push( RegionMetadata.construct( context, - region.getEncodedRegionId(coretimeApi), + getEncodedRegionId(region.getRegionId(), coretimeApi), region, name, location, diff --git a/src/contexts/regions/native/index.ts b/src/contexts/regions/native/index.ts index e23540a7..353048b3 100644 --- a/src/contexts/regions/native/index.ts +++ b/src/contexts/regions/native/index.ts @@ -1,5 +1,5 @@ import { ApiPromise } from '@polkadot/api'; -import { CoreMask, Region, RegionId } from 'coretime-utils'; +import { Region, RegionId } from 'coretime-utils'; import { parseHNString } from '@/utils/functions'; @@ -21,17 +21,13 @@ export const fetchRegions = async ( const regionId = { begin: parseHNString(begin.toString()), core: parseHNString(core.toString()), - mask: new CoreMask(mask), + mask: mask, }; - return new Region( - regionId, - { - end: parseHNString(end), - owner, - paid: paid ? parseHNString(paid) : null, - }, - 0 - ); + return new Region(regionId, { + end: parseHNString(end), + owner, + paid: paid ? parseHNString(paid) : null, + }); }) .filter((entry) => !!entry) as Array; return brokerRegions; @@ -47,25 +43,17 @@ export const fetchRegion = async ( if (!coretimeApi) return null; const record: any = ( - await coretimeApi.query.broker.regions({ - begin: regionId.begin, - core: regionId.core, - mask: regionId.mask.toRawHex(), - }) + await coretimeApi.query.broker.regions(regionId) ).toHuman(); if (record) { const { end, owner, paid } = record; - return new Region( - regionId, - { - end: parseHNString(end), - owner, - paid: paid ? parseHNString(paid) : null, - }, - 0 - ); + return new Region(regionId, { + end: parseHNString(end), + owner, + paid: paid ? parseHNString(paid) : null, + }); } else { return null; } diff --git a/src/contexts/tasks/index.tsx b/src/contexts/tasks/index.tsx index 6f438b43..d98c8923 100644 --- a/src/contexts/tasks/index.tsx +++ b/src/contexts/tasks/index.tsx @@ -1,4 +1,4 @@ -import { CoreIndex, CoreMask, Region } from 'coretime-utils'; +import { CoreIndex, getEncodedRegionId } from 'coretime-utils'; import React, { createContext, useContext, useEffect, useState } from 'react'; import { parseHNString } from '@/utils/functions'; @@ -14,7 +14,7 @@ type Tasks = Record; interface TasksData { tasks: Array; fetchWorkplan: () => Promise; - fetchRegionWorkload: (_core: CoreIndex, _mask: CoreMask) => Promise; + fetchRegionWorkload: (_core: CoreIndex, _mask: string) => Promise; loadTasksFromLocalStorage: () => void; addTask: (_task: TaskMetadata) => void; } @@ -26,7 +26,7 @@ const defaultTasksData: TasksData = { }, fetchRegionWorkload: async ( _core: CoreIndex, - _mask: CoreMask + _mask: string ): Promise => { return null; }, @@ -74,16 +74,12 @@ const TaskDataProvider = ({ children }: Props) => { mask, } = record; - const region = new Region( - { - begin: parseHNString(begin.toString()), - core: parseHNString(core.toString()), - mask: new CoreMask(mask), - }, - { end: 0, owner: '', paid: null }, - 0 - ); - tasks[region.getEncodedRegionId(coretimeApi).toString()] = taskId + const regionId = { + begin: parseHNString(begin.toString()), + core: parseHNString(core.toString()), + mask, + }; + tasks[getEncodedRegionId(regionId, coretimeApi).toString()] = taskId ? parseHNString(taskId) : null; }); @@ -95,7 +91,7 @@ const TaskDataProvider = ({ children }: Props) => { // The tasks currently running on a Polkadot core. const fetchRegionWorkload = async ( core: CoreIndex, - regionMask: CoreMask + regionMask: string ): Promise => { if ( !coretimeApi || @@ -115,7 +111,7 @@ const TaskDataProvider = ({ children }: Props) => { } = workload; // Make the workload is for the specific region. - return mask == regionMask.toRawHex() ? parseHNString(taskId) : null; + return mask == regionMask ? parseHNString(taskId) : null; } return 0; }; diff --git a/src/hooks/renewableParas.tsx b/src/hooks/renewableParas.tsx index 361a9e1e..4d609d76 100644 --- a/src/hooks/renewableParas.tsx +++ b/src/hooks/renewableParas.tsx @@ -1,4 +1,3 @@ -import { CoreMask } from 'coretime-utils'; import { useEffect, useState } from 'react'; import { parseHNString } from '@/utils/functions'; @@ -10,7 +9,7 @@ type RenewableParachain = { core: number; paraID: number; price: number; - mask: CoreMask; + mask: string; when: number; }; @@ -58,7 +57,7 @@ export const useRenewableParachains = () => { parachains.push({ core, price, - mask: new CoreMask(mask), + mask, paraID: parseHNString(Task), when, }); diff --git a/src/utils/functions.test.ts b/src/utils/functions.test.ts index 53dbf63e..529403b3 100644 --- a/src/utils/functions.test.ts +++ b/src/utils/functions.test.ts @@ -1,4 +1,4 @@ -import { CoreMask, RegionId } from 'coretime-utils'; +import { completeMask, RegionId } from 'coretime-utils'; import { extractRegionIdFromRaw, @@ -33,7 +33,7 @@ describe('Util functions', () => { const regionId: RegionId = { begin: 4, core: 0, - mask: CoreMask.completeMask(), + mask: completeMask(), }; expect(extractRegionIdFromRaw(BigInt(raw))).toStrictEqual(regionId); }); diff --git a/src/utils/functions.ts b/src/utils/functions.ts index 1aa9418e..c78f02c9 100644 --- a/src/utils/functions.ts +++ b/src/utils/functions.ts @@ -2,7 +2,7 @@ import { ApiPromise } from '@polkadot/api'; import { AddressOrPair, SubmittableExtrinsic } from '@polkadot/api/types'; import { ISubmittableResult, Signer } from '@polkadot/types/types'; import { formatBalance as polkadotFormatBalance } from '@polkadot/util'; -import { CoreMask, RegionId } from 'coretime-utils'; +import { RegionId } from 'coretime-utils'; import Decimal from 'decimal.js'; import { @@ -115,7 +115,7 @@ export const extractRegionIdFromRaw = (rawRegionId: bigint): RegionId => { return { begin, core, - mask: new CoreMask(('0x' + mask.toString(16)).padEnd(22, '0')), + mask: ('0x' + mask.toString(16)).padEnd(22, '0'), }; };