Skip to content

Commit

Permalink
chore: Explorer Example (#39)
Browse files Browse the repository at this point in the history
* chore: Make price printer an example

Running the example is now very simple.

* chore: Add price explorer example

* chore: format

---------

Co-authored-by: kayibal <[email protected]>
  • Loading branch information
kayibal and kayibal authored Nov 6, 2024
1 parent 12839c0 commit 3522dc2
Show file tree
Hide file tree
Showing 11 changed files with 676 additions and 293 deletions.
296 changes: 198 additions & 98 deletions Cargo.lock

Large diffs are not rendered by default.

16 changes: 12 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ version = "0.32.0"
edition = "2021"

[workspace]
members = ["tycho_simulation_py", "tutorial"]
members = ["tycho_simulation_py"]

[dependencies]
ethabi = "13.0"
ethers = "2.0.13"
serde_json = "1.0.105"
serde = { version = "1.0", features = ["rc"] }
tokio = { version = "1.38.0", features = ["full"] }
futures = "0.3.28"
futures = "0.3.31"
revm = { version = "12.1.0", features = ["ethersdb", "serde"] }
hex = "0.4.3"
thiserror = "1"
Expand All @@ -28,14 +28,15 @@ uuid = { version = "1.4.1", features = [
] }
num-traits = "0.2.17"
dotenv = "0.15.0"
tycho-core = { git = "ssh://github.com/propeller-heads/tycho-indexer.git", package = "tycho-core", tag = "0.41.0" }
tycho-client = { git = "ssh://github.com/propeller-heads/tycho-indexer.git", package = "tycho-client", tag = "0.41.0" }
tycho-core = { git = "https://github.com/propeller-heads/tycho-indexer.git", package = "tycho-core", tag = "0.41.0" }
tycho-client = { git = "https://github.com/propeller-heads/tycho-indexer.git", package = "tycho-client", tag = "0.41.0" }
foundry-config = { git = "https://github.com/foundry-rs/foundry", rev = "2544793" }
foundry-evm = { git = "https://github.com/foundry-rs/foundry", rev = "2544793" }
revm-inspectors = { version = "0.5", features = ["serde"] }
mini-moka = "0.10"
lazy_static = "1.4.0"
itertools = "0.10.5"
unicode-width = "0.1.13"

[dev-dependencies]
mockito = "1.1.1"
Expand All @@ -47,6 +48,13 @@ tracing-subscriber = { version = "0.3.17", default-features = false, features =
"fmt",
] }
tempfile = "3.13.0"

# price printer example
clap = { version = "4.5.3", features = ["derive"] }
anyhow = "1.0.79"
ratatui = "0.29.0"
crossterm = { version = "0.28.1" , features = ["event-stream"]}

[features]
default = []
network_tests = []
Expand Down
12 changes: 12 additions & 0 deletions examples/explorer/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Price Printer

This example allows you to list all pools over a certain tvl threshold and explore
quotes from each pool.


## How to run

```bash
export TYCHO_API_TOKEN=sampletoken
RUST_LOG=info cargo run --example explorer -- --tvl 1000
```
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use ethers::types::H160;
use std::{
collections::{hash_map::Entry, HashMap},
str::FromStr,
sync::mpsc::Sender,
};
use tokio::sync::mpsc::Sender;
use tracing::{debug, info, warn};

use tycho_client::{
Expand All @@ -23,7 +23,7 @@ use tycho_simulation::{
use crate::data_feed::state::BlockState;

// TODO: Make extractors configurable
async fn process_messages(
pub async fn process_messages(
tycho_url: String,
auth_key: Option<String>,
state_tx: Sender<BlockState>,
Expand Down Expand Up @@ -232,6 +232,7 @@ async fn process_messages(

state_tx
.send(state)
.await
.expect("Sending tick failed!")
}

Expand Down
46 changes: 46 additions & 0 deletions examples/explorer/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
pub mod data_feed;
mod ui;

extern crate tycho_simulation;

use clap::Parser;
use data_feed::{state::BlockState, tycho};
use futures::future::select_all;
use std::env;
use tokio::{sync::mpsc, task::JoinHandle};

#[derive(Parser)]
struct Cli {
/// The tvl threshold to filter the graph by
#[arg(short, long, default_value_t = 1000.0)]
tvl_threshold: f64,
}

#[tokio::main]
async fn main() {
// Parse command-line arguments into a Cli struct
let cli = Cli::parse();

let tycho_url =
env::var("TYCHO_URL").unwrap_or_else(|_| "tycho-beta.propellerheads.xyz".to_string());
let tycho_api_key: String =
env::var("TYCHO_API_KEY").unwrap_or_else(|_| "sampletoken".to_string());

// Create communication channels for inter-thread communication
let (tick_tx, tick_rx) = mpsc::channel::<BlockState>(12);

let tycho_message_processor: JoinHandle<anyhow::Result<()>> = tokio::spawn(async move {
tycho::process_messages(tycho_url, Some(tycho_api_key), tick_tx, cli.tvl_threshold).await;
anyhow::Result::Ok(())
});

let terminal = ratatui::init();
let terminal_app = tokio::spawn(async move {
ui::App::new(tick_rx)
.run(terminal)
.await
});
let tasks = [terminal_app, tycho_message_processor];
let _ = select_all(tasks).await;
ratatui::restore();
}
Loading

0 comments on commit 3522dc2

Please sign in to comment.