fix: Move encode_input back into encoding_utils.rs

We don't want the user to be able to use it outside the crate

Took 16 minutes
This commit is contained in:
Diana Carvalho
2025-05-27 12:12:47 +01:00
parent 73c1a2db6a
commit 92d36b9f48
4 changed files with 33 additions and 29 deletions

View File

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

View File

@@ -14,7 +14,10 @@ use tokio::{
use crate::encoding::{
errors::EncodingError,
evm::utils::{encode_input, get_client, get_runtime},
evm::{
encoding_utils::encode_input,
utils::{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;
use alloy_primitives::{Address, Keccak256};
use alloy_sol_types::{eip712_domain, SolStruct, SolValue};
use num_bigint::BigUint;
use tycho_common::Bytes;
@@ -13,7 +13,6 @@ use crate::encoding::{
errors::EncodingError,
evm::{
approvals::permit2::PermitSingle,
utils,
utils::{biguint_to_u256, bytes_to_address},
},
models,
@@ -219,8 +218,7 @@ pub fn encode_tycho_router_call(
Err(EncodingError::FatalError("Invalid function signature for Tycho router".to_string()))?
};
let contract_interaction =
utils::encode_input(&encoded_solution.function_signature, method_calldata);
let contract_interaction = encode_input(&encoded_solution.function_signature, method_calldata);
let value = if solution.given_token == native_address {
solution.given_amount.clone()
} else {
@@ -258,3 +256,25 @@ pub fn sign_permit(
EncodingError::FatalError(format!("Failed to sign permit2 approval with error: {e}"))
})
}
/// 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
}

View File

@@ -9,7 +9,7 @@ use alloy::{
providers::{ProviderBuilder, RootProvider},
transports::BoxTransport,
};
use alloy_primitives::{aliases::U24, Address, Keccak256, U256, U8};
use alloy_primitives::{aliases::U24, Address, U256, U8};
use alloy_sol_types::SolValue;
use num_bigint::BigUint;
use once_cell::sync::Lazy;
@@ -159,25 +159,3 @@ 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
}