Skip to content

Add Instruction discriminators to Associated Token Program #3

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

Merged
merged 2 commits into from
May 17, 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
2 changes: 1 addition & 1 deletion clients/js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@solana-program/token",
"version": "0.1.3",
"version": "0.1.4",
"description": "JavaScript client for the Token program",
"sideEffects": false,
"module": "./dist/src/index.mjs",
Expand Down
46 changes: 45 additions & 1 deletion clients/js/src/generated/instructions/createAssociatedToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,24 @@

import {
Address,
Codec,
Decoder,
Encoder,
IAccountMeta,
IAccountSignerMeta,
IInstruction,
IInstructionWithAccounts,
IInstructionWithData,
ReadonlyAccount,
TransactionSigner,
WritableAccount,
WritableSignerAccount,
combineCodec,
getStructDecoder,
getStructEncoder,
getU8Decoder,
getU8Encoder,
transformEncoder,
} from '@solana/web3.js';
import { findAssociatedTokenPda } from '../pdas';
import { ASSOCIATED_TOKEN_PROGRAM_ADDRESS } from '../programs';
Expand All @@ -39,6 +49,7 @@ export type CreateAssociatedTokenInstruction<
| IAccountMeta<string> = 'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA',
TRemainingAccounts extends readonly IAccountMeta<string>[] = [],
> = IInstruction<TProgram> &
IInstructionWithData<Uint8Array> &
IInstructionWithAccounts<
[
TAccountPayer extends string
Expand All @@ -62,6 +73,31 @@ export type CreateAssociatedTokenInstruction<
]
>;

export type CreateAssociatedTokenInstructionData = { discriminator: number };

export type CreateAssociatedTokenInstructionDataArgs = {};

export function getCreateAssociatedTokenInstructionDataEncoder(): Encoder<CreateAssociatedTokenInstructionDataArgs> {
return transformEncoder(
getStructEncoder([['discriminator', getU8Encoder()]]),
(value) => ({ ...value, discriminator: 0 })
);
}

export function getCreateAssociatedTokenInstructionDataDecoder(): Decoder<CreateAssociatedTokenInstructionData> {
return getStructDecoder([['discriminator', getU8Decoder()]]);
}

export function getCreateAssociatedTokenInstructionDataCodec(): Codec<
CreateAssociatedTokenInstructionDataArgs,
CreateAssociatedTokenInstructionData
> {
return combineCodec(
getCreateAssociatedTokenInstructionDataEncoder(),
getCreateAssociatedTokenInstructionDataDecoder()
);
}

export type CreateAssociatedTokenAsyncInput<
TAccountPayer extends string = string,
TAccountAta extends string = string,
Expand Down Expand Up @@ -156,6 +192,7 @@ export async function getCreateAssociatedTokenInstructionAsync<
getAccountMeta(accounts.tokenProgram),
],
programAddress,
data: getCreateAssociatedTokenInstructionDataEncoder().encode({}),
} as CreateAssociatedTokenInstruction<
typeof ASSOCIATED_TOKEN_PROGRAM_ADDRESS,
TAccountPayer,
Expand Down Expand Up @@ -254,6 +291,7 @@ export function getCreateAssociatedTokenInstruction<
getAccountMeta(accounts.tokenProgram),
],
programAddress,
data: getCreateAssociatedTokenInstructionDataEncoder().encode({}),
} as CreateAssociatedTokenInstruction<
typeof ASSOCIATED_TOKEN_PROGRAM_ADDRESS,
TAccountPayer,
Expand Down Expand Up @@ -286,13 +324,16 @@ export type ParsedCreateAssociatedTokenInstruction<
/** SPL Token program. */
tokenProgram: TAccountMetas[5];
};
data: CreateAssociatedTokenInstructionData;
};

