-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api-evm): implement
eth_getBlockByNumber
(#779)
* Add missing packages * Remove unused options from app.json * Add basic eth_getBlockByNumber * Use block resource * Return hashes or object * gasLimit from milestones * Allow latest tags
- Loading branch information
1 parent
f454817
commit 805425a
Showing
6 changed files
with
100 additions
and
14 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
packages/api-evm/source/actions/eth-get-block-by-number.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { inject, injectable } from "@mainsail/container"; | ||
import { Contracts, Identifiers } from "@mainsail/contracts"; | ||
|
||
import { BlockResource } from "../resources/index.js"; | ||
|
||
@injectable() | ||
export class EthGetBlockByNumberAction implements Contracts.Api.RPC.Action { | ||
public readonly name: string = "eth_getBlockByNumber"; | ||
|
||
@inject(Identifiers.Application.Instance) | ||
private readonly app!: Contracts.Kernel.Application; | ||
|
||
@inject(Identifiers.State.Store) | ||
private readonly stateStore!: Contracts.State.Store; | ||
|
||
@inject(Identifiers.Database.Service) | ||
private readonly databaseService!: Contracts.Database.DatabaseService; | ||
|
||
public readonly schema = { | ||
$id: `jsonRpc_${this.name}`, | ||
|
||
maxItems: 2, | ||
minItems: 2, | ||
|
||
prefixItems: [ | ||
{ oneOf: [{ $ref: "prefixedHex" }, { enum: ["latest", "finalized", "safe"], type: "string" }] }, // TODO: Extract block tag | ||
{ type: "boolean" }, | ||
], | ||
type: "array", | ||
}; | ||
|
||
public async handle(parameters: [string, boolean]): Promise<object | null> { | ||
const height = parameters[0].startsWith("0x") ? Number.parseInt(parameters[0]) : this.stateStore.getHeight(); | ||
const transactionObject = parameters[1]; | ||
|
||
const commit = await this.databaseService.getCommit(height); | ||
|
||
if (!commit) { | ||
// eslint-disable-next-line unicorn/no-null | ||
return null; | ||
} | ||
|
||
return this.app.resolve(BlockResource).transform(commit.block, transactionObject); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { inject, injectable } from "@mainsail/container"; | ||
import { Contracts, Identifiers } from "@mainsail/contracts"; | ||
|
||
@injectable() | ||
export class BlockResource { | ||
@inject(Identifiers.Cryptography.Configuration) | ||
private readonly configuration!: Contracts.Crypto.Configuration; | ||
|
||
public async transform(block: Contracts.Crypto.Block, transactionObject: boolean): Promise<object> { | ||
const blockData: Contracts.Crypto.BlockData = block.data; | ||
|
||
const milestone = this.configuration.getMilestone(blockData.height); | ||
|
||
/* eslint-disable sort-keys-fix/sort-keys-fix */ | ||
return { | ||
number: `0x${blockData.height.toString(16)}`, | ||
hash: `0x${blockData.id}`, | ||
parentHash: `0x${blockData.previousBlock}`, | ||
nonce: "0x0", | ||
sha3Uncles: "0x1dcc4de8dec75d7aab85b567b6ccd41ad4e2a311b82e5872087ed76f0f1ccf8f", // No uncles in ARK, this is hash of empty list | ||
logsBloom: "", // TODO: Implement logs bloom, | ||
transactionsRoot: `0x${blockData.stateHash}`, | ||
stateRoot: `0x${blockData.stateHash}`, | ||
receiptsRoot: `0x${blockData.stateHash}`, | ||
miner: blockData.generatorAddress, | ||
difficulty: "0x0", | ||
totalDifficulty: "0x0", | ||
extraData: "0x", | ||
size: `0x${blockData.payloadLength.toString(16)}`, // TODO: Implement block size | ||
gasLimit: `0x${milestone.block.maxGasLimit.toString(16)}`, | ||
gasUsed: `0x${blockData.totalGasUsed.toString(16)}`, | ||
timestamp: `0x${blockData.timestamp.toString(16)}`, | ||
transactions: transactionObject | ||
? block.transactions.map((transaction) => transaction.data) | ||
: block.transactions.map((transaction) => transaction.id), | ||
uncles: [], | ||
}; | ||
/* eslint-enable sort-keys-fix/sort-keys-fix */ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./block.js"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters