Skip to content

Commit

Permalink
fix: update web3.js1.0 example to handle stake lamports correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobcreech committed Aug 29, 2024
1 parent b91b91a commit cb48733
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions web3js-1.0/src/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@ export async function getStakeActivation(
stakeAddress: PublicKey
): Promise<StakeActivation> {
const SYSVAR_STAKE_HISTORY_ADDRESS = new PublicKey("SysvarStakeHistory1111111111111111111111111");
const [epochInfo, stakeAccount, stakeHistory] = await Promise.all([
const [epochInfo, { stakeAccount, stakeAccountLamports }, stakeHistory] = await Promise.all([
connection.getEpochInfo(),
(async () => {
const stakeAccountParsed = await connection.getParsedAccountInfo(stakeAddress);
if (stakeAccountParsed === null || stakeAccountParsed.value === null) {
throw new Error("Account not found");
}
return getStakeAccount(stakeAccountParsed);
const stakeAccount = getStakeAccount(stakeAccountParsed);
const stakeAccountLamports = stakeAccountParsed.value.lamports;
return { stakeAccount, stakeAccountLamports };
})(),
(async () => {
const stakeHistoryParsed = await connection.getParsedAccountInfo(SYSVAR_STAKE_HISTORY_ADDRESS);
Expand Down Expand Up @@ -48,7 +50,7 @@ export async function getStakeActivation(
} else {
status = 'inactive';
}
const inactive = BigInt(stakeAccountParsed.value.lamports) - effective - stakeAccount.meta.rentExemptReserve;
const inactive = BigInt(stakeAccountLamports) - effective - stakeAccount.meta.rentExemptReserve;

return {
status,
Expand Down

0 comments on commit cb48733

Please sign in to comment.