From d56323160ac34b41c35edc3714b0e67a6a84aac4 Mon Sep 17 00:00:00 2001 From: Hector Rodriguez Fornies Date: Mon, 30 Sep 2024 16:25:53 +0200 Subject: [PATCH] Add version filed to delegate types --- .../address/components/AddressDelegatedTo.tsx | 2 +- modules/app/components/DateWithHover.tsx | 2 +- .../delegates/api/fetchDelegateAddresses.ts | 1 + modules/delegates/api/fetchDelegatedTo.ts | 5 ++- modules/delegates/api/fetchDelegates.ts | 36 ++++++++++++------- modules/delegates/types/delegate.d.ts | 13 ++++--- modules/gql/queries/allDelegates.ts | 1 + modules/gql/queries/delegates.ts | 1 + modules/migration/helpers/expirationChecks.ts | 8 ++--- .../migration/hooks/useMigrationStatus.tsx | 14 ++++---- pages/migration/delegator.tsx | 2 +- 11 files changed, 53 insertions(+), 32 deletions(-) diff --git a/modules/address/components/AddressDelegatedTo.tsx b/modules/address/components/AddressDelegatedTo.tsx index 5dc5a3b35..63553b772 100644 --- a/modules/address/components/AddressDelegatedTo.tsx +++ b/modules/address/components/AddressDelegatedTo.tsx @@ -37,7 +37,7 @@ const CollapsableRow = ({ delegate, network, bpi, totalDelegated }: CollapsableR const { address, lockAmount, events } = delegate; const sortedEvents = events.sort((prev, next) => (prev.blockTimestamp > next.blockTimestamp ? -1 : 1)); - const formattedDate = formatDateWithTime(delegate.expirationDate); + const formattedDate = delegate.expirationDate ? formatDateWithTime(delegate.expirationDate) : ''; const dateText = delegate.isExpired ? `This contract expired ${formattedDate}` : `This contract will expire ${formattedDate}`; diff --git a/modules/app/components/DateWithHover.tsx b/modules/app/components/DateWithHover.tsx index 068d3b5d9..2529eec1a 100644 --- a/modules/app/components/DateWithHover.tsx +++ b/modules/app/components/DateWithHover.tsx @@ -16,7 +16,7 @@ export function DateWithHover({ timeago, label }: { - date: Date | string | number | null; + date?: Date | string | number | null; timeago?: boolean; label?: string; }): React.ReactElement { diff --git a/modules/delegates/api/fetchDelegateAddresses.ts b/modules/delegates/api/fetchDelegateAddresses.ts index 45e476af1..baa03a0d1 100644 --- a/modules/delegates/api/fetchDelegateAddresses.ts +++ b/modules/delegates/api/fetchDelegateAddresses.ts @@ -21,6 +21,7 @@ export async function fetchDelegateAddresses(network: SupportedNetworks): Promis query: allDelegates }); const delegates = data.allDelegates.nodes.map(delegate => ({ + ...delegate, blockTimestamp: new Date(delegate?.blockTimestamp), delegate: delegate?.delegate, voteDelegate: delegate?.voteDelegate diff --git a/modules/delegates/api/fetchDelegatedTo.ts b/modules/delegates/api/fetchDelegatedTo.ts index bd6682324..b189fd829 100644 --- a/modules/delegates/api/fetchDelegatedTo.ts +++ b/modules/delegates/api/fetchDelegatedTo.ts @@ -69,7 +69,10 @@ export async function fetchDelegatedTo( const delegatingToWalletAddress = delegatingTo?.delegate?.toLowerCase(); // Get the expiration date of the delegate - const expirationDate = add(new Date(delegatingTo?.blockTimestamp), { years: 1 }); + const expirationDate = + delegatingTo.version === '2' + ? undefined + : add(new Date(delegatingTo?.blockTimestamp), { years: 1 }); //only v1 delegate contracts expire const isAboutToExpire = delegatingTo.version !== '2' && isAboutToExpireCheck(expirationDate); diff --git a/modules/delegates/api/fetchDelegates.ts b/modules/delegates/api/fetchDelegates.ts index 0ccbef709..3c22badea 100644 --- a/modules/delegates/api/fetchDelegates.ts +++ b/modules/delegates/api/fetchDelegates.ts @@ -57,8 +57,10 @@ function mergeDelegateInfo({ newOnChainDelegate?: DelegateContractInformation; }): Delegate { // check if contract is expired to assing the status - const expirationDate = add(new Date(onChainDelegate.blockTimestamp), { years: 1 }); - const isExpired = isBefore(new Date(expirationDate), new Date()); + const expirationDate = + onChainDelegate.version === '2' ? undefined : add(new Date(onChainDelegate.blockTimestamp), { years: 1 }); + const isExpired = onChainDelegate.version === '2' ? false : isBefore(new Date(expirationDate!), new Date()); + const isAboutToExpire = onChainDelegate.version === '2' ? false : isAboutToExpireCheck(expirationDate); return { voteDelegateAddress: onChainDelegate.voteDelegateAddress, @@ -70,7 +72,7 @@ function mergeDelegateInfo({ : DelegateStatusEnum.shadow, expired: isExpired, expirationDate, - isAboutToExpire: isAboutToExpireCheck(expirationDate), + isAboutToExpire, description: githubDelegate?.description || '', name: githubDelegate?.name || 'Shadow Delegate', picture: githubDelegate?.picture || '', @@ -98,7 +100,8 @@ function mergeDelegateInfo({ address: newOnChainDelegate.address, voteDelegateAddress: newOnChainDelegate.voteDelegateAddress } - }) + }), + version: onChainDelegate.version || '1' }; } @@ -332,15 +335,18 @@ export async function fetchAndMergeDelegates( ) ); - const expirationDate = add(new Date(delegate.blockTimestamp), { years: 1 }); - + const expirationDate = + delegate.version === '2' ? undefined : add(new Date(delegate.blockTimestamp), { years: 1 }); + const expired = + delegate.version === '2' ? false : expirationDate && expirationDate > new Date() ? false : true; + const isAboutToExpire = delegate.version === '2' ? false : isAboutToExpireCheck(expirationDate); return { ...delegate, delegateType: ghDelegate ? DelegateTypeEnum.ALIGNED : DelegateTypeEnum.SHADOW, blockTimestamp: delegate.blockTimestamp, expirationDate, - expired: expirationDate > new Date() ? false : true, - isAboutToExpire: isAboutToExpireCheck(expirationDate), + expired, + isAboutToExpire, name: ghDelegate?.name, picture: ghDelegate?.picture, previous: @@ -389,7 +395,8 @@ export async function fetchSingleDelegateInfo( expired: foundDelegate.expired, isAboutToExpire: foundDelegate.isAboutToExpire, previous: foundDelegate.previous, - next: foundDelegate.next + next: foundDelegate.next, + version: foundDelegate.version }; } @@ -428,7 +435,8 @@ export async function fetchDelegatesInfo( expired: delegate.expired, isAboutToExpire: delegate.isAboutToExpire, previous: delegate.previous, - next: delegate.next + next: delegate.next, + version: delegate.version }; }); @@ -545,9 +553,10 @@ export async function fetchDelegatesPaginated({ ? DelegateStatusEnum.aligned : DelegateStatusEnum.shadow, creationDate: new Date(delegate.creationDate), - expirationDate: new Date(delegate.expirationDate), + expirationDate: delegate.version === '2' ? undefined : new Date(delegate.expirationDate), expired: delegate.expired, - isAboutToExpire: isAboutToExpireCheck(new Date(delegate.expirationDate)), + isAboutToExpire: + delegate.version === '2' ? false : isAboutToExpireCheck(new Date(delegate.expirationDate)), picture: githubDelegate?.picture, communication: githubDelegate?.communication, combinedParticipation: githubDelegate?.combinedParticipation, @@ -560,7 +569,8 @@ export async function fetchDelegatesPaginated({ proposalsSupported: votedProposals?.length || 0, execSupported: execSupported && { title: execSupported.title, address: execSupported.address }, previous: allDelegatesEntry?.previous, - next: allDelegatesEntry?.next + next: allDelegatesEntry?.next, + version: delegate.version }; }) as DelegatePaginated[] }; diff --git a/modules/delegates/types/delegate.d.ts b/modules/delegates/types/delegate.d.ts index 8007bc334..ff0f3330f 100644 --- a/modules/delegates/types/delegate.d.ts +++ b/modules/delegates/types/delegate.d.ts @@ -29,7 +29,7 @@ export type DelegateContractInformation = { mkrDelegated: string; proposalsSupported: number; mkrLockedDelegate: MKRLockedDelegateAPIResponse[]; - version: string; + version?: string | null; lastVoteDate: number | null; }; @@ -44,7 +44,7 @@ export type Delegate = { lastVoteDate: number | null; expired: boolean; isAboutToExpire: boolean; - expirationDate: Date | null; + expirationDate?: Date | null; externalUrl?: string; combinedParticipation?: string; pollParticipation?: string; @@ -56,6 +56,7 @@ export type Delegate = { execSupported: CMSProposal | undefined; mkrLockedDelegate: MKRLockedDelegateAPIResponse[]; blockTimestamp: string; + version?: string | null; previous?: { address: string; voteDelegateAddress: string; @@ -94,7 +95,7 @@ export type DelegationHistory = { }; export type DelegationHistoryWithExpirationDate = DelegationHistory & { - expirationDate: Date; + expirationDate?: Date | null; isAboutToExpire: boolean; isExpired: boolean; isRenewed: boolean; @@ -132,6 +133,7 @@ export type AllDelegatesEntry = { blockTimestamp: Date; delegate: string; voteDelegate: string; + version?: string | null; }; export type AllDelegatesEntryWithName = AllDelegatesEntry & { @@ -139,7 +141,7 @@ export type AllDelegatesEntryWithName = AllDelegatesEntry & { picture?: string; delegateType: DelegateTypeEnum; blockTimestamp: Date; - expirationDate: Date; + expirationDate?: Date | null; expired: boolean; isAboutToExpire: boolean; previous?: { @@ -156,7 +158,7 @@ export type DelegateInfo = Omit - isBefore(new Date(expirationDate), add(new Date(), { days: DAYS_TO_WARN_BEFORE })); +export const isAboutToExpireCheck = (expirationDate?: Date | null): boolean => + expirationDate ? isBefore(new Date(expirationDate), add(new Date(), { days: DAYS_TO_WARN_BEFORE })) : false; -export const isExpiredCheck = (expirationDate: Date): boolean => - isAfter(new Date(), new Date(expirationDate)); +export const isExpiredCheck = (expirationDate?: Date | null): boolean => + expirationDate ? isAfter(new Date(), new Date(expirationDate)) : false; diff --git a/modules/migration/hooks/useMigrationStatus.tsx b/modules/migration/hooks/useMigrationStatus.tsx index 9cfb2fd18..bc21f1912 100644 --- a/modules/migration/hooks/useMigrationStatus.tsx +++ b/modules/migration/hooks/useMigrationStatus.tsx @@ -52,14 +52,16 @@ export function useMigrationStatus(): { const isDelegateV1Contract = !!delegateContractExpirationDate; // TODO: update with version === '1' when available - const isDelegateContractExpiring = delegateContractExpirationDate - ? isAboutToExpireCheck(delegateContractExpirationDate) - : false; + const isDelegateContractExpiring = + isDelegateV1Contract && delegateContractExpirationDate + ? isAboutToExpireCheck(delegateContractExpirationDate) + : false; // check if is delegating to an expired contract, independently of its renewal status - const isDelegateContractExpired = delegateContractExpirationDate - ? isExpiredCheck(delegateContractExpirationDate) - : false; + const isDelegateContractExpired = + isDelegateV1Contract && delegateContractExpirationDate + ? isExpiredCheck(delegateContractExpirationDate) + : false; // Checks if its delegating to an expiring contract that is already renewed. const isDelegatedToExpiringContract = delegatedToData diff --git a/pages/migration/delegator.tsx b/pages/migration/delegator.tsx index 7dfc161b4..59086aecc 100644 --- a/pages/migration/delegator.tsx +++ b/pages/migration/delegator.tsx @@ -74,7 +74,7 @@ export default function DelegateMigrationPage(): React.ReactElement { i => i.address.toLowerCase() === delegate.previous?.address.toLowerCase() ); return ( - !delegate.expirationDate || (!delegate.expired && !delegate.isAboutToExpire && isPreviousDelegate) + delegate.version === '2' || (!delegate.expired && !delegate.isAboutToExpire && isPreviousDelegate) ); }); }, [addressDelegations, delegatesThatAreAboutToExpiry]);