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:
@@ -17,10 +17,7 @@ use tycho_common::Bytes;
|
|||||||
|
|
||||||
use crate::encoding::{
|
use crate::encoding::{
|
||||||
errors::EncodingError,
|
errors::EncodingError,
|
||||||
evm::{
|
evm::utils::{biguint_to_u256, bytes_to_address, encode_input, get_client, get_runtime},
|
||||||
encoding_utils::encode_input,
|
|
||||||
utils::{biguint_to_u256, bytes_to_address, get_client, get_runtime},
|
|
||||||
},
|
|
||||||
models,
|
models,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -14,10 +14,7 @@ use tokio::{
|
|||||||
|
|
||||||
use crate::encoding::{
|
use crate::encoding::{
|
||||||
errors::EncodingError,
|
errors::EncodingError,
|
||||||
evm::{
|
evm::utils::{encode_input, get_client, get_runtime},
|
||||||
encoding_utils::encode_input,
|
|
||||||
utils::{get_client, get_runtime},
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/// A manager for checking if an approval is needed for interacting with a certain spender.
|
/// A manager for checking if an approval is needed for interacting with a certain spender.
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ use alloy::{
|
|||||||
primitives::U256,
|
primitives::U256,
|
||||||
signers::{local::PrivateKeySigner, Signature, SignerSync},
|
signers::{local::PrivateKeySigner, Signature, SignerSync},
|
||||||
};
|
};
|
||||||
use alloy_primitives::{Address, Keccak256};
|
use alloy_primitives::Address;
|
||||||
use alloy_sol_types::{eip712_domain, SolStruct, SolValue};
|
use alloy_sol_types::{eip712_domain, SolStruct, SolValue};
|
||||||
use num_bigint::BigUint;
|
use num_bigint::BigUint;
|
||||||
use tycho_common::Bytes;
|
use tycho_common::Bytes;
|
||||||
@@ -13,34 +13,13 @@ use crate::encoding::{
|
|||||||
errors::EncodingError,
|
errors::EncodingError,
|
||||||
evm::{
|
evm::{
|
||||||
approvals::permit2::PermitSingle,
|
approvals::permit2::PermitSingle,
|
||||||
|
utils,
|
||||||
utils::{biguint_to_u256, bytes_to_address},
|
utils::{biguint_to_u256, bytes_to_address},
|
||||||
},
|
},
|
||||||
models,
|
models,
|
||||||
models::{EncodedSolution, NativeAction, Solution, Transaction, UserTransferType},
|
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.
|
/// Encodes a transaction for the Tycho Router using one of its supported swap methods.
|
||||||
///
|
///
|
||||||
/// # Overview
|
/// # Overview
|
||||||
@@ -240,7 +219,7 @@ pub fn encode_tycho_router_call(
|
|||||||
Err(EncodingError::FatalError("Invalid selector for Tycho router".to_string()))?
|
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 {
|
let value = if solution.given_token == native_address {
|
||||||
solution.given_amount.clone()
|
solution.given_amount.clone()
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
pub mod approvals;
|
pub mod approvals;
|
||||||
mod constants;
|
mod constants;
|
||||||
pub mod encoder_builders;
|
pub mod encoder_builders;
|
||||||
pub mod encoding_utils;
|
mod encoding_utils;
|
||||||
mod group_swaps;
|
mod group_swaps;
|
||||||
pub mod strategy_encoder;
|
pub mod strategy_encoder;
|
||||||
mod swap_encoder;
|
mod swap_encoder;
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ use alloy::{
|
|||||||
providers::{ProviderBuilder, RootProvider},
|
providers::{ProviderBuilder, RootProvider},
|
||||||
transports::BoxTransport,
|
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 alloy_sol_types::SolValue;
|
||||||
use num_bigint::BigUint;
|
use num_bigint::BigUint;
|
||||||
use once_cell::sync::Lazy;
|
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");
|
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
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user