diff --git a/config/executor_addresses.json b/config/executor_addresses.json index 18938f0..3611b22 100644 --- a/config/executor_addresses.json +++ b/config/executor_addresses.json @@ -1,9 +1,9 @@ { "ethereum": { "uniswap_v2": "0x00C1b81e3C8f6347E69e2DDb90454798A6Be975E", - "uniswap_v3": "0x5C2F5a71f67c01775180ADc06909288B4C329308", - "uniswap_v4": "0xF62849F9A0B5Bf2913b396098F7c7019b51A820a", - "vm:balancer_v2": "0x543778987b293C7E8Cf0722BB2e935ba6f4068D4" + "uniswap_v3": "0xF744EBfaA580cF3fFc25aD046E92BD8B770a0700", + "uniswap_v4": "0x90BE4620436354c9DfA58614B3Bdd5a80FBfAF31", + "vm:balancer_v2": "0xffe5B139b396c9A3d5d9ab89AAE78bF3070CbD64" }, "tenderly_ethereum": { "uniswap_v2": "0x00C1b81e3C8f6347E69e2DDb90454798A6Be975E", diff --git a/foundry/hardhat.config.js b/foundry/hardhat.config.js index 8969e8a..420692c 100644 --- a/foundry/hardhat.config.js +++ b/foundry/hardhat.config.js @@ -27,11 +27,13 @@ module.exports = { }, ethereum: { url: process.env.RPC_URL, - accounts: [process.env.PRIVATE_KEY] + accounts: [process.env.PRIVATE_KEY], + chainId: 1 }, base: { url: process.env.RPC_URL, - accounts: [process.env.PRIVATE_KEY] + accounts: [process.env.PRIVATE_KEY], + chainId: 8453 } }, diff --git a/foundry/scripts/set-executors.js b/foundry/scripts/set-executors.js index 421ff0d..a1b0d3c 100644 --- a/foundry/scripts/set-executors.js +++ b/foundry/scripts/set-executors.js @@ -51,7 +51,7 @@ async function main() { // Set executors const executorAddresses = executorsToSet.map(executor => executor.executor); const tx = await router.setExecutors(executorAddresses, { - gasLimit: 100000 + gasLimit: 200000 // should be around 50k per executor }); await tx.wait(); // Wait for the transaction to be mined console.log(`Executors set at transaction: ${tx.hash}`); diff --git a/src/encoding/evm/approvals/permit2.rs b/src/encoding/evm/approvals/permit2.rs index 0374afd..9eb6c3e 100644 --- a/src/encoding/evm/approvals/permit2.rs +++ b/src/encoding/evm/approvals/permit2.rs @@ -21,7 +21,7 @@ use crate::encoding::{ errors::EncodingError, evm::{ approvals::protocol_approvals_manager::get_client, - utils::{biguint_to_u256, bytes_to_address, encode_input}, + utils::{biguint_to_u256, bytes_to_address, encode_input, get_runtime}, }, models::Chain, }; @@ -70,15 +70,7 @@ sol! { impl Permit2 { pub fn new(swapper_pk: String, chain: Chain) -> Result { - 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 (handle, runtime) = get_runtime()?; let client = block_in_place(|| handle.block_on(get_client()))?; let pk = B256::from_str(&swapper_pk).map_err(|_| { EncodingError::FatalError("Failed to convert swapper private key to B256".to_string()) diff --git a/src/encoding/evm/approvals/protocol_approvals_manager.rs b/src/encoding/evm/approvals/protocol_approvals_manager.rs index 9af2355..d239f73 100644 --- a/src/encoding/evm/approvals/protocol_approvals_manager.rs +++ b/src/encoding/evm/approvals/protocol_approvals_manager.rs @@ -8,21 +8,32 @@ use alloy::{ use alloy_primitives::{Address, Bytes, TxKind, U256}; use alloy_sol_types::SolValue; use dotenv::dotenv; -use tokio::runtime::Runtime; +use tokio::{ + runtime::{Handle, Runtime}, + task::block_in_place, +}; -use crate::encoding::{errors::EncodingError, evm::utils::encode_input}; +use crate::encoding::{ + errors::EncodingError, + evm::utils::{encode_input, get_runtime}, +}; /// A manager for checking if an approval is needed for interacting with a certain spender. pub struct ProtocolApprovalsManager { client: Arc>, - runtime: Runtime, + 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>, } impl ProtocolApprovalsManager { pub fn new() -> Result { - let runtime = Runtime::new() - .map_err(|_| EncodingError::FatalError("Failed to create runtime".to_string()))?; - let client = runtime.block_on(get_client())?; - Ok(Self { client, runtime }) + let (handle, runtime) = get_runtime()?; + let client = block_in_place(|| handle.block_on(get_client()))?; + Ok(Self { client, runtime_handle: handle, runtime }) } /// Checks the current allowance for the given token, owner, and spender, and returns true @@ -41,9 +52,10 @@ impl ProtocolApprovalsManager { ..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: U256 = U256::abi_decode(&response, true).map_err(|_| { diff --git a/src/encoding/evm/strategy_encoder/strategy_encoders.rs b/src/encoding/evm/strategy_encoder/strategy_encoders.rs index 4a75885..303b186 100644 --- a/src/encoding/evm/strategy_encoder/strategy_encoders.rs +++ b/src/encoding/evm/strategy_encoder/strategy_encoders.rs @@ -469,13 +469,13 @@ mod tests { let pool_fee_usdc_eth = Bytes::from(BigInt::from(3000).to_signed_bytes_be()); let tick_spacing_usdc_eth = Bytes::from(BigInt::from(60).to_signed_bytes_be()); let mut static_attributes_usdc_eth: HashMap = HashMap::new(); - static_attributes_usdc_eth.insert("fee".into(), pool_fee_usdc_eth); + static_attributes_usdc_eth.insert("key_lp_fee".into(), pool_fee_usdc_eth); static_attributes_usdc_eth.insert("tick_spacing".into(), tick_spacing_usdc_eth); let pool_fee_eth_pepe = Bytes::from(BigInt::from(25000).to_signed_bytes_be()); let tick_spacing_eth_pepe = Bytes::from(BigInt::from(500).to_signed_bytes_be()); let mut static_attributes_eth_pepe: HashMap = HashMap::new(); - static_attributes_eth_pepe.insert("fee".into(), pool_fee_eth_pepe); + static_attributes_eth_pepe.insert("key_lp_fee".into(), pool_fee_eth_pepe); static_attributes_eth_pepe.insert("tick_spacing".into(), tick_spacing_eth_pepe); let swap_usdc_eth = Swap { @@ -525,7 +525,7 @@ mod tests { let hex_protocol_data = encode(&protocol_data); assert_eq!( executor_address, - Bytes::from_str("0xF62849F9A0B5Bf2913b396098F7c7019b51A820a").unwrap() + Bytes::from_str("0x90BE4620436354c9DfA58614B3Bdd5a80FBfAF31").unwrap() ); assert_eq!( hex_protocol_data, @@ -537,7 +537,7 @@ mod tests { // zero for one "00", // executor address - "f62849f9a0b5bf2913b396098f7c7019b51a820a", + "90be4620436354c9dfa58614b3bdd5a80fbfaf31", // first pool intermediary token (ETH) "0000000000000000000000000000000000000000", // fee @@ -891,13 +891,13 @@ mod tests { let pool_fee_usdc_eth = Bytes::from(BigInt::from(3000).to_signed_bytes_be()); let tick_spacing_usdc_eth = Bytes::from(BigInt::from(60).to_signed_bytes_be()); let mut static_attributes_usdc_eth: HashMap = HashMap::new(); - static_attributes_usdc_eth.insert("fee".into(), pool_fee_usdc_eth); + static_attributes_usdc_eth.insert("key_lp_fee".into(), pool_fee_usdc_eth); static_attributes_usdc_eth.insert("tick_spacing".into(), tick_spacing_usdc_eth); let pool_fee_eth_pepe = Bytes::from(BigInt::from(25000).to_signed_bytes_be()); let tick_spacing_eth_pepe = Bytes::from(BigInt::from(500).to_signed_bytes_be()); let mut static_attributes_eth_pepe: HashMap = HashMap::new(); - static_attributes_eth_pepe.insert("fee".into(), pool_fee_eth_pepe); + static_attributes_eth_pepe.insert("key_lp_fee".into(), pool_fee_eth_pepe); static_attributes_eth_pepe.insert("tick_spacing".into(), tick_spacing_eth_pepe); let swap_usdc_eth = Swap { @@ -990,12 +990,12 @@ mod tests { "01", // token out index "000000", // split // Swap data header - "f62849f9a0b5bf2913b396098f7c7019b51a820a", // executor address + "90be4620436354c9dfa58614b3bdd5a80fbfaf31", // executor address // Protocol data "a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", // group token in "6982508145454ce325ddbe47a25d4ec3d2311933", // group token in "00", // zero2one - "f62849f9a0b5bf2913b396098f7c7019b51a820a", // executor address + "90be4620436354c9dfa58614b3bdd5a80fbfaf31", // executor address // First pool params "0000000000000000000000000000000000000000", // intermediary token (ETH) "000bb8", // fee @@ -1110,7 +1110,7 @@ mod tests { let pool_fee_eth_pepe = Bytes::from(BigInt::from(25000).to_signed_bytes_be()); let tick_spacing_eth_pepe = Bytes::from(BigInt::from(500).to_signed_bytes_be()); let mut static_attributes_eth_pepe: HashMap = HashMap::new(); - static_attributes_eth_pepe.insert("fee".into(), pool_fee_eth_pepe); + static_attributes_eth_pepe.insert("key_lp_fee".into(), pool_fee_eth_pepe); static_attributes_eth_pepe.insert("tick_spacing".into(), tick_spacing_eth_pepe); let swap_eth_pepe = Swap { @@ -1173,7 +1173,7 @@ mod tests { let pool_fee_usdc_eth = Bytes::from(BigInt::from(3000).to_signed_bytes_be()); let tick_spacing_usdc_eth = Bytes::from(BigInt::from(60).to_signed_bytes_be()); let mut static_attributes_usdc_eth: HashMap = HashMap::new(); - static_attributes_usdc_eth.insert("fee".into(), pool_fee_usdc_eth); + static_attributes_usdc_eth.insert("key_lp_fee".into(), pool_fee_usdc_eth); static_attributes_usdc_eth.insert("tick_spacing".into(), tick_spacing_usdc_eth); let swap_usdc_eth = Swap { diff --git a/src/encoding/evm/swap_encoder/swap_encoders.rs b/src/encoding/evm/swap_encoder/swap_encoders.rs index f4b43d3..473b250 100644 --- a/src/encoding/evm/swap_encoder/swap_encoders.rs +++ b/src/encoding/evm/swap_encoder/swap_encoders.rs @@ -152,7 +152,7 @@ impl SwapEncoder for UniswapV4SwapEncoder { swap: Swap, encoding_context: EncodingContext, ) -> Result, EncodingError> { - let fee = get_static_attribute(&swap, "fee")?; + let fee = get_static_attribute(&swap, "key_lp_fee")?; let pool_fee_u24 = pad_to_fixed_size::<3>(&fee) .map_err(|_| EncodingError::FatalError("Failed to pad fee bytes".to_string()))?; @@ -417,7 +417,7 @@ mod tests { let token_out = Bytes::from("0xdAC17F958D2ee523a2206206994597C13D831ec7"); // USDT let mut static_attributes: HashMap = HashMap::new(); - static_attributes.insert("fee".into(), Bytes::from(fee.to_signed_bytes_be())); + static_attributes.insert("key_lp_fee".into(), Bytes::from(fee.to_signed_bytes_be())); static_attributes .insert("tick_spacing".into(), Bytes::from(tick_spacing.to_signed_bytes_be())); @@ -483,7 +483,7 @@ mod tests { let token_out = Bytes::from("0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599"); // WBTC let mut static_attributes: HashMap = HashMap::new(); - static_attributes.insert("fee".into(), Bytes::from(fee.to_signed_bytes_be())); + static_attributes.insert("key_lp_fee".into(), Bytes::from(fee.to_signed_bytes_be())); static_attributes .insert("tick_spacing".into(), Bytes::from(tick_spacing.to_signed_bytes_be())); @@ -553,7 +553,7 @@ mod tests { let mut usde_usdt_static_attributes: HashMap = HashMap::new(); usde_usdt_static_attributes - .insert("fee".into(), Bytes::from(usde_usdt_fee.to_signed_bytes_be())); + .insert("key_lp_fee".into(), Bytes::from(usde_usdt_fee.to_signed_bytes_be())); usde_usdt_static_attributes.insert( "tick_spacing".into(), Bytes::from(usde_usdt_tick_spacing.to_signed_bytes_be()), @@ -571,7 +571,7 @@ mod tests { let mut usdt_wbtc_static_attributes: HashMap = HashMap::new(); usdt_wbtc_static_attributes - .insert("fee".into(), Bytes::from(usdt_wbtc_fee.to_signed_bytes_be())); + .insert("key_lp_fee".into(), Bytes::from(usdt_wbtc_fee.to_signed_bytes_be())); usdt_wbtc_static_attributes.insert( "tick_spacing".into(), Bytes::from(usdt_wbtc_tick_spacing.to_signed_bytes_be()), diff --git a/src/encoding/evm/tycho_encoder.rs b/src/encoding/evm/tycho_encoder.rs index 48991d6..0937fe9 100644 --- a/src/encoding/evm/tycho_encoder.rs +++ b/src/encoding/evm/tycho_encoder.rs @@ -109,9 +109,10 @@ impl TychoEncoder for EVMTychoEncoder { .strategy_encoder .encode_strategy(solution.clone())?; - let value = match solution.native_action.as_ref() { - Some(NativeAction::Wrap) => solution.given_amount.clone(), - _ => BigUint::ZERO, + let value = if solution.given_token == self.native_address { + solution.given_amount.clone() + } else { + BigUint::ZERO }; transactions.push(Transaction { diff --git a/src/encoding/evm/utils.rs b/src/encoding/evm/utils.rs index a59abff..96e04ea 100644 --- a/src/encoding/evm/utils.rs +++ b/src/encoding/evm/utils.rs @@ -1,7 +1,8 @@ -use std::cmp::max; +use std::{cmp::max, sync::Arc}; use alloy_primitives::{aliases::U24, keccak256, Address, FixedBytes, Keccak256, U256, U8}; use num_bigint::BigUint; +use tokio::runtime::{Handle, Runtime}; use tycho_core::Bytes; use crate::encoding::{ @@ -120,3 +121,15 @@ pub fn get_static_attribute(swap: &Swap, attribute_name: &str) -> Result })? .to_vec()) } + +pub fn get_runtime() -> Result<(Handle, Option>), EncodingError> { + match Handle::try_current() { + Ok(h) => Ok((h, None)), + Err(_) => { + let rt = Arc::new(Runtime::new().map_err(|_| { + EncodingError::FatalError("Failed to create a new tokio runtime".to_string()) + })?); + Ok((rt.handle().clone(), Some(rt))) + } + } +}