/** * 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, getAddressDecoder, getAddressEncoder, getOptionDecoder, getOptionEncoder, getStructDecoder, getStructEncoder, getU8Decoder, getU8Encoder, SOLANA_ERROR__PROGRAM_CLIENTS__INSUFFICIENT_ACCOUNT_METAS, SolanaError, 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 ReadonlySignerAccount, type ReadonlyUint8Array, type TransactionSigner, type WritableAccount, } from "@solana/kit"; import { getAccountMetaFactory, type ResolvedInstructionAccount, } from "@solana/program-client-core"; import { SPL_STAKE_POOL_PROGRAM_ADDRESS } from "../programs/index.ts"; import { getPreferredValidatorTypeDecoder, getPreferredValidatorTypeEncoder, type PreferredValidatorType, type PreferredValidatorTypeArgs, } from "../types/index.ts"; export const SET_PREFERRED_VALIDATOR_DISCRIMINATOR = 5; export function getSetPreferredValidatorDiscriminatorBytes(): ReadonlyUint8Array { return getU8Encoder().encode(SET_PREFERRED_VALIDATOR_DISCRIMINATOR); } export type SetPreferredValidatorInstruction< TProgram extends string = typeof SPL_STAKE_POOL_PROGRAM_ADDRESS, TAccountStakePool extends string | AccountMeta = string, TAccountStaker extends string | AccountMeta = string, TAccountValidatorList extends string | AccountMeta = string, TRemainingAccounts extends readonly AccountMeta[] = [], > = Instruction & InstructionWithData & InstructionWithAccounts< [ TAccountStakePool extends string ? WritableAccount : TAccountStakePool, TAccountStaker extends string ? ReadonlySignerAccount & AccountSignerMeta : TAccountStaker, TAccountValidatorList extends string ? ReadonlyAccount : TAccountValidatorList, ...TRemainingAccounts, ] >; export type SetPreferredValidatorInstructionData = { discriminator: number; validatorType: PreferredValidatorType; validatorVoteAddress: Option
; }; export type SetPreferredValidatorInstructionDataArgs = { validatorType: PreferredValidatorTypeArgs; validatorVoteAddress: OptionOrNullable
; }; export function getSetPreferredValidatorInstructionDataEncoder(): Encoder { return transformEncoder( getStructEncoder([ ["discriminator", getU8Encoder()], ["validatorType", getPreferredValidatorTypeEncoder()], ["validatorVoteAddress", getOptionEncoder(getAddressEncoder())], ]), (value) => ({ ...value, discriminator: SET_PREFERRED_VALIDATOR_DISCRIMINATOR, }), ); } export function getSetPreferredValidatorInstructionDataDecoder(): Decoder { return getStructDecoder([ ["discriminator", getU8Decoder()], ["validatorType", getPreferredValidatorTypeDecoder()], ["validatorVoteAddress", getOptionDecoder(getAddressDecoder())], ]); } export function getSetPreferredValidatorInstructionDataCodec(): Codec< SetPreferredValidatorInstructionDataArgs, SetPreferredValidatorInstructionData > { return combineCodec( getSetPreferredValidatorInstructionDataEncoder(), getSetPreferredValidatorInstructionDataDecoder(), ); } export type SetPreferredValidatorInput< TAccountStakePool extends string = string, TAccountStaker extends string = string, TAccountValidatorList extends string = string, > = { stakePool: Address; staker: TransactionSigner; validatorList: Address; validatorType: SetPreferredValidatorInstructionDataArgs["validatorType"]; validatorVoteAddress: SetPreferredValidatorInstructionDataArgs["validatorVoteAddress"]; }; /** * (Staker only) Set the preferred deposit or withdraw stake account for * the stake pool * In order to avoid users abusing the stake pool as a free conversion * between SOL staked on different validators, the staker can force all * deposits and/or withdraws to go to one chosen account, or unset that * account. * 0. `[w]` Stake pool * 1. `[s]` Stake pool staker * 2. `[]` Validator list * Fails if the validator is not part of the stake pool. */ export function getSetPreferredValidatorInstruction< TAccountStakePool extends string, TAccountStaker extends string, TAccountValidatorList extends string, TProgramAddress extends Address = typeof SPL_STAKE_POOL_PROGRAM_ADDRESS, >( input: SetPreferredValidatorInput< TAccountStakePool, TAccountStaker, TAccountValidatorList >, config?: { programAddress?: TProgramAddress }, ): SetPreferredValidatorInstruction< TProgramAddress, TAccountStakePool, TAccountStaker, TAccountValidatorList > { // Program address. const programAddress = config?.programAddress ?? SPL_STAKE_POOL_PROGRAM_ADDRESS; // Original accounts. const originalAccounts = { stakePool: { value: input.stakePool ?? null, isWritable: true }, staker: { value: input.staker ?? null, isWritable: false }, validatorList: { value: input.validatorList ?? null, isWritable: false }, }; const accounts = originalAccounts as Record< keyof typeof originalAccounts, ResolvedInstructionAccount >; // Original args. const args = { ...input }; const getAccountMeta = getAccountMetaFactory(programAddress, "programId"); return Object.freeze({ accounts: [ getAccountMeta("stakePool", accounts.stakePool), getAccountMeta("staker", accounts.staker), getAccountMeta("validatorList", accounts.validatorList), ], data: getSetPreferredValidatorInstructionDataEncoder().encode( args as SetPreferredValidatorInstructionDataArgs, ), programAddress, } as SetPreferredValidatorInstruction< TProgramAddress, TAccountStakePool, TAccountStaker, TAccountValidatorList >); } export type ParsedSetPreferredValidatorInstruction< TProgram extends string = typeof SPL_STAKE_POOL_PROGRAM_ADDRESS, TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[], > = { programAddress: Address; accounts: { stakePool: TAccountMetas[0]; staker: TAccountMetas[1]; validatorList: TAccountMetas[2]; }; data: SetPreferredValidatorInstructionData; }; export function parseSetPreferredValidatorInstruction< TProgram extends string, TAccountMetas extends readonly AccountMeta[], >( instruction: Instruction & InstructionWithAccounts & InstructionWithData, ): ParsedSetPreferredValidatorInstruction { if (instruction.accounts.length < 3) { throw new SolanaError( SOLANA_ERROR__PROGRAM_CLIENTS__INSUFFICIENT_ACCOUNT_METAS, { actualAccountMetas: instruction.accounts.length, expectedAccountMetas: 3, }, ); } let accountIndex = 0; const getNextAccount = () => { const accountMeta = (instruction.accounts as TAccountMetas)[accountIndex]!; accountIndex += 1; return accountMeta; }; return { programAddress: instruction.programAddress, accounts: { stakePool: getNextAccount(), staker: getNextAccount(), validatorList: getNextAccount(), }, data: getSetPreferredValidatorInstructionDataDecoder().decode( instruction.data, ), }; }