import { PublicKey } from '@solana/web3.js'; export const TOKEN_METADA_PROGRAM_ID = new PublicKey('metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s'); export async function deriveMetadataAccount(mint: PublicKey) { const utf8Encode = new TextEncoder(); const prefix = utf8Encode.encode('metadata'); const [metadata] = await PublicKey.findProgramAddress( [prefix, new PublicKey(TOKEN_METADA_PROGRAM_ID).toBytes(), mint.toBytes()], new PublicKey(TOKEN_METADA_PROGRAM_ID), ); return metadata; } export function decodeTokenMeta(data) { const beetType = data[0]; const updateAuthority = new PublicKey(data.slice(1, 33)).toBase58(); const mint = new PublicKey(data.slice(33, 65)).toBase58(); const name = readString(data.slice(65)); const nameBytes = new TextEncoder().encode(name); const symbol = readString(data.slice(65 + 4 + nameBytes.length)); const symbolBytes = new TextEncoder().encode(symbol); const uri = readString(data.slice(65 + 4 + nameBytes.length + 4 + symbolBytes.length)); return { beetType, updateAuthority, mint, name: name.replace(/\0/g, ''), symbol: symbol.trim().replace(/\0/g, ''), uri: uri.trim().replace(/\0/g, ''), }; } export function encodeTokenMeta(name: string, symbol: string, uri: string) { const res = new Uint8Array(1 + 36 + 14 + 204 + 2 + 1 + 1 + 1 + 1 + 1); res.fill(0); const utf8Encode = new TextEncoder(); const nameBytes = utf8Encode.encode(name).slice(0, 32); const symbolBytes = utf8Encode.encode(symbol).slice(0, 10); const uriBytes = utf8Encode.encode(uri).slice(0, 200); res[0] = 33; res[1] = nameBytes.length; res[2] = 0; res[3] = 0; res[4] = 0; for (let i = 0; i < nameBytes.length; i++) { res[5 + i] = nameBytes[i]; } let current = 5 + nameBytes.length; res[current] = symbolBytes.length; res[current + 1] = 0; res[current + 2] = 0; res[current + 3] = 0; for (let i = 0; i < symbolBytes.length; i++) { res[current + 4 + i] = symbolBytes[i]; } current = current + 4 + symbolBytes.length; res[current] = uriBytes.length; res[current + 1] = 0; res[current + 2] = 0; res[current + 3] = 0; for (let i = 0; i < uriBytes.length; i++) { res[current + 4 + i] = uriBytes[i]; } current = current + 4 + uriBytes.length + 2 + 1 + 1 + 1 + 1 + 1; res[current - 2] = 1; return res.slice(0, current); } function readString(buff: Buffer) { const len = Number(buff.readUInt32LE()); const copiedBuf = Uint8Array.prototype.slice.call(buff, 4, 4 + len); const res = new TextDecoder().decode(copiedBuf); return res; }