|
| 1 | +// Copyright (c) 2026 IOTA Stiftung |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +import { useGetTransaction } from '@iota/core'; |
| 5 | +import { useIotaClientQuery } from '@iota/dapp-kit'; |
| 6 | +import { normalizeIotaAddress } from '@iota/iota-sdk/utils'; |
| 7 | +import { useMemo } from 'react'; |
| 8 | +import { UPGRADE_POLICIES, type UpgradePolicyInfo } from '~/lib'; |
| 9 | + |
| 10 | +const UPGRADE_CAP_TYPE = '0x2::package::UpgradeCap'; |
| 11 | + |
| 12 | +const IMMUTABLE_POLICY: UpgradePolicyInfo = { |
| 13 | + label: 'Immutable', |
| 14 | + description: 'Prevents any upgrades to the package. The UpgradeCap has been destroyed.', |
| 15 | + isImmutable: true, |
| 16 | +}; |
| 17 | + |
| 18 | +const CUSTOM_POLICY: UpgradePolicyInfo = { |
| 19 | + label: 'Custom', |
| 20 | + description: |
| 21 | + 'The UpgradeCap is wrapped inside a custom policy object. The package may still be upgradeable under custom conditions.', |
| 22 | + isImmutable: false, |
| 23 | +}; |
| 24 | + |
| 25 | +const MAKE_IMMUTABLE_FUNCTION = { |
| 26 | + package: '0x2', |
| 27 | + module: 'package', |
| 28 | + function: 'make_immutable', |
| 29 | +} as const; |
| 30 | + |
| 31 | +export function usePackageUpgradePolicy(txDigest: string | null | undefined): { |
| 32 | + upgradePolicy: UpgradePolicyInfo | null; |
| 33 | + isPending: boolean; |
| 34 | +} { |
| 35 | + const { data: txnData, isPending: isTxPending } = useGetTransaction(txDigest ?? ''); |
| 36 | + |
| 37 | + const upgradeCapObjectId = useMemo(() => { |
| 38 | + if (!txnData?.objectChanges) return undefined; |
| 39 | + const upgradeCapChange = txnData.objectChanges.find( |
| 40 | + (change) => |
| 41 | + change.type === 'created' && |
| 42 | + 'objectType' in change && |
| 43 | + change.objectType === UPGRADE_CAP_TYPE, |
| 44 | + ); |
| 45 | + return upgradeCapChange && 'objectId' in upgradeCapChange |
| 46 | + ? upgradeCapChange.objectId |
| 47 | + : undefined; |
| 48 | + }, [txnData?.objectChanges]); |
| 49 | + |
| 50 | + const { data: upgradeCapData, isPending: isUpgradeCapPending } = useIotaClientQuery( |
| 51 | + 'getObject', |
| 52 | + { |
| 53 | + id: upgradeCapObjectId!, |
| 54 | + options: { showContent: true }, |
| 55 | + }, |
| 56 | + { |
| 57 | + enabled: !!upgradeCapObjectId, |
| 58 | + }, |
| 59 | + ); |
| 60 | + |
| 61 | + // When the UpgradeCap is not accessible (deleted or wrapped), check whether |
| 62 | + // it was destroyed via `make_immutable` or wrapped in a custom policy object. |
| 63 | + const upgradeCapMissing = |
| 64 | + !!upgradeCapObjectId && |
| 65 | + !isUpgradeCapPending && |
| 66 | + (!!upgradeCapData?.error || !upgradeCapData?.data); |
| 67 | + |
| 68 | + const { data: lastCapTxData, isPending: isLastCapTxPending } = useIotaClientQuery( |
| 69 | + 'queryTransactionBlocks', |
| 70 | + { |
| 71 | + filter: { InputObject: upgradeCapObjectId! }, |
| 72 | + options: { showInput: true }, |
| 73 | + order: 'descending', |
| 74 | + limit: 1, |
| 75 | + }, |
| 76 | + { |
| 77 | + enabled: upgradeCapMissing, |
| 78 | + }, |
| 79 | + ); |
| 80 | + |
| 81 | + const upgradePolicy = useMemo<UpgradePolicyInfo | null>(() => { |
| 82 | + const isUpgradeCapLoading = !!upgradeCapObjectId && isUpgradeCapPending; |
| 83 | + const isLastCapTxLoading = upgradeCapMissing && isLastCapTxPending; |
| 84 | + |
| 85 | + if (!txDigest || isTxPending || isUpgradeCapLoading || isLastCapTxLoading) { |
| 86 | + return null; |
| 87 | + } |
| 88 | + |
| 89 | + if (!upgradeCapObjectId) { |
| 90 | + return IMMUTABLE_POLICY; |
| 91 | + } |
| 92 | + |
| 93 | + // UpgradeCap exists and is accessible: read the policy field |
| 94 | + if (upgradeCapData?.data) { |
| 95 | + const content = upgradeCapData.data.content; |
| 96 | + if (content?.dataType === 'moveObject' && content.fields) { |
| 97 | + const fields = content.fields as Record<string, unknown>; |
| 98 | + const policy = Number(fields.policy); |
| 99 | + const policyInfo = UPGRADE_POLICIES[policy]; |
| 100 | + return { |
| 101 | + label: policyInfo?.label ?? `Unknown (${policy})`, |
| 102 | + description: policyInfo?.description ?? '', |
| 103 | + isImmutable: false, |
| 104 | + }; |
| 105 | + } |
| 106 | + return null; |
| 107 | + } |
| 108 | + |
| 109 | + // UpgradeCap is missing: determine if it was destroyed or wrapped |
| 110 | + const lastTx = lastCapTxData?.data?.[0]; |
| 111 | + if (lastTx?.transaction?.data?.transaction?.kind === 'ProgrammableTransaction') { |
| 112 | + const transactions = lastTx.transaction.data.transaction.transactions; |
| 113 | + const normalizedPkg = normalizeIotaAddress(MAKE_IMMUTABLE_FUNCTION.package); |
| 114 | + const wasMadeImmutable = transactions.some( |
| 115 | + (tx) => |
| 116 | + 'MoveCall' in tx && |
| 117 | + normalizeIotaAddress(tx.MoveCall.package) === normalizedPkg && |
| 118 | + tx.MoveCall.module === MAKE_IMMUTABLE_FUNCTION.module && |
| 119 | + tx.MoveCall.function === MAKE_IMMUTABLE_FUNCTION.function, |
| 120 | + ); |
| 121 | + return wasMadeImmutable ? IMMUTABLE_POLICY : CUSTOM_POLICY; |
| 122 | + } |
| 123 | + |
| 124 | + return null; |
| 125 | + }, [ |
| 126 | + txDigest, |
| 127 | + upgradeCapObjectId, |
| 128 | + upgradeCapData, |
| 129 | + upgradeCapMissing, |
| 130 | + lastCapTxData, |
| 131 | + isTxPending, |
| 132 | + isUpgradeCapPending, |
| 133 | + isLastCapTxPending, |
| 134 | + ]); |
| 135 | + |
| 136 | + const isPending = |
| 137 | + !!txDigest && |
| 138 | + (isTxPending || |
| 139 | + (!!upgradeCapObjectId && isUpgradeCapPending) || |
| 140 | + (upgradeCapMissing && isLastCapTxPending)); |
| 141 | + |
| 142 | + return { |
| 143 | + upgradePolicy, |
| 144 | + isPending, |
| 145 | + }; |
| 146 | +} |
0 commit comments