Search code examples
tokenmetadataweb3jssolana

@solana/web3.js Is there an API for ERC721 Metadata?


Given a token mint address, I'm looking for a way to get access to the metadata of an ERC721 token. Is there an API in @solana/web3.js?


Solution

  • Solana stores token metadata at an address that is derived from the original token's address as per https://docs.solana.com/developing/programming-model/calling-between-programs#hash-based-generated-program-addresses

    The reference code is in rust, here is the implementation from @solana/web3.js.
    (source)

      static async findProgramAddress(
        seeds: Array<Buffer | Uint8Array>,
        programId: PublicKey,
      ): Promise<[PublicKey, number]> {
        let nonce = 255;
        let address;
        while (nonce != 0) {
          try {
            const seedsWithNonce = seeds.concat(Buffer.from([nonce]));
            address = await this.createProgramAddress(seedsWithNonce, programId);
          } catch (err) {
            if (err instanceof TypeError) {
              throw err;
            }
            nonce--;
            continue;
          }
          return [address, nonce];
        }
        throw new Error(`Unable to find a viable program address nonce`);
      }
    

    Note that the metadata is encoded in base64, using the borsh library, as per https://docs.metaplex.com/nft-standard#token-metadata-program.

    Here is a concise implementation to retrieve and parse metadata using only borsh and @solana/web3.js https://gist.github.com/dvcrn/c099c9b5a095ffe4ddb6481c22cde5f4

    Finally, MagicDen has an endpoint that returns the metadata: https://api-mainnet.magiceden.io/rpc/getNFTByMintAddress/DugthRKbQZRMcDQQfjnj2HeSjz2VARPC8H9abxdNK2SS