Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

token-js: add derivation of createAssociatedTokenAccountIdempotentInstruction #7183

Merged
merged 6 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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