Skip to content

Commit

Permalink
refactor: ♻️ Add Hypercert reports server route and Report interface
Browse files Browse the repository at this point in the history
  • Loading branch information
thebeyondr authored and baumstern committed Jan 23, 2024
1 parent 20f5602 commit f4abd31
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
103 changes: 103 additions & 0 deletions app/routes/hypercert-reports.server..tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import {
ClaimsByOwnerQuery,
HypercertClient,
HypercertIndexerInterface,
HypercertMetadata,
HypercertStorageInterface,
} from "@hypercerts-org/sdk";
import type { LoaderFunctionArgs } from "@remix-run/node";
import { Report } from "~/types";

const createHypercertClient = () =>
new HypercertClient({ chain: { id: 11155111 } }); // Sepolia testnet

const getHypercertClaims = async (indexer: HypercertIndexerInterface) => {
let claims: ClaimsByOwnerQuery["claims"] | null;
try {
if (!process.env.HC_OWNER_ADDRESS) {
throw new Error("Owner address environment variable is not set");
}
const response = await indexer.claimsByOwner(
process.env.HC_OWNER_ADDRESS as string,
);
claims = (response as ClaimsByOwnerQuery).claims;
} catch (error) {
console.error("Failed to fetch claims by owner", error);
throw new Error("Failed to fetch claims");
}

return claims;
};

const getHypercertMetadata = async (
claimUri: string,
storage: HypercertStorageInterface,
) => {
let metadata: HypercertMetadata | null;

try {
const response = await storage.getMetadata(claimUri);
metadata = response;
} catch (error) {
console.error("Failed to fetch metadata", error);
throw new Error("Failed to fetch metadata");
}

return metadata;
};

const getHypercertReports = async (
indexer: HypercertIndexerInterface,
storage: HypercertStorageInterface,
) => {
const reports = new Array<Report>();
const errors = [];
try {
const claims = await getHypercertClaims(indexer);

for (const claim of claims) {
try {
const metadata: HypercertMetadata = await getHypercertMetadata(
claim.uri as string,
storage,
);

reports.push({
title: metadata.name,
description: metadata.description,
id: claim.id,
});
} catch (error) {
errors.push(
`Failed to fetch metadata for claim with ID ${claim.id}: ${error}`,
);
}
}

if (errors.length > 0) {
console.error(
"Errors occurred while fetching metadata for claims:",
errors,
);
}

return new Response(JSON.stringify(reports), {
status: 200,
statusText: "OK",
});
} catch (error) {
console.error("Failed to fetch hypercert claims:", error);
return new Response(
JSON.stringify({ error: "Failed to fetch hypercert claims" }),
{
status: 500,
statusText: "Could not fetch hypercert reports",
},
);
}
};

export const loader = async ({ request }: LoaderFunctionArgs) => {
const { indexer, storage } = new HypercertClient({ chain: { id: 11155111 } });
const response = await getHypercertReports(indexer, storage);
};
7 changes: 7 additions & 0 deletions app/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export interface Report {
id: string;
title: string;
description: string;
created_at?: string;
updated_at?: string;
}

0 comments on commit f4abd31

Please sign in to comment.