From a18bf8df282c45e0f042af90913dedc1b12d5c56 Mon Sep 17 00:00:00 2001 From: m0ar Date: Fri, 27 Sep 2024 16:13:55 +0200 Subject: [PATCH] if resolving a legacy history entry from the dev contract, filter out the duplicate entries --- src/api/v2/resolvers/dpid.ts | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/src/api/v2/resolvers/dpid.ts b/src/api/v2/resolvers/dpid.ts index 0b0d6db..0d49e3c 100644 --- a/src/api/v2/resolvers/dpid.ts +++ b/src/api/v2/resolvers/dpid.ts @@ -3,7 +3,6 @@ import parentLogger from "../../../logger.js"; import { CACHE_TTL_ANCHORED, CACHE_TTL_PENDING, DPID_ENV, getDpidAliasRegistry } from "../../../util/config.js"; import { ResolverError } from "../../../errors.js"; import { getCodexHistory, type HistoryQueryResult, type HistoryVersion } from "../queries/history.js"; -import { BigNumber } from "ethers"; import { getFromCache, setToCache } from "../../../redis.js"; import type { DpidAliasRegistry } from "@desci-labs/desci-contracts/dist/typechain-types/DpidAliasRegistry.js"; @@ -152,7 +151,8 @@ export const resolveDpid = async (dpid: number, versionIx?: number): Promise ({ // No CommitID available version: "", - time: BigNumber.from(time).toNumber(), + time: time.toNumber(), manifest, })), }; @@ -179,5 +179,23 @@ export const resolveDpid = async (dpid: number, versionIx?: number): Promise {} + +type LegacyVersion = DpidAliasRegistry.LegacyVersionStructOutput; + +const undupeIfLegacyDevHistory = (versions: LegacyVersion[]) => { + if (DPID_ENV !== "dev") { + return versions; + } + + return versions.reduce((unduped, current) => { + if (unduped.length === 0 || !isLegacyDupe(current, unduped[unduped.length - 1])) { + unduped.push(current); + } + return unduped; + }, [] as LegacyVersion[]); +}; + +const isLegacyDupe = (a: LegacyVersion, b: LegacyVersion) => { + return a[0] === b[0] && a[1].toNumber() === b[1].toNumber(); +};