|
| 1 | +import yargs from 'yargs' |
| 2 | +import { hideBin } from 'yargs/helpers' |
| 3 | + |
| 4 | +import type { T8NOptions } from './types.js' |
| 5 | + |
| 6 | +export function getArguments() { |
| 7 | + const argsParsed = yargs(hideBin(process.argv)) |
| 8 | + .parserConfiguration({ |
| 9 | + 'dot-notation': false, |
| 10 | + }) |
| 11 | + .option('state.fork', { |
| 12 | + describe: 'Fork to use', |
| 13 | + type: 'string', |
| 14 | + demandOption: true, |
| 15 | + }) |
| 16 | + .option('state.chainid', { |
| 17 | + describe: 'ChainID to use', |
| 18 | + type: 'string', |
| 19 | + default: '1', |
| 20 | + }) |
| 21 | + .option('state.reward', { |
| 22 | + describe: |
| 23 | + 'Coinbase reward after running txs. If 0: coinbase account is touched and rewarded 0 wei. If -1, the coinbase account is not touched (default)', |
| 24 | + type: 'string', |
| 25 | + default: '-1', |
| 26 | + }) |
| 27 | + .option('input.alloc', { |
| 28 | + describe: 'Initial state allocation', |
| 29 | + type: 'string', |
| 30 | + demandOption: true, |
| 31 | + }) |
| 32 | + .option('input.txs', { |
| 33 | + describe: 'JSON input of txs to run on top of the initial state allocation', |
| 34 | + type: 'string', |
| 35 | + demandOption: true, |
| 36 | + }) |
| 37 | + .option('input.env', { |
| 38 | + describe: 'Input environment (coinbase, difficulty, etc.)', |
| 39 | + type: 'string', |
| 40 | + demandOption: true, |
| 41 | + }) |
| 42 | + .option('output.basedir', { |
| 43 | + describe: 'Base directory to write output to', |
| 44 | + type: 'string', |
| 45 | + demandOption: true, |
| 46 | + }) |
| 47 | + .option('output.result', { |
| 48 | + describe: 'File to write output results to (relative to `output.basedir`)', |
| 49 | + type: 'string', |
| 50 | + demandOption: true, |
| 51 | + }) |
| 52 | + .option('output.alloc', { |
| 53 | + describe: 'File to write output allocation to (after running the transactions)', |
| 54 | + type: 'string', |
| 55 | + demandOption: true, |
| 56 | + }) |
| 57 | + .option('output.body', { |
| 58 | + deprecate: true, |
| 59 | + description: 'File to write transaction RLPs to (currently unused)', |
| 60 | + type: 'string', |
| 61 | + }) |
| 62 | + .option('log', { |
| 63 | + describe: 'Optionally write light-trace logs to stdout', |
| 64 | + type: 'boolean', |
| 65 | + default: false, |
| 66 | + }) |
| 67 | + .strict() |
| 68 | + .help().argv |
| 69 | + |
| 70 | + const args = argsParsed as any as T8NOptions |
| 71 | + |
| 72 | + args.input = { |
| 73 | + alloc: (<any>args)['input.alloc'], |
| 74 | + txs: (<any>args)['input.txs'], |
| 75 | + env: (<any>args)['input.env'], |
| 76 | + } |
| 77 | + args.output = { |
| 78 | + basedir: (<any>args)['output.basedir'], |
| 79 | + result: (<any>args)['output.result'], |
| 80 | + alloc: (<any>args)['output.alloc'], |
| 81 | + } |
| 82 | + args.state = { |
| 83 | + fork: (<any>args)['state.fork'], |
| 84 | + reward: BigInt((<any>args)['state.reward']), |
| 85 | + chainid: BigInt((<any>args)['state.chainid']), |
| 86 | + } |
| 87 | + |
| 88 | + return args |
| 89 | +} |
| 90 | + |
| 91 | +/** |
| 92 | + * This function accepts an `inputs.env` which converts non-hex-prefixed numbers |
| 93 | + * to a BigInt value, to avoid errors when converting non-prefixed hex strings to |
| 94 | + * numbers |
| 95 | + * @param input |
| 96 | + * @returns converted input |
| 97 | + */ |
| 98 | +export function normalizeNumbers(input: any) { |
| 99 | + const keys = [ |
| 100 | + 'currentGasLimit', |
| 101 | + 'currentNumber', |
| 102 | + 'currentTimestamp', |
| 103 | + 'currentRandom', |
| 104 | + 'currentDifficulty', |
| 105 | + 'currentBaseFee', |
| 106 | + 'currentBlobGasUsed', |
| 107 | + 'currentExcessBlobGas', |
| 108 | + 'parentDifficulty', |
| 109 | + 'parentTimestamp', |
| 110 | + 'parentBaseFee', |
| 111 | + 'parentGasUsed', |
| 112 | + 'parentGasLimit', |
| 113 | + 'parentBlobGasUsed', |
| 114 | + 'parentExcessBlobGas', |
| 115 | + ] |
| 116 | + |
| 117 | + for (const key of keys) { |
| 118 | + const value = input[key] |
| 119 | + if (value !== undefined) { |
| 120 | + if (value.substring(0, 2) !== '0x') { |
| 121 | + input[key] = BigInt(value) |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + return input |
| 126 | +} |
0 commit comments