@mihalex/farms-sdk-tests
Version:
294 lines (276 loc) • 10.2 kB
text/typescript
import { PublicKey, Connection } from "@solana/web3.js";
import BN from "bn.js"; // eslint-disable-line @typescript-eslint/no-unused-vars
import * as borsh from "@coral-xyz/borsh"; // eslint-disable-line @typescript-eslint/no-unused-vars
import * as types from "../types"; // eslint-disable-line @typescript-eslint/no-unused-vars
import { PROGRAM_ID } from "../programId";
export interface UserStateFields {
userId: BN;
farmState: PublicKey;
owner: PublicKey;
/** User data to account for rewards */
legacyStake: BN;
/**
* Rewards tally used for computation of gained rewards
* (scaled from `Decimal` representation).
*/
rewardsTallyScaled: Array<BN>;
/** Number of reward tokens ready for claim */
rewardsIssuedUnclaimed: Array<BN>;
lastClaimTs: Array<BN>;
/**
* User stake deposited and usable, generating rewards and fees.
* (scaled from `Decimal` representation).
*/
activeStakeScaled: BN;
/**
* User stake deposited but not usable and not generating rewards yet.
* (scaled from `Decimal` representation).
*/
pendingDepositStakeScaled: BN;
/**
* After this timestamp, pending user stake can be moved to user stake
* Initialized to now() + delayed user stake period
*/
pendingDepositStakeTs: BN;
/**
* User deposits unstaked, pending for withdrawal, not usable and not generating rewards.
* (scaled from `Decimal` representation).
*/
pendingWithdrawalUnstakeScaled: BN;
/** After this timestamp, user can withdraw their deposit. */
pendingWithdrawalUnstakeTs: BN;
/** User bump used for account address validation */
bump: BN;
/** Delegatee used for initialisation - useful to check against */
delegatee: PublicKey;
lastStakeTs: BN;
padding: Array<BN>;
}
export interface UserStateJSON {
userId: string;
farmState: string;
owner: string;
/** User data to account for rewards */
legacyStake: string;
/**
* Rewards tally used for computation of gained rewards
* (scaled from `Decimal` representation).
*/
rewardsTallyScaled: Array<string>;
/** Number of reward tokens ready for claim */
rewardsIssuedUnclaimed: Array<string>;
lastClaimTs: Array<string>;
/**
* User stake deposited and usable, generating rewards and fees.
* (scaled from `Decimal` representation).
*/
activeStakeScaled: string;
/**
* User stake deposited but not usable and not generating rewards yet.
* (scaled from `Decimal` representation).
*/
pendingDepositStakeScaled: string;
/**
* After this timestamp, pending user stake can be moved to user stake
* Initialized to now() + delayed user stake period
*/
pendingDepositStakeTs: string;
/**
* User deposits unstaked, pending for withdrawal, not usable and not generating rewards.
* (scaled from `Decimal` representation).
*/
pendingWithdrawalUnstakeScaled: string;
/** After this timestamp, user can withdraw their deposit. */
pendingWithdrawalUnstakeTs: string;
/** User bump used for account address validation */
bump: string;
/** Delegatee used for initialisation - useful to check against */
delegatee: string;
lastStakeTs: string;
padding: Array<string>;
}
export class UserState {
readonly userId: BN;
readonly farmState: PublicKey;
readonly owner: PublicKey;
/** User data to account for rewards */
readonly legacyStake: BN;
/**
* Rewards tally used for computation of gained rewards
* (scaled from `Decimal` representation).
*/
readonly rewardsTallyScaled: Array<BN>;
/** Number of reward tokens ready for claim */
readonly rewardsIssuedUnclaimed: Array<BN>;
readonly lastClaimTs: Array<BN>;
/**
* User stake deposited and usable, generating rewards and fees.
* (scaled from `Decimal` representation).
*/
readonly activeStakeScaled: BN;
/**
* User stake deposited but not usable and not generating rewards yet.
* (scaled from `Decimal` representation).
*/
readonly pendingDepositStakeScaled: BN;
/**
* After this timestamp, pending user stake can be moved to user stake
* Initialized to now() + delayed user stake period
*/
readonly pendingDepositStakeTs: BN;
/**
* User deposits unstaked, pending for withdrawal, not usable and not generating rewards.
* (scaled from `Decimal` representation).
*/
readonly pendingWithdrawalUnstakeScaled: BN;
/** After this timestamp, user can withdraw their deposit. */
readonly pendingWithdrawalUnstakeTs: BN;
/** User bump used for account address validation */
readonly bump: BN;
/** Delegatee used for initialisation - useful to check against */
readonly delegatee: PublicKey;
readonly lastStakeTs: BN;
readonly padding: Array<BN>;
static readonly discriminator = Buffer.from([
72, 177, 85, 249, 76, 167, 186, 126,
]);
static readonly layout = borsh.struct([
borsh.u64("userId"),
borsh.publicKey("farmState"),
borsh.publicKey("owner"),
borsh.u64("legacyStake"),
borsh.array(borsh.u128(), 10, "rewardsTallyScaled"),
borsh.array(borsh.u64(), 10, "rewardsIssuedUnclaimed"),
borsh.array(borsh.u64(), 10, "lastClaimTs"),
borsh.u128("activeStakeScaled"),
borsh.u128("pendingDepositStakeScaled"),
borsh.u64("pendingDepositStakeTs"),
borsh.u128("pendingWithdrawalUnstakeScaled"),
borsh.u64("pendingWithdrawalUnstakeTs"),
borsh.u64("bump"),
borsh.publicKey("delegatee"),
borsh.u64("lastStakeTs"),
borsh.array(borsh.u64(), 50, "padding"),
]);
constructor(fields: UserStateFields) {
this.userId = fields.userId;
this.farmState = fields.farmState;
this.owner = fields.owner;
this.legacyStake = fields.legacyStake;
this.rewardsTallyScaled = fields.rewardsTallyScaled;
this.rewardsIssuedUnclaimed = fields.rewardsIssuedUnclaimed;
this.lastClaimTs = fields.lastClaimTs;
this.activeStakeScaled = fields.activeStakeScaled;
this.pendingDepositStakeScaled = fields.pendingDepositStakeScaled;
this.pendingDepositStakeTs = fields.pendingDepositStakeTs;
this.pendingWithdrawalUnstakeScaled = fields.pendingWithdrawalUnstakeScaled;
this.pendingWithdrawalUnstakeTs = fields.pendingWithdrawalUnstakeTs;
this.bump = fields.bump;
this.delegatee = fields.delegatee;
this.lastStakeTs = fields.lastStakeTs;
this.padding = fields.padding;
}
static async fetch(
c: Connection,
address: PublicKey,
programId: PublicKey = PROGRAM_ID,
): Promise<UserState | null> {
const info = await c.getAccountInfo(address);
if (info === null) {
return null;
}
if (!info.owner.equals(programId)) {
throw new Error("account doesn't belong to this program");
}
return this.decode(info.data);
}
static async fetchMultiple(
c: Connection,
addresses: PublicKey[],
programId: PublicKey = PROGRAM_ID,
): Promise<Array<UserState | null>> {
const infos = await c.getMultipleAccountsInfo(addresses);
return infos.map((info) => {
if (info === null) {
return null;
}
if (!info.owner.equals(programId)) {
throw new Error("account doesn't belong to this program");
}
return this.decode(info.data);
});
}
static decode(data: Buffer): UserState {
if (!data.slice(0, 8).equals(UserState.discriminator)) {
throw new Error("invalid account discriminator");
}
const dec = UserState.layout.decode(data.slice(8));
return new UserState({
userId: dec.userId,
farmState: dec.farmState,
owner: dec.owner,
legacyStake: dec.legacyStake,
rewardsTallyScaled: dec.rewardsTallyScaled,
rewardsIssuedUnclaimed: dec.rewardsIssuedUnclaimed,
lastClaimTs: dec.lastClaimTs,
activeStakeScaled: dec.activeStakeScaled,
pendingDepositStakeScaled: dec.pendingDepositStakeScaled,
pendingDepositStakeTs: dec.pendingDepositStakeTs,
pendingWithdrawalUnstakeScaled: dec.pendingWithdrawalUnstakeScaled,
pendingWithdrawalUnstakeTs: dec.pendingWithdrawalUnstakeTs,
bump: dec.bump,
delegatee: dec.delegatee,
lastStakeTs: dec.lastStakeTs,
padding: dec.padding,
});
}
toJSON(): UserStateJSON {
return {
userId: this.userId.toString(),
farmState: this.farmState.toString(),
owner: this.owner.toString(),
legacyStake: this.legacyStake.toString(),
rewardsTallyScaled: this.rewardsTallyScaled.map((item) =>
item.toString(),
),
rewardsIssuedUnclaimed: this.rewardsIssuedUnclaimed.map((item) =>
item.toString(),
),
lastClaimTs: this.lastClaimTs.map((item) => item.toString()),
activeStakeScaled: this.activeStakeScaled.toString(),
pendingDepositStakeScaled: this.pendingDepositStakeScaled.toString(),
pendingDepositStakeTs: this.pendingDepositStakeTs.toString(),
pendingWithdrawalUnstakeScaled:
this.pendingWithdrawalUnstakeScaled.toString(),
pendingWithdrawalUnstakeTs: this.pendingWithdrawalUnstakeTs.toString(),
bump: this.bump.toString(),
delegatee: this.delegatee.toString(),
lastStakeTs: this.lastStakeTs.toString(),
padding: this.padding.map((item) => item.toString()),
};
}
static fromJSON(obj: UserStateJSON): UserState {
return new UserState({
userId: new BN(obj.userId),
farmState: new PublicKey(obj.farmState),
owner: new PublicKey(obj.owner),
legacyStake: new BN(obj.legacyStake),
rewardsTallyScaled: obj.rewardsTallyScaled.map((item) => new BN(item)),
rewardsIssuedUnclaimed: obj.rewardsIssuedUnclaimed.map(
(item) => new BN(item),
),
lastClaimTs: obj.lastClaimTs.map((item) => new BN(item)),
activeStakeScaled: new BN(obj.activeStakeScaled),
pendingDepositStakeScaled: new BN(obj.pendingDepositStakeScaled),
pendingDepositStakeTs: new BN(obj.pendingDepositStakeTs),
pendingWithdrawalUnstakeScaled: new BN(
obj.pendingWithdrawalUnstakeScaled,
),
pendingWithdrawalUnstakeTs: new BN(obj.pendingWithdrawalUnstakeTs),
bump: new BN(obj.bump),
delegatee: new PublicKey(obj.delegatee),
lastStakeTs: new BN(obj.lastStakeTs),
padding: obj.padding.map((item) => new BN(item)),
});
}
}