-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.ts
77 lines (64 loc) · 1.69 KB
/
cli.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
import { AccountState, Context, evm } from "./bytecode-parser";
import { hexStringToUint8Array } from "./opcodes";
import yargs from "yargs/yargs";
import { hideBin } from "yargs/helpers";
type Transaction = {
from?: string,
to?: string,
value?: number,
gasLeft?: number
}
const t = await yargs(hideBin(process.argv))
.option("verbose", {
alias: "v",
describe: "output the trace of each of the operations",
choices: [0, 1, 2, 3],
default: 1
})
.option("code", {
alias: "c",
describe: "the bytecode to run",
type: "string",
coerce: (codeString: string) => {
if (codeString.slice(0, 2) == "0x") return hexStringToUint8Array(codeString.slice(2,));
return hexStringToUint8Array(codeString);
},
})
.option("tx", {
describe: "Transaction details",
coerce: (tx: Transaction) => tx,
})
.demandOption("code")
.argv;
const CONTRACT_ADDRESS = BigInt(t?.tx?.to || 0xee);
const worldState = new Map<bigint, AccountState>();
worldState.set(CONTRACT_ADDRESS, {
balance: worldState.get(CONTRACT_ADDRESS)?.balance || 0n,
code: {
bin: t.code
},
storage: new Map<bigint, bigint>(),
nonce: 0n
});
const context: Context = {
address: CONTRACT_ADDRESS,
caller: BigInt(t?.tx?.from || 0x00),
origin: BigInt(t?.tx?.from || 0x00),
gasPrice: 1n,
gasLeft: t?.tx?.gasLeft || 15_000_000,
isStatic: false,
callValue: BigInt(t.tx?.value || 0),
callData: new Uint8Array(0),
bytecode: t.code,
block: {
basefee: BigInt(0n),
coinbase: BigInt(0n),
timestamp: BigInt(0n),
number: BigInt(0n),
difficulty: BigInt(0n),
gasLimit: BigInt(0n),
chainId: BigInt(1n),
},
state: worldState,
}
evm(context, t.verbose);