Skip to content

Commit 4d3627a

Browse files
feat: Create tycho-test lib
- Move shared code from tycho-integration test into it - Extend code to be used by the sdk range test - Allow doing router and executor overwrites - Allow setting executors_json and historical_trade to TychoEncoder - Move methods from sdk to tycho-test calculate_executor_storage_slot, setup_router_overwrites - Add RouterOverwritesData model - Misc fixes: - Create tenderly link in N + 1 block - When execution fails and we can not decode the selector -> print the whole input data used Took 2 hours 37 minutes Took 11 seconds
1 parent 2383b99 commit 4d3627a

File tree

17 files changed

+306
-108
lines changed

17 files changed

+306
-108
lines changed

Cargo.lock

Lines changed: 35 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ categories = ["finance", "cryptography::cryptocurrencies"]
1212
readme = "README.md"
1313

1414
[workspace]
15-
members = ["tycho-integration-test", "tycho_simulation_py"]
15+
members = ["tycho-integration-test", "tycho-test", "tycho_simulation_py"]
1616

1717
[workspace.dependencies]
1818
# Serialization/Deserialization

tycho-integration-test/Cargo.toml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,24 @@ edition = "2021"
66
[dependencies]
77
alloy = { workspace = true, features = ["rpc-types-eth", "sol-types"] }
88
alloy-chains = "0.2.6"
9-
alloy-rpc-types-trace = "1.0.38"
109
clap = { version = "4.5.48", features = ["derive"] }
1110
colored = "3.0.0"
1211
dotenv = { workspace = true }
1312
futures = { workspace = true }
1413
itertools = { workspace = true }
15-
lru = "0.16.1"
1614
num-bigint = { workspace = true }
1715
num-traits = { workspace = true }
1816
miette = { version = "7.6.0" }
19-
regex = "1.12.2"
2017
serde = { workspace = true }
2118
serde_json = { workspace = true }
2219
rand = "0.9.2"
2320
tokio = { workspace = true }
24-
tokio-retry2 = { version = "0.6.0", features = ["jitter"] }
2521
tracing = { workspace = true }
2622
metrics = "0.24.2"
2723
metrics-exporter-prometheus = "0.17.2"
2824
actix-web = "4.11.0"
2925
tycho-client = { workspace = true }
3026
tycho-common = { workspace = true }
31-
tycho-ethereum = { workspace = true }
32-
tycho-execution = { workspace = true }
3327
tycho-simulation = { path = "..", features = ["evm"] }
34-
urlencoding = "2.1.3"
28+
tycho-test = { path = "../tycho-test" }
3529
tracing-subscriber = { workspace = true }

tycho-integration-test/src/main.rs

Lines changed: 16 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
mod execution;
21
mod metrics;
3-
mod stream_processor;
42

