UNPKG

solana-token-extension-boost

Version:

SDK for Solana Token Extensions with wallet adapter support

104 lines (103 loc) 4.34 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TransferHookToken = void 0; const web3_js_1 = require("@solana/web3.js"); const spl_token_1 = require("@solana/spl-token"); const token_1 = require("../../core/token"); /** * TransferHookToken - Extension for Token with transfer hook functionality * * This extension allows executing custom logic on token transfers through a separate program */ class TransferHookToken extends token_1.Token { constructor(connection, mint, programId) { super(connection, mint); this.programId = programId; } /** * Create transfer instruction with transfer hook * * @param source - Source account address * @param destination - Destination account address * @param owner - Account owner * @param amount - Token amount to transfer * @param decimals - Token decimal places * @param extraAccounts - Additional accounts to include in the instruction (optional) * @returns Transaction instruction */ createTransferInstruction(source, destination, owner, amount, decimals, extraAccounts = []) { // Note: Since createTransferCheckedWithTransferHookInstruction might not be // directly available in this version of @solana/spl-token, // we use the standard transfer instruction instead return (0, spl_token_1.createTransferCheckedInstruction)(source, this.mint, destination, owner, amount, decimals, [], spl_token_1.TOKEN_2022_PROGRAM_ID); } /** * Generate instructions to create a new TransferHookToken * * @param connection - Connection to Solana cluster * @param payer - Public key of the transaction fee payer * @param params - Initialization parameters * @returns Instructions, signers, and mint address */ static async createInstructions(connection, payer, params) { try { const mintKeypair = web3_js_1.Keypair.generate(); const mint = mintKeypair.publicKey; const mintLen = (0, spl_token_1.getMintLen)([spl_token_1.ExtensionType.TransferHook]); const lamports = await connection.getMinimumBalanceForRentExemption(mintLen); const instructions = [ web3_js_1.SystemProgram.createAccount({ fromPubkey: payer, newAccountPubkey: mint, space: mintLen, lamports, programId: spl_token_1.TOKEN_2022_PROGRAM_ID, }), (0, spl_token_1.createInitializeTransferHookInstruction)(mint, payer, params.programId, spl_token_1.TOKEN_2022_PROGRAM_ID), (0, spl_token_1.createInitializeMintInstruction)(mint, params.decimals, params.mintAuthority, null, spl_token_1.TOKEN_2022_PROGRAM_ID), ]; return { instructions, signers: [mintKeypair], mint }; } catch (error) { throw new Error(`Could not create TransferHookToken instructions: ${error.message}`); } } /** * Get the program ID that will be called on transfers * * @returns Transfer hook program ID */ getTransferHookProgramId() { return this.programId; } /** * Check if an account has the TransferHook extension enabled * * @param tokenAccount - Token account to check * @returns Promise resolving to boolean indicating if the extension is enabled */ async hasTransferHookExtension(tokenAccount) { try { const account = await (0, spl_token_1.getAccount)(this.connection, tokenAccount, "confirmed", spl_token_1.TOKEN_2022_PROGRAM_ID); // Check if the account has the transfer hook extension // This is a simplistic check - in a real implementation, // we would use the proper @solana/spl-token method to check try { // In @solana/spl-token, there's a method to check this // For now we'll just return true if we can get the account return true; } catch { return false; } } catch { return false; } } } exports.TransferHookToken = TransferHookToken;