export function parseCreateAssociatedTokenInstruction<
TProgram extends string,
TAccountMetas extends readonly IAccountMeta[],
>(
instruction: IInstruction<TProgram> & IInstructionWithAccounts<TAccountMetas>
instruction: IInstruction<TProgram> &
IInstructionWithAccounts<TAccountMetas> &
IInstructionWithData<Uint8Array>
): ParsedCreateAssociatedTokenInstruction<TProgram, TAccountMetas> {
if (instruction.accounts.length < 6) {
// TODO: Coded error.
Expand All @@ -314,5 +355,8 @@ export function parseCreateAssociatedTokenInstruction<
systemProgram: getNextAccount(),
tokenProgram: getNextAccount(),
},
data: getCreateAssociatedTokenInstructionDataDecoder().decode(
instruction.data
),
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,24 @@

import {
Address,
Codec,
Decoder,
Encoder,
IAccountMeta,
IAccountSignerMeta,
IInstruction,
IInstructionWithAccounts,
IInstructionWithData,
ReadonlyAccount,
TransactionSigner,
WritableAccount,
WritableSignerAccount,
combineCodec,
getStructDecoder,
getStructEncoder,
getU8Decoder,
getU8Encoder,
transformEncoder,
} from '@solana/web3.js';
import { findAssociatedTokenPda } from '../pdas';
import { ASSOCIATED_TOKEN_PROGRAM_ADDRESS } from '../programs';
Expand All @@ -39,6 +49,7 @@ export type CreateAssociatedTokenIdempotentInstruction<
| IAccountMeta<string> = 'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA',
TRemainingAccounts extends readonly IAccountMeta<string>[] = [],
> = IInstruction<TProgram> &
IInstructionWithData<Uint8Array> &
IInstructionWithAccounts<
[
TAccountPayer extends string
Expand All @@ -62,6 +73,33 @@ export type CreateAssociatedTokenIdempotentInstruction<
]
>;

export type CreateAssociatedTokenIdempotentInstructionData = {
discriminator: number;
};

export type CreateAssociatedTokenIdempotentInstructionDataArgs = {};

export function getCreateAssociatedTokenIdempotentInstructionDataEncoder(): Encoder<CreateAssociatedTokenIdempotentInstructionDataArgs> {
return transformEncoder(
getStructEncoder([['discriminator', getU8Encoder()]]),
(value) => ({ ...value, discriminator: 1 })
);
}

export function getCreateAssociatedTokenIdempotentInstructionDataDecoder(): Decoder<CreateAssociatedTokenIdempotentInstructionData> {
return getStructDecoder([['discriminator', getU8Decoder()]]);
}

export function getCreateAssociatedTokenIdempotentInstructionDataCodec(): Codec<
CreateAssociatedTokenIdempotentInstructionDataArgs,
CreateAssociatedTokenIdempotentInstructionData
> {
return combineCodec(
getCreateAssociatedTokenIdempotentInstructionDataEncoder(),
getCreateAssociatedTokenIdempotentInstructionDataDecoder()
);
}

export type CreateAssociatedTokenIdempotentAsyncInput<
TAccountPayer extends string = string,
TAccountAta extends string = string,
Expand Down Expand Up @@ -156,6 +194,7 @@ export async function getCreateAssociatedTokenIdempotentInstructionAsync<
getAccountMeta(accounts.tokenProgram),
],
programAddress,
data: getCreateAssociatedTokenIdempotentInstructionDataEncoder().encode({}),
} as CreateAssociatedTokenIdempotentInstruction<
typeof ASSOCIATED_TOKEN_PROGRAM_ADDRESS,
TAccountPayer,
Expand Down Expand Up @@ -254,6 +293,7 @@ export function getCreateAssociatedTokenIdempotentInstruction<
getAccountMeta(accounts.tokenProgram),
],
programAddress,
data: getCreateAssociatedTokenIdempotentInstructionDataEncoder().encode({}),
} as CreateAssociatedTokenIdempotentInstruction<
typeof ASSOCIATED_TOKEN_PROGRAM_ADDRESS,
TAccountPayer,
Expand Down Expand Up @@ -286,13 +326,16 @@ export type ParsedCreateAssociatedTokenIdempotentInstruction<
/** SPL Token program. */
tokenProgram: TAccountMetas[5];
};
data: CreateAssociatedTokenIdempotentInstructionData;
};

