Here’s a step-by-step guide on how to fetch a list of tokens (not NFTs) created by a Solana user using the getParsedTokenAccountsByOwner
method:
Prerequisites
- You have a Solana account with the necessary permissions to access token metadata.
- You have installed the Solana CLI and verified your wallet address.
Method: getParsedTokenAccountsByOwner
The getParsedTokenAccountsByOwner
method allows you to retrieve a list of parsed token accounts for a given owner. This method is specific to Solana’s transaction parsing API and is used to fetch data that was previously unindexed by the Solana blockchain.
Here are the steps to fetch the tokens created by a user:
- Install the required library: You’ll need to install the
solana-token-metadata
package using npm or yarn:
npm install solana-token-metadata
- Create a script that connects to your Solana cluster and initializes the token metadata index:
const { connect } = require('@solana/web3.js');
const TokenMetadata = require('solana-token-metadata');
// Connect to the Solana network (devnet or testnet)
connect();
// Initialize the token metadata index
const app = new TokenMetadata();
app.init().then(() => {
console.log('Token metadata index initialized!');
}).catch((error) => {
console.error(error);
});
- Get the owner’s ID
: Get the Solana account ID of the user you want to fetch tokens for. You can use the
getAccount
method to retrieve an account object:
const ownerAccount = await getAccount();
console.log('Owner account:', ownerAccount);
- Call
getParsedTokenAccountsByOwner
: Use thegetParsedTokenAccountsByOwner
function to fetch a list of parsed token accounts for the given owner:
const parsedTokens = await app.getParsedTokenAccountsByOwner(ownerAccount.publicKey.toString());
console.log('Parsed tokens:');
parsedTokens.forEach((token) => {
console.log(- ${token.tokenId}
);
});
Example Code
Here’s an example code snippet that demonstrates how to fetch the tokens created by a user using the getParsedTokenAccountsByOwner
method:
const { connect, getAccount } = require('@solana/web3.js');
const TokenMetadata = require('solana-token-metadata');
// Connect to the Solana network (devnet or testnet)
connect();
// Initialize the token metadata index
app.init().then(() => {
console.log('Token metadata index initialized!');
}).catch((error) => {
console.error(error);
});
async function fetchTokens() {
const ownerAccount = await getAccount();
const parsedTokens = await app.getParsedTokenAccountsByOwner(ownerAccount.publicKey.toString());
console.log(Parsed tokens: ${parsedTokens.length}
);
// Process the parsed token data
parsedTokens.forEach((token) => {
console.log(- ${token.tokenId}
);
});
}
fetchTokens();
This code connects to the Solana network, initializes the token metadata index, and calls getParsedTokenAccountsByOwner
to fetch a list of tokens created by the given owner. The fetched tokens are then processed and logged to the console.
Note that this example uses the getAccount
method to retrieve an account object and the getParsedTokenAccountsByOwner
function to fetch parsed token accounts. You’ll need to modify these functions according to your specific use case.