Skip to content

Commit

Permalink
Add version filed to delegate types
Browse files Browse the repository at this point in the history
  • Loading branch information
torhector2 committed Sep 30, 2024
1 parent 69d36c3 commit d563231
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 32 deletions.
2 changes: 1 addition & 1 deletion modules/address/components/AddressDelegatedTo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;
Expand Down
2 changes: 1 addition & 1 deletion modules/app/components/DateWithHover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions modules/delegates/api/fetchDelegateAddresses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion modules/delegates/api/fetchDelegatedTo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
36 changes: 23 additions & 13 deletions modules/delegates/api/fetchDelegates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 || '',
Expand Down Expand Up @@ -98,7 +100,8 @@ function mergeDelegateInfo({
address: newOnChainDelegate.address,
voteDelegateAddress: newOnChainDelegate.voteDelegateAddress
}
})
}),
version: onChainDelegate.version || '1'
};
}

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
};
}

Expand Down Expand Up @@ -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
};
});

Expand Down Expand Up @@ -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,
Expand All @@ -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[]
};
Expand Down
13 changes: 8 additions & 5 deletions modules/delegates/types/delegate.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export type DelegateContractInformation = {
mkrDelegated: string;
proposalsSupported: number;
mkrLockedDelegate: MKRLockedDelegateAPIResponse[];
version: string;
version?: string | null;
lastVoteDate: number | null;
};

Expand All @@ -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;
Expand All @@ -56,6 +56,7 @@ export type Delegate = {
execSupported: CMSProposal | undefined;
mkrLockedDelegate: MKRLockedDelegateAPIResponse[];
blockTimestamp: string;
version?: string | null;
previous?: {
address: string;
voteDelegateAddress: string;
Expand Down Expand Up @@ -94,7 +95,7 @@ export type DelegationHistory = {
};

export type DelegationHistoryWithExpirationDate = DelegationHistory & {
expirationDate: Date;
expirationDate?: Date | null;
isAboutToExpire: boolean;
isExpired: boolean;
isRenewed: boolean;
Expand Down Expand Up @@ -132,14 +133,15 @@ export type AllDelegatesEntry = {
blockTimestamp: Date;
delegate: string;
voteDelegate: string;
version?: string | null;
};

export type AllDelegatesEntryWithName = AllDelegatesEntry & {
name?: string;
picture?: string;
delegateType: DelegateTypeEnum;
blockTimestamp: Date;
expirationDate: Date;
expirationDate?: Date | null;
expired: boolean;
isAboutToExpire: boolean;
previous?: {
Expand All @@ -156,7 +158,7 @@ export type DelegateInfo = Omit<DelegateRepoInformation, 'externalUrl' | 'descri
address: string;
status: DelegateStatusEnum;
blockTimestamp: Date;
expirationDate: Date;
expirationDate?: Date | null;
expired: boolean;
isAboutToExpire: boolean;
previous?: {
Expand All @@ -167,4 +169,5 @@ export type DelegateInfo = Omit<DelegateRepoInformation, 'externalUrl' | 'descri
address: string;
voteDelegateAddress: string;
};
version?: string | null;
};
1 change: 1 addition & 0 deletions modules/gql/queries/allDelegates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export const allDelegates = gql`
delegate
voteDelegate
blockTimestamp
version
}
}
}
Expand Down
1 change: 1 addition & 0 deletions modules/gql/queries/delegates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export const delegatesQuery = gql`
lastVoted
delegatorCount
totalMkr
version
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions modules/migration/helpers/expirationChecks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import { add, isAfter, isBefore } from 'date-fns';

const DAYS_TO_WARN_BEFORE = 30;

export const isAboutToExpireCheck = (expirationDate: Date): boolean =>
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;
14 changes: 8 additions & 6 deletions modules/migration/hooks/useMigrationStatus.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pages/migration/delegator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down

0 comments on commit d563231

Please sign in to comment.