Skip to content

Commit

Permalink
(feature): add some validation and update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Jipperism committed May 7, 2024
1 parent 02f1b14 commit 8061f04
Showing 1 changed file with 26 additions and 19 deletions.
45 changes: 26 additions & 19 deletions src/storage/storeClaim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { supabase } from "@/clients/supabaseClient";
import { Tables } from "@/types/database.types";
import { NewClaim } from "@/types/types";
import { isAddress } from "viem";
import { z } from "zod";

/*
This function stores the chain, contract address, token ID, metadata and URI of a hypercert in the database.
Expand Down Expand Up @@ -36,27 +37,33 @@ export const storeClaim = async ({ claims }: StoreClaim) => {
return;
}

// TODO validations
if (claims.some((claim) => !isAddress(claim.creator_address))) {
const erroneousClaim = claims.find(
(claim) => !isAddress(claim.creator_address),
const validationSchema = z
.object({
contract_id: z.string().uuid(),
creator_address: z.string(),
token_id: z.bigint(),
block_timestamp: z.bigint(),
units: z.bigint(),
uri: z.string(),
})
.refine(
(x) => isAddress(x.creator_address),
`[StoreClaim] Invalid creator address`,
);
throw new Error(
`[StoreClaim] Invalid creator address ${erroneousClaim?.token_id}`,
);
}

const _claims = claims.map((claim) => ({
owner_address: claim.creator_address,
contracts_id: claim.contract_id,
token_id: claim.token_id.toString(),
creation_block_timestamp: claim.block_timestamp.toString(),
last_block_update_timestamp: claim.block_timestamp.toString(),
type: "claim" as const,
units: claim.units.toString(),
uri: claim.uri,
value: 1,
}));
const _claims = claims
.map((claim) => validationSchema.parse(claim))
.map((claim) => ({
owner_address: claim.creator_address,
contracts_id: claim.contract_id,
token_id: claim.token_id.toString(),
creation_block_timestamp: claim.block_timestamp.toString(),
last_block_update_timestamp: claim.block_timestamp.toString(),
type: "claim" as const,
units: claim.units.toString(),
uri: claim.uri,
value: 1,
}));

console.debug(`[StoreClaim] Storing ${claims.length} claims`);

Expand Down

0 comments on commit 8061f04

Please sign in to comment.