diff --git a/frontend/context/RewardProvider.tsx b/frontend/context/RewardProvider.tsx index 29270b97d..dead21038 100644 --- a/frontend/context/RewardProvider.tsx +++ b/frontend/context/RewardProvider.tsx @@ -1,6 +1,5 @@ import { useQuery } from '@tanstack/react-query'; -import { ethers } from 'ethers'; -import { formatUnits } from 'ethers/lib/utils'; +import { formatEther } from 'ethers/lib/utils'; import { isNil } from 'lodash'; import { createContext, @@ -174,26 +173,26 @@ export const RewardProvider = ({ children }: PropsWithChildren) => { // const rewardsPerSecondInBg = ( // BigNumber.isBigNumber(rewardsPerSecond) ? rewardsPerSecond.toNumber() : 0 // ) as number; - console.log(rewardsPerSecond); // console.log('stakingRewardsDetails', stakingRewardsDetails); // console.log('selectedStakingContractDetails', selectedStakingContractDetails); // available rewards for the current epoch in ETH const availableRewardsForEpochEth = useMemo(() => { + if (!rewardsPerSecond) return; if (!isEligibleForRewards) return; - if (!availableRewardsForEpochEth) return; - if (!isSelectedStakingContractDetailsLoading) return; + if (isSelectedStakingContractDetailsLoading) return; - // wait for service to load - if (!isServiceLoaded) return; + // // wait for service to load + // if (!isServiceLoaded) return; + if (!stakingRewardsDetails) return; - // if agent is not running, return available rewards for the current epoch - if (!isServiceRunning) - return parseFloat(formatUnits(`${availableRewardsForEpoch}`)); + // // if agent is not running, return available rewards for the current epoch + // if (!isServiceRunning) { + // return parseFloat(formatUnits(`${availableRewardsForEpoch}`)); + // } // calculate the time agent staked in the current epoch - const timeWhenAgentStakedInCurrentEpoch = 1; // const rewardsPerSecondInBg = BigNumber.isBigNumber(rewardsPerSecond) // ? rewardsPerSecond @@ -203,20 +202,39 @@ export const RewardProvider = ({ children }: PropsWithChildren) => { // // multiply rewards per second with the time agent staked in the current epoch // const currentEpochRewards = 0; - // if agent is running, calculate rewards earned for the current epoch - const currentEpochRewards = ethers.BigNumber.from( - stakingRewardsDetails.rewardsPerSecondInNumber, - ).mul(timeWhenAgentStakedInCurrentEpoch); + const nextCheckpointTimestamp = + stakingRewardsDetails.lastCheckpointTimestamp + + stakingRewardsDetails.livenessPeriod; - return ethers.utils.formatEther(currentEpochRewards); + // if agent is running, calculate rewards earned for the current epoch + // (tsCheckpoint + livenessPeriod) - tsStartTime) + const stakingDurationInCurrentEpoch = + nextCheckpointTimestamp - + (selectedStakingContractDetails?.serviceStakingStartTime || 0); + + console.log({ + rewardsPerSecond, + serviceStakingStartTime: + selectedStakingContractDetails?.serviceStakingStartTime, + stakingDurationInCurrentEpoch, + nextCheckpointTimestamp, + }); + + const rewardsInCurrentEpochAfterStaking = + formatEther(rewardsPerSecond) * stakingDurationInCurrentEpoch; + + return parseFloat(rewardsInCurrentEpochAfterStaking); }, [ isEligibleForRewards, isSelectedStakingContractDetailsLoading, isServiceLoaded, isServiceRunning, stakingRewardsDetails, + rewardsPerSecond, ]); + console.log({ availableRewardsForEpochEth }); + const optimisticRewardsEarnedForEpoch = useMemo(() => { if (!isEligibleForRewards) return; if (!availableRewardsForEpochEth) return; diff --git a/frontend/service/agents/PredictTrader.ts b/frontend/service/agents/PredictTrader.ts index a13e50e3c..ba3b58bae 100644 --- a/frontend/service/agents/PredictTrader.ts +++ b/frontend/service/agents/PredictTrader.ts @@ -111,23 +111,21 @@ export abstract class PredictTraderService extends StakedAgentService { const minimumStakedAmount = parseFloat(ethers.utils.formatEther(`${minStakingDeposit}`)) * 2; - console.log(rewardsPerSecond.toString()); return { // mechRequestCount, serviceInfo, - livenessPeriod, + livenessPeriod: livenessPeriod.toNumber(), livenessRatio, rewardsPerSecond, - rewardsPerSecondInNumber: rewardsPerSecond - ? Number(rewardsPerSecond.toString()) - : 0, - isEligibleForRewards, + rewardsPerSecondInNumber: Number(rewardsPerSecond.toNumber()), + isEligibleForRewards: isEligibleForRewards || true, // TODO: remove availableRewardsForEpoch, // accruedServiceStakingRewards: accruedStakingReward // ? parseFloat(ethers.utils.formatEther(`${accruedStakingReward}`)) // : 0, accruedServiceStakingRewards: 0, minimumStakedAmount, + lastCheckpointTimestamp: tsCheckpoint.toNumber(), } as StakingRewardsInfo; }; diff --git a/frontend/types/Autonolas.ts b/frontend/types/Autonolas.ts index 5381cf2e6..19362e5ec 100644 --- a/frontend/types/Autonolas.ts +++ b/frontend/types/Autonolas.ts @@ -8,14 +8,15 @@ const zodBigNumber = z.object({ export const StakingRewardsInfoSchema = z.object({ // mechRequestCount: z.number(), serviceInfo: z.array(z.unknown()), - livenessPeriod: zodBigNumber, + livenessPeriod: z.number(), livenessRatio: zodBigNumber, - rewardsPerSecond: zodBigNumber, + rewardsPerSecond: z.number(), rewardsPerSecondInNumber: z.number(), isEligibleForRewards: z.boolean(), availableRewardsForEpoch: z.number(), accruedServiceStakingRewards: z.number(), minimumStakedAmount: z.number(), + lastCheckpointTimestamp: z.number(), }); export type StakingRewardsInfo = z.infer;