/** * 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 ReadonlySignerAccount, type ReadonlyUint8Array, type TransactionSigner, type WritableAccount, type WritableSignerAccount, } from "@solana/kit"; import { getAccountMetaFactory, type ResolvedInstructionAccount, } from "@solana/program-client-core"; import { MPL_TOKEN_AUTH_RULES_PROGRAM_ADDRESS } from "../programs/index.ts"; import { getValidateArgsDecoder, getValidateArgsEncoder, type ValidateArgs, type ValidateArgsArgs, } from "../types/index.ts"; export const VALIDATE_DISCRIMINATOR = 1; export function getValidateDiscriminatorBytes(): ReadonlyUint8Array { return getU8Encoder().encode(VALIDATE_DISCRIMINATOR); } export type ValidateInstruction< TProgram extends string = typeof MPL_TOKEN_AUTH_RULES_PROGRAM_ADDRESS, TAccountRuleSetPda extends string | AccountMeta = string, TAccountMint extends string | AccountMeta = string, TAccountSystemProgram extends string | AccountMeta = "11111111111111111111111111111111", TAccountPayer extends string | AccountMeta = string, TAccountRuleAuthority extends string | AccountMeta = string, TAccountRuleSetStatePda extends string | AccountMeta = string, TRemainingAccounts extends readonly AccountMeta[] = [], > = Instruction & InstructionWithData & InstructionWithAccounts< [ TAccountRuleSetPda extends string ? ReadonlyAccount : TAccountRuleSetPda, TAccountMint extends string ? ReadonlyAccount : TAccountMint, TAccountSystemProgram extends string ? ReadonlyAccount : TAccountSystemProgram, TAccountPayer extends string ? WritableSignerAccount & AccountSignerMeta : TAccountPayer, TAccountRuleAuthority extends string ? ReadonlySignerAccount & AccountSignerMeta : TAccountRuleAuthority, TAccountRuleSetStatePda extends string ? WritableAccount : TAccountRuleSetStatePda, ...TRemainingAccounts, ] >; export type ValidateInstructionData = { discriminator: number; /** The operation to validate together with its rule payload. */ validateArgs: ValidateArgs; }; export type ValidateInstructionDataArgs = { /** The operation to validate together with its rule payload. */ validateArgs: ValidateArgsArgs; }; export function getValidateInstructionDataEncoder(): Encoder { return transformEncoder( getStructEncoder([ ["discriminator", getU8Encoder()], ["validateArgs", getValidateArgsEncoder()], ]), (value) => ({ ...value, discriminator: VALIDATE_DISCRIMINATOR }), ); } export function getValidateInstructionDataDecoder(): Decoder { return getStructDecoder([ ["discriminator", getU8Decoder()], ["validateArgs", getValidateArgsDecoder()], ]); } export function getValidateInstructionDataCodec(): Codec< ValidateInstructionDataArgs, ValidateInstructionData > { return combineCodec( getValidateInstructionDataEncoder(), getValidateInstructionDataDecoder(), ); } export type ValidateInput< TAccountRuleSetPda extends string = string, TAccountMint extends string = string, TAccountSystemProgram extends string = string, TAccountPayer extends string = string, TAccountRuleAuthority extends string = string, TAccountRuleSetStatePda extends string = string, > = { /** The PDA account where the RuleSet is stored */ ruleSetPda: Address; /** Mint of token asset */ mint: Address; /** System program */ systemProgram?: Address; /** Payer for RuleSet state PDA account */ payer?: TransactionSigner; /** Signing authority for any Rule state updates */ ruleAuthority?: TransactionSigner; /** The PDA account where any RuleSet state is stored */ ruleSetStatePda?: Address; validateArgs: ValidateInstructionDataArgs["validateArgs"]; }; /** * Validates an operation against a stored RuleSet. * * Normally invoked via CPI by another program (e.g. Token Metadata) * to decide whether a token operation is permitted. Returns an error * if any rule for the operation fails. When `updateRuleState` is set, * stateful rules such as Frequency are advanced and persisted to the * rule-set state PDA. */ export function getValidateInstruction< TAccountRuleSetPda extends string, TAccountMint extends string, TAccountSystemProgram extends string, TAccountPayer extends string, TAccountRuleAuthority extends string, TAccountRuleSetStatePda extends string, TProgramAddress extends Address = typeof MPL_TOKEN_AUTH_RULES_PROGRAM_ADDRESS, >( input: ValidateInput< TAccountRuleSetPda, TAccountMint, TAccountSystemProgram, TAccountPayer, TAccountRuleAuthority, TAccountRuleSetStatePda >, config?: { programAddress?: TProgramAddress }, ): ValidateInstruction< TProgramAddress, TAccountRuleSetPda, TAccountMint, TAccountSystemProgram, TAccountPayer, TAccountRuleAuthority, TAccountRuleSetStatePda > { // Program address. const programAddress = config?.programAddress ?? MPL_TOKEN_AUTH_RULES_PROGRAM_ADDRESS; // Original accounts. const originalAccounts = { ruleSetPda: { value: input.ruleSetPda ?? null, isWritable: false }, mint: { value: input.mint ?? null, isWritable: false }, systemProgram: { value: input.systemProgram ?? null, isWritable: false }, payer: { value: input.payer ?? null, isWritable: true }, ruleAuthority: { value: input.ruleAuthority ?? null, isWritable: false }, ruleSetStatePda: { value: input.ruleSetStatePda ?? null, isWritable: true }, }; 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("ruleSetPda", accounts.ruleSetPda), getAccountMeta("mint", accounts.mint), getAccountMeta("systemProgram", accounts.systemProgram), getAccountMeta("payer", accounts.payer), getAccountMeta("ruleAuthority", accounts.ruleAuthority), getAccountMeta("ruleSetStatePda", accounts.ruleSetStatePda), ], data: getValidateInstructionDataEncoder().encode( args as ValidateInstructionDataArgs, ), programAddress, } as ValidateInstruction< TProgramAddress, TAccountRuleSetPda, TAccountMint, TAccountSystemProgram, TAccountPayer, TAccountRuleAuthority, TAccountRuleSetStatePda >); } export type ParsedValidateInstruction< TProgram extends string = typeof MPL_TOKEN_AUTH_RULES_PROGRAM_ADDRESS, TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[], > = { programAddress: Address; accounts: { /** The PDA account where the RuleSet is stored */ ruleSetPda: TAccountMetas[0]; /** Mint of token asset */ mint: TAccountMetas[1]; /** System program */ systemProgram: TAccountMetas[2]; /** Payer for RuleSet state PDA account */ payer?: TAccountMetas[3] | undefined; /** Signing authority for any Rule state updates */ ruleAuthority?: TAccountMetas[4] | undefined; /** The PDA account where any RuleSet state is stored */ ruleSetStatePda?: TAccountMetas[5] | undefined; }; data: ValidateInstructionData; }; export function parseValidateInstruction< TProgram extends string, TAccountMetas extends readonly AccountMeta[], >( instruction: Instruction & InstructionWithAccounts & InstructionWithData, ): ParsedValidateInstruction { if (instruction.accounts.length < 6) { throw new SolanaError( SOLANA_ERROR__PROGRAM_CLIENTS__INSUFFICIENT_ACCOUNT_METAS, { actualAccountMetas: instruction.accounts.length, expectedAccountMetas: 6, }, ); } 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: { ruleSetPda: getNextAccount(), mint: getNextAccount(), systemProgram: getNextAccount(), payer: getNextOptionalAccount(), ruleAuthority: getNextOptionalAccount(), ruleSetStatePda: getNextOptionalAccount(), }, data: getValidateInstructionDataDecoder().decode(instruction.data), }; }