|
| 1 | +import { useAsync } from "react-use"; |
| 2 | +import { useContextApi } from "next-common/context/api"; |
| 3 | +import { BN_ZERO } from "@polkadot/util"; |
| 4 | +import { Option } from "@polkadot/types"; |
| 5 | +import { |
| 6 | + buildNoBytesResult, |
| 7 | + buildBaseResult, |
| 8 | + buildCompletedResult, |
| 9 | + buildPreimageForKey, |
| 10 | + decodeCallBytes, |
| 11 | + extractCallData, |
| 12 | + isHashOnlyStorageKey, |
| 13 | + parseHashOrBounded, |
| 14 | + resolveInlinePreimage, |
| 15 | +} from "./usePreimageNewCommon"; |
| 16 | + |
| 17 | +const oldPreimageResultCache = new Map(); |
| 18 | + |
| 19 | +function parseDeposit(rawDeposit) { |
| 20 | + if (!rawDeposit) return undefined; |
| 21 | + return { who: rawDeposit[0].toString(), amount: rawDeposit[1] }; |
| 22 | +} |
| 23 | + |
| 24 | +function parseStatusFor(optStatus) { |
| 25 | + const status = optStatus.unwrapOr(null); |
| 26 | + if (!status) return { status: null }; |
| 27 | + |
| 28 | + if (status.isRequested) { |
| 29 | + const req = status.asRequested; |
| 30 | + // Older versions: asRequested is an option with no structured fields. |
| 31 | + if (req instanceof Option) { |
| 32 | + return { status, statusName: "requested" }; |
| 33 | + } |
| 34 | + return { |
| 35 | + status, |
| 36 | + statusName: "requested", |
| 37 | + count: req.count.toNumber(), |
| 38 | + deposit: parseDeposit(req.deposit.unwrapOr(null)), |
| 39 | + proposalLength: req.len.unwrapOr(BN_ZERO), |
| 40 | + }; |
| 41 | + } |
| 42 | + |
| 43 | + if (status.isUnrequested) { |
| 44 | + const unreq = status.asUnrequested; |
| 45 | + // Older versions, asUnrequested is an option. |
| 46 | + if (unreq instanceof Option) { |
| 47 | + return { |
| 48 | + status, |
| 49 | + statusName: "unrequested", |
| 50 | + deposit: parseDeposit(unreq.unwrapOr(null)), |
| 51 | + }; |
| 52 | + } |
| 53 | + return { |
| 54 | + status, |
| 55 | + statusName: "unrequested", |
| 56 | + deposit: parseDeposit(unreq.deposit), |
| 57 | + proposalLength: unreq.len, |
| 58 | + }; |
| 59 | + } |
| 60 | + |
| 61 | + console.error(`Unhandled PalletPreimageRequestStatus type: ${status.type}`); |
| 62 | + return { status }; |
| 63 | +} |
| 64 | + |
| 65 | +async function fetchOldPreimage(proposalHash, api, hashOnly) { |
| 66 | + const base = buildBaseResult(proposalHash, hashOnly, api.registry, null); |
| 67 | + |
| 68 | + if (!api.query.preimage?.statusFor) return base; |
| 69 | + |
| 70 | + const optStatus = await api.query.preimage.statusFor(proposalHash); |
| 71 | + const parsedStatus = parseStatusFor(optStatus); |
| 72 | + const withStatus = { ...base, ...parsedStatus }; |
| 73 | + |
| 74 | + if (!parsedStatus.status) return withStatus; |
| 75 | + |
| 76 | + if (!api.query.preimage?.preimageFor) return withStatus; |
| 77 | + |
| 78 | + const bytesKey = buildPreimageForKey( |
| 79 | + proposalHash, |
| 80 | + withStatus.proposalLength, |
| 81 | + hashOnly, |
| 82 | + ); |
| 83 | + const optBytes = await api.query.preimage.preimageFor(...bytesKey); |
| 84 | + const callData = extractCallData(optBytes); |
| 85 | + |
| 86 | + if (!callData) return buildNoBytesResult(withStatus); |
| 87 | + |
| 88 | + const decoded = decodeCallBytes( |
| 89 | + callData, |
| 90 | + api.registry, |
| 91 | + withStatus.proposalLength, |
| 92 | + ); |
| 93 | + return buildCompletedResult(withStatus, decoded); |
| 94 | +} |
| 95 | + |
| 96 | +export default function useOldPreimage(hashOrBounded) { |
| 97 | + const api = useContextApi(); |
| 98 | + |
| 99 | + const { value, loading } = useAsync(async () => { |
| 100 | + if (!hashOrBounded || !api) return null; |
| 101 | + |
| 102 | + const { proposalHash, inlineData } = parseHashOrBounded(hashOrBounded, api); |
| 103 | + if (!proposalHash) return null; |
| 104 | + |
| 105 | + if (oldPreimageResultCache.has(proposalHash)) { |
| 106 | + return oldPreimageResultCache.get(proposalHash); |
| 107 | + } |
| 108 | + |
| 109 | + const hashOnly = isHashOnlyStorageKey(api); |
| 110 | + |
| 111 | + const result = inlineData |
| 112 | + ? resolveInlinePreimage(proposalHash, hashOnly, api, inlineData) |
| 113 | + : await fetchOldPreimage(proposalHash, api, hashOnly); |
| 114 | + |
| 115 | + if (result?.isCompleted) { |
| 116 | + oldPreimageResultCache.set(proposalHash, result); |
| 117 | + } |
| 118 | + |
| 119 | + return result; |
| 120 | + }, [hashOrBounded, api]); |
| 121 | + |
| 122 | + const isStatusLoaded = Boolean(api) && !loading; |
| 123 | + const isPreimageLoaded = |
| 124 | + Boolean(api) && !loading && Boolean(value?.isCompleted); |
| 125 | + |
| 126 | + return [value ?? {}, isStatusLoaded, isPreimageLoaded]; |
| 127 | +} |
0 commit comments