/** * 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, getBooleanDecoder, getBooleanEncoder, getBytesDecoder, getBytesEncoder, getStructDecoder, getStructEncoder, transformEncoder, type AccountMeta, type AccountSignerMeta, type Address, type FixedSizeCodec, type FixedSizeDecoder, type FixedSizeEncoder, type Instruction, type InstructionWithAccounts, type InstructionWithData, type ReadonlyAccount, type ReadonlyUint8Array, type TransactionSigner, type WritableAccount, type WritableSignerAccount, } from "@solana/kit"; import { PERPETUALS_PROGRAM_ADDRESS } from "../programs"; import { getAccountMetaFactory, type ResolvedAccount } from "../shared"; export const TEST_INIT_DISCRIMINATOR = new Uint8Array([ 48, 51, 92, 122, 81, 19, 112, 41, ]); export function getTestInitDiscriminatorBytes() { return fixEncoderSize(getBytesEncoder(), 8).encode(TEST_INIT_DISCRIMINATOR); } export type TestInitInstruction< TProgram extends string = typeof PERPETUALS_PROGRAM_ADDRESS, TAccountUpgradeAuthority extends string | AccountMeta = string, TAccountAdmin extends string | AccountMeta = string, TAccountTransferAuthority extends string | AccountMeta = string, TAccountPerpetuals extends string | AccountMeta = string, TAccountSystemProgram extends string | AccountMeta = "11111111111111111111111111111111", TAccountTokenProgram extends string | AccountMeta = "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA", TRemainingAccounts extends readonly AccountMeta[] = [], > = Instruction & InstructionWithData & InstructionWithAccounts< [ TAccountUpgradeAuthority extends string ? WritableSignerAccount & AccountSignerMeta : TAccountUpgradeAuthority, TAccountAdmin extends string ? ReadonlyAccount : TAccountAdmin, TAccountTransferAuthority extends string ? WritableAccount : TAccountTransferAuthority, TAccountPerpetuals extends string ? WritableAccount : TAccountPerpetuals, TAccountSystemProgram extends string ? ReadonlyAccount : TAccountSystemProgram, TAccountTokenProgram extends string ? ReadonlyAccount : TAccountTokenProgram, ...TRemainingAccounts, ] >; export type TestInitInstructionData = { discriminator: ReadonlyUint8Array; allowSwap: boolean; allowAddLiquidity: boolean; allowRemoveLiquidity: boolean; allowIncreasePosition: boolean; allowDecreasePosition: boolean; allowCollateralWithdrawal: boolean; allowLiquidatePosition: boolean; }; export type TestInitInstructionDataArgs = { allowSwap: boolean; allowAddLiquidity: boolean; allowRemoveLiquidity: boolean; allowIncreasePosition: boolean; allowDecreasePosition: boolean; allowCollateralWithdrawal: boolean; allowLiquidatePosition: boolean; }; export function getTestInitInstructionDataEncoder(): FixedSizeEncoder { return transformEncoder( getStructEncoder([ ["discriminator", fixEncoderSize(getBytesEncoder(), 8)], ["allowSwap", getBooleanEncoder()], ["allowAddLiquidity", getBooleanEncoder()], ["allowRemoveLiquidity", getBooleanEncoder()], ["allowIncreasePosition", getBooleanEncoder()], ["allowDecreasePosition", getBooleanEncoder()], ["allowCollateralWithdrawal", getBooleanEncoder()], ["allowLiquidatePosition", getBooleanEncoder()], ]), (value) => ({ ...value, discriminator: TEST_INIT_DISCRIMINATOR }), ); } export function getTestInitInstructionDataDecoder(): FixedSizeDecoder { return getStructDecoder([ ["discriminator", fixDecoderSize(getBytesDecoder(), 8)], ["allowSwap", getBooleanDecoder()], ["allowAddLiquidity", getBooleanDecoder()], ["allowRemoveLiquidity", getBooleanDecoder()], ["allowIncreasePosition", getBooleanDecoder()], ["allowDecreasePosition", getBooleanDecoder()], ["allowCollateralWithdrawal", getBooleanDecoder()], ["allowLiquidatePosition", getBooleanDecoder()], ]); } export function getTestInitInstructionDataCodec(): FixedSizeCodec< TestInitInstructionDataArgs, TestInitInstructionData > { return combineCodec( getTestInitInstructionDataEncoder(), getTestInitInstructionDataDecoder(), ); } export type TestInitInput< TAccountUpgradeAuthority extends string = string, TAccountAdmin extends string = string, TAccountTransferAuthority extends string = string, TAccountPerpetuals extends string = string, TAccountSystemProgram extends string = string, TAccountTokenProgram extends string = string, > = { upgradeAuthority: TransactionSigner; admin: Address; transferAuthority: Address; perpetuals: Address; systemProgram?: Address; tokenProgram?: Address; allowSwap: TestInitInstructionDataArgs["allowSwap"]; allowAddLiquidity: TestInitInstructionDataArgs["allowAddLiquidity"]; allowRemoveLiquidity: TestInitInstructionDataArgs["allowRemoveLiquidity"]; allowIncreasePosition: TestInitInstructionDataArgs["allowIncreasePosition"]; allowDecreasePosition: TestInitInstructionDataArgs["allowDecreasePosition"]; allowCollateralWithdrawal: TestInitInstructionDataArgs["allowCollateralWithdrawal"]; allowLiquidatePosition: TestInitInstructionDataArgs["allowLiquidatePosition"]; }; export function getTestInitInstruction< TAccountUpgradeAuthority extends string, TAccountAdmin extends string, TAccountTransferAuthority extends string, TAccountPerpetuals extends string, TAccountSystemProgram extends string, TAccountTokenProgram extends string, TProgramAddress extends Address = typeof PERPETUALS_PROGRAM_ADDRESS, >( input: TestInitInput< TAccountUpgradeAuthority, TAccountAdmin, TAccountTransferAuthority, TAccountPerpetuals, TAccountSystemProgram, TAccountTokenProgram >, config?: { programAddress?: TProgramAddress }, ): TestInitInstruction< TProgramAddress, TAccountUpgradeAuthority, TAccountAdmin, TAccountTransferAuthority, TAccountPerpetuals, TAccountSystemProgram, TAccountTokenProgram > { // Program address. const programAddress = config?.programAddress ?? PERPETUALS_PROGRAM_ADDRESS; // Original accounts. const originalAccounts = { upgradeAuthority: { value: input.upgradeAuthority ?? null, isWritable: true, }, admin: { value: input.admin ?? null, isWritable: false }, transferAuthority: { value: input.transferAuthority ?? null, isWritable: true, }, perpetuals: { value: input.perpetuals ?? null, isWritable: true }, systemProgram: { value: input.systemProgram ?? null, isWritable: false }, tokenProgram: { value: input.tokenProgram ?? null, isWritable: false }, }; const accounts = originalAccounts as Record< keyof typeof originalAccounts, ResolvedAccount >; // Original args. const args = { ...input }; // Resolve default values. if (!accounts.systemProgram.value) { accounts.systemProgram.value = "11111111111111111111111111111111" as Address<"11111111111111111111111111111111">; } if (!accounts.tokenProgram.value) { accounts.tokenProgram.value = "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA" as Address<"TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA">; } const getAccountMeta = getAccountMetaFactory(programAddress, "programId"); return Object.freeze({ accounts: [ getAccountMeta(accounts.upgradeAuthority), getAccountMeta(accounts.admin), getAccountMeta(accounts.transferAuthority), getAccountMeta(accounts.perpetuals), getAccountMeta(accounts.systemProgram), getAccountMeta(accounts.tokenProgram), ], data: getTestInitInstructionDataEncoder().encode( args as TestInitInstructionDataArgs, ), programAddress, } as TestInitInstruction< TProgramAddress, TAccountUpgradeAuthority, TAccountAdmin, TAccountTransferAuthority, TAccountPerpetuals, TAccountSystemProgram, TAccountTokenProgram >); } export type ParsedTestInitInstruction< TProgram extends string = typeof PERPETUALS_PROGRAM_ADDRESS, TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[], > = { programAddress: Address; accounts: { upgradeAuthority: TAccountMetas[0]; admin: TAccountMetas[1]; transferAuthority: TAccountMetas[2]; perpetuals: TAccountMetas[3]; systemProgram: TAccountMetas[4]; tokenProgram: TAccountMetas[5]; }; data: TestInitInstructionData; }; export function parseTestInitInstruction< TProgram extends string, TAccountMetas extends readonly AccountMeta[], >( instruction: Instruction & InstructionWithAccounts & InstructionWithData, ): ParsedTestInitInstruction { if (instruction.accounts.length < 6) { // 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: { upgradeAuthority: getNextAccount(), admin: getNextAccount(), transferAuthority: getNextAccount(), perpetuals: getNextAccount(), systemProgram: getNextAccount(), tokenProgram: getNextAccount(), }, data: getTestInitInstructionDataDecoder().decode(instruction.data), }; }