Skip to content

Bytes

A dynamic array of byte values can be represented using the Bytes type, which represents raw bytes.

ts
import type { Bytes } from 'fuels';

const bytes: Bytes = [40, 41, 42];

const { value } = await contract.functions.bytes_comparison(bytes).simulate();

expect(value).toBeTruthy();
See code in context

Using Bytes

The Bytes type can be integrated with your contract calls. Consider the following contract that can compare and return a Bytes:

ts
contract;

use std::bytes::Bytes;

abi BytesTest {
    fn echo_bytes(value: Bytes) -> Bytes;
    fn bytes_comparison(value: Bytes) -> bool;
}

impl BytesTest for Contract {
    fn echo_bytes(value: Bytes) -> Bytes {
        value
    }

    fn bytes_comparison(value: Bytes) -> bool {
        let mut bytes = Bytes::new();

        bytes.push(40u8);
        bytes.push(41u8);
        bytes.push(42u8);

        value == bytes
    }
}
See code in context

A Bytes array can be created using a native JavaScript array of numbers or Big Numbers, and sent to a Sway contract:

ts
import type { Bytes } from 'fuels';

const bytes: Bytes = [8, 42, 77];

const { value } = await contract.functions.echo_bytes(bytes).simulate();

expect(value).toStrictEqual(new Uint8Array(bytes));
See code in context