Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add isEligibleForRewards logic, some fixes around loading states #547

Merged
merged 2 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions frontend/config/stakingPrograms/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ export const BASE_STAKING_PROGRAMS: StakingProgramMap = {
stakingRequirements: {
[TokenSymbol.OLAS]: 100,
},
// TODO: find out how the activity is tracked and provide mech if through it
// mech: MECHS[EvmChainId.Base][??].contract,
activityChecker:
ACTIVITY_CHECKERS[EvmChainId.Base][
ActivityCheckerType.MemeActivityChecker
Expand Down
4 changes: 2 additions & 2 deletions frontend/context/RewardProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export const RewardProvider = ({ children }: PropsWithChildren) => {
const {
data: stakingRewardsDetails,
refetch: refetchStakingRewardsDetails,
isFetched: isStakingRewardsDetailsFetched,
isLoading: isStakingRewardsDetailsLoading,
} = useStakingRewardsDetails();
const {
data: availableRewardsForEpoch,
Expand Down Expand Up @@ -177,7 +177,7 @@ export const RewardProvider = ({ children }: PropsWithChildren) => {
isEligibleForRewards,
optimisticRewardsEarnedForEpoch,
updateRewards,
isStakingRewardsDetailsFetched,
isStakingRewardsDetailsFetched: !isStakingRewardsDetailsLoading,
}}
>
{children}
Expand Down
19 changes: 9 additions & 10 deletions frontend/context/ServicesProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@ export const ServicesProvider = ({ children }: PropsWithChildren) => {
const {
data: services,
isError,
isFetched: isServicesFetched,
isLoading,
isLoading: isServicesLoading,
isFetching,
refetch,
} = useQuery<MiddlewareServiceResponse[]>({
Expand All @@ -104,7 +103,7 @@ export const ServicesProvider = ({ children }: PropsWithChildren) => {

const {
data: selectedServiceStatus,
isFetched: isSelectedServiceStatusFetched,
isLoading: isSelectedServiceStatusLoading,
refetch: refetchSelectedServiceStatus,
} = useQuery({
queryKey: REACT_QUERY_KEYS.SERVICE_DEPLOYMENT_STATUS_KEY(
Expand Down Expand Up @@ -161,7 +160,7 @@ export const ServicesProvider = ({ children }: PropsWithChildren) => {
}, [selectedAgentType]);

const serviceWallets: Optional<AgentWallets> = useMemo(() => {
if (!isServicesFetched) return;
if (isServicesLoading) return;
if (isEmpty(services)) return [];

return services?.reduce<AgentWallets>(
Expand Down Expand Up @@ -208,14 +207,14 @@ export const ServicesProvider = ({ children }: PropsWithChildren) => {
},
[],
);
}, [isServicesFetched, services]);
}, [isServicesLoading, services]);

/**
* Select the first service by default
*/
useEffect(() => {
if (!selectedAgentConfig) return;
if (!isServicesFetched) return;
if (isSelectedServiceStatusLoading) return;
if (!services) return;
if (isEmpty(services)) return;

Expand All @@ -227,7 +226,7 @@ export const ServicesProvider = ({ children }: PropsWithChildren) => {

setSelectedServiceConfigId(currentService.service_config_id);
}, [
isServicesFetched,
isSelectedServiceStatusLoading,
selectedServiceConfigId,
services,
selectedAgentConfig,
Expand All @@ -239,8 +238,8 @@ export const ServicesProvider = ({ children }: PropsWithChildren) => {
services,
serviceWallets,
isError,
isFetched: isServicesFetched,
isLoading,
isFetched: !isServicesLoading,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need to change isFetched to isLoading, there are still places where the buttons stuck in infinite loading b/c service is not created and isFetched is always false due to the query having enabled=false

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice 👏

isLoading: isServicesLoading,
isFetching,
refetch,
paused,
Expand All @@ -250,7 +249,7 @@ export const ServicesProvider = ({ children }: PropsWithChildren) => {
selectedService: selectedServiceWithStatus,
selectedServiceStatusOverride,
refetchSelectedServiceStatus,
isSelectedServiceStatusFetched,
isSelectedServiceStatusFetched: !isSelectedServiceStatusLoading,
selectedAgentConfig,
selectedAgentType,
updateAgentType,
Expand Down
33 changes: 13 additions & 20 deletions frontend/service/agents/Memeooor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,31 +40,27 @@ export abstract class MemeooorBaseService extends StakedAgentService {

const provider = PROVIDERS[chainId].multicallProvider;

// TODO: discuss how to calculate the activity
// const mechContract =
// MECHS[chainId][stakingProgramConfig.mechType!].contract;

const contractCalls = [
// mechContract.getRequestsCount(agentMultisigAddress),
stakingTokenProxyContract.getServiceInfo(serviceId),
stakingTokenProxyContract.livenessPeriod(),
activityChecker.livenessRatio(),
stakingTokenProxyContract.rewardsPerSecond(),
stakingTokenProxyContract.calculateStakingReward(serviceId),
stakingTokenProxyContract.minStakingDeposit(),
stakingTokenProxyContract.tsCheckpoint(),
activityChecker.livenessRatio(),
activityChecker.getMultisigNonces(),
];
const multicallResponse = await provider.all(contractCalls);

const [
// mechRequestCount,
serviceInfo,
livenessPeriod,
livenessRatio,
rewardsPerSecond,
accruedStakingReward,
minStakingDeposit,
tsCheckpoint,
livenessRatio,
currentMultisigNonces,
] = multicallResponse;

/**
Expand All @@ -83,19 +79,16 @@ export abstract class MemeooorBaseService extends StakedAgentService {
uint256 inactivity;}
*/

const lastMultisigNonces = serviceInfo[2];
const nowInSeconds = Math.floor(Date.now() / 1000);

// const requiredMechRequests =
// (Math.ceil(Math.max(livenessPeriod, nowInSeconds - tsCheckpoint)) *
// livenessRatio) /
// 1e18 +
// MECH_REQUESTS_SAFETY_MARGIN;

// const mechRequestCountOnLastCheckpoint = serviceInfo[2][1];
// const eligibleRequests =
// mechRequestCount - mechRequestCountOnLastCheckpoint;

// const isEligibleForRewards = eligibleRequests >= requiredMechRequests;
const [isEligibleForRewards] = await provider.all([
activityChecker.isRatioPass(
currentMultisigNonces,
lastMultisigNonces,
Math.ceil(nowInSeconds - tsCheckpoint),
),
]);

const availableRewardsForEpoch = Math.max(
rewardsPerSecond * livenessPeriod, // expected rewards for the epoch
Expand All @@ -112,7 +105,7 @@ export abstract class MemeooorBaseService extends StakedAgentService {
livenessPeriod,
livenessRatio,
rewardsPerSecond,
isEligibleForRewards: false,
isEligibleForRewards,
availableRewardsForEpoch,
accruedServiceStakingRewards: parseFloat(
ethers.utils.formatEther(`${accruedStakingReward}`),
Expand Down
Loading