In Sway, an Array
is a fixed-size collection of elements of the same type, similar to a Tuple
. Arrays
can hold arbitrary types, including non-primitive types, with their size determined at compile time.
You can pass a TypeScript Array
into your contract method seamlessly just like you would pass an Array
to a TypeScript function.
The SDK handles the conversion from TypeScript to Sway in the background, allowing the expected data to be passed through the type regardless of the Array
type.
An Array
in Sway is simply a typed Array
, as demonstrated in the following example:
const numberArray: number[] = [1, 2, 3, 4, 5]; // in Sway: [u8; 5]
const boolArray: boolean[] = [true, false, true]; // in Sway: [bool; 3]
In Sway, Arrays
are fixed in size, so the storage size is determined at the time of program compilation, not during runtime.
Let's say you have a contract that takes an Array
of type u64
with a size length of 2 as a parameter and returns it:
fn echo_u64_array(u64_array: [u64; 2]) -> [u64; 2] {
u64_array
}
To execute the contract call using the SDK, you would do something like this:
const u64Array = [10000000, 20000000];
const { value } = await contract.functions.echo_u64_array(u64Array).simulate();
expect(new BN(value[0]).toNumber()).toEqual(u64Array[0]);
expect(new BN(value[1]).toNumber()).toEqual(u64Array[1]);
You can easily access and validate the Array
returned by the contract method, as demonstrated in the previous example.
As previously mentioned, Sway Arrays
have a predefined type and size, so you need to be careful when passing Arrays
as parameters to contract calls.
Passing an Array with an incorrect size, whether it has more or fewer elements than the specified length, will result in an error:
// will throw error because the array length is not 2
await contract.functions.echo_u64_array([10000000]).simulate();
Similarly, passing an Array
with an incorrect type will also result in an error:
// will throw error because the second element is not of type u64
await contract.functions.echo_u64_array([10000000, 'a']).simulate();
If your Array
size is unknown until runtime, consider using the Vectors type, which is more suitable for dynamic-sized collections.
Was this page helpful?