xenith-layerzero 0.1.0

LayerZero v2 transport implementation for xenith
Documentation
/// Encode a LayerZero v2 TYPE_3 executor option that sets the `lzReceive` gas limit.
///
/// The encoding follows the LayerZero v2 OptionsBuilder spec:
/// `[0x00, 0x03] ++ abi_encode_packed(type: u16 = 1, gas: u256)`
///
/// # Example
///
/// ```
/// use xenith_layerzero::encode_executor_lz_receive_option;
/// let opts = encode_executor_lz_receive_option(200_000);
/// assert_eq!(&opts[0..2], &[0x00, 0x03]); // TYPE_3
/// assert_eq!(opts.len(), 36);
/// ```
pub fn encode_executor_lz_receive_option(gas: u64) -> Vec<u8> {
    let mut out = vec![0x00, 0x03]; // TYPE_3
    out.extend_from_slice(&1u16.to_be_bytes()); // OPTION_TYPE_LZRECEIVE = 1
                                                // gas as abi_encode_packed u256 (32 bytes big-endian)
    let mut gas_buf = [0u8; 32];
    gas_buf[24..32].copy_from_slice(&gas.to_be_bytes());
    out.extend_from_slice(&gas_buf);
    out
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_options_encoding() {
        let opts = encode_executor_lz_receive_option(200_000);
        assert_eq!(opts.len(), 36);
        assert_eq!(&opts[0..2], &[0x00, 0x03], "TYPE_3 prefix");
        assert_eq!(&opts[2..4], &[0x00, 0x01], "OPTION_TYPE_LZRECEIVE");
        // gas = 200_000 = 0x030D40; verify last 3 bytes of the u256
        assert_eq!(&opts[33..36], &[0x03, 0x0D, 0x40]);
        // bytes [4..33] are all zero (leading zeros of u256)
        assert!(opts[4..33].iter().all(|&b| b == 0));
    }
}