Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit ab9fa9a

Browse files
authored
token-js: add derivation of createAssociatedTokenAccountIdempotentInstruction (#7183)
* add derivation of createAssociatedTokenAccountIdempotentInstruction * fix minor issues * fix format * fix lint * remove TODO comment
1 parent ab83005 commit ab9fa9a

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

token/js/src/instructions/associatedTokenAccount.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { PublicKey } from '@solana/web3.js';
22
import { SystemProgram, TransactionInstruction } from '@solana/web3.js';
33
import { ASSOCIATED_TOKEN_PROGRAM_ID, TOKEN_PROGRAM_ID } from '../constants.js';
4+
import { getAssociatedTokenAddressSync } from '../state/mint.js';
45

56
/**
67
* Construct a CreateAssociatedTokenAccount instruction
@@ -64,6 +65,38 @@ export function createAssociatedTokenAccountIdempotentInstruction(
6465
);
6566
}
6667

68+
/**
69+
* Derive the associated token account and construct a CreateAssociatedTokenAccountIdempotent instruction
70+
*
71+
* @param payer Payer of the initialization fees
72+
* @param owner Owner of the new account
73+
* @param mint Token mint account
74+
* @param allowOwnerOffCurve Allow the owner account to be a PDA (Program Derived Address)
75+
* @param programId SPL Token program account
76+
* @param associatedTokenProgramId SPL Associated Token program account
77+
*
78+
* @return Instruction to add to a transaction
79+
*/
80+
export function createAssociatedTokenAccountIdempotentInstructionWithDerivation(
81+
payer: PublicKey,
82+
owner: PublicKey,
83+
mint: PublicKey,
84+
allowOwnerOffCurve = true,
85+
programId = TOKEN_PROGRAM_ID,
86+
associatedTokenProgramId = ASSOCIATED_TOKEN_PROGRAM_ID,
87+
) {
88+
const associatedToken = getAssociatedTokenAddressSync(mint, owner, allowOwnerOffCurve);
89+
90+
return createAssociatedTokenAccountIdempotentInstruction(
91+
payer,
92+
associatedToken,
93+
owner,
94+
mint,
95+
programId,
96+
associatedTokenProgramId,
97+
);
98+
}
99+
67100
function buildAssociatedTokenAccountInstruction(
68101
payer: PublicKey,
69102
associatedToken: PublicKey,

token/js/test/unit/index.test.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { expect, use } from 'chai';
33
import {
44
ASSOCIATED_TOKEN_PROGRAM_ID,
55
createAssociatedTokenAccountInstruction,
6+
createAssociatedTokenAccountIdempotentInstruction,
7+
createAssociatedTokenAccountIdempotentInstructionWithDerivation,
68
createReallocateInstruction,
79
createInitializeMintInstruction,
810
createInitializeMint2Instruction,
@@ -167,6 +169,37 @@ describe('spl-associated-token-account instructions', () => {
167169
expect(ix.programId).to.eql(ASSOCIATED_TOKEN_PROGRAM_ID);
168170
expect(ix.keys).to.have.length(6);
169171
});
172+
173+
it('create idempotent', () => {
174+
const ix = createAssociatedTokenAccountIdempotentInstruction(
175+
Keypair.generate().publicKey,
176+
Keypair.generate().publicKey,
177+
Keypair.generate().publicKey,
178+
Keypair.generate().publicKey,
179+
);
180+
expect(ix.programId).to.eql(ASSOCIATED_TOKEN_PROGRAM_ID);
181+
expect(ix.keys).to.have.length(6);
182+
});
183+
184+
it('create idempotent with derivation', () => {
185+
const ix = createAssociatedTokenAccountIdempotentInstructionWithDerivation(
186+
Keypair.generate().publicKey,
187+
Keypair.generate().publicKey,
188+
Keypair.generate().publicKey,
189+
);
190+
expect(ix.programId).to.eql(ASSOCIATED_TOKEN_PROGRAM_ID);
191+
expect(ix.keys).to.have.length(6);
192+
});
193+
194+
it('create idempotent with derivation same without', () => {
195+
const payer = Keypair.generate().publicKey;
196+
const owner = Keypair.generate().publicKey;
197+
const mint = Keypair.generate().publicKey;
198+
const associatedToken = getAssociatedTokenAddressSync(mint, owner, true);
199+
const ix = createAssociatedTokenAccountIdempotentInstruction(payer, associatedToken, owner, mint);
200+
const ixDerivation = createAssociatedTokenAccountIdempotentInstructionWithDerivation(payer, owner, mint);
201+
expect(ix).to.deep.eq(ixDerivation);
202+
});
170203
});
171204

172205
describe('state', () => {

0 commit comments

Comments
 (0)