Move bundler config generation to a seperate file (#50)

* split config logic to seprate file
This commit is contained in:
skypigr
2023-02-16 03:32:59 -08:00
committed by GitHub
parent 653ed3008f
commit 916ea7b36f
3 changed files with 53 additions and 42 deletions

1
.gitignore vendored
View File

@@ -20,3 +20,4 @@ tsconfig.tsbuildinfo
**/dist/
/packages/bundler/src/types/
yarn-error.log
.vscode

View File

@@ -0,0 +1,49 @@
import ow from 'ow'
import fs from 'fs'
import { BundlerConfig, bundlerConfigDefault, BundlerConfigShape } from './BundlerConfig'
import { ethers, Wallet } from 'ethers'
import { BaseProvider } from '@ethersproject/providers'
function getCommandLineParams (programOpts: any): Partial<BundlerConfig> {
const params: any = {}
for (const bundlerConfigShapeKey in BundlerConfigShape) {
const optionValue = programOpts[bundlerConfigShapeKey]
if (optionValue != null) {
params[bundlerConfigShapeKey] = optionValue
}
}
return params as BundlerConfig
}
function mergeConfigs (...sources: Array<Partial<BundlerConfig>>): BundlerConfig {
const mergedConfig = Object.assign({}, ...sources)
ow(mergedConfig, ow.object.exactShape(BundlerConfigShape))
return mergedConfig
}
export async function resolveConfiguration (programOpts: any): Promise<{ config: BundlerConfig, provider: BaseProvider, wallet: Wallet }> {
const commandLineParams = getCommandLineParams(programOpts)
let fileConfig: Partial<BundlerConfig> = {}
const configFileName = programOpts.config
if (fs.existsSync(configFileName)) {
fileConfig = JSON.parse(fs.readFileSync(configFileName, 'ascii'))
}
const config = mergeConfigs(bundlerConfigDefault, fileConfig, commandLineParams)
console.log('Merged configuration:', JSON.stringify(config))
const provider: BaseProvider = config.network === 'hardhat'
// eslint-disable-next-line
? require('hardhat').ethers.provider
: ethers.getDefaultProvider(config.network)
let mnemonic: string
let wallet: Wallet
try {
mnemonic = fs.readFileSync(config.mnemonic, 'ascii').trim()
wallet = Wallet.fromMnemonic(mnemonic).connect(provider)
} catch (e: any) {
throw new Error(`Unable to read --mnemonic ${config.mnemonic}: ${e.message as string}`)
}
return { config, provider, wallet }
}

View File

@@ -1,12 +1,9 @@
import ow from 'ow'
import fs from 'fs'
import { Command } from 'commander'
import { erc4337RuntimeVersion } from '@account-abstraction/utils'
import { ethers, Wallet } from 'ethers'
import { BaseProvider } from '@ethersproject/providers'
import { BundlerConfig, bundlerConfigDefault, BundlerConfigShape } from './BundlerConfig'
import { BundlerServer } from './BundlerServer'
import { UserOpMethodHandler } from './UserOpMethodHandler'
import { EntryPoint, EntryPoint__factory } from '@account-abstraction/contracts'
@@ -15,6 +12,7 @@ import { initServer } from './modules/initServer'
import { DebugMethodHandler } from './DebugMethodHandler'
import { DeterministicDeployer } from '@account-abstraction/sdk'
import { isGeth } from './utils'
import { resolveConfiguration } from './Config'
// this is done so that console.log outputs BigNumber as hex string instead of unreadable object
export const inspectCustomSymbol = Symbol.for('nodejs.util.inspect.custom')
@@ -27,34 +25,9 @@ const CONFIG_FILE_NAME = 'workdir/bundler.config.json'
export let showStackTraces = false
export function resolveConfiguration (programOpts: any): BundlerConfig {
let fileConfig: Partial<BundlerConfig> = {}
const commandLineParams = getCommandLineParams(programOpts)
const configFileName = programOpts.config
if (fs.existsSync(configFileName)) {
fileConfig = JSON.parse(fs.readFileSync(configFileName, 'ascii'))
}
const mergedConfig = Object.assign({}, bundlerConfigDefault, fileConfig, commandLineParams)
console.log('Merged configuration:', JSON.stringify(mergedConfig))
ow(mergedConfig, ow.object.exactShape(BundlerConfigShape))
return mergedConfig
}
function getCommandLineParams (programOpts: any): Partial<BundlerConfig> {
const params: any = {}
for (const bundlerConfigShapeKey in BundlerConfigShape) {
const optionValue = programOpts[bundlerConfigShapeKey]
if (optionValue != null) {
params[bundlerConfigShapeKey] = optionValue
}
}
return params as BundlerConfig
}
export async function connectContracts (
wallet: Wallet,
entryPointAddress: string): Promise<{ entryPoint: EntryPoint}> {
entryPointAddress: string): Promise<{ entryPoint: EntryPoint }> {
const entryPoint = EntryPoint__factory.connect(entryPointAddress, wallet)
return {
entryPoint
@@ -101,7 +74,7 @@ export async function runBundler (argv: string[], overrideExit = true): Promise<
console.log('command-line arguments: ', program.opts())
const config = resolveConfiguration(programOpts)
const { config, provider, wallet } = await resolveConfiguration(programOpts)
if (programOpts.createMnemonic != null) {
const mnemonicFile = config.mnemonic
console.log('Creating mnemonic in file', mnemonicFile)
@@ -113,18 +86,6 @@ export async function runBundler (argv: string[], overrideExit = true): Promise<
console.log('created mnemonic file', mnemonicFile)
process.exit(1)
}
const provider: BaseProvider =
// eslint-disable-next-line
config.network === 'hardhat' ? require('hardhat').ethers.provider :
ethers.getDefaultProvider(config.network)
let mnemonic: string
let wallet: Wallet
try {
mnemonic = fs.readFileSync(config.mnemonic, 'ascii').trim()
wallet = Wallet.fromMnemonic(mnemonic).connect(provider)
} catch (e: any) {
throw new Error(`Unable to read --mnemonic ${config.mnemonic}: ${e.message as string}`)
}
const {
// name: chainName,