How to read metadata from NFTcontract using TS-SDK

Hi @danielbate I am currently trying to read metadata From NFT contract , But its returning something else.

Typescript code :

const { Provider, Wallet, Contract, getRandomB256 } = require('fuels');
const { abi } = require('./abi');

async function main() {
  let contractId = '0xdd723a38d1fe832f231fc6ffc69b5dc51850b5fc7f4b5b03f9fa11821877c5ca';
  // Create a provider, with the Latest Testnet URL.
  const provider = await Provider.create('https://testnet.fuel.network/v1/graphql');

  // Initialize wallet with a private key
  const privateKey = ''; // Replace with your private key
  const wallet = Wallet.fromPrivateKey(privateKey, provider);
  const contract = new Contract(contractId, abi, wallet);
  
  // Fetch total assets
  const { value } = await contract.functions.total_assets().get();
  console.log("Value of Total Assets:", Number(value));

  // // Define assetId and metadataKey
  const assetId = { bits: '0x2f18e07d3177f6fa66b9999c2174d5d027e659f3f44bd5b9d449c43df12d7559' }; // Replace with actual asset ID
  const metadataKey = 'https://arweave.net/LxILnA3lG-oRW5eZR4XAB-RnzpcFRjhSwh_PfQmw1Jw';

  // Create the Metadata::String variant
  const metadataEnum = { String: metadataKey };


  const metadata = await contract.functions.metadata(assetId, metadataKey).get();
  let metaKey = metadata.callResult
  console.log("Metadata:",(metaKey.receipts[1]));
}

main().catch(console.error);

Response I am getting :

Fuelup show versions

contract ID : 0xdd723a38d1fe832f231fc6ffc69b5dc51850b5fc7f4b5b03f9fa11821877c5ca
Asset ID : 0x2f18e07d3177f6fa66b9999c2174d5d027e659f3f44bd5b9d449c43df12d7559

Contract code for getting metadata

impl SRC7 for Contract {
    /// Returns metadata for the corresponding `asset` and `key`.
    ///
    /// # Arguments
    ///
    /// * `asset`: [AssetId] - The asset of which to query the metadata.
    /// * `key`: [String] - The key to the specific metadata.
    ///
    /// # Returns
    ///
    /// * [Option<Metadata>] - `Some` metadata that corresponds to the `key` or `None`.
    ///
    /// # Number of Storage Accesses
    ///
    /// * Reads: `1`
    ///
    /// # Examples
    ///
    /// ```sway
    /// use src_7::{SRC7, Metadata};
    /// use std::string::String;
    ///
    /// fn foo(contract_id: ContractId, asset: AssetId) {
    ///     let contract_abi = abi(SRC7, contract_id);
    ///     let key = String::from_ascii_str("image");
    ///     let data = contract_abi.metadata(asset, key);
    ///     assert(data.is_some());
    /// }
    /// ```
    #[storage(read)]
    fn metadata(asset: AssetId, key: String) -> Option<Metadata> {
        storage.metadata.get(asset, key)
    }
}

set Metadata code :

const { Provider, Wallet, Contract, getRandomB256 } = require('fuels');
const { abi } = require('./abi');

async function main() {
  let contractId = '0xdd723a38d1fe832f231fc6ffc69b5dc51850b5fc7f4b5b03f9fa11821877c5ca';
  // Create a provider, with the Latest Testnet URL.
  const provider = await Provider.create('https://testnet.fuel.network/v1/graphql');

  // Initialize wallet with a private key
  const privateKey = ''; // Replace with your private key
  const wallet = Wallet.fromPrivateKey(privateKey, provider);
  const contract = new Contract(contractId, abi, wallet);
  
  // Fetch total assets
  const { value } = await contract.functions.total_assets().get();
  console.log("Value of Total Assets:", Number(value));

  // // Define assetId and metadataKey
  const assetId = { bits: '0x2f18e07d3177f6fa66b9999c2174d5d027e659f3f44bd5b9d449c43df12d7559' }; // Replace with actual asset ID
  const metadataKey = 'https://arweave.net/LxILnA3lG-oRW5eZR4XAB-RnzpcFRjhSwh_PfQmw1Jw';

  // Create the Metadata::String variant
  const metadataEnum = { String: metadataKey };

  // Call set_metadata function with the correct parameters
  const metadata = await contract.functions.set_metadata(assetId, "This is NO NFT", metadataEnum).txParams({ gasPrice: 1 }).call();
  let txn = await metadata.waitForResult()
  console.log("Metadata:",txn.transactionResponse);

}

main().catch(console.error);

Do we need anyother details to Provide over here.

The value property on your response object of undefined would indicate there is no metadata stored for metadataKey.

As the function doc states:

undefined in our case represents None. Have you definitely set metadata for that key?

1 Like

Hi @danielbate

yes , I have set metadata for that key transaction executed properly

help me to get what i am missing

I find out the issue and resolved, Thanks :slight_smile:

1 Like

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.