From f3e528cbef09e95971c54527559538fbace8f224 Mon Sep 17 00:00:00 2001 From: Juan Aguilar Santillana Date: Thu, 22 Aug 2024 17:29:24 +0200 Subject: [PATCH] add short createAssociatedTokenAccountIdempotentInstruction --- .../instructions/associatedTokenAccount.ts | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/token/js/src/instructions/associatedTokenAccount.ts b/token/js/src/instructions/associatedTokenAccount.ts index 306425ddf32..94db079e555 100644 --- a/token/js/src/instructions/associatedTokenAccount.ts +++ b/token/js/src/instructions/associatedTokenAccount.ts @@ -1,6 +1,7 @@ import type { PublicKey } from '@solana/web3.js'; import { SystemProgram, TransactionInstruction } from '@solana/web3.js'; import { ASSOCIATED_TOKEN_PROGRAM_ID, TOKEN_PROGRAM_ID } from '../constants.js'; +import { getAssociatedTokenAddressSync } from '../state/mint.js'; /** * Construct a CreateAssociatedTokenAccount instruction @@ -64,6 +65,31 @@ export function createAssociatedTokenAccountIdempotentInstruction( ); } +/** + * Construct a createAssociatedTokenAccountIdempotentInstructionShort instruction + * + * @param payer Payer of the initialization fees + * @param owner Owner of the new account + * @param mint Token mint account + * @param allowOwnerOffCurve Allow the owner account to be a PDA (Program Derived Address) + * @param programId SPL Token program account + * @param associatedTokenProgramId SPL Associated Token program account + * + * @return Instruction to add to a transaction + */ +export function createAssociatedTokenAccountIdempotentInstructionShort( + payer: PublicKey, + owner: PublicKey, + mint: PublicKey, + allowOwnerOffCurve = true, + programId = TOKEN_PROGRAM_ID, + associatedTokenProgramId = ASSOCIATED_TOKEN_PROGRAM_ID, +) { + const associatedToken = getAssociatedTokenAddressSync(mint, owner, allowOwnerOffCurve); + + return createAssociatedTokenAccountIdempotentInstruction(payer, owner, mint, programId, associatedTokenProgramId); +} + function buildAssociatedTokenAccountInstruction( payer: PublicKey, associatedToken: PublicKey,