I built a ui using NextJS and the Solana wallet adapter library. I'm using getParsedTokenAccountsByowner
to display all token accounts when the user connects their wallet. I want to map over all these token accounts and add a key value pair of isMintAuthority to them so I can display a "Mint Tokens" button to the user, IF they have mintAuthority. Here's the react-query hook I put together:
export function useGetMintAuthority({ mintAddress }: { mintAddress: PublicKey }) {
const { connection } = useConnection();
return useQuery({
queryKey: ["get-mint-authority", { endpoint: connection.rpcEndpoint, mintAddress }],
queryFn: async () => {
// Fetch the account info for the mint address
const accountInfo = await connection.getAccountInfo(mintAddress);
if (!accountInfo) throw new Error("Failed to find mint account");
// Parse the account info to get the mint data
const mintData = MintLayout.decode(accountInfo.data);
// The mint authority is an optional field, so it could be null
const mintAuthority = mintData.mintAuthority ? new PublicKey(mintData.mintAuthority) : null;
return mintAuthority;
},
});
}
The problem is this only works for one account at a time. Without getting into the limitations and headache of React Hooks inside loops or conditionals and the asynchronous nature of fetching mint authority. Is there a best way or recommended way to do this?