Skip to content

Commit 16889df

Browse files
authored
Add Instruction discriminators to Associated Token Program (#3)
* Add Instruction discriminators to Associated Token Program * Bump JS client
1 parent f476906 commit 16889df

File tree

8 files changed

+357
-8
lines changed

8 files changed

+357
-8
lines changed

clients/js/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@solana-program/token",
3-
"version": "0.1.3",
3+
"version": "0.1.4",
44
"description": "JavaScript client for the Token program",
55
"sideEffects": false,
66
"module": "./dist/src/index.mjs",

clients/js/src/generated/instructions/createAssociatedToken.ts

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,24 @@
88

99
import {
1010
Address,
11+
Codec,
12+
Decoder,
13+
Encoder,
1114
IAccountMeta,
1215
IAccountSignerMeta,
1316
IInstruction,
1417
IInstructionWithAccounts,
18+
IInstructionWithData,
1519
ReadonlyAccount,
1620
TransactionSigner,
1721
WritableAccount,
1822
WritableSignerAccount,
23+
combineCodec,
24+
getStructDecoder,
25+
getStructEncoder,
26+
getU8Decoder,
27+
getU8Encoder,
28+
transformEncoder,
1929
} from '@solana/web3.js';
2030
import { findAssociatedTokenPda } from '../pdas';
2131
import { ASSOCIATED_TOKEN_PROGRAM_ADDRESS } from '../programs';
@@ -39,6 +49,7 @@ export type CreateAssociatedTokenInstruction<
3949
| IAccountMeta<string> = 'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA',
4050
TRemainingAccounts extends readonly IAccountMeta<string>[] = [],
4151
> = IInstruction<TProgram> &
52+
IInstructionWithData<Uint8Array> &
4253
IInstructionWithAccounts<
4354
[
4455
TAccountPayer extends string
@@ -62,6 +73,31 @@ export type CreateAssociatedTokenInstruction<
6273
]
6374
>;
6475

76+
export type CreateAssociatedTokenInstructionData = { discriminator: number };
77+
78+
export type CreateAssociatedTokenInstructionDataArgs = {};
79+
80+
export function getCreateAssociatedTokenInstructionDataEncoder(): Encoder<CreateAssociatedTokenInstructionDataArgs> {
81+
return transformEncoder(
82+
getStructEncoder([['discriminator', getU8Encoder()]]),
83+
(value) => ({ ...value, discriminator: 0 })
84+
);
85+
}
86+
87+
export function getCreateAssociatedTokenInstructionDataDecoder(): Decoder<CreateAssociatedTokenInstructionData> {
88+
return getStructDecoder([['discriminator', getU8Decoder()]]);
89+
}
90+
91+
export function getCreateAssociatedTokenInstructionDataCodec(): Codec<
92+
CreateAssociatedTokenInstructionDataArgs,
93+
CreateAssociatedTokenInstructionData
94+
> {
95+
return combineCodec(
96+
getCreateAssociatedTokenInstructionDataEncoder(),
97+
getCreateAssociatedTokenInstructionDataDecoder()
98+
);
99+
}
100+
65101
export type CreateAssociatedTokenAsyncInput<
66102
TAccountPayer extends string = string,
67103
TAccountAta extends string = string,
@@ -156,6 +192,7 @@ export async function getCreateAssociatedTokenInstructionAsync<
156192
getAccountMeta(accounts.tokenProgram),
157193
],
158194
programAddress,
195+
data: getCreateAssociatedTokenInstructionDataEncoder().encode({}),
159196
} as CreateAssociatedTokenInstruction<
160197
typeof ASSOCIATED_TOKEN_PROGRAM_ADDRESS,
161198
TAccountPayer,
@@ -254,6 +291,7 @@ export function getCreateAssociatedTokenInstruction<
254291
getAccountMeta(accounts.tokenProgram),
255292
],
256293
programAddress,
294+
data: getCreateAssociatedTokenInstructionDataEncoder().encode({}),
257295
} as CreateAssociatedTokenInstruction<
258296
typeof ASSOCIATED_TOKEN_PROGRAM_ADDRESS,
259297
TAccountPayer,
@@ -286,13 +324,16 @@ export type ParsedCreateAssociatedTokenInstruction<
286324
/** SPL Token program. */
287325
tokenProgram: TAccountMetas[5];
288326
};
327+
data: CreateAssociatedTokenInstructionData;
289328
};
290329

