Last active
June 26, 2022 19:41
-
-
Save dvcrn/a1b0ff0a0b4b3ab02aff44bc84ac4522 to your computer and use it in GitHub Desktop.
Get Metaplex Metadata
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 metaplex from "@metaplex/js"; | |
import * as web3 from "@solana/web3.js"; | |
const connection = new web3.Connection( | |
web3.clusterApiUrl("mainnet-beta"), | |
"confirmed" | |
); | |
const tokenAddress = "CxkKDaBvtHqg8aHBVY8E4YYBsCfJkJVsTAEdTo5k4SEw"; | |
(async () => { | |
const metadataPDA = await metaplex.programs.metadata.Metadata.getPDA( | |
tokenAddress | |
); | |
const mintAccInfo = await connection.getAccountInfo(metadataPDA); // fetch account info | |
const metadata = metaplex.programs.metadata.Metadata.from( | |
new metaplex.Account(tokenAddress, mintAccInfo) | |
); | |
console.log(metadata); | |
})(); |
Thanks for adding this @AWolf81!
looks like Metadata doesn't have getPDA or from methods anymore after latest updates
Is there still a way to get metadata without the getPDA method?
export const METADATA_PROGRAM_ID = 'metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s';
const getPDA = async (
mint: anchor.web3.PublicKey,
): Promise<anchor.web3.PublicKey> => {
return (
await anchor.web3.PublicKey.findProgramAddress(
[
Buffer.from('metadata'),
TOKEN_METADATA_PROGRAM_ID.toBuffer(),
mint.toBuffer(),
],
TOKEN_METADATA_PROGRAM_ID,
)
)[0];
};
Hopefully, this is helpful for you.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There is a change in the latest Metaplex version (see metaplex/js issue #129).
The code above is working for version 4.1.0.
For v4.10.1 the below code is working (but could be simplified with
Metadata.load
method):Codesandbox React example