From 6c384d06ed228f2a79bc399ba2a344b0f86aba81 Mon Sep 17 00:00:00 2001 From: Jaco Date: Fri, 11 Nov 2022 11:22:36 +0200 Subject: [PATCH] Fix length of gov 2 submissions (#8333) * Fix length of gov 2 submissions * No ProposedAction --- packages/page-preimages/src/types.ts | 4 ++ packages/page-preimages/src/usePreimage.ts | 40 +++++++++++-------- .../page-referenda/src/Referenda/Submit.tsx | 13 +++--- .../react-components/src/ProposedAction.tsx | 11 +++-- 4 files changed, 40 insertions(+), 28 deletions(-) diff --git a/packages/page-preimages/src/types.ts b/packages/page-preimages/src/types.ts index a446a934ad..f288ebc3cd 100644 --- a/packages/page-preimages/src/types.ts +++ b/packages/page-preimages/src/types.ts @@ -4,10 +4,14 @@ import type { Bytes } from '@polkadot/types'; import type { Call } from '@polkadot/types/interfaces'; import type { PalletPreimageRequestStatus } from '@polkadot/types/lookup'; +import type { BN } from '@polkadot/util'; +import type { HexString } from '@polkadot/util/types'; export interface Preimage { count: number; bytes: Bytes | null; proposal: Call | null; + proposalHash: HexString; + proposalLength: BN; status: PalletPreimageRequestStatus | null; } diff --git a/packages/page-preimages/src/usePreimage.ts b/packages/page-preimages/src/usePreimage.ts index 987e1b6532..0297df4b23 100644 --- a/packages/page-preimages/src/usePreimage.ts +++ b/packages/page-preimages/src/usePreimage.ts @@ -5,15 +5,16 @@ import type { ApiPromise } from '@polkadot/api'; import type { Bytes, Option } from '@polkadot/types'; import type { Call, Hash } from '@polkadot/types/interfaces'; import type { FrameSupportPreimagesBounded, PalletPreimageRequestStatus } from '@polkadot/types/lookup'; +import type { BN } from '@polkadot/util'; import type { HexString } from '@polkadot/util/types'; import type { Preimage } from './types'; import { useMemo } from 'react'; import { createNamedHook, useApi, useCall } from '@polkadot/react-hooks'; -import { isBn, isString } from '@polkadot/util'; +import { BN_ZERO, isBn, isString } from '@polkadot/util'; -function createResult (api: ApiPromise, optStatus: Option, optBytes: Option): Preimage { +function createResult (api: ApiPromise, optStatus: Option, optBytes: Option, [proposalHash, proposalLength]: [HexString, BN]): Preimage { const status = optStatus.unwrapOr(null); const bytes = optBytes.unwrapOr(null); let count = 0; @@ -41,20 +42,23 @@ function createResult (api: ApiPromise, optStatus: Option): unknown[] { +function getParams (hash: Hash | HexString, optStatus: Option): [HexString, BN] { const status = optStatus.unwrapOr(null); + const hexHash = isString(hash) + ? hash + : hash.toHex(); - return [ - status - ? status.isRequested - ? [hash, status.asRequested.len.unwrapOr(0)] - : [hash, status.asUnrequested.len] - : [hash, 0] - ]; + return status + ? status.isRequested + ? [hexHash, status.asRequested.len.unwrapOr(BN_ZERO)] + : [hexHash, status.asUnrequested.len] + : [hexHash, BN_ZERO]; } export function getPreimageHash (hashOrBounded: Hash | HexString | FrameSupportPreimagesBounded): HexString { @@ -73,26 +77,28 @@ export function getPreimageHash (hashOrBounded: Hash | HexString | FrameSupportP : hashOrBounded.toHex(); } -function usePreimageImpl (hashOrBounded: Hash | HexString | FrameSupportPreimagesBounded): Preimage | undefined { +function usePreimageImpl (hashOrBounded?: Hash | HexString | FrameSupportPreimagesBounded | null): Preimage | undefined { const { api } = useApi(); const hash = useMemo( - () => getPreimageHash(hashOrBounded), + () => hashOrBounded && getPreimageHash(hashOrBounded), [hashOrBounded] ); - const optStatus = useCall>(api.query.preimage.statusFor, [hash]); + const optStatus = useCall>(hash && api.query.preimage.statusFor, [hash]); const params = useMemo( - () => optStatus && getParams(hash, optStatus), + () => hash && optStatus && getParams(hash, optStatus), [hash, optStatus] ); - const optBytes = useCall>(params && api.query.preimage.preimageFor, params); + const optBytes = useCall>(params && api.query.preimage.preimageFor, [params]); return useMemo( - () => optBytes && optStatus && createResult(api, optStatus, optBytes), - [api, optBytes, optStatus] + () => optBytes && optStatus && params + ? createResult(api, optStatus, optBytes, params) + : undefined, + [api, optBytes, optStatus, params] ); } diff --git a/packages/page-referenda/src/Referenda/Submit.tsx b/packages/page-referenda/src/Referenda/Submit.tsx index 566ea190b5..f869e0ad1f 100644 --- a/packages/page-referenda/src/Referenda/Submit.tsx +++ b/packages/page-referenda/src/Referenda/Submit.tsx @@ -5,11 +5,13 @@ import type { ApiPromise } from '@polkadot/api'; import type { RawParam } from '@polkadot/react-params/types'; import type { PalletReferendaTrackInfo } from '@polkadot/types/lookup'; import type { BN } from '@polkadot/util'; +import type { HexString } from '@polkadot/util/types'; import type { PalletReferenda } from '../types'; import React, { useCallback, useEffect, useMemo, useState } from 'react'; import styled from 'styled-components'; +import usePreimage from '@polkadot/app-preimages/usePreimage'; import { getGovernanceTracks } from '@polkadot/apps-config'; import { Button, Dropdown, Input, InputAddress, InputBalance, Modal, TxButton } from '@polkadot/react-components'; import { useApi, useBestNumber, useToggle } from '@polkadot/react-hooks'; @@ -30,7 +32,7 @@ interface Props { } interface HashState { - hash?: string; + hash?: HexString | null; isHashValid: boolean; } @@ -94,7 +96,8 @@ function Submit ({ className = '', isMember, members, palletReferenda, tracks }: const [origin, setOrigin] = useState(null); const [atAfter, setAtAfter] = useState(null); const [defaultAtAfter, setDefaultAtAfter] = useState(null); - const [{ hash, isHashValid }, setHash] = useState({ hash: '', isHashValid: false }); + const [{ hash, isHashValid }, setHash] = useState({ hash: null, isHashValid: false }); + const preimage = usePreimage(hash); useEffect((): void => { tracks && trackId !== undefined && bestNumber && setDefaultAtAfter((prev) => @@ -152,7 +155,7 @@ function Submit ({ className = '', isMember, members, palletReferenda, tracks }: const _onChangeHash = useCallback( (hash?: string) => - setHash({ hash, isHashValid: isHex(hash, 256) }), + setHash({ hash: hash as HexString, isHashValid: isHex(hash, 256) }), [] ); @@ -230,10 +233,10 @@ function Submit ({ className = '', isMember, members, palletReferenda, tracks }: ('Submit proposal')} onStart={toggleSubmit} - params={[originParam || origin, { Lookup: hash }, atAfter]} + params={[originParam || origin, { Lookup: { hash: preimage?.proposalHash, len: preimage?.proposalLength } }, atAfter]} tx={api.tx[palletReferenda as 'referenda'].submit} /> diff --git a/packages/react-components/src/ProposedAction.tsx b/packages/react-components/src/ProposedAction.tsx index 0685f4aacc..d0044d8348 100644 --- a/packages/react-components/src/ProposedAction.tsx +++ b/packages/react-components/src/ProposedAction.tsx @@ -7,7 +7,7 @@ import type { BN } from '@polkadot/util'; import React from 'react'; import styled from 'styled-components'; -import { formatNumber, isString } from '@polkadot/util'; +import { formatNumber, isString, isUndefined } from '@polkadot/util'; import CallDisplay from './Call'; import Expander from './Expander'; @@ -18,26 +18,25 @@ import { isTreasuryProposalVote } from './util'; interface Props { className?: string; proposal?: Call | null; - idNumber: BN | number | string; + idNumber?: BN | number | string; withLinks?: boolean; expandNested?: boolean; } function ProposedAction ({ className = '', expandNested, idNumber, proposal, withLinks }: Props): React.ReactElement { const { t } = useTranslation(); - const stringId = isString(idNumber) + const stringId = isString(idNumber) || isUndefined(idNumber) ? idNumber : formatNumber(idNumber); if (!proposal) { return ( -

#{stringId} {t('No execution details available for this proposal')}

+

{stringId ? `#${stringId}: ` : ''}{t('No execution details available for this proposal')}

); } const { meta, method, section } = proposal.registry.findMetaCall(proposal.callIndex); - - const header = `#${stringId}: ${section}.${method}`; + const header = `${stringId ? `#${stringId}: ` : ''}${section}.${method}`; return (