From 10352561c0c5ffdc3bb21f53149887a5db5f6dde Mon Sep 17 00:00:00 2001 From: Evan-Kim2028 Date: Mon, 15 May 2023 15:49:11 -0400 Subject: [PATCH 01/26] add swap sim example --- examples/sim-swap.rs | 71 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 examples/sim-swap.rs diff --git a/examples/sim-swap.rs b/examples/sim-swap.rs new file mode 100644 index 0000000..d626ad6 --- /dev/null +++ b/examples/sim-swap.rs @@ -0,0 +1,71 @@ +use std::{error::Error, str::FromStr, sync::Arc}; + +use ethers::{ + prelude::abigen, + providers::{Http, Provider, Middleware}, + types::{U256, H160}, +}; + +use cfmms::pool::UniswapV3Pool; + +abigen!( + IQuoter, +r#"[ + function quoteExactInputSingle(address tokenIn, address tokenOut,uint24 fee, uint256 amountIn, uint160 sqrtPriceLimitX96) external returns (uint256 amountOut) +]"#;); + + +#[tokio::main] +async fn main() -> Result<(), Box> { + //load rpc endpoint from local environment + // let rpc_endpoint = std::env::var("ETHEREUM_MAINNET_ENDPOINT") + // .expect("Could not get ETHEREUM_MAINNET_ENDPOINT"); + + // let provider = Arc::new(Provider::::try_from(rpc_endpoint).unwrap()); + + let provider = Arc::new(Provider::::try_from("https://eth.llamarpc.com").expect("cant load from endpoint")); + + //Instantiate Pools and Quoter + let pool = UniswapV3Pool::new_from_address( + H160::from_str("0x88e6A0c2dDD26FEEb64F039a2c41296FcB3f5640").unwrap(), // univ3 usdc/weth pool + provider.clone(), + ) + .await // use ? at end of await?? + .unwrap(); + + let quoter = IQuoter::new( + H160::from_str("0xb27308f9f90d607463bb33ea1bebb41c27ce5ab6").unwrap(), + provider.clone(), + ); + + let amount_in = U256::from_dec_str("1000000000000000000").unwrap(); // 1 WETH + + let current_block = provider.get_block_number().await.unwrap(); + let amount_out = pool + .simulate_swap(pool.token_b, amount_in, provider.clone()) + .await + .unwrap(); + + let expected_amount_out = quoter + .quote_exact_input_single( + pool.token_b, + pool.token_a, + pool.fee, + amount_in, + U256::zero(), + ) + .block(current_block) + .call() + .await + .unwrap(); + + assert_eq!(amount_out, expected_amount_out); + + println!( + "Current block: {} Amount in: {} Amount out: {} Expected amount out: {}", + current_block, amount_in, amount_out, expected_amount_out + ); + + Ok(()) + +} \ No newline at end of file From 7a11048ce8f166ab3f3e8dbbca8278c1415e2e52 Mon Sep 17 00:00:00 2001 From: Evan-Kim2028 Date: Mon, 15 May 2023 17:54:36 -0400 Subject: [PATCH 02/26] add project layout to readme --- README.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/README.md b/README.md index 5795be4..22e60d0 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,25 @@ Sync pairs simulate swaps, and interact with constant function market makers on - [Crates.io](https://crates.io/crates/cfmms) - [Documentation in progress](https://docs.rs/cfmms/0.1.3/cfmms/) +## Project Layout +```./ +├── src/ +│ ├── batch_requests/ +│ ├── dex/ +│ ├── pool/ +│ ├── abi.rs +│ ├── checkpoint.rs +│ ├── errors.rs +│ ├── lib.rs +│ ├── sync.rs +│ └── throttle.rs +├── Cargo.lock +├── Cargo.toml +├── foundry.toml +└── README.md +``` + + ## Supported Dexes From a9400d752033b74c437b3fdebc66cde2d155d735 Mon Sep 17 00:00:00 2001 From: Evan-Kim2028 Date: Mon, 15 May 2023 18:00:46 -0400 Subject: [PATCH 03/26] update example rpc endpoint variable --- examples/sim-swap.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/examples/sim-swap.rs b/examples/sim-swap.rs index d626ad6..2194947 100644 --- a/examples/sim-swap.rs +++ b/examples/sim-swap.rs @@ -17,13 +17,12 @@ r#"[ #[tokio::main] async fn main() -> Result<(), Box> { - //load rpc endpoint from local environment - // let rpc_endpoint = std::env::var("ETHEREUM_MAINNET_ENDPOINT") - // .expect("Could not get ETHEREUM_MAINNET_ENDPOINT"); + // load rpc endpoint from local environment + let rpc_endpoint = std::env::var("ETHEREUM_MAINNET_ENDPOINT") + .expect("Could not get ETHEREUM_MAINNET_ENDPOINT"); - // let provider = Arc::new(Provider::::try_from(rpc_endpoint).unwrap()); + let provider = Arc::new(Provider::::try_from(rpc_endpoint).unwrap()); - let provider = Arc::new(Provider::::try_from("https://eth.llamarpc.com").expect("cant load from endpoint")); //Instantiate Pools and Quoter let pool = UniswapV3Pool::new_from_address( From 506bebf31a8bb74ccd4ed005cfa7b008ee2d479b Mon Sep 17 00:00:00 2001 From: Evan-Kim2028 Date: Mon, 15 May 2023 18:22:44 -0400 Subject: [PATCH 04/26] update with build and run test commands --- README.md | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 22e60d0..0ccdc0b 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,11 @@ Sync pairs simulate swaps, and interact with constant function market makers on └── README.md ``` - +The core logic is contained in the following files: +* batch_requests - handles batch pool requests to the Ethereum endpoint +* dex - contains interfaces that handle dex invariants +* pool - contains pool interfaces that handle pools based on dex invariants +* abi - generates bindings for the UniswapV2 and UniswapV3 contracts ## Supported Dexes @@ -37,8 +41,12 @@ Sync pairs simulate swaps, and interact with constant function market makers on | UniswapV2 variants | ✅|| | UniswapV3 | ✅|| +## Build, Run Tests, and Examples +1. In order to build, clone the github repo: +`git clone https://github.com/paradigmxyz/artemis +cd artemis` -## Running Examples +2. Run tests with cargo `cargo test --all` -To run any of the examples, first set a local environment variable called `ETHEREUM_MAINNET_ENDPOINT`. Then you can simply run `cargo run --example `. +3. To run any of the examples, first set a local environment variable called `ETHEREUM_MAINNET_ENDPOINT`. Then you can simply run `cargo run --example `. From b668335bdcf1e190473d737a8acd9e2a4e6398c4 Mon Sep 17 00:00:00 2001 From: Evan-Kim2028 Date: Tue, 16 May 2023 08:59:03 -0400 Subject: [PATCH 05/26] docs(sync): Add function docs --- src/sync.rs | 43 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 6 deletions(-) diff --git a/src/sync.rs b/src/sync.rs index 82f54c9..16eb160 100644 --- a/src/sync.rs +++ b/src/sync.rs @@ -1,3 +1,8 @@ +//! # Sync +//! +//! A module that syncs multiple pool states between all dexes. +//! Contains logic for managing adding checkpoints during a sync, endpoint throttling requests, and removing inactive pools. + use crate::{checkpoint, errors::CFMMError}; use super::dex::Dex; @@ -10,35 +15,57 @@ use std::{ sync::{Arc, Mutex}, }; -//Get all pairs and sync reserve values for each Dex in the `dexes` vec. +/// Synchronizes all pairs and sync reserve values for each DEX in `Vec` with **fixed** step size of `100000`. +/// Step is the block range used to get all pools from a dex if syncing from event logs. Use `sync_pairs_with_step` +/// to specify a custom step size. Sync pairs with throttle but the throttle is disabled because the default variable is fixed at 0. +/// +/// This function synchronizes the pairs and reserve values for each DEX +/// in `Vec`. It utilizes the specified `middleware` for performing +/// the synchronization. An optional `checkpoint_path` can be provided to resume the +/// synchronization from a previously saved checkpoint. + pub async fn sync_pairs( dexes: Vec, middleware: Arc, checkpoint_path: Option<&str>, ) -> Result, CFMMError> { - //Sync pairs with throttle but set the requests per second limit to 0, disabling the throttle. + //throttle is disabled with a default value of 0 sync_pairs_with_throttle(dexes, 100000, middleware, 0, checkpoint_path).await } -//Get all pairs and sync reserve values for each Dex in the `dexes` vec. +/// Synchronizes all pairs and sync reserve values for each DEX in `Vec` with **variable** step size. +/// Step is the block range used to get all pools from a dex if syncing from event logs. +/// Sync pairs with throttle but the throttle is disabled because the default variable is fixed at 0. +/// +/// This function synchronizes the pairs and reserve values for each DEX +/// in `Vec`. It utilizes the specified `middleware` for performing +/// the synchronization. An optional `checkpoint_path` can be provided to resume the +/// synchronization from a previously saved checkpoint. pub async fn sync_pairs_with_step( dexes: Vec, step: usize, middleware: Arc, checkpoint_path: Option<&str>, ) -> Result, CFMMError> { - //Sync pairs with throttle but set the requests per second limit to 0, disabling the throttle. + //throttle is disabled with a default value of 0 sync_pairs_with_throttle(dexes, step, middleware, 0, checkpoint_path).await } -//Get all pairs and sync reserve values for each Dex in the `dexes` vec. +/// Get all pairs and sync reserve values for each DEX in `Vec` with a throttle. +/// +/// This function asynchronously retrieves all pairs and synchronizes the reserve values for each DEX +/// in `Vec`. It uses a specified `step` to define the block range when syncing +/// from event logs. The synchronization is performed using the given `middleware` and a `requests_per_second_limit` +/// is applied to limit the number of requests per second. An optional `checkpoint_path` can be provided to +/// save a checkpoint for resuming the synchronization from a specific point. pub async fn sync_pairs_with_throttle( dexes: Vec, - step: usize, //TODO: Add docs on step. Step is the block range used to get all pools from a dex if syncing from event logs + step: usize, middleware: Arc, requests_per_second_limit: usize, checkpoint_path: Option<&str>, ) -> Result, CFMMError> { + //Get the current block number let current_block = middleware .get_block_number() .await @@ -139,6 +166,10 @@ pub async fn sync_pairs_with_throttle( Ok(aggregated_pools) } +/// Removes empty pools with empty `token_a` values from the `pools` vector. +/// +/// This function iterates over the provided `pools` vector and removes any pools +/// that have an empty `token_a` value. The cleaned vector is then returned. pub fn remove_empty_pools(pools: Vec) -> Vec { let mut cleaned_pools = vec![]; From 312f87302344725fc4a88893d3134f62cf97f776 Mon Sep 17 00:00:00 2001 From: Evan-Kim2028 Date: Tue, 16 May 2023 09:00:27 -0400 Subject: [PATCH 06/26] docs(sync): refactor mod doc wording --- .gitignore | 1 + README.md | 2 ++ src/pool/mod.rs | 5 +++++ src/sync.rs | 2 +- 4 files changed, 9 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index fd760ce..b38abd6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /target /contracts/out /cache +/note.md diff --git a/README.md b/README.md index 0ccdc0b..fd64579 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,8 @@ The core logic is contained in the following files: * dex - contains interfaces that handle dex invariants * pool - contains pool interfaces that handle pools based on dex invariants * abi - generates bindings for the UniswapV2 and UniswapV3 contracts +* sync - syncs multiple pool states between all dexes + ## Supported Dexes diff --git a/src/pool/mod.rs b/src/pool/mod.rs index f665e9a..16cc25e 100644 --- a/src/pool/mod.rs +++ b/src/pool/mod.rs @@ -1,3 +1,8 @@ +//! # Pool +//! +//! A module that syncs multiple pool states between all dexes. +//! Contains logic for managing adding checkpoints during a sync, endpoint throttling requests, and removing inactive pools. +//! use std::{cmp::Ordering, sync::Arc}; use ethers::{ diff --git a/src/sync.rs b/src/sync.rs index 16eb160..42a26b5 100644 --- a/src/sync.rs +++ b/src/sync.rs @@ -1,6 +1,6 @@ //! # Sync //! -//! A module that syncs multiple pool states between all dexes. +//! Syncs multiple pool states between all dexes. //! Contains logic for managing adding checkpoints during a sync, endpoint throttling requests, and removing inactive pools. use crate::{checkpoint, errors::CFMMError}; From 2c5cffbc9b9c2a8373dc3f5dc8dfa8751594374d Mon Sep 17 00:00:00 2001 From: Evan-Kim2028 Date: Tue, 16 May 2023 09:11:15 -0400 Subject: [PATCH 07/26] docs(sync): remove whitespace --- src/sync.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/sync.rs b/src/sync.rs index 42a26b5..644bb9c 100644 --- a/src/sync.rs +++ b/src/sync.rs @@ -23,7 +23,6 @@ use std::{ /// in `Vec`. It utilizes the specified `middleware` for performing /// the synchronization. An optional `checkpoint_path` can be provided to resume the /// synchronization from a previously saved checkpoint. - pub async fn sync_pairs( dexes: Vec, middleware: Arc, From 8c9bff49703419384015067b0944a88a424936c5 Mon Sep 17 00:00:00 2001 From: Evan-Kim2028 Date: Tue, 16 May 2023 10:10:37 -0400 Subject: [PATCH 08/26] docs(Pool): add docs --- src/pool/mod.rs | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/pool/mod.rs b/src/pool/mod.rs index 16cc25e..1cc3eb9 100644 --- a/src/pool/mod.rs +++ b/src/pool/mod.rs @@ -1,8 +1,8 @@ -//! # Pool +//! Pool //! -//! A module that syncs multiple pool states between all dexes. -//! Contains logic for managing adding checkpoints during a sync, endpoint throttling requests, and removing inactive pools. -//! +//! This module provides a collection of structures and functions related to Constant Function Market Maker (CFMM) pools. +//! The module includes support for two types of CFMM pools: Uniswap V2 and Uniswap V3. + use std::{cmp::Ordering, sync::Arc}; use ethers::{ @@ -27,8 +27,10 @@ pub enum Pool { UniswapV3(UniswapV3Pool), } +/// Pool implementation serves as a common interface for any pool in a dex. impl Pool { - //Creates a new pool with all pool data populated from the pair address. + /// Creates a new pool with all pool data populated from a pair address. + /// This is useful if you know the address of the pool pub async fn new_from_address( pair_address: H160, dex_variant: DexVariant, @@ -45,6 +47,7 @@ impl Pool { } } + /// Returns the fee percentage associated with the pool. pub fn fee(&self) -> u32 { match self { Pool::UniswapV2(pool) => pool.fee(), @@ -52,7 +55,8 @@ impl Pool { } } - //Creates a new pool with all pool data populated from the pair address. + /// Creates a new pool with all pool data populated from an event log. + /// This is useful if you want to create a new pool from an event log without knowing the pool address a priori pub async fn new_from_event_log( log: Log, middleware: Arc, @@ -72,7 +76,7 @@ impl Pool { } } - //Creates a new pool with all pool data populated from the pair address. + /// Creates a new pool with all pool data populated from the pair address taken from the event log. pub fn new_empty_pool_from_event_log(log: Log) -> Result> { let event_signature = log.topics[0]; @@ -99,7 +103,8 @@ impl Pool { } } - //Get price of base token per pair token + /// Get price of base token per pair token where base token is the token you want the price denominated in. + /// E.G. If you want the price of ETH in ETH/DAI, then the base token is DAI. pub fn calculate_price(&self, base_token: H160) -> Result { match self { Pool::UniswapV2(pool) => pool.calculate_price(base_token), @@ -125,6 +130,7 @@ impl Pool { } } + /// Simulate swap of token_in for token_out pub async fn simulate_swap( &self, token_in: H160, @@ -153,6 +159,8 @@ impl Pool { } } +/// Currently not implemented anywhere +/// Converts the given amount from one decimal precision to another for a single token amount. pub fn convert_to_decimals(amount: U256, decimals: u8, target_decimals: u8) -> U256 { match target_decimals.cmp(&decimals) { Ordering::Less => amount / U256::from(10u128.pow((decimals - target_decimals) as u32)), @@ -161,6 +169,8 @@ pub fn convert_to_decimals(amount: U256, decimals: u8, target_decimals: u8) -> U } } +/// Currently not implemented anywhere +/// Converts given amounts to a common decimal precision shared between two token amounts. pub fn convert_to_common_decimals( amount_a: U256, a_decimals: u8, @@ -180,6 +190,7 @@ pub fn convert_to_common_decimals( } } +/// Currently not implemented anywhere pub async fn simulate_route( mut token_in: H160, mut amount_in: U256, From 2134eb8785b74a60f9785cad1f4bdf41f6fc723d Mon Sep 17 00:00:00 2001 From: Evan-Kim2028 Date: Tue, 16 May 2023 14:41:19 -0400 Subject: [PATCH 09/26] docs(readme): expand project layout files --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index fd64579..dec21e8 100644 --- a/README.md +++ b/README.md @@ -14,8 +14,16 @@ Sync pairs simulate swaps, and interact with constant function market makers on ```./ ├── src/ │ ├── batch_requests/ + |── uniswap_v2/ + |── uniswap_v3/ │ ├── dex/ + |── mod.rs + |── uniswap_v2.rs + |── uniswap_v3.rs │ ├── pool/ + |── mod.rs + |── uniswap_v2.rs + |── uniswap_v3.rs │ ├── abi.rs │ ├── checkpoint.rs │ ├── errors.rs From fd8e15ef11f22ce873fd9ab14526425cca72d116 Mon Sep 17 00:00:00 2001 From: Evan-Kim2028 Date: Thu, 18 May 2023 11:05:27 -0400 Subject: [PATCH 10/26] docs(pool): Add UniswapV3Pool doc comments --- src/pool/uniswap_v3.rs | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/pool/uniswap_v3.rs b/src/pool/uniswap_v3.rs index 4934e07..559718b 100644 --- a/src/pool/uniswap_v3.rs +++ b/src/pool/uniswap_v3.rs @@ -23,6 +23,7 @@ pub const U256_TWO: U256 = U256([2, 0, 0, 0]); pub const Q128: U256 = U256([0, 0, 1, 0]); pub const Q224: U256 = U256([0, 0, 0, 4294967296]); #[derive(Clone, Copy, Debug, Default)] +/// Contains all protocol specific data to represent a Uniswap V3 pool. pub struct UniswapV3Pool { pub address: H160, pub token_a: H160, @@ -67,7 +68,7 @@ impl UniswapV3Pool { } } - //Creates a new instance of the pool from the pair address + /// Creates a new pool instance from the pair address. pub async fn new_from_address( pair_address: H160, middleware: Arc, @@ -95,6 +96,7 @@ impl UniswapV3Pool { Ok(pool) } + /// Creates a new pool with all pool data populated from an event log. pub async fn new_from_event_log( log: Log, middleware: Arc, @@ -104,6 +106,7 @@ impl UniswapV3Pool { UniswapV3Pool::new_from_address(pair_address, middleware).await } + /// Creates a `UniswapV3Pool` with all pool data populated from the pair address taken from the event log. pub fn new_empty_pool_from_event_log(log: Log) -> Result> { let tokens = ethers::abi::decode(&[ParamType::Uint(32), ParamType::Address], &log.data)?; let token_a = H160::from(log.topics[0]); @@ -130,6 +133,7 @@ impl UniswapV3Pool { self.fee } + /// Retrieve pool data in a way that minimizes number of RPC calls required to sync the pool state. pub async fn get_pool_data( &mut self, middleware: Arc, @@ -336,12 +340,12 @@ impl UniswapV3Pool { Ok(token1) } - /* Legend: - sqrt(price) = sqrt(y/x) - L = sqrt(x*y) - ==> x = L^2/price - ==> y = L^2*price - */ + + /// Legend: + /// sqrt(price) = sqrt(y/x) \ + /// L = sqrt(x*y) \ + /// ==> x = L^2/price \ + /// ==> y = L^2*price \ pub fn calculate_virtual_reserves(&self) -> Result<(u128, u128), ArithmeticError> { let price: f64 = self.calculate_price(self.token_a); @@ -371,6 +375,9 @@ impl UniswapV3Pool { )) } + /// Compute the token exchange rate at the current tick range. + /// + /// The token is called the base_token when it is equal to the token_a address in the Pool. pub fn calculate_price(&self, base_token: H160) -> f64 { let tick = uniswap_v3_math::tick_math::get_tick_at_sqrt_ratio(self.sqrt_price).unwrap(); let shift = self.token_a_decimals as i8 - self.token_b_decimals as i8; @@ -557,6 +564,7 @@ impl UniswapV3Pool { Ok((-current_state.amount_calculated).into_raw()) } + /// Simulate a swap using the cached current state of a `UniswapV3Pool`. pub async fn simulate_swap_with_cache( &self, token_in: H160, @@ -715,6 +723,7 @@ impl UniswapV3Pool { Ok((-current_state.amount_calculated).into_raw()) } + /// Simulate a swap through a route of pools. pub async fn simulate_swap( &self, token_in: H160, From ac371c19e9beb01aa666b5fa240bd862f68e014d Mon Sep 17 00:00:00 2001 From: Evan-Kim2028 Date: Thu, 18 May 2023 11:14:45 -0400 Subject: [PATCH 11/26] feat(example): add pool price calculation --- examples/calculate-price.rs | 61 +++++++++++++++++++++++++++++++++++++ src/lib.rs | 2 +- 2 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 examples/calculate-price.rs diff --git a/examples/calculate-price.rs b/examples/calculate-price.rs new file mode 100644 index 0000000..c88e3b7 --- /dev/null +++ b/examples/calculate-price.rs @@ -0,0 +1,61 @@ +use std::{error::Error, str::FromStr, sync::Arc}; + +use ethers::{ + prelude::abigen, + providers::{Http, Provider, Middleware}, + types::{H160}, +}; + +use cfmms::pool::UniswapV3Pool; +use cfmms::abi::IUniswapV3Pool; + +abigen!( + IQuoter, +r#"[ + function quoteExactInputSingle(address tokenIn, address tokenOut,uint24 fee, uint256 amountIn, uint160 sqrtPriceLimitX96) external returns (uint256 amountOut) +]"#;); + + +#[tokio::main] +async fn main() -> Result<(), Box> { + // load rpc endpoint from local environment + let rpc_endpoint = std::env::var("ETHEREUM_MAINNET_ENDPOINT") + .expect("Could not get ETHEREUM_MAINNET_ENDPOINT"); + + let provider = Arc::new(Provider::::try_from(rpc_endpoint).unwrap()); + + //Instantiate Pools and Quoter + let mut pool = UniswapV3Pool::new_from_address( + H160::from_str("0x88e6A0c2dDD26FEEb64F039a2c41296FcB3f5640").unwrap(), // univ3 usdc/weth pool + provider.clone(), + ) + .await // use ? at end of await?? + .unwrap(); + + pool.get_pool_data(provider.clone()).await.unwrap(); + + let block_pool = IUniswapV3Pool::new( + H160::from_str("0x88e6A0c2dDD26FEEb64F039a2c41296FcB3f5640").unwrap(), + provider.clone(), + ); + + let current_block = provider.get_block_number().await.unwrap(); + + let sqrt_price = block_pool.slot_0().block(current_block).call().await.unwrap().0; + pool.sqrt_price = sqrt_price; + + let float_price_a = pool.calculate_price(pool.token_a); + + let float_price_b = pool.calculate_price(pool.token_b); + + dbg!(pool); + + println!("Current Block: {current_block}"); + println!("Price A: {float_price_a}"); + println!("Price B: {float_price_b}"); + + + + Ok(()) + +} diff --git a/src/lib.rs b/src/lib.rs index 00ed994..890aa12 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,4 @@ -mod abi; +pub mod abi; pub mod checkpoint; pub mod dex; pub mod errors; From 0ed84e8658e7a744a6b12e04e531c448d2c577fe Mon Sep 17 00:00:00 2001 From: Evan-Kim2028 Date: Mon, 22 May 2023 15:55:39 -0400 Subject: [PATCH 12/26] docs(sync): fix comment fmt length --- src/sync.rs | 44 ++++++++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/src/sync.rs b/src/sync.rs index 644bb9c..8931586 100644 --- a/src/sync.rs +++ b/src/sync.rs @@ -1,7 +1,8 @@ //! # Sync //! //! Syncs multiple pool states between all dexes. -//! Contains logic for managing adding checkpoints during a sync, endpoint throttling requests, and removing inactive pools. +//! Contains logic for managing adding checkpoints during a sync, +//! endpoint throttling requests, and removing inactive pools. use crate::{checkpoint, errors::CFMMError}; @@ -15,14 +16,17 @@ use std::{ sync::{Arc, Mutex}, }; -/// Synchronizes all pairs and sync reserve values for each DEX in `Vec` with **fixed** step size of `100000`. -/// Step is the block range used to get all pools from a dex if syncing from event logs. Use `sync_pairs_with_step` -/// to specify a custom step size. Sync pairs with throttle but the throttle is disabled because the default variable is fixed at 0. +/// Synchronizes all pairs and sync reserve values for each DEX in `Vec` with +/// **fixed** step size of `100000`. Step is the block range used to get all pools +/// from a dex if syncing from event logs. Use `sync_pairs_with_step` to specify a +/// custom step size. Sync pairs with throttle but the throttle is disabled because +/// the default variable is fixed at 0. /// -/// This function synchronizes the pairs and reserve values for each DEX -/// in `Vec`. It utilizes the specified `middleware` for performing -/// the synchronization. An optional `checkpoint_path` can be provided to resume the -/// synchronization from a previously saved checkpoint. +/// This function synchronizes the pairs and reserve values for each DEX in `Vec`. +/// It utilizes the specified `middleware` for performing the synchronization. An +/// optional `checkpoint_path` can be provided to resume the synchronization from a +/// previously saved checkpoint. + pub async fn sync_pairs( dexes: Vec, middleware: Arc, @@ -32,14 +36,16 @@ pub async fn sync_pairs( sync_pairs_with_throttle(dexes, 100000, middleware, 0, checkpoint_path).await } -/// Synchronizes all pairs and sync reserve values for each DEX in `Vec` with **variable** step size. -/// Step is the block range used to get all pools from a dex if syncing from event logs. -/// Sync pairs with throttle but the throttle is disabled because the default variable is fixed at 0. +/// Synchronizes all pairs and sync reserve values for each DEX in `Vec` +/// with **variable** step size. Step is the block range used to get all pools +/// from a dex if syncing from event logs. Sync pairs with throttle but the +/// throttle is disabled because the default variable is fixed at 0. /// /// This function synchronizes the pairs and reserve values for each DEX /// in `Vec`. It utilizes the specified `middleware` for performing -/// the synchronization. An optional `checkpoint_path` can be provided to resume the -/// synchronization from a previously saved checkpoint. +/// the synchronization. An optional `checkpoint_path` can be provided to +/// resume the synchronization from a previously saved checkpoint. + pub async fn sync_pairs_with_step( dexes: Vec, step: usize, @@ -52,11 +58,13 @@ pub async fn sync_pairs_with_step( /// Get all pairs and sync reserve values for each DEX in `Vec` with a throttle. /// -/// This function asynchronously retrieves all pairs and synchronizes the reserve values for each DEX -/// in `Vec`. It uses a specified `step` to define the block range when syncing -/// from event logs. The synchronization is performed using the given `middleware` and a `requests_per_second_limit` -/// is applied to limit the number of requests per second. An optional `checkpoint_path` can be provided to -/// save a checkpoint for resuming the synchronization from a specific point. +/// This function asynchronously retrieves all pairs and synchronizes the reserve values +/// for each DEX in `Vec`. It uses a specified `step` to define the block range +/// when syncing from event logs. The synchronization is performed using the given +/// `middleware` and a `requests_per_second_limit` is applied to limit the number of +/// requests per second. An optional `checkpoint_path` can be provided to save a +/// checkpoint for resuming the synchronization from a specific point. + pub async fn sync_pairs_with_throttle( dexes: Vec, step: usize, From 561b13ea23b9f8d9186071ec15f22e65cced3a14 Mon Sep 17 00:00:00 2001 From: Evan-Kim2028 Date: Tue, 23 May 2023 13:52:54 -0400 Subject: [PATCH 13/26] refactor: run cargo fmt --- examples/calculate-price.rs | 20 +++++++++++--------- examples/sim-swap.rs | 11 ++++------- src/pool/mod.rs | 2 +- src/pool/uniswap_v3.rs | 6 +++--- src/sync.rs | 10 +++++----- 5 files changed, 24 insertions(+), 25 deletions(-) diff --git a/examples/calculate-price.rs b/examples/calculate-price.rs index c88e3b7..bc285a2 100644 --- a/examples/calculate-price.rs +++ b/examples/calculate-price.rs @@ -2,12 +2,12 @@ use std::{error::Error, str::FromStr, sync::Arc}; use ethers::{ prelude::abigen, - providers::{Http, Provider, Middleware}, - types::{H160}, + providers::{Http, Middleware, Provider}, + types::H160, }; -use cfmms::pool::UniswapV3Pool; use cfmms::abi::IUniswapV3Pool; +use cfmms::pool::UniswapV3Pool; abigen!( IQuoter, @@ -15,7 +15,6 @@ r#"[ function quoteExactInputSingle(address tokenIn, address tokenOut,uint24 fee, uint256 amountIn, uint160 sqrtPriceLimitX96) external returns (uint256 amountOut) ]"#;); - #[tokio::main] async fn main() -> Result<(), Box> { // load rpc endpoint from local environment @@ -26,7 +25,7 @@ async fn main() -> Result<(), Box> { //Instantiate Pools and Quoter let mut pool = UniswapV3Pool::new_from_address( - H160::from_str("0x88e6A0c2dDD26FEEb64F039a2c41296FcB3f5640").unwrap(), // univ3 usdc/weth pool + H160::from_str("0x88e6A0c2dDD26FEEb64F039a2c41296FcB3f5640").unwrap(), // univ3 usdc/weth pool provider.clone(), ) .await // use ? at end of await?? @@ -41,7 +40,13 @@ async fn main() -> Result<(), Box> { let current_block = provider.get_block_number().await.unwrap(); - let sqrt_price = block_pool.slot_0().block(current_block).call().await.unwrap().0; + let sqrt_price = block_pool + .slot_0() + .block(current_block) + .call() + .await + .unwrap() + .0; pool.sqrt_price = sqrt_price; let float_price_a = pool.calculate_price(pool.token_a); @@ -54,8 +59,5 @@ async fn main() -> Result<(), Box> { println!("Price A: {float_price_a}"); println!("Price B: {float_price_b}"); - - Ok(()) - } diff --git a/examples/sim-swap.rs b/examples/sim-swap.rs index 2194947..fdbd8a9 100644 --- a/examples/sim-swap.rs +++ b/examples/sim-swap.rs @@ -2,8 +2,8 @@ use std::{error::Error, str::FromStr, sync::Arc}; use ethers::{ prelude::abigen, - providers::{Http, Provider, Middleware}, - types::{U256, H160}, + providers::{Http, Middleware, Provider}, + types::{H160, U256}, }; use cfmms::pool::UniswapV3Pool; @@ -14,7 +14,6 @@ r#"[ function quoteExactInputSingle(address tokenIn, address tokenOut,uint24 fee, uint256 amountIn, uint160 sqrtPriceLimitX96) external returns (uint256 amountOut) ]"#;); - #[tokio::main] async fn main() -> Result<(), Box> { // load rpc endpoint from local environment @@ -23,10 +22,9 @@ async fn main() -> Result<(), Box> { let provider = Arc::new(Provider::::try_from(rpc_endpoint).unwrap()); - //Instantiate Pools and Quoter let pool = UniswapV3Pool::new_from_address( - H160::from_str("0x88e6A0c2dDD26FEEb64F039a2c41296FcB3f5640").unwrap(), // univ3 usdc/weth pool + H160::from_str("0x88e6A0c2dDD26FEEb64F039a2c41296FcB3f5640").unwrap(), // univ3 usdc/weth pool provider.clone(), ) .await // use ? at end of await?? @@ -66,5 +64,4 @@ async fn main() -> Result<(), Box> { ); Ok(()) - -} \ No newline at end of file +} diff --git a/src/pool/mod.rs b/src/pool/mod.rs index 1cc3eb9..ed14d73 100644 --- a/src/pool/mod.rs +++ b/src/pool/mod.rs @@ -1,6 +1,6 @@ //! Pool //! -//! This module provides a collection of structures and functions related to Constant Function Market Maker (CFMM) pools. +//! This module provides a collection of structures and functions related to Constant Function Market Maker (CFMM) pools. //! The module includes support for two types of CFMM pools: Uniswap V2 and Uniswap V3. use std::{cmp::Ordering, sync::Arc}; diff --git a/src/pool/uniswap_v3.rs b/src/pool/uniswap_v3.rs index 559718b..00fa6f3 100644 --- a/src/pool/uniswap_v3.rs +++ b/src/pool/uniswap_v3.rs @@ -375,9 +375,9 @@ impl UniswapV3Pool { )) } - /// Compute the token exchange rate at the current tick range. - /// - /// The token is called the base_token when it is equal to the token_a address in the Pool. + /// Compute the token exchange rate at the current tick range. + /// + /// The token is called the base_token when it is equal to the token_a address in the Pool. pub fn calculate_price(&self, base_token: H160) -> f64 { let tick = uniswap_v3_math::tick_math::get_tick_at_sqrt_ratio(self.sqrt_price).unwrap(); let shift = self.token_a_decimals as i8 - self.token_b_decimals as i8; diff --git a/src/sync.rs b/src/sync.rs index 8931586..5f8713a 100644 --- a/src/sync.rs +++ b/src/sync.rs @@ -1,7 +1,7 @@ //! # Sync //! -//! Syncs multiple pool states between all dexes. -//! Contains logic for managing adding checkpoints during a sync, +//! Syncs multiple pool states between all dexes. +//! Contains logic for managing adding checkpoints during a sync, //! endpoint throttling requests, and removing inactive pools. use crate::{checkpoint, errors::CFMMError}; @@ -60,9 +60,9 @@ pub async fn sync_pairs_with_step( /// /// This function asynchronously retrieves all pairs and synchronizes the reserve values /// for each DEX in `Vec`. It uses a specified `step` to define the block range -/// when syncing from event logs. The synchronization is performed using the given -/// `middleware` and a `requests_per_second_limit` is applied to limit the number of -/// requests per second. An optional `checkpoint_path` can be provided to save a +/// when syncing from event logs. The synchronization is performed using the given +/// `middleware` and a `requests_per_second_limit` is applied to limit the number of +/// requests per second. An optional `checkpoint_path` can be provided to save a /// checkpoint for resuming the synchronization from a specific point. pub async fn sync_pairs_with_throttle( From 7dabd991d96deef8f6cae1df15943c578894357e Mon Sep 17 00:00:00 2001 From: Evan-Kim2028 Date: Tue, 23 May 2023 13:53:10 -0400 Subject: [PATCH 14/26] docs(readme): update cfmms-rs description --- README.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index dec21e8..0663b61 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,10 @@ Tests are still being written, assume bugs until tested. If you would like to he # cfmms-rs +`cfmms-rs` is a vertically integrated library for interacting with CFMMs (constant function market makers) on Ethereum. Features include: +- batch sync token pair data from Uniswap V2 and V3 through optimized smart contracts that minimize the number of RPC calls +- calculate token pair spot prices and simulate token swaps +- execute transactions Sync pairs simulate swaps, and interact with constant function market makers on Ethereum. - [Crates.io](https://crates.io/crates/cfmms) @@ -37,7 +41,7 @@ Sync pairs simulate swaps, and interact with constant function market makers on ``` The core logic is contained in the following files: -* batch_requests - handles batch pool requests to the Ethereum endpoint +* batch_requests - handles batch pool requests to the Ethereum endpoint using solidity contracts in `contracts/` * dex - contains interfaces that handle dex invariants * pool - contains pool interfaces that handle pools based on dex invariants * abi - generates bindings for the UniswapV2 and UniswapV3 contracts From 22aa3b8d852ed2ec55b63e3cc1d44722120a3b8e Mon Sep 17 00:00:00 2001 From: Evan-Kim2028 Date: Thu, 25 May 2023 12:39:08 -0400 Subject: [PATCH 15/26] refactor(calc-price): use `?` for error handling --- examples/calculate-price.rs | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/examples/calculate-price.rs b/examples/calculate-price.rs index bc285a2..b29cda4 100644 --- a/examples/calculate-price.rs +++ b/examples/calculate-price.rs @@ -18,8 +18,7 @@ r#"[ #[tokio::main] async fn main() -> Result<(), Box> { // load rpc endpoint from local environment - let rpc_endpoint = std::env::var("ETHEREUM_MAINNET_ENDPOINT") - .expect("Could not get ETHEREUM_MAINNET_ENDPOINT"); + let rpc_endpoint = std::env::var("ETHEREUM_MAINNET_ENDPOINT")?; let provider = Arc::new(Provider::::try_from(rpc_endpoint).unwrap()); @@ -28,8 +27,7 @@ async fn main() -> Result<(), Box> { H160::from_str("0x88e6A0c2dDD26FEEb64F039a2c41296FcB3f5640").unwrap(), // univ3 usdc/weth pool provider.clone(), ) - .await // use ? at end of await?? - .unwrap(); + .await?; pool.get_pool_data(provider.clone()).await.unwrap(); @@ -38,14 +36,13 @@ async fn main() -> Result<(), Box> { provider.clone(), ); - let current_block = provider.get_block_number().await.unwrap(); + let current_block = provider.get_block_number().await?; let sqrt_price = block_pool .slot_0() .block(current_block) .call() - .await - .unwrap() + .await? .0; pool.sqrt_price = sqrt_price; @@ -53,8 +50,6 @@ async fn main() -> Result<(), Box> { let float_price_b = pool.calculate_price(pool.token_b); - dbg!(pool); - println!("Current Block: {current_block}"); println!("Price A: {float_price_a}"); println!("Price B: {float_price_b}"); From 1a25b33e2a50e1e5cd3d9fbe6746ce06d3b69a6a Mon Sep 17 00:00:00 2001 From: Evan-Kim2028 Date: Thu, 25 May 2023 12:43:35 -0400 Subject: [PATCH 16/26] refactor(sim-swap): use ? for error handling --- examples/sim-swap.rs | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/examples/sim-swap.rs b/examples/sim-swap.rs index fdbd8a9..119bb47 100644 --- a/examples/sim-swap.rs +++ b/examples/sim-swap.rs @@ -17,8 +17,7 @@ r#"[ #[tokio::main] async fn main() -> Result<(), Box> { // load rpc endpoint from local environment - let rpc_endpoint = std::env::var("ETHEREUM_MAINNET_ENDPOINT") - .expect("Could not get ETHEREUM_MAINNET_ENDPOINT"); + let rpc_endpoint = std::env::var("ETHEREUM_MAINNET_ENDPOINT")?; let provider = Arc::new(Provider::::try_from(rpc_endpoint).unwrap()); @@ -27,8 +26,7 @@ async fn main() -> Result<(), Box> { H160::from_str("0x88e6A0c2dDD26FEEb64F039a2c41296FcB3f5640").unwrap(), // univ3 usdc/weth pool provider.clone(), ) - .await // use ? at end of await?? - .unwrap(); + .await?; let quoter = IQuoter::new( H160::from_str("0xb27308f9f90d607463bb33ea1bebb41c27ce5ab6").unwrap(), @@ -37,11 +35,10 @@ async fn main() -> Result<(), Box> { let amount_in = U256::from_dec_str("1000000000000000000").unwrap(); // 1 WETH - let current_block = provider.get_block_number().await.unwrap(); + let current_block = provider.get_block_number().await?; let amount_out = pool .simulate_swap(pool.token_b, amount_in, provider.clone()) - .await - .unwrap(); + .await?; let expected_amount_out = quoter .quote_exact_input_single( @@ -53,8 +50,7 @@ async fn main() -> Result<(), Box> { ) .block(current_block) .call() - .await - .unwrap(); + .await?; assert_eq!(amount_out, expected_amount_out); From b00476ea854a368177e71c24ddf379de251b87ad Mon Sep 17 00:00:00 2001 From: Evan-Kim2028 Date: Thu, 25 May 2023 12:44:40 -0400 Subject: [PATCH 17/26] refactor(sim-swap): use ? for error handling --- examples/sim-swap.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/sim-swap.rs b/examples/sim-swap.rs index 119bb47..b53bc51 100644 --- a/examples/sim-swap.rs +++ b/examples/sim-swap.rs @@ -19,21 +19,21 @@ async fn main() -> Result<(), Box> { // load rpc endpoint from local environment let rpc_endpoint = std::env::var("ETHEREUM_MAINNET_ENDPOINT")?; - let provider = Arc::new(Provider::::try_from(rpc_endpoint).unwrap()); + let provider = Arc::new(Provider::::try_from(rpc_endpoint)?); //Instantiate Pools and Quoter let pool = UniswapV3Pool::new_from_address( - H160::from_str("0x88e6A0c2dDD26FEEb64F039a2c41296FcB3f5640").unwrap(), // univ3 usdc/weth pool + H160::from_str("0x88e6A0c2dDD26FEEb64F039a2c41296FcB3f5640")?, // univ3 usdc/weth pool provider.clone(), ) .await?; let quoter = IQuoter::new( - H160::from_str("0xb27308f9f90d607463bb33ea1bebb41c27ce5ab6").unwrap(), + H160::from_str("0xb27308f9f90d607463bb33ea1bebb41c27ce5ab6")?, provider.clone(), ); - let amount_in = U256::from_dec_str("1000000000000000000").unwrap(); // 1 WETH + let amount_in = U256::from_dec_str("1000000000000000000")?; // 1 WETH let current_block = provider.get_block_number().await?; let amount_out = pool From 350cb52ad22315a0cb95664e76ac882d10005c7a Mon Sep 17 00:00:00 2001 From: Evan-Kim2028 Date: Thu, 25 May 2023 12:45:18 -0400 Subject: [PATCH 18/26] refactor(calc-price): use ? for error handling --- examples/calculate-price.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/calculate-price.rs b/examples/calculate-price.rs index b29cda4..380f850 100644 --- a/examples/calculate-price.rs +++ b/examples/calculate-price.rs @@ -20,19 +20,19 @@ async fn main() -> Result<(), Box> { // load rpc endpoint from local environment let rpc_endpoint = std::env::var("ETHEREUM_MAINNET_ENDPOINT")?; - let provider = Arc::new(Provider::::try_from(rpc_endpoint).unwrap()); + let provider = Arc::new(Provider::::try_from(rpc_endpoint)?); //Instantiate Pools and Quoter let mut pool = UniswapV3Pool::new_from_address( - H160::from_str("0x88e6A0c2dDD26FEEb64F039a2c41296FcB3f5640").unwrap(), // univ3 usdc/weth pool + H160::from_str("0x88e6A0c2dDD26FEEb64F039a2c41296FcB3f5640")?, // univ3 usdc/weth pool provider.clone(), ) .await?; - pool.get_pool_data(provider.clone()).await.unwrap(); + pool.get_pool_data(provider.clone()).await?; let block_pool = IUniswapV3Pool::new( - H160::from_str("0x88e6A0c2dDD26FEEb64F039a2c41296FcB3f5640").unwrap(), + H160::from_str("0x88e6A0c2dDD26FEEb64F039a2c41296FcB3f5640")?, provider.clone(), ); From b79899218e17cbde3e1e80b2ecc14d4cd4670879 Mon Sep 17 00:00:00 2001 From: Evan-Kim2028 Date: Thu, 25 May 2023 12:46:24 -0400 Subject: [PATCH 19/26] docs(pool): reword fee doc comments --- src/pool/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pool/mod.rs b/src/pool/mod.rs index ed14d73..23c1ebc 100644 --- a/src/pool/mod.rs +++ b/src/pool/mod.rs @@ -47,7 +47,7 @@ impl Pool { } } - /// Returns the fee percentage associated with the pool. + /// Returns the fee associated with the pool, represented as a percentage pub fn fee(&self) -> u32 { match self { Pool::UniswapV2(pool) => pool.fee(), From 92db32776618a5dd177e750533b324e92f5f460a Mon Sep 17 00:00:00 2001 From: Evan-Kim2028 Date: Thu, 25 May 2023 12:47:14 -0400 Subject: [PATCH 20/26] chore: run cargo fmt --- examples/calculate-price.rs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/examples/calculate-price.rs b/examples/calculate-price.rs index 380f850..1a377b7 100644 --- a/examples/calculate-price.rs +++ b/examples/calculate-price.rs @@ -38,12 +38,7 @@ async fn main() -> Result<(), Box> { let current_block = provider.get_block_number().await?; - let sqrt_price = block_pool - .slot_0() - .block(current_block) - .call() - .await? - .0; + let sqrt_price = block_pool.slot_0().block(current_block).call().await?.0; pool.sqrt_price = sqrt_price; let float_price_a = pool.calculate_price(pool.token_a); From 28943643b56761ecad47b09776a3913ab65fa77d Mon Sep 17 00:00:00 2001 From: Evan-Kim2028 Date: Thu, 25 May 2023 12:49:43 -0400 Subject: [PATCH 21/26] refactor(new-pool): use ? for error handling --- examples/create-new-pool.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/create-new-pool.rs b/examples/create-new-pool.rs index 8fd608c..000fb2f 100644 --- a/examples/create-new-pool.rs +++ b/examples/create-new-pool.rs @@ -12,11 +12,11 @@ async fn main() -> Result<(), Box> { //Add rpc endpoint here: let rpc_endpoint = std::env::var("ETHEREUM_MAINNET_ENDPOINT") .expect("Could not get ETHEREUM_MAINNET_ENDPOINT"); - let provider = Arc::new(Provider::::try_from(rpc_endpoint).unwrap()); + let provider = Arc::new(Provider::::try_from(rpc_endpoint)?); //UniswapV2 usdc weth pool on Eth mainnet let _uniswap_v2_usdc_weth_pool = Pool::new_from_address( - H160::from_str("0xB4e16d0168e52d35CaCD2c6185b44281Ec28C9Dc").unwrap(), + H160::from_str("0xB4e16d0168e52d35CaCD2c6185b44281Ec28C9Dc")?, DexVariant::UniswapV2, provider.clone(), ) @@ -24,7 +24,7 @@ async fn main() -> Result<(), Box> { //UniswapV3 usdc weth pool on Eth mainnet let _uniswap_v3_usdc_weth_pool = Pool::new_from_address( - H160::from_str("0x88e6A0c2dDD26FEEb64F039a2c41296FcB3f5640").unwrap(), + H160::from_str("0x88e6A0c2dDD26FEEb64F039a2c41296FcB3f5640")?, DexVariant::UniswapV3, provider.clone(), ) From 3188d3c33f4d33c65ced6285dee7bbbc504d7496 Mon Sep 17 00:00:00 2001 From: Evan-Kim2028 Date: Thu, 25 May 2023 12:50:19 -0400 Subject: [PATCH 22/26] refactor(calc-price): use ? for error handling --- examples/create-new-pool.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/examples/create-new-pool.rs b/examples/create-new-pool.rs index 000fb2f..84ed603 100644 --- a/examples/create-new-pool.rs +++ b/examples/create-new-pool.rs @@ -10,8 +10,7 @@ use cfmms::{dex::DexVariant, pool::Pool}; #[tokio::main] async fn main() -> Result<(), Box> { //Add rpc endpoint here: - let rpc_endpoint = std::env::var("ETHEREUM_MAINNET_ENDPOINT") - .expect("Could not get ETHEREUM_MAINNET_ENDPOINT"); + let rpc_endpoint = std::env::var("ETHEREUM_MAINNET_ENDPOINT")?; let provider = Arc::new(Provider::::try_from(rpc_endpoint)?); //UniswapV2 usdc weth pool on Eth mainnet From 026b1af3997497dcd93fb9d6ac8a051b6900e86e Mon Sep 17 00:00:00 2001 From: Evan-Kim2028 Date: Thu, 25 May 2023 12:50:47 -0400 Subject: [PATCH 23/26] refactor(checkpoint): use ? for error handling --- examples/generate-mainnet-checkpoint.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/examples/generate-mainnet-checkpoint.rs b/examples/generate-mainnet-checkpoint.rs index b89571a..fca10b1 100644 --- a/examples/generate-mainnet-checkpoint.rs +++ b/examples/generate-mainnet-checkpoint.rs @@ -13,14 +13,13 @@ use cfmms::{ #[tokio::main] async fn main() -> Result<(), Box> { //Add rpc endpoint here: - let rpc_endpoint = std::env::var("ETHEREUM_MAINNET_ENDPOINT") - .expect("Could not get ETHEREUM_MAINNET_ENDPOINT"); - let provider = Arc::new(Provider::::try_from(rpc_endpoint).unwrap()); + let rpc_endpoint = std::env::var("ETHEREUM_MAINNET_ENDPOINT")?; + let provider = Arc::new(Provider::::try_from(rpc_endpoint)?); let dexes = vec![ //Add Sushiswap Dex::new( - H160::from_str("0xC0AEe478e3658e2610c5F7A4A2E1777cE9e4f2Ac").unwrap(), + H160::from_str("0xC0AEe478e3658e2610c5F7A4A2E1777cE9e4f2Ac")?, DexVariant::UniswapV2, 10794229, Some(300), From 4a4a5484c7c4cfbfc7717eeb66ffc66c8c2a12fd Mon Sep 17 00:00:00 2001 From: Evan-Kim2028 Date: Thu, 25 May 2023 12:51:14 -0400 Subject: [PATCH 24/26] refactor(sync-pairs): use ? for error handling --- examples/sync-pairs-with-ipc.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/sync-pairs-with-ipc.rs b/examples/sync-pairs-with-ipc.rs index 1e1d566..391b559 100644 --- a/examples/sync-pairs-with-ipc.rs +++ b/examples/sync-pairs-with-ipc.rs @@ -23,21 +23,21 @@ async fn main() -> Result<(), Box> { let dexes = vec![ //UniswapV2 Dex::new( - H160::from_str("0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f").unwrap(), + H160::from_str("0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f")?, DexVariant::UniswapV2, 2638438, Some(300), ), //Add Sushiswap Dex::new( - H160::from_str("0xC0AEe478e3658e2610c5F7A4A2E1777cE9e4f2Ac").unwrap(), + H160::from_str("0xC0AEe478e3658e2610c5F7A4A2E1777cE9e4f2Ac")?, DexVariant::UniswapV2, 10794229, Some(300), ), //Add UniswapV3 Dex::new( - H160::from_str("0x1F98431c8aD98523631AE4a59f267346ea31F984").unwrap(), + H160::from_str("0x1F98431c8aD98523631AE4a59f267346ea31F984")?, DexVariant::UniswapV3, 12369621, None, From 72f5957259831d1043e69151e74124efcbe5cdb8 Mon Sep 17 00:00:00 2001 From: Evan-Kim2028 Date: Thu, 25 May 2023 12:51:47 -0400 Subject: [PATCH 25/26] refactor(sync-pair-throttle): use ? for error handling --- examples/sync-pairs-with-throttle.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/examples/sync-pairs-with-throttle.rs b/examples/sync-pairs-with-throttle.rs index 08ed10a..a3bbdd5 100644 --- a/examples/sync-pairs-with-throttle.rs +++ b/examples/sync-pairs-with-throttle.rs @@ -13,14 +13,13 @@ use cfmms::{ #[tokio::main] async fn main() -> Result<(), Box> { //Add rpc endpoint here: - let rpc_endpoint = std::env::var("ETHEREUM_MAINNET_ENDPOINT") - .expect("Could not get ETHEREUM_MAINNET_ENDPOINT"); - let provider = Arc::new(Provider::::try_from(rpc_endpoint).unwrap()); + let rpc_endpoint = std::env::var("ETHEREUM_MAINNET_ENDPOINT")?; + let provider = Arc::new(Provider::::try_from(rpc_endpoint)?); let dexes = vec![ //Add UniswapV3 Dex::new( - H160::from_str("0x1F98431c8aD98523631AE4a59f267346ea31F984").unwrap(), + H160::from_str("0x1F98431c8aD98523631AE4a59f267346ea31F984")?, DexVariant::UniswapV3, 12369621, None, From bcd92c12cc9348bcc5c5c5a5b4456ec34dde1258 Mon Sep 17 00:00:00 2001 From: Evan-Kim2028 Date: Thu, 25 May 2023 12:52:21 -0400 Subject: [PATCH 26/26] refactor(sync-pairs): use ? for error handling --- examples/sync-pairs.rs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/examples/sync-pairs.rs b/examples/sync-pairs.rs index 9210fc9..b58b726 100644 --- a/examples/sync-pairs.rs +++ b/examples/sync-pairs.rs @@ -13,28 +13,27 @@ use cfmms::{ #[tokio::main] async fn main() -> Result<(), Box> { //Add rpc endpoint here: - let rpc_endpoint = std::env::var("ETHEREUM_MAINNET_ENDPOINT") - .expect("Could not get ETHEREUM_MAINNET_ENDPOINT"); - let provider = Arc::new(Provider::::try_from(rpc_endpoint).unwrap()); + let rpc_endpoint = std::env::var("ETHEREUM_MAINNET_ENDPOINT")?; + let provider = Arc::new(Provider::::try_from(rpc_endpoint)?); let dexes = vec![ //UniswapV2 Dex::new( - H160::from_str("0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f").unwrap(), + H160::from_str("0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f")?, DexVariant::UniswapV2, 2638438, Some(300), ), //Add Sushiswap Dex::new( - H160::from_str("0xC0AEe478e3658e2610c5F7A4A2E1777cE9e4f2Ac").unwrap(), + H160::from_str("0xC0AEe478e3658e2610c5F7A4A2E1777cE9e4f2Ac")?, DexVariant::UniswapV2, 10794229, Some(300), ), //Add UniswapV3 Dex::new( - H160::from_str("0x1F98431c8aD98523631AE4a59f267346ea31F984").unwrap(), + H160::from_str("0x1F98431c8aD98523631AE4a59f267346ea31F984")?, DexVariant::UniswapV3, 12369621, None,