2

Given the public key of a wallet, how would one get all the NFTs owned by this wallet, without using the metaplex-foundation/js package?

The reason I don't want to use metaplex's library is because they consider it experimental and hence not suitable for production.

I would still like to do it in js, with @solana/web3, but not the metaplex wrapper library because it's noted as experimental in the docs. (Yes, I know that everybody uses it anyways, but these are my employers requirements, not mine)

Thanks

3
  • you state what you don't want to use, but not what you do. shall we provide answers in python? C? COBOL?
    – trent.sol
    Commented Aug 2, 2022 at 16:26
  • 1
    I'd like to do it in js, with @solana/web3, but not the metaplex wrapper library because it's noted as experimental in the docs. (I know that everybody uses it anyways, but these are my employers requirements, not mine) Commented Aug 3, 2022 at 18:28
  • @trent.sol same here - as github.com/metaplex-foundation/js notes, metaplex is 0.x, interfaces change between versions, there's no changelog, and Metaplex can sometimes break in odd ways.
    – mikemaccana
    Commented Jan 27, 2023 at 20:18

1 Answer 1

3

One way to do it is to get all SPL token accounts and then parse the accounts where the mint meets the NFT requirements for Solana (total supply 1, no mint authority)

You can use Solana JSON RPC to retrieve all token accounts

alternatively, you can use @solana/web3 Connection

import {Connection, PublicKey} from '@solana/web3'
import { getMint } from "@solana/spl-token";


const connection = new Connection('https://api.devnet.solana.com')
const ownerAddress = new PublicKey('<YourAddress>')

// get token accounts
const token_accounts 
 = await connection.getParsedTokenAccountsByOwner(ownerAddress,{programId:<TOKEN_PROGRAM_ID>})

// parse token accounts one by one
// 1 - get mint using getMint(..)
// 2 - get Token total supply using connection.getTokenSupply(..)
// 3 - get mint authority using connection.getAccountInfo(..)
// 4 - check if the user balance is equal to 1 otherwise it's an empty token account. Use connection.getBalance(..)
// 5 - do mint checks that I specified.

This is a low-level implementation. You can refere to this code if you want an easier way.

1
  • 1
    Thanks for your answer, but actually I believe the NFT requirements on Solana include the presence of a (Master) Edition account, hence there can be a mint authority that is a PDA. But I should be able to find that by tweaking your solution. Commented Aug 8, 2022 at 15:46

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.