Skip to main content

tycho_execution/encoding/evm/
constants.rs

1use std::{
2    collections::{HashMap, HashSet},
3    sync::LazyLock,
4};
5
6use tycho_common::{models::Chain, Bytes};
7
8use crate::encoding::errors::EncodingError;
9
10pub(crate) const DEFAULT_EXECUTORS_JSON: &str =
11    include_str!("../../../config/executor_addresses.json");
12pub(crate) const DEFAULT_ROUTERS_JSON: &str = include_str!("../../../config/router_addresses.json");
13pub(crate) const PROTOCOL_SPECIFIC_CONFIG: &str =
14    include_str!("../../../config/protocol_specific_addresses.json");
15
16/// Default router addresses keyed by chain, parsed from `config/router_addresses.json`.
17pub static DEFAULT_ROUTER_ADDRESSES: LazyLock<HashMap<Chain, Bytes>> = LazyLock::new(|| {
18    serde_json::from_str(DEFAULT_ROUTERS_JSON).expect("valid router_addresses.json")
19});
20
21/// Returns the default Tycho router address for `chain`, or an error if none is configured.
22pub fn get_router_address(chain: &Chain) -> Result<&'static Bytes, EncodingError> {
23    DEFAULT_ROUTER_ADDRESSES
24        .get(chain)
25        .ok_or_else(|| {
26            EncodingError::FatalError(format!(
27                "No default router address found for chain {chain:?}"
28            ))
29        })
30}
31
32/// The address used by the TychoRouter to represent native ETH.
33///
34/// Callers must use this address (not `address(0)`) for the `tokenIn` / `tokenOut`
35/// parameters when ABI-encoding router function calls that involve native ETH.
36/// The encoding pipeline's `EncodedSolution` only contains the inner swap bytes;
37/// the outer function arguments — including the token addresses — are the caller's
38/// responsibility.
39pub static ROUTER_ETH_ADDRESS: LazyLock<Bytes> = LazyLock::new(|| {
40    Bytes::from(alloy::primitives::hex!("EeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE").to_vec())
41});
42
43/// The number of blocks in the future for which to fetch Angstrom Attestations
44///
45/// It is important to note that fetching more blocks will send more attestations to the
46/// Tycho Router, resulting in a higher gas usage. Fetching fewer blocks may result in attestations
47/// expiring if the transaction is not sent fast enough.
48pub const ANGSTROM_DEFAULT_BLOCKS_IN_FUTURE: u64 = 5;
49
50/// These protocols support the optimization of grouping swaps.
51///
52/// This requires special encoding to send call data of multiple swaps to a single executor,
53/// as if it were a single swap. The protocol likely uses flash accounting to save gas on token
54/// transfers.
55pub static GROUPABLE_PROTOCOLS: LazyLock<HashSet<&'static str>> = LazyLock::new(|| {
56    let mut set = HashSet::new();
57    set.insert("uniswap_v4");
58    set.insert("uniswap_v4_hooks");
59    set.insert("vm:balancer_v3");
60    set.insert("ekubo_v2");
61    set.insert("ekubo_v3");
62    set
63});
64
65/// These groupable protocols use simple concatenation instead of PLE when forming swap groups.
66pub static NON_PLE_ENCODED_PROTOCOLS: LazyLock<HashSet<&'static str>> = LazyLock::new(|| {
67    let mut set = HashSet::new();
68    set.insert("ekubo_v2");
69    set.insert("ekubo_v3");
70    set
71});