Skip to content

Commit

Permalink
Merge pull request #168 from ar-io/fix-staking-rewards-earned
Browse files Browse the repository at this point in the history
fix: adjust code to deal with missing rewards on current epoch
  • Loading branch information
kunstmusik authored Feb 14, 2025
2 parents 606200a + 3955017 commit 8b2eef8
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 9 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [1.9.5] - 2025-02-14

### Fixed

- Adjusted rewards calculation to work with new scheme where rewards were unavailable on current epoch

## [1.9.4] - 2025-02-13

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@ar-io/network-portal",
"private": true,
"version": "1.9.4",
"version": "1.9.5",
"type": "module",
"scripts": {
"build": "yarn clean && tsc --build tsconfig.build.json && NODE_OPTIONS=--max-old-space-size=32768 vite build",
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useGatewaysPerEpoch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const useGatewaysPerEpoch = () => {
epochIndex: epoch.epochIndex,
totalEligibleGateways:
epoch.distributions.totalEligibleGateways ||
Object.keys(epoch.distributions.rewards.eligible).length,
Object.keys(epoch.distributions.rewards?.eligible ?? {}).length,
};
});
},
Expand Down
15 changes: 8 additions & 7 deletions src/hooks/useRewardsEarned.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,18 @@ const useRewardsEarned = (walletAddress?: string) => {

useEffect(() => {
if (epochs && walletAddress) {
const sorted = epochs.sort((a, b) => (a?.epochIndex || 0) - (b?.epochIndex || 0));
const sorted = epochs.sort(
(a, b) => (a?.epochIndex || 0) - (b?.epochIndex || 0),
);
const previousEpoch = sorted[sorted.length - 2];
// rewards are not avialable on current epoch
const previousEpochDistributed =
previousEpoch?.distributions.rewards.distributed;
const previousEpochRewards = previousEpochDistributed
? previousEpochDistributed[walletAddress] || 0
: 0;
previousEpoch?.distributions.rewards?.distributed ?? {};
const previousEpochRewards = previousEpochDistributed[walletAddress] || 0;

const totalForPastAvailableEpochs = epochs.reduce((acc, epoch) => {
const distributed = epoch?.distributions.rewards.distributed;
return acc + (distributed ? distributed[walletAddress] || 0 : 0);
const distributed = epoch?.distributions.rewards?.distributed ?? {};
return acc + (distributed[walletAddress] || 0);
}, 0);

setRewardsEarned({
Expand Down

0 comments on commit 8b2eef8

Please # to comment.