@@ -9,6 +9,12 @@ import { useRealmByPubkeyQuery } from "@hooks/queries/realm"
99import { getNativeTreasuryAddress } from "@solana/spl-governance"
1010
1111export type MetadataKey = IdlAccounts < MythicMetadata > [ "metadataKey" ]
12+ export type MetadataItemForList = {
13+ displayName : string | undefined
14+ daoImage : string | undefined
15+ realm : PublicKey
16+ }
17+
1218const metadataProgramId = new PublicKey ( "metaThtkusoWYDvHBFXfvc93Z3d8iBeDZ4DVyq8SYVR" )
1319
1420export function useGetOnchainMetadata ( realmAddress : PublicKey | undefined ) {
@@ -19,7 +25,7 @@ export function useGetOnchainMetadata(realmAddress: PublicKey | undefined) {
1925
2026 return useQuery ( {
2127 enabled : realm !== undefined ,
22- queryKey : [ 'get-onchain= metadata' , { realmAddress : realmAddress ?. toBase58 ( ) } ] ,
28+ queryKey : [ 'get-onchain- metadata' , { realmAddress : realmAddress ?. toBase58 ( ) } ] ,
2329 queryFn : async ( ) => {
2430 if ( ! realm || ! realm . result ) {
2531 return null
@@ -65,6 +71,41 @@ export function useGetOnchainMetadata(realmAddress: PublicKey | undefined) {
6571 return null
6672 }
6773 } ,
74+ refetchOnWindowFocus : false ,
75+ staleTime : 3600000 , // 1 hour
76+ cacheTime : 3600000 * 24 * 10 ,
77+ } )
78+ }
79+
80+ export function useGetAllMetadata ( ) {
81+ const { connection } = useConnection ( )
82+ const provider = new AnchorProvider ( connection , { } as Wallet , { } )
83+ const client = new Program ( idl as MythicMetadata , metadataProgramId , provider )
84+
85+ const query = useQuery ( {
86+ queryKey : [ 'get-onchain-metadata' ] ,
87+ queryFn : async ( ) => {
88+ const metadatas = await client . account . metadata . all ( )
89+ const displayNameKeyId = metadataKeys [ 1 ] . id
90+ const daoImageKeyId = metadataKeys [ 2 ] . id
91+
92+ const metadataItems : MetadataItemForList [ ] = metadatas . map ( metadata => {
93+ const displayName = metadata . account . items . find ( i => i . metadataKeyId . eq ( displayNameKeyId ) ) ?. value . toString ( )
94+ const daoImage = metadata . account . items . find ( i => i . metadataKeyId . eq ( daoImageKeyId ) ) ?. value . toString ( )
95+
96+ return {
97+ displayName,
98+ daoImage,
99+ realm : metadata . account . subject
100+ }
101+ } )
102+
103+ return metadataItems
104+ } ,
105+ staleTime : 3600000 , // 1 hour
106+ cacheTime : 3600000 * 24 * 10 ,
68107 refetchOnWindowFocus : false
69108 } )
109+
110+ return query
70111}
0 commit comments