import { PublicKey } from '@solana/web3.js' ; import { useWallet } from '@oyster/common' ; import { getNativeTreasuryAddress , getRealmConfigAddress , getSignatoryRecordAddress , getTokenOwnerRecordAddress , getVoteRecordAddress , Governance , Proposal , ProposalInstruction , RealmConfigAccount , SignatoryRecord , TokenOwnerRecord , VoteRecord , } from '@solana/spl-governance' ; import { pubkeyFilter } from '@solana/spl-governance' ; import { useAccountByPda , useGovernanceAccountByPda , useGovernanceAccountByPubkey , useGovernanceAccountsByFilter , } from './accountHooks' ; import { useRpcContext } from './useRpcContext' ; export function useRealmConfig ( realm ) { const { programId } = useRpcContext ( ) ; return useGovernanceAccountByPda < RealmConfigAccount > ( RealmConfigAccount , async ( ) => { if ( ! realm ) { return ; } return await getRealmConfigAddress ( programId , realm ) ; } , [ realm ] ) ?. tryUnwrap ( ) ; } export function useGovernance ( governance ) { return useGovernanceAccountByPubkey < Governance > ( Governance , governance ) ?. tryUnwrap ( ) ; } export function useGovernancesByRealm ( realm ) { return useGovernanceAccountsByFilter < Governance > ( Governance , [ pubkeyFilter ( 1 , realm ) , ] ) ; } export function useProposal ( proposal ) { return useGovernanceAccountByPubkey < Proposal > ( Proposal , proposal ) ?. tryUnwrap ( ) ; } export function useProposalsByGovernance ( governance ) { return useGovernanceAccountsByFilter < Proposal > ( Proposal , [ pubkeyFilter ( 1 , governance ) , ] ) ; } export function useTokenOwnerRecord ( tokenOwnerRecord ) { return useGovernanceAccountByPubkey < TokenOwnerRecord > ( TokenOwnerRecord , tokenOwnerRecord ) ; } export function useTokenOwnerRecords ( realm , governingTokenMint ) { return useGovernanceAccountsByFilter < TokenOwnerRecord > ( TokenOwnerRecord , [ pubkeyFilter ( 1 , realm ) , pubkeyFilter ( 1 + 32 , governingTokenMint ) , ] ) ; } export function useWalletTokenOwnerRecord ( realm , governingTokenMint ) { const { wallet , programId } = useRpcContext ( ) ; return useGovernanceAccountByPda < TokenOwnerRecord > ( TokenOwnerRecord , async ( ) => { if ( ! realm || ! wallet ?. publicKey || ! governingTokenMint ) { return ; } return await getTokenOwnerRecordAddress ( programId , realm , governingTokenMint , wallet . publicKey ) ; } , [ wallet ?. publicKey , governingTokenMint , realm ] ) ?. tryUnwrap ( ) ; } export function useTokenOwnerRecordByOwner ( ownerPk ) { return useGovernanceAccountsByFilter < TokenOwnerRecord > ( TokenOwnerRecord , [ pubkeyFilter ( 1 + 32 + 32 , ownerPk ) , ] ) ; } export function useWalletTokenOwnerRecords ( ) { const { publicKey } = useWallet ( ) ; return useGovernanceAccountsByFilter < TokenOwnerRecord > ( TokenOwnerRecord , [ pubkeyFilter ( 1 + 32 + 32 , publicKey ) , ] ) ; } export function useProposalAuthority ( proposalOwner ) { const { publicKey , connected } = useWallet ( ) ; const tokenOwnerRecord = useTokenOwnerRecord ( proposalOwner ) ; return connected && tokenOwnerRecord ?. isSome ( ) && ( tokenOwnerRecord . value . account . governingTokenOwner . toBase58 ( ) === publicKey ?. toBase58 ( ) || tokenOwnerRecord . value . account . governanceDelegate ?. toBase58 ( ) === publicKey ?. toBase58 ( ) ) ? tokenOwnerRecord ?. tryUnwrap ( ) : undefined ; } export function useWalletSignatoryRecord ( proposal ) { const { wallet , programId } = useRpcContext ( ) ; return useGovernanceAccountByPda < SignatoryRecord > ( SignatoryRecord , async ( ) => { if ( ! proposal || ! wallet ?. publicKey ) { return ; } return await getSignatoryRecordAddress ( programId , proposal , wallet . publicKey ) ; } , [ wallet ?. publicKey , proposal ] ) ?. tryUnwrap ( ) ; } export function useSignatoriesByProposal ( proposal ) { return useGovernanceAccountsByFilter < SignatoryRecord > ( SignatoryRecord , [ pubkeyFilter ( 1 , proposal ) , ] ) ; } export function useInstructionsByProposal ( proposal ) { return useGovernanceAccountsByFilter < ProposalInstruction > ( ProposalInstruction , [ pubkeyFilter ( 1 , proposal ) ] ) ; } export const useVoteRecordsByProposal = ( proposal ) => { return useGovernanceAccountsByFilter < VoteRecord > ( VoteRecord , [ pubkeyFilter ( 1 , proposal ) , ] ) ; } ; export const useTokenOwnerVoteRecord = ( proposal , tokenOwnerRecord ) => { const { programId } = useRpcContext ( ) ; return useGovernanceAccountByPda < VoteRecord > ( VoteRecord , async ( ) => { if ( ! proposal || ! tokenOwnerRecord ) { return ; } return await getVoteRecordAddress ( programId , proposal , tokenOwnerRecord ) ; } , [ tokenOwnerRecord , proposal ] ) ; } ; export function useNativeTreasury ( governance ) { const { programId } = useRpcContext ( ) ; return useAccountByPda ( async ( ) => { if ( ! governance ) { return ; } return await getNativeTreasuryAddress ( programId , governance ) ; } , [ governance ] ) ?. tryUnwrap ( ) ; }