@zebec-network/zebec-stake-sdk 1.0.6 → 1.0.8
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 +16 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/service.d.ts +4 -2
- package/dist/service.js +55 -9
- package/package.json +1 -1
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
package/dist/index.js
CHANGED
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
|
-
|
|
74
|
+
getAllStakesInfoOfUser(userAdress: Address, lockupAddress: Address, options?: {
|
|
75
75
|
minDelayMs?: number;
|
|
76
76
|
maxConcurrent?: number;
|
|
77
77
|
}): Promise<StakeInfoWithHash[]>;
|
|
78
|
-
|
|
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
|
@@ -248,7 +248,7 @@ class StakeService {
|
|
|
248
248
|
const stakePda = (0, pda_1.deriveStakeAddress)(staker, lockup, params.nonce, this.program.programId);
|
|
249
249
|
const rewardVault = (0, pda_1.deriveRewardVaultAddress)(lockup, this.program.programId);
|
|
250
250
|
const stakeVault = (0, pda_1.deriveStakeVaultAddress)(lockup, this.program.programId);
|
|
251
|
-
const stakerTokenAccount = (0, solana_common_1.getAssociatedTokenAddressSync)(stakeToken, staker);
|
|
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
|
}
|
|
@@ -321,7 +321,7 @@ class StakeService {
|
|
|
321
321
|
nonce: BigInt(userNonceAccount.nonce.toString()),
|
|
322
322
|
};
|
|
323
323
|
}
|
|
324
|
-
async
|
|
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
|
|
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:
|
|
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
|
|
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;
|