@zebec-network/zebec-stake-sdk 1.0.9 → 1.0.11

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
@@ -1,5 +1,5 @@
1
1
  import { Address, AnchorProvider, BN, Program, Provider } from "@coral-xyz/anchor";
2
- import { PublicKey, TransactionInstruction } from "@solana/web3.js";
2
+ import { Connection, PublicKey, TransactionInstruction } from "@solana/web3.js";
3
3
  import { TransactionPayload } from "@zebec-network/solana-common";
4
4
  import { ZebecStakeIdlV1 } from "./artifacts";
5
5
  import { ReadonlyProvider } from "./providers";
@@ -78,6 +78,8 @@ export declare class StakeService {
78
78
  getAllStakesCount(lockupAddress: Address): Promise<number>;
79
79
  getStakeSignatureForStake(stakeInfo: StakeInfo): Promise<string | null>;
80
80
  getAllStakesInfo(lockupAddress: Address): Promise<StakeInfo[]>;
81
+ get programId(): PublicKey;
82
+ get connection(): Connection;
81
83
  }
82
84
  export type InitLockupInstructionData = {
83
85
  rewardSchemes: ParsedRewardScheme[];
package/dist/service.js CHANGED
@@ -126,7 +126,7 @@ class StakeService {
126
126
  return provider.wallet.signTransaction(tx);
127
127
  };
128
128
  }
129
- return new solana_common_1.TransactionPayload(this.provider.connection, errorMap, instructions, payerKey, signers, addressLookupTableAccounts, signTransaction);
129
+ return new solana_common_1.TransactionPayload(this.connection, errorMap, instructions, payerKey, signers, addressLookupTableAccounts, signTransaction);
130
130
  }
