UNPKG

solana-token-extension-boost

Version:

SDK for Solana Token Extensions with wallet adapter support

132 lines (131 loc) 6.46 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TokenAccount = void 0; const web3_js_1 = require("@solana/web3.js"); const spl_token_1 = require("@solana/spl-token"); class TokenAccount { constructor(connection, mint, owner) { this.connection = connection; this.mint = mint; this.owner = owner; } /** * Create a standard token account * * @param payer - Transaction fee payer * @returns Token account information */ async createAccount(payer) { const tokenAccountKeypair = web3_js_1.Keypair.generate(); const accountLen = (0, spl_token_1.getAccountLen)([]); const lamports = await this.connection.getMinimumBalanceForRentExemption(accountLen); const transaction = new web3_js_1.Transaction(); transaction.add(web3_js_1.SystemProgram.createAccount({ fromPubkey: payer.publicKey, newAccountPubkey: tokenAccountKeypair.publicKey, space: accountLen, lamports, programId: spl_token_1.TOKEN_2022_PROGRAM_ID, }), (0, spl_token_1.createInitializeAccountInstruction)(tokenAccountKeypair.publicKey, this.mint, this.owner, spl_token_1.TOKEN_2022_PROGRAM_ID)); const signature = await (0, web3_js_1.sendAndConfirmTransaction)(this.connection, transaction, [payer, tokenAccountKeypair], { commitment: 'confirmed' }); return { tokenAccount: tokenAccountKeypair.publicKey, tokenAccountKeypair, signature }; } /** * Create a token account with immutable owner extension * * @param payer - Transaction fee payer * @returns Token account information */ async createAccountWithImmutableOwner(payer) { const tokenAccountKeypair = web3_js_1.Keypair.generate(); const accountLen = (0, spl_token_1.getAccountLen)([spl_token_1.ExtensionType.ImmutableOwner]); const lamports = await this.connection.getMinimumBalanceForRentExemption(accountLen); const transaction = new web3_js_1.Transaction(); // Step 1: Create the account transaction.add(web3_js_1.SystemProgram.createAccount({ fromPubkey: payer.publicKey, newAccountPubkey: tokenAccountKeypair.publicKey, space: accountLen, lamports, programId: spl_token_1.TOKEN_2022_PROGRAM_ID, })); // Step 2: Initialize the immutable owner extension transaction.add((0, spl_token_1.createInitializeImmutableOwnerInstruction)(tokenAccountKeypair.publicKey, spl_token_1.TOKEN_2022_PROGRAM_ID)); // Step 3: Initialize the account transaction.add((0, spl_token_1.createInitializeAccountInstruction)(tokenAccountKeypair.publicKey, this.mint, this.owner, spl_token_1.TOKEN_2022_PROGRAM_ID)); // Send transaction const signature = await (0, web3_js_1.sendAndConfirmTransaction)(this.connection, transaction, [payer, tokenAccountKeypair], { commitment: 'confirmed' }); return { tokenAccount: tokenAccountKeypair.publicKey, tokenAccountKeypair, signature }; } /** * Create an Associated Token Account * Note: ATAs already have immutable owner built-in * * @param payer - Transaction fee payer * @returns Token account information */ async createAssociatedTokenAccount(payer) { // Get the Associated Token Account address const tokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(this.mint, this.owner, true, spl_token_1.TOKEN_2022_PROGRAM_ID, spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID); const transaction = new web3_js_1.Transaction(); transaction.add((0, spl_token_1.createAssociatedTokenAccountInstruction)(payer.publicKey, tokenAccount, this.owner, this.mint, spl_token_1.TOKEN_2022_PROGRAM_ID, spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID)); const signature = await (0, web3_js_1.sendAndConfirmTransaction)(this.connection, transaction, [payer], { commitment: 'confirmed' }); return { tokenAccount, signature }; } /** * Create a token account with immutable owner using the helper function * Alternative method using the SPL-token helper * * @param payer - Transaction fee payer * @returns Token account information */ async createAccountWithImmutableOwnerAlt(payer) { // Use the SPL-token helper function to create an account const tokenAccount = await (0, spl_token_1.createAccount)(this.connection, payer, this.mint, this.owner, undefined, { commitment: 'confirmed' }, spl_token_1.TOKEN_2022_PROGRAM_ID); return { tokenAccount, signature: "Used createAccount helper function" }; } /** * Create an account with default state * This allows setting a default state for the account (e.g., frozen) * * @param payer - Transaction fee payer * @param state - Default account state * @returns Token account information */ async createAccountWithDefaultState(payer, state) { const tokenAccountKeypair = web3_js_1.Keypair.generate(); const accountLen = (0, spl_token_1.getAccountLen)([spl_token_1.ExtensionType.DefaultAccountState]); const lamports = await this.connection.getMinimumBalanceForRentExemption(accountLen); const transaction = new web3_js_1.Transaction(); transaction.add(web3_js_1.SystemProgram.createAccount({ fromPubkey: payer.publicKey, newAccountPubkey: tokenAccountKeypair.publicKey, space: accountLen, lamports, programId: spl_token_1.TOKEN_2022_PROGRAM_ID, })); transaction.add((0, spl_token_1.createInitializeDefaultAccountStateInstruction)(tokenAccountKeypair.publicKey, state, spl_token_1.TOKEN_2022_PROGRAM_ID)); transaction.add((0, spl_token_1.createInitializeAccountInstruction)(tokenAccountKeypair.publicKey, this.mint, this.owner, spl_token_1.TOKEN_2022_PROGRAM_ID)); const signature = await (0, web3_js_1.sendAndConfirmTransaction)(this.connection, transaction, [payer, tokenAccountKeypair], { commitment: 'confirmed' }); return { tokenAccount: tokenAccountKeypair.publicKey, tokenAccountKeypair, signature }; } } exports.TokenAccount = TokenAccount;