@kamino-finance/farms-sdk
Version:
230 lines (213 loc) • 6.86 kB
text/typescript
/**
* 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,
getU128Decoder,
getU128Encoder,
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 { FARMS_PROGRAM_ADDRESS } from "../programs";
import { getAccountMetaFactory, type ResolvedAccount } from "../shared";
export const UNSTAKE_DISCRIMINATOR = new Uint8Array([
90, 95, 107, 42, 205, 124, 50, 225,
]);
export function getUnstakeDiscriminatorBytes() {
return fixEncoderSize(getBytesEncoder(), 8).encode(UNSTAKE_DISCRIMINATOR);
}
export type UnstakeInstruction<
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,
TAccountScopePrices extends string | AccountMeta<string> = string,
TRemainingAccounts extends readonly AccountMeta<string>[] = [],
> = Instruction<TProgram> &
InstructionWithData<ReadonlyUint8Array> &
InstructionWithAccounts<
[
TAccountOwner extends string
? WritableSignerAccount<TAccountOwner> &
AccountSignerMeta<TAccountOwner>
: TAccountOwner,
TAccountUserState extends string
? WritableAccount<TAccountUserState>
: TAccountUserState,
TAccountFarmState extends string
? WritableAccount<TAccountFarmState>
: TAccountFarmState,
TAccountScopePrices extends string
? ReadonlyAccount<TAccountScopePrices>
: TAccountScopePrices,
...TRemainingAccounts,
]
>;
export type UnstakeInstructionData = {
discriminator: ReadonlyUint8Array;
stakeSharesScaled: bigint;
};
export type UnstakeInstructionDataArgs = { stakeSharesScaled: number | bigint };
export function getUnstakeInstructionDataEncoder(): FixedSizeEncoder<UnstakeInstructionDataArgs> {
return transformEncoder(
getStructEncoder([
["discriminator", fixEncoderSize(getBytesEncoder(), 8)],
["stakeSharesScaled", getU128Encoder()],
]),
(value) => ({ ...value, discriminator: UNSTAKE_DISCRIMINATOR }),
);
}
export function getUnstakeInstructionDataDecoder(): FixedSizeDecoder<UnstakeInstructionData> {
return getStructDecoder([
["discriminator", fixDecoderSize(getBytesDecoder(), 8)],
["stakeSharesScaled", getU128Decoder()],
]);
}
export function getUnstakeInstructionDataCodec(): FixedSizeCodec<
UnstakeInstructionDataArgs,
UnstakeInstructionData
> {
return combineCodec(
getUnstakeInstructionDataEncoder(),
getUnstakeInstructionDataDecoder(),
);
}
export type UnstakeInput<
TAccountOwner extends string = string,
TAccountUserState extends string = string,
TAccountFarmState extends string = string,
TAccountScopePrices extends string = string,
> = {
owner: TransactionSigner<TAccountOwner>;
userState: Address<TAccountUserState>;
farmState: Address<TAccountFarmState>;
scopePrices?: Address<TAccountScopePrices>;
stakeSharesScaled: UnstakeInstructionDataArgs["stakeSharesScaled"];
};
export function getUnstakeInstruction<
TAccountOwner extends string,
TAccountUserState extends string,
TAccountFarmState extends string,
TAccountScopePrices extends string,
TProgramAddress extends Address = typeof FARMS_PROGRAM_ADDRESS,
>(
input: UnstakeInput<
TAccountOwner,
TAccountUserState,
TAccountFarmState,
TAccountScopePrices
>,
config?: { programAddress?: TProgramAddress },
): UnstakeInstruction<
TProgramAddress,
TAccountOwner,
TAccountUserState,
TAccountFarmState,
TAccountScopePrices
> {
// Program address.
const programAddress = config?.programAddress ?? FARMS_PROGRAM_ADDRESS;
// Original accounts.
const originalAccounts = {
owner: { value: input.owner ?? null, isWritable: true },
userState: { value: input.userState ?? null, isWritable: true },
farmState: { value: input.farmState ?? null, isWritable: true },
scopePrices: { value: input.scopePrices ?? 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.userState),
getAccountMeta(accounts.farmState),
getAccountMeta(accounts.scopePrices),
],
data: getUnstakeInstructionDataEncoder().encode(
args as UnstakeInstructionDataArgs,
),
programAddress,
} as UnstakeInstruction<
TProgramAddress,
TAccountOwner,
TAccountUserState,
TAccountFarmState,
TAccountScopePrices
>);
}
export type ParsedUnstakeInstruction<
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];
scopePrices?: TAccountMetas[3] | undefined;
};
data: UnstakeInstructionData;
};
export function parseUnstakeInstruction<
TProgram extends string,
TAccountMetas extends readonly AccountMeta[],
>(
instruction: Instruction<TProgram> &
InstructionWithAccounts<TAccountMetas> &
InstructionWithData<ReadonlyUint8Array>,
): ParsedUnstakeInstruction<TProgram, TAccountMetas> {
if (instruction.accounts.length < 4) {
// 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(),
scopePrices: getNextOptionalAccount(),
},
data: getUnstakeInstructionDataDecoder().decode(instruction.data),
};
}