Skip to content
This repository has been archived by the owner on Mar 24, 2023. It is now read-only.

Commit

Permalink
Merge pull request #550 from godwokenrises/order-txs-and-logs
Browse files Browse the repository at this point in the history
fix: Return txs in block / logs in tx in correct order
  • Loading branch information
RetricSu authored Oct 15, 2022
2 parents aafa02e + 70e45fc commit c01aa34
Showing 1 changed file with 12 additions and 31 deletions.
43 changes: 12 additions & 31 deletions packages/api-server/src/db/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,13 @@ export class Query {
return await this.getTransactions({ block_number: blockNumber.toString() });
}

// Order by `transaction_index`, now only for search txs in a block
private async getTransactions(
params: Readonly<Partial<KnexType.MaybeRawRecord<DBTransaction>>>
): Promise<Transaction[]> {
const transactions = await this.knex<DBTransaction>("transactions").where(
params
);
const transactions = await this.knex<DBTransaction>("transactions")
.where(params)
.orderBy("transaction_index", "asc");

return transactions.map((tx) => formatTransaction(tx));
}
Expand Down Expand Up @@ -225,30 +226,6 @@ export class Query {
return formatTransaction(transaction);
}

async getTransactionHashesByBlockHash(blockHash: Hash): Promise<Hash[]> {
return await this.getTransactionHashes({
block_hash: hexToBuffer(blockHash),
});
}

async getTransactionHashesByBlockNumber(
blockNumber: bigint
): Promise<Hash[]> {
return await this.getTransactionHashes({
block_number: blockNumber.toString(),
});
}

private async getTransactionHashes(
params: Readonly<Partial<KnexType.MaybeRawRecord<DBTransaction>>>
): Promise<Hash[]> {
const transactionHashes = await this.knex<DBTransaction>("transactions")
.select("hash")
.where(params);

return transactionHashes.map((tx) => bufferToHex(tx.hash));
}

async getTransactionEthHashesByBlockHash(blockHash: Hash): Promise<Hash[]> {
return await this.getTransactionEthHashes({
block_hash: hexToBuffer(blockHash),
Expand All @@ -263,12 +240,14 @@ export class Query {
});
}

// Order by `transaction_index`, only for search tx eth hashes in a block.
private async getTransactionEthHashes(
params: Readonly<Partial<KnexType.MaybeRawRecord<DBTransaction>>>
): Promise<Hash[]> {
const transactionHashes = await this.knex<DBTransaction>("transactions")
.select("eth_tx_hash")
.where(params);
.where(params)
.orderBy("transaction_index", "asc");

return transactionHashes.map((tx) => bufferToHex(tx.eth_tx_hash));
}
Expand Down Expand Up @@ -309,9 +288,11 @@ export class Query {
return undefined;
}

const logs = await this.knex<DBLog>("logs").where({
transaction_hash: hexToBuffer(txHash),
});
const logs = await this.knex<DBLog>("logs")
.where({
transaction_hash: hexToBuffer(txHash),
})
.orderBy("log_index", "asc");

return [formatTransaction(tx), logs.map((log) => formatLog(log))];
}
Expand Down

0 comments on commit c01aa34

Please sign in to comment.