Getting metadata entries attached to a mosaic

Get the list of metadata entries attached to an mosaic.

Prerequisites

Method #01: Using the SDK

 // replace with mosaic id
const mosaicIdHex = '0DC67FBE1CAD29E3';
const mosaicId = new MosaicId(mosaicIdHex);
// replace with node endpoint
const nodeUrl = 'http://ngl-dual-101.testnet.symboldev.network:3000';
const repositoryFactory = new RepositoryFactoryHttp(nodeUrl);
const metadataHttp = repositoryFactory.createMetadataRepository();
const searchCriteria = {
  targetId: mosaicId,
  metadataType: MetadataType.Mosaic,
};
metadataHttp.search(searchCriteria).subscribe(
  (metadataEntries: Page<Metadata>) => {
    if (metadataEntries.pageSize > 0) {
      console.log('Page', metadataEntries.pageNumber);
      metadataEntries.data.map((entry: Metadata) => {
        const metadataEntry = entry.metadataEntry;
        console.log('n n Key:t', metadataEntry.scopedMetadataKey);
        console.log('n ---');
        console.log('n Value:t', metadataEntry.value);
        console.log(
          'n Sender Address:t',
          metadataEntry.sourceAddress.pretty(),
        );
        console.log(
          'n Target address:t',
          metadataEntry.targetAddress.pretty(),
        );
        console.log(
          'n Scoped metadata key:t',
          metadataEntry.scopedMetadataKey.toHex(),
        );
        console.log('n TargetId:t', metadataEntry.targetId);
      });
    } else {
      console.log('n The mosaic does not have metadata entries assigned.');
    }
  },
  (err) => console.log(err),
);
 // replace with mosaic id
const mosaicIdHex = '0DC67FBE1CAD29E3';
const mosaicId = new symbol_sdk_1.MosaicId(mosaicIdHex);
// replace with node endpoint
const nodeUrl = 'http://ngl-dual-101.testnet.symboldev.network:3000';
const repositoryFactory = new symbol_sdk_1.RepositoryFactoryHttp(nodeUrl);
const metadataHttp = repositoryFactory.createMetadataRepository();
const searchCriteria = {
    targetId: mosaicId,
    metadataType: symbol_sdk_1.MetadataType.Mosaic,
};
metadataHttp.search(searchCriteria).subscribe((metadataEntries) => {
    if (metadataEntries.pageSize > 0) {
        console.log('Page', metadataEntries.pageNumber);
        metadataEntries.data.map((entry) => {
            const metadataEntry = entry.metadataEntry;
            console.log('n n Key:t', metadataEntry.scopedMetadataKey);
            console.log('n ---');
            console.log('n Value:t', metadataEntry.value);
            console.log('n Sender Address:t', metadataEntry.sourceAddress.pretty());
            console.log('n Target address:t', metadataEntry.targetAddress.pretty());
            console.log('n Scoped metadata key:t', metadataEntry.scopedMetadataKey.toHex());
            console.log('n TargetId:t', metadataEntry.targetId);
        });
    }
    else {
        console.log('n The mosaic does not have metadata entries assigned.');
    }
}, (err) => console.log(err));
         // replace with node endpoint
        try (final RepositoryFactory repositoryFactory = new RepositoryFactoryVertxImpl(
            "http://ngl-dual-101.testnet.symboldev.network:3000")) {
            final MetadataRepository metadataRepository = repositoryFactory
                .createMetadataRepository();
            // replace with mosaic id
            final String mosaicIdHex = "0DC67FBE1CAD29E3";
            final MosaicId mosaicId = new MosaicId(mosaicIdHex);
            MetadataPaginationStreamer streamer = new MetadataPaginationStreamer(
                metadataRepository);
            MetadataSearchCriteria criteria = new MetadataSearchCriteria().targetId(mosaicId);
            final List<Metadata> metadata = streamer
                .search(criteria).toList().toFuture().get();
            final JsonHelper helper = new JsonHelperJackson2();
            System.out.println(helper.prettyPrint(metadata));
        }

Method #02: Using the CLI

 symbol-cli metadata mosaic --mosaic-id 71415AC19C818709

Last updated by Xavi Artigas on 2021-07-26.

Did you find what you were looking for? Give us your feedback.