-
-
Save tevdoradze/297fec2bd9bb7d84b5a9a93b1d179650 to your computer and use it in GitHub Desktop.
get metadata from metaplex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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