53
use std::{
64
collections::{HashMap, HashSet},
@@ -9,14 +7,7 @@ use std::{
97
time::Duration,
108
};
119

12-
use alloy::{
13-
eips::BlockNumberOrTag,
14-
network::Ethereum,
15-
primitives::Address,
16-
providers::{Provider, ProviderBuilder, RootProvider},
17-
rpc::types::Block,
18-
};
19-
use alloy_chains::NamedChain;
10+
use alloy::{eips::BlockNumberOrTag, primitives::Address, providers::Provider, rpc::types::Block};
2011
use clap::Parser;
2112
use dotenv::dotenv;
2213
use itertools::Itertools;
@@ -28,18 +19,13 @@ use tokio::sync::Semaphore;
2819
use tracing::{error, info, warn};
2920
use tracing_subscriber::EnvFilter;
3021
use tycho_common::simulation::protocol_sim::ProtocolSim;
31-
use tycho_ethereum::entrypoint_tracer::{
32-
allowance_slot_detector::{AllowanceSlotDetectorConfig, EVMAllowanceSlotDetector},
33-
balance_slot_detector::{BalanceSlotDetectorConfig, EVMBalanceSlotDetector},
34-
};
3522
use tycho_simulation::{
3623
protocol::models::ProtocolComponent,
3724
rfq::protocols::hashflow::{client::HashflowClient, state::HashflowState},
3825
tycho_common::models::Chain,
3926
utils::load_all_tokens,
4027
};
41-
42-
use crate::{
28+
use tycho_test::{
4329
execution::{
4430
encoding::encode_swap,
4531
models::{TychoExecutionInput, TychoExecutionResult},
@@ -173,7 +159,7 @@ async fn run(cli: Cli) -> miette::Result<()> {
173159
let cli = Arc::new(cli);
174160
let chain = cli.chain;
175161

176-
let rpc_tools = RPCTools::new(&cli.rpc_url, &chain).await?;
162+
let rpc_tools = tycho_test::RPCTools::new(&cli.rpc_url, &chain).await?;
177163

178164
// Load tokens from Tycho
179165
info!(%cli.tycho_url, "Loading tokens...");
@@ -247,7 +233,7 @@ async fn run(cli: Cli) -> miette::Result<()> {
247233
async fn process_update(
248234
cli: Arc<Cli>,
249235
chain: Chain,
250-
rpc_tools: RPCTools,
236+
rpc_tools: tycho_test::RPCTools,
251237
tycho_state: Arc<RwLock<TychoState>>,
252238
update: &StreamUpdate,
253239
) -> miette::Result<()> {
@@ -490,6 +476,7 @@ async fn process_update(
490476
block_execution_info.clone(),
491477
&block,
492478
cli.block_wait_time,
479+
None,
493480
)
494481
.await
495482
{
@@ -510,7 +497,7 @@ async fn process_update(
510497
}
511498
.clone();
512499

513-
let state = {
500+
let state_str = {
514501
let current_state = tycho_state
515502
.read()
516503
.map_err(|e| miette!("Failed to acquire read lock on Tycho state: {e}"))?;
@@ -519,18 +506,15 @@ async fn process_update(
519506
.states
520507
.get(&execution_info.component_id)
521508
{
522-
Some(state) => state.clone(),
523-
None => {
524-
error!(id=%execution_info.component_id, "State not found in saved protocol states");
525-
continue;
526-
}
509+
Some(state) => format!("{:?}", state),
510+
None => "".to_string(),
527511
}
528512
};
529513
(n_reverts, n_failures) = process_execution_result(
530514
simulation_id,
531515
result,
532516
execution_info,
533-
state,
517+
state_str,
534518
(*block).clone(),
535519
chain.id().to_string(),
536520
n_reverts,
@@ -691,11 +675,13 @@ async fn process_state(
691675
// Simulate execution amount out against the RPC
692676
let (solution, transaction) = match encode_swap(
693677
&component,
694-
Arc::from(state.clone_box()),
678+
Some(Arc::from(state.clone_box())),
695679
token_in,
696680
token_out,
697681
amount_in.clone(),
698682
chain,
683+
None,
684+
false,
699685
) {
700686
Ok(res) => res,
701687
Err(e) => {
@@ -740,7 +726,8 @@ fn process_execution_result(
740726
simulation_id: &String,
741727
result: &TychoExecutionResult,
742728
execution_info: TychoExecutionInput,
743-
state: Box<dyn ProtocolSim>,
729+
state_str: String,
730+
744731
block: Block,
745732
chain_id: String,
746733
mut n_reverts: i32,
@@ -775,7 +762,7 @@ fn process_execution_result(
775762

776763
info!(
777764
event_type = "execution_slippage",
778-
state = ?state,
765+
state = ?state_str,
779766
token_in = %execution_info.token_in,
780767
token_out = %execution_info.token_out,
781768
simulated_amount = %amount_out,
@@ -823,7 +810,7 @@ fn process_execution_result(
823810
error_message = %revert_reason,
824811
error_name = %error_name,
825812
error_category = %error_category,
826-
state = ?state,
813+
state = ?state_str,
827814
token_in = %execution_info.token_in,
828815
token_out = %execution_info.token_out,
829816
tenderly_url = %tenderly_url,
@@ -889,49 +876,6 @@ fn categorize_error(error_name: &str) -> &'static str {
889876
}
890877
}
891878

892-
#[derive(Clone)]
893-
struct RPCTools {
894-
rpc_url: String,
895-
provider: RootProvider<Ethereum>,
896-
evm_balance_slot_detector: Arc<EVMBalanceSlotDetector>,
897-
evm_allowance_slot_detector: Arc<EVMAllowanceSlotDetector>,
898-
}
899-
900-
impl RPCTools {
901-
pub async fn new(rpc_url: &str, chain: &Chain) -> miette::Result<Self> {
902-
let provider: RootProvider<Ethereum> = ProviderBuilder::default()
903-
.with_chain(
904-
NamedChain::try_from(chain.id())
905-
.into_diagnostic()
906-
.wrap_err("Invalid chain")?,
907-
)
908-
.connect(rpc_url)
909-
.await
910-
.into_diagnostic()
911-
.wrap_err("Failed to connect to provider")?;
912-
let evm_balance_slot_detector = Arc::new(
913-
EVMBalanceSlotDetector::new(BalanceSlotDetectorConfig {
914-
rpc_url: rpc_url.to_string(),
915-
..Default::default()
916-
})
917-
.into_diagnostic()?,
918-
);
919-
let evm_allowance_slot_detector = Arc::new(
920-
EVMAllowanceSlotDetector::new(AllowanceSlotDetectorConfig {
921-
rpc_url: rpc_url.to_string(),
922-
..Default::default()
923-
})
924-
.into_diagnostic()?,
925-
);
926-
Ok(Self {
927-
rpc_url: rpc_url.to_string(),
928-
provider,
929-
evm_balance_slot_detector,
930-
evm_allowance_slot_detector,
931-
})
932-
}
933-
}
934-
935879
/// Generate a unique simulation ID based on protocol system and state ID
936880
fn generate_simulation_id(protocol_system: &str, state_id: &str) -> String {
937881
let random_number: u32 = rand::random::<u32>() % 90000 + 10000; // Range 10000-99999

tycho-test/Cargo.toml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
[package]
2+
name = "tycho-test"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
alloy = { workspace = true, features = ["rpc-types-eth", "sol-types"] }
8+
alloy-chains = "0.2.6"
9+
alloy-rpc-types-trace = "1.0.38"
10+
colored = "3.0.0"
11+
dotenv = { workspace = true }
12+
futures = { workspace = true }
13+
itertools = { workspace = true }
14+
lru = "0.16.1"
15+
num-bigint = { workspace = true }
16+
num-traits = { workspace = true }
17+
miette = { version = "7.6.0" }
18+
regex = "1.12.2"
19+
serde = { workspace = true }
20+
serde_json = { workspace = true }
21+
rand = "0.9.2"
22+
tokio = { workspace = true }
23+
tokio-retry2 = { version = "0.6.0", features = ["jitter"] }
24+
tracing = { workspace = true }
25+
metrics = "0.24.2"
26+
metrics-exporter-prometheus = "0.17.2"
27+
actix-web = "4.11.0"
28+
tycho-client = { workspace = true }
29+
tycho-common = { workspace = true }
30+
tycho-ethereum = { workspace = true }
31+
tycho-execution = { workspace = true }
32+
tycho-simulation = { path = "..", features = ["evm"] }
33+
urlencoding = "2.1.3"
34+
tracing-subscriber = { workspace = true }

0 commit comments

Comments
 (0)