/** * 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, getStructDecoder, getStructEncoder, getU8Decoder, getU8Encoder, SOLANA_ERROR__PROGRAM_CLIENTS__INSUFFICIENT_ACCOUNT_METAS, SolanaError, transformEncoder, type AccountMeta, type AccountSignerMeta, type Address, type Codec, type Decoder, type Encoder, type Instruction, type InstructionWithAccounts, type InstructionWithData, type ReadonlyAccount, type ReadonlyUint8Array, type TransactionSigner, type WritableAccount, type WritableSignerAccount, } from "@solana/kit"; import { getAccountMetaFactory, getAddressFromResolvedInstructionAccount, type ResolvedInstructionAccount, } from "@solana/program-client-core"; import { findRuleSetBufferPda } from "../pdas/index.ts"; import { MPL_TOKEN_AUTH_RULES_PROGRAM_ADDRESS } from "../programs/index.ts"; import { getWriteToBufferArgsDecoder, getWriteToBufferArgsEncoder, type WriteToBufferArgs, type WriteToBufferArgsArgs, } from "../types/index.ts"; export const WRITE_TO_BUFFER_DISCRIMINATOR = 2; export function getWriteToBufferDiscriminatorBytes(): ReadonlyUint8Array { return getU8Encoder().encode(WRITE_TO_BUFFER_DISCRIMINATOR); } export type WriteToBufferInstruction< TProgram extends string = typeof MPL_TOKEN_AUTH_RULES_PROGRAM_ADDRESS, TAccountPayer extends string | AccountMeta = string, TAccountBufferPda extends string | AccountMeta = string, TAccountSystemProgram extends string | AccountMeta = "11111111111111111111111111111111", TRemainingAccounts extends readonly AccountMeta[] = [], > = Instruction & InstructionWithData & InstructionWithAccounts< [ TAccountPayer extends string ? WritableSignerAccount & AccountSignerMeta : TAccountPayer, TAccountBufferPda extends string ? WritableAccount : TAccountBufferPda, TAccountSystemProgram extends string ? ReadonlyAccount : TAccountSystemProgram, ...TRemainingAccounts, ] >; export type WriteToBufferInstructionData = { discriminator: number; /** The chunk of serialized RuleSet data to append. */ writeToBufferArgs: WriteToBufferArgs; }; export type WriteToBufferInstructionDataArgs = { /** The chunk of serialized RuleSet data to append. */ writeToBufferArgs: WriteToBufferArgsArgs; }; export function getWriteToBufferInstructionDataEncoder(): Encoder { return transformEncoder( getStructEncoder([ ["discriminator", getU8Encoder()], ["writeToBufferArgs", getWriteToBufferArgsEncoder()], ]), (value) => ({ ...value, discriminator: WRITE_TO_BUFFER_DISCRIMINATOR }), ); } export function getWriteToBufferInstructionDataDecoder(): Decoder { return getStructDecoder([ ["discriminator", getU8Decoder()], ["writeToBufferArgs", getWriteToBufferArgsDecoder()], ]); } export function getWriteToBufferInstructionDataCodec(): Codec< WriteToBufferInstructionDataArgs, WriteToBufferInstructionData > { return combineCodec( getWriteToBufferInstructionDataEncoder(), getWriteToBufferInstructionDataDecoder(), ); } export type WriteToBufferAsyncInput< TAccountPayer extends string = string, TAccountBufferPda extends string = string, TAccountSystemProgram extends string = string, > = { /** Payer and creator of the RuleSet */ payer: TransactionSigner; /** The PDA account where the RuleSet buffer is stored */ bufferPda?: Address; /** System program */ systemProgram?: Address; writeToBufferArgs: WriteToBufferInstructionDataArgs["writeToBufferArgs"]; }; /** * Appends serialized RuleSet bytes to a buffer PDA. * * RuleSets larger than a single transaction can hold are streamed in * chunks to a buffer account, then applied in one `createOrUpdate` * call that reads from the buffer. */ export async function getWriteToBufferInstructionAsync< TAccountPayer extends string, TAccountBufferPda extends string, TAccountSystemProgram extends string, TProgramAddress extends Address = typeof MPL_TOKEN_AUTH_RULES_PROGRAM_ADDRESS, >( input: WriteToBufferAsyncInput< TAccountPayer, TAccountBufferPda, TAccountSystemProgram >, config?: { programAddress?: TProgramAddress }, ): Promise< WriteToBufferInstruction< TProgramAddress, TAccountPayer, TAccountBufferPda, TAccountSystemProgram > > { // Program address. const programAddress = config?.programAddress ?? MPL_TOKEN_AUTH_RULES_PROGRAM_ADDRESS; // Original accounts. const originalAccounts = { payer: { value: input.payer ?? null, isWritable: true }, bufferPda: { value: input.bufferPda ?? null, isWritable: true }, systemProgram: { value: input.systemProgram ?? null, isWritable: false }, }; const accounts = originalAccounts as Record< keyof typeof originalAccounts, ResolvedInstructionAccount >; // Original args. const args = { ...input }; // Resolve default values. if (!accounts.bufferPda.value) { accounts.bufferPda.value = await findRuleSetBufferPda( { owner: getAddressFromResolvedInstructionAccount( "payer", accounts.payer.value, ), }, { programAddress }, ); } if (!accounts.systemProgram.value) { accounts.systemProgram.value = "11111111111111111111111111111111" as Address<"11111111111111111111111111111111">; } const getAccountMeta = getAccountMetaFactory(programAddress, "programId"); return Object.freeze({ accounts: [ getAccountMeta("payer", accounts.payer), getAccountMeta("bufferPda", accounts.bufferPda), getAccountMeta("systemProgram", accounts.systemProgram), ], data: getWriteToBufferInstructionDataEncoder().encode( args as WriteToBufferInstructionDataArgs, ), programAddress, } as WriteToBufferInstruction< TProgramAddress, TAccountPayer, TAccountBufferPda, TAccountSystemProgram >); } export type WriteToBufferInput< TAccountPayer extends string = string, TAccountBufferPda extends string = string, TAccountSystemProgram extends string = string, > = { /** Payer and creator of the RuleSet */ payer: TransactionSigner; /** The PDA account where the RuleSet buffer is stored */ bufferPda: Address; /** System program */ systemProgram?: Address; writeToBufferArgs: WriteToBufferInstructionDataArgs["writeToBufferArgs"]; }; /** * Appends serialized RuleSet bytes to a buffer PDA. * * RuleSets larger than a single transaction can hold are streamed in * chunks to a buffer account, then applied in one `createOrUpdate` * call that reads from the buffer. */ export function getWriteToBufferInstruction< TAccountPayer extends string, TAccountBufferPda extends string, TAccountSystemProgram extends string, TProgramAddress extends Address = typeof MPL_TOKEN_AUTH_RULES_PROGRAM_ADDRESS, >( input: WriteToBufferInput< TAccountPayer, TAccountBufferPda, TAccountSystemProgram >, config?: { programAddress?: TProgramAddress }, ): WriteToBufferInstruction< TProgramAddress, TAccountPayer, TAccountBufferPda, TAccountSystemProgram > { // Program address. const programAddress = config?.programAddress ?? MPL_TOKEN_AUTH_RULES_PROGRAM_ADDRESS; // Original accounts. const originalAccounts = { payer: { value: input.payer ?? null, isWritable: true }, bufferPda: { value: input.bufferPda ?? null, isWritable: true }, systemProgram: { value: input.systemProgram ?? null, isWritable: false }, }; const accounts = originalAccounts as Record< keyof typeof originalAccounts, ResolvedInstructionAccount >; // Original args. const args = { ...input }; // Resolve default values. if (!accounts.systemProgram.value) { accounts.systemProgram.value = "11111111111111111111111111111111" as Address<"11111111111111111111111111111111">; } const getAccountMeta = getAccountMetaFactory(programAddress, "programId"); return Object.freeze({ accounts: [ getAccountMeta("payer", accounts.payer), getAccountMeta("bufferPda", accounts.bufferPda), getAccountMeta("systemProgram", accounts.systemProgram), ], data: getWriteToBufferInstructionDataEncoder().encode( args as WriteToBufferInstructionDataArgs, ), programAddress, } as WriteToBufferInstruction< TProgramAddress, TAccountPayer, TAccountBufferPda, TAccountSystemProgram >); } export type ParsedWriteToBufferInstruction< TProgram extends string = typeof MPL_TOKEN_AUTH_RULES_PROGRAM_ADDRESS, TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[], > = { programAddress: Address; accounts: { /** Payer and creator of the RuleSet */ payer: TAccountMetas[0]; /** The PDA account where the RuleSet buffer is stored */ bufferPda: TAccountMetas[1]; /** System program */ systemProgram: TAccountMetas[2]; }; data: WriteToBufferInstructionData; }; export function parseWriteToBufferInstruction< TProgram extends string, TAccountMetas extends readonly AccountMeta[], >( instruction: Instruction & InstructionWithAccounts & InstructionWithData, ): ParsedWriteToBufferInstruction { if (instruction.accounts.length < 3) { throw new SolanaError( SOLANA_ERROR__PROGRAM_CLIENTS__INSUFFICIENT_ACCOUNT_METAS, { actualAccountMetas: instruction.accounts.length, expectedAccountMetas: 3, }, ); } let accountIndex = 0; const getNextAccount = () => { const accountMeta = (instruction.accounts as TAccountMetas)[accountIndex]!; accountIndex += 1; return accountMeta; }; return { programAddress: instruction.programAddress, accounts: { payer: getNextAccount(), bufferPda: getNextAccount(), systemProgram: getNextAccount(), }, data: getWriteToBufferInstructionDataDecoder().decode(instruction.data), }; }