Skip to content

Instantly share code, notes, and snippets.

@tevdoradze
Forked from dvcrn/index.ts
Created September 11, 2021 20:37
Show Gist options
  • Save tevdoradze/297fec2bd9bb7d84b5a9a93b1d179650 to your computer and use it in GitHub Desktop.
Save tevdoradze/297fec2bd9bb7d84b5a9a93b1d179650 to your computer and use it in GitHub Desktop.
get metadata from metaplex
import * as web3 from "@solana/web3.js";
import * as metadata from "./actions/metadata";
import { PublicKey } from "@solana/web3.js";
const TOKEN_METADATA_PROGRAM_ID = new web3.PublicKey(
"metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s"
);
const tokenAddress = new web3.PublicKey(
"CxkKDaBvtHqg8aHBVY8E4YYBsCfJkJVsTAEdTo5k4SEw"
);
async function getMetadataAccount(
tokenMint: web3.PublicKey
): Promise<web3.PublicKey> {
const [addr, nr] = await PublicKey.findProgramAddress(
[
Buffer.from("metadata"),
TOKEN_METADATA_PROGRAM_ID.toBuffer(),
tokenMint.toBuffer(),
],
TOKEN_METADATA_PROGRAM_ID
);
return addr;
}
(async () => {
// Connect to cluster
var connection = new web3.Connection(
web3.clusterApiUrl("mainnet-beta"),
"confirmed"
);
const m = await getMetadataAccount(tokenAddress);
console.log("metadata acc: ", m);
const accInfo = await connection.getAccountInfo(m);
console.log(accInfo);
// finally, decode metadata
// get the decodeMetadata function from metaplex - https://github.com/metaplex-foundation/metaplex/blob/master/js/packages/common/src/actions/metadata.ts#L438
console.log(metadata.decodeMetadata(accInfo!.data));
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment