Skip to content
This repository has been archived by the owner on Jan 10, 2025. It is now read-only.

Commit

Permalink
token-js: add derivation of createAssociatedTokenAccountIdempotentIns…
Browse files Browse the repository at this point in the history
…truction (#7183)

* add derivation of createAssociatedTokenAccountIdempotentInstruction

* fix minor issues

* fix format

* fix lint

* remove TODO comment
  • Loading branch information
zzau13 authored Aug 23, 2024
1 parent ab83005 commit ab9fa9a
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
33 changes: 33 additions & 0 deletions token/js/src/instructions/associatedTokenAccount.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -64,6 +65,38 @@ export function createAssociatedTokenAccountIdempotentInstruction(
);
}

/**
* Derive the associated token account and construct a CreateAssociatedTokenAccountIdempotent 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 createAssociatedTokenAccountIdempotentInstructionWithDerivation(
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,
associatedToken,
owner,
mint,
programId,
associatedTokenProgramId,
);
}

function buildAssociatedTokenAccountInstruction(
payer: PublicKey,
associatedToken: PublicKey,
Expand Down
33 changes: 33 additions & 0 deletions token/js/test/unit/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { expect, use } from 'chai';
import {
ASSOCIATED_TOKEN_PROGRAM_ID,
createAssociatedTokenAccountInstruction,
createAssociatedTokenAccountIdempotentInstruction,
createAssociatedTokenAccountIdempotentInstructionWithDerivation,
createReallocateInstruction,
createInitializeMintInstruction,
createInitializeMint2Instruction,
Expand Down Expand Up @@ -167,6 +169,37 @@ describe('spl-associated-token-account instructions', () => {
expect(ix.programId).to.eql(ASSOCIATED_TOKEN_PROGRAM_ID);
expect(ix.keys).to.have.length(6);
});

it('create idempotent', () => {
const ix = createAssociatedTokenAccountIdempotentInstruction(
Keypair.generate().publicKey,
Keypair.generate().publicKey,
Keypair.generate().publicKey,
Keypair.generate().publicKey,
);
expect(ix.programId).to.eql(ASSOCIATED_TOKEN_PROGRAM_ID);
expect(ix.keys).to.have.length(6);
});

it('create idempotent with derivation', () => {
const ix = createAssociatedTokenAccountIdempotentInstructionWithDerivation(
Keypair.generate().publicKey,
Keypair.generate().publicKey,
Keypair.generate().publicKey,
);
expect(ix.programId).to.eql(ASSOCIATED_TOKEN_PROGRAM_ID);
expect(ix.keys).to.have.length(6);
});

it('create idempotent with derivation same without', () => {
const payer = Keypair.generate().publicKey;
const owner = Keypair.generate().publicKey;
const mint = Keypair.generate().publicKey;
const associatedToken = getAssociatedTokenAddressSync(mint, owner, true);
const ix = createAssociatedTokenAccountIdempotentInstruction(payer, associatedToken, owner, mint);
const ixDerivation = createAssociatedTokenAccountIdempotentInstructionWithDerivation(payer, owner, mint);
expect(ix).to.deep.eq(ixDerivation);
});
});

describe('state', () => {
Expand Down

0 comments on commit ab9fa9a

Please sign in to comment.