Skip to content

Instantly share code, notes, and snippets.

@rubpy
Created October 22, 2024 21:56
Show Gist options
  • Save rubpy/ed85e754e38cd827dc38e7f3510bf706 to your computer and use it in GitHub Desktop.
Save rubpy/ed85e754e38cd827dc38e7f3510bf706 to your computer and use it in GitHub Desktop.
import web3 from "@solana/web3.js";
import bs58 from "bs58";
//////////////////////////////////////////////////
const utf8TextDecoder = new TextDecoder("utf-8");
function decodeVariableUtf8String(buffer: Buffer, start: number, maxLength: number): string {
const end = start + maxLength;
let pos = start;
for (; pos < end && buffer[pos] !== 0; ++pos);
return utf8TextDecoder.decode(
new Uint8Array(buffer.buffer, buffer.byteOffset + start, pos - start)
);
}
const METAPLEX_PROGRAM_ID = new web3.PublicKey("metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s");
(async (rpcUrl: string) => {
const conn = new web3.Connection(rpcUrl, "processed");
const TARGET_UPDATE_AUTHORITY = new web3.PublicKey("TSLvdd1pWpHVjahSpsvCXUbgwsL3JAcvokwaKt1eokM");
const mints = new Set<bigint>();
conn.onProgramAccountChange(METAPLEX_PROGRAM_ID, (info: web3.KeyedAccountInfo) => {
if (!info.accountInfo.data || info.accountInfo.data.byteLength < 128) {
return;
}
const mintId =
info.accountInfo.data.readBigUint64LE(33)
| (info.accountInfo.data.readBigUint64LE(33 + 8) << 64n)
| (info.accountInfo.data.readBigUint64LE(33 + 16) << 128n)
| (info.accountInfo.data.readBigUint64LE(33 + 24) << 192n);
if (mints.has(mintId)) {
return;
}
mints.add(mintId);
const mint = info.accountInfo.data.subarray(33, 33 + 32);
const name = decodeVariableUtf8String(info.accountInfo.data, 69, 32);
const symbol = decodeVariableUtf8String(info.accountInfo.data, 105, 10);
console.log(JSON.stringify({
mint: bs58.encode(mint),
name,
symbol,
}));
}, undefined, [
{ memcmp: { offset: 0, bytes: bs58.encode([0x04, ...TARGET_UPDATE_AUTHORITY.toBuffer()]) } },
]);
})(process.env.SOL_RPC_URL || "https://mainnet.helius-rpc.com/?api-key=00000000-0000-0000-0000-000000000000");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment