Skip to content

Commit

Permalink
feat: Add canStartAgent check to MainHeader component
Browse files Browse the repository at this point in the history
  • Loading branch information
mohandast52 committed May 31, 2024
1 parent 1cafb91 commit df6c489
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
11 changes: 10 additions & 1 deletion frontend/components/Main/MainHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import { useWallet } from '@/hooks/useWallet';
import { ServicesService } from '@/service';
import { WalletService } from '@/service/Wallet';

import { useStakingContractInfo } from '../store/stackingContractInfo';

const { Text } = Typography;

const LOADING_MESSAGE =
Expand All @@ -38,6 +40,7 @@ export const MainHeader = () => {
isBalanceLoaded,
setIsPaused: setIsBalancePollingPaused,
} = useBalance();
const { canStartAgent } = useStakingContractInfo();

const safeOlasBalanceWithStaked = useMemo(() => {
if (safeBalance?.OLAS === undefined) return;
Expand Down Expand Up @@ -259,7 +262,12 @@ export const MainHeader = () => {
}

return (
<Button type="primary" size="large" onClick={handleStart}>
<Button
type="primary"
size="large"
onClick={handleStart}
disabled={!canStartAgent}
>
Start agent
</Button>
);
Expand All @@ -273,6 +281,7 @@ export const MainHeader = () => {
services,
storeState?.isInitialFunded,
totalEthBalance,
canStartAgent,
]);

return (
Expand Down
12 changes: 8 additions & 4 deletions frontend/components/store/stackingContractInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ const initialState = {
isStakingContractInfoLoading: true,
isRewardsAvailable: false,
hasEnoughServiceSlots: false,
canStartAgent: false,
maxNumServices: 0,
};

export const useStakingContractInfo = create<{
isStakingContractInfoLoading: boolean;
isRewardsAvailable: boolean;
hasEnoughServiceSlots: boolean;
canStartAgent: boolean;
fetchStakingContractInfo: () => Promise<void>;
maxNumServices: number;
}>((set) => {
Expand All @@ -31,13 +33,15 @@ export const useStakingContractInfo = create<{
if (!info) return;

const { availableRewards, maxNumServices, getServiceIds } = info;
const isRewardsAvailable = availableRewards > 0;
const hasEnoughServiceSlots = getServiceIds.length < maxNumServices;
const canStartAgent = isRewardsAvailable && hasEnoughServiceSlots;

set({
// availableRewards,
maxNumServices,
// getServiceIds,
isRewardsAvailable: availableRewards > 0,
hasEnoughServiceSlots: getServiceIds.length < maxNumServices,
isRewardsAvailable,
hasEnoughServiceSlots,
canStartAgent,
});
} catch (error) {
console.error('Failed to fetch staking contract info', error);
Expand Down

0 comments on commit df6c489

Please sign in to comment.