Skip to content

Instantly share code, notes, and snippets.

@erran
Last active November 23, 2021 20:29
Show Gist options
  • Save erran/94c1ad77c903894cf9091a8bd9aada86 to your computer and use it in GitHub Desktop.
Save erran/94c1ad77c903894cf9091a8bd9aada86 to your computer and use it in GitHub Desktop.
Collect all NFTs for a given owner wallet based on the specified update authority.

Clone this gist locally as a github repository and run the following commands:

# install dependencies:
yarn install

# run script with OWNER_ADDRESS set to trashy pandas burn wallet and UPDATE_AUTHORITIES set to all three Trashy Pandas collection update authorities.
env OWNER_ADDRESS=2tszvJtNLoreu8R16ERwQFWu4gywqwfLW9rpgMF7tEJj UPDATE_AUTHORITIES=BsK12QfLSAUH6UBJs7baDEy5vvfTgyHuaR4VX43YDdyZ,CjFEeNaHhdtKpWbk9LwUJtn5aMGut34q4VP4Ky5ovF51,gfEZGsSFMnwazJFeQ2gQKeQy7ip2hsR6SmEKsoxZFV3 node index.js
const BN = require('bn.js');
const bs58 = require('bs58');
const { Account, Connection, programs, utils } = require('@metaplex/js');
if (!process.env.OWNER_ADDRESS || process.env.OWNER_ADDRESS.length < 44) {
throw new Error('Please provide a valid Solana wallet public address for the owner via $OWNER_ADDRESS.');
}
if (!process.env.UPDATE_AUTHORITIES || process.env.UPDATE_AUTHORITIES.length < 44) {
throw new Error('Please provide a valid comma-separated list of Solana wallet public address for the update authority via $UPDATE_AUTHORITIES.');
}
const updateAuthorities = process.env.UPDATE_AUTHORITIES.split(/,\s*?/)
const tokensForWallet = async () => {
const connection = new Connection('mainnet-beta');
const accounts = await programs.TokenAccount.getTokenAccountsByOwner(connection, process.env.OWNER_ADDRESS);
const accountsWithAmount = accounts
.map(({ data }) => data)
// Only include tokens where amount equal to 1.
// Note: This is not the same as mint supply.
// NFTs by definition have supply of 1, but an account balance > 1 implies a mint supply > 1.
.filter(({ amount }) => amount?.eq(new BN(1)));
const metadataAddresses = await Promise.all(
accountsWithAmount.map(async ({ mint }) => await programs.metadata.Metadata.getPDA(mint)),
);
const tokenInfo = await Account.getInfos(connection, metadataAddresses);
return Array.from(tokenInfo.values())
.map((m) => programs.metadata.MetadataData.deserialize(m.data))
.filter((m) => updateAuthorities.includes(m.updateAuthority));
}
tokensForWallet(process.env.OWNER_ADDRESS)
.then((tokens) => JSON.stringify(tokens))
.then((json) => console.log(json))
.catch((error) => console.error({ error: JSON.stringify(error) }))
{
"name": "web3",
"version": "0.0.1",
"description": "Print all tokens by owner address",
"main": "index.js",
"author": "Erran Carey <me@errancarey.com>",
"license": "MIT",
"dependencies": {
"@metaplex/js": "^4.3.0",
"bn.js": "^5.2.0",
"bs58": "^4.0.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment