Skip to content

Commit

Permalink
feat(api-evm): resolve block tag parameter (#856)
Browse files Browse the repository at this point in the history
* resolve block tag

* Empty commit

* style: resolve style guide violations
  • Loading branch information
oXtxNt9U authored Feb 10, 2025
1 parent 70e0bdf commit 2c25692
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
5 changes: 3 additions & 2 deletions packages/api-evm/source/actions/eth-get-block-by-number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { inject, injectable } from "@mainsail/container";
import { Contracts, Identifiers } from "@mainsail/contracts";

import { BlockResource } from "../resources/index.js";
import { resolveBlockTag } from "../utils/resolve-block-tag.js";

@injectable()
export class EthGetBlockByNumberAction implements Contracts.Api.RPC.Action {
Expand All @@ -26,8 +27,8 @@ export class EthGetBlockByNumberAction implements Contracts.Api.RPC.Action {
type: "array",
};

public async handle(parameters: [string, boolean]): Promise<object | null> {
const height = parameters[0].startsWith("0x") ? Number.parseInt(parameters[0]) : this.stateStore.getHeight();
public async handle(parameters: [string | Contracts.Crypto.BlockTag, boolean]): Promise<object | null> {
const height = await resolveBlockTag(this.stateStore, parameters[0]);
const transactionObject = parameters[1];

const block = await this.databaseService.getBlock(height);
Expand Down
21 changes: 21 additions & 0 deletions packages/api-evm/source/utils/resolve-block-tag.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Contracts } from "@mainsail/contracts";

export const resolveBlockTag = async (
stateStore: Contracts.State.Store,
tag: string | Contracts.Crypto.BlockTag,
): Promise<number> => {
if (tag.startsWith("0x")) {
return Number.parseInt(tag);
}

switch (tag as Contracts.Crypto.BlockTag) {
case "finalized":
case "latest":
case "safe": {
return stateStore.getHeight();
}
default: {
throw new Error("invalid blockTag:" + tag);
}
}
};
2 changes: 1 addition & 1 deletion packages/api-evm/source/validation/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const schemas: Record<"blockTag", AnySchemaObject> = {
$id: "blockTag",
anyOf: [
{
enum: ["latest", "finalized", "safe"],
enum: ["latest", "safe", "finalized"],
type: "string",
},
{
Expand Down

0 comments on commit 2c25692

Please sign in to comment.