Skip to content

Instantly share code, notes, and snippets.

@billyjacoby
Created October 17, 2021 18:46
Show Gist options
  • Save billyjacoby/41aad19b4d37cb0553ce0893eafdf67c to your computer and use it in GitHub Desktop.
Save billyjacoby/41aad19b4d37cb0553ce0893eafdf67c to your computer and use it in GitHub Desktop.
import * as web3 from "@solana/web3.js";
import { Metadata } from "@metaplex/js";
import { deserialize } from "@metaplex/js";
const programId = "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA";
const programIdKey = new web3.PublicKey(programId);
const walletAddress = "AmtW4L39QwE2DGU3f3uEoL1rVCgwwit5W4UhjxiM35Nt";
const network = "mainnet-beta";
const walletPubKey = new web3.PublicKey(walletAddress);
export default async function handler(req, res) {
const connection = new web3.Connection(
web3.clusterApiUrl(network),
"confirmed"
);
const tokens = await connection.getTokenAccountsByOwner(walletPubKey, {
programId: programIdKey,
});
console.log(tokens.value[0].account);
/**
{
data: <Buffer 37 99 8c cb f2 d0 45 8b 61 5c bc c6 b1 a3 67 c4 74 9e 9f ef 73 06 62 2e 1b 1b 58 91 01 20 bc 9a 91 38 f8 64 fa 44 50 a6 1d b9 37 57 78 5d 84 f2 ec 6a ... 115 more bytes>,
executable: false,
lamports: 2039280,
owner: PublicKey {
_bn: <BN: 6ddf6e1d765a193d9cbe146ceeb79ac1cb485ed5f5b37913a8cf5857eff00a9>
},
rentEpoch: 235
}
*/
const metadata = await Metadata.load(connection, tokens.value[0].account);
//* Errors out with the following error message:
/*
error - Error: Assertion failed
at assert (/Users/billyjacoby/Programming-Local/SOL/solana-next/node_modules/bn.js/lib/bn.js:6:21)
at BN._initArray (/Users/billyjacoby/Programming-Local/SOL/solana-next/node_modules/bn.js/lib/bn.js:145:5)
at BN.init [as _init] (/Users/billyjacoby/Programming-Local/SOL/solana-next/node_modules/bn.js/lib/bn.js:86:19)
at new BN (/Users/billyjacoby/Programming-Local/SOL/solana-next/node_modules/bn.js/lib/bn.js:39:12)
at new PublicKey (/Users/billyjacoby/Programming-Local/SOL/solana-next/node_modules/@solana/web3.js/lib/index.cjs.js:144:20)
at Function.<anonymous> (/Users/billyjacoby/Programming-Local/SOL/solana-next/node_modules/@metaplex/js/lib/index.cjs.js:216:58)
at Generator.next (<anonymous>)
at /Users/billyjacoby/Programming-Local/SOL/solana-next/node_modules/@metaplex/js/lib/index.cjs.js:104:71
at new Promise (<anonymous>)
at __awaiter (/Users/billyjacoby/Programming-Local/SOL/solana-next/node_modules/@metaplex/js/lib/index.cjs.js:100:12) {
page: '/api/getTokens'
}
*/
console.log(metadata);
res.status(200).json({ tokens });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment