/** * 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 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 UPDATE_DECREASE_POSITION_REQUEST_DISCRIMINATOR = new Uint8Array([ 69, 44, 72, 173, 62, 20, 177, 146, ]); export function getUpdateDecreasePositionRequestDiscriminatorBytes() { return fixEncoderSize(getBytesEncoder(), 8).encode( UPDATE_DECREASE_POSITION_REQUEST_DISCRIMINATOR, ); } export type UpdateDecreasePositionRequestInstruction< TProgram extends string = typeof PERPETUALS_PROGRAM_ADDRESS, TAccountOwner extends string | AccountMeta = string, TAccountPerpetuals extends string | AccountMeta = string, TAccountPool extends string | AccountMeta = string, TAccountPosition extends string | AccountMeta = string, TAccountPositionRequest extends string | AccountMeta = string, TAccountCustody extends string | AccountMeta = string, TAccountCustodyOracleAccount extends string | AccountMeta = string, TRemainingAccounts extends readonly AccountMeta[] = [], > = Instruction & InstructionWithData & InstructionWithAccounts< [ TAccountOwner extends string ? WritableSignerAccount & AccountSignerMeta : TAccountOwner, TAccountPerpetuals extends string ? ReadonlyAccount : TAccountPerpetuals, TAccountPool extends string ? ReadonlyAccount : TAccountPool, TAccountPosition extends string ? ReadonlyAccount : TAccountPosition, TAccountPositionRequest extends string ? WritableAccount : TAccountPositionRequest, TAccountCustody extends string ? ReadonlyAccount : TAccountCustody, TAccountCustodyOracleAccount extends string ? ReadonlyAccount : TAccountCustodyOracleAccount, ...TRemainingAccounts, ] >; export type UpdateDecreasePositionRequestInstructionData = { discriminator: ReadonlyUint8Array; sizeUsdDelta: bigint; triggerPrice: bigint; }; export type UpdateDecreasePositionRequestInstructionDataArgs = { sizeUsdDelta: number | bigint; triggerPrice: number | bigint; }; export function getUpdateDecreasePositionRequestInstructionDataEncoder(): FixedSizeEncoder { return transformEncoder( getStructEncoder([ ["discriminator", fixEncoderSize(getBytesEncoder(), 8)], ["sizeUsdDelta", getU64Encoder()], ["triggerPrice", getU64Encoder()], ]), (value) => ({ ...value, discriminator: UPDATE_DECREASE_POSITION_REQUEST_DISCRIMINATOR, }), ); } export function getUpdateDecreasePositionRequestInstructionDataDecoder(): FixedSizeDecoder { return getStructDecoder([ ["discriminator", fixDecoderSize(getBytesDecoder(), 8)], ["sizeUsdDelta", getU64Decoder()], ["triggerPrice", getU64Decoder()], ]); } export function getUpdateDecreasePositionRequestInstructionDataCodec(): FixedSizeCodec< UpdateDecreasePositionRequestInstructionDataArgs, UpdateDecreasePositionRequestInstructionData > { return combineCodec( getUpdateDecreasePositionRequestInstructionDataEncoder(), getUpdateDecreasePositionRequestInstructionDataDecoder(), ); } export type UpdateDecreasePositionRequestInput< TAccountOwner extends string = string, TAccountPerpetuals extends string = string, TAccountPool extends string = string, TAccountPosition extends string = string, TAccountPositionRequest extends string = string, TAccountCustody extends string = string, TAccountCustodyOracleAccount extends string = string, > = { owner: TransactionSigner; perpetuals: Address; pool: Address; position: Address; positionRequest: Address; custody: Address; custodyOracleAccount: Address; sizeUsdDelta: UpdateDecreasePositionRequestInstructionDataArgs["sizeUsdDelta"]; triggerPrice: UpdateDecreasePositionRequestInstructionDataArgs["triggerPrice"]; }; export function getUpdateDecreasePositionRequestInstruction< TAccountOwner extends string, TAccountPerpetuals extends string, TAccountPool extends string, TAccountPosition extends string, TAccountPositionRequest extends string, TAccountCustody extends string, TAccountCustodyOracleAccount extends string, TProgramAddress extends Address = typeof PERPETUALS_PROGRAM_ADDRESS, >( input: UpdateDecreasePositionRequestInput< TAccountOwner, TAccountPerpetuals, TAccountPool, TAccountPosition, TAccountPositionRequest, TAccountCustody, TAccountCustodyOracleAccount >, config?: { programAddress?: TProgramAddress }, ): UpdateDecreasePositionRequestInstruction< TProgramAddress, TAccountOwner, TAccountPerpetuals, TAccountPool, TAccountPosition, TAccountPositionRequest, TAccountCustody, TAccountCustodyOracleAccount > { // Program address. const programAddress = config?.programAddress ?? PERPETUALS_PROGRAM_ADDRESS; // Original accounts. const originalAccounts = { owner: { value: input.owner ?? null, isWritable: true }, perpetuals: { value: input.perpetuals ?? null, isWritable: false }, pool: { value: input.pool ?? null, isWritable: false }, position: { value: input.position ?? null, isWritable: false }, positionRequest: { value: input.positionRequest ?? null, isWritable: true }, custody: { value: input.custody ?? null, isWritable: false }, custodyOracleAccount: { value: input.custodyOracleAccount ?? 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.owner), getAccountMeta(accounts.perpetuals), getAccountMeta(accounts.pool), getAccountMeta(accounts.position), getAccountMeta(accounts.positionRequest), getAccountMeta(accounts.custody), getAccountMeta(accounts.custodyOracleAccount), ], data: getUpdateDecreasePositionRequestInstructionDataEncoder().encode( args as UpdateDecreasePositionRequestInstructionDataArgs, ), programAddress, } as UpdateDecreasePositionRequestInstruction< TProgramAddress, TAccountOwner, TAccountPerpetuals, TAccountPool, TAccountPosition, TAccountPositionRequest, TAccountCustody, TAccountCustodyOracleAccount >); } export type ParsedUpdateDecreasePositionRequestInstruction< TProgram extends string = typeof PERPETUALS_PROGRAM_ADDRESS, TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[], > = { programAddress: Address; accounts: { owner: TAccountMetas[0]; perpetuals: TAccountMetas[1]; pool: TAccountMetas[2]; position: TAccountMetas[3]; positionRequest: TAccountMetas[4]; custody: TAccountMetas[5]; custodyOracleAccount: TAccountMetas[6]; }; data: UpdateDecreasePositionRequestInstructionData; }; export function parseUpdateDecreasePositionRequestInstruction< TProgram extends string, TAccountMetas extends readonly AccountMeta[], >( instruction: Instruction & InstructionWithAccounts & InstructionWithData, ): ParsedUpdateDecreasePositionRequestInstruction { 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: { owner: getNextAccount(), perpetuals: getNextAccount(), pool: getNextAccount(), position: getNextAccount(), positionRequest: getNextAccount(), custody: getNextAccount(), custodyOracleAccount: getNextAccount(), }, data: getUpdateDecreasePositionRequestInstructionDataDecoder().decode( instruction.data, ), }; }