@zebec-network/zebec-stake-sdk 1.0.6 → 1.0.7

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/README.md CHANGED
@@ -17,3 +17,19 @@ To build the package
17
17
  ```
18
18
  yarn build
19
19
  ```
20
+
21
+ To run specific test filess
22
+
23
+ ```
24
+ yarn test <test file path> -f "<regex for test name>"
25
+ // example:
26
+ // yarn test ./test/e2e/getLockupInfo.test.ts
27
+ ```
28
+
29
+ ## publish
30
+
31
+ Build package and bump package version to specific need and publish
32
+
33
+ ```
34
+ npm publish --access public
35
+ ```
package/dist/index.d.ts CHANGED
@@ -3,3 +3,4 @@ export * from "./artifacts";
3
3
  export * from "./service";
4
4
  export * from "./providers";
5
5
  export * from "./pda";
6
+ export * from "./rateLimitQueue";
package/dist/index.js CHANGED
@@ -19,3 +19,4 @@ __exportStar(require("./artifacts"), exports);
19
19
  __exportStar(require("./service"), exports);
20
20
  __exportStar(require("./providers"), exports);
21
21
  __exportStar(require("./pda"), exports);
22
+ __exportStar(require("./rateLimitQueue"), exports);
package/dist/service.d.ts CHANGED
@@ -71,11 +71,13 @@ export declare class StakeService {
71
71
  getLockupInfo(lockupAddress: Address): Promise<LockupInfo | null>;
72
72
  getStakeInfo(stakeAddress: Address, lockupAddress: Address): Promise<StakeInfo | null>;
73
73
  getUserNonceInfo(userNonceAddress: Address): Promise<UserNonceInfo | null>;
74
- getAllStakesInfo(userAdress: Address, lockupAddress: Address, options?: {
74
+ getAllStakesInfoOfUser(userAdress: Address, lockupAddress: Address, options?: {
75
75
  minDelayMs?: number;
76
76
  maxConcurrent?: number;
77
77
  }): Promise<StakeInfoWithHash[]>;
78
- getTotalStakeCount(lockupAddress: Address): Promise<number>;
78
+ getAllStakesCount(lockupAddress: Address): Promise<number>;
79
+ getStakeSignatureForStake(stakeInfo: StakeInfo): Promise<string | null>;
80
+ getAllStakesInfo(lockupAddress: Address): Promise<StakeInfo[]>;
79
81
  }
80
82
  export type InitLockupInstructionData = {
81
83
  rewardSchemes: ParsedRewardScheme[];
package/dist/service.js CHANGED
@@ -321,7 +321,7 @@ class StakeService {
321
321
  nonce: BigInt(userNonceAccount.nonce.toString()),
322
322
  };
323
323
  }
324
- async getAllStakesInfo(userAdress, lockupAddress, options = {}) {
324
+ async getAllStakesInfoOfUser(userAdress, lockupAddress, options = {}) {
325
325
  const lockupAccount = await this.program.account.lockup.fetchNullable(lockupAddress, this.provider.connection.commitment);
326
326
  if (!lockupAccount) {
327
327
  throw new Error("Lockup account does not exists for address: " + lockupAddress);
@@ -363,20 +363,16 @@ class StakeService {
363
363
  const { maxConcurrent = 3, minDelayMs = 400 } = options;
364
364
  const queue = new rateLimitQueue_1.RateLimitedQueue(maxConcurrent, minDelayMs); // Max 3 concurrent, 300ms between requests
365
365
  const promises = stakeAccountsInfo.map((stakeInfo, index) => queue.add(async () => {
366
- const signatures = await (0, utils_1.callWithEnhancedBackoff)(async () => this.provider.connection.getSignaturesForAddress((0, anchor_1.translateAddress)(stakeInfo.address), {}, "finalized"));
367
- const stakeSignatures = signatures.filter((s) => {
368
- return !s.err && (s.blockTime ?? 0) === stakeInfo.createdTime;
369
- });
370
- const signatureInfo = stakeSignatures[stakeSignatures.length - 1];
366
+ const signature = await this.getStakeSignatureForStake(stakeInfo);
371
367
  stakesWithHash[index] = {
372
- hash: signatureInfo ? signatureInfo.signature : "",
368
+ hash: signature ? signature : "",
373
369
  ...stakeInfo,
374
370
  };
375
371
  }));
376
372
  await Promise.all(promises);
377
373
  return stakesWithHash;
378
374
  }
379
- async getTotalStakeCount(lockupAddress) {
375
+ async getAllStakesCount(lockupAddress) {
380
376
  const dataSize = this.program.account.userStakeData.size;
381
377
  const accountInfos = await this.provider.connection.getProgramAccounts(this.program.programId, {
382
378
  commitment: "finalized",
@@ -398,5 +394,55 @@ class StakeService {
398
394
  });
399
395
  return accountInfos.length;
400
396
  }
397
+ async getStakeSignatureForStake(stakeInfo) {
398
+ const signatures = await (0, utils_1.callWithEnhancedBackoff)(async () => this.provider.connection.getSignaturesForAddress((0, anchor_1.translateAddress)(stakeInfo.address), {}, "finalized"));
399
+ const stakeSignatures = signatures.filter((s) => {
400
+ return !s.err && (s.blockTime ?? 0) === stakeInfo.createdTime;
401
+ });
402
+ const signatureInfo = stakeSignatures[stakeSignatures.length - 1];
403
+ return signatureInfo ? signatureInfo.signature : null;
404
+ }
405
+ async getAllStakesInfo(lockupAddress) {
406
+ const lockupAccount = await this.program.account.lockup.fetchNullable(lockupAddress, this.provider.connection.commitment);
407
+ if (!lockupAccount) {
408
+ throw new Error("Lockup account does not exists for address: " + lockupAddress);
409
+ }
410
+ const stakeTokenAddress = lockupAccount.stakedToken.tokenAddress;
411
+ const rewardTokenAddress = lockupAccount.rewardToken.tokenAddress;
412
+ const stakeTokenDecimals = await (0, solana_common_1.getMintDecimals)(this.provider.connection, stakeTokenAddress);
413
+ const rewardTokenDecimals = await (0, solana_common_1.getMintDecimals)(this.provider.connection, rewardTokenAddress);
414
+ const UNITS_PER_STAKE_TOKEN = constants_1.TEN_BIGNUM.pow(stakeTokenDecimals);
415
+ const UNITS_PER_REWARD_TOKEN = constants_1.TEN_BIGNUM.pow(rewardTokenDecimals);
416
+ const dataSize = this.program.account.userStakeData.size;
417
+ const accountInfos = await this.provider.connection.getProgramAccounts(this.program.programId, {
418
+ commitment: "finalized",
419
+ filters: [
420
+ {
421
+ dataSize,
422
+ },
423
+ {
424
+ memcmp: {
425
+ bytes: lockupAddress.toString(),
426
+ offset: 81,
427
+ },
428
+ },
429
+ ],
430
+ });
431
+ return accountInfos.map((accountInfo) => {
432
+ const stakeAccount = this.program.coder.accounts.decode(this.program.idl.accounts[2].name, accountInfo.account.data);
433
+ const info = {
434
+ address: accountInfo.pubkey.toString(),
435
+ nonce: BigInt(stakeAccount.nonce.toString()),
436
+ createdTime: stakeAccount.createdTime.toNumber(),
437
+ stakedAmount: (0, bignumber_js_1.BigNumber)(stakeAccount.stakedAmount.toString()).div(UNITS_PER_STAKE_TOKEN).toFixed(),
438
+ rewardAmount: (0, bignumber_js_1.BigNumber)(stakeAccount.rewardAmount.toString()).div(UNITS_PER_REWARD_TOKEN).toFixed(),
439
+ stakeClaimed: stakeAccount.stakeClaimed,
440
+ lockPeriod: stakeAccount.lockPeriod.toNumber(),
441
+ lockup: stakeAccount.lockup.toString(),
442
+ staker: stakeAccount.staker.toString(),
443
+ };
444
+ return info;
445
+ });
446
+ }
401
447
  }
402
448
  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.6",
3
+ "version": "1.0.7",
4
4
  "description": "An SDK for zebec network stake solana program",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",