-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
execution.ts
136 lines (117 loc) · 3.64 KB
/
execution.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import {
bigIntToHex,
bytesToHex,
privateToAddress,
toBytes,
} from "@nomicfoundation/ethereumjs-util";
import { MessageTrace } from "../../../../src/internal/hardhat-network/stack-traces/message-trace";
import { defaultHardhatNetworkParams } from "../../../../src/internal/core/config/default-config";
import {
MempoolOrder,
TracingConfig,
} from "../../../../src/internal/hardhat-network/provider/node-types";
import { EdrProviderWrapper } from "../../../../src/internal/hardhat-network/provider/provider";
import { VMTracer } from "../../../../src/internal/hardhat-network/stack-traces/vm-tracer";
import { LoggerConfig } from "../../../../src/internal/hardhat-network/provider/modules/logger";
function toBuffer(x: Parameters<typeof toBytes>[0]) {
return Buffer.from(toBytes(x));
}
const abi = require("ethereumjs-abi");
const senderPrivateKey =
"0xe331b6d69882b4cb4ea581d88e0b604039a3de5967688d3dcffdd2270c0fd109";
const senderAddress = bytesToHex(privateToAddress(toBuffer(senderPrivateKey)));
export async function instantiateProvider(
loggerConfig: LoggerConfig,
tracingConfig: TracingConfig
): Promise<EdrProviderWrapper> {
const config = {
hardfork: "shanghai",
chainId: 1,
networkId: 1,
blockGasLimit: 10_000_000,
minGasPrice: 0n,
automine: true,
intervalMining: 0,
mempoolOrder: "priority" as MempoolOrder,
chains: defaultHardhatNetworkParams.chains,
genesisAccounts: [
{
privateKey: senderPrivateKey,
balance: 1e15,
},
],
allowUnlimitedContractSize: false,
throwOnTransactionFailures: false,
throwOnCallFailures: false,
allowBlocksWithSameTimestamp: false,
coinbase: "0x0000000000000000000000000000000000000000",
initialBaseFeePerGas: 0,
enableTransientStorage: false,
};
const provider = await EdrProviderWrapper.create(
config,
loggerConfig,
tracingConfig
);
return provider;
}
export function encodeConstructorParams(
contractAbi: any[],
params: any[]
): Buffer {
const fAbi = contractAbi.find((a) => a.type === "constructor");
if (fAbi === undefined || params.length === 0) {
return Buffer.from([]);
}
const types = fAbi.inputs.map((i: any) => i.type);
return abi.rawEncode(types, params);
}
export function encodeCall(
contractAbi: any[],
functionName: string,
params: any[]
): Buffer {
const fAbi = contractAbi.find(
(a) => a.name === functionName && a.inputs.length === params.length
);
const types = fAbi.inputs.map((i: any) => i.type);
const methodId = abi.methodID(functionName, types);
return Buffer.concat([methodId, abi.rawEncode(types, params)]);
}
export interface TxData {
data: Buffer;
to?: Buffer;
value?: bigint;
gas?: bigint;
}
export async function traceTransaction(
provider: EdrProviderWrapper,
txData: TxData
): Promise<MessageTrace> {
const vmTracer = new VMTracer();
provider.setVmTracer(vmTracer);
try {
await provider.request({
method: "eth_sendTransaction",
params: [
{
from: senderAddress,
data: bytesToHex(txData.data),
to: txData.to !== undefined ? bytesToHex(txData.to) : undefined,
value: bigIntToHex(txData.value ?? 0n),
// If the test didn't define a gasLimit, we assume 4M is enough
gas: bigIntToHex(txData.gas ?? 4000000n),
gasPrice: bigIntToHex(10n),
},
],
});
const trace = vmTracer.getLastTopLevelMessageTrace();
if (trace === undefined) {
const error = vmTracer.getLastError();
throw error ?? new Error("Cannot get last top level message trace");
}
return trace;
} finally {
provider.setVmTracer(undefined);
}
}