@zebec-network/zebec-stake-sdk 1.0.0 → 1.0.2
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 +7 -0
- package/dist/service.js +44 -0
- package/package.json +1 -1
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
|
@@ -199,6 +199,10 @@ class StakeService {
|
|
|
199
199
|
if (!lockupAccount) {
|
|
200
200
|
throw new Error("Lockup account does not exists for address: " + lockup);
|
|
201
201
|
}
|
|
202
|
+
const lockPeriods = lockupAccount.stakeInfo.durationMap.map((item) => item.duration.toNumber());
|
|
203
|
+
if (!lockPeriods.includes(params.lockPeriod)) {
|
|
204
|
+
throw new Error("Invalid lockperiod. Available options are: " + lockPeriods.map((l) => l.toString()).concat(", "));
|
|
205
|
+
}
|
|
202
206
|
const stakeToken = lockupAccount.stakedToken.tokenAddress;
|
|
203
207
|
const stakeVault = (0, pda_1.deriveStakeVaultAddress)(lockup, this.program.programId);
|
|
204
208
|
const userNonce = (0, pda_1.deriveUserNonceAddress)(staker, lockup, this.program.programId);
|
|
@@ -290,6 +294,7 @@ class StakeService {
|
|
|
290
294
|
rewardAmount: (0, bignumber_js_1.BigNumber)(stakeAccount.rewardAmount.toString()).div(UNITS_PER_REWARD_TOKEN).toFixed(),
|
|
291
295
|
stakeClaimed: stakeAccount.stakeClaimed,
|
|
292
296
|
lockPeriod: stakeAccount.lockPeriod.toNumber(),
|
|
297
|
+
rewardClaimed: !stakeAccount.rewardAmount.eqn(0),
|
|
293
298
|
};
|
|
294
299
|
}
|
|
295
300
|
async getUserNonceInfo(userNonceAddress) {
|
|
@@ -301,5 +306,44 @@ class StakeService {
|
|
|
301
306
|
nonce: BigInt(userNonceAccount.nonce.toString()),
|
|
302
307
|
};
|
|
303
308
|
}
|
|
309
|
+
async getAllStakeInfos(userAdress, lockupAddress) {
|
|
310
|
+
const lockupAccount = await this.program.account.lockup.fetchNullable(lockupAddress, this.provider.connection.commitment);
|
|
311
|
+
if (!lockupAccount) {
|
|
312
|
+
throw new Error("Lockup account does not exists for address: " + lockupAddress);
|
|
313
|
+
}
|
|
314
|
+
const stakeTokenAddress = lockupAccount.stakedToken.tokenAddress;
|
|
315
|
+
const rewardTokenAddress = lockupAccount.rewardToken.tokenAddress;
|
|
316
|
+
const stakeTokenDecimals = await (0, solana_common_1.getMintDecimals)(this.provider.connection, stakeTokenAddress);
|
|
317
|
+
const rewardTokenDecimals = await (0, solana_common_1.getMintDecimals)(this.provider.connection, rewardTokenAddress);
|
|
318
|
+
const UNITS_PER_STAKE_TOKEN = constants_1.TEN_BIGNUM.pow(stakeTokenDecimals);
|
|
319
|
+
const UNITS_PER_REWARD_TOKEN = constants_1.TEN_BIGNUM.pow(rewardTokenDecimals);
|
|
320
|
+
const userNonceAddress = (0, pda_1.deriveUserNonceAddress)(userAdress, lockupAddress, this.program.programId);
|
|
321
|
+
const userNonceAccount = await this.program.account.userNonce.fetchNullable(userNonceAddress, this.provider.connection.commitment);
|
|
322
|
+
if (!userNonceAccount) {
|
|
323
|
+
return [];
|
|
324
|
+
}
|
|
325
|
+
const currentNonce = userNonceAccount.nonce.toNumber();
|
|
326
|
+
const nonces = Array.from({ length: currentNonce }, (_, i) => BigInt(i));
|
|
327
|
+
const promises = nonces.map(async (nonce) => {
|
|
328
|
+
const stakeAddress = (0, pda_1.deriveStakeAddress)(userAdress, lockupAddress, nonce, this.program.programId);
|
|
329
|
+
const stakeAccount = await this.program.account.userStakeData.fetch(stakeAddress, this.provider.connection.commitment);
|
|
330
|
+
const signatures = await this.provider.connection.getSignaturesForAddress(stakeAddress, {}, "finalized");
|
|
331
|
+
const stakeSignatures = signatures.filter((s) => {
|
|
332
|
+
return !s.err && (s.blockTime ?? 0) === stakeAccount.createdTime.toNumber();
|
|
333
|
+
});
|
|
334
|
+
const info = {
|
|
335
|
+
hash: stakeSignatures[stakeSignatures.length - 1].signature,
|
|
336
|
+
nonce: BigInt(stakeAccount.nonce.toString()),
|
|
337
|
+
createdTime: stakeAccount.createdTime.toNumber(),
|
|
338
|
+
stakedAmount: (0, bignumber_js_1.BigNumber)(stakeAccount.stakedAmount.toString()).div(UNITS_PER_STAKE_TOKEN).toFixed(),
|
|
339
|
+
rewardAmount: (0, bignumber_js_1.BigNumber)(stakeAccount.rewardAmount.toString()).div(UNITS_PER_REWARD_TOKEN).toFixed(),
|
|
340
|
+
stakeClaimed: stakeAccount.stakeClaimed,
|
|
341
|
+
lockPeriod: stakeAccount.lockPeriod.toNumber(),
|
|
342
|
+
rewardClaimed: !stakeAccount.rewardAmount.eqn(0),
|
|
343
|
+
};
|
|
344
|
+
return info;
|
|
345
|
+
});
|
|
346
|
+
return Promise.all(promises);
|
|
347
|
+
}
|
|
304
348
|
}
|
|
305
349
|
exports.StakeService = StakeService;
|