Skip to content

Commit

Permalink
Merge pull request #235 from valory-xyz/mohan/eviction-fix
Browse files Browse the repository at this point in the history
feat: "Start Agent" button fix
  • Loading branch information
mohandast52 authored Jul 19, 2024
2 parents 2b13bc2 + f105a3a commit 72ebd15
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 42 deletions.
4 changes: 2 additions & 2 deletions frontend/components/Main/MainHeader/CannotStartAgent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@ const NoJobsAvailablePopover = () => (

export const CannotStartAgent = () => {
const {
canStartAgent,
isEligibleForStakingAction,
hasEnoughServiceSlots,
isRewardsAvailable,
isAgentEvicted,
} = useStakingContractInfo();

if (canStartAgent) return null;
if (isEligibleForStakingAction) return null;
if (!hasEnoughServiceSlots) return <NoJobsAvailablePopover />;
if (!isRewardsAvailable) return <NoRewardsAvailablePopover />;
if (isAgentEvicted) return <AgentEvictedPopover />;
Expand Down
62 changes: 29 additions & 33 deletions frontend/components/Main/MainHeader/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { InfoCircleOutlined } from '@ant-design/icons';
import { Badge, Button, Flex, Popover, Skeleton, Typography } from 'antd';
import {
Badge,
Button,
ButtonProps,
Flex,
Popover,
Skeleton,
Typography,
} from 'antd';
import Image from 'next/image';
import { useCallback, useEffect, useMemo, useState } from 'react';

Expand Down Expand Up @@ -86,8 +94,11 @@ export const MainHeader = () => {
const [isModalOpen, setIsModalOpen] = useState(false);
const handleModalClose = useCallback(() => setIsModalOpen(false), []);

const { isInitialStakingLoad, isAgentEvicted, canStartAgent } =
useStakingContractInfo();
const {
isInitialStakingLoad,
isEligibleForStakingAction,
canStartEvictedAgent,
} = useStakingContractInfo();

// hook to setup tray icon
useSetupTrayIcon();
Expand Down Expand Up @@ -208,16 +219,6 @@ export const MainHeader = () => {
}, [services, setServiceStatus]);

const serviceToggleButton = useMemo(() => {
if (!canStartAgent) return <CannotStartAgent />;

if (canStartAgent && isAgentEvicted) {
return (
<Button type="primary" size="large" onClick={handleStart}>
Start agent
</Button>
);
}

if (serviceButtonState === ServiceButtonLoadingState.Pausing) {
return (
<Button type="default" size="large" ghost disabled loading>
Expand Down Expand Up @@ -246,6 +247,8 @@ export const MainHeader = () => {
);
}

if (!isEligibleForStakingAction) return <CannotStartAgent />;

if (!isBalanceLoaded) {
return (
<Button type="primary" size="large" disabled>
Expand All @@ -269,32 +272,25 @@ export const MainHeader = () => {
if (services[0] && storeState?.isInitialFunded)
return safeOlasBalanceWithStaked >= requiredOlas; // at present agent will always require staked/bonded OLAS (or the ability to stake/bond)

// case if agent is evicted and user has met the staking criteria
if (canStartEvictedAgent) return true;

return (
safeOlasBalanceWithStaked >= requiredOlas &&
totalEthBalance > requiredGas
);
})();

const serviceExists = !!services?.[0];

if (!isDeployable) {
return (
<Button type="default" size="large" disabled>
Start agent {!serviceExists && '& stake'}
</Button>
);
}
const buttonProps: ButtonProps = {
type: 'primary',
size: 'large',
disabled: !isDeployable,
onClick: isDeployable ? handleStart : undefined,
};
const buttonText = `Start agent ${serviceExists ? '' : '& stake'}`;

return (
<Button
type="primary"
size="large"
disabled={!canStartAgent}
onClick={handleStart}
>
Start agent {!serviceExists && '& stake'}
</Button>
);
return <Button {...buttonProps}>{buttonText}</Button>;
}, [
handlePause,
handleStart,
Expand All @@ -305,8 +301,8 @@ export const MainHeader = () => {
services,
storeState?.isInitialFunded,
totalEthBalance,
canStartAgent,
isAgentEvicted,
isEligibleForStakingAction,
canStartEvictedAgent,
]);

return (
Expand Down
17 changes: 10 additions & 7 deletions frontend/context/StakingContractInfoProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ type StakingContractInfoContextProps = {
isInitialStakingLoad: boolean;
isRewardsAvailable: boolean;
hasEnoughServiceSlots: boolean;
isAgentEvicted: boolean; // TODO: Implement this
canStartAgent: boolean;
isAgentEvicted: boolean;
isEligibleForStakingAction: boolean;
canStartEvictedAgent: boolean;
};

export const StakingContractInfoContext =
Expand All @@ -29,7 +30,8 @@ export const StakingContractInfoContext =
isRewardsAvailable: false,
hasEnoughServiceSlots: false,
isAgentEvicted: false,
canStartAgent: false,
isEligibleForStakingAction: false,
canStartEvictedAgent: false,
});

export const StakingContractInfoProvider = ({
Expand All @@ -42,7 +44,7 @@ export const StakingContractInfoProvider = ({
const [isRewardsAvailable, setIsRewardsAvailable] = useState(false);
const [hasEnoughServiceSlots, setHasEnoughServiceSlots] = useState(false);
const [isAgentEvicted, setIsAgentEvicted] = useState(false);
const [canStartAgent, setCanStartAgent] = useState(false);
const [isEligibleForStakingAction, setCanStartAgent] = useState(false);

const updateStakingContractInfo = useCallback(async () => {
try {
Expand Down Expand Up @@ -87,13 +89,13 @@ export const StakingContractInfoProvider = ({
* - service has enough slots
* - if agent is evicted, then service should be staked for minimum duration
*/
const canStartAgent =
const isEligibleForStakingAction =
hasEnoughRewardsAndSlots &&
(isAgentEvicted ? isServiceStakedForMinimumDuration : true);

setIsRewardsAvailable(isRewardsAvailable);
setHasEnoughServiceSlots(hasEnoughServiceSlots);
setCanStartAgent(canStartAgent);
setCanStartAgent(isEligibleForStakingAction);
setIsAgentEvicted(isAgentEvicted);
setStakingLoadCount((prev) => prev + 1);
} catch (error) {
Expand All @@ -111,7 +113,8 @@ export const StakingContractInfoProvider = ({
isRewardsAvailable,
hasEnoughServiceSlots,
isAgentEvicted,
canStartAgent,
isEligibleForStakingAction,
canStartEvictedAgent: isEligibleForStakingAction && isAgentEvicted,
}}
>
{children}
Expand Down

0 comments on commit 72ebd15

Please sign in to comment.