Skip to content

chore: Consolidate Event Logs + Transaction Receipts endpoints #509

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 29 additions & 35 deletions src/db/contractEventLogs/getContractEventLogs.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
import { ContractEventLogs, Prisma } from "@prisma/client";
import base64 from "base-64";
import { z } from "zod";
import { prisma } from "../client";

interface GetContractLogsParams {
interface GetContractEventLogsParams {
chainId: number;
contractAddress: string;
contractAddresses: string[];
fromBlock: number;
toBlock?: number;
topics?: string[];
}

export const getContractEventLogsByBlockAndTopics = async ({
export const getEventLogsByBlock = async ({
chainId,
contractAddress,
contractAddresses,
fromBlock,
toBlock,
topics,
}: GetContractLogsParams) => {
const whereClause = {
}: GetContractEventLogsParams): Promise<ContractEventLogs[]> => {
const whereClause: Prisma.ContractEventLogsWhereInput = {
chainId,
contractAddress,
contractAddress: {
in: contractAddresses,
},
blockNumber: {
gte: fromBlock,
...(toBlock ? { lte: toBlock } : {}),
lte: toBlock,
},
...(topics && topics.length > 0
? {
Expand All @@ -41,26 +44,24 @@ export const getContractEventLogsByBlockAndTopics = async ({
});
};

interface GetEventLogsByBlockTimestampParams {
fromBlockTimestamp: number;
toBlockTimestamp?: number;
interface GetEventLogsByTimestampParams {
chainId?: string;
fromTimestamp: number;
toTimestamp?: number;
contractAddresses?: string[];
topics?: string[];
}

export const getEventLogsByBlockTimestamp = async ({
fromBlockTimestamp,
toBlockTimestamp,
export const getEventLogsByTimestamp = async ({
fromTimestamp,
toTimestamp,
contractAddresses,
topics,
}: GetEventLogsByBlockTimestampParams) => {
const fromBlockDate = new Date(fromBlockTimestamp);
const toBlockDate = toBlockTimestamp ? new Date(toBlockTimestamp) : undefined;

const whereClause = {
}: GetEventLogsByTimestampParams): Promise<ContractEventLogs[]> => {
const whereClause: Prisma.ContractEventLogsWhereInput = {
timestamp: {
gte: fromBlockDate,
...(toBlockDate && { lte: toBlockDate }),
gte: new Date(fromTimestamp),
lte: toTimestamp ? new Date(toTimestamp) : undefined,
},
...(contractAddresses && contractAddresses.length > 0
? { contractAddress: { in: contractAddresses } }
Expand All @@ -84,22 +85,12 @@ export const getEventLogsByBlockTimestamp = async ({

interface GetEventLogsByCursorParams {
cursor?: string;
limit?: number;
limit: number;
contractAddresses?: string[];
topics?: string[];
maxCreatedAt?: Date;
}

/*
cursor?: {
createdAt: Date;
chainId: number;
blockNumber: number;
transactionIndex: number;
logIndex: number;
};
*/

const CursorSchema = z.object({
createdAt: z.number().transform((s) => new Date(s)),
chainId: z.number(),
Expand All @@ -110,11 +101,14 @@ const CursorSchema = z.object({

export const getEventLogsByCursor = async ({
cursor,
limit = 100,
limit,
contractAddresses,
topics,
maxCreatedAt,
}: GetEventLogsByCursorParams) => {
}: GetEventLogsByCursorParams): Promise<{
cursor?: string;
logs: ContractEventLogs[];
}> => {
let cursorObj: z.infer<typeof CursorSchema> | null = null;
if (cursor) {
const decodedCursor = base64.decode(cursor);
Expand All @@ -136,7 +130,7 @@ export const getEventLogsByCursor = async ({
cursorObj = validationResult.data;
}

const whereClause = {
const whereClause: Prisma.ContractEventLogsWhereInput = {
AND: [
...(contractAddresses && contractAddresses.length > 0
? [{ contractAddress: { in: contractAddresses } }]
Expand Down
106 changes: 49 additions & 57 deletions src/db/contractTransactionReceipts/getContractTransactionReceipts.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
import { ContractTransactionReceipts, Prisma } from "@prisma/client";
import base64 from "base-64";
import { z } from "zod";
import { prisma } from "../client";

interface GetContractTransactionReceiptsParams {
interface GetTransactionReceiptsParams {
chainId: number;
contractAddress: string;
addresses: string[];
fromBlock: number;
toBlock?: number;
}

export const getContractTransactionReceiptsByBlock = async ({
export const getTransactionReceiptsByBlock = async ({
chainId,
contractAddress,
addresses,
fromBlock,
toBlock,
}: GetContractTransactionReceiptsParams) => {
const whereClause = {
}: GetTransactionReceiptsParams): Promise<ContractTransactionReceipts[]> => {
const whereClause: Prisma.ContractTransactionReceiptsWhereInput = {
chainId,
contractAddress,
contractAddress: { in: addresses },
blockNumber: {
gte: fromBlock,
...(toBlock ? { lte: toBlock } : {}),
lte: toBlock,
},
};

Expand All @@ -29,27 +30,26 @@ export const getContractTransactionReceiptsByBlock = async ({
});
};

interface GetContractTransactionReceiptsByBlockTimestampParams {
fromBlockTimestamp: number;
toBlockTimestamp?: number;
contractAddresses?: string[];
interface GetContractTransactionReceiptsByTimestampParams {
fromTimestamp: number;
toTimestamp?: number;
addresses?: string[];
}

export const getTransactionReceiptsByBlockTimestamp = async ({
fromBlockTimestamp,
toBlockTimestamp,
contractAddresses,
}: GetContractTransactionReceiptsByBlockTimestampParams) => {
const fromBlockDate = new Date(fromBlockTimestamp);
const toBlockDate = toBlockTimestamp ? new Date(toBlockTimestamp) : undefined;

const whereClause = {
export const getTransactionReceiptsByTimestamp = async ({
fromTimestamp,
toTimestamp,
addresses,
}: GetContractTransactionReceiptsByTimestampParams): Promise<
ContractTransactionReceipts[]
> => {
const whereClause: Prisma.ContractTransactionReceiptsWhereInput = {
timestamp: {
gte: fromBlockDate,
...(toBlockDate && { lte: toBlockDate }),
gte: new Date(fromTimestamp),
lte: toTimestamp ? new Date(toTimestamp) : undefined,
},
...(contractAddresses && contractAddresses.length > 0
? { contractAddress: { in: contractAddresses } }
...(addresses && addresses.length > 0
? { contractAddress: { in: addresses } }
: {}),
};

Expand All @@ -60,20 +60,11 @@ export const getTransactionReceiptsByBlockTimestamp = async ({

interface GetContractTransactionReceiptsByCursorParams {
cursor?: string;
limit?: number;
contractAddresses?: string[];
limit: number;
addresses?: string[];
maxCreatedAt?: Date;
}

/*
cursor?: {
createdAt: Date;
chainId: number;
blockNumber: number;
transactionIndex: number;
};
*/

const CursorSchema = z.object({
createdAt: z.number().transform((s) => new Date(s)),
chainId: z.number(),
Expand All @@ -83,10 +74,13 @@ const CursorSchema = z.object({

export const getTransactionReceiptsByCursor = async ({
cursor,
limit = 100,
contractAddresses,
limit,
addresses,
maxCreatedAt,
}: GetContractTransactionReceiptsByCursorParams) => {
}: GetContractTransactionReceiptsByCursorParams): Promise<{
cursor?: string;
receipts: ContractTransactionReceipts[];
}> => {
let cursorObj: z.infer<typeof CursorSchema> | null = null;
if (cursor) {
const decodedCursor = base64.decode(cursor);
Expand All @@ -106,10 +100,10 @@ export const getTransactionReceiptsByCursor = async ({
cursorObj = validationResult.data;
}

const whereClause = {
const whereClause: Prisma.ContractTransactionReceiptsWhereInput = {
AND: [
...(contractAddresses && contractAddresses.length > 0
? [{ contractAddress: { in: contractAddresses } }]
...(addresses && addresses.length > 0
? [{ contractAddress: { in: addresses } }]
: []),
...(cursorObj
? [
Expand Down Expand Up @@ -147,31 +141,29 @@ export const getTransactionReceiptsByCursor = async ({
],
};

const transactionReceipts = await prisma.contractTransactionReceipts.findMany(
{
where: whereClause,
orderBy: [
{ createdAt: "asc" },
{ chainId: "asc" },
{ blockNumber: "asc" },
{ transactionIndex: "asc" },
],
take: limit,
},
);
const receipts = await prisma.contractTransactionReceipts.findMany({
where: whereClause,
orderBy: [
{ createdAt: "asc" },
{ chainId: "asc" },
{ blockNumber: "asc" },
{ transactionIndex: "asc" },
],
take: limit,
});

/* cursor rules */
// if new logs returned, return new cursor
// if no new logs and no cursor return null (original cursor)
// if no new logs and cursor return original cursor
let newCursor = cursor;
if (transactionReceipts.length > 0) {
const lastReceipt = transactionReceipts[transactionReceipts.length - 1];
if (receipts.length > 0) {
const lastReceipt = receipts[receipts.length - 1];
const cursorString = `${lastReceipt.createdAt.getTime()}-${
lastReceipt.chainId
}-${lastReceipt.blockNumber}-${lastReceipt.transactionIndex}`;
newCursor = base64.encode(cursorString);
}

return { cursor: newCursor, transactionReceipts };
return { cursor: newCursor, receipts };
};
10 changes: 5 additions & 5 deletions src/server/routes/chain/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@ const responseSchema = Type.Object({
responseSchema.examples = [
{
result: {
name: "Mumbai",
name: "Polygon Amoy Testnet",
chain: "Polygon",
rpc: ["https://mumbai.rpc.thirdweb.com/${THIRDWEB_API_SECRET_KEY}"],
rpc: ["https://80002.rpc.thirdweb.com/${THIRDWEB_API_SECRET_KEY}"],
nativeCurrency: {
name: "MATIC",
symbol: "MATIC",
decimals: 18,
},
shortName: "maticmum",
chainId: 80001,
shortName: "polygonamoy",
chainId: 80002,
testnet: true,
slug: "mumbai",
slug: "polygon-amoy-testnet",
},
},
];
Expand Down
Loading
Loading