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