Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: get next asset id issue #11213

Merged
merged 5 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/apps-config/src/api/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
Expand Down
4 changes: 4 additions & 0 deletions packages/page-assets/src/Overview/Create/Info.tsx
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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();
Expand Down Expand Up @@ -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}
Expand Down
20 changes: 15 additions & 5 deletions packages/page-assets/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -24,24 +26,32 @@ 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)
: !id.sub(BN_ONE).eq(ids[index - 1])
);

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);
Expand All @@ -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 (
Expand Down
Loading