Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(api-evm): implement eth_estimateGas #836

Draft
wants to merge 5 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/api-evm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"@mainsail/container": "workspace:*",
"@mainsail/contracts": "workspace:*",
"@mainsail/kernel": "workspace:*",
"dayjs": "1.11.10",
"ethers": "6.11.0",
"joi": "17.12.2"
},
Expand Down
91 changes: 91 additions & 0 deletions packages/api-evm/source/actions/eth-estimate-gas.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { inject, injectable, tagged } from "@mainsail/container";
import { Contracts, Exceptions, Identifiers } from "@mainsail/contracts";
import dayjs from "dayjs";

type TxData = {
from: string;
to: string;
data: string;
gas: string;
gasPrice: string;
value: string;
};

@injectable()
export class EthEstimateGasAction implements Contracts.Api.RPC.Action {
@inject(Identifiers.Evm.Instance)
@tagged("instance", "validator")
private readonly evm!: Contracts.Evm.Instance;

@inject(Identifiers.Cryptography.Configuration)
protected readonly configuration!: Contracts.Crypto.Configuration;

public readonly name: string = "eth_estimateGas";

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

maxItems: 2,
minItems: 2,

prefixItems: [
{
additionalProperties: false,
properties: {
data: { $ref: "prefixedHex" },
from: { $ref: "address" },
gas: { $ref: "prefixedHex" },
gasPrice: { $ref: "prefixedHex" },
to: { $ref: "address" },
value: { $ref: "prefixedHex" },
},
required: ["from", "to", "data", "gas", "gasPrice"],
type: "object",
},
{ $ref: "blockTag" },
],

type: "array",
};

public async handle(parameters: [TxData, Contracts.Crypto.BlockTag]): Promise<any> {
const [data] = parameters;

const { evmSpec } = this.configuration.getMilestone();

const accountInfo = await this.evm.getAccountInfo(data.from);

const commitKey = { height: BigInt(this.configuration.getHeight()), round: BigInt(0) };

const dataToProcess = {
blockContext: {
commitKey,
gasLimit: BigInt(data.gas),
timestamp: BigInt(dayjs().valueOf()),
validatorAddress: "0x0000000000000000000000000000000000000001",
},
caller: data.from,
data: Buffer.from(data.data.slice(2), "hex"),
gasLimit: BigInt(data.gas),
gasPrice: BigInt(data.gasPrice),
nonce: accountInfo.nonce,
recipient: data.to,
specId: evmSpec,
txHash: "0".repeat(64),
value: BigInt(data.value),
};

await this.evm.prepareNextCommit({
commitKey,
});

const { receipt } = await this.evm.process(dataToProcess);
const { success, gasUsed } = receipt;

if (success) {
return `0x${gasUsed.toString(16)}`;
}

throw new Exceptions.RpcError("execution reverted");
}
}
1 change: 1 addition & 0 deletions packages/api-evm/source/actions/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from "./eth-block-number.js";
export * from "./eth-call.js";
export * from "./eth-estimate-gas.js";
export * from "./eth-get-balance.js";
export * from "./eth-get-block-by-hash.js";
export * from "./eth-get-block-by-number.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 @@ -5,6 +5,7 @@ import Joi from "joi";
import {
CallAction,
EthBlockNumberAction,
EthEstimateGasAction,
EthGetBalanceAction,
EthGetBlockByHashAction,
EthGetBlockByNumberAction,
Expand Down Expand Up @@ -77,6 +78,7 @@ export class ServiceProvider extends AbstractServiceProvider<Server> {
return [
this.app.resolve(CallAction),
this.app.resolve(EthBlockNumberAction),
this.app.resolve(EthEstimateGasAction),
this.app.resolve(EthGetBalanceAction),
this.app.resolve(EthGetBlockByHashAction),
this.app.resolve(EthGetBlockByNumberAction),
Expand Down
2 changes: 1 addition & 1 deletion packages/core/bin/config/testnet/core/crypto.json
Original file line number Diff line number Diff line change
Expand Up @@ -2595,7 +2595,7 @@
},
"timeouts": {
"blockPrepareTime": 4000,
"blockTime": 8000,
"blockTime": 2000,
"stageTimeout": 2000,
"stageTimeoutIncrease": 2000,
"tolerance": 100
Expand Down
2 changes: 2 additions & 0 deletions packages/evm-service/source/instances/evm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ export class EvmInstance implements Contracts.Evm.Instance {
}

public async process(txContext: Contracts.Evm.TransactionContext): Promise<Contracts.Evm.ProcessResult> {
console.log(txContext);

return this.#evm.process(txContext);
}

Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading