From 6021d472366ead2b04a6b6d2786b092bcc47a325 Mon Sep 17 00:00:00 2001 From: shotgunofdeath Date: Wed, 7 Feb 2024 12:17:07 +0100 Subject: [PATCH] feat(associated token pda search): add tokenProgramId param BREAKING CHANGE: the tokenProgramId param for the findAssociatedTokenPda() function breaks js tests which use instructions generated by Kinobi. It will also break any code relying on this function. --- .gitignore | 1 + clients/js/.gitignore | 1 + clients/js/package.json | 2 +- .../instructions/createAssociatedToken.ts | 15 ++++++++------- .../instructions/createTokenIfMissing.ts | 15 ++++++++------- clients/js/src/hooked/AssociatedToken.ts | 7 +++++-- configs/kinobi.cjs | 6 +++++- pnpm-lock.yaml | 1 + 8 files changed, 30 insertions(+), 18 deletions(-) diff --git a/.gitignore b/.gitignore index 62b7fd67..a023c4e4 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 47e3bbc8..bf57af5d 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 396c8768..86e91486 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/generated/instructions/createAssociatedToken.ts b/clients/js/src/generated/instructions/createAssociatedToken.ts index 49526f90..e287341c 100644 --- a/clients/js/src/generated/instructions/createAssociatedToken.ts +++ b/clients/js/src/generated/instructions/createAssociatedToken.ts @@ -70,10 +70,18 @@ export function createAssociatedToken( if (!resolvedAccounts.owner.value) { resolvedAccounts.owner.value = context.identity.publicKey; } + if (!resolvedAccounts.tokenProgram.value) { + resolvedAccounts.tokenProgram.value = context.programs.getPublicKey( + 'splToken', + 'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA' + ); + resolvedAccounts.tokenProgram.isWritable = false; + } if (!resolvedAccounts.ata.value) { resolvedAccounts.ata.value = findAssociatedTokenPda(context, { owner: expectPublicKey(resolvedAccounts.owner.value), mint: expectPublicKey(resolvedAccounts.mint.value), + tokenProgramId: expectPublicKey(resolvedAccounts.tokenProgram.value), }); } if (!resolvedAccounts.systemProgram.value) { @@ -83,13 +91,6 @@ export function createAssociatedToken( ); resolvedAccounts.systemProgram.isWritable = false; } - if (!resolvedAccounts.tokenProgram.value) { - resolvedAccounts.tokenProgram.value = context.programs.getPublicKey( - 'splToken', - 'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA' - ); - resolvedAccounts.tokenProgram.isWritable = false; - } // Accounts in order. const orderedAccounts: ResolvedAccount[] = Object.values( diff --git a/clients/js/src/generated/instructions/createTokenIfMissing.ts b/clients/js/src/generated/instructions/createTokenIfMissing.ts index 2904110d..353fa418 100644 --- a/clients/js/src/generated/instructions/createTokenIfMissing.ts +++ b/clients/js/src/generated/instructions/createTokenIfMissing.ts @@ -115,10 +115,18 @@ export function createTokenIfMissing( if (!resolvedAccounts.owner.value) { resolvedAccounts.owner.value = context.identity.publicKey; } + if (!resolvedAccounts.tokenProgram.value) { + resolvedAccounts.tokenProgram.value = context.programs.getPublicKey( + 'splToken', + 'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA' + ); + resolvedAccounts.tokenProgram.isWritable = false; + } if (!resolvedAccounts.ata.value) { resolvedAccounts.ata.value = findAssociatedTokenPda(context, { owner: expectPublicKey(resolvedAccounts.owner.value), mint: expectPublicKey(resolvedAccounts.mint.value), + tokenProgramId: expectPublicKey(resolvedAccounts.tokenProgram.value), }); } if (!resolvedAccounts.token.value) { @@ -131,13 +139,6 @@ export function createTokenIfMissing( ); resolvedAccounts.systemProgram.isWritable = false; } - if (!resolvedAccounts.tokenProgram.value) { - resolvedAccounts.tokenProgram.value = context.programs.getPublicKey( - 'splToken', - 'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA' - ); - resolvedAccounts.tokenProgram.isWritable = false; - } if (!resolvedAccounts.ataProgram.value) { resolvedAccounts.ataProgram.value = context.programs.getPublicKey( 'splAssociatedToken', diff --git a/clients/js/src/hooked/AssociatedToken.ts b/clients/js/src/hooked/AssociatedToken.ts index 14b7557f..98f80109 100644 --- a/clients/js/src/hooked/AssociatedToken.ts +++ b/clients/js/src/hooked/AssociatedToken.ts @@ -8,14 +8,17 @@ export function findAssociatedTokenPda( mint: PublicKey; /** The owner of the token account */ owner: PublicKey; + /** The Token or Token2022 Program id */ + tokenProgramId?: PublicKey; } ): Pda { const associatedTokenProgramId = context.programs.getPublicKey('splAssociatedToken'); - const tokenProgramId = context.programs.getPublicKey('splToken'); + const tokenProgramIdResolved = + seeds.tokenProgramId ?? context.programs.getPublicKey('splToken'); return context.eddsa.findPda(associatedTokenProgramId, [ publicKey().serialize(seeds.owner), - publicKey().serialize(tokenProgramId), + publicKey().serialize(tokenProgramIdResolved), publicKey().serialize(seeds.mint), ]); } diff --git a/configs/kinobi.cjs b/configs/kinobi.cjs index 394cd09f..c7e783ac 100755 --- a/configs/kinobi.cjs +++ b/configs/kinobi.cjs @@ -63,7 +63,11 @@ kinobi.update( // Update instructions. const ataPdaDefaults = k.pdaDefault("AssociatedToken", { importFrom: "hooked", - seeds: { owner: k.accountDefault("owner"), mint: k.accountDefault("mint") }, + seeds: { + owner: k.accountDefault("owner"), + mint: k.accountDefault("mint"), + tokenProgramId: k.accountDefault("tokenProgram") + }, }); kinobi.update( new k.UpdateInstructionsVisitor({ diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 3bd7544e..3b43ce58 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: