Skip to content

Commit

Permalink
refactor all int chainId to string (#762)
Browse files Browse the repository at this point in the history
* refactor all int chainId to string

* send string chainId to primsa sql template tag

* revert back to gt for cursor
  • Loading branch information
d4mr authored Nov 11, 2024
1 parent 2f762dc commit 0b784dc
Show file tree
Hide file tree
Showing 16 changed files with 54 additions and 33 deletions.
5 changes: 3 additions & 2 deletions src/db/chainIndexers/getChainIndexer.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Prisma } from "@prisma/client";
import type { PrismaTransaction } from "../../schema/prisma";
import { getPrismaWithPostgresTx } from "../client";

Expand All @@ -14,7 +15,7 @@ export const getLastIndexedBlock = async ({

const indexedChain = await prisma.chainIndexers.findUnique({
where: {
chainId,
chainId: chainId.toString(),
},
});

Expand Down Expand Up @@ -42,7 +43,7 @@ export const getBlockForIndexing = async ({
FROM
"chain_indexers"
WHERE
"chainId"=${chainId}
"chainId"=${Prisma.sql`${chainId.toString()}`}
FOR UPDATE NOWAIT
`;
return lastIndexedBlock[0].lastIndexedBlock;
Expand Down
8 changes: 4 additions & 4 deletions src/db/chainIndexers/upsertChainIndexer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PrismaTransaction } from "../../schema/prisma";
import type { PrismaTransaction } from "../../schema/prisma";
import { getPrismaWithPostgresTx } from "../client";

interface UpsertChainIndexerParams {
Expand All @@ -15,14 +15,14 @@ export const upsertChainIndexer = async ({
const prisma = getPrismaWithPostgresTx(pgtx);
return prisma.chainIndexers.upsert({
where: {
chainId,
chainId: chainId.toString(),
},
update: {
chainId,
chainId: chainId.toString(),
lastIndexedBlock: currentBlockNumber,
},
create: {
chainId,
chainId: chainId.toString(),
lastIndexedBlock: currentBlockNumber,
},
});
Expand Down
2 changes: 1 addition & 1 deletion src/db/contractEventLogs/deleteContractEventLogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const deleteContractEventLogs = async ({
}: DeleteContractEventLogsParams) => {
return prisma.contractEventLogs.deleteMany({
where: {
chainId,
chainId: chainId.toString(),
contractAddress,
},
});
Expand Down
16 changes: 9 additions & 7 deletions src/db/contractEventLogs/getContractEventLogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const getContractEventLogsByBlockAndTopics = async ({
topics,
}: GetContractLogsParams) => {
const whereClause = {
chainId,
chainId: chainId.toString(),
contractAddress,
blockNumber: {
gte: fromBlock,
Expand Down Expand Up @@ -118,7 +118,9 @@ export const getEventLogsByCursor = async ({
let cursorObj: z.infer<typeof CursorSchema> | null = null;
if (cursor) {
const decodedCursor = base64.decode(cursor);
const parsedCursor = decodedCursor.split("-").map((val) => parseInt(val));
const parsedCursor = decodedCursor
.split("-")
.map((val) => Number.parseInt(val));
const [createdAt, chainId, blockNumber, transactionIndex, logIndex] =
parsedCursor;
const validationResult = CursorSchema.safeParse({
Expand Down Expand Up @@ -148,22 +150,22 @@ export const getEventLogsByCursor = async ({
{ createdAt: { gt: cursorObj.createdAt } },
{
createdAt: { equals: cursorObj.createdAt },
chainId: { gt: cursorObj.chainId },
chainId: { gt: cursorObj.chainId.toString() },
},
{
createdAt: { equals: cursorObj.createdAt },
chainId: { equals: cursorObj.chainId },
chainId: { equals: cursorObj.chainId.toString() },
blockNumber: { gt: cursorObj.blockNumber },
},
{
createdAt: { equals: cursorObj.createdAt },
chainId: { equals: cursorObj.chainId },
chainId: { equals: cursorObj.chainId.toString() },
blockNumber: { equals: cursorObj.blockNumber },
transactionIndex: { gt: cursorObj.transactionIndex },
},
{
createdAt: { equals: cursorObj.createdAt },
chainId: { equals: cursorObj.chainId },
chainId: { equals: cursorObj.chainId.toString() },
blockNumber: { equals: cursorObj.blockNumber },
transactionIndex: { equals: cursorObj.transactionIndex },
logIndex: { gt: cursorObj.logIndex },
Expand Down Expand Up @@ -234,7 +236,7 @@ export const getContractEventLogsIndexedBlockRange = async ({
}: GetContractEventLogsIndexedBlockRangeParams) => {
const result = await prisma.contractEventLogs.aggregate({
where: {
chainId,
chainId: chainId.toString(),
contractAddress,
},
_min: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const deleteContractTransactionReceipts = async ({
}: DeleteContractTransactionReceiptsParams) => {
return prisma.contractTransactionReceipts.deleteMany({
where: {
chainId,
chainId: chainId.toString(),
contractAddress,
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const getContractTransactionReceiptsByBlock = async ({
toBlock,
}: GetContractTransactionReceiptsParams) => {
const whereClause = {
chainId,
chainId: chainId.toString(),
contractAddress,
blockNumber: {
gte: fromBlock,
Expand Down Expand Up @@ -90,7 +90,9 @@ export const getTransactionReceiptsByCursor = async ({
let cursorObj: z.infer<typeof CursorSchema> | null = null;
if (cursor) {
const decodedCursor = base64.decode(cursor);
const parsedCursor = decodedCursor.split("-").map((val) => parseInt(val));
const parsedCursor = decodedCursor
.split("-")
.map((val) => Number.parseInt(val));
const [createdAt, chainId, blockNumber, transactionIndex] = parsedCursor;
const validationResult = CursorSchema.safeParse({
createdAt,
Expand Down Expand Up @@ -118,16 +120,16 @@ export const getTransactionReceiptsByCursor = async ({
{ createdAt: { gt: cursorObj.createdAt } },
{
createdAt: { equals: cursorObj.createdAt },
chainId: { gt: cursorObj.chainId },
chainId: { gt: cursorObj.chainId.toString() },
},
{
createdAt: { equals: cursorObj.createdAt },
chainId: { equals: cursorObj.chainId },
chainId: { equals: cursorObj.chainId.toString() },
blockNumber: { gt: cursorObj.blockNumber },
},
{
createdAt: { equals: cursorObj.createdAt },
chainId: { equals: cursorObj.chainId },
chainId: { equals: cursorObj.chainId.toString() },
blockNumber: { gt: cursorObj.blockNumber },
transactionIndex: { gt: cursorObj.transactionIndex },
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
Warnings:
- The primary key for the `chain_indexers` table will be changed. If it partially fails, the table could be left without primary key constraint.
*/
-- AlterTable
ALTER TABLE "chain_indexers" DROP CONSTRAINT "chain_indexers_pkey",
ALTER COLUMN "chainId" SET DATA TYPE TEXT,
ADD CONSTRAINT "chain_indexers_pkey" PRIMARY KEY ("chainId");

-- AlterTable
ALTER TABLE "contract_event_logs" ALTER COLUMN "chainId" SET DATA TYPE TEXT;

-- AlterTable
ALTER TABLE "contract_transaction_receipts" ALTER COLUMN "chainId" SET DATA TYPE TEXT;
6 changes: 3 additions & 3 deletions src/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ model ContractSubscriptions {
}

model ContractEventLogs {
chainId Int
chainId String
blockNumber Int
contractAddress String
transactionHash String
Expand Down Expand Up @@ -251,7 +251,7 @@ model ContractEventLogs {
}

model ContractTransactionReceipts {
chainId Int
chainId String
blockNumber Int
contractAddress String
contractId String // ${chainId}:${contractAddress}
Expand Down Expand Up @@ -280,7 +280,7 @@ model ContractTransactionReceipts {
}

model ChainIndexers {
chainId Int @id
chainId String @id
lastIndexedBlock Int
createdAt DateTime @default(now())
Expand Down
6 changes: 3 additions & 3 deletions src/server/routes/contract/events/getContractEventLogs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Static, Type } from "@sinclair/typebox";
import { FastifyInstance } from "fastify";
import { Type, type Static } from "@sinclair/typebox";
import type { FastifyInstance } from "fastify";
import { StatusCodes } from "http-status-codes";
import { getContractEventLogsByBlockAndTopics } from "../../../../db/contractEventLogs/getContractEventLogs";
import { isContractSubscribed } from "../../../../db/contractSubscriptions/getContractSubscriptions";
Expand Down Expand Up @@ -152,7 +152,7 @@ export async function getContractEventLogs(fastify: FastifyInstance) {
});

return {
chainId: log.chainId,
chainId: Number.parseInt(log.chainId),
contractAddress: log.contractAddress,
blockNumber: log.blockNumber,
transactionHash: log.transactionHash,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export async function getEventLogs(fastify: FastifyInstance) {
});

return {
chainId: log.chainId,
chainId: Number.parseInt(log.chainId),
contractAddress: log.contractAddress,
blockNumber: log.blockNumber,
transactionHash: log.transactionHash,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export async function getContractTransactionReceipts(fastify: FastifyInstance) {

const transactionReceipts = resultTransactionReceipts.map((txRcpt) => {
return {
chainId: txRcpt.chainId,
chainId: Number.parseInt(txRcpt.chainId),
blockNumber: txRcpt.blockNumber,
contractAddress: txRcpt.contractAddress,
transactionHash: txRcpt.transactionHash,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export async function getContractTransactionReceiptsByTimestamp(

const transactionReceipts = resultTransactionReceipts.map((txRcpt) => {
return {
chainId: txRcpt.chainId,
chainId: Number.parseInt(txRcpt.chainId),
blockNumber: txRcpt.blockNumber,
contractAddress: txRcpt.contractAddress,
transactionHash: txRcpt.transactionHash,
Expand Down
2 changes: 1 addition & 1 deletion src/server/schemas/eventLog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const toEventLogSchema = (
});

return {
chainId: log.chainId,
chainId: Number.parseInt(log.chainId),
contractAddress: log.contractAddress,
blockNumber: log.blockNumber,
transactionHash: log.transactionHash,
Expand Down
2 changes: 1 addition & 1 deletion src/server/schemas/transactionReceipt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const transactionReceiptSchema = Type.Object({
export const toTransactionReceiptSchema = (
transactionReceipt: ContractTransactionReceipts,
): Static<typeof transactionReceiptSchema> => ({
chainId: transactionReceipt.chainId,
chainId: Number.parseInt(transactionReceipt.chainId),
blockNumber: transactionReceipt.blockNumber,
contractAddress: transactionReceipt.contractAddress,
transactionHash: transactionReceipt.transactionHash,
Expand Down
2 changes: 1 addition & 1 deletion src/worker/tasks/processEventLogsWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ const getLogs = async ({
return await Promise.all(
allLogs.map(
async (log): Promise<Prisma.ContractEventLogsCreateInput> => ({
chainId,
chainId: chainId.toString(),
blockNumber: Number(log.blockNumber),
contractAddress: normalizeAddress(log.address),
transactionHash: log.transactionHash,
Expand Down
2 changes: 1 addition & 1 deletion src/worker/tasks/processTransactionReceiptsWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ const getFormattedTransactionReceipts = async ({
});

receipts.push({
chainId,
chainId: chainId.toString(),
blockNumber: Number(receipt.blockNumber),
contractAddress: toAddress,
contractId: getContractId(chainId, toAddress),
Expand Down

0 comments on commit 0b784dc

Please sign in to comment.