From 985509ac1c0aa6a68694a2516f441f03f9e74460 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Sun, 25 Jul 2021 16:37:38 +0200 Subject: [PATCH] feat: support additional ganache arguments (#347) * feat: support additional ganache arguments * feat: add fork argument option --- ethers-core/src/utils/ganache.rs | 35 ++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/ethers-core/src/utils/ganache.rs b/ethers-core/src/utils/ganache.rs index 1ce10727..e1df6e80 100644 --- a/ethers-core/src/utils/ganache.rs +++ b/ethers-core/src/utils/ganache.rs @@ -81,6 +81,8 @@ pub struct Ganache { port: Option, block_time: Option, mnemonic: Option, + fork: Option, + args: Vec, } 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>(mut self, fork: T) -> Self { + self.fork = Some(fork.into()); + self + } + + /// Adds an argument to pass to the `ganache-cli`. + pub fn arg>(mut self, arg: T) -> Self { + self.args.push(arg.into()); + self + } + + /// Adds multiple arguments to pass to the `ganache-cli`. + pub fn args(mut self, args: I) -> Self + where + I: IntoIterator, + S: Into, + { + 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