Reputation: 351
I get a list of tokens own by a publicKey thanks to that method :
connection
.getParsedTokenAccountsByOwner(
new PublicKey(publicKey.toBase58()),
{
programId: TOKEN_PROGRAM_ID
}
)
.then((b) => {
const owner = b?.value?.[0].account.owner;
const pb = b?.value?.[0].pubkey;
const nonZeroAccounts = b?.value?.filter(
(obj) => obj.account.data.parsed.info.tokenAmount.uiAmount > 0
);
setTokens(JSON.stringify(nonZeroAccounts, null, 2));
return value for an account that own 1 token for example :
{
"account": {
"data": {
"parsed": {
"info": {
"isNative": false,
"mint": "example",
"owner": "example",
"state": "initialized",
"tokenAmount": {
"amount": "1",
"decimals": 0,
"uiAmount": 1,
"uiAmountString": "1"
}
},
"type": "account"
},
"program": "spl-token",
"space": 165
},
"executable": false,
"lamports": 2039280,
"owner": {
"_bn": "example"
},
"rentEpoch": 245
},
"pubkey": {
"_bn": "example"
}
Now that I have these informations, I would like to know how to get the metadata of that token
Upvotes: 8
Views: 12373
Reputation: 351
After Soubriquet request, I ll add here the actual working code.
Metaplex doc isnt always up to date, so don't trust it.
const Metadata = require("@metaplex-foundation/mpl-token-metadata");
async function getMetadataPDA(mint)
{
const [publicKey] = await web3.PublicKey.findProgramAddress(
[Buffer.from("metadata"),
Metadata.PROGRAM_ID.toBuffer(),
mint.toBuffer()],
Metadata.PROGRAM_ID
);
return publicKey;
}
let pda = await getMetadataPDA(mint);
let res = await Metadata.Metadata.fromAccountAddress(connection, account);
Consider that mint is a PublicKey, not a string.
Here is JSON structure of the returned values https://docs.metaplex.com/token-metadata/Versions/v1.0.0/nft-standard
Upvotes: 8
Reputation: 21087
Migrating OP's solution from the question to an answer:
As of 12.18.2021, please now use
import * as metadata from "@metaplex-foundation/mpl-token-metadata"; const nftsmetadata:metadata.MetadataData[] = await metadata.Metadata.findDataByOwner(connection, publicKey);
Upvotes: -1
Reputation: 745
Unsure if this fully addresses your issue, but one way that I've fetched metadata for all tokens in a wallet is by using the metaplex library:
import { programs } from "@metaplex/js"
const tokenMetadata = programs.metadata.Metadata.findByOwnerV2(connection, walletPublicKey);
console.log(JSON.stringify(tokenMetadata));
Printing the tokenMetadata for this test wallet looks like the following:
[
{
"pubkey": "HPswFDfXcmpvj6RSvJoiuTe6r1wGuwJe9HgzkfhpHKUc",
"info": {
"executable": false,
"owner": {
"_bn": "0b7065b1e3d17c45389d527f6b04c3cd58b86c731aa0fdb549b6d1bc03f82946"
},
"lamports": 5616720,
"data": {
"type": "Buffer",
"data": [
4, 60, 149, 84, 75, 223, 131, 43, 238, 37, 212, 193, 171, 139, 29,
162, 158, 45, 52, 157, 244, 236, 53, 189, 60, 118, 83, 187, 176, 3,
87, 134, 164, 70, 147, 118, 78, 64, 38, 124, 25, 241, 242, 54, 51, 83,
80, 221, 253, 4, 95, 165, 233, 112, 107, 151, 166, 96, 148, 162, 114,
132, 111, 55, 68, 32, 0, 0, 0, 89, 111, 117, 114, 32, 67, 111, 108,
108, 101, 99, 116, 105, 111, 110, 32, 35, 52, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 200, 0,
0, 0, 104, 116, 116, 112, 115, 58, 47, 47, 97, 114, 119, 101, 97, 118,
101, 46, 110, 101, 116, 47, 121, 57, 105, 54, 104, 50, 97, 107, 65,
100, 80, 105, 90, 112, 98, 65, 45, 114, 65, 110, 57, 87, 97, 101, 68,
110, 83, 76, 85, 73, 78, 84, 117, 106, 109, 69, 113, 101, 118, 72, 66,
117, 48, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 2, 0, 0, 0, 148, 111, 225, 158, 161, 28, 45, 38, 87,
197, 46, 209, 25, 126, 164, 251, 66, 44, 245, 255, 202, 46, 123, 104,
19, 136, 141, 79, 66, 188, 138, 134, 1, 0, 206, 203, 108, 109, 123,
133, 164, 214, 142, 166, 98, 20, 48, 35, 184, 204, 119, 234, 128, 56,
190, 152, 177, 103, 83, 12, 148, 233, 189, 70, 167, 39, 0, 100, 1, 1,
1, 254, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0
]
}
},
"data": {
"key": 4,
"updateAuthority": "55VXi3ANYmig6eP8oewpz5KFzzjyT87RiX4mw2bfjchy",
"mint": "5kVxPxTbu9k4Yu6zUtBgJ4CYKo7whX3PyQDd12zpdjz3",
"data": {
"name": "Your Collection #4",
"symbol": "",
"uri": "https://arweave.net/y9i6h2akAdPiZpbA-rAn9WaeDnSLUINTujmEqevHBu0",
"sellerFeeBasisPoints": 0,
"creators": [
{
"address": "AzSHgm4CEQr3akEvh7AppmZw3oDYWunu18FoM61XRwA5",
"verified": 1,
"share": 0
},
{
"address": "EvEttHAwkVks2s5zEZjoPNVDBkhumTyvLTvaD5JQ3XCJ",
"verified": 0,
"share": 100
}
]
},
"primarySaleHappened": 1,
"isMutable": 1
}
}
]
Upvotes: 4