fix: get next asset id issue (#11213)

* fix: get next asset id

* fix: asset id bit length as prop

* chore: type for asset id bitlength

* fix: special case for assethub chains

* chore: remove toLowerCase()
This commit is contained in:
Arjun Porwal 2025-01-21 21:17:30 +05:30 committed by GitHub
parent 7d8c54c018
commit ee75d54869
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 20 additions and 6 deletions

View File

@ -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}`);

View File

@ -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,6 +21,8 @@ interface Props {
openId: BN;
}
const ASSET_ID_BIT_LENGTH: BitLength = 128;
function Info ({ assetIds, className = '', defaultValue, onChange, openId }: Props): React.ReactElement<Props> {
const { t } = useTranslation();
const { api } = useApi();
@ -114,6 +117,7 @@ function Info ({ assetIds, className = '', defaultValue, onChange, openId }: Pro
</Modal.Columns>
<Modal.Columns hint={t('The selected id for the asset. This should not match an already-existing asset id.')}>
<InputNumber
bitLength={ASSET_ID_BIT_LENGTH}
defaultValue={initial?.assetId || initialId}
isError={!isValidId}
isZeroable={false}

View File

@ -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.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)
@ -36,12 +45,13 @@ function findOpenId (ids?: BN[]): BN {
);
return lastTaken
? lastTaken.sub(BN_ONE)
? lastTaken.add(BN_ONE)
: ids[ids.length - 1].add(BN_ONE);
}
function AssetApp ({ basePath, className }: Props): React.ReactElement<Props> {
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<Props> {
);
const openId = useMemo(
() => findOpenId(ids),
[ids]
() => findOpenId(api.genesisHash.toHex(), ids),
[api.genesisHash, ids]
);
return (