Skip to content

Commit

Permalink
fix: undefined in first time agent running notification
Browse files Browse the repository at this point in the history
  • Loading branch information
Atatakai authored and Atatakai committed Jul 15, 2024
1 parent 93cfa1a commit 4d96b58
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 29 deletions.
9 changes: 7 additions & 2 deletions frontend/components/Main/MainHeader/FirstRunModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,21 @@ import { Button, Flex, Modal, Typography } from 'antd';
import Image from 'next/image';
import { FC } from 'react';

import { useReward } from '@/hooks/useReward';
import { useServiceTemplates } from '@/hooks/useServiceTemplates';
import { getMinimumStakedAmountRequired } from '@/utils/service';

const { Title, Paragraph } = Typography;

type FirstRunModalProps = { open: boolean; onClose: () => void };

export const FirstRunModal: FC<FirstRunModalProps> = ({ open, onClose }) => {
const { minimumStakedAmountRequired } = useReward();
const { getServiceTemplates } = useServiceTemplates();

if (!open) return null;

const minimumStakedAmountRequired =
getMinimumStakedAmountRequired(getServiceTemplates()[0]);

return (
<Modal
open={open}
Expand Down
8 changes: 4 additions & 4 deletions frontend/components/Main/MainHeader/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { COLOR } from '@/constants/colors';
import { LOW_BALANCE } from '@/constants/thresholds';
import { useBalance } from '@/hooks/useBalance';
import { useElectronApi } from '@/hooks/useElectronApi';
import { useReward } from '@/hooks/useReward';
import { useServices } from '@/hooks/useServices';
import { useServiceTemplates } from '@/hooks/useServiceTemplates';
import { useStakingContractInfo } from '@/hooks/useStakingContractInfo';
Expand All @@ -19,6 +18,7 @@ import { WalletService } from '@/service/Wallet';

import { requiredGas, requiredOlas } from './constants';
import { FirstRunModal } from './FirstRunModal';
import { getMinimumStakedAmountRequired } from '@/utils/service';

const { Text } = Typography;

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

const { minimumStakedAmountRequired } = useReward();

const { canStartAgent } = useStakingContractInfo();

// hook to setup tray icon
Expand Down Expand Up @@ -170,6 +168,9 @@ export const MainHeader = () => {
if (serviceExists) {
showNotification?.('Your agent is now running!');
} else {
const minimumStakedAmountRequired =
getMinimumStakedAmountRequired(serviceTemplate);

showNotification?.(
`Your agent is running and you've staked ${minimumStakedAmountRequired} OLAS!`,
);
Expand All @@ -186,7 +187,6 @@ export const MainHeader = () => {
}
}, [
masterSafeAddress,
minimumStakedAmountRequired,
serviceTemplate,
services,
setIsBalancePollingPaused,
Expand Down
28 changes: 11 additions & 17 deletions frontend/components/Main/MainNeedsFunds.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,23 @@ import { Flex, Typography } from 'antd';
import { formatUnits } from 'ethers/lib/utils';
import { ReactNode, useEffect, useMemo } from 'react';

import { SERVICE_TEMPLATES } from '@/constants/serviceTemplates';
import { UNICODE_SYMBOLS } from '@/constants/symbols';
import { useBalance } from '@/hooks/useBalance';
import { useElectronApi } from '@/hooks/useElectronApi';
import { useStore } from '@/hooks/useStore';

import { Alert } from '../Alert';
import { CardSection } from '../styled/CardSection';
import { getMinimumStakedAmountRequired } from '@/utils/service';
import { useServiceTemplates } from '@/hooks/useServiceTemplates';

const { Text, Paragraph } = Typography;
const COVER_PREV_BLOCK_BORDER_STYLE = { marginTop: '-1px' };

const useNeedsFunds = () => {
const serviceTemplate = SERVICE_TEMPLATES[0];
const { getServiceTemplates } = useServiceTemplates();
const serviceTemplate = useMemo(() => getServiceTemplates()[0], []);

const { storeState } = useStore();
const { safeBalance, totalOlasStakedBalance } = useBalance();

Expand All @@ -25,24 +28,15 @@ const useNeedsFunds = () => {
const monthlyGasEstimate = Number(
formatUnits(`${serviceTemplate.configuration.monthly_gas_estimate}`, 18),
);
const olasCostOfBond = Number(
formatUnits(`${serviceTemplate.configuration.olas_cost_of_bond}`, 18),
);
const olasRequiredToStake = Number(
formatUnits(
`${serviceTemplate.configuration.olas_required_to_stake}`,
18,
),
);

const minimumStakedAmountRequired =
getMinimumStakedAmountRequired(serviceTemplate);

return {
eth: monthlyGasEstimate,
olas: olasCostOfBond + olasRequiredToStake,
olas: minimumStakedAmountRequired,
};
}, [
serviceTemplate.configuration.monthly_gas_estimate,
serviceTemplate.configuration.olas_cost_of_bond,
serviceTemplate.configuration.olas_required_to_stake,
]);
}, [serviceTemplate]);

const hasEnoughEthForInitialFunding = useMemo(
() => (safeBalance?.ETH || 0) >= (serviceFundRequirements?.eth || 0),
Expand Down
4 changes: 0 additions & 4 deletions frontend/context/RewardProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ export const RewardProvider = ({ children }: PropsWithChildren) => {
const [availableRewardsForEpoch, setAvailableRewardsForEpoch] =
useState<number>();
const [isEligibleForRewards, setIsEligibleForRewards] = useState<boolean>();
const [minimumStakedAmountRequired, setMinimumStakedAmountRequired] =
useState<number>();

const availableRewardsForEpochEth = useMemo<number | undefined>(() => {
if (!availableRewardsForEpoch) return;
Expand Down Expand Up @@ -87,7 +85,6 @@ export const RewardProvider = ({ children }: PropsWithChildren) => {
setAccruedServiceStakingRewards(
stakingRewardsInfo?.accruedServiceStakingRewards,
);
setMinimumStakedAmountRequired(stakingRewardsInfo?.minimumStakedAmount);
setAvailableRewardsForEpoch(rewards);
}, [service]);

Expand All @@ -114,7 +111,6 @@ export const RewardProvider = ({ children }: PropsWithChildren) => {
availableRewardsForEpochEth,
isEligibleForRewards,
optimisticRewardsEarnedForEpoch,
minimumStakedAmountRequired,
updateRewards,
}}
>
Expand Down
2 changes: 0 additions & 2 deletions frontend/hooks/useReward.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@ export const useReward = () => {
availableRewardsForEpoch,
availableRewardsForEpochEth,
isEligibleForRewards,
minimumStakedAmountRequired,
accruedServiceStakingRewards,
} = useContext(RewardContext);

return {
availableRewardsForEpoch,
availableRewardsForEpochEth,
isEligibleForRewards,
minimumStakedAmountRequired,
accruedServiceStakingRewards,
};
};
13 changes: 13 additions & 0 deletions frontend/utils/service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { ServiceTemplate } from "@/client";
import { formatUnits } from "ethers/lib/utils";

export const getMinimumStakedAmountRequired = (serviceTemplate: ServiceTemplate) => {
const olasCostOfBond = Number(
formatUnits(`${serviceTemplate.configuration.olas_cost_of_bond}`, 18)
);
const olasRequiredToStake = Number(
formatUnits(`${serviceTemplate.configuration.olas_required_to_stake}`, 18)
);

return olasCostOfBond + olasRequiredToStake;
};

0 comments on commit 4d96b58

Please sign in to comment.