Skip to content

Commit

Permalink
feat: enhance RewardProvider with service and staking contract contex…
Browse files Browse the repository at this point in the history
…t integration (lots going on)
  • Loading branch information
mohandast52 committed Feb 7, 2025
1 parent ee59009 commit a3c4098
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 9 deletions.
63 changes: 59 additions & 4 deletions frontend/context/RewardProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useQuery } from '@tanstack/react-query';
import { ethers } from 'ethers';
import { formatUnits } from 'ethers/lib/utils';
import { isNil } from 'lodash';
import {
Expand All @@ -14,7 +15,9 @@ import { FIVE_SECONDS_INTERVAL } from '@/constants/intervals';
import { REACT_QUERY_KEYS } from '@/constants/react-query-keys';
import { useElectronApi } from '@/hooks/useElectronApi';
import { useOnlineStatusContext } from '@/hooks/useOnlineStatus';
import { useService } from '@/hooks/useService';
import { useServices } from '@/hooks/useServices';
import { useStakingContractContext } from '@/hooks/useStakingContractDetails';
import { useStore } from '@/hooks/useStore';
import { StakingRewardsInfoSchema } from '@/types/Autonolas';
import { asMiddlewareChain } from '@/utils/middlewareHelpers';
Expand Down Expand Up @@ -147,6 +150,16 @@ export const RewardProvider = ({ children }: PropsWithChildren) => {
isLoading: isStakingRewardsDetailsLoading,
} = useStakingRewardsDetails();

const { selectedService } = useServices();
const { isLoaded: isServiceLoaded, isServiceRunning } = useService(
selectedService?.service_config_id,
);

const {
isSelectedStakingContractDetailsLoading,
selectedStakingContractDetails,
} = useStakingContractContext();

const {
data: availableRewardsForEpoch,
isLoading: isAvailableRewardsForEpochLoading,
Expand All @@ -157,19 +170,61 @@ export const RewardProvider = ({ children }: PropsWithChildren) => {
const accruedServiceStakingRewards =
stakingRewardsDetails?.accruedServiceStakingRewards;

const rewardsPerSecond = stakingRewardsDetails?.rewardsPerSecondInNumber;
// 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<number | undefined>(() => {
if (!availableRewardsForEpoch) return;
return parseFloat(formatUnits(`${availableRewardsForEpoch}`));
}, [availableRewardsForEpoch]);
if (!isEligibleForRewards) return;
if (!availableRewardsForEpochEth) return;
if (!isSelectedStakingContractDetailsLoading) return;

// wait for service to load
if (!isServiceLoaded) return;

// 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
// : ethers.BigNumber.from(0);
// console.log(rewardsPerSecondInBg);
// // rewardsPerSecondInBg.mul(timeWhenAgentStakedInCurrentEpoch).toS;
// // 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);

return ethers.utils.formatEther(currentEpochRewards);
}, [
isEligibleForRewards,
isSelectedStakingContractDetailsLoading,
isServiceLoaded,
isServiceRunning,
stakingRewardsDetails,
]);

// optimistic rewards earned for the current epoch in ETH
const optimisticRewardsEarnedForEpoch = useMemo<number | undefined>(() => {
if (!isEligibleForRewards) return;
if (!availableRewardsForEpochEth) return;
return availableRewardsForEpochEth;
}, [availableRewardsForEpochEth, isEligibleForRewards]);

// console.log({ stakingRewardsDetails });

// store the first staking reward achieved in the store for notification
useEffect(() => {
if (!isEligibleForRewards) return;
Expand Down
15 changes: 10 additions & 5 deletions frontend/service/agents/PredictTrader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export abstract class PredictTraderService extends StakedAgentService {
stakingTokenProxyContract.livenessPeriod(),
activityChecker.livenessRatio(),
stakingTokenProxyContract.rewardsPerSecond(),
stakingTokenProxyContract.calculateStakingReward(serviceId),
// stakingTokenProxyContract.calculateStakingReward(serviceId),
stakingTokenProxyContract.minStakingDeposit(),
stakingTokenProxyContract.tsCheckpoint(),
];
Expand All @@ -66,7 +66,7 @@ export abstract class PredictTraderService extends StakedAgentService {
livenessPeriod,
livenessRatio,
rewardsPerSecond,
accruedStakingReward,
// accruedStakingReward,
minStakingDeposit,
tsCheckpoint,
] = multicallResponse;
Expand Down Expand Up @@ -111,17 +111,22 @@ export abstract class PredictTraderService extends StakedAgentService {
const minimumStakedAmount =
parseFloat(ethers.utils.formatEther(`${minStakingDeposit}`)) * 2;

console.log(rewardsPerSecond.toString());
return {
// mechRequestCount,
serviceInfo,
livenessPeriod,
livenessRatio,
rewardsPerSecond,
rewardsPerSecondInNumber: rewardsPerSecond
? Number(rewardsPerSecond.toString())
: 0,
isEligibleForRewards,
availableRewardsForEpoch,
accruedServiceStakingRewards: parseFloat(
ethers.utils.formatEther(`${accruedStakingReward}`),
),
// accruedServiceStakingRewards: accruedStakingReward
// ? parseFloat(ethers.utils.formatEther(`${accruedStakingReward}`))
// : 0,
accruedServiceStakingRewards: 0,
minimumStakedAmount,
} as StakingRewardsInfo;
};
Expand Down
1 change: 1 addition & 0 deletions frontend/types/Autonolas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const StakingRewardsInfoSchema = z.object({
livenessPeriod: zodBigNumber,
livenessRatio: zodBigNumber,
rewardsPerSecond: zodBigNumber,
rewardsPerSecondInNumber: z.number(),
isEligibleForRewards: z.boolean(),
availableRewardsForEpoch: z.number(),
accruedServiceStakingRewards: z.number(),
Expand Down

0 comments on commit a3c4098

Please sign in to comment.