Skip to content

Commit

Permalink
feat(api-evm): implement eth_getTransactionByBlockNumberAndIndex (#816
Browse files Browse the repository at this point in the history
)

* Add action

* Add database method
  • Loading branch information
sebastijankuzner authored Jan 10, 2025
1 parent cdb4d80 commit 4560e78
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { inject, injectable } from "@mainsail/container";
import { Contracts, Identifiers } from "@mainsail/contracts";

import { TransactionResource } from "../resources/index.js";

@injectable()
export class EthGetTransactionByBlockNumberAndIndex implements Contracts.Api.RPC.Action {
@inject(Identifiers.Application.Instance)
private readonly app!: Contracts.Kernel.Application;

@inject(Identifiers.Database.Service)
private readonly databaseService!: Contracts.Database.DatabaseService;

public readonly name: string = "eth_getTransactionByBlockNumberAndIndex";

public readonly schema = {
$id: `jsonRpc_${this.name}`,

maxItems: 2,
minItems: 2,

prefixItems: [{ $ref: "prefixedHex" }, { $ref: "prefixedHex" }], // TODO: Limit sequence
type: "array",
};

public async handle(parameters: [string, string]): Promise<any> {
const transaction = await this.databaseService.getTransactionByBlockHeightAndIndex(
Number.parseInt(parameters[0]),
Number.parseInt(parameters[1]),
);

if (!transaction) {
return null;
}

return this.app.resolve(TransactionResource).transform(transaction.data);
}
}
1 change: 1 addition & 0 deletions packages/api-evm/source/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export * from "./eth-get-block-transaction-count-by-number.js";
export * from "./eth-get-code.js";
export * from "./eth-get-storage-at.js";
export * from "./eth-get-transaction-by-block-hash-and-index.js";
export * from "./eth-get-transaction-by-block-number-and-index.js";
export * from "./eth-get-transaction-by-hash.js";
export * from "./eth-get-transaction-count.js";
export * from "./eth-get-uncle-by-block-hash-and-index.js";
Expand Down
2 changes: 2 additions & 0 deletions packages/api-evm/source/service-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
EthGetCodeAction,
EthGetStorageAtAction,
EthGetTransactionByBlockHashAndIndex,
EthGetTransactionByBlockNumberAndIndex,
EthGetTransactionByHash,
EthGetTransactionCount,
EthGetUncleByBlockHashAndIndex,
Expand Down Expand Up @@ -84,6 +85,7 @@ export class ServiceProvider extends AbstractServiceProvider<Server> {
this.app.resolve(EthGetCodeAction),
this.app.resolve(EthGetStorageAtAction),
this.app.resolve(EthGetTransactionByBlockHashAndIndex),
this.app.resolve(EthGetTransactionByBlockNumberAndIndex),
this.app.resolve(EthGetTransactionByHash),
this.app.resolve(EthGetTransactionCount),
this.app.resolve(EthGetUncleByBlockHashAndIndex),
Expand Down
1 change: 1 addition & 0 deletions packages/contracts/source/contracts/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export interface DatabaseService {

getTransactionById(id: string): Promise<Transaction | undefined>;
getTransactionByBlockIdAndIndex(blockId: string, index: number): Promise<Transaction | undefined>;
getTransactionByBlockHeightAndIndex(height: number, index: number): Promise<Transaction | undefined>;

addCommit(block: Commit): void;
persist(): Promise<void>;
Expand Down
19 changes: 19 additions & 0 deletions packages/database/source/database-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,25 @@ export class DatabaseService implements Contracts.Database.DatabaseService {
return this.#readTransaction(`${height}-${index}`);
}

public async getTransactionByBlockHeightAndIndex(
height: number,
index: number,
): Promise<Contracts.Crypto.Transaction | undefined> {
// Get TX from cache
if (this.#commitCache.has(height)) {
const block = this.#commitCache.get(height)!.block;

if (block.transactions.length <= index) {
return undefined;
}

return block.transactions[index];
}

// Get TX from storage
return this.#readTransaction(`${height}-${index}`);
}

public async *readCommits(start: number, end: number): AsyncGenerator<Contracts.Crypto.Commit> {
for (let height = start; height <= end; height++) {
const data = await this.#readCommitBytes(height);
Expand Down

0 comments on commit 4560e78

Please sign in to comment.