Solana tokens are created using the SPL Token Program, so we'll track transactions involving this program to identify new tokens. Add the following function to your index.js
file:
async function getNewTokens(startSlot) {
try {
const currentSlot = await connection.getSlot();
const signatures = await connection.getConfirmedSignaturesForAddress2(
new solanaWeb3.PublicKey(solanaWeb3.TOKEN_PROGRAM_ID),
{ startSlot, limit: 100 }
);
console.log('Newly Created Tokens:');
for (const signatureInfo of signatures) {
const transaction = await connection.getConfirmedTransaction(signatureInfo.signature);
const transactionInstructions = transaction.transaction.message.instructions;
transactionInstructions.forEach((instruction) => {
if (instruction.programId.equals(solanaWeb3.TOKEN_PROGRAM_ID)) {
const data = Buffer.from(instruction.data);
const command = data.readUInt8(0);
// Command 1 represents the Token Mint To instruction
if (command === 1) {
const mintAccount = instruction.keys[0].pubkey.toBase58();
console.log(`- New Token Mint: ${mintAccount}`);
}
}
});
}
} catch (error) {
console.error('Error fetching new tokens:', error);
}
}
// Replace with the starting slot number
const startSlot = 1000000;
getNewTokens(startSlot);