/** * 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, getU64Decoder, getU64Encoder, transformEncoder, type AccountMeta, type Address, type FixedSizeCodec, type FixedSizeDecoder, type FixedSizeEncoder, type Instruction, type InstructionWithAccounts, type InstructionWithData, type ReadonlyAccount, type ReadonlyUint8Array, } from "@solana/kit"; import { PERPETUALS_PROGRAM_ADDRESS } from "../programs"; import { getAccountMetaFactory, type ResolvedAccount } from "../shared"; export const GET_DECREASE_POSITION_DISCRIMINATOR = new Uint8Array([ 106, 31, 146, 195, 103, 108, 48, 250, ]); export function getGetDecreasePositionDiscriminatorBytes() { return fixEncoderSize(getBytesEncoder(), 8).encode( GET_DECREASE_POSITION_DISCRIMINATOR, ); } export type GetDecreasePositionInstruction< TProgram extends string = typeof PERPETUALS_PROGRAM_ADDRESS, TAccountPerpetuals extends string | AccountMeta = string, TAccountPool extends string | AccountMeta = string, TAccountPosition extends string | AccountMeta = string, TAccountCustody extends string | AccountMeta = string, TAccountCustodyOracleAccount extends string | AccountMeta = string, TAccountCollateralCustody extends string | AccountMeta = string, TAccountCollateralCustodyOracleAccount extends string | AccountMeta = string, TRemainingAccounts extends readonly AccountMeta[] = [], > = Instruction & InstructionWithData & InstructionWithAccounts< [ TAccountPerpetuals extends string ? ReadonlyAccount : TAccountPerpetuals, TAccountPool extends string ? ReadonlyAccount : TAccountPool, TAccountPosition extends string ? ReadonlyAccount : TAccountPosition, TAccountCustody extends string ? ReadonlyAccount : TAccountCustody, TAccountCustodyOracleAccount extends string ? ReadonlyAccount : TAccountCustodyOracleAccount, TAccountCollateralCustody extends string ? ReadonlyAccount : TAccountCollateralCustody, TAccountCollateralCustodyOracleAccount extends string ? ReadonlyAccount : TAccountCollateralCustodyOracleAccount, ...TRemainingAccounts, ] >; export type GetDecreasePositionInstructionData = { discriminator: ReadonlyUint8Array; collateralUsdDelta: bigint; sizeUsdDelta: bigint; }; export type GetDecreasePositionInstructionDataArgs = { collateralUsdDelta: number | bigint; sizeUsdDelta: number | bigint; }; export function getGetDecreasePositionInstructionDataEncoder(): FixedSizeEncoder { return transformEncoder( getStructEncoder([ ["discriminator", fixEncoderSize(getBytesEncoder(), 8)], ["collateralUsdDelta", getU64Encoder()], ["sizeUsdDelta", getU64Encoder()], ]), (value) => ({ ...value, discriminator: GET_DECREASE_POSITION_DISCRIMINATOR, }), ); } export function getGetDecreasePositionInstructionDataDecoder(): FixedSizeDecoder { return getStructDecoder([ ["discriminator", fixDecoderSize(getBytesDecoder(), 8)], ["collateralUsdDelta", getU64Decoder()], ["sizeUsdDelta", getU64Decoder()], ]); } export function getGetDecreasePositionInstructionDataCodec(): FixedSizeCodec< GetDecreasePositionInstructionDataArgs, GetDecreasePositionInstructionData > { return combineCodec( getGetDecreasePositionInstructionDataEncoder(), getGetDecreasePositionInstructionDataDecoder(), ); } export type GetDecreasePositionInput< TAccountPerpetuals extends string = string, TAccountPool extends string = string, TAccountPosition extends string = string, TAccountCustody extends string = string, TAccountCustodyOracleAccount extends string = string, TAccountCollateralCustody extends string = string, TAccountCollateralCustodyOracleAccount extends string = string, > = { perpetuals: Address; pool: Address; position: Address; custody: Address; custodyOracleAccount: Address; collateralCustody: Address; collateralCustodyOracleAccount: Address; collateralUsdDelta: GetDecreasePositionInstructionDataArgs["collateralUsdDelta"]; sizeUsdDelta: GetDecreasePositionInstructionDataArgs["sizeUsdDelta"]; }; export function getGetDecreasePositionInstruction< TAccountPerpetuals extends string, TAccountPool extends string, TAccountPosition extends string, TAccountCustody extends string, TAccountCustodyOracleAccount extends string, TAccountCollateralCustody extends string, TAccountCollateralCustodyOracleAccount extends string, TProgramAddress extends Address = typeof PERPETUALS_PROGRAM_ADDRESS, >( input: GetDecreasePositionInput< TAccountPerpetuals, TAccountPool, TAccountPosition, TAccountCustody, TAccountCustodyOracleAccount, TAccountCollateralCustody, TAccountCollateralCustodyOracleAccount >, config?: { programAddress?: TProgramAddress }, ): GetDecreasePositionInstruction< TProgramAddress, TAccountPerpetuals, TAccountPool, TAccountPosition, TAccountCustody, TAccountCustodyOracleAccount, TAccountCollateralCustody, TAccountCollateralCustodyOracleAccount > { // Program address. const programAddress = config?.programAddress ?? PERPETUALS_PROGRAM_ADDRESS; // Original accounts. const originalAccounts = { perpetuals: { value: input.perpetuals ?? null, isWritable: false }, pool: { value: input.pool ?? null, isWritable: false }, position: { value: input.position ?? null, isWritable: false }, custody: { value: input.custody ?? null, isWritable: false }, custodyOracleAccount: { value: input.custodyOracleAccount ?? null, isWritable: false, }, collateralCustody: { value: input.collateralCustody ?? null, isWritable: false, }, collateralCustodyOracleAccount: { value: input.collateralCustodyOracleAccount ?? null, isWritable: false, }, }; 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.perpetuals), getAccountMeta(accounts.pool), getAccountMeta(accounts.position), getAccountMeta(accounts.custody), getAccountMeta(accounts.custodyOracleAccount), getAccountMeta(accounts.collateralCustody), getAccountMeta(accounts.collateralCustodyOracleAccount), ], data: getGetDecreasePositionInstructionDataEncoder().encode( args as GetDecreasePositionInstructionDataArgs, ), programAddress, } as GetDecreasePositionInstruction< TProgramAddress, TAccountPerpetuals, TAccountPool, TAccountPosition, TAccountCustody, TAccountCustodyOracleAccount, TAccountCollateralCustody, TAccountCollateralCustodyOracleAccount >); } export type ParsedGetDecreasePositionInstruction< TProgram extends string = typeof PERPETUALS_PROGRAM_ADDRESS, TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[], > = { programAddress: Address; accounts: { perpetuals: TAccountMetas[0]; pool: TAccountMetas[1]; position: TAccountMetas[2]; custody: TAccountMetas[3]; custodyOracleAccount: TAccountMetas[4]; collateralCustody: TAccountMetas[5]; collateralCustodyOracleAccount: TAccountMetas[6]; }; data: GetDecreasePositionInstructionData; }; export function parseGetDecreasePositionInstruction< TProgram extends string, TAccountMetas extends readonly AccountMeta[], >( instruction: Instruction & InstructionWithAccounts & InstructionWithData, ): ParsedGetDecreasePositionInstruction { if (instruction.accounts.length < 7) { // 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: { perpetuals: getNextAccount(), pool: getNextAccount(), position: getNextAccount(), custody: getNextAccount(), custodyOracleAccount: getNextAccount(), collateralCustody: getNextAccount(), collateralCustodyOracleAccount: getNextAccount(), }, data: getGetDecreasePositionInstructionDataDecoder().decode( instruction.data, ), }; }