Skip to content

Commit 0b784dc

Browse files
authored
refactor all int chainId to string (#762)
* refactor all int chainId to string * send string chainId to primsa sql template tag * revert back to gt for cursor
1 parent 2f762dc commit 0b784dc

File tree

16 files changed

+54
-33
lines changed

16 files changed

+54
-33
lines changed

src/db/chainIndexers/getChainIndexer.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Prisma } from "@prisma/client";
12
import type { PrismaTransaction } from "../../schema/prisma";
23
import { getPrismaWithPostgresTx } from "../client";
34

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

1516
const indexedChain = await prisma.chainIndexers.findUnique({
1617
where: {
17-
chainId,
18+
chainId: chainId.toString(),
1819
},
1920
});
2021

@@ -42,7 +43,7 @@ export const getBlockForIndexing = async ({
4243
FROM
4344
"chain_indexers"
4445
WHERE
45-
"chainId"=${chainId}
46+
"chainId"=${Prisma.sql`${chainId.toString()}`}
4647
FOR UPDATE NOWAIT
4748
`;
4849
return lastIndexedBlock[0].lastIndexedBlock;

src/db/chainIndexers/upsertChainIndexer.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { PrismaTransaction } from "../../schema/prisma";
1+
import type { PrismaTransaction } from "../../schema/prisma";
22
import { getPrismaWithPostgresTx } from "../client";
33

44
interface UpsertChainIndexerParams {
@@ -15,14 +15,14 @@ export const upsertChainIndexer = async ({
1515
const prisma = getPrismaWithPostgresTx(pgtx);
1616
return prisma.chainIndexers.upsert({
1717
where: {
18-
chainId,
18+
chainId: chainId.toString(),
1919
},
2020
update: {
21-
chainId,
21+
chainId: chainId.toString(),
2222
lastIndexedBlock: currentBlockNumber,
2323
},
2424
create: {
25-
chainId,
25+
chainId: chainId.toString(),
2626
lastIndexedBlock: currentBlockNumber,
2727
},
2828
});

src/db/contractEventLogs/deleteContractEventLogs.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export const deleteContractEventLogs = async ({
1111
}: DeleteContractEventLogsParams) => {
1212
return prisma.contractEventLogs.deleteMany({
1313
where: {
14-
chainId,
14+
chainId: chainId.toString(),
1515
contractAddress,
1616
},
1717
});

src/db/contractEventLogs/getContractEventLogs.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export const getContractEventLogsByBlockAndTopics = async ({
1818
topics,
1919
}: GetContractLogsParams) => {
2020
const whereClause = {
21-
chainId,
21+
chainId: chainId.toString(),
2222
contractAddress,
2323
blockNumber: {
2424
gte: fromBlock,
@@ -118,7 +118,9 @@ export const getEventLogsByCursor = async ({
118118
let cursorObj: z.infer<typeof CursorSchema> | null = null;
119119
if (cursor) {
120120
const decodedCursor = base64.decode(cursor);
121-
const parsedCursor = decodedCursor.split("-").map((val) => parseInt(val));
121+
const parsedCursor = decodedCursor
122+
.split("-")
123+
.map((val) => Number.parseInt(val));
122124
const [createdAt, chainId, blockNumber, transactionIndex, logIndex] =
123125
parsedCursor;
124126
const validationResult = CursorSchema.safeParse({
@@ -148,22 +150,22 @@ export const getEventLogsByCursor = async ({
148150
{ createdAt: { gt: cursorObj.createdAt } },
149151
{
150152
createdAt: { equals: cursorObj.createdAt },
151-
chainId: { gt: cursorObj.chainId },
153+
chainId: { gt: cursorObj.chainId.toString() },
152154
},
153155
{
154156
createdAt: { equals: cursorObj.createdAt },
155-
chainId: { equals: cursorObj.chainId },
157+
chainId: { equals: cursorObj.chainId.toString() },
156158
blockNumber: { gt: cursorObj.blockNumber },
157159
},
158160
{
159161
createdAt: { equals: cursorObj.createdAt },
160-
chainId: { equals: cursorObj.chainId },
162+
chainId: { equals: cursorObj.chainId.toString() },
161163
blockNumber: { equals: cursorObj.blockNumber },
162164
transactionIndex: { gt: cursorObj.transactionIndex },
163165
},
164166
{
165167
createdAt: { equals: cursorObj.createdAt },
166-
chainId: { equals: cursorObj.chainId },
168+
chainId: { equals: cursorObj.chainId.toString() },
167169
blockNumber: { equals: cursorObj.blockNumber },
168170
transactionIndex: { equals: cursorObj.transactionIndex },
169171
logIndex: { gt: cursorObj.logIndex },
@@ -234,7 +236,7 @@ export const getContractEventLogsIndexedBlockRange = async ({
234236
}: GetContractEventLogsIndexedBlockRangeParams) => {
235237
const result = await prisma.contractEventLogs.aggregate({
236238
where: {
237-
chainId,
239+
chainId: chainId.toString(),
238240
contractAddress,
239241
},
240242
_min: {

src/db/contractTransactionReceipts/deleteContractTransactionReceipts.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export const deleteContractTransactionReceipts = async ({
1111
}: DeleteContractTransactionReceiptsParams) => {
1212
return prisma.contractTransactionReceipts.deleteMany({
1313
where: {
14-
chainId,
14+
chainId: chainId.toString(),
1515
contractAddress,
1616
},
1717
});

src/db/contractTransactionReceipts/getContractTransactionReceipts.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export const getContractTransactionReceiptsByBlock = async ({
1616
toBlock,
1717
}: GetContractTransactionReceiptsParams) => {
1818
const whereClause = {
19-
chainId,
19+
chainId: chainId.toString(),
2020
contractAddress,
2121
blockNumber: {
2222
gte: fromBlock,
@@ -90,7 +90,9 @@ export const getTransactionReceiptsByCursor = async ({
9090
let cursorObj: z.infer<typeof CursorSchema> | null = null;
9191
if (cursor) {
9292
const decodedCursor = base64.decode(cursor);
93-
const parsedCursor = decodedCursor.split("-").map((val) => parseInt(val));
93+
const parsedCursor = decodedCursor
94+
.split("-")
95+
.map((val) => Number.parseInt(val));
9496
const [createdAt, chainId, blockNumber, transactionIndex] = parsedCursor;
9597
const validationResult = CursorSchema.safeParse({
9698
createdAt,
@@ -118,16 +120,16 @@ export const getTransactionReceiptsByCursor = async ({
118120
{ createdAt: { gt: cursorObj.createdAt } },
119121
{
120122
createdAt: { equals: cursorObj.createdAt },
121-
chainId: { gt: cursorObj.chainId },
123+
chainId: { gt: cursorObj.chainId.toString() },
122124
},
123125
{
124126
createdAt: { equals: cursorObj.createdAt },
125-
chainId: { equals: cursorObj.chainId },
127+
chainId: { equals: cursorObj.chainId.toString() },
126128
blockNumber: { gt: cursorObj.blockNumber },
127129
},
128130
{
129131
createdAt: { equals: cursorObj.createdAt },
130-
chainId: { equals: cursorObj.chainId },
132+
chainId: { equals: cursorObj.chainId.toString() },
131133
blockNumber: { gt: cursorObj.blockNumber },
132134
transactionIndex: { gt: cursorObj.transactionIndex },
133135
},
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
Warnings:
3+
4+
- The primary key for the `chain_indexers` table will be changed. If it partially fails, the table could be left without primary key constraint.
5+
6+
*/
7+
-- AlterTable
8+
ALTER TABLE "chain_indexers" DROP CONSTRAINT "chain_indexers_pkey",
9+
ALTER COLUMN "chainId" SET DATA TYPE TEXT,
10+
ADD CONSTRAINT "chain_indexers_pkey" PRIMARY KEY ("chainId");
11+
12+
-- AlterTable
13+
ALTER TABLE "contract_event_logs" ALTER COLUMN "chainId" SET DATA TYPE TEXT;
14+
15+
-- AlterTable
16+
ALTER TABLE "contract_transaction_receipts" ALTER COLUMN "chainId" SET DATA TYPE TEXT;

src/prisma/schema.prisma

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ model ContractSubscriptions {
222222
}
223223

224224
model ContractEventLogs {
225-
chainId Int
225+
chainId String
226226
blockNumber Int
227227
contractAddress String
228228
transactionHash String
@@ -251,7 +251,7 @@ model ContractEventLogs {
251251
}
252252

253253
model ContractTransactionReceipts {
254-
chainId Int
254+
chainId String
255255
blockNumber Int
256256
contractAddress String
257257
contractId String // ${chainId}:${contractAddress}
@@ -280,7 +280,7 @@ model ContractTransactionReceipts {
280280
}
281281

282282
model ChainIndexers {
283-
chainId Int @id
283+
chainId String @id
284284
lastIndexedBlock Int
285285
286286
createdAt DateTime @default(now())

src/server/routes/contract/events/getContractEventLogs.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { Static, Type } from "@sinclair/typebox";
2-
import { FastifyInstance } from "fastify";
1+
import { Type, type Static } from "@sinclair/typebox";
2+
import type { FastifyInstance } from "fastify";
33
import { StatusCodes } from "http-status-codes";
44
import { getContractEventLogsByBlockAndTopics } from "../../../../db/contractEventLogs/getContractEventLogs";
55
import { isContractSubscribed } from "../../../../db/contractSubscriptions/getContractSubscriptions";
@@ -152,7 +152,7 @@ export async function getContractEventLogs(fastify: FastifyInstance) {
152152
});
153153

154154
return {
155-
chainId: log.chainId,
155+
chainId: Number.parseInt(log.chainId),
156156
contractAddress: log.contractAddress,
157157
blockNumber: log.blockNumber,
158158
transactionHash: log.transactionHash,

src/server/routes/contract/events/getEventLogsByTimestamp.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ export async function getEventLogs(fastify: FastifyInstance) {
101101
});
102102

103103
return {
104-
chainId: log.chainId,
104+
chainId: Number.parseInt(log.chainId),
105105
contractAddress: log.contractAddress,
106106
blockNumber: log.blockNumber,
107107
transactionHash: log.transactionHash,

src/server/routes/contract/transactions/getTransactionReceipts.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ export async function getContractTransactionReceipts(fastify: FastifyInstance) {
120120

121121
const transactionReceipts = resultTransactionReceipts.map((txRcpt) => {
122122
return {
123-
chainId: txRcpt.chainId,
123+
chainId: Number.parseInt(txRcpt.chainId),
124124
blockNumber: txRcpt.blockNumber,
125125
contractAddress: txRcpt.contractAddress,
126126
transactionHash: txRcpt.transactionHash,

src/server/routes/contract/transactions/getTransactionReceiptsByTimestamp.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export async function getContractTransactionReceiptsByTimestamp(
8181

8282
const transactionReceipts = resultTransactionReceipts.map((txRcpt) => {
8383
return {
84-
chainId: txRcpt.chainId,
84+
chainId: Number.parseInt(txRcpt.chainId),
8585
blockNumber: txRcpt.blockNumber,
8686
contractAddress: txRcpt.contractAddress,
8787
transactionHash: txRcpt.transactionHash,

src/server/schemas/eventLog.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export const toEventLogSchema = (
2727
});
2828

2929
return {
30-
chainId: log.chainId,
30+
chainId: Number.parseInt(log.chainId),
3131
contractAddress: log.contractAddress,
3232
blockNumber: log.blockNumber,
3333
transactionHash: log.transactionHash,

src/server/schemas/transactionReceipt.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export const transactionReceiptSchema = Type.Object({
2424
export const toTransactionReceiptSchema = (
2525
transactionReceipt: ContractTransactionReceipts,
2626
): Static<typeof transactionReceiptSchema> => ({
27-
chainId: transactionReceipt.chainId,
27+
chainId: Number.parseInt(transactionReceipt.chainId),
2828
blockNumber: transactionReceipt.blockNumber,
2929
contractAddress: transactionReceipt.contractAddress,
3030
transactionHash: transactionReceipt.transactionHash,

src/worker/tasks/processEventLogsWorker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ const getLogs = async ({
173173
return await Promise.all(
174174
allLogs.map(
175175
async (log): Promise<Prisma.ContractEventLogsCreateInput> => ({
176-
chainId,
176+
chainId: chainId.toString(),
177177
blockNumber: Number(log.blockNumber),
178178
contractAddress: normalizeAddress(log.address),
179179
transactionHash: log.transactionHash,

src/worker/tasks/processTransactionReceiptsWorker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ const getFormattedTransactionReceipts = async ({
178178
});
179179

180180
receipts.push({
181-
chainId,
181+
chainId: chainId.toString(),
182182
blockNumber: Number(receipt.blockNumber),
183183
contractAddress: toAddress,
184184
contractId: getContractId(chainId, toAddress),

0 commit comments

Comments
 (0)