Skip to content

Commit

Permalink
program upgrade proposal warning (#1888)
Browse files Browse the repository at this point in the history
  • Loading branch information
abrzezinski94 authored Oct 20, 2023
1 parent 4b7068f commit a3f1baf
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 16 deletions.
2 changes: 1 addition & 1 deletion hooks/queries/realmConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -38,6 +37,7 @@ export const useRealmConfigQuery = () => {
realm.owner,
realm.pubkey
)

return asFindable(getRealmConfig)(connection, realmConfigPk)
},
staleTime: 3600000, // 1 hour
Expand Down
58 changes: 43 additions & 15 deletions pages/dao/[symbol]/proposal/[pk]/ProposalWarnings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -102,9 +106,31 @@ const PossibleWrongGovernance = () => (
</div>
)

const ProgramUpgrade = () => (
<div className="rounded-md bg-yellow-50 p-4">
<div className="flex">
<div className="flex-shrink-0">
<ExclamationCircleIcon
className="h-5 w-5 text-yellow-400"
aria-hidden="true"
/>
</div>
<div className="ml-3">
<h3 className="text-sm font-medium text-yellow-800">
Instructions like this one are dangerous
</h3>
<div className="mt-2">
<p className="text-sm text-yellow-700">
This proposal upgrade program check params carefully
</p>
</div>
</div>
</div>
</div>
)

const useProposalSafetyCheck = (proposal: Proposal) => {
const config = useRealmConfigQuery().data?.result

const { realmInfo } = useRealm()
const { data: transactions } = useSelectedProposalTransactions()
const governance = useGovernanceByPubkeyQuery(proposal?.governance).data
Expand All @@ -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())

Expand All @@ -140,25 +161,29 @@ 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'
}
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)) {
Expand All @@ -171,10 +196,10 @@ const useProposalSafetyCheck = (proposal: Proposal) => {
)

if (possibleWrongGovernance) {
realmConfigWarnings.push('possibleWrongGovernance')
proposalWarnings.push('possibleWrongGovernance')
}

return realmConfigWarnings
return proposalWarnings
}, [
realmInfo,
config,
Expand All @@ -184,7 +209,7 @@ const useProposalSafetyCheck = (proposal: Proposal) => {
treasuryAddress.result,
])

return realmConfigWarnings
return proposalWarnings
}

const ProposalWarnings = ({ proposal }: { proposal: Proposal }) => {
Expand All @@ -199,6 +224,9 @@ const ProposalWarnings = ({ proposal }: { proposal: Proposal }) => {
{warnings?.includes('possibleWrongGovernance') && (
<PossibleWrongGovernance></PossibleWrongGovernance>
)}
{warnings?.includes('programUpgrade') && (
<ProgramUpgrade></ProgramUpgrade>
)}
</>
)
}
Expand Down

1 comment on commit a3f1baf

@vercel
Copy link

@vercel vercel bot commented on a3f1baf Oct 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.