Skip to content

Commit

Permalink
fix: memoize some values to prevent recalculations
Browse files Browse the repository at this point in the history
  • Loading branch information
kemuru committed Oct 29, 2024
1 parent 723cf91 commit dafcad2
Showing 1 changed file with 14 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -150,18 +150,20 @@ const SimulatorPopup: React.FC<ISimulatorPopup> = ({ amountToStake, isStaking })
const currentTreeDisputesPerPnk = foundCourt?.treeDisputesPerPnk;
const currentTreeExpectedRewardPerPnk = foundCourt?.treeExpectedRewardPerPnk;

const totalVotes =
!isUndefined(courtCurrentEffectiveStake) && !isUndefined(currentTreeVotesPerPnk)
? courtCurrentEffectiveStake * currentTreeVotesPerPnk
: undefined;
const totalCases =
!isUndefined(courtCurrentEffectiveStake) && !isUndefined(currentTreeDisputesPerPnk)
? courtCurrentEffectiveStake * currentTreeDisputesPerPnk
: undefined;
const totalRewards =
!isUndefined(courtCurrentEffectiveStake) && !isUndefined(currentTreeExpectedRewardPerPnk)
? courtCurrentEffectiveStake * currentTreeExpectedRewardPerPnk
: undefined;
const totals = useMemo(() => {
if (isUndefined(courtCurrentEffectiveStake)) return {};
return {
votes: !isUndefined(currentTreeVotesPerPnk) ? courtCurrentEffectiveStake * currentTreeVotesPerPnk : undefined,
cases: !isUndefined(currentTreeDisputesPerPnk)
? courtCurrentEffectiveStake * currentTreeDisputesPerPnk
: undefined,
rewards: !isUndefined(currentTreeExpectedRewardPerPnk)
? courtCurrentEffectiveStake * currentTreeExpectedRewardPerPnk
: undefined,
};
}, [courtCurrentEffectiveStake, currentTreeVotesPerPnk, currentTreeDisputesPerPnk, currentTreeExpectedRewardPerPnk]);

const { votes: totalVotes, cases: totalCases, rewards: totalRewards } = totals;

const courtFutureEffectiveStake = !isUndefined(courtCurrentEffectiveStake)
? Math.max(isStaking ? courtCurrentEffectiveStake + amountToStake : courtCurrentEffectiveStake - amountToStake, 0)
Expand Down

0 comments on commit dafcad2

Please sign in to comment.