-
Notifications
You must be signed in to change notification settings - Fork 306
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add squads program auth integration (#379)
- Loading branch information
Showing
8 changed files
with
299 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
'use client'; | ||
|
||
import { ParsedAccountRenderer } from '@components/account/ParsedAccountRenderer'; | ||
import React from 'react'; | ||
|
||
import { ProgramMultisigCard } from '@/app/components/account/ProgramMultisigCard'; | ||
|
||
type Props = Readonly<{ | ||
params: { | ||
address: string; | ||
}; | ||
}>; | ||
|
||
function ProgramMultisigCardRenderer({ | ||
account, | ||
onNotFound, | ||
}: React.ComponentProps<React.ComponentProps<typeof ParsedAccountRenderer>['renderComponent']>) { | ||
const parsedData = account?.data?.parsed; | ||
if (!parsedData || parsedData?.program !== 'bpf-upgradeable-loader') { | ||
return onNotFound(); | ||
} | ||
return <ProgramMultisigCard data={parsedData} />; | ||
} | ||
|
||
export default function ProgramMultisigPageClient({ params: { address } }: Props) { | ||
return <ParsedAccountRenderer address={address} renderComponent={ProgramMultisigCardRenderer} />; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import getReadableTitleFromAddress, { AddressPageMetadataProps } from '@utils/get-readable-title-from-address'; | ||
import { Metadata } from 'next/types'; | ||
|
||
import ProgramMultisigPageClient from './page-client'; | ||
|
||
export async function generateMetadata(props: AddressPageMetadataProps): Promise<Metadata> { | ||
return { | ||
description: `Multisig information for the upgrade authority of the program with address ${props.params.address} on Solana`, | ||
title: `Upgrade Authority Multisig | ${await getReadableTitleFromAddress(props)} | Solana`, | ||
}; | ||
} | ||
|
||
type Props = Readonly<{ | ||
params: { | ||
address: string; | ||
}; | ||
}>; | ||
|
||
export default function ProgramMultisigPage(props: Props) { | ||
return <ProgramMultisigPageClient {...props} />; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { PublicKey } from '@solana/web3.js'; | ||
import { Suspense } from 'react'; | ||
|
||
import { UpgradeableLoaderAccountData } from '@/app/providers/accounts'; | ||
import { useAnchorProgram } from '@/app/providers/anchor'; | ||
import { useCluster } from '@/app/providers/cluster'; | ||
import { | ||
SQUADS_V3_ADDRESS, | ||
SQUADS_V4_ADDRESS, | ||
useSquadsMultisig, | ||
useSquadsMultisigLookup, | ||
} from '@/app/providers/squadsMultisig'; | ||
|
||
import { Address } from '../common/Address'; | ||
import { LoadingCard } from '../common/LoadingCard'; | ||
import { TableCardBody } from '../common/TableCardBody'; | ||
|
||
export function ProgramMultisigCard({ data }: { data: UpgradeableLoaderAccountData }) { | ||
return ( | ||
<Suspense fallback={<LoadingCard message="Loading multisig information" />}> | ||
<ProgramMultisigCardInner programAuthority={data.programData?.authority} /> | ||
</Suspense> | ||
); | ||
} | ||
|
||
function ProgramMultisigCardInner({ programAuthority }: { programAuthority: PublicKey | null | undefined }) { | ||
const { cluster, url } = useCluster(); | ||
const { data: squadMapInfo } = useSquadsMultisigLookup(programAuthority, cluster); | ||
const anchorProgram = useAnchorProgram(squadMapInfo?.version === 'v3' ? SQUADS_V3_ADDRESS : SQUADS_V4_ADDRESS, url); | ||
const { data: squadInfo } = useSquadsMultisig( | ||
anchorProgram.program, | ||
squadMapInfo?.multisig, | ||
cluster, | ||
squadMapInfo?.version | ||
); | ||
|
||
let members: PublicKey[]; | ||
if (squadInfo !== undefined && squadInfo?.version === 'v4') { | ||
members = squadInfo.multisig.members.map(obj => obj.key) ?? []; | ||
} else { | ||
members = squadInfo?.multisig.keys ?? []; | ||
} | ||
|
||
return ( | ||
<div className="card security-txt"> | ||
<div className="card-header"> | ||
<h3 className="card-header-title mb-0 d-flex align-items-center"> | ||
Upgrade Authority Multisig Information | ||
</h3> | ||
</div> | ||
<TableCardBody> | ||
<tr> | ||
<td>Multisig Program</td> | ||
<td className="text-lg-end">{squadMapInfo?.version === 'v4' ? 'Squads V4' : 'Squads V3'}</td> | ||
</tr> | ||
<tr> | ||
<td>Multisig Program Id</td> | ||
<td className="text-lg-end"> | ||
<Address | ||
pubkey={ | ||
new PublicKey(squadMapInfo?.version === 'v4' ? SQUADS_V4_ADDRESS : SQUADS_V3_ADDRESS) | ||
} | ||
alignRight | ||
link | ||
/> | ||
</td> | ||
</tr> | ||
<tr> | ||
<td>Multisig Account</td> | ||
<td className="text-lg-end"> | ||
{squadMapInfo?.isSquad ? ( | ||
<Address pubkey={new PublicKey(squadMapInfo.multisig)} alignRight link /> | ||
) : null} | ||
</td> | ||
</tr> | ||
<tr> | ||
<td>Multisig Approval Threshold</td> | ||
<td className="text-lg-end"> | ||
{squadInfo?.multisig.threshold} | ||
{' of '} | ||
{squadInfo?.version === 'v4' | ||
? squadInfo?.multisig.members.length | ||
: squadInfo?.multisig.keys.length} | ||
</td> | ||
</tr> | ||
{members.map((member, idx) => ( | ||
<tr key={idx}> | ||
<td>Multisig Member {idx + 1}</td> | ||
<td className="text-lg-end"> | ||
<Address pubkey={member} alignRight link /> | ||
</td> | ||
</tr> | ||
))} | ||
</TableCardBody> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { Program } from '@coral-xyz/anchor'; | ||
import { PublicKey } from '@solana/web3.js'; | ||
import { Cluster } from '@utils/cluster'; | ||
import useSWRImmutable from 'swr/immutable'; | ||
|
||
export const SQUADS_V3_ADDRESS = 'SMPLecH534NA9acpos4G6x7uf3LWbCAwZQE9e8ZekMu'; | ||
export const SQUADS_V4_ADDRESS = 'SQDS4ep65T869zMMBKyuUq6aD6EgTu8psMjkvj52pCf'; | ||
|
||
export type SquadsMultisigVersion = 'v3' | 'v4'; | ||
export type SquadsMultisigMapInfo = { | ||
isSquad: boolean; | ||
version: SquadsMultisigVersion; | ||
multisig: string; | ||
}; | ||
export type MinimalMultisigInfo = | ||
| { version: 'v4'; multisig: { threshold: number; members: Array<{ key: PublicKey }> } } | ||
| { version: 'v3'; multisig: { threshold: number; keys: Array<PublicKey> } }; | ||
|
||
const SQUADS_MAP_URL = 'https://4fnetmviidiqkjzenwxe66vgoa0soerr.lambda-url.us-east-1.on.aws/isSquadV2'; | ||
|
||
// Squads Multisig reverse map info is only available on mainnet | ||
export function useSquadsMultisigLookup(programAuthority: PublicKey | null | undefined, cluster: Cluster) { | ||
return useSWRImmutable<SquadsMultisigMapInfo | null>( | ||
['squadsReverseMap', programAuthority?.toString(), cluster], | ||
async ([_prefix, programIdString, cluster]: [string, string | undefined, Cluster]) => { | ||
if (cluster !== Cluster.MainnetBeta || !programIdString) { | ||
return null; | ||
} | ||
const response = await fetch(`${SQUADS_MAP_URL}/${programIdString}`); | ||
const data = await response.json(); | ||
return 'error' in data ? null : (data as SquadsMultisigMapInfo); | ||
}, | ||
{ suspense: true } | ||
); | ||
} | ||
|
||
export function useSquadsMultisig( | ||
anchorProgram: Program | null | undefined, | ||
multisig: string | undefined, | ||
cluster: Cluster, | ||
version: SquadsMultisigVersion | undefined | ||
) { | ||
return useSWRImmutable<MinimalMultisigInfo | null>( | ||
['squadsMultisig', multisig, cluster], | ||
async ([_prefix, multisig, cluster]: [string, string | undefined, Cluster]) => { | ||
if (cluster !== Cluster.MainnetBeta || !multisig || !version) { | ||
return null; | ||
} | ||
if (version === 'v4') { | ||
const multisigInfo = await (anchorProgram?.account as unknown as any).multisig.fetch( | ||
multisig, | ||
'confirmed' | ||
); | ||
return { | ||
multisig: multisigInfo, | ||
version, | ||
}; | ||
} else if (version === 'v3') { | ||
const multisigInfo = await (anchorProgram?.account as unknown as any).ms.fetch(multisig, 'confirmed'); | ||
return { | ||
multisig: multisigInfo, | ||
version, | ||
}; | ||
} else { | ||
return null; | ||
} | ||
} | ||
); | ||
} |
Oops, something went wrong.