Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Add evm_transactionSimulation end point for experimental purposes #4152

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
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
29 changes: 29 additions & 0 deletions src/chains/ethereum/ethereum/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { BaseFeeHeader, Block, RuntimeBlock } from "@ganache/ethereum-block";
import {
Transaction,
TransactionFactory,
calculateIntrinsicGas,
TypedTransaction
} from "@ganache/ethereum-transaction";
import {
Expand Down Expand Up @@ -1914,6 +1915,34 @@ export default class EthereumApi implements Api {
return null;
}

async evm_simulateTransaction(
transaction: Ethereum.Transaction
): Promise<Quantity> {
const data = Data.from(transaction.data);
const intrinsicGas = calculateIntrinsicGas(
data,
true,
this.#blockchain.common
);
console.time("eth_simulateTransaction");
const blockchain = this.#blockchain;
const minerOptions = this.#options.miner;

const fromAddress = Address.from(transaction.from);
const toAddress = Address.from(transaction.to);
const code = await blockchain.accounts.getCode(toAddress);
const latest = blockchain.blocks.latest;

const result = await blockchain.vm.evm.runCode({
code: code.toBuffer(),
gasLimit: minerOptions.callGasLimit.toBigInt(),
caller: fromAddress,
address: toAddress,
data: data.toBuffer()
});
console.timeEnd("eth_simulateTransaction");
return Quantity.from(result.executionGasUsed + intrinsicGas);
}
/**
* Creates new message call transaction or a contract creation, if the data field contains code.
*
Expand Down