feat: add util fns, change callback_selector to string, update first_swap check
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
use std::cmp::max;
|
||||
|
||||
use alloy_primitives::{aliases::U24, Address, Keccak256, U256, U8};
|
||||
use alloy_primitives::{aliases::U24, keccak256, Address, FixedBytes, Keccak256, U256, U8};
|
||||
use num_bigint::BigUint;
|
||||
use tycho_core::Bytes;
|
||||
|
||||
@@ -91,3 +91,17 @@ pub fn get_token_position(tokens: Vec<Bytes>, token: Bytes) -> Result<U8, Encodi
|
||||
);
|
||||
Ok(position)
|
||||
}
|
||||
|
||||
/// Pads a byte slice to a fixed size array with leading zeros.
|
||||
pub fn pad_to_fixed_size<const N: usize>(input: &[u8]) -> Result<[u8; N], EncodingError> {
|
||||
let mut padded = [0u8; N];
|
||||
let start = N - input.len();
|
||||
padded[start..].copy_from_slice(input);
|
||||
Ok(padded)
|
||||
}
|
||||
|
||||
/// Encodes a function selector to a fixed size array of 4 bytes.
|
||||
pub fn encode_function_selector(selector: &str) -> FixedBytes<4> {
|
||||
let hash = keccak256(selector.as_bytes());
|
||||
FixedBytes::<4>::from([hash[0], hash[1], hash[2], hash[3]])
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user