291330
export function parseCreateAssociatedTokenInstruction<
292331
TProgram extends string,
293332
TAccountMetas extends readonly IAccountMeta[],
294333
>(
295-
instruction: IInstruction<TProgram> & IInstructionWithAccounts<TAccountMetas>
334+
instruction: IInstruction<TProgram> &
335+
IInstructionWithAccounts<TAccountMetas> &
336+
IInstructionWithData<Uint8Array>
296337
): ParsedCreateAssociatedTokenInstruction<TProgram, TAccountMetas> {
297338
if (instruction.accounts.length < 6) {
298339
// TODO: Coded error.
@@ -314,5 +355,8 @@ export function parseCreateAssociatedTokenInstruction<
314355
systemProgram: getNextAccount(),
315356
tokenProgram: getNextAccount(),
316357
},
358+
data: getCreateAssociatedTokenInstructionDataDecoder().decode(
359+
instruction.data
360+
),
317361
};
318362
}

clients/js/src/generated/instructions/createAssociatedTokenIdempotent.ts

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,24 @@
88

99
import {
1010
Address,
11+
Codec,
12+
Decoder,
13+
Encoder,
1114
IAccountMeta,
1215
IAccountSignerMeta,
1316
IInstruction,
1417
IInstructionWithAccounts,
18+
IInstructionWithData,
1519
ReadonlyAccount,
1620
TransactionSigner,
1721
WritableAccount,
1822
WritableSignerAccount,
23+
combineCodec,
24+
getStructDecoder,
25+
getStructEncoder,
26+
getU8Decoder,
27+
getU8Encoder,
28+
transformEncoder,
1929
} from '@solana/web3.js';
2030
import { findAssociatedTokenPda } from '../pdas';
2131
import { ASSOCIATED_TOKEN_PROGRAM_ADDRESS } from '../programs';
@@ -39,6 +49,7 @@ export type CreateAssociatedTokenIdempotentInstruction<
3949
| IAccountMeta<string> = 'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA',
4050
TRemainingAccounts extends readonly IAccountMeta<string>[] = [],
4151
> = IInstruction<TProgram> &
52+
IInstructionWithData<Uint8Array> &
4253
IInstructionWithAccounts<
4354
[
4455
TAccountPayer extends string
@@ -62,6 +73,33 @@ export type CreateAssociatedTokenIdempotentInstruction<
6273
]
6374
>;
6475

76+
export type CreateAssociatedTokenIdempotentInstructionData = {
77+
discriminator: number;
78+
};
79+
80+
export type CreateAssociatedTokenIdempotentInstructionDataArgs = {};
81+
82+
export function getCreateAssociatedTokenIdempotentInstructionDataEncoder(): Encoder<CreateAssociatedTokenIdempotentInstructionDataArgs> {
83+
return transformEncoder(
84+
getStructEncoder([['discriminator', getU8Encoder()]]),
85+
(value) => ({ ...value, discriminator: 1 })
86+
);
87+
}
88+
89+
export function getCreateAssociatedTokenIdempotentInstructionDataDecoder(): Decoder<CreateAssociatedTokenIdempotentInstructionData> {
90+
return getStructDecoder([['discriminator', getU8Decoder()]]);
91+
}
92+
93+
export function getCreateAssociatedTokenIdempotentInstructionDataCodec(): Codec<
94+
CreateAssociatedTokenIdempotentInstructionDataArgs,
95+
CreateAssociatedTokenIdempotentInstructionData
96+
> {
97+
return combineCodec(
98+
getCreateAssociatedTokenIdempotentInstructionDataEncoder(),
99+
getCreateAssociatedTokenIdempotentInstructionDataDecoder()
100+
);
101+
}
102+
65103
export type CreateAssociatedTokenIdempotentAsyncInput<
66104
TAccountPayer extends string = string,
67105
TAccountAta extends string = string,
@@ -156,6 +194,7 @@ export async function getCreateAssociatedTokenIdempotentInstructionAsync<
156194
getAccountMeta(accounts.tokenProgram),
157195
],
158196
programAddress,
197+
data: getCreateAssociatedTokenIdempotentInstructionDataEncoder().encode({}),
159198
} as CreateAssociatedTokenIdempotentInstruction<
160199
typeof ASSOCIATED_TOKEN_PROGRAM_ADDRESS,
161200
TAccountPayer,
@@ -254,6 +293,7 @@ export function getCreateAssociatedTokenIdempotentInstruction<
254293
getAccountMeta(accounts.tokenProgram),
255294
],
256295
programAddress,
296+
data: getCreateAssociatedTokenIdempotentInstructionDataEncoder().encode({}),
257297
} as CreateAssociatedTokenIdempotentInstruction<
258298
typeof ASSOCIATED_TOKEN_PROGRAM_ADDRESS,
259299
TAccountPayer,
@@ -286,13 +326,16 @@ export type ParsedCreateAssociatedTokenIdempotentInstruction<
286326
/** SPL Token program. */
287327
tokenProgram: TAccountMetas[5];
288328
};
329+
data: CreateAssociatedTokenIdempotentInstructionData;
289330
};
290331

