Skip to content

Commit

Permalink
Merge pull request #100 from propeller-heads/ah/import-grouping
Browse files Browse the repository at this point in the history
  • Loading branch information
kayibal authored Dec 16, 2024
2 parents e11e29b + a0e5d69 commit c7d44df
Show file tree
Hide file tree
Showing 38 changed files with 211 additions and 202 deletions.
3 changes: 2 additions & 1 deletion examples/explorer/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ pub mod utils;

extern crate tycho_simulation;

use std::env;

use clap::Parser;
use futures::{future::select_all, StreamExt};
use std::env;
use tokio::{sync::mpsc, task::JoinHandle};
use tycho_client::feed::component_tracker::ComponentFilter;
use tycho_core::dto::Chain;
Expand Down
3 changes: 2 additions & 1 deletion examples/explorer/ui.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::{cmp::max, str::FromStr, time::Instant};

use futures::StreamExt;
use itertools::Itertools;
use num_bigint::BigUint;
Expand All @@ -13,7 +15,6 @@ use ratatui::{
},
DefaultTerminal, Frame,
};
use std::{cmp::max, str::FromStr, time::Instant};
use tokio::{select, sync::mpsc::Receiver};
use tycho_core::Bytes;
use tycho_simulation::protocol::{
Expand Down
3 changes: 2 additions & 1 deletion rustfmt.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ binop_separator = "Back"
trailing_comma = "Vertical"
trailing_semicolon = false
use_field_init_shorthand = true
chain_width=40
chain_width=40
group_imports = "StdExternalCrate"
4 changes: 2 additions & 2 deletions src/evm/account_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,11 +229,11 @@ impl AccountStorage {

#[cfg(test)]
mod tests {
use super::*;
use std::{collections::HashMap, error::Error, str::FromStr};

use revm::primitives::{AccountInfo, Address, KECCAK_EMPTY, U256};
use std::{collections::HashMap, error::Error, str::FromStr};

use super::*;
use crate::evm::account_storage::{Account, AccountStorage};

#[test]
Expand Down
40 changes: 22 additions & 18 deletions src/evm/decoder.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
use crate::{
evm::{
engine_db::{update_engine, SHARED_TYCHO_DB},
tycho_models::{AccountUpdate, ResponseAccount},
},
models::Token,
protocol::{
errors::InvalidSnapshotError,
models::{BlockUpdate, ProtocolComponent, TryFromWithBlock},
state::ProtocolSim,
},
};
use alloy_primitives::Address;
use std::{
collections::{hash_map::Entry, HashMap},
future::Future,
pin::Pin,
str::FromStr,
sync::Arc,
};

use alloy_primitives::Address;
use thiserror::Error;
use tokio::sync::RwLock;
use tracing::{debug, info, warn};
use tycho_client::feed::{synchronizer::ComponentWithState, FeedMessage, Header};
use tycho_core::Bytes;

use crate::{
evm::{
engine_db::{update_engine, SHARED_TYCHO_DB},
tycho_models::{AccountUpdate, ResponseAccount},
},
models::Token,
protocol::{
errors::InvalidSnapshotError,
models::{BlockUpdate, ProtocolComponent, TryFromWithBlock},
state::ProtocolSim,
},
};

#[derive(Error, Debug)]
pub enum StreamDecodeError {
#[error("{0}")]
Expand Down Expand Up @@ -328,18 +330,20 @@ impl TychoStreamDecoder {

#[cfg(test)]
mod tests {
use std::{fs, path::Path};

use num_bigint::ToBigUint;
use rstest::*;
use tycho_client::feed::FeedMessage;
use tycho_core::Bytes;

use crate::{
evm::{
decoder::{StreamDecodeError, TychoStreamDecoder},
protocol::uniswap_v2::state::UniswapV2State,
},
models::Token,
};
use num_bigint::ToBigUint;
use rstest::*;
use std::{fs, path::Path};
use tycho_client::feed::FeedMessage;
use tycho_core::Bytes;

async fn setup_decoder(set_tokens: bool) -> TychoStreamDecoder {
let mut decoder = TychoStreamDecoder::new();
Expand Down
5 changes: 2 additions & 3 deletions src/evm/engine_db/simulation_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use std::{
};

use alloy::providers::Provider;

use alloy_primitives::StorageValue;
use revm::{
db::DatabaseRef,
Expand Down Expand Up @@ -444,18 +443,18 @@ where

#[cfg(test)]
mod tests {
use super::*;
use std::{env, error::Error, str::FromStr};

use alloy::{
providers::{ProviderBuilder, RootProvider},
transports::BoxTransport,
};

use dotenv::dotenv;
use rstest::rstest;
use tokio::runtime::Runtime;

use super::*;

fn get_runtime() -> Option<Arc<Runtime>> {
let runtime = tokio::runtime::Handle::try_current()
.is_err()
Expand Down
4 changes: 2 additions & 2 deletions src/evm/engine_db/tycho_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,13 +347,13 @@ impl DatabaseRef for PreCachedDB {

#[cfg(test)]
mod tests {
use super::*;
use std::{error::Error, str::FromStr};

use chrono::DateTime;
use revm::primitives::U256;
use rstest::{fixture, rstest};
use std::{error::Error, str::FromStr};

use super::*;
use crate::evm::tycho_models::{AccountUpdate, Block, Chain, ChangeType};

#[fixture]
Expand Down
6 changes: 4 additions & 2 deletions src/evm/protocol/filters.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use crate::evm::protocol::vm::utils::json_deserialize_be_bigint_list;
use num_bigint::BigInt;
use std::collections::HashSet;

use num_bigint::BigInt;
use tracing::info;
use tycho_client::feed::synchronizer::ComponentWithState;

use crate::evm::protocol::vm::utils::json_deserialize_be_bigint_list;

const ZERO_ADDRESS: &str = "0x0000000000000000000000000000000000000000";
pub fn balancer_pool_filter(component: &ComponentWithState) -> bool {
// Check for rate_providers in static_attributes
Expand Down
6 changes: 4 additions & 2 deletions src/evm/protocol/safe_math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
//! Should an operation cause an overflow a result containing TradeSimulationError
//! will be returned.
//! Functions for the types I256, U256, U512 are available.
use crate::protocol::errors::SimulationError;
use alloy_primitives::{I256, U256, U512};

use crate::protocol::errors::SimulationError;

pub fn safe_mul_u256(a: U256, b: U256) -> Result<U256, SimulationError> {
let res = a.checked_mul(b);
_construc_result_u256(res)
Expand Down Expand Up @@ -109,11 +110,12 @@ pub fn _construc_result_i256(res: Option<I256>) -> Result<I256, SimulationError>

#[cfg(test)]
mod safe_math_tests {
use super::*;
use std::str::FromStr;

use rstest::rstest;

use super::*;

const U256_MAX: U256 = U256::from_limbs([u64::MAX, u64::MAX, u64::MAX, u64::MAX]);
const U512_MAX: U512 = U512::from_limbs([
u64::MAX,
Expand Down
10 changes: 4 additions & 6 deletions src/evm/protocol/u256_num.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
//! Numeric methods for the U256 type
use alloy_primitives::U256;
use std::{cmp::max, panic};
use std::{cmp::max, collections::HashMap, panic};

use alloy_primitives::bytes::Bytes;
use alloy_primitives::{bytes::Bytes, U256};
use num_bigint::BigUint;
use std::collections::HashMap;

/// Converts a U256 integer into it's closest floating point representation
///
Expand Down Expand Up @@ -129,10 +127,10 @@ pub fn map_slots_to_u256(

#[cfg(test)]
mod test {
use super::*;

use rstest::rstest;

use super::*;

#[rstest]
#[case::one(U256::from(1u64), 1.0f64)]
#[case::two(U256::from(2), 2.0f64)]
Expand Down
8 changes: 5 additions & 3 deletions src/evm/protocol/uniswap_v2/reserve_price.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::evm::protocol::u256_num::u256_to_f64;
use alloy_primitives::U256;

use crate::evm::protocol::u256_num::u256_to_f64;

/// Computes a spot price given two token reserves
///
/// To find the most accurate spot price possible:
Expand Down Expand Up @@ -29,11 +30,12 @@ pub fn spot_price_from_reserves(

#[cfg(test)]
mod test {
use super::*;
use std::str::FromStr;

use approx::assert_ulps_eq;
use rstest::rstest;
use std::str::FromStr;

use super::*;

#[rstest]
#[case::dai_weth(
Expand Down
19 changes: 10 additions & 9 deletions src/evm/protocol/uniswap_v2/state.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use std::{any::Any, collections::HashMap};

use alloy_primitives::U256;
use num_bigint::{BigUint, ToBigUint};
use tycho_core::{dto::ProtocolStateDelta, Bytes};

use super::reserve_price::spot_price_from_reserves;
use crate::{
evm::protocol::{
safe_math::{safe_add_u256, safe_div_u256, safe_mul_u256, safe_sub_u256},
Expand All @@ -13,10 +17,6 @@ use crate::{
state::ProtocolSim,
},
};
use std::{any::Any, collections::HashMap};
use tycho_core::{dto::ProtocolStateDelta, Bytes};

use super::reserve_price::spot_price_from_reserves;

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct UniswapV2State {
Expand Down Expand Up @@ -147,17 +147,18 @@ impl ProtocolSim for UniswapV2State {

#[cfg(test)]
mod tests {
use super::*;

use std::collections::{HashMap, HashSet};
use std::{
collections::{HashMap, HashSet},
str::FromStr,
};

use approx::assert_ulps_eq;
use num_traits::One;
use rstest::rstest;
use std::str::FromStr;

use tycho_core::hex_bytes::Bytes;

use super::*;

#[rstest]
#[case::same_dec(
U256::from_str("6770398782322527849696614").unwrap(),
Expand Down
12 changes: 6 additions & 6 deletions src/evm/protocol/uniswap_v2/tycho_decoder.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use alloy_primitives::U256;
use std::collections::HashMap;

use alloy_primitives::U256;
use tycho_client::feed::{synchronizer::ComponentWithState, Header};
use tycho_core::Bytes;

use super::state::UniswapV2State;
use crate::{
models::Token,
protocol::{errors::InvalidSnapshotError, models::TryFromWithBlock},
};

use super::state::UniswapV2State;

impl TryFromWithBlock<ComponentWithState> for UniswapV2State {
type Error = InvalidSnapshotError;

Expand Down Expand Up @@ -42,16 +42,16 @@ impl TryFromWithBlock<ComponentWithState> for UniswapV2State {

#[cfg(test)]
mod tests {
use super::*;

use chrono::DateTime;
use std::{collections::HashMap, str::FromStr};

use chrono::DateTime;
use tycho_core::{
dto::{Chain, ChangeType, ProtocolComponent, ResponseProtocolState},
hex_bytes::Bytes,
};

use super::*;

fn usv2_component() -> ProtocolComponent {
let creation_time = DateTime::from_timestamp(1622526000, 0)
.unwrap()
Expand Down
3 changes: 2 additions & 1 deletion src/evm/protocol/uniswap_v3/solidity_math.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use alloy_primitives::{U256, U512};

use crate::{
evm::protocol::safe_math::{div_mod_u512, safe_div_u512, safe_mul_u512},
protocol::errors::SimulationError,
};
use alloy_primitives::{U256, U512};

pub fn mul_div_rounding_up(a: U256, b: U256, denom: U256) -> Result<U256, SimulationError> {
let a_big = U512::from(a);
Expand Down
12 changes: 7 additions & 5 deletions src/evm/protocol/uniswap_v3/sqrt_price_math.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use alloy_primitives::U256;

use super::solidity_math::{mul_div, mul_div_rounding_up};
use crate::{
evm::protocol::{
safe_math::{div_mod_u256, safe_add_u256, safe_div_u256, safe_mul_u256, safe_sub_u256},
u256_num::u256_to_f64,
},
protocol::errors::SimulationError,
};
use alloy_primitives::U256;

use super::solidity_math::{mul_div, mul_div_rounding_up};

const Q96: U256 = U256::from_limbs([0, 4294967296, 0, 0]);
const RESOLUTION: U256 = U256::from_limbs([96, 0, 0, 0]);
Expand Down Expand Up @@ -170,10 +170,12 @@ pub fn sqrt_price_q96_to_f64(x: U256, token_0_decimals: u32, token_1_decimals: u

#[cfg(test)]
mod tests {
use super::*;
use std::str::FromStr;

use approx::assert_ulps_eq;
use rstest::rstest;
use std::str::FromStr;

use super::*;

fn u256(s: &str) -> U256 {
U256::from_str(s).unwrap()
Expand Down
Loading

0 comments on commit c7d44df

Please sign in to comment.