Skip to content

Commit

Permalink
Merge pull request #52 from eseiker/update-event-api-schema
Browse files Browse the repository at this point in the history
update event api schema
  • Loading branch information
eseiker committed Aug 10, 2023
2 parents c434337 + 440f7c4 commit 4c45892
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 32 deletions.
29 changes: 21 additions & 8 deletions api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
import { Buffer } from "node:buffer";

import { stringify as losslessJsonStringify } from "npm:lossless-json";
import { getAddress, keccak256, toBytes, toHex } from "npm:viem";
import { getAddress, keccak256, toHex } from "npm:viem";

import type { PrismaClient } from "./prisma-shim.ts";

Expand All @@ -37,6 +37,18 @@ import {
import { runWithPrisma } from "./runHelpers.ts";

export function api(prisma: PrismaClient) {
const hexToBuffer = (hex: string): Buffer => {
return Buffer.from(hex.replace("0x", ""), "hex");
};

const getBlockNumberFromHash = async (blockHash?: string | number) => {
if (typeof blockHash !== "string") return blockHash;
return (await prisma.event.findFirst({
where: { blockHash: hexToBuffer(blockHash) },
}))
?.blockNumber;
};

const logger = getLogger(ApiLoggerName);
const abortController = new AbortController();

Expand All @@ -56,17 +68,18 @@ export function api(prisma: PrismaClient) {
return;
}

const blockFrom = await getBlockNumberFromHash(request.blockFrom);
const blockTo = await getBlockNumberFromHash(request.blockTo);

ctx.response.body = losslessJsonStringify((await prisma.event.findMany({
where: {
blockHash: request.blockHash && Buffer.from(toBytes(request.blockHash)),
blockNumber: request.blockIndex ??
{ gte: request.blockFrom, lte: request.blockTo },
blockHash: request.blockHash && hexToBuffer(request.blockHash),
blockNumber: request.blockIndex ?? { gte: blockFrom, lte: blockTo },
logIndex: request.logIndex,
txHash: request.transactionHash &&
Buffer.from(toBytes(request.transactionHash)),
txHash: request.transactionHash && hexToBuffer(request.transactionHash),
sourceAddress: request.sourceAddress &&
Buffer.from(toBytes(request.sourceAddress)),
abiHash: (request.abiHash && Buffer.from(toBytes(request.abiHash))) ||
hexToBuffer(request.sourceAddress),
abiHash: (request.abiHash && hexToBuffer(request.abiHash)) ??
(request.abiSignature &&
Buffer.from(
keccak256(
Expand Down
69 changes: 45 additions & 24 deletions apiSchema.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import Ajv, { type JSONSchemaType } from "https://esm.sh/ajv@8.12.0";
import ajvFormats from "https://esm.sh/ajv-formats@3.0.0-rc.0";
import ajvFormats from "https://esm.sh/ajv-formats@2.1.1";

const ajv = new Ajv();
const ajv = new Ajv({ allowUnionTypes: true });
ajvFormats(ajv);

interface EventRequest {
blockHash?: string;
blockIndex?: number;
blockFrom?: number;
blockTo?: number;
blockHash?: string;
blockFrom?: number | string;
blockTo?: number | string;
logIndex?: number;
transactionHash?: string;
sourceAddress?: string;
Expand All @@ -21,17 +21,47 @@ interface EventRequest {
const eventRequestSchema: JSONSchemaType<EventRequest> = {
type: "object",
properties: {
blockHash: { type: "string", nullable: true },
blockIndex: { type: "number", nullable: true },
blockFrom: { type: "number", nullable: true },
blockTo: { type: "number", nullable: true },
blockHash: {
type: "string",
nullable: true,
pattern: "^(0x)?[a-fA-F0-9]{64}$",
},
blockFrom: {
type: ["number", "string"],
nullable: true,
anyOf: [
{ type: "number" },
{ type: "string", pattern: "^(0x)?[a-fA-F0-9]{64}$" },
],
},
blockTo: {
type: ["number", "string"],
nullable: true,
anyOf: [
{ type: "number" },
{ type: "string", pattern: "^(0x)?[a-fA-F0-9]{64}$" },
],
},
logIndex: { type: "number", nullable: true },
transactionHash: { type: "string", nullable: true },
sourceAddress: { type: "string", nullable: true },
abiHash: { type: "string", nullable: true },
transactionHash: {
type: "string",
nullable: true,
pattern: "^(0x)?[a-fA-F0-9]{64}$",
},
sourceAddress: {
type: "string",
nullable: true,
pattern: "^(0x)?[a-fA-F0-9]{40}$",
},
abiHash: {
type: "string",
nullable: true,
pattern: "^(0x)?[a-fA-F0-9]{64}$",
},
abiSignature: { type: "string", nullable: true },
after: { type: "string", nullable: true, format: "iso-date-time" },
before: { type: "string", nullable: true, format: "iso-date-time" },
after: { type: "string", nullable: true, format: "date-time" },
before: { type: "string", nullable: true, format: "date-time" },
},
allOf: [
{
Expand All @@ -40,24 +70,15 @@ const eventRequestSchema: JSONSchemaType<EventRequest> = {
allOf: [
{ not: { required: ["blockHash"] } },
{ not: { required: ["blockIndex"] } },
{
not: {
anyOf: [{ required: ["blockFrom"] }, { required: ["blockTo"] }],
},
},
{ not: { required: ["blockFrom"] } },
{ not: { required: ["blockTo"] } },
],
},
{ required: ["blockHash"] },
{ required: ["blockIndex"] },
{ anyOf: [{ required: ["blockFrom"] }, { required: ["blockTo"] }] },
],
},
{
oneOf: [
{ not: { required: ["transactionHash"] } },
{ required: ["transactionHash"] },
],
},
{
oneOf: [
{
Expand Down

0 comments on commit 4c45892

Please sign in to comment.