diff --git a/Cargo.lock b/Cargo.lock index 949d8e0..d351255 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4317,8 +4317,8 @@ checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" [[package]] name = "tycho-core" -version = "0.46.0" -source = "git+https://github.com/propeller-heads/tycho-indexer.git?tag=0.46.0#481b2f252eddb98442eab78069fa427068b5050d" +version = "0.55.2" +source = "git+https://github.com/propeller-heads/tycho-indexer.git?tag=0.55.2#dfa50d5e318253001938655a49aa3e05f958d89e" dependencies = [ "anyhow", "async-trait", diff --git a/Cargo.toml b/Cargo.toml index a1911e2..3dcfb37 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,8 +14,8 @@ lazy_static = "1.4.0" num-bigint = { version = "0.4.6", features = ["serde"] } hex = "0.4.3" num-traits = "0.2.19" -serde = { version = "1.0.217", features = ["derive"] } -serde_json = "1.0.135" +serde = { version = "1.0", features = ["derive"] } +serde_json = "1.0.105" thiserror = "1.0.69" tokio = { version = "1.38.0", features = ["full"] } chrono = "0.4.39" @@ -24,7 +24,7 @@ clap = { version = "4.5.3", features = ["derive"] } alloy = { version = "0.9.2", features = ["providers", "rpc-types-eth", "eip712", "signer-local"], optional = true } alloy-sol-types = { version = "0.8.14", optional = true } alloy-primitives = { version = "0.8.9", optional = true } -tycho-core = { git = "https://github.com/propeller-heads/tycho-indexer.git", package = "tycho-core", tag = "0.46.0" } +tycho-core = { git = "https://github.com/propeller-heads/tycho-indexer.git", package = "tycho-core", tag = "0.55.2" } once_cell = "1.20.2" [dev-dependencies] diff --git a/README.md b/README.md index 3910a29..bc64ddd 100644 --- a/README.md +++ b/README.md @@ -6,11 +6,16 @@ Tycho Execution makes it easy to trade on different DEXs by handling the complex custom code for each DEX, you get a simple, ready-to-use tool that generates the necessary data to execute trades. It's designed to be safe, straightforward, and quick to set up, so anyone can start trading without extra effort. +## Quickstart + +To get started, have a look at our [Quickstart example](examples/quickstart/README.md). + ## Bin Usage Guide ### Installation First, build and install the binary: + ```bash # Build the project cargo build --release @@ -19,12 +24,11 @@ cargo build --release cargo install --path . ``` -After installation, the `tycho-encode` command will be available to use from any directory in your terminal. The command accepts the following options: +After installation, the `tycho-encode` command will be available to use from any directory in your terminal. The command +accepts the following options: - `-c`: Path to the executor addresses configuration file (defaults to `src/encoding/config/executor_addresses.json`) - `-p`: Private key for signing approvals (required when direct_execution is false) -- `ROUTER_ADDRESS`: Router contract address (defaults to `0xaa820C29648D5EA543d712cC928377Bd7206a0E7`) - ### Encoding Transactions @@ -36,9 +40,6 @@ echo '' | tycho-encode # Using custom config path echo '' | tycho-encode -c /path/to/your/config.json - -# Using custom router address and config path -echo '' | tycho-encode -c /path/to/your/config.json 0x1234...5678 ``` #### Example @@ -49,7 +50,6 @@ Here's a complete example that encodes a swap from WETH to DAI using Uniswap V2: echo '{"sender":"0x1234567890123456789012345678901234567890","receiver":"0x1234567890123456789012345678901234567890","given_token":"0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2","given_amount":"1000000000000000000","checked_token":"0x6B175474E89094C44Da98b954EedeAC495271d0F","exact_out":false,"slippage":0.01,"expected_amount":"1000000000000000000","check_amount":"990000000000000000","router_address":"0xaa820C29648D5EA543d712cC928377Bd7206a0E7","swaps":[{"component":{"id":"0x88e6A0c2dDD26FEEb64F039a2c41296FcB3f5640","protocol_system":"uniswap_v2","protocol_type_name":"UniswapV2Pool","chain":"ethereum","tokens":["0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"],"contract_ids":["0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D"],"static_attributes":{"factory":"0x5c69bee701ef814a2b6a3edd4b1652cb9cc5aa6f"},"change":"Update","creation_tx":"0x0000000000000000000000000000000000000000000000000000000000000000","created_at":"2024-02-28T12:00:00"},"token_in":"0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2","token_out":"0x6B175474E89094C44Da98b954EedeAC495271d0F","split":1.0}],"direct_execution":true}' | tycho-encode ``` - #### JSON Payload Structure: Solution struct The `Solution` struct is composed of the following fields: @@ -64,10 +64,10 @@ The `Solution` struct is composed of the following fields: - `expected_amount`: The expected output amount - `check_amount`: The minimum acceptable output amount (accounting for slippage) - `swaps`: Array of swap steps, each containing: - - `component`: Details about the DEX/protocol being used - - `token_in`: Input token address for this step - - `token_out`: Output token address for this step - - `split`: Proportion of tokens to route through this step (1.0 = 100%) + - `component`: Details about the DEX/protocol being used + - `token_in`: Input token address for this step + - `token_out`: Output token address for this step + - `split`: Proportion of tokens to route through this step (1.0 = 100%) - `router_address`: The address of the protocol's router contract - `direct_execution`: Boolean indicating if the transaction should be executed directly diff --git a/examples/quickstart/Readme.md b/examples/quickstart/README.md similarity index 100% rename from examples/quickstart/Readme.md rename to examples/quickstart/README.md diff --git a/examples/quickstart/main.rs b/examples/quickstart/main.rs index f63cd19..f54f5e5 100644 --- a/examples/quickstart/main.rs +++ b/examples/quickstart/main.rs @@ -10,26 +10,25 @@ use tycho_execution::encoding::{ strategy_encoder::strategy_encoder_registry::EVMStrategyEncoderRegistry, tycho_encoder::EVMTychoEncoder, }, - models::{Chain, Solution, Swap}, + models::{Solution, Swap}, strategy_encoder::StrategyEncoderRegistry, tycho_encoder::TychoEncoder, }; fn main() { // Setup variables - let router_address = "0x1234567890abcdef1234567890abcdef12345678".to_string(); + let router_address = Bytes::from_str("0x1234567890abcdef1234567890abcdef12345678") + .expect("Failed to create router address"); let signer_pk = Some("0x123456789abcdef123456789abcdef123456789abcdef123456789abcdef1234".to_string()); let user_address = Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2") .expect("Failed to create user address"); - let executors_file_path = "src/encoding/config/executor_addresses.json"; - let eth_chain = Chain::from(TychoCoreChain::Ethereum); // Initialize the encoder let strategy_encoder_registry = - EVMStrategyEncoderRegistry::new(eth_chain.clone(), executors_file_path, signer_pk.clone()) + EVMStrategyEncoderRegistry::new(TychoCoreChain::Ethereum, None, signer_pk.clone()) .expect("Failed to create strategy encoder registry"); - let encoder = EVMTychoEncoder::new(strategy_encoder_registry, router_address, eth_chain) + let encoder = EVMTychoEncoder::new(strategy_encoder_registry, TychoCoreChain::Ethereum) .expect("Failed to create encoder"); // ------------------- Encode a simple swap ------------------- @@ -65,6 +64,7 @@ fn main() { exact_out: false, // it's an exact in solution check_amount: None, // the amount out will not be checked in execution swaps: vec![simple_swap], + router_address, ..Default::default() }; diff --git a/src/bin/lib/cli.rs b/src/bin/lib/cli.rs index 25e8786..8027b2a 100644 --- a/src/bin/lib/cli.rs +++ b/src/bin/lib/cli.rs @@ -1,6 +1,4 @@ pub use clap::Parser; -pub const DEFAULT_ROUTER_ADDRESS: &str = "0xaa820C29648D5EA543d712cC928377Bd7206a0E7"; -pub const DEFAULT_CONFIG_PATH: &str = "src/encoding/config/executor_addresses.json"; #[derive(Parser)] /// Encode swap transactions for the Tycho router @@ -36,15 +34,11 @@ pub const DEFAULT_CONFIG_PATH: &str = "src/encoding/config/executor_addresses.js /// } /// ``` pub struct Cli { - /// Router contract address to use for encoding transactions - #[arg(default_value = DEFAULT_ROUTER_ADDRESS)] - pub router_address: String, - /// Private key for signing approvals (required when direct_execution is false) #[arg(short)] pub private_key: Option, /// Path to the executor addresses configuration file - #[arg(short, default_value = DEFAULT_CONFIG_PATH)] - pub config_path: String, + #[arg(short)] + pub config_path: Option, } diff --git a/src/bin/tycho-encode.rs b/src/bin/tycho-encode.rs index 62bb74d..f8bd8b3 100644 --- a/src/bin/tycho-encode.rs +++ b/src/bin/tycho-encode.rs @@ -33,7 +33,7 @@ fn main() -> Result<(), Box> { } // Encode the solution - let encoded = encode_swaps(&buffer, &cli.router_address, &cli.config_path, cli.private_key)?; + let encoded = encode_swaps(&buffer, cli.config_path, cli.private_key)?; // Output the encoded result as JSON to stdout println!( @@ -47,17 +47,14 @@ fn main() -> Result<(), Box> { fn encode_swaps( input: &str, - router_address: &str, - config_path: &str, + config_path: Option, private_key: Option, ) -> Result> { let solution: Solution = serde_json::from_str(input)?; let chain = Chain::Ethereum; - let strategy_selector = - EVMStrategyEncoderRegistry::new(chain.into(), config_path, private_key)?; - let encoder = - EVMTychoEncoder::new(strategy_selector, router_address.to_string(), chain.into())?; + let strategy_selector = EVMStrategyEncoderRegistry::new(chain, config_path, private_key)?; + let encoder = EVMTychoEncoder::new(strategy_selector, chain)?; let transactions = encoder.encode_router_calldata(vec![solution])?; Ok(serde_json::json!({ diff --git a/src/encoding/config/executor_addresses.json b/src/encoding/config/executor_addresses.json index 6e27a2d..32c9e3e 100644 --- a/src/encoding/config/executor_addresses.json +++ b/src/encoding/config/executor_addresses.json @@ -1,6 +1,7 @@ { "ethereum": { "uniswap_v2": "0x5C2F5a71f67c01775180ADc06909288B4C329308", + "uniswap_v3": "0x5C2F5a71f67c01775180ADc06909288B4C329308", "vm:balancer_v2": "0x543778987b293C7E8Cf0722BB2e935ba6f4068D4" } } \ No newline at end of file diff --git a/src/encoding/evm/approvals/permit2.rs b/src/encoding/evm/approvals/permit2.rs index c118358..2985a28 100644 --- a/src/encoding/evm/approvals/permit2.rs +++ b/src/encoding/evm/approvals/permit2.rs @@ -11,7 +11,10 @@ use alloy_primitives::{PrimitiveSignature as Signature, B256}; use alloy_sol_types::{eip712_domain, sol, SolStruct, SolValue}; use chrono::Utc; use num_bigint::BigUint; -use tokio::runtime::Runtime; +use tokio::{ + runtime::{Handle, Runtime}, + task::block_in_place, +}; use tycho_core::Bytes; use crate::encoding::{ @@ -25,12 +28,19 @@ use crate::encoding::{ /// Struct for managing Permit2 operations, including encoding approvals and fetching allowance /// data. +#[derive(Clone)] pub struct Permit2 { address: Address, client: Arc>, - runtime: Runtime, signer: PrivateKeySigner, chain_id: ChainId, + runtime_handle: Handle, + // Store the runtime to prevent it from being dropped before use. + // This is required since tycho-execution does not have a pre-existing runtime. + // However, if the library is used in a context where a runtime already exists, it is not + // necessary to store it. + #[allow(dead_code)] + runtime: Option>, } /// Type alias for representing allowance data as a tuple of (amount, expiration, nonce). Used for @@ -60,9 +70,16 @@ sol! { impl Permit2 { pub fn new(signer_pk: String, chain: Chain) -> Result { - let runtime = Runtime::new() - .map_err(|_| EncodingError::FatalError("Failed to create runtime".to_string()))?; - let client = runtime.block_on(get_client())?; + let (handle, runtime) = match Handle::try_current() { + Ok(h) => (h, None), + Err(_) => { + let rt = Arc::new(Runtime::new().map_err(|_| { + EncodingError::FatalError("Failed to create a new tokio runtime".to_string()) + })?); + (rt.handle().clone(), Some(rt)) + } + }; + let client = block_in_place(|| handle.block_on(get_client()))?; let pk = B256::from_str(&signer_pk).map_err(|_| { EncodingError::FatalError("Failed to convert signer private key to B256".to_string()) })?; @@ -73,9 +90,10 @@ impl Permit2 { address: Address::from_str("0x000000000022D473030F116dDEE9F6B43aC78BA3") .map_err(|_| EncodingError::FatalError("Permit2 address not valid".to_string()))?, client, - runtime, + runtime_handle: handle, signer, chain_id: chain.id, + runtime, }) } @@ -94,9 +112,10 @@ impl Permit2 { ..Default::default() }; - let output = self - .runtime - .block_on(async { self.client.call(&tx).await }); + let output = block_in_place(|| { + self.runtime_handle + .block_on(async { self.client.call(&tx).await }) + }); match output { Ok(response) => { let allowance: Allowance = @@ -283,14 +302,16 @@ mod tests { input: TransactionInput { input: Some(AlloyBytes::from(data)), data: None }, ..Default::default() }; - let receipt = permit2.runtime.block_on(async { - let pending_tx = permit2 - .client - .send_transaction(tx) - .await - .unwrap(); - // Wait for the transaction to be mined - pending_tx.get_receipt().await.unwrap() + let receipt = block_in_place(|| { + permit2.runtime_handle.block_on(async { + let pending_tx = permit2 + .client + .send_transaction(tx) + .await + .unwrap(); + // Wait for the transaction to be mined + pending_tx.get_receipt().await.unwrap() + }) }); assert!(receipt.status(), "Approve transaction failed"); @@ -314,7 +335,7 @@ mod tests { ..Default::default() }; - let result = permit2.runtime.block_on(async { + let result = permit2.runtime_handle.block_on(async { let pending_tx = permit2 .client .send_transaction(tx) diff --git a/src/encoding/evm/constants.rs b/src/encoding/evm/constants.rs new file mode 100644 index 0000000..1426dd4 --- /dev/null +++ b/src/encoding/evm/constants.rs @@ -0,0 +1,2 @@ +pub const DEFAULT_EXECUTORS_JSON: &str = + include_str!("../../../src/encoding/config/executor_addresses.json"); diff --git a/src/encoding/evm/mod.rs b/src/encoding/evm/mod.rs index 9c736bb..55557f3 100644 --- a/src/encoding/evm/mod.rs +++ b/src/encoding/evm/mod.rs @@ -1,4 +1,5 @@ pub mod approvals; +mod constants; pub mod strategy_encoder; mod swap_encoder; pub mod tycho_encoder; diff --git a/src/encoding/evm/models.rs b/src/encoding/evm/models.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/encoding/evm/strategy_encoder/strategy_encoder_registry.rs b/src/encoding/evm/strategy_encoder/strategy_encoder_registry.rs index 8ca10ce..71b266d 100644 --- a/src/encoding/evm/strategy_encoder/strategy_encoder_registry.rs +++ b/src/encoding/evm/strategy_encoder/strategy_encoder_registry.rs @@ -21,10 +21,11 @@ pub struct EVMStrategyEncoderRegistry { impl StrategyEncoderRegistry for EVMStrategyEncoderRegistry { fn new( - chain: Chain, - executors_file_path: &str, + chain: tycho_core::dto::Chain, + executors_file_path: Option, signer_pk: Option, ) -> Result { + let chain = Chain::from(chain); let swap_encoder_registry = SwapEncoderRegistry::new(executors_file_path, chain.clone())?; let mut strategies: HashMap> = HashMap::new(); @@ -54,3 +55,15 @@ impl StrategyEncoderRegistry for EVMStrategyEncoderRegistry { } } } + +impl Clone for EVMStrategyEncoderRegistry { + fn clone(&self) -> Self { + Self { + strategies: self + .strategies + .iter() + .map(|(k, v)| (k.clone(), v.clone_box())) + .collect(), + } + } +} diff --git a/src/encoding/evm/strategy_encoder/strategy_encoders.rs b/src/encoding/evm/strategy_encoder/strategy_encoders.rs index af032a7..fa2b211 100644 --- a/src/encoding/evm/strategy_encoder/strategy_encoders.rs +++ b/src/encoding/evm/strategy_encoder/strategy_encoders.rs @@ -73,6 +73,7 @@ pub trait EVMStrategyEncoder: StrategyEncoder { /// * `permit2`: Permit2, responsible for managing permit2 operations and providing necessary /// signatures and permit2 objects for calling the router /// * `selector`: String, the selector for the swap function in the router contract +#[derive(Clone)] pub struct SplitSwapStrategyEncoder { swap_encoder_registry: SwapEncoderRegistry, permit2: Permit2, @@ -260,11 +261,7 @@ impl SplitSwapStrategyEncoder { impl EVMStrategyEncoder for SplitSwapStrategyEncoder {} impl StrategyEncoder for SplitSwapStrategyEncoder { - fn encode_strategy( - &self, - solution: Solution, - router_address: Bytes, - ) -> Result<(Vec, Bytes), EncodingError> { + fn encode_strategy(&self, solution: Solution) -> Result<(Vec, Bytes), EncodingError> { self.validate_split_percentages(&solution.swaps)?; self.validate_swap_path( &solution.swaps, @@ -273,7 +270,7 @@ impl StrategyEncoder for SplitSwapStrategyEncoder { &solution.native_action, )?; let (permit, signature) = self.permit2.get_permit( - &router_address, + &solution.router_address, &solution.sender, &solution.given_token, &solution.given_amount, @@ -345,9 +342,9 @@ impl StrategyEncoder for SplitSwapStrategyEncoder { })?; let encoding_context = EncodingContext { - receiver: router_address.clone(), + receiver: solution.router_address.clone(), exact_out: solution.exact_out, - router_address: router_address.clone(), + router_address: solution.router_address.clone(), }; let protocol_data = swap_encoder.encode_swap(swap.clone(), encoding_context)?; let swap_data = self.encode_swap_header( @@ -398,13 +395,17 @@ impl StrategyEncoder for SplitSwapStrategyEncoder { .abi_encode(); let contract_interaction = encode_input(&self.selector, method_calldata); - Ok((contract_interaction, router_address)) + Ok((contract_interaction, solution.router_address)) } fn get_swap_encoder(&self, protocol_system: &str) -> Option<&Box> { self.swap_encoder_registry .get_encoder(protocol_system) } + + fn clone_box(&self) -> Box { + Box::new(self.clone()) + } } /// This strategy encoder is used for solutions that are sent directly to the executor, bypassing @@ -412,6 +413,7 @@ impl StrategyEncoder for SplitSwapStrategyEncoder { /// /// # Fields /// * `swap_encoder_registry`: SwapEncoderRegistry, containing all possible swap encoders +#[derive(Clone)] pub struct ExecutorStrategyEncoder { swap_encoder_registry: SwapEncoderRegistry, } @@ -423,17 +425,7 @@ impl ExecutorStrategyEncoder { } impl EVMStrategyEncoder for ExecutorStrategyEncoder {} impl StrategyEncoder for ExecutorStrategyEncoder { - fn encode_strategy( - &self, - solution: Solution, - _router_address: Bytes, - ) -> Result<(Vec, Bytes), EncodingError> { - let router_address = solution.router_address.ok_or_else(|| { - EncodingError::InvalidInput( - "Router address is required for straight-to-executor solutions".to_string(), - ) - })?; - + fn encode_strategy(&self, solution: Solution) -> Result<(Vec, Bytes), EncodingError> { let swap = solution .swaps .first() @@ -451,7 +443,7 @@ impl StrategyEncoder for ExecutorStrategyEncoder { let encoding_context = EncodingContext { receiver: solution.receiver, exact_out: solution.exact_out, - router_address, + router_address: solution.router_address, }; let protocol_data = swap_encoder.encode_swap(swap.clone(), encoding_context)?; @@ -459,10 +451,15 @@ impl StrategyEncoder for ExecutorStrategyEncoder { .map_err(|_| EncodingError::FatalError("Invalid executor address".to_string()))?; Ok((protocol_data, executor_address)) } + fn get_swap_encoder(&self, protocol_system: &str) -> Option<&Box> { self.swap_encoder_registry .get_encoder(protocol_system) } + + fn clone_box(&self) -> Box { + Box::new(self.clone()) + } } #[cfg(test)] @@ -495,7 +492,7 @@ mod tests { fn get_swap_encoder_registry() -> SwapEncoderRegistry { let eth_chain = eth_chain(); - SwapEncoderRegistry::new("src/encoding/config/executor_addresses.json", eth_chain).unwrap() + SwapEncoderRegistry::new(None, eth_chain).unwrap() } #[test] @@ -529,13 +526,13 @@ mod tests { receiver: Bytes::from_str("0x1d96f2f6bef1202e4ce1ff6dad0c2cb002861d3e").unwrap(), swaps: vec![swap], direct_execution: true, - router_address: Some(Bytes::zero(20)), + router_address: Bytes::from_str("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395").unwrap(), slippage: None, native_action: None, }; let (protocol_data, executor_address) = encoder - .encode_strategy(solution, Bytes::zero(20)) + .encode_strategy(solution) .unwrap(); let hex_protocol_data = encode(&protocol_data); assert_eq!( @@ -620,13 +617,13 @@ mod tests { check_amount, sender: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(), receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(), + router_address: Bytes::from_str("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395").unwrap(), swaps: vec![swap], ..Default::default() }; - let router_address = Bytes::from_str("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395").unwrap(); let (calldata, _) = encoder - .encode_strategy(solution, router_address) + .encode_strategy(solution) .unwrap(); let expected_min_amount_encoded = hex::encode(U256::abi_encode(&expected_min_amount)); let expected_input = [ @@ -720,14 +717,14 @@ mod tests { check_amount: None, sender: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(), receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(), + router_address: Bytes::from_str("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395").unwrap(), swaps: vec![swap], native_action: Some(NativeAction::Wrap), ..Default::default() }; - let router_address = Bytes::from_str("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395").unwrap(); let (calldata, _) = encoder - .encode_strategy(solution, router_address) + .encode_strategy(solution) .unwrap(); let hex_calldata = encode(&calldata); @@ -768,14 +765,14 @@ mod tests { check_amount: None, sender: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(), receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(), + router_address: Bytes::from_str("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395").unwrap(), swaps: vec![swap], native_action: Some(NativeAction::Unwrap), ..Default::default() }; - let router_address = Bytes::from_str("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395").unwrap(); let (calldata, _) = encoder - .encode_strategy(solution, router_address) + .encode_strategy(solution) .unwrap(); let hex_calldata = encode(&calldata); @@ -857,13 +854,13 @@ mod tests { check_amount: None, sender: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(), receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(), + router_address: Bytes::from_str("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395").unwrap(), swaps: vec![swap_weth_dai, swap_weth_wbtc, swap_dai_usdc, swap_wbtc_usdc], ..Default::default() }; - let router_address = Bytes::from_str("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395").unwrap(); let (calldata, _) = encoder - .encode_strategy(solution, router_address) + .encode_strategy(solution) .unwrap(); let _hex_calldata = encode(&calldata); diff --git a/src/encoding/evm/swap_encoder/builder.rs b/src/encoding/evm/swap_encoder/builder.rs index 4d99206..430dcb2 100644 --- a/src/encoding/evm/swap_encoder/builder.rs +++ b/src/encoding/evm/swap_encoder/builder.rs @@ -1,6 +1,8 @@ use crate::encoding::{ errors::EncodingError, - evm::swap_encoder::swap_encoders::{BalancerV2SwapEncoder, UniswapV2SwapEncoder}, + evm::swap_encoder::swap_encoders::{ + BalancerV2SwapEncoder, UniswapV2SwapEncoder, UniswapV3SwapEncoder, + }, swap_encoder::SwapEncoder, }; @@ -22,6 +24,7 @@ impl SwapEncoderBuilder { match self.protocol_system.as_str() { "uniswap_v2" => Ok(Box::new(UniswapV2SwapEncoder::new(self.executor_address))), "vm:balancer_v2" => Ok(Box::new(BalancerV2SwapEncoder::new(self.executor_address))), + "uniswap_v3" => Ok(Box::new(UniswapV3SwapEncoder::new(self.executor_address))), _ => Err(EncodingError::FatalError(format!( "Unknown protocol system: {}", self.protocol_system diff --git a/src/encoding/evm/swap_encoder/swap_encoder_registry.rs b/src/encoding/evm/swap_encoder/swap_encoder_registry.rs index a9bb22f..30599d3 100644 --- a/src/encoding/evm/swap_encoder/swap_encoder_registry.rs +++ b/src/encoding/evm/swap_encoder/swap_encoder_registry.rs @@ -1,7 +1,9 @@ use std::{collections::HashMap, fs}; use crate::encoding::{ - errors::EncodingError, evm::swap_encoder::builder::SwapEncoderBuilder, models::Chain, + errors::EncodingError, + evm::{constants::DEFAULT_EXECUTORS_JSON, swap_encoder::builder::SwapEncoderBuilder}, + models::Chain, swap_encoder::SwapEncoder, }; @@ -15,8 +17,20 @@ pub struct SwapEncoderRegistry { impl SwapEncoderRegistry { /// Populates the registry with the `SwapEncoders` for the given blockchain by parsing the /// executors' addresses in the file at the given path. - pub fn new(executors_file_path: &str, blockchain: Chain) -> Result { - let config_str = fs::read_to_string(executors_file_path)?; + pub fn new( + executors_file_path: Option, + blockchain: Chain, + ) -> Result { + let config_str = if let Some(ref path) = executors_file_path { + fs::read_to_string(path).map_err(|e| { + EncodingError::FatalError(format!( + "Error reading executors file from {:?}: {}", + executors_file_path, e + )) + })? + } else { + DEFAULT_EXECUTORS_JSON.to_string() + }; let config: HashMap> = serde_json::from_str(&config_str)?; let mut encoders = HashMap::new(); let executors = config diff --git a/src/encoding/evm/swap_encoder/swap_encoders.rs b/src/encoding/evm/swap_encoder/swap_encoders.rs index 34c1203..f3d0fb0 100644 --- a/src/encoding/evm/swap_encoder/swap_encoders.rs +++ b/src/encoding/evm/swap_encoder/swap_encoders.rs @@ -104,28 +104,25 @@ impl SwapEncoder for UniswapV3SwapEncoder { let zero_to_one = Self::get_zero_to_one(token_in_address, token_out_address); let component_id = Address::from_str(&swap.component.id) .map_err(|_| EncodingError::FatalError("Invalid USV3 component id".to_string()))?; - let mut pool_fee_bytes = swap + let pool_fee_bytes = swap .component .static_attributes - .get("pool_fee") + .get("fee") .ok_or_else(|| { EncodingError::FatalError( "Pool fee not found in Uniswap v3 static attributes".to_string(), ) })? - .as_ref() .to_vec(); - // Reverse to get be bytes, since this is encoded as le bytes - pool_fee_bytes.reverse(); + // this is necessary to pad on the left with zeros if the fee is less than 3 bytes + let mut padded_fee_bytes = [0u8; 3]; + let start = 3 - pool_fee_bytes.len(); + padded_fee_bytes[start..].copy_from_slice(&pool_fee_bytes); - let pool_fee_u24: [u8; 3] = pool_fee_bytes[pool_fee_bytes.len() - 3..] + let pool_fee_u24: [u8; 3] = padded_fee_bytes[(padded_fee_bytes.len() - 3)..] .try_into() - .map_err(|_| { - EncodingError::FatalError( - "Pool fee static attribute must be at least 3 bytes".to_string(), - ) - })?; + .map_err(|_| EncodingError::FatalError("Failed to extract fee bytes".to_string()))?; let args = ( token_in_address, @@ -214,6 +211,7 @@ mod tests { use std::collections::HashMap; use alloy::hex::encode; + use num_bigint::BigInt; use tycho_core::{dto::ProtocolComponent, Bytes}; use super::*; @@ -257,9 +255,10 @@ mod tests { } #[test] fn test_encode_uniswap_v3() { - let encoded_pool_fee: [u8; 4] = 500u32.to_le_bytes(); + let fee = BigInt::from(500); + let encoded_pool_fee = Bytes::from(fee.to_signed_bytes_be()); let mut static_attributes: HashMap = HashMap::new(); - static_attributes.insert("pool_fee".into(), Bytes::from(encoded_pool_fee[..3].to_vec())); + static_attributes.insert("fee".into(), Bytes::from(encoded_pool_fee.to_vec())); let usv3_pool = ProtocolComponent { id: String::from("0x88e6A0c2dDD26FEEb64F039a2c41296FcB3f5640"), diff --git a/src/encoding/evm/tycho_encoder.rs b/src/encoding/evm/tycho_encoder.rs index a022d18..6e66755 100644 --- a/src/encoding/evm/tycho_encoder.rs +++ b/src/encoding/evm/tycho_encoder.rs @@ -1,5 +1,3 @@ -use std::str::FromStr; - use num_bigint::BigUint; use tycho_core::Bytes; @@ -10,30 +8,23 @@ use crate::encoding::{ tycho_encoder::TychoEncoder, }; -/// Represents an encoder for a swap through the given router address using any strategy supported -/// by the strategy registry. +/// Represents an encoder for a swap using any strategy supported by the strategy registry. /// /// # Fields /// * `strategy_registry`: S, the strategy registry to use to select the best strategy to encode a /// solution, based on its supported strategies and the solution attributes. -/// * `router_address`: Bytes, the address of the router to use to execute the swaps. /// * `native_address`: Address of the chain's native token /// * `wrapped_address`: Address of the chain's wrapped native token +#[derive(Clone)] pub struct EVMTychoEncoder { strategy_registry: S, - router_address: Bytes, native_address: Bytes, wrapped_address: Bytes, } impl EVMTychoEncoder { - pub fn new( - strategy_registry: S, - router_address: String, - chain: Chain, - ) -> Result { - let router_address = Bytes::from_str(&router_address) - .map_err(|_| EncodingError::FatalError("Invalid router address".to_string()))?; + pub fn new(strategy_registry: S, chain: tycho_core::dto::Chain) -> Result { + let chain: Chain = Chain::from(chain); if chain.name != *"ethereum" { return Err(EncodingError::InvalidInput( "Currently only Ethereum is supported".to_string(), @@ -41,7 +32,6 @@ impl EVMTychoEncoder { } Ok(EVMTychoEncoder { strategy_registry, - router_address, native_address: chain.native_token()?, wrapped_address: chain.wrapped_token()?, }) @@ -111,16 +101,11 @@ impl TychoEncoder for EVMTychoEncoder { for solution in solutions.iter() { self.validate_solution(solution)?; - let router_address = solution - .router_address - .clone() - .unwrap_or(self.router_address.clone()); - let strategy = self .strategy_registry .get_encoder(solution)?; let (contract_interaction, target_address) = - strategy.encode_strategy(solution.clone(), router_address)?; + strategy.encode_strategy(solution.clone())?; let value = match solution.native_action.as_ref() { Some(NativeAction::Wrap) => solution.given_amount.clone(), @@ -139,6 +124,8 @@ impl TychoEncoder for EVMTychoEncoder { #[cfg(test)] mod tests { + use std::str::FromStr; + use tycho_core::dto::{Chain as TychoCoreChain, ProtocolComponent}; use super::*; @@ -150,10 +137,6 @@ mod tests { strategy: Box, } - fn eth_chain() -> Chain { - TychoCoreChain::Ethereum.into() - } - fn dai() -> Bytes { Bytes::from_str("0x6b175474e89094c44da98b954eedeac495271d0f").unwrap() } @@ -168,8 +151,8 @@ mod tests { impl StrategyEncoderRegistry for MockStrategyRegistry { fn new( - _chain: Chain, - _executors_file_path: &str, + _chain: tycho_core::dto::Chain, + _executors_file_path: Option, _signer_pk: Option, ) -> Result { Ok(Self { strategy: Box::new(MockStrategy) }) @@ -183,14 +166,11 @@ mod tests { } } + #[derive(Clone)] struct MockStrategy; impl StrategyEncoder for MockStrategy { - fn encode_strategy( - &self, - _solution: Solution, - _router_address: Bytes, - ) -> Result<(Vec, Bytes), EncodingError> { + fn encode_strategy(&self, _solution: Solution) -> Result<(Vec, Bytes), EncodingError> { Ok(( Bytes::from_str("0x1234") .unwrap() @@ -202,16 +182,15 @@ mod tests { fn get_swap_encoder(&self, _protocol_system: &str) -> Option<&Box> { None } + fn clone_box(&self) -> Box { + Box::new(self.clone()) + } } fn get_mocked_tycho_encoder() -> EVMTychoEncoder { - let strategy_registry = MockStrategyRegistry::new(eth_chain(), "", None).unwrap(); - EVMTychoEncoder::new( - strategy_registry, - "0x1234567890abcdef1234567890abcdef12345678".to_string(), - eth_chain(), - ) - .unwrap() + let strategy_registry = + MockStrategyRegistry::new(TychoCoreChain::Ethereum, None, None).unwrap(); + EVMTychoEncoder::new(strategy_registry, TychoCoreChain::Ethereum).unwrap() } #[test] @@ -233,7 +212,7 @@ mod tests { exact_out: false, given_amount: eth_amount_in.clone(), given_token: eth(), - router_address: None, + router_address: Bytes::from_str("0x1234567890abcdef1234567890abcdef12345678").unwrap(), swaps: vec![swap], native_action: Some(NativeAction::Wrap), ..Default::default() diff --git a/src/encoding/models.rs b/src/encoding/models.rs index 6a0e937..4853fcc 100644 --- a/src/encoding/models.rs +++ b/src/encoding/models.rs @@ -41,8 +41,8 @@ pub struct Solution { pub check_amount: Option, /// List of swaps to fulfill the solution. pub swaps: Vec, - /// If not set, then the Tycho Router will be used - pub router_address: Option, + /// Address of the router contract to be used for the swaps. + pub router_address: Bytes, /// If set, the corresponding native action will be executed. pub native_action: Option, /// If set to true, the solution will be encoded to be sent directly to the Executor and @@ -137,6 +137,7 @@ impl From for ChainId { TychoCoreChain::ZkSync => ChainId(324), TychoCoreChain::Arbitrum => ChainId(42161), TychoCoreChain::Starknet => ChainId(0), + TychoCoreChain::Base => ChainId(8453), } } } diff --git a/src/encoding/strategy_encoder.rs b/src/encoding/strategy_encoder.rs index bb7ecf4..744df63 100644 --- a/src/encoding/strategy_encoder.rs +++ b/src/encoding/strategy_encoder.rs @@ -1,21 +1,14 @@ -use tycho_core::Bytes; +use tycho_core::{dto::Chain, Bytes}; -use crate::encoding::{ - errors::EncodingError, - models::{Chain, Solution}, - swap_encoder::SwapEncoder, -}; +use crate::encoding::{errors::EncodingError, models::Solution, swap_encoder::SwapEncoder}; /// Encodes a solution using a specific strategy. pub trait StrategyEncoder { - fn encode_strategy( - &self, - to_encode: Solution, - router_address: Bytes, - ) -> Result<(Vec, Bytes), EncodingError>; + fn encode_strategy(&self, to_encode: Solution) -> Result<(Vec, Bytes), EncodingError>; #[allow(clippy::borrowed_box)] fn get_swap_encoder(&self, protocol_system: &str) -> Option<&Box>; + fn clone_box(&self) -> Box; } /// Contains the supported strategies to encode a solution, and chooses the best strategy to encode @@ -23,7 +16,7 @@ pub trait StrategyEncoder { pub trait StrategyEncoderRegistry { fn new( chain: Chain, - executors_file_path: &str, + executors_file_path: Option, signer_pk: Option, ) -> Result where