131
131
  async getInitLockupInstruction(creator, lockup, stakeToken, rewardToken, rewardVault, stakeVault, data) {
132
132
  return this.program.methods
@@ -177,7 +177,7 @@ class StakeService {
177
177
  const stakeToken = (0, anchor_1.translateAddress)(params.stakeToken);
178
178
  const rewardToken = (0, anchor_1.translateAddress)(params.rewardToken);
179
179
  const feeVault = (0, anchor_1.translateAddress)(params.feeVault);
180
- const stakeTokenDecimals = await (0, solana_common_1.getMintDecimals)(this.provider.connection, stakeToken);
180
+ const stakeTokenDecimals = await (0, solana_common_1.getMintDecimals)(this.connection, stakeToken);
181
181
  const UNITS_PER_STAKE_TOKEN = constants_1.TEN_BIGNUM.pow(stakeTokenDecimals);
182
182
  const rewardSchemes = params.rewardSchemes.map((value) => {
183
183
  return {
@@ -185,9 +185,9 @@ class StakeService {
185
185
  reward: new anchor_1.BN((0, core_utils_1.percentToBps)(value.rewardRate)),
186
186
  };
187
187
  });
188
- const lockup = (0, pda_1.deriveLockupAddress)(params.name, this.program.programId);
189
- const rewardVault = (0, pda_1.deriveRewardVaultAddress)(lockup, this.program.programId);
190
- const stakeVault = (0, pda_1.deriveStakeVaultAddress)(lockup, this.program.programId);
188
+ const lockup = (0, pda_1.deriveLockupAddress)(params.name, this.programId);
189
+ const rewardVault = (0, pda_1.deriveRewardVaultAddress)(lockup, this.programId);
190
+ const stakeVault = (0, pda_1.deriveStakeVaultAddress)(lockup, this.programId);
191
191
  const fee = new anchor_1.BN((0, bignumber_js_1.BigNumber)(params.fee).times(UNITS_PER_STAKE_TOKEN).toFixed(0));
192
192
  const minimumStake = new anchor_1.BN((0, bignumber_js_1.BigNumber)(params.minimumStake).times(UNITS_PER_STAKE_TOKEN).toFixed(0));
193
193
  const instruction = await this.getInitLockupInstruction(creator, lockup, stakeToken, rewardToken, rewardVault, stakeVault, {
@@ -204,8 +204,8 @@ class StakeService {
204
204
  if (!staker) {
205
205
  throw new Error("MissingArgument: Please provide either staker address or publicKey in provider");
206
206
  }
207
- const lockup = (0, pda_1.deriveLockupAddress)(params.lockupName, this.program.programId);
208
- const lockupAccount = await this.program.account.lockup.fetchNullable(lockup, this.provider.connection.commitment);
207
+ const lockup = (0, pda_1.deriveLockupAddress)(params.lockupName, this.programId);
208
+ const lockupAccount = await this.program.account.lockup.fetchNullable(lockup, this.connection.commitment);
209
209
  if (!lockupAccount) {
210
210
  throw new Error("Lockup account does not exists for address: " + lockup);
211
211
  }
@@ -214,16 +214,16 @@ class StakeService {
214
214
  throw new Error("Invalid lockperiod. Available options are: " + lockPeriods.map((l) => l.toString()).concat(", "));
215
215
  }
216
216
  const stakeToken = lockupAccount.stakedToken.tokenAddress;
217
- const stakeVault = (0, pda_1.deriveStakeVaultAddress)(lockup, this.program.programId);
218
- const userNonce = (0, pda_1.deriveUserNonceAddress)(staker, lockup, this.program.programId);
219
- const userNonceAccount = await this.program.account.userNonce.fetchNullable(userNonce, this.provider.connection.commitment);
217
+ const stakeVault = (0, pda_1.deriveStakeVaultAddress)(lockup, this.programId);
218
+ const userNonce = (0, pda_1.deriveUserNonceAddress)(staker, lockup, this.programId);
219
+ const userNonceAccount = await this.program.account.userNonce.fetchNullable(userNonce, this.connection.commitment);
220
220
  let nonce = BigInt(0);
221
221
  if (userNonceAccount) {
222
222
  nonce = BigInt(userNonceAccount.nonce.toString());
223
223
  }
224
- const stakePda = (0, pda_1.deriveStakeAddress)(staker, lockup, nonce, this.program.programId);
224
+ const stakePda = (0, pda_1.deriveStakeAddress)(staker, lockup, nonce, this.programId);
225
225
  const stakeVaultTokenAccount = (0, solana_common_1.getAssociatedTokenAddressSync)(stakeToken, stakeVault, true);
226
- const stakeTokenDecimals = await (0, solana_common_1.getMintDecimals)(this.provider.connection, stakeToken);
226
+ const stakeTokenDecimals = await (0, solana_common_1.getMintDecimals)(this.connection, stakeToken);
227
227
  const UNITS_PER_STAKE_TOKEN = constants_1.TEN_BIGNUM.pow(stakeTokenDecimals);
228
228
  const instruction = await this.getStakeInstruction(lockup, stakeToken, stakeVault, staker, userNonce, stakePda, stakeVaultTokenAccount, {
229
229
  amount: new anchor_1.BN((0, bignumber_js_1.BigNumber)(params.amount).times(UNITS_PER_STAKE_TOKEN).toFixed(0)),
@@ -237,28 +237,28 @@ class StakeService {
237
237
  if (!staker) {
238
238
  throw new Error("MissingArgument: Please provide either staker address or publicKey in provider");
239
239
  }
240
- const lockup = (0, pda_1.deriveLockupAddress)(params.lockupName, this.program.programId);
241
- const lockupAccount = await this.program.account.lockup.fetchNullable(lockup, this.provider.connection.commitment);
240
+ const lockup = (0, pda_1.deriveLockupAddress)(params.lockupName, this.programId);
241
+ const lockupAccount = await this.program.account.lockup.fetchNullable(lockup, this.connection.commitment);
242
242
  if (!lockupAccount) {
243
243
  throw new Error("Lockup account does not exists for address: " + lockup);
244
244
  }
245
245
  const stakeToken = lockupAccount.stakedToken.tokenAddress;
246
246
  const rewardToken = lockupAccount.rewardToken.tokenAddress;
247
247
  const feeVault = lockupAccount.feeInfo.feeVault;
248
- const stakePda = (0, pda_1.deriveStakeAddress)(staker, lockup, params.nonce, this.program.programId);
249
- const rewardVault = (0, pda_1.deriveRewardVaultAddress)(lockup, this.program.programId);
250
- const stakeVault = (0, pda_1.deriveStakeVaultAddress)(lockup, this.program.programId);
248
+ const stakePda = (0, pda_1.deriveStakeAddress)(staker, lockup, params.nonce, this.programId);
249
+ const rewardVault = (0, pda_1.deriveRewardVaultAddress)(lockup, this.programId);
250
+ const stakeVault = (0, pda_1.deriveStakeVaultAddress)(lockup, this.programId);
251
251
  const stakerTokenAccount = (0, solana_common_1.getAssociatedTokenAddressSync)(stakeToken, staker, true);
252
252
  const instruction = await this.getUnstakeInstruction(feeVault, lockup, stakePda, rewardToken, rewardVault, stakeToken, stakeVault, staker, stakerTokenAccount, new anchor_1.BN(params.nonce.toString()));
253
253
  return this._createPayload(staker, [instruction]);
254
254
  }
255
255
  async getLockupInfo(lockupAddress) {
256
- const lockupAccount = await this.program.account.lockup.fetchNullable(lockupAddress, this.provider.connection.commitment);
256
+ const lockupAccount = await this.program.account.lockup.fetchNullable(lockupAddress, this.connection.commitment);
257
257
  if (!lockupAccount) {
258
258
  return null;
259
259
  }
260
260
  const stakeTokenAddress = lockupAccount.stakedToken.tokenAddress;
261
- const stakeTokenDecimals = await (0, solana_common_1.getMintDecimals)(this.provider.connection, stakeTokenAddress);
261
+ const stakeTokenDecimals = await (0, solana_common_1.getMintDecimals)(this.connection, stakeTokenAddress);
262
262
  const UNITS_PER_STAKE_TOKEN = constants_1.TEN_BIGNUM.pow(stakeTokenDecimals);
263
263
  return {
264
264
  address: lockupAddress.toString(),
@@ -285,17 +285,17 @@ class StakeService {
285
285
  };
286
286
  }
287
287
  async getStakeInfo(stakeAddress, lockupAddress) {
288
- const lockupAccount = await this.program.account.lockup.fetchNullable(lockupAddress, this.provider.connection.commitment);
288
+ const lockupAccount = await this.program.account.lockup.fetchNullable(lockupAddress, this.connection.commitment);
289
289
  if (!lockupAccount) {
290
290
  throw new Error("Lockup account does not exists for address: " + lockupAddress);
291
291
  }
292
292
  const stakeTokenAddress = lockupAccount.stakedToken.tokenAddress;
293
293
  const rewardTokenAddress = lockupAccount.rewardToken.tokenAddress;
294
- const stakeTokenDecimals = await (0, solana_common_1.getMintDecimals)(this.provider.connection, stakeTokenAddress);
295
- const rewardTokenDecimals = await (0, solana_common_1.getMintDecimals)(this.provider.connection, rewardTokenAddress);
294
+ const stakeTokenDecimals = await (0, solana_common_1.getMintDecimals)(this.connection, stakeTokenAddress);
295
+ const rewardTokenDecimals = await (0, solana_common_1.getMintDecimals)(this.connection, rewardTokenAddress);
296
296
  const UNITS_PER_STAKE_TOKEN = constants_1.TEN_BIGNUM.pow(stakeTokenDecimals);
297
297
  const UNITS_PER_REWARD_TOKEN = constants_1.TEN_BIGNUM.pow(rewardTokenDecimals);
298
- const stakeAccount = await this.program.account.userStakeData.fetchNullable(stakeAddress, this.provider.connection.commitment);
298
+ const stakeAccount = await this.program.account.userStakeData.fetchNullable(stakeAddress, this.connection.commitment);
299
299
  if (!stakeAccount) {
300
300
  return null;
301
301
  }
@@ -312,7 +312,7 @@ class StakeService {
312
312
  };
313
313
  }
314
314
  async getUserNonceInfo(userNonceAddress) {
315
- const userNonceAccount = await this.program.account.userNonce.fetchNullable(userNonceAddress, this.provider.connection.commitment);
315
+ const userNonceAccount = await this.program.account.userNonce.fetchNullable(userNonceAddress, this.connection.commitment);
316
316
  if (!userNonceAccount) {
317
317
  return null;
318
318
  }
@@ -322,26 +322,26 @@ class StakeService {
322
322
  };
323
323
  }
324
324
  async getAllStakesInfoOfUser(userAdress, lockupAddress, options = {}) {
325
- const lockupAccount = await this.program.account.lockup.fetchNullable(lockupAddress, this.provider.connection.commitment);
325
+ const lockupAccount = await this.program.account.lockup.fetchNullable(lockupAddress, this.connection.commitment);
326
326
  if (!lockupAccount) {
327
327
  throw new Error("Lockup account does not exists for address: " + lockupAddress);
328
328
  }
329
329
  const stakeTokenAddress = lockupAccount.stakedToken.tokenAddress;
330
330
  const rewardTokenAddress = lockupAccount.rewardToken.tokenAddress;
331
- const stakeTokenDecimals = await (0, solana_common_1.getMintDecimals)(this.provider.connection, stakeTokenAddress);
332
- const rewardTokenDecimals = await (0, solana_common_1.getMintDecimals)(this.provider.connection, rewardTokenAddress);
331
+ const stakeTokenDecimals = await (0, solana_common_1.getMintDecimals)(this.connection, stakeTokenAddress);
332
+ const rewardTokenDecimals = await (0, solana_common_1.getMintDecimals)(this.connection, rewardTokenAddress);
333
333
  const UNITS_PER_STAKE_TOKEN = constants_1.TEN_BIGNUM.pow(stakeTokenDecimals);
334
334
  const UNITS_PER_REWARD_TOKEN = constants_1.TEN_BIGNUM.pow(rewardTokenDecimals);
335
- const userNonceAddress = (0, pda_1.deriveUserNonceAddress)(userAdress, lockupAddress, this.program.programId);
336
- const userNonceAccount = await this.program.account.userNonce.fetchNullable(userNonceAddress, this.provider.connection.commitment);
335
+ const userNonceAddress = (0, pda_1.deriveUserNonceAddress)(userAdress, lockupAddress, this.programId);
336
+ const userNonceAccount = await this.program.account.userNonce.fetchNullable(userNonceAddress, this.connection.commitment);
337
337
  if (!userNonceAccount) {
338
338
  return [];
339
339
  }
340
340
  const currentNonce = userNonceAccount.nonce.toNumber();
341
341
  const nonces = Array.from({ length: currentNonce }, (_, i) => BigInt(i));
342
- const stakeAddresses = nonces.map((nonce) => (0, pda_1.deriveStakeAddress)(userAdress, lockupAddress, nonce, this.program.programId));
343
- const accountInfos = await this.provider.connection.getMultipleAccountsInfo(stakeAddresses, {
344
- commitment: "finalized",
342
+ const stakeAddresses = nonces.map((nonce) => (0, pda_1.deriveStakeAddress)(userAdress, lockupAddress, nonce, this.programId));
343
+ const accountInfos = await this.connection.getMultipleAccountsInfo(stakeAddresses, {
344
+ commitment: this.connection.commitment,
345
345
  });
346
346
  const stakeAccountsInfo = accountInfos.map((value, i) => {
347
347
  (0, assert_1.default)(value, "Account does not exists for stake address: " + stakeAddresses[i] + " at nonce: " + nonces[i]);
@@ -374,8 +374,8 @@ class StakeService {
374
374
  }
375
375
  async getAllStakesCount(lockupAddress) {
376
376
  const dataSize = this.program.account.userStakeData.size;
377
- const accountInfos = await this.provider.connection.getProgramAccounts(this.program.programId, {
378
- commitment: "finalized",
377
+ const accountInfos = await this.connection.getProgramAccounts(this.programId, {
378
+ commitment: this.connection.commitment,
379
379
  dataSlice: {
380
380
  length: 0,
381
381
  offset: 0,
@@ -395,7 +395,8 @@ class StakeService {
395
395
  return accountInfos.length;
396
396
  }
397
397
  async getStakeSignatureForStake(stakeInfo) {
398
- const signatures = await (0, utils_1.callWithEnhancedBackoff)(async () => this.provider.connection.getSignaturesForAddress((0, anchor_1.translateAddress)(stakeInfo.address), {}, "finalized"));
398
+ const commitment = this.connection.commitment === "finalized" ? "finalized" : "confirmed";
399
+ const signatures = await (0, utils_1.callWithEnhancedBackoff)(async () => this.connection.getSignaturesForAddress((0, anchor_1.translateAddress)(stakeInfo.address), {}, commitment));
399
400
  const stakeSignatures = signatures.filter((s) => {
400
401
  return !s.err && (s.blockTime ?? 0) === stakeInfo.createdTime;
401
402
  });
@@ -403,18 +404,18 @@ class StakeService {
403
404
  return signatureInfo ? signatureInfo.signature : null;
404
405
  }
405
406
  async getAllStakesInfo(lockupAddress) {
406
- const lockupAccount = await this.program.account.lockup.fetchNullable(lockupAddress, this.provider.connection.commitment);
407
+ const lockupAccount = await this.program.account.lockup.fetchNullable(lockupAddress, this.connection.commitment);
407
408
  if (!lockupAccount) {
408
409
  throw new Error("Lockup account does not exists for address: " + lockupAddress);
409
410
  }
410
411
  const stakeTokenAddress = lockupAccount.stakedToken.tokenAddress;
411
412
  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);
413
+ const stakeTokenDecimals = await (0, solana_common_1.getMintDecimals)(this.connection, stakeTokenAddress);
414
+ const rewardTokenDecimals = await (0, solana_common_1.getMintDecimals)(this.connection, rewardTokenAddress);
414
415
  const UNITS_PER_STAKE_TOKEN = constants_1.TEN_BIGNUM.pow(stakeTokenDecimals);
415
416
  const UNITS_PER_REWARD_TOKEN = constants_1.TEN_BIGNUM.pow(rewardTokenDecimals);
416
417
  const dataSize = this.program.account.userStakeData.size;
417
- const accountInfos = await this.provider.connection.getProgramAccounts(this.program.programId, {
418
+ const accountInfos = await this.connection.getProgramAccounts(this.programId, {
418
419
  commitment: "finalized",
419
420
  filters: [
420
421
  {
@@ -444,5 +445,11 @@ class StakeService {
444
445
  return info;
445
446
  });
446
447
  }
448
+ get programId() {
449
+ return this.program.programId;
450
+ }
451
+ get connection() {
452
+ return this.provider.connection;
453
+ }
447
454
  }
448
455
  exports.StakeService = StakeService;
package/package.json CHANGED
@@ -1,46 +1,47 @@
1
- {
2
- "name": "@zebec-network/zebec-stake-sdk",
3
- "version": "1.0.9",
4
- "description": "An SDK for zebec network stake solana program",
5
- "main": "dist/index.js",
6
- "types": "dist/index.d.ts",
7
- "author": "Ashish Sapkota",
8
- "license": "MIT",
9
- "files": [
10
- "dist"
11
- ],
12
- "keywords": [
13
- "solana",
14
- "zebec",
15
- "zebec network",
16
- "stake",
17
- "staking"
18
- ],
19
- "scripts": {
20
- "build": "npm run clean && tsc",
21
- "clean": "rimraf ./dist",
22
- "format": "prettier --write .",
23
- "start": "ts-node src/index.ts",
24
- "test": "ts-mocha -p ./tsconfig.json -t 1000000000"
25
- },
26
- "dependencies": {
27
- "@coral-xyz/anchor": "^0.31.1",
28
- "@solana/web3.js": "1.98.2",
29
- "@types/bn.js": "^5.1.6",
30
- "@zebec-network/core-utils": "^1.0.4",
31
- "@zebec-network/solana-common": "^1.2.6",
32
- "bignumber.js": "^9.3.0",
33
- "buffer": "^6.0.3",
34
- "dotenv": "^16.5.0"
35
- },
36
- "devDependencies": {
37
- "@types/mocha": "^10.0.10",
38
- "@types/node": "^22.14.1",
39
- "mocha": "^11.1.0",
40
- "prettier": "^3.5.3",
41
- "rimraf": "^6.0.1",
42
- "ts-mocha": "^11.1.0",
43
- "ts-node": "^10.9.2",
44
- "typescript": "^5.8.3"
45
- }
1
+ {
2
+ "name": "@zebec-network/zebec-stake-sdk",
3
+ "version": "1.0.11",
4
+ "description": "An SDK for zebec network stake solana program",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "author": "Ashish Sapkota",
8
+ "license": "MIT",
9
+ "files": [
10
+ "dist"
11
+ ],
12
+ "keywords": [
13
+ "solana",
14
+ "zebec",
15
+ "zebec network",
16
+ "stake",
17
+ "staking"
18
+ ],
19
+ "scripts": {
20
+ "build": "npm run clean && tsc",
21
+ "clean": "rimraf ./dist",
22
+ "format": "prettier --write .",
23
+ "start": "ts-node src/index.ts",
24
+ "test": "ts-mocha -p ./tsconfig.json -t 1000000000 test/**/*.test.ts",
25
+ "test:single": "ts-mocha -p ./tsconfig.json -t 1000000000"
26
+ },
27
+ "dependencies": {
28
+ "@coral-xyz/anchor": "^0.31.1",
29
+ "@solana/web3.js": "1.98.2",
30
+ "@zebec-network/core-utils": "^1.0.4",
31
+ "@zebec-network/solana-common": "^1.5.0",
32
+ "bignumber.js": "^9.3.0",
33
+ "buffer": "^6.0.3",
34
+ "dotenv": "^16.5.0"
35
+ },
36
+ "devDependencies": {
37
+ "@types/bn.js": "^5.1.6",
38
+ "@types/mocha": "^10.0.10",
39
+ "@types/node": "^22.14.1",
40
+ "mocha": "^11.1.0",
41
+ "prettier": "^3.5.3",
42
+ "rimraf": "^6.0.1",
43
+ "ts-mocha": "^11.1.0",
44
+ "ts-node": "^10.9.2",
45
+ "typescript": "^5.8.3"
46
+ }
46
47
  }