string_to_bytes32

Function string_to_bytes32 

Source
pub fn string_to_bytes32(pool_id: &str) -> Result<[u8; 32], SimulationError>
Expand description

Converts a hexadecimal string into a fixed-size 32-byte array.

This function takes a string slice (e.g., a pool ID) that may or may not have a 0x prefix. It decodes the hex string into bytes, ensuring it does not exceed 32 bytes in length. If the string is valid and fits within 32 bytes, the bytes are copied into a [u8; 32] array, with right zero-padding for unused bytes.

§Arguments

  • pool_id - A string slice representing a hexadecimal pool ID. It can optionally start with the 0x prefix.

§Returns

  • Ok([u8; 32]) - On success, returns a 32-byte array with the decoded bytes. If the input is shorter than 32 bytes, the rest of the array is right padded with zeros.
  • Err(SimulationError) - Returns an error if:
    • The input string is not a valid hexadecimal string.
    • The decoded bytes exceed 32 bytes in length.

§Example

use tycho_simulation::evm::protocol::vm::utils::string_to_bytes32;

let pool_id = "0x1234abcd";
match string_to_bytes32(pool_id) {
    Ok(bytes32) => println!("Bytes32: {:?}", bytes32),
    Err(e) => eprintln!("Error: {}", e),
}