use alloy::primitives::U256; #[derive(thiserror::Error, Debug)] #[error("Expected {expected} bytes but got {actual} bytes")] pub struct LengthError { pub expected: usize, pub actual: usize, } pub fn u256_to_bytes(value: U256) -> [u8; 32] { value.to_le_bytes() } pub fn bytes_to_u256(bytes: &[u8]) -> Option { let bytes: [u8; 32] = bytes.try_into().ok()?; Some(U256::from_le_bytes(bytes)) } pub fn try_bytes_to_u256(bytes: &[u8]) -> diesel::result::QueryResult { let bytes: [u8; 32] = bytes.try_into().map_err(|_| { diesel::result::Error::DeserializationError(Box::new(LengthError { expected: 32, actual: bytes.len(), })) })?; Ok(U256::from_le_bytes(bytes)) }