diff --git a/.gitignore b/.gitignore index 62b7fd6..a023c4e 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ dist .amman .crates .bin +.idea diff --git a/clients/js/.gitignore b/clients/js/.gitignore index 47e3bbc..bf57af5 100644 --- a/clients/js/.gitignore +++ b/clients/js/.gitignore @@ -1,2 +1,3 @@ .vercel docs +.idea diff --git a/clients/js/package.json b/clients/js/package.json index 396c876..86e9148 100644 --- a/clients/js/package.json +++ b/clients/js/package.json @@ -1,6 +1,6 @@ { "name": "@metaplex-foundation/mpl-toolbox", - "version": "0.9.2", + "version": "0.9.3", "description": "Auto-generated essential Solana and Metaplex programs", "main": "dist/src/index.js", "types": "dist/src/index.d.ts", diff --git a/clients/js/src/createMintWithAssociatedToken.ts b/clients/js/src/createMintWithAssociatedToken.ts index a295f22..1b0f0cf 100644 --- a/clients/js/src/createMintWithAssociatedToken.ts +++ b/clients/js/src/createMintWithAssociatedToken.ts @@ -9,7 +9,7 @@ import { } from '@metaplex-foundation/umi'; import { createMint, CreateMintArgs } from './createMint'; import { createAssociatedToken, mintTokensTo } from './generated'; -import { findAssociatedTokenPda } from './hooked'; +import { findAssociatedTokenPda, TOKEN_PROGRAM_ID } from './hooked'; // Inputs. export type CreateMintWithAssociatedTokenArgs = Omit< @@ -47,7 +47,10 @@ export function createMintWithAssociatedToken( mintTokensTo(context, { amount, mint: input.mint.publicKey, - token: findAssociatedTokenPda(context, mintAndOwner), + token: findAssociatedTokenPda(context, { + ...mintAndOwner, + tokenProgramId: TOKEN_PROGRAM_ID, + }), mintAuthority: input.mintAuthority && isSigner(input.mintAuthority) ? input.mintAuthority diff --git a/clients/js/src/hooked/AssociatedToken.ts b/clients/js/src/hooked/AssociatedToken.ts index 14b7557..55998c0 100644 --- a/clients/js/src/hooked/AssociatedToken.ts +++ b/clients/js/src/hooked/AssociatedToken.ts @@ -1,5 +1,6 @@ import { Context, Pda, PublicKey } from '@metaplex-foundation/umi'; import { publicKey } from '@metaplex-foundation/umi/serializers'; +import { TOKEN_PROGRAM_ID, TOKEN_2022_PROGRAM_ID } from './constants'; export function findAssociatedTokenPda( context: Pick, @@ -8,14 +9,15 @@ export function findAssociatedTokenPda( mint: PublicKey; /** The owner of the token account */ owner: PublicKey; + /** The Token or Token2022 Program id */ + tokenProgramId: typeof TOKEN_PROGRAM_ID | typeof TOKEN_2022_PROGRAM_ID; } ): Pda { const associatedTokenProgramId = context.programs.getPublicKey('splAssociatedToken'); - const tokenProgramId = context.programs.getPublicKey('splToken'); return context.eddsa.findPda(associatedTokenProgramId, [ publicKey().serialize(seeds.owner), - publicKey().serialize(tokenProgramId), + publicKey().serialize(seeds.tokenProgramId), publicKey().serialize(seeds.mint), ]); } diff --git a/clients/js/src/hooked/constants.ts b/clients/js/src/hooked/constants.ts new file mode 100644 index 0000000..07ef56c --- /dev/null +++ b/clients/js/src/hooked/constants.ts @@ -0,0 +1,11 @@ +import { publicKey } from '@metaplex-foundation/umi'; + +/** Address of the SPL Token program */ +export const TOKEN_PROGRAM_ID = publicKey( + 'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA' +); + +/** Address of the SPL Token 2022 program */ +export const TOKEN_2022_PROGRAM_ID = publicKey( + 'TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb' +); diff --git a/clients/js/src/hooked/index.ts b/clients/js/src/hooked/index.ts index 1e20c2f..e689165 100644 --- a/clients/js/src/hooked/index.ts +++ b/clients/js/src/hooked/index.ts @@ -1,2 +1,3 @@ export * from './AssociatedToken'; export * from './resolvers'; +export * from './constants'; diff --git a/clients/js/test/createAssociatedToken.test.ts b/clients/js/test/createAssociatedToken.test.ts index c524701..b59b654 100644 --- a/clients/js/test/createAssociatedToken.test.ts +++ b/clients/js/test/createAssociatedToken.test.ts @@ -13,6 +13,7 @@ import { getTokenSize, Token, TokenState, + TOKEN_PROGRAM_ID, } from '../src'; import { createUmi } from './_setup'; @@ -32,6 +33,7 @@ test('it can create new associated token accounts with minimum configuration', a const [ata] = findAssociatedTokenPda(umi, { mint: newMint.publicKey, owner: umi.identity.publicKey, + tokenProgramId: TOKEN_PROGRAM_ID, }); const tokenAccount = await fetchToken(umi, ata); t.like(tokenAccount, { @@ -62,6 +64,7 @@ test('it can create new associated token accounts with maximum configuration', a const [ata] = findAssociatedTokenPda(umi, { mint: newMint.publicKey, owner: newOwner.publicKey, + tokenProgramId: TOKEN_PROGRAM_ID, }); await createMint(umi, { mint: newMint }).sendAndConfirm(umi); diff --git a/clients/js/test/createLutForTransactionBuilder.test.ts b/clients/js/test/createLutForTransactionBuilder.test.ts index 1ff710c..3dcc71c 100644 --- a/clients/js/test/createLutForTransactionBuilder.test.ts +++ b/clients/js/test/createLutForTransactionBuilder.test.ts @@ -13,6 +13,7 @@ import { findAddressLookupTablePda, findAssociatedTokenPda, transferSol, + TOKEN_PROGRAM_ID, } from '../src'; import { createUmi } from './_setup'; @@ -24,7 +25,11 @@ test('it generates LUT builders for a given transaction builder', async (t) => { // And a base builder that creates an associated token account. const mint = generateSigner(umi); const owner = generateSigner(umi).publicKey; - const [ata] = findAssociatedTokenPda(umi, { mint: mint.publicKey, owner }); + const [ata] = findAssociatedTokenPda(umi, { + mint: mint.publicKey, + owner, + tokenProgramId: TOKEN_PROGRAM_ID, + }); const baseBuilder = transactionBuilder() .add(createMint(umi, { mint })) .add(createAssociatedToken(umi, { mint: mint.publicKey, owner })); diff --git a/clients/js/test/createMintWithAssociatedToken.test.ts b/clients/js/test/createMintWithAssociatedToken.test.ts index 8229071..18ddecf 100644 --- a/clients/js/test/createMintWithAssociatedToken.test.ts +++ b/clients/js/test/createMintWithAssociatedToken.test.ts @@ -13,6 +13,7 @@ import { Mint, Token, TokenState, + TOKEN_PROGRAM_ID, } from '../src'; import { createUmi } from './_setup'; @@ -37,7 +38,11 @@ test('it can create a new mint and token account with no tokens', async (t) => { }); // And a new associated token account with no tokens. - const [ata] = findAssociatedTokenPda(umi, { mint: mint.publicKey, owner }); + const [ata] = findAssociatedTokenPda(umi, { + mint: mint.publicKey, + owner, + tokenProgramId: TOKEN_PROGRAM_ID, + }); const tokenAccount = await fetchToken(umi, ata); t.like(tokenAccount, { publicKey: ata, @@ -77,7 +82,11 @@ test('it can create a new mint and token account with a single token', async (t) }); // And a new associated token account with one token. - const [ata] = findAssociatedTokenPda(umi, { mint: mint.publicKey, owner }); + const [ata] = findAssociatedTokenPda(umi, { + mint: mint.publicKey, + owner, + tokenProgramId: TOKEN_PROGRAM_ID, + }); const tokenAccount = await fetchToken(umi, ata); t.like(tokenAccount, { publicKey: ata, @@ -117,7 +126,11 @@ test('it can create a new mint and token account with many tokens', async (t) => }); // And a new associated token account with 42 tokens. - const [ata] = findAssociatedTokenPda(umi, { mint: mint.publicKey, owner }); + const [ata] = findAssociatedTokenPda(umi, { + mint: mint.publicKey, + owner, + tokenProgramId: TOKEN_PROGRAM_ID, + }); const tokenAccount = await fetchToken(umi, ata); t.like(tokenAccount, { publicKey: ata, @@ -158,7 +171,11 @@ test('it can create a new mint and token account with decimals', async (t) => { }); // And a new associated token account with 42 tokens. - const [ata] = findAssociatedTokenPda(umi, { mint: mint.publicKey, owner }); + const [ata] = findAssociatedTokenPda(umi, { + mint: mint.publicKey, + owner, + tokenProgramId: TOKEN_PROGRAM_ID, + }); const tokenAccount = await fetchToken(umi, ata); t.like(tokenAccount, { publicKey: ata, @@ -196,6 +213,7 @@ test('it defaults to using the identity as the owner', async (t) => { const [ata] = findAssociatedTokenPda(umi, { mint: mint.publicKey, owner: umi.identity.publicKey, + tokenProgramId: TOKEN_PROGRAM_ID, }); const tokenAccount = await fetchToken(umi, ata); t.like(tokenAccount, { diff --git a/clients/js/test/createTokenIfMissing.test.ts b/clients/js/test/createTokenIfMissing.test.ts index 67f232e..c451790 100644 --- a/clients/js/test/createTokenIfMissing.test.ts +++ b/clients/js/test/createTokenIfMissing.test.ts @@ -22,6 +22,7 @@ import { TokExInvalidTokenMintError, TokExInvalidTokenOwnerError, TokExInvalidTokenProgramError, + TOKEN_PROGRAM_ID, } from '../src'; import { createMint, createUmi } from './_setup'; @@ -35,7 +36,11 @@ test('it creates a new associated token if missing', async (t) => { await createTokenIfMissing(umi, { mint, owner }).sendAndConfirm(umi); // Then a new associated token account was created. - const [ata] = findAssociatedTokenPda(umi, { mint, owner }); + const [ata] = findAssociatedTokenPda(umi, { + mint, + owner, + tokenProgramId: TOKEN_PROGRAM_ID, + }); const ataAccount = await fetchToken(umi, ata); t.like(ataAccount, { publicKey: ata, @@ -56,7 +61,11 @@ test('it defaults to the identity if no owner is provided', async (t) => { await createTokenIfMissing(umi, { mint }).sendAndConfirm(umi); // Then a new associated token account was created for the identity. - const [ata] = findAssociatedTokenPda(umi, { mint, owner: identity }); + const [ata] = findAssociatedTokenPda(umi, { + mint, + owner: identity, + tokenProgramId: TOKEN_PROGRAM_ID, + }); const ataAccount = await fetchToken(umi, ata); t.like(ataAccount, { publicKey: ata, @@ -83,7 +92,11 @@ test('the payer pays for the storage fees if a token account gets created', asyn t.deepEqual(payerBalance, subtractAmounts(sol(100), storageFee)); // And this matches the lamports on the ATA account. - const ata = findAssociatedTokenPda(umi, { mint, owner: identity }); + const ata = findAssociatedTokenPda(umi, { + mint, + owner: identity, + tokenProgramId: TOKEN_PROGRAM_ID, + }); const ataAccount = await fetchToken(umi, ata); t.deepEqual(ataAccount.header.lamports, storageFee); }); @@ -93,7 +106,11 @@ test('it does not create an account if an associated token account already exist const umi = await createUmi(); const mint = (await createMint(umi)).publicKey; const owner = generateSigner(umi).publicKey; - const [ata] = findAssociatedTokenPda(umi, { mint, owner }); + const [ata] = findAssociatedTokenPda(umi, { + mint, + owner, + tokenProgramId: TOKEN_PROGRAM_ID, + }); await createAssociatedToken(umi, { mint, owner }).sendAndConfirm(umi); t.true(await umi.rpc.accountExists(ata)); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 3bd7544..3b43ce5 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1857,6 +1857,7 @@ packages: /node-gyp-build@4.5.0: resolution: {integrity: sha512-2iGbaQBV+ITgCz76ZEjmhUKAKVf7xfY1sRl4UiKQspfZMH2h06SyhNsnSVy50cwkFQDGLyif6m/6uFXHkOZ6rg==} hasBin: true + requiresBuild: true dev: true /npm-run-path@4.0.1: