From ac8ba0fec11eaf07ef8d15cb0deddefedebceafe Mon Sep 17 00:00:00 2001 From: Sylvain Goumy Date: Tue, 10 Dec 2024 14:40:00 -0500 Subject: [PATCH 1/2] chore: optimize getEvents join --- packages/core/src/sync-store/encoding.ts | 10 +- packages/core/src/sync-store/index.ts | 481 ++++++++++++++++------- 2 files changed, 353 insertions(+), 138 deletions(-) diff --git a/packages/core/src/sync-store/encoding.ts b/packages/core/src/sync-store/encoding.ts index d18e3d03f..16f2ef4f0 100644 --- a/packages/core/src/sync-store/encoding.ts +++ b/packages/core/src/sync-store/encoding.ts @@ -17,7 +17,7 @@ import type { ColumnType, Insertable } from "kysely"; import type { Address, Hash, Hex } from "viem"; import { hexToBigInt, hexToNumber } from "viem"; -type BlocksTable = { +export type BlocksTable = { hash: Hash; chainId: number; checkpoint: string; @@ -84,7 +84,7 @@ export const encodeBlock = ({ }; }; -type LogsTable = { +export type LogsTable = { id: string; chainId: number; checkpoint: string | null; @@ -138,7 +138,7 @@ export const encodeLog = ({ }; }; -type TransactionsTable = { +export type TransactionsTable = { hash: Hash; chainId: number; /** `checkpoint` will be null for transactions inserted before 0.8. This is to avoid a very slow migration. */ @@ -212,7 +212,7 @@ export const encodeTransaction = ({ }; }; -type TransactionReceiptsTable = { +export type TransactionReceiptsTable = { transactionHash: Hash; chainId: number; blockHash: Hash; @@ -256,7 +256,7 @@ export const encodeTransactionReceipt = ({ }; }; -type TracesTable = { +export type TracesTable = { id: string; chainId: number; checkpoint: string; diff --git a/packages/core/src/sync-store/index.ts b/packages/core/src/sync-store/index.ts index 7485e6704..daf77b57e 100644 --- a/packages/core/src/sync-store/index.ts +++ b/packages/core/src/sync-store/index.ts @@ -36,7 +36,12 @@ import { hexToBigInt, } from "viem"; import { + type BlocksTable, + type LogsTable, type PonderSyncSchema, + type TracesTable, + type TransactionReceiptsTable, + type TransactionsTable, encodeBlock, encodeLog, encodeTrace, @@ -497,7 +502,7 @@ export const createSyncStore = ({ const addressSQL = ( qb: SelectQueryBuilder< PonderSyncSchema, - "logs" | "blocks" | "traces", + "logs" | "blocks" | "traces" | "transactions", {} >, address: LogFilter["address"], @@ -516,6 +521,142 @@ export const createSyncStore = ({ return qb; }; + const blockColumns = [ + "blocks.baseFeePerGas as block_baseFeePerGas", + "blocks.difficulty as block_difficulty", + "blocks.extraData as block_extraData", + "blocks.gasLimit as block_gasLimit", + "blocks.gasUsed as block_gasUsed", + "blocks.hash as block_hash", + "blocks.logsBloom as block_logsBloom", + "blocks.miner as block_miner", + "blocks.mixHash as block_mixHash", + "blocks.nonce as block_nonce", + "blocks.number as block_number", + "blocks.parentHash as block_parentHash", + "blocks.receiptsRoot as block_receiptsRoot", + "blocks.sha3Uncles as block_sha3Uncles", + "blocks.size as block_size", + "blocks.stateRoot as block_stateRoot", + "blocks.timestamp as block_timestamp", + "blocks.totalDifficulty as block_totalDifficulty", + "blocks.transactionsRoot as block_transactionsRoot", + ]; + + const transactionColumns = [ + "transactions.accessList as tx_accessList", + "transactions.from as tx_from", + "transactions.gas as tx_gas", + "transactions.gasPrice as tx_gasPrice", + "transactions.hash as tx_hash", + "transactions.input as tx_input", + "transactions.maxFeePerGas as tx_maxFeePerGas", + "transactions.maxPriorityFeePerGas as tx_maxPriorityFeePerGas", + "transactions.nonce as tx_nonce", + "transactions.r as tx_r", + "transactions.s as tx_s", + "transactions.to as tx_to", + "transactions.transactionIndex as tx_transactionIndex", + "transactions.type as tx_type", + "transactions.value as tx_value", + "transactions.v as tx_v", + ]; + const transactionColumnsNull = [ + ksql`null`.as("tx_accessList"), + ksql`null`.as("tx_from"), + ksql`null`.as("tx_gas"), + ksql`null`.as("tx_gasPrice"), + ksql`null`.as("tx_hash"), + ksql`null`.as("tx_input"), + ksql`null`.as("tx_maxFeePerGas"), + ksql`null`.as("tx_maxPriorityFeePerGas"), + ksql`null`.as("tx_nonce"), + ksql`null`.as("tx_r"), + ksql`null`.as("tx_s"), + ksql`null`.as("tx_to"), + ksql`null`.as("tx_transactionIndex"), + ksql`null`.as("tx_type"), + ksql`null`.as("tx_value"), + ksql`null`.as("tx_v"), + ]; + + const logsColumns = [ + "logs.address as log_address", + "logs.chainId as log_chainId", + "logs.data as log_data", + "logs.id as log_id", + "logs.logIndex as log_logIndex", + "logs.topic0 as log_topic0", + "logs.topic1 as log_topic1", + "logs.topic2 as log_topic2", + "logs.topic3 as log_topic3", + ]; + const logsColumnsNull = [ + ksql`null`.as("log_address"), + ksql`null`.as("log_chainId"), + ksql`null`.as("log_data"), + ksql`null`.as("log_id"), + ksql`null`.as("log_logIndex"), + ksql`null`.as("log_topic0"), + ksql`null`.as("log_topic1"), + ksql`null`.as("log_topic2"), + ksql`null`.as("log_topic3"), + ]; + + const traceColumns = [ + "traces.id as trace_id", + "traces.callType as trace_callType", + "traces.from as trace_from", + "traces.to as trace_to", + "traces.gas as trace_gas", + "traces.gasUsed as trace_gasUsed", + "traces.input as trace_input", + "traces.output as trace_output", + "traces.error as trace_error", + "traces.revertReason as trace_revertReason", + "traces.value as trace_value", + "traces.index as trace_index", + "traces.subcalls as trace_subcalls", + ]; + const traceColumnsNull = [ + ksql`null`.as("trace_id"), + ksql`null`.as("trace_callType"), + ksql`null`.as("trace_from"), + ksql`null`.as("trace_to"), + ksql`null`.as("trace_gas"), + ksql`null`.as("trace_gasUsed"), + ksql`null`.as("trace_input"), + ksql`null`.as("trace_output"), + ksql`null`.as("trace_error"), + ksql`null`.as("trace_revertReason"), + ksql`null`.as("trace_value"), + ksql`null`.as("trace_index"), + ksql`null`.as("trace_subcalls"), + ]; + + const txReceiptColumns = [ + "transactionReceipts.contractAddress as txr_contractAddress", + "transactionReceipts.cumulativeGasUsed as txr_cumulativeGasUsed", + "transactionReceipts.effectiveGasPrice as txr_effectiveGasPrice", + "transactionReceipts.from as txr_from", + "transactionReceipts.gasUsed as txr_gasUsed", + "transactionReceipts.logsBloom as txr_logsBloom", + "transactionReceipts.status as txr_status", + "transactionReceipts.to as txr_to", + "transactionReceipts.type as txr_type", + ]; + const txReceiptColumnsNull = [ + ksql`null`.as("txr_contractAddress"), + ksql`null`.as("txr_cumulativeGasUsed"), + ksql`null`.as("txr_effectiveGasPrice"), + ksql`null`.as("txr_from"), + ksql`null`.as("txr_gasUsed"), + ksql`null`.as("txr_logsBloom"), + ksql`null`.as("txr_status"), + ksql`null`.as("txr_to"), + ksql`null`.as("txr_type"), + ]; + const logSQL = ( filter: LogFilter, db: Kysely, @@ -525,14 +666,14 @@ export const createSyncStore = ({ .selectFrom("logs") .select([ ksql.raw(`'${index}'`).as("filterIndex"), - "checkpoint", - "chainId", - "blockHash", - "transactionHash", - "id as logId", + "logs.checkpoint as checkpoint", + "logs.chainId as chainId", + "logs.blockHash as blockHash", + "logs.transactionHash as transactionHash", + "logs.id as logId", ksql`null`.as("traceId"), ]) - .where("chainId", "=", filter.chainId) + .where("logs.chainId", "=", filter.chainId) .$call((qb) => { for (const idx of [0, 1, 2, 3] as const) { // If it's an array of length 1, collapse it. @@ -552,10 +693,30 @@ export const createSyncStore = ({ }) .$call((qb) => addressSQL(qb as any, filter.address, "address")) .$if(filter.fromBlock !== undefined, (qb) => - qb.where("blockNumber", ">=", filter.fromBlock!.toString()), + qb.where("logs.blockNumber", ">=", filter.fromBlock!.toString()), ) .$if(filter.toBlock !== undefined, (qb) => - qb.where("blockNumber", "<=", filter.toBlock!.toString()), + qb.where("logs.blockNumber", "<=", filter.toBlock!.toString()), + ) + .innerJoin("blocks", "blocks.hash", "blockHash") + .leftJoin("transactions", "transactions.hash", "logs.transactionHash") + // @ts-ignore + .select([ + ...blockColumns, + ...logsColumns, + ...transactionColumns, + ...traceColumnsNull, + ]) + .$call((qb) => + shouldGetTransactionReceipt(filter) + ? qb + .leftJoin( + "transactionReceipts", + "transactionReceipts.transactionHash", + "logs.transactionHash", + ) // @ts-ignore + .select(txReceiptColumns) + : qb.select(txReceiptColumnsNull), ); const blockSQL = ( @@ -567,23 +728,32 @@ export const createSyncStore = ({ .selectFrom("blocks") .select([ ksql.raw(`'${index}'`).as("filterIndex"), - "checkpoint", - "chainId", - "hash as blockHash", + "blocks.checkpoint as checkpoint", + "blocks.chainId as chainId", + "blocks.hash as blockHash", ksql`null`.as("transactionHash"), ksql`null`.as("logId"), ksql`null`.as("traceId"), ]) - .where("chainId", "=", filter.chainId) + .where("blocks.chainId", "=", filter.chainId) .$if(filter !== undefined && filter.interval !== undefined, (qb) => - qb.where(ksql`(number - ${filter.offset}) % ${filter.interval} = 0`), + qb.where( + ksql`(blocks.number - ${filter.offset}) % ${filter.interval} = 0`, + ), ) .$if(filter.fromBlock !== undefined, (qb) => - qb.where("number", ">=", filter.fromBlock!.toString()), + qb.where("blocks.number", ">=", filter.fromBlock!.toString()), ) .$if(filter.toBlock !== undefined, (qb) => - qb.where("number", "<=", filter.toBlock!.toString()), - ); + qb.where("blocks.number", "<=", filter.toBlock!.toString()), + ) // @ts-ignore + .select([ + ...blockColumns, + ...logsColumnsNull, + ...transactionColumnsNull, + ...traceColumnsNull, + ...txReceiptColumnsNull, + ]); const transactionSQL = ( filter: TransactionFilter, @@ -594,14 +764,14 @@ export const createSyncStore = ({ .selectFrom("transactions") .select([ ksql.raw(`'${index}'`).as("filterIndex"), - "checkpoint", - "chainId", - "blockHash", - "hash as transactionHash", + "transactions.checkpoint as checkpoint", + "transactions.chainId as chainId", + "transactions.blockHash as blockHash", + "transactions.hash as transactionHash", ksql`null`.as("logId"), ksql`null`.as("traceId"), ]) - .where("chainId", "=", filter.chainId) + .where("transactions.chainId", "=", filter.chainId) .$call((qb) => addressSQL(qb as any, filter.fromAddress, "from")) .$call((qb) => addressSQL(qb as any, filter.toAddress, "to")) .$if(filter.includeReverted === false, (qb) => @@ -619,10 +789,37 @@ export const createSyncStore = ({ ), ) .$if(filter.fromBlock !== undefined, (qb) => - qb.where("blockNumber", ">=", filter.fromBlock!.toString()), + qb.where( + "transactions.blockNumber", + ">=", + filter.fromBlock!.toString(), + ), ) .$if(filter.toBlock !== undefined, (qb) => - qb.where("blockNumber", "<=", filter.toBlock!.toString()), + qb.where( + "transactions.blockNumber", + "<=", + filter.toBlock!.toString(), + ), + ) + .innerJoin("blocks", "blocks.hash", "transactions.blockHash") + // @ts-ignore + .select([ + ...blockColumns, + ...transactionColumns, + ...logsColumnsNull, + ...traceColumnsNull, + ]) + .$call((qb) => + shouldGetTransactionReceipt(filter) + ? qb + .leftJoin( + "transactionReceipts", + "transactionReceipts.transactionHash", + "transactions.hash", + ) // @ts-ignore + .select(txReceiptColumns) + : qb.select(txReceiptColumnsNull), ); const transferSQL = ( @@ -634,25 +831,45 @@ export const createSyncStore = ({ .selectFrom("traces") .select([ ksql.raw(`'${index}'`).as("filterIndex"), - "checkpoint", - "chainId", - "blockHash", - "transactionHash", + "traces.checkpoint as checkpoint", + "traces.chainId as chainId", + "traces.blockHash as blockHash", + "traces.transactionHash as transactionHash", ksql`null`.as("logId"), - "id as traceId", + "traces.id as traceId", ]) - .where("chainId", "=", filter.chainId) + .where("traces.chainId", "=", filter.chainId) .$call((qb) => addressSQL(qb as any, filter.fromAddress, "from")) .$call((qb) => addressSQL(qb as any, filter.toAddress, "to")) - .where("value", ">", "0") + .where("traces.value", ">", "0") .$if(filter.includeReverted === false, (qb) => - qb.where("isReverted", "=", 0), + qb.where("traces.isReverted", "=", 0), ) .$if(filter.fromBlock !== undefined, (qb) => - qb.where("blockNumber", ">=", filter.fromBlock!.toString()), + qb.where("traces.blockNumber", ">=", filter.fromBlock!.toString()), ) .$if(filter.toBlock !== undefined, (qb) => - qb.where("blockNumber", "<=", filter.toBlock!.toString()), + qb.where("traces.blockNumber", "<=", filter.toBlock!.toString()), + ) + .innerJoin("blocks", "blocks.hash", "traces.blockHash") + .leftJoin("transactions", "transactions.hash", "traces.transactionHash") + // @ts-ignore + .select([ + ...blockColumns, + ...transactionColumns, + ...logsColumnsNull, + ...traceColumns, + ]) + .$call((qb) => + shouldGetTransactionReceipt(filter) + ? qb + .leftJoin( + "transactionReceipts", + "transactionReceipts.transactionHash", + "traces.transactionHash", + ) // @ts-ignore + .select(txReceiptColumns) + : qb.select(txReceiptColumnsNull), ); const traceSQL = ( @@ -664,21 +881,21 @@ export const createSyncStore = ({ .selectFrom("traces") .select([ ksql.raw(`'${index}'`).as("filterIndex"), - "checkpoint", - "chainId", - "blockHash", - "transactionHash", + "traces.checkpoint as checkpoint", + "traces.chainId as chainId", + "traces.blockHash as blockHash", + "traces.transactionHash as transactionHash", ksql`null`.as("logId"), - "id as traceId", + "traces.id as traceId", ]) - .where("chainId", "=", filter.chainId) + .where("traces.chainId", "=", filter.chainId) .$call((qb) => addressSQL(qb as any, filter.fromAddress, "from")) .$call((qb) => addressSQL(qb as any, filter.toAddress, "to")) .$if(filter.includeReverted === false, (qb) => - qb.where("isReverted", "=", 0), + qb.where("traces.isReverted", "=", 0), ) .$if(filter.callType !== undefined, (qb) => - qb.where("type", "=", filter.callType!), + qb.where("traces.type", "=", filter.callType!), ) .$if(filter.functionSelector !== undefined, (qb) => { if (Array.isArray(filter.functionSelector)) { @@ -688,10 +905,30 @@ export const createSyncStore = ({ } }) .$if(filter.fromBlock !== undefined, (qb) => - qb.where("blockNumber", ">=", filter.fromBlock!.toString()), + qb.where("traces.blockNumber", ">=", filter.fromBlock!.toString()), ) .$if(filter.toBlock !== undefined, (qb) => - qb.where("blockNumber", "<=", filter.toBlock!.toString()), + qb.where("traces.blockNumber", "<=", filter.toBlock!.toString()), + ) + .innerJoin("blocks", "blocks.hash", "traces.blockHash") + .leftJoin("transactions", "transactions.hash", "traces.transactionHash") + // @ts-ignore + .select([ + ...blockColumns, + ...transactionColumns, + ...logsColumnsNull, + ...traceColumns, + ]) + .$call((qb) => + shouldGetTransactionReceipt(filter) + ? qb + .leftJoin( + "transactionReceipts", + "transactionReceipts.transactionHash", + "traces.transactionHash", + ) // @ts-ignore + .select(txReceiptColumns) + : qb.select(txReceiptColumnsNull), ); const rows = await db.wrap( @@ -714,6 +951,72 @@ export const createSyncStore = ({ blockHash: string; transactionHash: string; traceId: string; + block_baseFeePerGas: BlocksTable["baseFeePerGas"]; + block_difficulty: BlocksTable["difficulty"]; + block_extraData: BlocksTable["extraData"]; + block_gasLimit: BlocksTable["gasLimit"]; + block_gasUsed: BlocksTable["gasUsed"]; + block_hash: BlocksTable["hash"]; + block_logsBloom: BlocksTable["logsBloom"]; + block_miner: BlocksTable["miner"]; + block_mixHash: BlocksTable["mixHash"]; + block_nonce: BlocksTable["nonce"]; + block_number: BlocksTable["number"]; + block_parentHash: BlocksTable["parentHash"]; + block_receiptsRoot: BlocksTable["receiptsRoot"]; + block_sha3Uncles: BlocksTable["sha3Uncles"]; + block_size: BlocksTable["size"]; + block_stateRoot: BlocksTable["stateRoot"]; + block_timestamp: BlocksTable["timestamp"]; + block_totalDifficulty: BlocksTable["totalDifficulty"]; + block_transactionsRoot: BlocksTable["transactionsRoot"]; + log_address: LogsTable["address"]; + log_chainId: LogsTable["chainId"]; + log_data: LogsTable["data"]; + log_id: LogsTable["id"]; + log_logIndex: LogsTable["logIndex"]; + log_topic0: LogsTable["topic0"]; + log_topic1: LogsTable["topic1"]; + log_topic2: LogsTable["topic2"]; + log_topic3: LogsTable["topic3"]; + tx_accessList: TransactionsTable["accessList"]; + tx_from: TransactionsTable["from"]; + tx_gas: TransactionsTable["gas"]; + tx_gasPrice: TransactionsTable["gasPrice"]; + tx_hash: TransactionsTable["hash"]; + tx_input: TransactionsTable["input"]; + tx_maxFeePerGas: TransactionsTable["maxFeePerGas"]; + tx_maxPriorityFeePerGas: TransactionsTable["maxPriorityFeePerGas"]; + tx_nonce: TransactionsTable["nonce"]; + tx_r: TransactionsTable["r"]; + tx_s: TransactionsTable["s"]; + tx_to: TransactionsTable["to"]; + tx_transactionIndex: TransactionsTable["transactionIndex"]; + tx_type: TransactionsTable["type"]; + tx_value: TransactionsTable["value"]; + tx_v: TransactionsTable["v"]; + trace_id: TracesTable["id"]; + trace_callType: TracesTable["type"]; + trace_from: TracesTable["from"]; + trace_to: TracesTable["to"]; + trace_gas: TracesTable["gas"]; + trace_gasUsed: TracesTable["gasUsed"]; + trace_input: TracesTable["input"]; + trace_output: TracesTable["output"]; + trace_value: TracesTable["value"]; + trace_index: TracesTable["index"]; + trace_subcalls: TracesTable["subcalls"]; + trace_error: TracesTable["error"]; + trace_revertReason: TracesTable["revertReason"]; + txr_contractAddress: TransactionReceiptsTable["contractAddress"]; + txr_cumulativeGasUsed: TransactionReceiptsTable["cumulativeGasUsed"]; + txr_effectiveGasPrice: TransactionReceiptsTable["effectiveGasPrice"]; + txr_from: TransactionReceiptsTable["from"]; + txr_gasUsed: TransactionReceiptsTable["gasUsed"]; + txr_logsBloom: TransactionReceiptsTable["logsBloom"]; + txr_status: TransactionReceiptsTable["status"]; + txr_to: TransactionReceiptsTable["to"]; + txr_type: TransactionReceiptsTable["type"]; } > | undefined; @@ -743,95 +1046,7 @@ export const createSyncStore = ({ "event.filterIndex as event_filterIndex", "event.checkpoint as event_checkpoint", ]) - .innerJoin("blocks", "blocks.hash", "event.blockHash") - .select([ - "blocks.baseFeePerGas as block_baseFeePerGas", - "blocks.difficulty as block_difficulty", - "blocks.extraData as block_extraData", - "blocks.gasLimit as block_gasLimit", - "blocks.gasUsed as block_gasUsed", - "blocks.hash as block_hash", - "blocks.logsBloom as block_logsBloom", - "blocks.miner as block_miner", - "blocks.mixHash as block_mixHash", - "blocks.nonce as block_nonce", - "blocks.number as block_number", - "blocks.parentHash as block_parentHash", - "blocks.receiptsRoot as block_receiptsRoot", - "blocks.sha3Uncles as block_sha3Uncles", - "blocks.size as block_size", - "blocks.stateRoot as block_stateRoot", - "blocks.timestamp as block_timestamp", - "blocks.totalDifficulty as block_totalDifficulty", - "blocks.transactionsRoot as block_transactionsRoot", - ]) - .leftJoin("logs", "logs.id", "event.logId") - .select([ - "logs.address as log_address", - "logs.chainId as log_chainId", - "logs.data as log_data", - "logs.id as log_id", - "logs.logIndex as log_logIndex", - "logs.topic0 as log_topic0", - "logs.topic1 as log_topic1", - "logs.topic2 as log_topic2", - "logs.topic3 as log_topic3", - ]) - .leftJoin( - "transactions", - "transactions.hash", - "event.transactionHash", - ) - .select([ - "transactions.accessList as tx_accessList", - "transactions.from as tx_from", - "transactions.gas as tx_gas", - "transactions.gasPrice as tx_gasPrice", - "transactions.hash as tx_hash", - "transactions.input as tx_input", - "transactions.maxFeePerGas as tx_maxFeePerGas", - "transactions.maxPriorityFeePerGas as tx_maxPriorityFeePerGas", - "transactions.nonce as tx_nonce", - "transactions.r as tx_r", - "transactions.s as tx_s", - "transactions.to as tx_to", - "transactions.transactionIndex as tx_transactionIndex", - "transactions.type as tx_type", - "transactions.value as tx_value", - "transactions.v as tx_v", - ]) - .leftJoin("traces", "traces.id", "event.traceId") - .select([ - "traces.id as trace_id", - "traces.type as trace_callType", - "traces.from as trace_from", - "traces.to as trace_to", - "traces.gas as trace_gas", - "traces.gasUsed as trace_gasUsed", - "traces.input as trace_input", - "traces.output as trace_output", - "traces.error as trace_error", - "traces.revertReason as trace_revertReason", - "traces.value as trace_value", - "traces.index as trace_index", - "traces.subcalls as trace_subcalls", - ]) - .leftJoin( - "transactionReceipts", - "transactionReceipts.transactionHash", - "event.transactionHash", - ) - .select([ - "transactionReceipts.contractAddress as txr_contractAddress", - "transactionReceipts.cumulativeGasUsed as txr_cumulativeGasUsed", - "transactionReceipts.effectiveGasPrice as txr_effectiveGasPrice", - "transactionReceipts.from as txr_from", - "transactionReceipts.gasUsed as txr_gasUsed", - "transactionReceipts.logsBloom as txr_logsBloom", - "transactionReceipts.status as txr_status", - "transactionReceipts.to as txr_to", - "transactionReceipts.type as txr_type", - ]) + .selectAll() .where("event.checkpoint", ">", from) .where("event.checkpoint", "<=", to) .orderBy("event.checkpoint", "asc") From a3d0a14ded8fe448398a4e183001e98f9bf27b85 Mon Sep 17 00:00:00 2001 From: Sylvain Goumy Date: Tue, 10 Dec 2024 15:58:17 -0500 Subject: [PATCH 2/2] fix: union null casting and order --- packages/core/src/sync-store/index.ts | 134 ++++++++++++++++++++------ 1 file changed, 103 insertions(+), 31 deletions(-) diff --git a/packages/core/src/sync-store/index.ts b/packages/core/src/sync-store/index.ts index daf77b57e..5870265cf 100644 --- a/packages/core/src/sync-store/index.ts +++ b/packages/core/src/sync-store/index.ts @@ -506,7 +506,12 @@ export const createSyncStore = ({ {} >, address: LogFilter["address"], - column: "address" | "from" | "to", + column: + | "address" + | "transactions.from" + | "transactions.to" + | "traces.from" + | "traces.to", ) => { if (typeof address === "string") return qb.where(column, "=", address); if (isAddressFactory(address)) { @@ -564,25 +569,24 @@ export const createSyncStore = ({ const transactionColumnsNull = [ ksql`null`.as("tx_accessList"), ksql`null`.as("tx_from"), - ksql`null`.as("tx_gas"), - ksql`null`.as("tx_gasPrice"), + ksql`CAST(null AS numeric(78, 0))`.as("tx_gas"), + ksql`CAST(null AS numeric(78, 0))`.as("tx_gasPrice"), ksql`null`.as("tx_hash"), ksql`null`.as("tx_input"), - ksql`null`.as("tx_maxFeePerGas"), - ksql`null`.as("tx_maxPriorityFeePerGas"), - ksql`null`.as("tx_nonce"), + ksql`CAST(null AS numeric(78, 0))`.as("tx_maxFeePerGas"), + ksql`CAST(null AS numeric(78, 0))`.as("tx_maxPriorityFeePerGas"), + ksql`CAST(null AS int4)`.as("tx_nonce"), ksql`null`.as("tx_r"), ksql`null`.as("tx_s"), ksql`null`.as("tx_to"), - ksql`null`.as("tx_transactionIndex"), + ksql`CAST(null AS int4)`.as("tx_transactionIndex"), ksql`null`.as("tx_type"), - ksql`null`.as("tx_value"), - ksql`null`.as("tx_v"), + ksql`CAST(null AS numeric(78, 0))`.as("tx_value"), + ksql`CAST(null AS numeric(78, 0))`.as("tx_v"), ]; const logsColumns = [ "logs.address as log_address", - "logs.chainId as log_chainId", "logs.data as log_data", "logs.id as log_id", "logs.logIndex as log_logIndex", @@ -593,10 +597,9 @@ export const createSyncStore = ({ ]; const logsColumnsNull = [ ksql`null`.as("log_address"), - ksql`null`.as("log_chainId"), ksql`null`.as("log_data"), ksql`null`.as("log_id"), - ksql`null`.as("log_logIndex"), + ksql`CAST(null AS int4)`.as("log_logIndex"), ksql`null`.as("log_topic0"), ksql`null`.as("log_topic1"), ksql`null`.as("log_topic2"), @@ -605,7 +608,7 @@ export const createSyncStore = ({ const traceColumns = [ "traces.id as trace_id", - "traces.callType as trace_callType", + "traces.type as trace_callType", "traces.from as trace_from", "traces.to as trace_to", "traces.gas as trace_gas", @@ -623,15 +626,15 @@ export const createSyncStore = ({ ksql`null`.as("trace_callType"), ksql`null`.as("trace_from"), ksql`null`.as("trace_to"), - ksql`null`.as("trace_gas"), - ksql`null`.as("trace_gasUsed"), + ksql`CAST(null AS numeric(78, 0))`.as("trace_gas"), + ksql`CAST(null AS numeric(78, 0))`.as("trace_gasUsed"), ksql`null`.as("trace_input"), ksql`null`.as("trace_output"), ksql`null`.as("trace_error"), ksql`null`.as("trace_revertReason"), - ksql`null`.as("trace_value"), - ksql`null`.as("trace_index"), - ksql`null`.as("trace_subcalls"), + ksql`CAST(null AS numeric(78, 0))`.as("trace_value"), + ksql`CAST(null AS int4)`.as("trace_index"), + ksql`CAST(null AS int4)`.as("trace_subcalls"), ]; const txReceiptColumns = [ @@ -647,10 +650,10 @@ export const createSyncStore = ({ ]; const txReceiptColumnsNull = [ ksql`null`.as("txr_contractAddress"), - ksql`null`.as("txr_cumulativeGasUsed"), - ksql`null`.as("txr_effectiveGasPrice"), + ksql`CAST(null AS numeric(78, 0))`.as("txr_cumulativeGasUsed"), + ksql`CAST(null AS numeric(78, 0))`.as("txr_effectiveGasPrice"), ksql`null`.as("txr_from"), - ksql`null`.as("txr_gasUsed"), + ksql`CAST(null AS numeric(78, 0))`.as("txr_gasUsed"), ksql`null`.as("txr_logsBloom"), ksql`null`.as("txr_status"), ksql`null`.as("txr_to"), @@ -703,8 +706,8 @@ export const createSyncStore = ({ // @ts-ignore .select([ ...blockColumns, - ...logsColumns, ...transactionColumns, + ...logsColumns, ...traceColumnsNull, ]) .$call((qb) => @@ -749,8 +752,8 @@ export const createSyncStore = ({ ) // @ts-ignore .select([ ...blockColumns, - ...logsColumnsNull, ...transactionColumnsNull, + ...logsColumnsNull, ...traceColumnsNull, ...txReceiptColumnsNull, ]); @@ -772,8 +775,12 @@ export const createSyncStore = ({ ksql`null`.as("traceId"), ]) .where("transactions.chainId", "=", filter.chainId) - .$call((qb) => addressSQL(qb as any, filter.fromAddress, "from")) - .$call((qb) => addressSQL(qb as any, filter.toAddress, "to")) + .$call((qb) => + addressSQL(qb as any, filter.fromAddress, "transactions.from"), + ) + .$call((qb) => + addressSQL(qb as any, filter.toAddress, "transactions.to"), + ) .$if(filter.includeReverted === false, (qb) => qb.where( db @@ -839,8 +846,8 @@ export const createSyncStore = ({ "traces.id as traceId", ]) .where("traces.chainId", "=", filter.chainId) - .$call((qb) => addressSQL(qb as any, filter.fromAddress, "from")) - .$call((qb) => addressSQL(qb as any, filter.toAddress, "to")) + .$call((qb) => addressSQL(qb as any, filter.fromAddress, "traces.from")) + .$call((qb) => addressSQL(qb as any, filter.toAddress, "traces.to")) .where("traces.value", ">", "0") .$if(filter.includeReverted === false, (qb) => qb.where("traces.isReverted", "=", 0), @@ -889,8 +896,8 @@ export const createSyncStore = ({ "traces.id as traceId", ]) .where("traces.chainId", "=", filter.chainId) - .$call((qb) => addressSQL(qb as any, filter.fromAddress, "from")) - .$call((qb) => addressSQL(qb as any, filter.toAddress, "to")) + .$call((qb) => addressSQL(qb as any, filter.fromAddress, "traces.from")) + .$call((qb) => addressSQL(qb as any, filter.toAddress, "traces.to")) .$if(filter.includeReverted === false, (qb) => qb.where("traces.isReverted", "=", 0), ) @@ -971,7 +978,6 @@ export const createSyncStore = ({ block_totalDifficulty: BlocksTable["totalDifficulty"]; block_transactionsRoot: BlocksTable["transactionsRoot"]; log_address: LogsTable["address"]; - log_chainId: LogsTable["chainId"]; log_data: LogsTable["data"]; log_id: LogsTable["id"]; log_logIndex: LogsTable["logIndex"]; @@ -1046,7 +1052,73 @@ export const createSyncStore = ({ "event.filterIndex as event_filterIndex", "event.checkpoint as event_checkpoint", ]) - .selectAll() + .select([ + "event.block_hash as block_hash", + "event.block_baseFeePerGas as block_baseFeePerGas", + "event.block_difficulty as block_difficulty", + "event.block_extraData as block_extraData", + "event.block_gasLimit as block_gasLimit", + "event.block_gasUsed as block_gasUsed", + "event.block_logsBloom as block_logsBloom", + "event.block_miner as block_miner", + "event.block_mixHash as block_mixHash", + "event.block_nonce as block_nonce", + "event.block_number as block_number", + "event.block_parentHash as block_parentHash", + "event.block_receiptsRoot as block_receiptsRoot", + "event.block_sha3Uncles as block_sha3Uncles", + "event.block_size as block_size", + "event.block_stateRoot as block_stateRoot", + "event.block_timestamp as block_timestamp", + "event.block_totalDifficulty as block_totalDifficulty", + "event.block_transactionsRoot as block_transactionsRoot", + "event.log_address as log_address", + "event.log_data as log_data", + "event.log_id as log_id", + "event.log_logIndex as log_logIndex", + "event.log_topic0 as log_topic0", + "event.log_topic1 as log_topic1", + "event.log_topic2 as log_topic2", + "event.log_topic3 as log_topic3", + "event.tx_hash as tx_hash", + "event.tx_from as tx_from", + "event.tx_to as tx_to", + "event.tx_gas as tx_gas", + "event.tx_gasPrice as tx_gasPrice", + "event.tx_input as tx_input", + "event.tx_maxFeePerGas as tx_maxFeePerGas", + "event.tx_maxPriorityFeePerGas as tx_maxPriorityFeePerGas", + "event.tx_nonce as tx_nonce", + "event.tx_r as tx_r", + "event.tx_s as tx_s", + "event.tx_transactionIndex as tx_transactionIndex", + "event.tx_type as tx_type", + "event.tx_value as tx_value", + "event.tx_v as tx_v", + "event.tx_accessList as tx_accessList", + "event.trace_id as trace_id", + "event.trace_callType as trace_callType", + "event.trace_from as trace_from", + "event.trace_to as trace_to", + "event.trace_gas as trace_gas", + "event.trace_gasUsed as trace_gasUsed", + "event.trace_input as trace_input", + "event.trace_output as trace_output", + "event.trace_value as trace_value", + "event.trace_index as trace_index", + "event.trace_subcalls as trace_subcalls", + "event.trace_error as trace_error", + "event.trace_revertReason as trace_revertReason", + "event.txr_contractAddress as txr_contractAddress", + "event.txr_cumulativeGasUsed as txr_cumulativeGasUsed", + "event.txr_effectiveGasPrice as txr_effectiveGasPrice", + "event.txr_from as txr_from", + "event.txr_gasUsed as txr_gasUsed", + "event.txr_logsBloom as txr_logsBloom", + "event.txr_status as txr_status", + "event.txr_to as txr_to", + "event.txr_type as txr_type", + ]) .where("event.checkpoint", ">", from) .where("event.checkpoint", "<=", to) .orderBy("event.checkpoint", "asc")