From 3c9bcea70e9fdc7e1afd483726b3ff3062feb073 Mon Sep 17 00:00:00 2001 From: Alex Towle Date: Tue, 31 Oct 2023 19:15:45 -0500 Subject: [PATCH] Added a pause script to `test-utils` (#644) * Added a pause script to `test-utils` * Improved the output * Added some documentation about running the scripts --- Cargo.lock | 7 ++++++ crates/hyperdrive-math/src/long.rs | 3 ++- crates/test-utils/Cargo.toml | 29 +++++++++++++---------- crates/test-utils/README.md | 31 ++++++++++++++++++++++++ crates/test-utils/src/agent.rs | 2 +- crates/test-utils/src/bin/pause.rs | 38 ++++++++++++++++++++++++++++++ 6 files changed, 96 insertions(+), 14 deletions(-) create mode 100644 crates/test-utils/src/bin/pause.rs diff --git a/Cargo.lock b/Cargo.lock index eb8d5b7b8..e3e33e5ea 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -637,6 +637,12 @@ dependencies = [ "winapi", ] +[[package]] +name = "dotenvy" +version = "0.15.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1aaf95b3e5c8f23aa320147307562d361db0ae0d51242340f558153b4eb2439b" + [[package]] name = "dunce" version = "1.0.4" @@ -3110,6 +3116,7 @@ name = "test-utils" version = "0.1.0" dependencies = [ "async-trait", + "dotenvy", "ethers", "eyre", "fixed-point", diff --git a/crates/hyperdrive-math/src/long.rs b/crates/hyperdrive-math/src/long.rs index b37f75774..6ef8b09d4 100644 --- a/crates/hyperdrive-math/src/long.rs +++ b/crates/hyperdrive-math/src/long.rs @@ -666,7 +666,8 @@ mod tests { // considering fees. // 2. The pool's solvency is close to zero. // 3. Bob's budget is consumed. - let is_max_price = max_spot_price - spot_price_after_long < fixed!(1e15); + let is_max_price = + max_spot_price - spot_price_after_long.min(max_spot_price) < fixed!(1e15); let is_solvency_consumed = { let state = bob.get_state().await?; let error_tolerance = fixed!(1_000e18).mul_div_down(fixed_rate, fixed!(0.1e18)); diff --git a/crates/test-utils/Cargo.toml b/crates/test-utils/Cargo.toml index 2687bf4fb..c350825f5 100644 --- a/crates/test-utils/Cargo.toml +++ b/crates/test-utils/Cargo.toml @@ -3,10 +3,27 @@ name = "test-utils" version = "0.1.0" edition = "2021" +[[bin]] +name = "pause" +path = "src/bin/pause.rs" + +[[example]] +name = "dev_chain" + +[[example]] +name = "max_short" + +[[example]] +name = "test_chain_new" + +[[example]] +name = "test_chain_load_crash" + [dependencies] # External dependencies. async-trait = "0.1.73" +dotenvy = "0.15" ethers = "2.0.8" eyre = "0.6.8" lazy_static = "1.4.0" @@ -24,15 +41,3 @@ fixed-point-macros = { path = "../fixed-point-macros" } hyperdrive-addresses = { path = "../hyperdrive-addresses" } hyperdrive-math = { path = "../hyperdrive-math" } hyperdrive-wrappers = { path = "../hyperdrive-wrappers" } - -[[example]] -name = "dev_chain" - -[[example]] -name = "max_short" - -[[example]] -name = "test_chain_new" - -[[example]] -name = "test_chain_load_crash" diff --git a/crates/test-utils/README.md b/crates/test-utils/README.md index 2efaf298b..e74dbac7f 100644 --- a/crates/test-utils/README.md +++ b/crates/test-utils/README.md @@ -76,3 +76,34 @@ example with the command: ```bash cargo run --example max_short ``` + +# Scripts + +Along with the test utilities, this crate also contains scripts for interacting +with live versions of the protocol. In order to make use of these scripts, add +the following fields to your root level `.env` file: + +```bash +HYPERDRIVE_ETHEREUM_URL= +HYPERDRIVE_ARTIFACTS_URL= +HYPERDRIVE_PRIVATE_KEY= +``` + +These variables will be overwritten by variables already present in the +environment which makes it easy to use alternate variables as follows: + +```bash +ENVVAR=ENVVAR_VALUE cargo run --bin SCRIPT +``` + +## Pause + +The `pause` script pauses the Hyperdrive pool, which puts the pool into a state +where `openLong`, `openShort`, and `addLiquidity` are disabled. For competition +pools, only the admin can call `pause`, so make sure that you are using the +correct private key in your environment. You can run the `pause` script with the +command: + +```bash +cargo run --bin pause +``` diff --git a/crates/test-utils/src/agent.rs b/crates/test-utils/src/agent.rs index 1e6410bc6..eac029e91 100644 --- a/crates/test-utils/src/agent.rs +++ b/crates/test-utils/src/agent.rs @@ -658,7 +658,7 @@ impl Agent { ) -> Result<()> { let tx = ContractCall_(self.hyperdrive.checkpoint(checkpoint)) .apply(self.pre_process_options(maybe_tx_options)); - tx.0.send().await?; + tx.0.send().await?.await?; Ok(()) } diff --git a/crates/test-utils/src/bin/pause.rs b/crates/test-utils/src/bin/pause.rs new file mode 100644 index 000000000..8c91d1ee5 --- /dev/null +++ b/crates/test-utils/src/bin/pause.rs @@ -0,0 +1,38 @@ +use std::env; + +use dotenvy::dotenv; +use ethers::signers::LocalWallet; +use eyre::Result; +use hyperdrive_wrappers::wrappers::ierc4626_hyperdrive::IERC4626Hyperdrive; +use test_utils::chain::{Chain, DevChain, MNEMONIC}; + +#[tokio::main] +async fn main() -> Result<()> { + // Connect to the chain and set up an agent with the provided envvars. + dotenv().expect("Failed to load .env file"); + let chain = DevChain::new( + &env::var("HYPERDRIVE_ETHEREUM_URL")?, + &env::var("HYPERDRIVE_ARTIFACTS_URL")?, + MNEMONIC, + 0, + ) + .await?; + let client = chain + .client(env::var("HYPERDRIVE_PRIVATE_KEY")?.parse::()?) + .await?; + + // Pause the pool. + println!("Pausing the pool..."); + let hyperdrive = IERC4626Hyperdrive::new(chain.addresses().hyperdrive, client); + hyperdrive.pause(true).send().await?.await?; + + // Check that the pool is paused. + let market_state = hyperdrive.get_market_state().call().await?; + if market_state.is_paused { + println!("The pool was successfully paused!"); + } else { + panic!("The pool was not paused!"); + } + + Ok(()) +}