/** * 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, fixDecoderSize, fixEncoderSize, getBytesDecoder, getBytesEncoder, getStructDecoder, getStructEncoder, transformEncoder, type AccountMeta, type AccountSignerMeta, type Address, type FixedSizeCodec, type FixedSizeDecoder, type FixedSizeEncoder, type Instruction, type InstructionWithAccounts, type InstructionWithData, type ReadonlySignerAccount, type ReadonlyUint8Array, type TransactionSigner, type WritableAccount, } from "@solana/kit"; import { PERPETUALS_PROGRAM_ADDRESS } from "../programs"; import { getAccountMetaFactory, type ResolvedAccount } from "../shared"; import { getPermissionsDecoder, getPermissionsEncoder, type Permissions, type PermissionsArgs, } from "../types"; export const SET_PERPETUALS_CONFIG_DISCRIMINATOR = new Uint8Array([ 80, 72, 21, 191, 29, 121, 45, 111, ]); export function getSetPerpetualsConfigDiscriminatorBytes() { return fixEncoderSize(getBytesEncoder(), 8).encode( SET_PERPETUALS_CONFIG_DISCRIMINATOR, ); } export type SetPerpetualsConfigInstruction< TProgram extends string = typeof PERPETUALS_PROGRAM_ADDRESS, TAccountAdmin extends string | AccountMeta = string, TAccountPerpetuals extends string | AccountMeta = string, TRemainingAccounts extends readonly AccountMeta[] = [], > = Instruction & InstructionWithData & InstructionWithAccounts< [ TAccountAdmin extends string ? ReadonlySignerAccount & AccountSignerMeta : TAccountAdmin, TAccountPerpetuals extends string ? WritableAccount : TAccountPerpetuals, ...TRemainingAccounts, ] >; export type SetPerpetualsConfigInstructionData = { discriminator: ReadonlyUint8Array; permissions: Permissions; }; export type SetPerpetualsConfigInstructionDataArgs = { permissions: PermissionsArgs; }; export function getSetPerpetualsConfigInstructionDataEncoder(): FixedSizeEncoder { return transformEncoder( getStructEncoder([ ["discriminator", fixEncoderSize(getBytesEncoder(), 8)], ["permissions", getPermissionsEncoder()], ]), (value) => ({ ...value, discriminator: SET_PERPETUALS_CONFIG_DISCRIMINATOR, }), ); } export function getSetPerpetualsConfigInstructionDataDecoder(): FixedSizeDecoder { return getStructDecoder([ ["discriminator", fixDecoderSize(getBytesDecoder(), 8)], ["permissions", getPermissionsDecoder()], ]); } export function getSetPerpetualsConfigInstructionDataCodec(): FixedSizeCodec< SetPerpetualsConfigInstructionDataArgs, SetPerpetualsConfigInstructionData > { return combineCodec( getSetPerpetualsConfigInstructionDataEncoder(), getSetPerpetualsConfigInstructionDataDecoder(), ); } export type SetPerpetualsConfigInput< TAccountAdmin extends string = string, TAccountPerpetuals extends string = string, > = { admin: TransactionSigner; perpetuals: Address; permissions: SetPerpetualsConfigInstructionDataArgs["permissions"]; }; export function getSetPerpetualsConfigInstruction< TAccountAdmin extends string, TAccountPerpetuals extends string, TProgramAddress extends Address = typeof PERPETUALS_PROGRAM_ADDRESS, >( input: SetPerpetualsConfigInput, config?: { programAddress?: TProgramAddress }, ): SetPerpetualsConfigInstruction< TProgramAddress, TAccountAdmin, TAccountPerpetuals > { // Program address. const programAddress = config?.programAddress ?? PERPETUALS_PROGRAM_ADDRESS; // Original accounts. const originalAccounts = { admin: { value: input.admin ?? null, isWritable: false }, perpetuals: { value: input.perpetuals ?? null, isWritable: true }, }; const accounts = originalAccounts as Record< keyof typeof originalAccounts, ResolvedAccount >; // Original args. const args = { ...input }; const getAccountMeta = getAccountMetaFactory(programAddress, "programId"); return Object.freeze({ accounts: [ getAccountMeta(accounts.admin), getAccountMeta(accounts.perpetuals), ], data: getSetPerpetualsConfigInstructionDataEncoder().encode( args as SetPerpetualsConfigInstructionDataArgs, ), programAddress, } as SetPerpetualsConfigInstruction< TProgramAddress, TAccountAdmin, TAccountPerpetuals >); } export type ParsedSetPerpetualsConfigInstruction< TProgram extends string = typeof PERPETUALS_PROGRAM_ADDRESS, TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[], > = { programAddress: Address; accounts: { admin: TAccountMetas[0]; perpetuals: TAccountMetas[1]; }; data: SetPerpetualsConfigInstructionData; }; export function parseSetPerpetualsConfigInstruction< TProgram extends string, TAccountMetas extends readonly AccountMeta[], >( instruction: Instruction & InstructionWithAccounts & InstructionWithData, ): ParsedSetPerpetualsConfigInstruction { if (instruction.accounts.length < 2) { // 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: { admin: getNextAccount(), perpetuals: getNextAccount() }, data: getSetPerpetualsConfigInstructionDataDecoder().decode( instruction.data, ), }; }