Skip to content

Commit

Permalink
clean up code a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
cprussin committed Oct 8, 2024
1 parent 71dd9f4 commit 8cd4792
Showing 1 changed file with 14 additions and 18 deletions.
32 changes: 14 additions & 18 deletions apps/staking/src/components/Header/stats.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,34 +52,30 @@ export const Stats = ({ className, ...props }: HTMLProps<HTMLDivElement>) => {
};

const Loading = () => (
<div className="mb-1 h-5 w-10 animate-pulse rounded-md bg-white/30" />
<div className="inline-block h-[1em] w-10 animate-pulse rounded-md bg-white/30" />
);

const fetchStats = async (connection: Connection) => {
const client = new PythStakingClient({ connection });
const poolData = await client.getPoolDataAccount();
const rewardCustodyAccount = await client.getRewardCustodyAccount();
const totalDelegated = sum(
poolData.delState.map(
({ totalDelegation, deltaDelegation }) =>
totalDelegation + deltaDelegation,
),
);
const totalSelfStaked = sum(
poolData.selfDelState.map(
({ totalDelegation, deltaDelegation }) =>
totalDelegation + deltaDelegation,
),
);
const [poolData, rewardCustodyAccount] = await Promise.all([
client.getPoolDataAccount(),
client.getRewardCustodyAccount(),
]);

return {
totalStaked: totalDelegated + totalSelfStaked,
totalStaked:
sumDelegations(poolData.delState) + sumDelegations(poolData.selfDelState),
rewardsDistributed:
poolData.claimableRewards +
INITIAL_REWARD_POOL_SIZE -
rewardCustodyAccount.amount,
};
};

const sum = (values: bigint[]): bigint =>
values.reduce((acc, value) => acc + value, 0n);
const sumDelegations = (
values: { totalDelegation: bigint; deltaDelegation: bigint }[],
) =>
values.reduce(
(acc, value) => acc + value.totalDelegation + value.deltaDelegation,
0n,
);

0 comments on commit 8cd4792

Please sign in to comment.