Skip to content

Commit

Permalink
Merge pull request #622 from valory-xyz/mohan/fix-todo
Browse files Browse the repository at this point in the history
fix: Few todos and bugs
  • Loading branch information
mohandast52 authored Dec 20, 2024
2 parents 478d6ba + 1b58c84 commit ae71545
Show file tree
Hide file tree
Showing 11 changed files with 72 additions and 79 deletions.
17 changes: 14 additions & 3 deletions frontend/components/AddressLink.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
import { ReactNode } from 'react';

import { MiddlewareChain } from '@/client';
import { UNICODE_SYMBOLS } from '@/constants/symbols';
import { EXPLORER_URL_BY_MIDDLEWARE_CHAIN } from '@/constants/urls';
import { Address } from '@/types/Address';
import { truncateAddress } from '@/utils/truncate';

type AddressLinkProps = {
address?: Address;
hideLinkArrow?: boolean;
address: Address;
middlewareChain: MiddlewareChain;
prefix?: ReactNode;
hideLinkArrow?: boolean;
};

export const AddressLink = ({
address,
hideLinkArrow = false,
prefix,
middlewareChain,
}: AddressLinkProps) => {
if (!address) return null;
Expand All @@ -23,7 +27,14 @@ export const AddressLink = ({
target="_blank"
href={`${EXPLORER_URL_BY_MIDDLEWARE_CHAIN[middlewareChain]}/address/${address}`}
>
{truncateAddress(address)}
{prefix ? (
<>
&nbsp;
{prefix}
</>
) : (
truncateAddress(address)
)}

{hideLinkArrow ? null : (
<>
Expand Down
21 changes: 13 additions & 8 deletions frontend/components/MainPage/modals/MigrationModal.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import { Button, Flex, Modal, Typography } from 'antd';
import Image from 'next/image';

import { UNICODE_SYMBOLS } from '@/constants/symbols';
import { AddressLink } from '@/components/AddressLink';
import { MODAL_WIDTH } from '@/constants/width';
import { useServices } from '@/hooks/useServices';
import { useStakingProgram } from '@/hooks/useStakingProgram';

const { Title, Text, Link } = Typography;
const { Title, Text } = Typography;

type MigrationModalProps = { open: boolean; onClose: () => void };
export const MigrationSuccessModal = ({
open,
onClose,
}: MigrationModalProps) => {
const { activeStakingProgramMeta } = useStakingProgram();
const { selectedAgentConfig } = useServices();
const { activeStakingProgramMeta, activeStakingProgramAddress } =
useStakingProgram();

// Close modal if no active staking program, migration doesn't apply to non-stakers
if (!activeStakingProgramMeta) {
Expand All @@ -39,7 +42,6 @@ export const MigrationSuccessModal = ({
]}
>
<Flex gap={8} vertical>
{/* Robot head */}
<Flex align="center" justify="center">
<Image
src="/splash-robot-head.png"
Expand All @@ -52,10 +54,13 @@ export const MigrationSuccessModal = ({
<Text>
Your agent is now staked on {activeStakingProgramMeta.name}.
</Text>
{/* TODO: Add relevant block explorer domain */}
<Link href="#">
View full contract details {UNICODE_SYMBOLS.EXTERNAL_LINK}
</Link>
{activeStakingProgramAddress && (
<AddressLink
address={activeStakingProgramAddress}
middlewareChain={selectedAgentConfig.middlewareHomeChainId}
prefix="View full contract details"
/>
)}
</Flex>
</Modal>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
ServiceStakingDetails,
StakingContractDetails,
} from '@/types/Autonolas';
import { formatCountdownDisplay } from '@/utils/time';

const { Text } = Typography;

Expand Down Expand Up @@ -50,7 +51,7 @@ export const CountdownUntilMigration = ({

const countdownDisplay = isNil(secondsUntilReady)
? 'Loading...'
: countdownDisplayFormat(secondsUntilReady);
: formatCountdownDisplay(secondsUntilReady);

return (
<Flex vertical gap={1} style={{ maxWidth: POPOVER_WIDTH_LARGE }}>
Expand All @@ -61,21 +62,3 @@ export const CountdownUntilMigration = ({
</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');
// TODO: update this support plural and singular text
return `${days} days ${formattedHours} hours ${formattedMinutes} minutes ${formattedSeconds} seconds`;
};
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { Flex, Tag, theme, Typography } from 'antd';
import { useMemo } from 'react';

import { AddressLink } from '@/components/AddressLink';
import { CardSection } from '@/components/styled/CardSection';
import { GNOSIS_STAKING_PROGRAMS_CONTRACT_ADDRESSES } from '@/config/stakingPrograms/gnosis';
import { UNICODE_SYMBOLS } from '@/constants/symbols';
import { EXPLORER_URL_BY_EVM_CHAIN_ID } from '@/constants/urls';
import { STAKING_PROGRAM_ADDRESS } from '@/config/stakingPrograms';
import { StakingProgramId } from '@/enums/StakingProgram';
import { StakingProgramStatus } from '@/enums/StakingProgramStatus';
import { useServices } from '@/hooks/useServices';
Expand Down Expand Up @@ -115,15 +114,11 @@ export const StakingContractSection = ({
<StakingContractDetails stakingProgramId={stakingProgramId} />

{evmChainId && (
<a
href={`${EXPLORER_URL_BY_EVM_CHAIN_ID[evmChainId]}/address/${
// TODO: make chain independent
GNOSIS_STAKING_PROGRAMS_CONTRACT_ADDRESSES[stakingProgramId]
}`}
target="_blank"
>
View contract details {UNICODE_SYMBOLS.EXTERNAL_LINK}
</a>
<AddressLink
address={STAKING_PROGRAM_ADDRESS[evmChainId][stakingProgramId]}
middlewareChain={selectedAgentConfig.middlewareHomeChainId}
prefix="View contract details"
/>
)}

{!migrateValidation.canMigrate && (
Expand Down
2 changes: 1 addition & 1 deletion frontend/components/SettingsPage/DebugInfoSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ const AgentStakingInfo = () => {
};

export const DebugInfoSection = () => {
const [isModalOpen, setIsModalOpen] = useState(true);
const [isModalOpen, setIsModalOpen] = useState(false);
const showModal = useCallback(() => setIsModalOpen(true), []);
const handleCancel = useCallback(() => setIsModalOpen(false), []);

Expand Down
2 changes: 1 addition & 1 deletion frontend/components/YourWalletPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const Address = () => {
{
left: 'Address',
leftClassName: 'text-light',
right: (
right: masterSafeAddress && (
<AddressLink
address={masterSafeAddress}
middlewareChain={middlewareChain}
Expand Down
1 change: 0 additions & 1 deletion frontend/config/agents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ export const AGENT_CONFIG: {
displayName: 'Prediction agent',
description: 'Participates in prediction markets.',
},
// TODO: check optimus config
// [AgentType.Optimus]: {
// name: 'Optimus',
// homeChainId: ChainId.Optimism,
Expand Down
8 changes: 0 additions & 8 deletions frontend/constants/urls.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { MiddlewareChain } from '@/client';
import { EvmChainId } from '@/enums/Chain';
import { asEvmChainId } from '@/utils/middlewareHelpers';

type Url = `http${'s' | ''}://${string}`;

Expand Down Expand Up @@ -65,10 +64,3 @@ export const SWAP_URL_BY_EVM_CHAIN: Record<number | EvmChainId, Url> = {
// [EvmChainId.OPTIMISM]: COW_SWAP_GNOSIS_XDAI_OLAS_URL,
[EvmChainId.Base]: SWAP_BASE_URL,
};

export const EXPLORER_URL_BY_EVM_CHAIN_ID: Record<number | EvmChainId, Url> =
Object.fromEntries(
Object.entries(EXPLORER_URL_BY_MIDDLEWARE_CHAIN).map(
([middlewareChain, url]) => [asEvmChainId(middlewareChain), url],
),
);
4 changes: 1 addition & 3 deletions frontend/hooks/useRewardsHistory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@ import { EvmChainId } from '@/enums/Chain';
import { Address } from '@/types/Address';
import { Nullable } from '@/types/Util';
import { asMiddlewareChain } from '@/utils/middlewareHelpers';
import { ONE_DAY_IN_MS } from '@/utils/time';

import { useService } from './useService';
import { useServices } from './useServices';

const ONE_DAY_IN_S = 24 * 60 * 60;
const ONE_DAY_IN_MS = ONE_DAY_IN_S * 1000;

const CheckpointGraphResponseSchema = z.object({
epoch: z.string({
message: 'Expected epoch to be a string',
Expand Down
25 changes: 2 additions & 23 deletions frontend/hooks/useStakingContractCountdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,7 @@ import {
StakingContractDetails,
} from '@/types/Autonolas';
import { Maybe } from '@/types/Util';

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');

const daysInWords = `${days} day${days !== 1 ? 's' : ''}`;
const hoursInWords = `${formattedHours} hour${hours !== 1 ? 's' : ''}`;
const minutesInWords = `${formattedMinutes} minute${minutes !== 1 ? 's' : ''}`;
const secondsInWords = `${formattedSeconds} second${seconds !== 1 ? 's' : ''}`;
return `${daysInWords} ${hoursInWords} ${minutesInWords} ${secondsInWords}`.trim();
};
import { formatCountdownDisplay } from '@/utils/time';

export const useStakingContractCountdown = ({
currentStakingContractInfo,
Expand Down Expand Up @@ -69,7 +48,7 @@ export const useStakingContractCountdown = ({

const countdownDisplay = isNil(secondsUntilReady)
? 'Loading...'
: countdownDisplayFormat(secondsUntilReady);
: formatCountdownDisplay(secondsUntilReady);

return countdownDisplay;
};
31 changes: 31 additions & 0 deletions frontend/utils/time.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { isNumber } from 'lodash';

export const ONE_DAY_IN_S = 24 * 60 * 60;
export const ONE_DAY_IN_MS = ONE_DAY_IN_S * 1000;

export const getTimeAgo = (timestampInSeconds: number) => {
if (!isNumber(timestampInSeconds)) return null;

Expand Down Expand Up @@ -65,3 +68,31 @@ export const formatToShortDateTime = (timeInMs?: number) => {
timeZone: 'UTC',
});
};

/**
*
* @param totalSeconds - total seconds to be formatted
* @returns formatted string in the format of 'X days X hours X minutes X seconds'
* @example 100000 => '1 day 3 hours 46 minutes 40 seconds'
*/
export const formatCountdownDisplay = (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');

const daysInWords = `${days} day${days !== 1 ? 's' : ''}`;
const hoursInWords = `${formattedHours} hour${hours !== 1 ? 's' : ''}`;
const minutesInWords = `${formattedMinutes} minute${minutes !== 1 ? 's' : ''}`;
const secondsInWords = `${formattedSeconds} second${seconds !== 1 ? 's' : ''}`;
return `${daysInWords} ${hoursInWords} ${minutesInWords} ${secondsInWords}`.trim();
};

0 comments on commit ae71545

Please sign in to comment.