Skip to content
Merged
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
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}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needed to wrap ${chainId} in quotes to work for a SQL string comparison. Opted to use Prisma.sql which is the recommended way of templating in SQL string.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm why can't this line just be:

"chainId"="${chainId.toString()}"

Copy link
Contributor Author

@d4mr d4mr Nov 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah that works, and given that we know chainId is sanitised it should be fine, but just a standard best practice to not use raw string interpolation with SQL

Copy link
Contributor

@arcoraven arcoraven Nov 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets simplify it. From how I'm reading Prisma docs (source), Prisma handles the sql injection risk.

The method is implemented as a tagged template, which allows you to pass a template literal where you can easily insert your variables. In turn, Prisma Client creates prepared statements that are safe from SQL injections:

const email = 'emelie@prisma.io'
const result = await prisma.$queryRaw`SELECT * FROM User WHERE email = ${email}`

"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
Loading