@zebec-network/zebec-stake-sdk 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/service.d.ts CHANGED
@@ -70,6 +70,9 @@ export declare class StakeService {
70
70
  getLockupInfo(lockupAddress: Address): Promise<LockupInfo | null>;
71
71
  getStakeInfo(stakeAddress: Address, lockupAddress: Address): Promise<StakeInfo | null>;
72
72
  getUserNonceInfo(userNonceAddress: Address): Promise<UserNonceInfo | null>;
73
+ getAllStakeInfos(userAdress: Address, lockupAddress: Address): Promise<(StakeInfo & {
74
+ hash: string;
75
+ })[]>;
73
76
  }
74
77
  export type InitLockupInstructionData = {
75
78
  rewardSchemes: ParsedRewardScheme[];
@@ -116,8 +119,12 @@ export type StakeInfo = {
116
119
  rewardAmount: string;
117
120
  stakeClaimed: boolean;
118
121
  lockPeriod: number;
122
+ rewardClaimed: boolean;
119
123
  };
120
124
  export type UserNonceInfo = {
121
125
  nonce: bigint;
122
126
  };
127
+ export type StakeInfoWithHash = StakeInfo & {
128
+ hash: string;
129
+ };
123
130
  export {};
package/dist/service.js CHANGED
@@ -1,6 +1,10 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
6
  exports.StakeService = exports.StakeServiceBuilder = void 0;
7
+ const assert_1 = __importDefault(require("assert"));
4
8
  const bignumber_js_1 = require("bignumber.js");
5
9
  const anchor_1 = require("@coral-xyz/anchor");
6
10
  const web3_js_1 = require("@solana/web3.js");
@@ -199,6 +203,10 @@ class StakeService {
199
203
  if (!lockupAccount) {
200
204
  throw new Error("Lockup account does not exists for address: " + lockup);
201
205
  }
206
+ const lockPeriods = lockupAccount.stakeInfo.durationMap.map((item) => item.duration.toNumber());
207
+ if (!lockPeriods.includes(params.lockPeriod)) {
208
+ throw new Error("Invalid lockperiod. Available options are: " + lockPeriods.map((l) => l.toString()).concat(", "));
209
+ }
202
210
  const stakeToken = lockupAccount.stakedToken.tokenAddress;
203
211
  const stakeVault = (0, pda_1.deriveStakeVaultAddress)(lockup, this.program.programId);
204
212
  const userNonce = (0, pda_1.deriveUserNonceAddress)(staker, lockup, this.program.programId);
@@ -290,6 +298,7 @@ class StakeService {
290
298
  rewardAmount: (0, bignumber_js_1.BigNumber)(stakeAccount.rewardAmount.toString()).div(UNITS_PER_REWARD_TOKEN).toFixed(),
291
299
  stakeClaimed: stakeAccount.stakeClaimed,
292
300
  lockPeriod: stakeAccount.lockPeriod.toNumber(),
301
+ rewardClaimed: !stakeAccount.rewardAmount.eqn(0),
293
302
  };
294
303
  }
295
304
  async getUserNonceInfo(userNonceAddress) {
@@ -301,5 +310,45 @@ class StakeService {
301
310
  nonce: BigInt(userNonceAccount.nonce.toString()),
302
311
  };
303
312
  }
313
+ async getAllStakeInfos(userAdress, lockupAddress) {
314
+ const lockupAccount = await this.program.account.lockup.fetchNullable(lockupAddress, this.provider.connection.commitment);
315
+ if (!lockupAccount) {
316
+ throw new Error("Lockup account does not exists for address: " + lockupAddress);
317
+ }
318
+ const stakeTokenAddress = lockupAccount.stakedToken.tokenAddress;
319
+ const rewardTokenAddress = lockupAccount.rewardToken.tokenAddress;
320
+ const stakeTokenDecimals = await (0, solana_common_1.getMintDecimals)(this.provider.connection, stakeTokenAddress);
321
+ const rewardTokenDecimals = await (0, solana_common_1.getMintDecimals)(this.provider.connection, rewardTokenAddress);
322
+ const UNITS_PER_STAKE_TOKEN = constants_1.TEN_BIGNUM.pow(stakeTokenDecimals);
323
+ const UNITS_PER_REWARD_TOKEN = constants_1.TEN_BIGNUM.pow(rewardTokenDecimals);
324
+ const userNonceAddress = (0, pda_1.deriveUserNonceAddress)(userAdress, lockupAddress, this.program.programId);
325
+ const userNonceAccount = await this.program.account.userNonce.fetchNullable(userNonceAddress, this.provider.connection.commitment);
326
+ if (!userNonceAccount) {
327
+ return [];
328
+ }
329
+ const currentNonce = userNonceAccount.nonce.toNumber();
330
+ const nonces = Array.from({ length: currentNonce }, (_, i) => BigInt(i));
331
+ const promises = nonces.map(async (nonce) => {
332
+ const stakeAddress = (0, pda_1.deriveStakeAddress)(userAdress, lockupAddress, nonce, this.program.programId);
333
+ const stakeAccount = await this.program.account.userStakeData.fetch(stakeAddress, this.provider.connection.commitment);
334
+ const signatures = await this.provider.connection.getSignaturesForAddress(stakeAddress, {}, "finalized");
335
+ const stakeSignatures = signatures.filter((s) => {
336
+ (0, assert_1.default)(s.blockTime, "Blocktime is missing in signature info");
337
+ return !s.err && s.blockTime === stakeAccount.createdTime.toNumber();
338
+ });
339
+ const info = {
340
+ hash: stakeSignatures[stakeSignatures.length - 1].signature,
341
+ nonce: BigInt(stakeAccount.nonce.toString()),
342
+ createdTime: stakeAccount.createdTime.toNumber(),
343
+ stakedAmount: (0, bignumber_js_1.BigNumber)(stakeAccount.stakedAmount.toString()).div(UNITS_PER_STAKE_TOKEN).toFixed(),
344
+ rewardAmount: (0, bignumber_js_1.BigNumber)(stakeAccount.rewardAmount.toString()).div(UNITS_PER_REWARD_TOKEN).toFixed(),
345
+ stakeClaimed: stakeAccount.stakeClaimed,
346
+ lockPeriod: stakeAccount.lockPeriod.toNumber(),
347
+ rewardClaimed: !stakeAccount.rewardAmount.eqn(0),
348
+ };
349
+ return info;
350
+ });
351
+ return Promise.all(promises);
352
+ }
304
353
  }
305
354
  exports.StakeService = StakeService;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zebec-network/zebec-stake-sdk",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "An SDK for zebec network stake solana program",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",