refactor: Misc improvements to code (#277)

* refactor: Misc improvements to code

- Decouple validating logic from TychoRunner
- Move all data fetching and decoding the tycho message into the same method
- Split validate_state into validate_state, validate_token_balances and simulate_and_execute
- Make rpc_provider and runtime attributes of TestRunner
- Add references where possible to avoid clones
- Remove unnecessary code
- Make clippy happy

#time 2h 36m

#time 0m


#time 3m

* chore: Use tycho deps and foundry from tycho_simulation

This is to try to decrease the risk of using conflicting versions in the different repositories

#time 32m


#time 0m

* chore: Read RPC_URL in main.rs

#time 10m

* fix: Support eth trades (skip balance and allowance overwrites) and set balance overwrite to amount in

For tokens like USDC setting the balance super high was making us getting blacklisted

#time 1h 12m

* fix: Fix curve tests and filter components_by_id with the expected_component_ids

#time 1h 30m


#time 0m

* fix: Don't use all the possible executor addresses. Hardcode just one for the test

Refactor overwrites logic:
- renamed functions
- moved logic around that fits together
- don't use StateOverrides and then convert to alloy overrides. Use alloy's directly

#time 1h 21m

* fix: Assume that the executors mapping starts at storage value 1

Move setup_router_overwrites away from the rpc and into the execution file
Delete unnecessary get_storage_at

#time 33m
This commit is contained in:
dianacarvalho1
2025-09-25 17:27:05 +01:00
committed by GitHub
parent 12369c3981
commit b577e7d6b2
13 changed files with 1161 additions and 1087 deletions

View File

@@ -9,10 +9,11 @@ use std::str::FromStr;
use alloy::{primitives::Keccak256, sol_types::SolValue};
use miette::{IntoDiagnostic, WrapErr};
use num_bigint::BigUint;
use tycho_common::{dto::Chain, Bytes};
use serde_json::json;
use tycho_simulation::{
evm::protocol::u256_num::biguint_to_u256,
protocol::models::ProtocolComponent,
tycho_common::{dto::Chain, Bytes},
tycho_execution::encoding::{
errors::EncodingError,
evm::{encoder_builders::TychoRouterEncoderBuilder, utils::bytes_to_address},
@@ -22,7 +23,7 @@ use tycho_simulation::{
},
};
use crate::execution::EXECUTORS_JSON;
use crate::execution::EXECUTOR_ADDRESS;
/// Creates a Solution for the given swap parameters.
///
@@ -36,17 +37,17 @@ use crate::execution::EXECUTORS_JSON;
/// # Returns
/// A `Result<Solution, EncodingError>` containing the solution, or an error if creation fails.
pub fn get_solution(
component: ProtocolComponent,
token_in: Bytes,
token_out: Bytes,
amount_in: BigUint,
amount_out: BigUint,
component: &ProtocolComponent,
token_in: &Bytes,
token_out: &Bytes,
amount_in: &BigUint,
amount_out: &BigUint,
) -> miette::Result<Solution> {
let alice_address = Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2")
let user_address = Bytes::from_str("0xf847a638E44186F3287ee9F8cAF73FF4d4B80784")
.into_diagnostic()
.wrap_err("Failed to parse Alice's address for Tycho router encoding")?;
let swap = SwapBuilder::new(component, token_in.clone(), token_out.clone()).build();
let swap = SwapBuilder::new(component.clone(), token_in.clone(), token_out.clone()).build();
let slippage = 0.0025; // 0.25% slippage
let bps = BigUint::from(10_000u32);
@@ -55,11 +56,11 @@ pub fn get_solution(
let min_amount_out = (amount_out * &multiplier) / &bps;
Ok(Solution {
sender: alice_address.clone(),
receiver: alice_address.clone(),
given_token: token_in,
given_amount: amount_in,
checked_token: token_out,
sender: user_address.clone(),
receiver: user_address.clone(),
given_token: token_in.clone(),
given_amount: amount_in.clone(),
checked_token: token_out.clone(),
exact_out: false,
checked_amount: min_amount_out,
swaps: vec![swap],
@@ -83,18 +84,26 @@ pub fn get_solution(
/// A `Result<Transaction, EncodingError>` containing the encoded transaction data for the Tycho
/// router, or an error if encoding fails.
pub fn encode_swap(
component: ProtocolComponent,
token_in: Bytes,
token_out: Bytes,
amount_in: BigUint,
amount_out: BigUint,
component: &ProtocolComponent,
token_in: &Bytes,
token_out: &Bytes,
amount_in: &BigUint,
amount_out: &BigUint,
) -> miette::Result<(Transaction, Solution)> {
let chain: tycho_common::models::Chain = Chain::Ethereum.into();
let protocol_system = component.protocol_system.clone();
let executors_json = json!({
"ethereum": {
(protocol_system):EXECUTOR_ADDRESS
}
})
.to_string();
let chain: tycho_simulation::tycho_common::models::Chain = Chain::Ethereum.into();
let encoder = TychoRouterEncoderBuilder::new()
.chain(chain)
.user_transfer_type(UserTransferType::TransferFrom)
.executors_addresses(EXECUTORS_JSON.to_string())
.executors_addresses(executors_json)
.historical_trade()
.build()
.into_diagnostic()