Skip to content

Instantly share code, notes, and snippets.

@wong2
Created September 6, 2022 04:13
Show Gist options
  • Save wong2/a343d8e48c9b0708edd16b06867fe265 to your computer and use it in GitHub Desktop.
Save wong2/a343d8e48c9b0708edd16b06867fe265 to your computer and use it in GitHub Desktop.
import { Chain, Common } from "@nomicfoundation/ethereumjs-common";
import { TransactionFactory } from "@nomicfoundation/ethereumjs-tx";
import { VM } from "@nomicfoundation/ethereumjs-vm";
import { ForkBlockchain } from "hardhat/internal/hardhat-network/provider/fork/ForkBlockchain";
import { ForkStateManager } from "hardhat/internal/hardhat-network/provider/fork/ForkStateManager";
import { makeForkClient } from "hardhat/internal/hardhat-network/provider/utils/makeForkClient";
const JSON_RPC_URL = "https://rpc.ankr.com/eth";
async function simulateTx(signedTx: string) {
const tx = TransactionFactory.fromSerializedData(Buffer.from(signedTx.substring(2), "hex"));
const common = new Common({ chain: Chain.Mainnet });
const { forkClient, forkBlockNumber } = await makeForkClient({
jsonRpcUrl: JSON_RPC_URL,
});
const stateManager = new ForkStateManager(forkClient, forkBlockNumber);
const blockchain = new ForkBlockchain(forkClient, forkBlockNumber, common);
const vm = await VM.create({ common, stateManager, blockchain });
return vm.runTx({ tx });
}
export { simulateTx };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment