/** * 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, getOptionDecoder, getOptionEncoder, getStructDecoder, getStructEncoder, getU64Decoder, getU64Encoder, transformEncoder, type AccountMeta, type AccountSignerMeta, type Address, type Codec, type Decoder, type Encoder, type Instruction, type InstructionWithAccounts, type InstructionWithData, type Option, type OptionOrNullable, 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"; import { getRequestTypeDecoder, getRequestTypeEncoder, type RequestType, type RequestTypeArgs, } from "../types"; export const CREATE_DECREASE_POSITION_REQUEST_DISCRIMINATOR = new Uint8Array([ 146, 21, 51, 121, 187, 208, 7, 69, ]); export function getCreateDecreasePositionRequestDiscriminatorBytes() { return fixEncoderSize(getBytesEncoder(), 8).encode( CREATE_DECREASE_POSITION_REQUEST_DISCRIMINATOR, ); } export type CreateDecreasePositionRequestInstruction< TProgram extends string = typeof PERPETUALS_PROGRAM_ADDRESS, TAccountOwner extends string | AccountMeta = string, TAccountReceivingAccount extends string | AccountMeta = string, TAccountPerpetuals extends string | AccountMeta = string, TAccountPool extends string | AccountMeta = string, TAccountPosition extends string | AccountMeta = string, TAccountPositionRequest extends string | AccountMeta = string, TAccountPositionRequestAta extends string | AccountMeta = string, TAccountCustody extends string | AccountMeta = string, TAccountCustodyOracleAccount extends string | AccountMeta = string, TAccountCollateralCustody extends string | AccountMeta = string, TAccountDesiredMint extends string | AccountMeta = string, TAccountReferral extends string | AccountMeta = string, TAccountTokenProgram extends string | AccountMeta = "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA", TAccountAssociatedTokenProgram extends string | AccountMeta = string, TAccountSystemProgram extends string | AccountMeta = "11111111111111111111111111111111", TAccountEventAuthority extends string | AccountMeta = string, TAccountProgram extends string | AccountMeta = string, TRemainingAccounts extends readonly AccountMeta[] = [], > = Instruction & InstructionWithData & InstructionWithAccounts< [ TAccountOwner extends string ? WritableSignerAccount & AccountSignerMeta : TAccountOwner, TAccountReceivingAccount extends string ? WritableAccount : TAccountReceivingAccount, TAccountPerpetuals extends string ? ReadonlyAccount : TAccountPerpetuals, TAccountPool extends string ? WritableAccount : TAccountPool, TAccountPosition extends string ? ReadonlyAccount : TAccountPosition, TAccountPositionRequest extends string ? WritableAccount : TAccountPositionRequest, TAccountPositionRequestAta extends string ? WritableAccount : TAccountPositionRequestAta, TAccountCustody extends string ? ReadonlyAccount : TAccountCustody, TAccountCustodyOracleAccount extends string ? ReadonlyAccount : TAccountCustodyOracleAccount, TAccountCollateralCustody extends string ? ReadonlyAccount : TAccountCollateralCustody, TAccountDesiredMint extends string ? ReadonlyAccount : TAccountDesiredMint, TAccountReferral extends string ? ReadonlyAccount : TAccountReferral, TAccountTokenProgram extends string ? ReadonlyAccount : TAccountTokenProgram, TAccountAssociatedTokenProgram extends string ? ReadonlyAccount : TAccountAssociatedTokenProgram, TAccountSystemProgram extends string ? ReadonlyAccount : TAccountSystemProgram, TAccountEventAuthority extends string ? ReadonlyAccount : TAccountEventAuthority, TAccountProgram extends string ? ReadonlyAccount : TAccountProgram, ...TRemainingAccounts, ] >; export type CreateDecreasePositionRequestInstructionData = { discriminator: ReadonlyUint8Array; collateralUsdDelta: bigint; sizeUsdDelta: bigint; requestType: RequestType; priceSlippage: Option; jupiterMinimumOut: Option; triggerPrice: Option; triggerAboveThreshold: Option; entirePosition: Option; counter: bigint; }; export type CreateDecreasePositionRequestInstructionDataArgs = { collateralUsdDelta: number | bigint; sizeUsdDelta: number | bigint; requestType: RequestTypeArgs; priceSlippage: OptionOrNullable; jupiterMinimumOut: OptionOrNullable; triggerPrice: OptionOrNullable; triggerAboveThreshold: OptionOrNullable; entirePosition: OptionOrNullable; counter: number | bigint; }; export function getCreateDecreasePositionRequestInstructionDataEncoder(): Encoder { return transformEncoder( getStructEncoder([ ["discriminator", fixEncoderSize(getBytesEncoder(), 8)], ["collateralUsdDelta", getU64Encoder()], ["sizeUsdDelta", getU64Encoder()], ["requestType", getRequestTypeEncoder()], ["priceSlippage", getOptionEncoder(getU64Encoder())], ["jupiterMinimumOut", getOptionEncoder(getU64Encoder())], ["triggerPrice", getOptionEncoder(getU64Encoder())], ["triggerAboveThreshold", getOptionEncoder(getBooleanEncoder())], ["entirePosition", getOptionEncoder(getBooleanEncoder())], ["counter", getU64Encoder()], ]), (value) => ({ ...value, discriminator: CREATE_DECREASE_POSITION_REQUEST_DISCRIMINATOR, }), ); } export function getCreateDecreasePositionRequestInstructionDataDecoder(): Decoder { return getStructDecoder([ ["discriminator", fixDecoderSize(getBytesDecoder(), 8)], ["collateralUsdDelta", getU64Decoder()], ["sizeUsdDelta", getU64Decoder()], ["requestType", getRequestTypeDecoder()], ["priceSlippage", getOptionDecoder(getU64Decoder())], ["jupiterMinimumOut", getOptionDecoder(getU64Decoder())], ["triggerPrice", getOptionDecoder(getU64Decoder())], ["triggerAboveThreshold", getOptionDecoder(getBooleanDecoder())], ["entirePosition", getOptionDecoder(getBooleanDecoder())], ["counter", getU64Decoder()], ]); } export function getCreateDecreasePositionRequestInstructionDataCodec(): Codec< CreateDecreasePositionRequestInstructionDataArgs, CreateDecreasePositionRequestInstructionData > { return combineCodec( getCreateDecreasePositionRequestInstructionDataEncoder(), getCreateDecreasePositionRequestInstructionDataDecoder(), ); } export type CreateDecreasePositionRequestInput< TAccountOwner extends string = string, TAccountReceivingAccount extends string = string, TAccountPerpetuals extends string = string, TAccountPool extends string = string, TAccountPosition extends string = string, TAccountPositionRequest extends string = string, TAccountPositionRequestAta extends string = string, TAccountCustody extends string = string, TAccountCustodyOracleAccount extends string = string, TAccountCollateralCustody extends string = string, TAccountDesiredMint extends string = string, TAccountReferral extends string = string, TAccountTokenProgram extends string = string, TAccountAssociatedTokenProgram extends string = string, TAccountSystemProgram extends string = string, TAccountEventAuthority extends string = string, TAccountProgram extends string = string, > = { owner: TransactionSigner; receivingAccount: Address; perpetuals: Address; pool: Address; position: Address; positionRequest: Address; positionRequestAta: Address; custody: Address; custodyOracleAccount: Address; collateralCustody: Address; desiredMint: Address; referral?: Address; tokenProgram?: Address; associatedTokenProgram: Address; systemProgram?: Address; eventAuthority: Address; program: Address; collateralUsdDelta: CreateDecreasePositionRequestInstructionDataArgs["collateralUsdDelta"]; sizeUsdDelta: CreateDecreasePositionRequestInstructionDataArgs["sizeUsdDelta"]; requestType: CreateDecreasePositionRequestInstructionDataArgs["requestType"]; priceSlippage: CreateDecreasePositionRequestInstructionDataArgs["priceSlippage"]; jupiterMinimumOut: CreateDecreasePositionRequestInstructionDataArgs["jupiterMinimumOut"]; triggerPrice: CreateDecreasePositionRequestInstructionDataArgs["triggerPrice"]; triggerAboveThreshold: CreateDecreasePositionRequestInstructionDataArgs["triggerAboveThreshold"]; entirePosition: CreateDecreasePositionRequestInstructionDataArgs["entirePosition"]; counter: CreateDecreasePositionRequestInstructionDataArgs["counter"]; }; export function getCreateDecreasePositionRequestInstruction< TAccountOwner extends string, TAccountReceivingAccount extends string, TAccountPerpetuals extends string, TAccountPool extends string, TAccountPosition extends string, TAccountPositionRequest extends string, TAccountPositionRequestAta extends string, TAccountCustody extends string, TAccountCustodyOracleAccount extends string, TAccountCollateralCustody extends string, TAccountDesiredMint extends string, TAccountReferral extends string, TAccountTokenProgram extends string, TAccountAssociatedTokenProgram extends string, TAccountSystemProgram extends string, TAccountEventAuthority extends string, TAccountProgram extends string, TProgramAddress extends Address = typeof PERPETUALS_PROGRAM_ADDRESS, >( input: CreateDecreasePositionRequestInput< TAccountOwner, TAccountReceivingAccount, TAccountPerpetuals, TAccountPool, TAccountPosition, TAccountPositionRequest, TAccountPositionRequestAta, TAccountCustody, TAccountCustodyOracleAccount, TAccountCollateralCustody, TAccountDesiredMint, TAccountReferral, TAccountTokenProgram, TAccountAssociatedTokenProgram, TAccountSystemProgram, TAccountEventAuthority, TAccountProgram >, config?: { programAddress?: TProgramAddress }, ): CreateDecreasePositionRequestInstruction< TProgramAddress, TAccountOwner, TAccountReceivingAccount, TAccountPerpetuals, TAccountPool, TAccountPosition, TAccountPositionRequest, TAccountPositionRequestAta, TAccountCustody, TAccountCustodyOracleAccount, TAccountCollateralCustody, TAccountDesiredMint, TAccountReferral, TAccountTokenProgram, TAccountAssociatedTokenProgram, TAccountSystemProgram, TAccountEventAuthority, TAccountProgram > { // Program address. const programAddress = config?.programAddress ?? PERPETUALS_PROGRAM_ADDRESS; // Original accounts. const originalAccounts = { owner: { value: input.owner ?? null, isWritable: true }, receivingAccount: { value: input.receivingAccount ?? null, isWritable: true, }, perpetuals: { value: input.perpetuals ?? null, isWritable: false }, pool: { value: input.pool ?? null, isWritable: true }, position: { value: input.position ?? null, isWritable: false }, positionRequest: { value: input.positionRequest ?? null, isWritable: true }, positionRequestAta: { value: input.positionRequestAta ?? null, isWritable: true, }, custody: { value: input.custody ?? null, isWritable: false }, custodyOracleAccount: { value: input.custodyOracleAccount ?? null, isWritable: false, }, collateralCustody: { value: input.collateralCustody ?? null, isWritable: false, }, desiredMint: { value: input.desiredMint ?? null, isWritable: false }, referral: { value: input.referral ?? null, isWritable: false }, tokenProgram: { value: input.tokenProgram ?? null, isWritable: false }, associatedTokenProgram: { value: input.associatedTokenProgram ?? null, isWritable: false, }, systemProgram: { value: input.systemProgram ?? null, isWritable: false }, eventAuthority: { value: input.eventAuthority ?? null, isWritable: false }, program: { value: input.program ?? null, isWritable: false }, }; const accounts = originalAccounts as Record< keyof typeof originalAccounts, ResolvedAccount >; // Original args. const args = { ...input }; // Resolve default values. if (!accounts.tokenProgram.value) { accounts.tokenProgram.value = "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA" as Address<"TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA">; } if (!accounts.systemProgram.value) { accounts.systemProgram.value = "11111111111111111111111111111111" as Address<"11111111111111111111111111111111">; } const getAccountMeta = getAccountMetaFactory(programAddress, "programId"); return Object.freeze({ accounts: [ getAccountMeta(accounts.owner), getAccountMeta(accounts.receivingAccount), getAccountMeta(accounts.perpetuals), getAccountMeta(accounts.pool), getAccountMeta(accounts.position), getAccountMeta(accounts.positionRequest), getAccountMeta(accounts.positionRequestAta), getAccountMeta(accounts.custody), getAccountMeta(accounts.custodyOracleAccount), getAccountMeta(accounts.collateralCustody), getAccountMeta(accounts.desiredMint), getAccountMeta(accounts.referral), getAccountMeta(accounts.tokenProgram), getAccountMeta(accounts.associatedTokenProgram), getAccountMeta(accounts.systemProgram), getAccountMeta(accounts.eventAuthority), getAccountMeta(accounts.program), ], data: getCreateDecreasePositionRequestInstructionDataEncoder().encode( args as CreateDecreasePositionRequestInstructionDataArgs, ), programAddress, } as CreateDecreasePositionRequestInstruction< TProgramAddress, TAccountOwner, TAccountReceivingAccount, TAccountPerpetuals, TAccountPool, TAccountPosition, TAccountPositionRequest, TAccountPositionRequestAta, TAccountCustody, TAccountCustodyOracleAccount, TAccountCollateralCustody, TAccountDesiredMint, TAccountReferral, TAccountTokenProgram, TAccountAssociatedTokenProgram, TAccountSystemProgram, TAccountEventAuthority, TAccountProgram >); } export type ParsedCreateDecreasePositionRequestInstruction< TProgram extends string = typeof PERPETUALS_PROGRAM_ADDRESS, TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[], > = { programAddress: Address; accounts: { owner: TAccountMetas[0]; receivingAccount: TAccountMetas[1]; perpetuals: TAccountMetas[2]; pool: TAccountMetas[3]; position: TAccountMetas[4]; positionRequest: TAccountMetas[5]; positionRequestAta: TAccountMetas[6]; custody: TAccountMetas[7]; custodyOracleAccount: TAccountMetas[8]; collateralCustody: TAccountMetas[9]; desiredMint: TAccountMetas[10]; referral?: TAccountMetas[11] | undefined; tokenProgram: TAccountMetas[12]; associatedTokenProgram: TAccountMetas[13]; systemProgram: TAccountMetas[14]; eventAuthority: TAccountMetas[15]; program: TAccountMetas[16]; }; data: CreateDecreasePositionRequestInstructionData; }; export function parseCreateDecreasePositionRequestInstruction< TProgram extends string, TAccountMetas extends readonly AccountMeta[], >( instruction: Instruction & InstructionWithAccounts & InstructionWithData, ): ParsedCreateDecreasePositionRequestInstruction { if (instruction.accounts.length < 17) { // 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; }; const getNextOptionalAccount = () => { const accountMeta = getNextAccount(); return accountMeta.address === PERPETUALS_PROGRAM_ADDRESS ? undefined : accountMeta; }; return { programAddress: instruction.programAddress, accounts: { owner: getNextAccount(), receivingAccount: getNextAccount(), perpetuals: getNextAccount(), pool: getNextAccount(), position: getNextAccount(), positionRequest: getNextAccount(), positionRequestAta: getNextAccount(), custody: getNextAccount(), custodyOracleAccount: getNextAccount(), collateralCustody: getNextAccount(), desiredMint: getNextAccount(), referral: getNextOptionalAccount(), tokenProgram: getNextAccount(), associatedTokenProgram: getNextAccount(), systemProgram: getNextAccount(), eventAuthority: getNextAccount(), program: getNextAccount(), }, data: getCreateDecreasePositionRequestInstructionDataDecoder().decode( instruction.data, ), }; }