1

I am struggling with the metaplex.nfts().loadNft():

https://github.com/metaplex-foundation/js#loadnft

I am getting a list of NFT from a wallet using findAllByOwner(). But, it says lazy = true.

So, I am trying to load the NFT and not the LazyNFT:

// Get list of NFTs
const holdingNFTs = await metaplex.nfts().findAllByOwner(wallet).run();

const lazyNft = holdingNFTs[0];
const nft = await metaplex.nfts().loadNft(lazyNft, { commitment: 'confirmed' } ).run();

And I am getting this error:

Argument of type 'Nft | LazyNft' is not assignable to parameter of type 'LazyNft'. Type 'Nft' is not assignable to type 'LazyNft'. Type 'Nft' is not assignable to type 'Readonly<{ lazy: true; }>'. Types of property 'lazy' are incompatible. Type 'false' is not assignable to type 'true'.

Any ideas? What am I doing wrong? I couldn't find any code examples.

Thanks!

4 Answers 4

2

Here is also an answer I just crafted today. Not the most optimal one but one that includes type

const mx = Metaplex.make(connection);
    if (!data) {
                // find All nfts owned by an address
                const userNfts: (Nft | LazyNft)[] = await mx
                  .nfts()
                  .findAllByOwner(userPublicKey)
                  .run();
                if (userNfts) {
                  // we might get nfts without metadata (LazyNfts). We have to then query for
                  // metadata extra
                    const loadedLazyNfts = Promise.all(userNfts.map((async (eachLazyNft: Nft | LazyNft) => {
                      if (eachLazyNft.lazy) {
                        return mx.nfts().loadNft(eachLazyNft).run();
                      }
                      return eachLazyNft
                    }) ));
                    loadedLazyNfts
                      .then((allLoadedNfts) => {
                        setData(allLoadedNfts);
                        setFetchError(false)
                        setIsLoading(false)
    
                        console.log(allLoadedNfts, 'allLoadedNfts')
                      } )
1
  • You don't need to use .then() since you have async/await (and you're using that elsewhere). Just do const loadedLazyNfts = await Promise.all(...); setData(loadedLazyNfts);
    – mikemaccana
    Aug 17, 2022 at 10:38
0

My mistake was use TypeScript instead of Javascript.

Here is the complete code working to fetch the NFTs from a wallet as LazyNFT and then load them to NFT type.

import { Metaplex, guestIdentity } from "@metaplex-foundation/js";
import { Connection, clusterApiUrl, PublicKey } from "@solana/web3.js";

const connection = new Connection(clusterApiUrl("mainnet-beta"));
const metaplex = new Metaplex(connection).use(guestIdentity());

const wallet = new PublicKey("WALLET");

const lazyNfts = await metaplex.nfts().findAllByOwner(wallet).run();
const lazy = lazyNfts[0];
console.log(lazy.lazy);

const nft = await metaplex.nfts().loadNft(lazy, {commitment: 'confirmed'}).run();
console.log(nft.json.image);
4
  • 1
    You should be able to use typescript? The JS SDK was written in TypeScript and as a result is of course fully typed.
    – Sammy
    Jul 25, 2022 at 18:07
  • please annotated the code to point out the differences and why they make it work vs what you tried before
    – trent.sol
    Jul 25, 2022 at 18:17
  • The code is the same, my mistake was using TypeScript instead Javascript. Just changed to Javascript and works fine.
    – Andres
    Jul 25, 2022 at 21:02
  • This is a workaround (as is the other suggested answer using type coersion) rather than an answer.
    – mikemaccana
    Aug 17, 2022 at 10:40
0

Typescript error says that you use the type that is either LazyNFT or NFT for a method that takes only LazyNFT. It prevents you from passing NFT type to this method. You can avoid this type of error if you use as LazyNFT for a value.

0

@andres, your solution doesn't work, no matter which wallet you provide, it will return the same set of NFTs of the same random address. (tbh i have no idea how it is chosen in the first place)

lazy.lazy is undefined, and the last call error come back as not a function

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.