291332
export function parseCreateAssociatedTokenIdempotentInstruction<
292333
TProgram extends string,
293334
TAccountMetas extends readonly IAccountMeta[],
294335
>(
295-
instruction: IInstruction<TProgram> & IInstructionWithAccounts<TAccountMetas>
336+
instruction: IInstruction<TProgram> &
337+
IInstructionWithAccounts<TAccountMetas> &
338+
IInstructionWithData<Uint8Array>
296339
): ParsedCreateAssociatedTokenIdempotentInstruction<TProgram, TAccountMetas> {
297340
if (instruction.accounts.length < 6) {
298341
// TODO: Coded error.
@@ -314,5 +357,8 @@ export function parseCreateAssociatedTokenIdempotentInstruction<
314357
systemProgram: getNextAccount(),
315358
tokenProgram: getNextAccount(),
316359
},
360+
data: getCreateAssociatedTokenIdempotentInstructionDataDecoder().decode(
361+
instruction.data
362+
),
317363
};
318364
}

clients/js/src/generated/instructions/recoverNestedAssociatedToken.ts

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,24 @@
88

99
import {
1010
Address,
11+
Codec,
12+
Decoder,
13+
Encoder,
1114
IAccountMeta,
1215
IAccountSignerMeta,
1316
IInstruction,
1417
IInstructionWithAccounts,
18+
IInstructionWithData,
1519
ReadonlyAccount,
1620
TransactionSigner,
1721
WritableAccount,
1822
WritableSignerAccount,
23+
combineCodec,
24+
getStructDecoder,
25+
getStructEncoder,
26+
getU8Decoder,
27+
getU8Encoder,
28+
transformEncoder,
1929
} from '@solana/web3.js';
2030
import { findAssociatedTokenPda } from '../pdas';
2131
import { ASSOCIATED_TOKEN_PROGRAM_ADDRESS } from '../programs';
@@ -44,6 +54,7 @@ export type RecoverNestedAssociatedTokenInstruction<
4454
| IAccountMeta<string> = 'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA',
4555
TRemainingAccounts extends readonly IAccountMeta<string>[] = [],
4656
> = IInstruction<TProgram> &
57+
IInstructionWithData<Uint8Array> &
4758
IInstructionWithAccounts<
4859
[
4960
TAccountNestedAssociatedAccountAddress extends string
@@ -72,6 +83,33 @@ export type RecoverNestedAssociatedTokenInstruction<
7283
]
7384
>;
7485

