From a062a2317706186ebead48caaa67575892be118b Mon Sep 17 00:00:00 2001 From: Arjun Porwal Date: Fri, 17 Jan 2025 18:04:27 +0530 Subject: [PATCH 1/5] fix: get next asset id --- packages/page-assets/src/index.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/page-assets/src/index.tsx b/packages/page-assets/src/index.tsx index 53e1d0762211..f853a7ca23a1 100644 --- a/packages/page-assets/src/index.tsx +++ b/packages/page-assets/src/index.tsx @@ -36,7 +36,7 @@ function findOpenId (ids?: BN[]): BN { ); return lastTaken - ? lastTaken.sub(BN_ONE) + ? lastTaken.add(BN_ONE) : ids[ids.length - 1].add(BN_ONE); } From 00f75c00624594e0c132da70e23df74e9812e37a Mon Sep 17 00:00:00 2001 From: Arjun Porwal Date: Tue, 21 Jan 2025 12:57:07 +0530 Subject: [PATCH 2/5] fix: asset id bit length as prop --- packages/page-assets/src/Overview/Create/Info.tsx | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/page-assets/src/Overview/Create/Info.tsx b/packages/page-assets/src/Overview/Create/Info.tsx index e9174c120439..6d83216d76f5 100644 --- a/packages/page-assets/src/Overview/Create/Info.tsx +++ b/packages/page-assets/src/Overview/Create/Info.tsx @@ -20,6 +20,8 @@ interface Props { openId: BN; } +const ASSET_ID_BIT_LENGTH = 128; + function Info ({ assetIds, className = '', defaultValue, onChange, openId }: Props): React.ReactElement { const { t } = useTranslation(); const { api } = useApi(); @@ -114,6 +116,7 @@ function Info ({ assetIds, className = '', defaultValue, onChange, openId }: Pro Date: Tue, 21 Jan 2025 12:59:02 +0530 Subject: [PATCH 3/5] chore: type for asset id bitlength --- packages/page-assets/src/Overview/Create/Info.tsx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/page-assets/src/Overview/Create/Info.tsx b/packages/page-assets/src/Overview/Create/Info.tsx index 6d83216d76f5..07c851f5cab6 100644 --- a/packages/page-assets/src/Overview/Create/Info.tsx +++ b/packages/page-assets/src/Overview/Create/Info.tsx @@ -1,6 +1,7 @@ // Copyright 2017-2025 @polkadot/app-assets authors & contributors // SPDX-License-Identifier: Apache-2.0 +import type { BitLength } from '@polkadot/react-components/types'; import type { BN } from '@polkadot/util'; import type { InfoState } from './types.js'; @@ -20,7 +21,7 @@ interface Props { openId: BN; } -const ASSET_ID_BIT_LENGTH = 128; +const ASSET_ID_BIT_LENGTH: BitLength = 128; function Info ({ assetIds, className = '', defaultValue, onChange, openId }: Props): React.ReactElement { const { t } = useTranslation(); From 84a0a0f0894e7b319a84f34e557f4088fff28e7b Mon Sep 17 00:00:00 2001 From: Arjun Porwal Date: Tue, 21 Jan 2025 13:45:44 +0530 Subject: [PATCH 4/5] fix: special case for assethub chains --- packages/apps-config/src/api/constants.ts | 2 +- packages/page-assets/src/index.tsx | 18 ++++++++++++++---- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/packages/apps-config/src/api/constants.ts b/packages/apps-config/src/api/constants.ts index 2e1cdbcec2c7..e30e677a2fc6 100644 --- a/packages/apps-config/src/api/constants.ts +++ b/packages/apps-config/src/api/constants.ts @@ -6,7 +6,7 @@ import type { HexString } from '@polkadot/util/types'; import { knownGenesis } from '@polkadot/networks/defaults'; import { assert, BN } from '@polkadot/util'; -function getGenesis (name: string): HexString { +export function getGenesis (name: string): HexString { const network = Object.entries(knownGenesis).find(([network]) => network === name); assert(network?.[1][0], `Unable to find genesisHash for ${name}`); diff --git a/packages/page-assets/src/index.tsx b/packages/page-assets/src/index.tsx index f853a7ca23a1..53feaa55790d 100644 --- a/packages/page-assets/src/index.tsx +++ b/packages/page-assets/src/index.tsx @@ -5,12 +5,14 @@ import '@polkadot/api-augment/substrate'; import type { BN } from '@polkadot/util'; +import type { HexString } from '@polkadot/util/types'; import React, { useMemo, useRef } from 'react'; import { Route, Routes } from 'react-router'; +import { getGenesis } from '@polkadot/apps-config'; import { Tabs } from '@polkadot/react-components'; -import { useAccounts } from '@polkadot/react-hooks'; +import { useAccounts, useApi } from '@polkadot/react-hooks'; import { BN_ONE } from '@polkadot/util'; import Balances from './Balances/index.js'; @@ -24,11 +26,18 @@ interface Props { className?: string; } -function findOpenId (ids?: BN[]): BN { +// Chains in which next asset id should be incremented from 1 +const GENESIS_HASHES = [getGenesis('statemint'), getGenesis('statemine')]; + +function findOpenId (genesisHash: HexString, ids?: BN[]): BN { if (!ids?.length) { return BN_ONE; } + if (GENESIS_HASHES.map((e) => e.toLowerCase()).includes(genesisHash)) { + return ids.sort((a, b) => a.cmp(b))[ids.length - 1].add(BN_ONE); + } + const lastTaken = ids.find((id, index) => index === 0 ? !id.eq(BN_ONE) @@ -42,6 +51,7 @@ function findOpenId (ids?: BN[]): BN { function AssetApp ({ basePath, className }: Props): React.ReactElement { const { t } = useTranslation(); + const { api } = useApi(); const { hasAccounts } = useAccounts(); const ids = useAssetIds(); const infos = useAssetInfos(ids); @@ -66,8 +76,8 @@ function AssetApp ({ basePath, className }: Props): React.ReactElement { ); const openId = useMemo( - () => findOpenId(ids), - [ids] + () => findOpenId(api.genesisHash.toHex().toLowerCase() as HexString, ids), + [api.genesisHash, ids] ); return ( From 349daf245b22de79fa47a3163d4dac051289d59a Mon Sep 17 00:00:00 2001 From: Arjun Porwal Date: Tue, 21 Jan 2025 18:52:05 +0530 Subject: [PATCH 5/5] chore: remove toLowerCase() --- packages/page-assets/src/index.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/page-assets/src/index.tsx b/packages/page-assets/src/index.tsx index 53feaa55790d..b00b97a52702 100644 --- a/packages/page-assets/src/index.tsx +++ b/packages/page-assets/src/index.tsx @@ -34,7 +34,7 @@ function findOpenId (genesisHash: HexString, ids?: BN[]): BN { return BN_ONE; } - if (GENESIS_HASHES.map((e) => e.toLowerCase()).includes(genesisHash)) { + if (GENESIS_HASHES.includes(genesisHash)) { return ids.sort((a, b) => a.cmp(b))[ids.length - 1].add(BN_ONE); } @@ -76,7 +76,7 @@ function AssetApp ({ basePath, className }: Props): React.ReactElement { ); const openId = useMemo( - () => findOpenId(api.genesisHash.toHex().toLowerCase() as HexString, ids), + () => findOpenId(api.genesisHash.toHex(), ids), [api.genesisHash, ids] );