Skip to content

Vectors

In Sway, a Vector is a dynamic-sized collection of elements of the same type. Vectors can hold arbitrary types, including non-primitive types.

Working with Vectors in the SDK

A basic Vector in Sway is similar to a TypeScript Array:

ts
// Sway Vec<u8>
const basicU8Vector = [1, 2, 3];
See code in context

Consider the following example of a EmployeeData struct in Sway:

rust
pub struct EmployeeData {
    name: str[8],
    age: u8,
    salary: u64,
    idHash: b256,
    ratings: [u8; 3],
    isActive: bool,
}
See code in context

Now, let's look at the following contract method. It receives a Vector of the Transaction struct type as a parameter and returns the last Transaction entry from the Vector:

ts
fn echo_last_employee_data(employee_data_vector: Vec<EmployeeData>) -> EmployeeData {
    employee_data_vector.get(employee_data_vector.len() - 1).unwrap()
}
See code in context

The code snippet below demonstrates how to call this Sway contract method, which accepts a Vec<Transaction>:

ts
import { getRandomB256 } from 'fuels';
import { EmployeeDataInput } from '../path/to/typegen/contracts/EchoU64Array';

const employees: EmployeeDataInput[] = [
  {
    name: 'John Doe',
    age: 30,
    salary: 8000,
    idHash: getRandomB256(),
    ratings: [1, 2, 3],
    isActive: true,
  },
  {
    name: 'Everyman',
    age: 31,
    salary: 9000,
    idHash: getRandomB256(),
    ratings: [5, 6, 7],
    isActive: true,
  },
];
const { value } = await contract.functions.echo_last_employee_data(employees).simulate();
See code in context

Converting Bytecode to Vectors

Some functions require you to pass in bytecode to the function. The type of the bytecode parameter is usually Vec<u8>, here's an example of how to pass bytecode to a function:

ts
fn compute_bytecode_root(bytecode_input: Vec<u8>) -> b256 {
    //simply return a fixed b256 value created from a hexadecimal string from testing purposes
    return 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20;
}
See code in context

To pass bytecode to this function, you can make use of the arrayify function to convert the bytecode file contents into a UInt8Array, the TS compatible type for Sway's Vec<u8> type and pass it the function like so:

ts
import { arrayify } from 'fuels';
import { BytecodeInputFactory } from '../path/to/typegen';

const bytecodeAsVecU8 = Array.from(arrayify(BytecodeInputFactory.bytecode));

const { waitForResult } = await bytecodeContract.functions
  .compute_bytecode_root(bytecodeAsVecU8)
  .call();

const { value: bytecodeRoot } = await waitForResult();
See code in context