1

I am trying to fetch an NFT's token account through Metaplex, and I am not sure which function works. I've tried

const nft = await metaplex.nfts().findByMint({ mintAddress: nftMint });

But that doesn't return the token account.

Any responses are greatly appreciated :)

2 Answers 2

2

https://solanacookbook.com/references/nfts.html#how-to-get-the-owner-of-an-nft

If you have the mint key of an NFT, you can find its current owner by sneak-peeking at the largest token account for that mint key.

Remember that NFTs have a supply of 1, and they are indivisible, meaning that only one token account will hold that token at any point in time, whilst all other token accounts for that mint key will have a balance of 0.

Once the largest token account is identified, we can retrieve its owner.

const largestAccounts = await connection.getTokenLargestAccounts(
  new PublicKey(tokenMint)
);
const largestAccountInfo = await connection.getParsedAccountInfo(
  largestAccounts.value[0].address
);
console.log(largestAccountInfo.value.data.parsed.info.owner);
1
  • Thanks for the response, but I found a much simpler method. Turns out that in Solana's SPL Token Library, there is a function called "getAssociatedTokenAddress". I answered the question.
    – incog
    Commented Nov 9, 2022 at 15:52
0

So I ended up finding an answer to this problem:

Turns out Solana's SPL Token Library has a method for this already: enter image description here

Here is how I used the code:

import { getAssociatedTokenAddress } from "@solana/spl-token";
const tokenAcc = await getAssociatedTokenAddress(mint, this.publicKey);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.