Skip to content

Commit

Permalink
Merge pull request #323 from valory-xyz/feat/countdown
Browse files Browse the repository at this point in the history
fix: disabled on-chain hash update, added countdown until user can migrate
  • Loading branch information
truemiller authored Sep 9, 2024
2 parents c8bd590 + 8f92be4 commit 3847120
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Button, Flex, Popover, theme, Typography } from 'antd';
import { useMemo, useState } from 'react';
import { useInterval } from 'usehooks-ts';

import { Chain, DeploymentStatus } from '@/client';
import { OpenAddFundsSection } from '@/components/MainPage/sections/AddFundsSection';
Expand All @@ -18,6 +19,7 @@ import { useServiceTemplates } from '@/hooks/useServiceTemplates';
import { useStakingContractInfo } from '@/hooks/useStakingContractInfo';
import { useStakingProgram } from '@/hooks/useStakingProgram';
import { ServicesService } from '@/service/Services';
import { StakingContractInfo } from '@/types/Autonolas';
import { getMinimumStakedAmountRequired } from '@/utils/service';

import { AlertInsufficientMigrationFunds, AlertNoSlots } from './alerts';
Expand Down Expand Up @@ -63,6 +65,9 @@ export const StakingContractSection = ({
const stakingContractInfoForStakingProgram =
stakingContractInfoRecord?.[stakingProgram];

const activeStakingContractInfo =
activeStakingProgram && stakingContractInfoRecord?.[activeStakingProgram];

const activeStakingProgramMeta =
activeStakingProgram && STAKING_PROGRAM_META[activeStakingProgram];

Expand Down Expand Up @@ -137,8 +142,12 @@ export const StakingContractSection = ({
return 'No available staking slots';
}

if (!isServiceStakedForMinimumDuration) {
return 'Service has not been staked for the minimum duration';
if (!isServiceStakedForMinimumDuration && activeStakingContractInfo) {
return (
<CountdownUntilMigration
activeStakingContractInfo={activeStakingContractInfo}
/>
);
}

if (!hasEnoughOlasToMigrate) {
Expand All @@ -162,7 +171,8 @@ export const StakingContractSection = ({
return 'Pearl is currently stopping, please wait before switching';
}
}, [
activeStakingProgramMeta,
activeStakingContractInfo,
activeStakingProgramMeta?.canMigrateTo,
hasEnoughOlasToMigrate,
hasEnoughRewards,
hasEnoughSlots,
Expand Down Expand Up @@ -315,3 +325,62 @@ export const StakingContractSection = ({
</>
);
};

const CountdownUntilMigration = ({
activeStakingContractInfo,
}: {
activeStakingContractInfo: Partial<StakingContractInfo>;
}) => {
const [secondsUntilReady, setSecondsUntilMigration] = useState<number>();

useInterval(() => {
if (!activeStakingContractInfo) return;

const { serviceStakingStartTime, minimumStakingDuration } =
activeStakingContractInfo;

if (!minimumStakingDuration) return;
if (!serviceStakingStartTime) return;

const now = Math.round(Date.now() / 1000);
const timeSinceLastStaked = now - serviceStakingStartTime;

const timeUntilMigration = minimumStakingDuration - timeSinceLastStaked;

if (timeUntilMigration < 0) {
setSecondsUntilMigration(0);
return;
}

setSecondsUntilMigration(timeUntilMigration);
}, 1000);

if (!secondsUntilReady) return "You're ready to switch contracts!"; // Shouldn't happen, but just in case

return (
<Flex vertical gap={1}>
<strong>Can&apos;t switch because you unstaked too recently.</strong>
<span>This may be because your agent was suspended.</span>
<span>Keep running your agent and you&apos;ll be able to switch in</span>
<span>{countdownDisplayFormat(secondsUntilReady)}</span>
</Flex>
);
};

const countdownDisplayFormat = (totalSeconds: number) => {
const days = Math.floor(totalSeconds / (24 * 3600));
totalSeconds %= 24 * 3600;

const hours = Math.floor(totalSeconds / 3600);
totalSeconds %= 3600;

const minutes = Math.floor(totalSeconds / 60);
const seconds = totalSeconds % 60;

// Ensure double digits for hours, minutes, and seconds
const formattedHours = String(hours).padStart(2, '0');
const formattedMinutes = String(minutes).padStart(2, '0');
const formattedSeconds = String(seconds).padStart(2, '0');

return `${days} days ${formattedHours} hours ${formattedMinutes} minutes ${formattedSeconds} seconds`;
};
5 changes: 3 additions & 2 deletions operate/services/manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -559,8 +559,9 @@ def _deploy_service_onchain_from_safe( # pylint: disable=too-many-statements,to
(not is_first_mint)
and (on_chain_hash is not None)
and (
on_chain_hash != service.hash
or current_agent_id != staking_params["agent_ids"][0]
# TODO Discuss how to manage on-chain hash updates with staking programs.
# on_chain_hash != service.hash or # noqa
current_agent_id != staking_params["agent_ids"][0]
or current_agent_bond != staking_params["min_staking_deposit"]
)
)
Expand Down

0 comments on commit 3847120

Please sign in to comment.