| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- /**
- * This code was AUTOGENERATED using the codama library.
- * Please DO NOT EDIT THIS FILE, instead use visitors
- * to add features, then rerun codama to update it.
- *
- * @see https://github.com/codama-idl/codama
- */
- import {
- combineCodec,
- getAddressDecoder,
- getAddressEncoder,
- getStructDecoder,
- getStructEncoder,
- getU32Decoder,
- getU32Encoder,
- transformEncoder,
- type AccountMeta,
- type AccountSignerMeta,
- type Address,
- type FixedSizeCodec,
- type FixedSizeDecoder,
- type FixedSizeEncoder,
- type Instruction,
- type InstructionWithAccounts,
- type InstructionWithData,
- type ReadonlyUint8Array,
- type TransactionSigner,
- type WritableSignerAccount,
- } from '@solana/kit';
- import { SYSTEM_PROGRAM_ADDRESS } from '../programs';
- import { getAccountMetaFactory, type ResolvedAccount } from '../shared';
- export const ASSIGN_DISCRIMINATOR = 1;
- export function getAssignDiscriminatorBytes() {
- return getU32Encoder().encode(ASSIGN_DISCRIMINATOR);
- }
- export type AssignInstruction<
- TProgram extends string = typeof SYSTEM_PROGRAM_ADDRESS,
- TAccountAccount extends string | AccountMeta<string> = string,
- TRemainingAccounts extends readonly AccountMeta<string>[] = [],
- > = Instruction<TProgram> &
- InstructionWithData<ReadonlyUint8Array> &
- InstructionWithAccounts<
- [
- TAccountAccount extends string
- ? WritableSignerAccount<TAccountAccount> &
- AccountSignerMeta<TAccountAccount>
- : TAccountAccount,
- ...TRemainingAccounts,
- ]
- >;
- export type AssignInstructionData = {
- discriminator: number;
- programAddress: Address;
- };
- export type AssignInstructionDataArgs = { programAddress: Address };
- export function getAssignInstructionDataEncoder(): FixedSizeEncoder<AssignInstructionDataArgs> {
- return transformEncoder(
- getStructEncoder([
- ['discriminator', getU32Encoder()],
- ['programAddress', getAddressEncoder()],
- ]),
- (value) => ({ ...value, discriminator: ASSIGN_DISCRIMINATOR })
- );
- }
- export function getAssignInstructionDataDecoder(): FixedSizeDecoder<AssignInstructionData> {
- return getStructDecoder([
- ['discriminator', getU32Decoder()],
- ['programAddress', getAddressDecoder()],
- ]);
- }
- export function getAssignInstructionDataCodec(): FixedSizeCodec<
- AssignInstructionDataArgs,
- AssignInstructionData
- > {
- return combineCodec(
- getAssignInstructionDataEncoder(),
- getAssignInstructionDataDecoder()
- );
- }
- export type AssignInput<TAccountAccount extends string = string> = {
- account: TransactionSigner<TAccountAccount>;
- programAddress: AssignInstructionDataArgs['programAddress'];
- };
- export function getAssignInstruction<
- TAccountAccount extends string,
- TProgramAddress extends Address = typeof SYSTEM_PROGRAM_ADDRESS,
- >(
- input: AssignInput<TAccountAccount>,
- config?: { programAddress?: TProgramAddress }
- ): AssignInstruction<TProgramAddress, TAccountAccount> {
- // Program address.
- const programAddress = config?.programAddress ?? SYSTEM_PROGRAM_ADDRESS;
- // Original accounts.
- const originalAccounts = {
- account: { value: input.account ?? null, isWritable: true },
- };
- const accounts = originalAccounts as Record<
- keyof typeof originalAccounts,
- ResolvedAccount
- >;
- // Original args.
- const args = { ...input };
- const getAccountMeta = getAccountMetaFactory(programAddress, 'omitted');
- const instruction = {
- accounts: [getAccountMeta(accounts.account)],
- programAddress,
- data: getAssignInstructionDataEncoder().encode(
- args as AssignInstructionDataArgs
- ),
- } as AssignInstruction<TProgramAddress, TAccountAccount>;
- return instruction;
- }
- export type ParsedAssignInstruction<
- TProgram extends string = typeof SYSTEM_PROGRAM_ADDRESS,
- TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[],
- > = {
- programAddress: Address<TProgram>;
- accounts: {
- account: TAccountMetas[0];
- };
- data: AssignInstructionData;
- };
- export function parseAssignInstruction<
- TProgram extends string,
- TAccountMetas extends readonly AccountMeta[],
- >(
- instruction: Instruction<TProgram> &
- InstructionWithAccounts<TAccountMetas> &
- InstructionWithData<ReadonlyUint8Array>
- ): ParsedAssignInstruction<TProgram, TAccountMetas> {
- if (instruction.accounts.length < 1) {
- // TODO: Coded error.
- throw new Error('Not enough accounts');
- }
- let accountIndex = 0;
- const getNextAccount = () => {
- const accountMeta = (instruction.accounts as TAccountMetas)[accountIndex]!;
- accountIndex += 1;
- return accountMeta;
- };
- return {
- programAddress: instruction.programAddress,
- accounts: {
- account: getNextAccount(),
- },
- data: getAssignInstructionDataDecoder().decode(instruction.data),
- };
- }
|