feat: Start simulation by calling get_amount_out
This commit is contained in:
1066
protocol-testing/Cargo.lock
generated
1066
protocol-testing/Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -1,7 +1,6 @@
|
|||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use colored::Colorize;
|
use colored::Colorize;
|
||||||
use hex::FromHex;
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use similar::{ChangeTag, TextDiff};
|
use similar::{ChangeTag, TextDiff};
|
||||||
use tycho_core::{dto::ProtocolComponent, Bytes};
|
use tycho_core::{dto::ProtocolComponent, Bytes};
|
||||||
|
|||||||
@@ -1,33 +1,31 @@
|
|||||||
use std::{collections::HashMap, env, ops::Deref, path::PathBuf, str::FromStr};
|
use std::{collections::HashMap, env, path::PathBuf, str::FromStr};
|
||||||
|
|
||||||
use alloy::{
|
use alloy::{primitives::U256, providers::Provider};
|
||||||
primitives::{bytes, U256},
|
|
||||||
providers::Provider,
|
|
||||||
};
|
|
||||||
use figment::{
|
use figment::{
|
||||||
providers::{Format, Yaml},
|
providers::{Format, Yaml},
|
||||||
Figment,
|
Figment,
|
||||||
};
|
};
|
||||||
use postgres::{Client, Error, NoTls};
|
use postgres::{Client, Error, NoTls};
|
||||||
use tokio::runtime::Runtime;
|
use tokio::runtime::Runtime;
|
||||||
use tracing::{debug, field::debug, info};
|
use tracing::{debug, info};
|
||||||
use tycho_core::{
|
use tycho_core::{
|
||||||
dto::{Chain, ProtocolComponent, ResponseAccount, ResponseProtocolState},
|
dto::{Chain, ProtocolComponent, ResponseAccount, ResponseProtocolState},
|
||||||
models::Address,
|
|
||||||
Bytes,
|
Bytes,
|
||||||
};
|
};
|
||||||
use tycho_simulation::{
|
use tycho_simulation::{
|
||||||
evm::{
|
evm::{
|
||||||
decoder::TychoStreamDecoder,
|
decoder::TychoStreamDecoder,
|
||||||
engine_db::tycho_db::PreCachedDB,
|
engine_db::tycho_db::PreCachedDB,
|
||||||
protocol::{
|
protocol::{u256_num::bytes_to_u256, vm::state::EVMPoolState},
|
||||||
u256_num::{bytes_to_u256, u256_to_f64},
|
|
||||||
vm::state::EVMPoolState,
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
tycho_client::feed::{synchronizer::StateSyncMessage, FeedMessage, Header},
|
models::Token,
|
||||||
|
tycho_client::feed::{
|
||||||
|
synchronizer::{ComponentWithState, Snapshot, StateSyncMessage},
|
||||||
|
FeedMessage, Header,
|
||||||
|
},
|
||||||
|
utils::load_all_tokens,
|
||||||
};
|
};
|
||||||
use tycho_simulation::tycho_client::feed::synchronizer::{ComponentW, ComponentWithState, Snapshot};
|
|
||||||
use crate::{
|
use crate::{
|
||||||
config::{IntegrationTest, IntegrationTestsConfig, ProtocolComponentWithTestConfig},
|
config::{IntegrationTest, IntegrationTestsConfig, ProtocolComponentWithTestConfig},
|
||||||
rpc::RPCProvider,
|
rpc::RPCProvider,
|
||||||
@@ -206,7 +204,7 @@ fn validate_state(
|
|||||||
|
|
||||||
let diff = expected_component
|
let diff = expected_component
|
||||||
.base
|
.base
|
||||||
.compare(&component, true);
|
.compare(component, true);
|
||||||
match diff {
|
match diff {
|
||||||
Some(diff) => {
|
Some(diff) => {
|
||||||
panic!("Component {} does not match the expected state:\n{}", component_id, diff);
|
panic!("Component {} does not match the expected state:\n{}", component_id, diff);
|
||||||
@@ -239,7 +237,7 @@ fn validate_state(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Test if balance check works
|
// TODO: Test if balance check works
|
||||||
if (!skip_balance_check) {
|
if !skip_balance_check {
|
||||||
info!(
|
info!(
|
||||||
"Validating token balance for component {} and token {}",
|
"Validating token balance for component {} and token {}",
|
||||||
component.id, token
|
component.id, token
|
||||||
@@ -271,7 +269,7 @@ fn validate_state(
|
|||||||
|
|
||||||
// Step 3: Run Tycho Simulation
|
// Step 3: Run Tycho Simulation
|
||||||
let mut decoder = TychoStreamDecoder::new();
|
let mut decoder = TychoStreamDecoder::new();
|
||||||
decoder.register_decoder::<EVMPoolState<PreCachedDB>>("test_protocol");
|
decoder.register_decoder::<EVMPoolState<PreCachedDB>>("test_protocol", None);
|
||||||
|
|
||||||
// Mock a stream message, with only a Snapshot and no deltas
|
// Mock a stream message, with only a Snapshot and no deltas
|
||||||
let mut states: HashMap<String, ComponentWithState> = HashMap::new();
|
let mut states: HashMap<String, ComponentWithState> = HashMap::new();
|
||||||
@@ -306,5 +304,52 @@ fn validate_state(
|
|||||||
},
|
},
|
||||||
)]);
|
)]);
|
||||||
|
|
||||||
let stream_message: FeedMessage = FeedMessage { state_msgs, sync_states: Default::default() };
|
let all_tokens = rt.block_on(load_all_tokens(
|
||||||
|
"localhost:4242",
|
||||||
|
true,
|
||||||
|
None,
|
||||||
|
Chain::Ethereum.into(),
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
));
|
||||||
|
|
||||||
|
let mut pairs: HashMap<String, Vec<Token>> = HashMap::new();
|
||||||
|
|
||||||
|
let message: FeedMessage = FeedMessage { state_msgs, sync_states: Default::default() };
|
||||||
|
|
||||||
|
let block_msg = rt
|
||||||
|
.block_on(decoder.decode(message))
|
||||||
|
.expect("Failed to decode message");
|
||||||
|
|
||||||
|
for (id, comp) in block_msg.new_pairs.iter() {
|
||||||
|
pairs
|
||||||
|
.entry(id.clone())
|
||||||
|
.or_insert_with(|| comp.tokens.clone());
|
||||||
|
}
|
||||||
|
|
||||||
|
// This is where we get blocked. Currently, Tycho Simulation expects the runtime to be
|
||||||
|
// prebuild and accessible from TychoSim - we should allow passing it when parsing the block
|
||||||
|
for (id, state) in block_msg.states.iter() {
|
||||||
|
if let Some(tokens) = pairs.get(id) {
|
||||||
|
let formatted_token_str = format!("{:}/{:}", &tokens[0].symbol, &tokens[1].symbol);
|
||||||
|
println!("Calculations for pool {:?} with tokens {:?}", id, formatted_token_str);
|
||||||
|
state
|
||||||
|
.spot_price(&tokens[0], &tokens[1])
|
||||||
|
.map(|price| println!("Spot price {:?}: {:?}", formatted_token_str, price))
|
||||||
|
.map_err(|e| eprintln!("Error calculating spot price for Pool {:?}: {:?}", id, e))
|
||||||
|
.ok();
|
||||||
|
let amount_in =
|
||||||
|
BigUint::from(1u32) * BigUint::from(10u32).pow(tokens[0].decimals as u32);
|
||||||
|
state
|
||||||
|
.get_amount_out(amount_in, &tokens[0], &tokens[1])
|
||||||
|
.map(|result| {
|
||||||
|
println!(
|
||||||
|
"Amount out for trading 1 {:?} -> {:?}: {:?} (takes {:?} gas)",
|
||||||
|
&tokens[0].symbol, &tokens[1].symbol, result.amount, result.gas
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.map_err(|e| eprintln!("Error calculating amount out for Pool {:?}: {:?}", id, e))
|
||||||
|
.ok();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
use std::{
|
use std::{
|
||||||
env,
|
|
||||||
error::Error,
|
error::Error,
|
||||||
fs,
|
fs,
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
|
|||||||
Reference in New Issue
Block a user