fix: Make functions in encoding_utils.rs private

They shouldn't be used outside the crate by the users. They should reimplement this functions themselves.
Move encode_input to utils.rs

Took 8 minutes
This commit is contained in:
Diana Carvalho
2025-05-26 17:16:35 +01:00
parent 4f85e3e88e
commit 5ddd2a9cd7
5 changed files with 29 additions and 34 deletions

View File

@@ -17,10 +17,7 @@ use tycho_common::Bytes;
use crate::encoding::{
errors::EncodingError,
evm::{
encoding_utils::encode_input,
utils::{biguint_to_u256, bytes_to_address, get_client, get_runtime},
},
evm::utils::{biguint_to_u256, bytes_to_address, encode_input, get_client, get_runtime},
models,
};

View File

@@ -14,10 +14,7 @@ use tokio::{
use crate::encoding::{
errors::EncodingError,
evm::{
encoding_utils::encode_input,
utils::{get_client, get_runtime},
},
evm::utils::{encode_input, get_client, get_runtime},
};
/// A manager for checking if an approval is needed for interacting with a certain spender.

View File

@@ -4,7 +4,7 @@ use alloy::{
primitives::U256,
signers::{local::PrivateKeySigner, Signature, SignerSync},
};
use alloy_primitives::{Address, Keccak256};
use alloy_primitives::Address;
use alloy_sol_types::{eip712_domain, SolStruct, SolValue};
use num_bigint::BigUint;
use tycho_common::Bytes;
@@ -13,34 +13,13 @@ use crate::encoding::{
errors::EncodingError,
evm::{
approvals::permit2::PermitSingle,
utils,
utils::{biguint_to_u256, bytes_to_address},
},
models,
models::{EncodedSolution, NativeAction, Solution, Transaction, UserTransferType},
};
/// Encodes the input data for a function call to the given function selector.
pub fn encode_input(selector: &str, mut encoded_args: Vec<u8>) -> Vec<u8> {
let mut hasher = Keccak256::new();
hasher.update(selector.as_bytes());
let selector_bytes = &hasher.finalize()[..4];
let mut call_data = selector_bytes.to_vec();
// Remove extra prefix if present (32 bytes for dynamic data)
// Alloy encoding is including a prefix for dynamic data indicating the offset or length
// but at this point we don't want that
if encoded_args.len() > 32 &&
encoded_args[..32] ==
[0u8; 31]
.into_iter()
.chain([32].to_vec())
.collect::<Vec<u8>>()
{
encoded_args = encoded_args[32..].to_vec();
}
call_data.extend(encoded_args);
call_data
}
/// Encodes a transaction for the Tycho Router using one of its supported swap methods.
///
/// # Overview
@@ -240,7 +219,7 @@ pub fn encode_tycho_router_call(
Err(EncodingError::FatalError("Invalid selector for Tycho router".to_string()))?
};
let contract_interaction = encode_input(&encoded_solution.selector, method_calldata);
let contract_interaction = utils::encode_input(&encoded_solution.selector, method_calldata);
let value = if solution.given_token == native_address {
solution.given_amount.clone()
} else {

View File

@@ -1,7 +1,7 @@
pub mod approvals;
mod constants;
pub mod encoder_builders;
pub mod encoding_utils;
mod encoding_utils;
mod group_swaps;
pub mod strategy_encoder;
mod swap_encoder;

View File

@@ -9,7 +9,7 @@ use alloy::{
providers::{ProviderBuilder, RootProvider},
transports::BoxTransport,
};
use alloy_primitives::{aliases::U24, Address, U256, U8};
use alloy_primitives::{aliases::U24, Address, Keccak256, U256, U8};
use alloy_sol_types::SolValue;
use num_bigint::BigUint;
use once_cell::sync::Lazy;
@@ -159,3 +159,25 @@ pub fn write_calldata_to_file(test_identifier: &str, hex_calldata: &str) {
writeln!(file, "{line}").expect("Failed to write calldata");
}
}
/// Encodes the input data for a function call to the given function selector.
pub fn encode_input(selector: &str, mut encoded_args: Vec<u8>) -> Vec<u8> {
let mut hasher = Keccak256::new();
hasher.update(selector.as_bytes());
let selector_bytes = &hasher.finalize()[..4];
let mut call_data = selector_bytes.to_vec();
// Remove extra prefix if present (32 bytes for dynamic data)
// Alloy encoding is including a prefix for dynamic data indicating the offset or length
// but at this point we don't want that
if encoded_args.len() > 32 &&
encoded_args[..32] ==
[0u8; 31]
.into_iter()
.chain([32].to_vec())
.collect::<Vec<u8>>()
{
encoded_args = encoded_args[32..].to_vec();
}
call_data.extend(encoded_args);
call_data
}