/** * 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 { getCreateOrUpdateArgsDecoder, getCreateOrUpdateArgsEncoder, type CreateOrUpdateArgs, type CreateOrUpdateArgsArgs, } from "../types/index.ts"; export const CREATE_OR_UPDATE_DISCRIMINATOR = 0; export function getCreateOrUpdateDiscriminatorBytes(): ReadonlyUint8Array { return getU8Encoder().encode(CREATE_OR_UPDATE_DISCRIMINATOR); } export type CreateOrUpdateInstruction< TProgram extends string = typeof MPL_TOKEN_AUTH_RULES_PROGRAM_ADDRESS, TAccountPayer extends string | AccountMeta = string, TAccountRuleSetPda extends string | AccountMeta = string, TAccountSystemProgram extends string | AccountMeta = "11111111111111111111111111111111", TAccountBufferPda extends string | AccountMeta = string, TRemainingAccounts extends readonly AccountMeta[] = [], > = Instruction & InstructionWithData & InstructionWithAccounts< [ TAccountPayer extends string ? WritableSignerAccount & AccountSignerMeta : TAccountPayer, TAccountRuleSetPda extends string ? WritableAccount : TAccountRuleSetPda, TAccountSystemProgram extends string ? ReadonlyAccount : TAccountSystemProgram, TAccountBufferPda extends string ? ReadonlyAccount : TAccountBufferPda, ...TRemainingAccounts, ] >; export type CreateOrUpdateInstructionData = { discriminator: number; /** The (versioned) CBOR-serialized RuleSet to store. */ createOrUpdateArgs: CreateOrUpdateArgs; }; export type CreateOrUpdateInstructionDataArgs = { /** The (versioned) CBOR-serialized RuleSet to store. */ createOrUpdateArgs: CreateOrUpdateArgsArgs; }; export function getCreateOrUpdateInstructionDataEncoder(): Encoder { return transformEncoder( getStructEncoder([ ["discriminator", getU8Encoder()], ["createOrUpdateArgs", getCreateOrUpdateArgsEncoder()], ]), (value) => ({ ...value, discriminator: CREATE_OR_UPDATE_DISCRIMINATOR }), ); } export function getCreateOrUpdateInstructionDataDecoder(): Decoder { return getStructDecoder([ ["discriminator", getU8Decoder()], ["createOrUpdateArgs", getCreateOrUpdateArgsDecoder()], ]); } export function getCreateOrUpdateInstructionDataCodec(): Codec< CreateOrUpdateInstructionDataArgs, CreateOrUpdateInstructionData > { return combineCodec( getCreateOrUpdateInstructionDataEncoder(), getCreateOrUpdateInstructionDataDecoder(), ); } export type CreateOrUpdateAsyncInput< TAccountPayer extends string = string, TAccountRuleSetPda extends string = string, TAccountSystemProgram extends string = string, TAccountBufferPda extends string = string, > = { /** Payer and creator of the RuleSet */ payer: TransactionSigner; /** The PDA account where the RuleSet is stored */ ruleSetPda: Address; /** System program */ systemProgram?: Address; /** The buffer to copy a complete ruleset from */ bufferPda?: Address; createOrUpdateArgs: CreateOrUpdateInstructionDataArgs["createOrUpdateArgs"]; }; /** * Creates or updates a RuleSet stored in a program-derived account. * * A RuleSet is a named, versioned collection of authorization rules * owned by its creator. Each call appends a new revision to the PDA; * existing revisions are preserved so previously-signed operations * remain reproducible. Pass a serialized RuleSet built with the * auth-rules Rust/JS SDK. */ export async function getCreateOrUpdateInstructionAsync< TAccountPayer extends string, TAccountRuleSetPda extends string, TAccountSystemProgram extends string, TAccountBufferPda extends string, TProgramAddress extends Address = typeof MPL_TOKEN_AUTH_RULES_PROGRAM_ADDRESS, >( input: CreateOrUpdateAsyncInput< TAccountPayer, TAccountRuleSetPda, TAccountSystemProgram, TAccountBufferPda >, config?: { programAddress?: TProgramAddress }, ): Promise< CreateOrUpdateInstruction< TProgramAddress, TAccountPayer, TAccountRuleSetPda, TAccountSystemProgram, TAccountBufferPda > > { // Program address. const programAddress = config?.programAddress ?? MPL_TOKEN_AUTH_RULES_PROGRAM_ADDRESS; // Original accounts. const originalAccounts = { payer: { value: input.payer ?? null, isWritable: true }, ruleSetPda: { value: input.ruleSetPda ?? null, isWritable: true }, systemProgram: { value: input.systemProgram ?? null, isWritable: false }, bufferPda: { value: input.bufferPda ?? 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">; } if (!accounts.bufferPda.value) { accounts.bufferPda.value = await findRuleSetBufferPda( { owner: getAddressFromResolvedInstructionAccount( "payer", accounts.payer.value, ), }, { programAddress }, ); } const getAccountMeta = getAccountMetaFactory(programAddress, "programId"); return Object.freeze({ accounts: [ getAccountMeta("payer", accounts.payer), getAccountMeta("ruleSetPda", accounts.ruleSetPda), getAccountMeta("systemProgram", accounts.systemProgram), getAccountMeta("bufferPda", accounts.bufferPda), ], data: getCreateOrUpdateInstructionDataEncoder().encode( args as CreateOrUpdateInstructionDataArgs, ), programAddress, } as CreateOrUpdateInstruction< TProgramAddress, TAccountPayer, TAccountRuleSetPda, TAccountSystemProgram, TAccountBufferPda >); } export type CreateOrUpdateInput< TAccountPayer extends string = string, TAccountRuleSetPda extends string = string, TAccountSystemProgram extends string = string, TAccountBufferPda extends string = string, > = { /** Payer and creator of the RuleSet */ payer: TransactionSigner; /** The PDA account where the RuleSet is stored */ ruleSetPda: Address; /** System program */ systemProgram?: Address; /** The buffer to copy a complete ruleset from */ bufferPda?: Address; createOrUpdateArgs: CreateOrUpdateInstructionDataArgs["createOrUpdateArgs"]; }; /** * Creates or updates a RuleSet stored in a program-derived account. * * A RuleSet is a named, versioned collection of authorization rules * owned by its creator. Each call appends a new revision to the PDA; * existing revisions are preserved so previously-signed operations * remain reproducible. Pass a serialized RuleSet built with the * auth-rules Rust/JS SDK. */ export function getCreateOrUpdateInstruction< TAccountPayer extends string, TAccountRuleSetPda extends string, TAccountSystemProgram extends string, TAccountBufferPda extends string, TProgramAddress extends Address = typeof MPL_TOKEN_AUTH_RULES_PROGRAM_ADDRESS, >( input: CreateOrUpdateInput< TAccountPayer, TAccountRuleSetPda, TAccountSystemProgram, TAccountBufferPda >, config?: { programAddress?: TProgramAddress }, ): CreateOrUpdateInstruction< TProgramAddress, TAccountPayer, TAccountRuleSetPda, TAccountSystemProgram, TAccountBufferPda > { // Program address. const programAddress = config?.programAddress ?? MPL_TOKEN_AUTH_RULES_PROGRAM_ADDRESS; // Original accounts. const originalAccounts = { payer: { value: input.payer ?? null, isWritable: true }, ruleSetPda: { value: input.ruleSetPda ?? null, isWritable: true }, systemProgram: { value: input.systemProgram ?? null, isWritable: false }, bufferPda: { value: input.bufferPda ?? 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("ruleSetPda", accounts.ruleSetPda), getAccountMeta("systemProgram", accounts.systemProgram), getAccountMeta("bufferPda", accounts.bufferPda), ], data: getCreateOrUpdateInstructionDataEncoder().encode( args as CreateOrUpdateInstructionDataArgs, ), programAddress, } as CreateOrUpdateInstruction< TProgramAddress, TAccountPayer, TAccountRuleSetPda, TAccountSystemProgram, TAccountBufferPda >); } export type ParsedCreateOrUpdateInstruction< 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 is stored */ ruleSetPda: TAccountMetas[1]; /** System program */ systemProgram: TAccountMetas[2]; /** The buffer to copy a complete ruleset from */ bufferPda?: TAccountMetas[3] | undefined; }; data: CreateOrUpdateInstructionData; }; export function parseCreateOrUpdateInstruction< TProgram extends string, TAccountMetas extends readonly AccountMeta[], >( instruction: Instruction & InstructionWithAccounts & InstructionWithData, ): ParsedCreateOrUpdateInstruction { if (instruction.accounts.length < 4) { throw new SolanaError( SOLANA_ERROR__PROGRAM_CLIENTS__INSUFFICIENT_ACCOUNT_METAS, { actualAccountMetas: instruction.accounts.length, expectedAccountMetas: 4, }, ); } let accountIndex = 0; const getNextAccount = () => { const accountMeta = (instruction.accounts as TAccountMetas)[accountIndex]!; accountIndex += 1; return accountMeta; }; const getNextOptionalAccount = () => { const accountMeta = getNextAccount(); return accountMeta.address === MPL_TOKEN_AUTH_RULES_PROGRAM_ADDRESS ? undefined : accountMeta; }; return { programAddress: instruction.programAddress, accounts: { payer: getNextAccount(), ruleSetPda: getNextAccount(), systemProgram: getNextAccount(), bufferPda: getNextOptionalAccount(), }, data: getCreateOrUpdateInstructionDataDecoder().decode(instruction.data), }; }