From a3f1baf26ffebb349accd225745c5fef2ade6107 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrian=20Brzezi=C5=84ski?= Date: Fri, 20 Oct 2023 18:18:26 +0200 Subject: [PATCH] program upgrade proposal warning (#1888) --- hooks/queries/realmConfig.ts | 2 +- .../proposal/[pk]/ProposalWarnings.tsx | 58 ++++++++++++++----- 2 files changed, 44 insertions(+), 16 deletions(-) diff --git a/hooks/queries/realmConfig.ts b/hooks/queries/realmConfig.ts index 6730078b25..f6cb11f96c 100644 --- a/hooks/queries/realmConfig.ts +++ b/hooks/queries/realmConfig.ts @@ -25,7 +25,6 @@ export const realmConfigQueryKeys = { export const useRealmConfigQuery = () => { const { connection } = useConnection() const realm = useRealmQuery().data?.result - const enabled = realm !== undefined const query = useQuery({ queryKey: enabled @@ -38,6 +37,7 @@ export const useRealmConfigQuery = () => { realm.owner, realm.pubkey ) + return asFindable(getRealmConfig)(connection, realmConfigPk) }, staleTime: 3600000, // 1 hour diff --git a/pages/dao/[symbol]/proposal/[pk]/ProposalWarnings.tsx b/pages/dao/[symbol]/proposal/[pk]/ProposalWarnings.tsx index 9403f3bdb4..7eb577a24a 100644 --- a/pages/dao/[symbol]/proposal/[pk]/ProposalWarnings.tsx +++ b/pages/dao/[symbol]/proposal/[pk]/ProposalWarnings.tsx @@ -3,7 +3,11 @@ import { useGovernanceByPubkeyQuery } from '@hooks/queries/governance' import { useSelectedProposalTransactions } from '@hooks/queries/proposalTransaction' import { useRealmConfigQuery } from '@hooks/queries/realmConfig' import useRealm from '@hooks/useRealm' -import { Proposal, getNativeTreasuryAddress } from '@solana/spl-governance' +import { + BPF_UPGRADE_LOADER_ID, + Proposal, + getNativeTreasuryAddress, +} from '@solana/spl-governance' import { useMemo } from 'react' import { useAsync } from 'react-async-hook' @@ -102,9 +106,31 @@ const PossibleWrongGovernance = () => ( ) +const ProgramUpgrade = () => ( +
+
+
+
+
+

+ Instructions like this one are dangerous +

+
+

+ This proposal upgrade program check params carefully +

+
+
+
+
+) + const useProposalSafetyCheck = (proposal: Proposal) => { const config = useRealmConfigQuery().data?.result - const { realmInfo } = useRealm() const { data: transactions } = useSelectedProposalTransactions() const governance = useGovernanceByPubkeyQuery(proposal?.governance).data @@ -123,13 +149,8 @@ const useProposalSafetyCheck = (proposal: Proposal) => { ) ) - const realmConfigWarnings = useMemo(() => { - if ( - realmInfo === undefined || - config === undefined || - transactions === undefined - ) - return [] + const proposalWarnings = useMemo(() => { + if (realmInfo === undefined || transactions === undefined) return [] const ixs = transactions.flatMap((pix) => pix.account.getAllInstructions()) @@ -140,15 +161,16 @@ const useProposalSafetyCheck = (proposal: Proposal) => { (x) => governance?.pubkey.equals(x) || treasuryAddress.result?.equals(x) ) - const realmConfigWarnings: ( + const proposalWarnings: ( | 'setGovernanceConfig' | 'setRealmConfig' | 'thirdPartyInstructionWritesConfig' | 'possibleWrongGovernance' + | 'programUpgrade' | undefined )[] = [] - realmConfigWarnings.push( + proposalWarnings.push( ...ixs.map((ix) => { if (ix.programId.equals(realmInfo.programId) && ix.data[0] === 19) { return 'setGovernanceConfig' @@ -156,9 +178,12 @@ const useProposalSafetyCheck = (proposal: Proposal) => { if (ix.programId.equals(realmInfo.programId) && ix.data[0] === 22) { return 'setRealmConfig' } + if (ix.programId.equals(BPF_UPGRADE_LOADER_ID)) { + return 'programUpgrade' + } if ( ix.accounts.find( - (a) => a.isWritable && a.pubkey.equals(config.pubkey) + (a) => a.isWritable && config && a.pubkey.equals(config.pubkey) ) !== undefined ) { if (ix.programId.equals(realmInfo.programId)) { @@ -171,10 +196,10 @@ const useProposalSafetyCheck = (proposal: Proposal) => { ) if (possibleWrongGovernance) { - realmConfigWarnings.push('possibleWrongGovernance') + proposalWarnings.push('possibleWrongGovernance') } - return realmConfigWarnings + return proposalWarnings }, [ realmInfo, config, @@ -184,7 +209,7 @@ const useProposalSafetyCheck = (proposal: Proposal) => { treasuryAddress.result, ]) - return realmConfigWarnings + return proposalWarnings } const ProposalWarnings = ({ proposal }: { proposal: Proposal }) => { @@ -199,6 +224,9 @@ const ProposalWarnings = ({ proposal }: { proposal: Proposal }) => { {warnings?.includes('possibleWrongGovernance') && ( )} + {warnings?.includes('programUpgrade') && ( + + )} ) }