Skip to content

Commit

Permalink
Add ATA Instruction discriminators in e2e tests (#33)
Browse files Browse the repository at this point in the history
This only affects the tests so no changeset is needed.
  • Loading branch information
lorisleiva authored May 17, 2024
1 parent e0ea30c commit 7fc38db
Show file tree
Hide file tree
Showing 7 changed files with 356 additions and 7 deletions.
66 changes: 63 additions & 3 deletions packages/renderers-js/e2e/token/idl.json
Original file line number Diff line number Diff line change
Expand Up @@ -2384,7 +2384,27 @@
}
}
],
"arguments": [],
"arguments": [
{
"kind": "instructionArgumentNode",
"name": "discriminator",
"type": {
"kind": "numberTypeNode",
"format": "u8",
"endian": "le"
},
"docs": [],
"defaultValue": { "kind": "numberValueNode", "number": 0 },
"defaultValueStrategy": "omitted"
}
],
"discriminators": [
{
"kind": "fieldDiscriminatorNode",
"name": "discriminator",
"offset": 0
}
],
"name": "createAssociatedToken",
"docs": [
"Creates an associated token account for the given wallet address and",
Expand Down Expand Up @@ -2486,7 +2506,27 @@
}
}
],
"arguments": [],
"arguments": [
{
"kind": "instructionArgumentNode",
"name": "discriminator",
"type": {
"kind": "numberTypeNode",
"format": "u8",
"endian": "le"
},
"docs": [],
"defaultValue": { "kind": "numberValueNode", "number": 1 },
"defaultValueStrategy": "omitted"
}
],
"discriminators": [
{
"kind": "fieldDiscriminatorNode",
"name": "discriminator",
"offset": 0
}
],
"name": "createAssociatedTokenIdempotent",
"docs": [
"Creates an associated token account for the given wallet address and",
Expand Down Expand Up @@ -2662,7 +2702,27 @@
}
}
],
"arguments": [],
"arguments": [
{
"kind": "instructionArgumentNode",
"name": "discriminator",
"type": {
"kind": "numberTypeNode",
"format": "u8",
"endian": "le"
},
"docs": [],
"defaultValue": { "kind": "numberValueNode", "number": 2 },
"defaultValueStrategy": "omitted"
}
],
"discriminators": [
{
"kind": "fieldDiscriminatorNode",
"name": "discriminator",
"offset": 0
}
],
"name": "recoverNestedAssociatedToken",
"docs": [
"Transfers from and closes a nested associated token account: an",
Expand Down
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
),
};
}
Loading

0 comments on commit 7fc38db

Please sign in to comment.