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

Support requestStatusFor for usePreimage #10310

Merged
merged 3 commits into from
Feb 29, 2024
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
1 change: 1 addition & 0 deletions packages/apps-routing/src/preimages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export default function create (t: TFunction): Route {
display: {
needsAccounts: true,
needsApi: [
'query.preimage.requestStatusFor',
'query.preimage.statusFor',
'tx.preimage.notePreimage'
]
Expand Down
5 changes: 3 additions & 2 deletions packages/page-preimages/src/usePreimages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ function filter (records: EventRecord[]): Changes<Hash> {

function usePreimagesImpl (): HexString[] | undefined {
const { api } = useApi();
const startValue = useMapKeys(api.query.preimage.statusFor, EMPTY_PARAMS, OPT_HASH);
const startValueStatusFor = useMapKeys(api.query.preimage.statusFor, EMPTY_PARAMS, OPT_HASH);
const startvalueRequstStatusFor = useMapKeys(api.query.preimage.requestStatusFor, EMPTY_PARAMS, OPT_HASH);
const hashes = useEventChanges([
api.events.preimage.Cleared,
api.events.preimage.Noted
], filter, startValue);
], filter, startValueStatusFor?.concat(startvalueRequstStatusFor || []));

return useMemo(
() => hashes?.map((h) => h.toHex()),
Expand Down
14 changes: 9 additions & 5 deletions packages/react-hooks/src/usePreimage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,9 @@ function convertDeposit (deposit?: [AccountId, u128] | null): PreimageDeposit |
}

/** @internal Returns the parameters required for a call to bytes */
function getBytesParams (interimResult: PreimageStatus, optStatus: Option<PalletPreimageRequestStatus>): BytesParams {
function getBytesParams (interimResult: PreimageStatus, someOptStatus: Option<PalletPreimageRequestStatus>): BytesParams {
const result = objectSpread<PreimageStatus>({}, interimResult, {
status: optStatus.unwrapOr(null)
status: someOptStatus.unwrapOr(null)
});

if (result.status) {
Expand Down Expand Up @@ -218,14 +218,18 @@ function usePreimageImpl (hashOrBounded?: Hash | HexString | FrameSupportPreimag
[api, hashOrBounded]
);

// api.query.preimage.statusFor has been deprecated in favor of api.query.preimage.requestStatusFor.
// To ensure we get all preimages correctly we query both storages. see: https://github.com/polkadot-js/apps/pull/10310
const optStatus = useCall<Option<PalletPreimageRequestStatus>>(!inlineData && paramsStatus && api.query.preimage?.statusFor, paramsStatus);
const optRequstStatus = useCall<Option<PalletPreimageRequestStatus>>(!inlineData && paramsStatus && api.query.preimage?.requestStatusFor, paramsStatus);
const someOptStatus = optStatus?.isSome ? optStatus : optRequstStatus;

// from the retrieved status (if any), get the on-chain stored bytes
const { paramsBytes, resultPreimageFor } = useMemo(
() => resultPreimageHash && optStatus
? getBytesParams(resultPreimageHash, optStatus)
() => resultPreimageHash && someOptStatus
? getBytesParams(resultPreimageHash, someOptStatus)
: {},
[optStatus, resultPreimageHash]
[someOptStatus, resultPreimageHash]
);

const optBytes = useCall<Option<Bytes>>(paramsBytes && api.query.preimage?.preimageFor, paramsBytes);
Expand Down
Loading