@macalinao/grill
Version:
Modern Solana development kit for React applications with automatic account batching, caching, and transaction notifications
43 lines • 1.19 kB
JavaScript
import { TOKEN_PROGRAM_ADDRESS } from "@solana-program/token";
import { useAssociatedTokenPda } from "./use-associated-token-pda.js";
import { useTokenAccount } from "./use-token-account.js";
/**
* Hook that computes the Associated Token Account address and fetches its data
* Combines useAssociatedTokenPda and useTokenAccount for convenience
*
* @example
* ```typescript
* const {
* address,
* tokenAccount,
* isLoading
* } = useAssociatedTokenAccount({
* mint: mintAddress,
* owner: walletAddress,
* });
*
* if (tokenAccount) {
* console.log("Balance:", tokenAccount.amount);
* }
* ```
*/
export function useAssociatedTokenAccount(options) {
const { mint, owner, tokenProgram = TOKEN_PROGRAM_ADDRESS } = options;
// Compute the ATA address
const ataAddress = useAssociatedTokenPda(mint && owner
? {
mint,
owner,
tokenProgram,
}
: null);
// Fetch the token account data
const accountResult = useTokenAccount({
address: ataAddress,
});
return {
...accountResult,
address: ataAddress,
};
}
//# sourceMappingURL=use-associated-token-account.js.map