diff --git a/crates/indexer/src/indexer.rs b/crates/indexer/src/indexer.rs index 5c1705be..dd7e9be8 100644 --- a/crates/indexer/src/indexer.rs +++ b/crates/indexer/src/indexer.rs @@ -278,6 +278,7 @@ impl Web3Indexer { let web3_logs = { let mut logs: Vec = vec![]; + // log_index is a log's index in block, not transaction, should update later. let mut log_index = 0; for log_item in log_item_vec { let log = parse_log(&log_item, &gw_tx_hash)?; @@ -465,6 +466,7 @@ impl Web3Indexer { let mut pg_tx = pool.begin().await?; let mut tx_index_cursor: u32 = 0; + let mut log_index_cursor: u32 = 0; let mut cumulative_gas_used: u128 = 0; let mut total_gas_limit: u128 = 0; @@ -484,11 +486,13 @@ impl Web3Indexer { let transaction_index = tx_index as u32 + tx_index_cursor; tx.tx.transaction_index = transaction_index; // update log.transaction_index too + // Update tx.log.index tx.logs = tx .logs .into_iter() .map(|mut log| { log.transaction_index = transaction_index; + log.log_index += log_index_cursor; log }) .collect(); @@ -496,6 +500,7 @@ impl Web3Indexer { tx.tx.cumulative_gas_used = cumulative_gas_used; total_gas_limit += tx.tx.gas_limit; + log_index_cursor += tx.logs.len() as u32; tx }) diff --git a/packages/api-server/migrations/20221018014620_fix_log_index.ts b/packages/api-server/migrations/20221018014620_fix_log_index.ts new file mode 100644 index 00000000..be3c322b --- /dev/null +++ b/packages/api-server/migrations/20221018014620_fix_log_index.ts @@ -0,0 +1,10 @@ +import { Knex } from "knex"; + +// Fix for wrong logs.log_index +export async function up(knex: Knex): Promise { + await knex.raw( + "with cte as(select block_number, id, log_index, row_number() over (partition by block_number order by id) rn from logs) update logs set log_index=cte.rn - 1 from cte where logs.block_number=cte.block_number and logs.id=cte.id;" + ); +} + +export async function down(knex: Knex): Promise {}