UNPKG

@kamino-finance/farms-sdk

Version:
293 lines (275 loc) 9.21 kB
/** * 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 ReadonlySignerAccount, type ReadonlyUint8Array, type TransactionSigner, type WritableAccount, } from "@solana/kit"; import { FARMS_PROGRAM_ADDRESS } from "../programs"; import { getAccountMetaFactory, type ResolvedAccount } from "../shared"; export const STAKE_DISCRIMINATOR = new Uint8Array([ 206, 176, 202, 18, 200, 209, 179, 108, ]); export function getStakeDiscriminatorBytes() { return fixEncoderSize(getBytesEncoder(), 8).encode(STAKE_DISCRIMINATOR); } export type StakeInstruction< TProgram extends string = typeof FARMS_PROGRAM_ADDRESS, TAccountOwner extends string | AccountMeta<string> = string, TAccountUserState extends string | AccountMeta<string> = string, TAccountFarmState extends string | AccountMeta<string> = string, TAccountFarmVault extends string | AccountMeta<string> = string, TAccountUserAta extends string | AccountMeta<string> = string, TAccountTokenMint extends string | AccountMeta<string> = string, TAccountScopePrices extends string | AccountMeta<string> = string, TAccountTokenProgram extends string | AccountMeta<string> = "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA", TRemainingAccounts extends readonly AccountMeta<string>[] = [], > = Instruction<TProgram> & InstructionWithData<ReadonlyUint8Array> & InstructionWithAccounts< [ TAccountOwner extends string ? ReadonlySignerAccount<TAccountOwner> & AccountSignerMeta<TAccountOwner> : TAccountOwner, TAccountUserState extends string ? WritableAccount<TAccountUserState> : TAccountUserState, TAccountFarmState extends string ? WritableAccount<TAccountFarmState> : TAccountFarmState, TAccountFarmVault extends string ? WritableAccount<TAccountFarmVault> : TAccountFarmVault, TAccountUserAta extends string ? WritableAccount<TAccountUserAta> : TAccountUserAta, TAccountTokenMint extends string ? ReadonlyAccount<TAccountTokenMint> : TAccountTokenMint, TAccountScopePrices extends string ? ReadonlyAccount<TAccountScopePrices> : TAccountScopePrices, TAccountTokenProgram extends string ? ReadonlyAccount<TAccountTokenProgram> : TAccountTokenProgram, ...TRemainingAccounts, ] >; export type StakeInstructionData = { discriminator: ReadonlyUint8Array; amount: bigint; }; export type StakeInstructionDataArgs = { amount: number | bigint }; export function getStakeInstructionDataEncoder(): FixedSizeEncoder<StakeInstructionDataArgs> { return transformEncoder( getStructEncoder([ ["discriminator", fixEncoderSize(getBytesEncoder(), 8)], ["amount", getU64Encoder()], ]), (value) => ({ ...value, discriminator: STAKE_DISCRIMINATOR }), ); } export function getStakeInstructionDataDecoder(): FixedSizeDecoder<StakeInstructionData> { return getStructDecoder([ ["discriminator", fixDecoderSize(getBytesDecoder(), 8)], ["amount", getU64Decoder()], ]); } export function getStakeInstructionDataCodec(): FixedSizeCodec< StakeInstructionDataArgs, StakeInstructionData > { return combineCodec( getStakeInstructionDataEncoder(), getStakeInstructionDataDecoder(), ); } export type StakeInput< TAccountOwner extends string = string, TAccountUserState extends string = string, TAccountFarmState extends string = string, TAccountFarmVault extends string = string, TAccountUserAta extends string = string, TAccountTokenMint extends string = string, TAccountScopePrices extends string = string, TAccountTokenProgram extends string = string, > = { owner: TransactionSigner<TAccountOwner>; userState: Address<TAccountUserState>; farmState: Address<TAccountFarmState>; farmVault: Address<TAccountFarmVault>; userAta: Address<TAccountUserAta>; tokenMint: Address<TAccountTokenMint>; scopePrices?: Address<TAccountScopePrices>; tokenProgram?: Address<TAccountTokenProgram>; amount: StakeInstructionDataArgs["amount"]; }; export function getStakeInstruction< TAccountOwner extends string, TAccountUserState extends string, TAccountFarmState extends string, TAccountFarmVault extends string, TAccountUserAta extends string, TAccountTokenMint extends string, TAccountScopePrices extends string, TAccountTokenProgram extends string, TProgramAddress extends Address = typeof FARMS_PROGRAM_ADDRESS, >( input: StakeInput< TAccountOwner, TAccountUserState, TAccountFarmState, TAccountFarmVault, TAccountUserAta, TAccountTokenMint, TAccountScopePrices, TAccountTokenProgram >, config?: { programAddress?: TProgramAddress }, ): StakeInstruction< TProgramAddress, TAccountOwner, TAccountUserState, TAccountFarmState, TAccountFarmVault, TAccountUserAta, TAccountTokenMint, TAccountScopePrices, TAccountTokenProgram > { // Program address. const programAddress = config?.programAddress ?? FARMS_PROGRAM_ADDRESS; // Original accounts. const originalAccounts = { owner: { value: input.owner ?? null, isWritable: false }, userState: { value: input.userState ?? null, isWritable: true }, farmState: { value: input.farmState ?? null, isWritable: true }, farmVault: { value: input.farmVault ?? null, isWritable: true }, userAta: { value: input.userAta ?? null, isWritable: true }, tokenMint: { value: input.tokenMint ?? null, isWritable: false }, scopePrices: { value: input.scopePrices ?? 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.tokenProgram.value) { accounts.tokenProgram.value = "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA" as Address<"TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA">; } const getAccountMeta = getAccountMetaFactory(programAddress, "programId"); return Object.freeze({ accounts: [ getAccountMeta(accounts.owner), getAccountMeta(accounts.userState), getAccountMeta(accounts.farmState), getAccountMeta(accounts.farmVault), getAccountMeta(accounts.userAta), getAccountMeta(accounts.tokenMint), getAccountMeta(accounts.scopePrices), getAccountMeta(accounts.tokenProgram), ], data: getStakeInstructionDataEncoder().encode( args as StakeInstructionDataArgs, ), programAddress, } as StakeInstruction< TProgramAddress, TAccountOwner, TAccountUserState, TAccountFarmState, TAccountFarmVault, TAccountUserAta, TAccountTokenMint, TAccountScopePrices, TAccountTokenProgram >); } export type ParsedStakeInstruction< TProgram extends string = typeof FARMS_PROGRAM_ADDRESS, TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[], > = { programAddress: Address<TProgram>; accounts: { owner: TAccountMetas[0]; userState: TAccountMetas[1]; farmState: TAccountMetas[2]; farmVault: TAccountMetas[3]; userAta: TAccountMetas[4]; tokenMint: TAccountMetas[5]; scopePrices?: TAccountMetas[6] | undefined; tokenProgram: TAccountMetas[7]; }; data: StakeInstructionData; }; export function parseStakeInstruction< TProgram extends string, TAccountMetas extends readonly AccountMeta[], >( instruction: Instruction<TProgram> & InstructionWithAccounts<TAccountMetas> & InstructionWithData<ReadonlyUint8Array>, ): ParsedStakeInstruction<TProgram, TAccountMetas> { if (instruction.accounts.length < 8) { // 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 === FARMS_PROGRAM_ADDRESS ? undefined : accountMeta; }; return { programAddress: instruction.programAddress, accounts: { owner: getNextAccount(), userState: getNextAccount(), farmState: getNextAccount(), farmVault: getNextAccount(), userAta: getNextAccount(), tokenMint: getNextAccount(), scopePrices: getNextOptionalAccount(), tokenProgram: getNextAccount(), }, data: getStakeInstructionDataDecoder().decode(instruction.data), }; }