feat: support additional ganache arguments (#347)

* feat: support additional ganache arguments

* feat: add fork argument option
This commit is contained in:
Matthias Seitz 2021-07-25 16:37:38 +02:00 committed by GitHub
parent ed83223b93
commit 985509ac1c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 35 additions and 0 deletions

View File

@ -81,6 +81,8 @@ pub struct Ganache {
port: Option<u16>,
block_time: Option<u64>,
mnemonic: Option<String>,
fork: Option<String>,
args: Vec<String>,
}
impl Ganache {
@ -108,6 +110,33 @@ impl Ganache {
self
}
/// Sets the `fork` argument to fork from another currently running Ethereum client
/// at a given block. Input should be the HTTP location and port of the other client,
/// e.g. `http://localhost:8545`. You can optionally specify the block to fork from
/// using an @ sign: `http://localhost:8545@1599200`
pub fn fork<T: Into<String>>(mut self, fork: T) -> Self {
self.fork = Some(fork.into());
self
}
/// Adds an argument to pass to the `ganache-cli`.
pub fn arg<T: Into<String>>(mut self, arg: T) -> Self {
self.args.push(arg.into());
self
}
/// Adds multiple arguments to pass to the `ganache-cli`.
pub fn args<I, S>(mut self, args: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
for arg in args {
self = self.arg(arg);
}
self
}
/// Consumes the builder and spawns `ganache-cli` with stdout redirected
/// to /dev/null. This takes ~2 seconds to execute as it blocks while
/// waiting for `ganache-cli` to launch.
@ -129,6 +158,12 @@ impl Ganache {
cmd.arg("-b").arg(block_time.to_string());
}
if let Some(fork) = self.fork {
cmd.arg("-f").arg(fork);
}
cmd.args(self.args);
let mut child = cmd.spawn().expect("couldnt start ganache-cli");
let stdout = child