Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Promote to main #39

Merged
merged 8 commits into from
Oct 3, 2024
Next Next commit
if resolving a legacy history entry from the dev contract, filter out…
… the duplicate entries
  • Loading branch information
m0ar committed Sep 27, 2024
commit a18bf8df282c45e0f042af90913dedc1b12d5c56
26 changes: 22 additions & 4 deletions src/api/v2/resolvers/dpid.ts
Original file line number Diff line number Diff line change
@@ -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<His
resolvedEntry = JSON.parse(fromCache);
}

const [owner, versions] = resolvedEntry;
const owner = resolvedEntry[0];
const versions = undupeIfLegacyDevHistory(resolvedEntry[1]);
const requestedVersion = versions[versionIx ?? versions.length - 1];

result = {
@@ -163,7 +163,7 @@ export const resolveDpid = async (dpid: number, versionIx?: number): Promise<His
versions: versions.map(([manifest, time]) => ({
// 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<His
};

type DpidErrorName = "RegistryContactFailed" | "CeramicContactFailed" | "LegacyLookupError" | "DpidNotFound";

export class DpidResolverError extends ResolverError<DpidErrorName> {}

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();
};
Loading