export function parseCreateAssociatedTokenIdempotentInstruction<
TProgram extends string,
TAccountMetas extends readonly IAccountMeta[],
>(
instruction: IInstruction<TProgram> & IInstructionWithAccounts<TAccountMetas>
instruction: IInstruction<TProgram> &
IInstructionWithAccounts<TAccountMetas> &
IInstructionWithData<Uint8Array>
): ParsedCreateAssociatedTokenIdempotentInstruction<TProgram, TAccountMetas> {
if (instruction.accounts.length < 6) {
// TODO: Coded error.
Expand All @@ -314,5 +357,8 @@ export function parseCreateAssociatedTokenIdempotentInstruction<
systemProgram: getNextAccount(),
tokenProgram: getNextAccount(),
},
data: getCreateAssociatedTokenIdempotentInstructionDataDecoder().decode(
instruction.data
),
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,24 @@

import {
Address,
Codec,
Decoder,
Encoder,
IAccountMeta,
IAccountSignerMeta,
IInstruction,
IInstructionWithAccounts,
IInstructionWithData,
ReadonlyAccount,
TransactionSigner,
WritableAccount,
WritableSignerAccount,
combineCodec,
getStructDecoder,
getStructEncoder,
getU8Decoder,
getU8Encoder,
transformEncoder,
} from '@solana/web3.js';
import { findAssociatedTokenPda } from '../pdas';
import { ASSOCIATED_TOKEN_PROGRAM_ADDRESS } from '../programs';
Expand Down Expand Up @@ -44,6 +54,7 @@ export type RecoverNestedAssociatedTokenInstruction<
| IAccountMeta<string> = 'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA',
TRemainingAccounts extends readonly IAccountMeta<string>[] = [],
> = IInstruction<TProgram> &
IInstructionWithData<Uint8Array> &
IInstructionWithAccounts<
[
TAccountNestedAssociatedAccountAddress extends string
Expand Down Expand Up @@ -72,6 +83,33 @@ export type RecoverNestedAssociatedTokenInstruction<
]
>;

export type RecoverNestedAssociatedTokenInstructionData = {
discriminator: number;
};

export type RecoverNestedAssociatedTokenInstructionDataArgs = {};

export function getRecoverNestedAssociatedTokenInstructionDataEncoder(): Encoder<RecoverNestedAssociatedTokenInstructionDataArgs> {
return transformEncoder(
getStructEncoder([['discriminator', getU8Encoder()]]),
(value) => ({ ...value, discriminator: 2 })
);
}

export function getRecoverNestedAssociatedTokenInstructionDataDecoder(): Decoder<RecoverNestedAssociatedTokenInstructionData> {
return getStructDecoder([['discriminator', getU8Decoder()]]);
}

export function getRecoverNestedAssociatedTokenInstructionDataCodec(): Codec<
RecoverNestedAssociatedTokenInstructionDataArgs,
RecoverNestedAssociatedTokenInstructionData
> {
return combineCodec(
getRecoverNestedAssociatedTokenInstructionDataEncoder(),
getRecoverNestedAssociatedTokenInstructionDataDecoder()
);
}

export type RecoverNestedAssociatedTokenAsyncInput<
TAccountNestedAssociatedAccountAddress extends string = string,
TAccountNestedTokenMintAddress extends string = string,
Expand Down Expand Up @@ -203,6 +241,7 @@ export async function getRecoverNestedAssociatedTokenInstructionAsync<
getAccountMeta(accounts.tokenProgram),
],
programAddress,
data: getRecoverNestedAssociatedTokenInstructionDataEncoder().encode({}),
} as RecoverNestedAssociatedTokenInstruction<
typeof ASSOCIATED_TOKEN_PROGRAM_ADDRESS,
TAccountNestedAssociatedAccountAddress,
Expand Down Expand Up @@ -321,6 +360,7 @@ export function getRecoverNestedAssociatedTokenInstruction<
getAccountMeta(accounts.tokenProgram),
],
programAddress,
data: getRecoverNestedAssociatedTokenInstructionDataEncoder().encode({}),
} as RecoverNestedAssociatedTokenInstruction<
typeof ASSOCIATED_TOKEN_PROGRAM_ADDRESS,
TAccountNestedAssociatedAccountAddress,
Expand Down Expand Up @@ -356,13 +396,16 @@ export type ParsedRecoverNestedAssociatedTokenInstruction<
/** SPL Token program. */
tokenProgram: TAccountMetas[6];
};
data: RecoverNestedAssociatedTokenInstructionData;
};

export function parseRecoverNestedAssociatedTokenInstruction<
TProgram extends string,
TAccountMetas extends readonly IAccountMeta[],
>(
instruction: IInstruction<TProgram> & IInstructionWithAccounts<TAccountMetas>
instruction: IInstruction<TProgram> &
IInstructionWithAccounts<TAccountMetas> &
IInstructionWithData<Uint8Array>
): ParsedRecoverNestedAssociatedTokenInstruction<TProgram, TAccountMetas> {
if (instruction.accounts.length < 7) {
// TODO: Coded error.
Expand All @@ -385,5 +428,8 @@ export function parseRecoverNestedAssociatedTokenInstruction<
walletAddress: getNextAccount(),
tokenProgram: getNextAccount(),
},
data: getRecoverNestedAssociatedTokenInstructionDataDecoder().decode(
instruction.data
),
};
}
21 changes: 20 additions & 1 deletion clients/js/src/generated/programs/associatedToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @see https://github.com/metaplex-foundation/kinobi
*/

import { Address } from '@solana/web3.js';
import { Address, containsBytes, getU8Encoder } from '@solana/web3.js';
import {
ParsedCreateAssociatedTokenIdempotentInstruction,
ParsedCreateAssociatedTokenInstruction,
Expand All @@ -22,6 +22,25 @@ export enum AssociatedTokenInstruction {
RecoverNestedAssociatedToken,
}

export function identifyAssociatedTokenInstruction(
instruction: { data: Uint8Array } | Uint8Array
): AssociatedTokenInstruction {
const data =
instruction instanceof Uint8Array ? instruction : instruction.data;
if (containsBytes(data, getU8Encoder().encode(0), 0)) {
return AssociatedTokenInstruction.CreateAssociatedToken;
}
if (containsBytes(data, getU8Encoder().encode(1), 0)) {
return AssociatedTokenInstruction.CreateAssociatedTokenIdempotent;
}
if (containsBytes(data, getU8Encoder().encode(2), 0)) {
return AssociatedTokenInstruction.RecoverNestedAssociatedToken;
}
throw new Error(
'The provided instruction could not be identified as a associatedToken instruction.'
);
}

export type ParsedAssociatedTokenInstruction<
TProgram extends string = 'ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL',
> =
Expand Down
Loading