86+
export type RecoverNestedAssociatedTokenInstructionData = {
87+
discriminator: number;
88+
};
89+
90+
export type RecoverNestedAssociatedTokenInstructionDataArgs = {};
91+
92+
export function getRecoverNestedAssociatedTokenInstructionDataEncoder(): Encoder<RecoverNestedAssociatedTokenInstructionDataArgs> {
93+
return transformEncoder(
94+
getStructEncoder([['discriminator', getU8Encoder()]]),
95+
(value) => ({ ...value, discriminator: 2 })
96+
);
97+
}
98+
99+
export function getRecoverNestedAssociatedTokenInstructionDataDecoder(): Decoder<RecoverNestedAssociatedTokenInstructionData> {
100+
return getStructDecoder([['discriminator', getU8Decoder()]]);
101+
}
102+
103+
export function getRecoverNestedAssociatedTokenInstructionDataCodec(): Codec<
104+
RecoverNestedAssociatedTokenInstructionDataArgs,
105+
RecoverNestedAssociatedTokenInstructionData
106+
> {
107+
return combineCodec(
108+
getRecoverNestedAssociatedTokenInstructionDataEncoder(),
109+
getRecoverNestedAssociatedTokenInstructionDataDecoder()
110+
);
111+
}
112+
75113
export type RecoverNestedAssociatedTokenAsyncInput<
76114
TAccountNestedAssociatedAccountAddress extends string = string,
77115
TAccountNestedTokenMintAddress extends string = string,
@@ -203,6 +241,7 @@ export async function getRecoverNestedAssociatedTokenInstructionAsync<
203241
getAccountMeta(accounts.tokenProgram),
204242
],
205243
programAddress,
244+
data: getRecoverNestedAssociatedTokenInstructionDataEncoder().encode({}),
206245
} as RecoverNestedAssociatedTokenInstruction<
207246
typeof ASSOCIATED_TOKEN_PROGRAM_ADDRESS,
208247
TAccountNestedAssociatedAccountAddress,
@@ -321,6 +360,7 @@ export function getRecoverNestedAssociatedTokenInstruction<
321360
getAccountMeta(accounts.tokenProgram),
322361
],
323362
programAddress,
363+
data: getRecoverNestedAssociatedTokenInstructionDataEncoder().encode({}),
324364
} as RecoverNestedAssociatedTokenInstruction<
325365
typeof ASSOCIATED_TOKEN_PROGRAM_ADDRESS,
326366
TAccountNestedAssociatedAccountAddress,
@@ -356,13 +396,16 @@ export type ParsedRecoverNestedAssociatedTokenInstruction<
356396
/** SPL Token program. */
357397
tokenProgram: TAccountMetas[6];
358398
};
399+
data: RecoverNestedAssociatedTokenInstructionData;
359400
};
360401

361402
export function parseRecoverNestedAssociatedTokenInstruction<
362403
TProgram extends string,
363404
TAccountMetas extends readonly IAccountMeta[],
364405
>(
365-
instruction: IInstruction<TProgram> & IInstructionWithAccounts<TAccountMetas>
406+
instruction: IInstruction<TProgram> &
407+
IInstructionWithAccounts<TAccountMetas> &
408+
IInstructionWithData<Uint8Array>
366409
): ParsedRecoverNestedAssociatedTokenInstruction<TProgram, TAccountMetas> {
367410
if (instruction.accounts.length < 7) {
368411
// TODO: Coded error.
@@ -385,5 +428,8 @@ export function parseRecoverNestedAssociatedTokenInstruction<
385428
walletAddress: getNextAccount(),
386429
tokenProgram: getNextAccount(),
387430
},
431+
data: getRecoverNestedAssociatedTokenInstructionDataDecoder().decode(
432+
instruction.data
433+
),
388434
};
389435
}

clients/js/src/generated/programs/associatedToken.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* @see https://github.com/metaplex-foundation/kinobi
77
*/
88

9-
import { Address } from '@solana/web3.js';
9+
import { Address, containsBytes, getU8Encoder } from '@solana/web3.js';
1010
import {
1111
ParsedCreateAssociatedTokenIdempotentInstruction,
1212
ParsedCreateAssociatedTokenInstruction,
@@ -22,6 +22,25 @@ export enum AssociatedTokenInstruction {
2222
RecoverNestedAssociatedToken,
2323
}
2424

25+
export function identifyAssociatedTokenInstruction(
26+
instruction: { data: Uint8Array } | Uint8Array
27+
): AssociatedTokenInstruction {
28+
const data =
29+
instruction instanceof Uint8Array ? instruction : instruction.data;
30+
if (containsBytes(data, getU8Encoder().encode(0), 0)) {
31+
return AssociatedTokenInstruction.CreateAssociatedToken;
32+
}
33+
if (containsBytes(data, getU8Encoder().encode(1), 0)) {
34+
return AssociatedTokenInstruction.CreateAssociatedTokenIdempotent;
35+
}
36+
if (containsBytes(data, getU8Encoder().encode(2), 0)) {
37+
return AssociatedTokenInstruction.RecoverNestedAssociatedToken;
38+
}
39+
throw new Error(
40+
'The provided instruction could not be identified as a associatedToken instruction.'
41+
);
42+
}
43+
2544
export type ParsedAssociatedTokenInstruction<
2645
TProgram extends string = 'ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL',
2746
> =

0 commit comments

Comments
 (0)