Skip to main content
Version: 11.x

Class: CairoFixedArray

Defined in: src/utils/cairoDataTypes/fixedArray.ts:23

A Cairo fixed array : a known number of items, all of the same type.

Its abi type is written [itemType; length], and both halves matter — the length is part of the type, so an array of the wrong size is refused rather than padded or cut. On the wire it carries no length of its own, exactly like a tuple : the type already says how many items follow, which is the whole difference with CairoArray.

Example​

const fArray = new CairoFixedArray([10, 20, 30], '[core::integer::u32; 3]', cairoTypeStrategy);
fArray.toApiRequest(); // ["10", "20", "30"] no length on the wire
fArray.decompose(cairoTypeStrategy); // [10n, 20n, 30n]

Constructors​

Constructor​

new CairoFixedArray(content, arrayType, parsingStrategy): CairoFixedArray

Defined in: src/utils/cairoDataTypes/fixedArray.ts:138

Build a fixed array, from values a caller passed or from the felts of a response.

Items are accepted raw, or already built and taken as they stand. An object is read by its values, which is the shape CairoFixedArray.compile produces. Another instance is adopted whole.

The strategy says how to build each item, and is required rather than defaulted : defaulting would mean importing the default strategy, which reads this very class to register itself, and the two modules would depend on each other. cairoTypeStrategy is what to pass for the language's own types.

Parameters​

content​

unknown

the items, as an array, an object, or the response iterator

arrayType​

string

the abi type, [itemType; length]

parsingStrategy​

AllowArray<CairoTypeStrategy>

how to build each item

Returns​

CairoFixedArray

Throws​

when the type is not a fixed array, when the item count does not match, or when the item type is one no strategy knows

Example​

const type = '[core::integer::u32; 2]';
new CairoFixedArray([10, 20], type, cairoTypeStrategy).toApiRequest();
// ["10", "20"]
new CairoFixedArray(['0xa', '0x14'].values(), type, cairoTypeStrategy).toApiRequest();
// ["10", "20"]
new CairoFixedArray([10], type, cairoTypeStrategy);
// throws Error("The ABI type [core::integer::u32; 2] is expecting 2 items. 1 items provided.")

Properties​

dynamicSelector​

static dynamicSelector: "CairoFixedArray"

Defined in: src/utils/cairoDataTypes/fixedArray.ts:32

The name this class is registered under in a strategy's dynamicSelectors.

Example​

const result = CairoFixedArray.dynamicSelector;
// result = "CairoFixedArray"

dynamicSelector​

readonly dynamicSelector: "CairoFixedArray" = CairoFixedArray.dynamicSelector

Defined in: src/utils/cairoDataTypes/fixedArray.ts:43

The selector on the instance, which is how a composite holding it knows what built it.

Example​

const fArray = new CairoFixedArray([10], '[core::integer::u32; 1]', cairoTypeStrategy);
const result = fArray.dynamicSelector;
// result = "CairoFixedArray"

content​

readonly content: CairoType[]

Defined in: src/utils/cairoDataTypes/fixedArray.ts:58

The items, each already built as the type the array declares.

Not the raw values handed in : like every other composite — CairoStruct, CairoArray, CairoTuple — what is kept is the typed form, which is what lets the whole tree serialize itself at once. CairoFixedArray.decompose reads them back.

Example​

const fArray = new CairoFixedArray([10, 20, 30], '[core::integer::u32; 3]', cairoTypeStrategy);
const result = fArray.content.length;
// result = 3

arrayType​

readonly arrayType: string

Defined in: src/utils/cairoDataTypes/fixedArray.ts:69

The abi type this array was built for.

Example​

const fArray = new CairoFixedArray([10], '[core::integer::u32; 1]', cairoTypeStrategy);
const result = fArray.arrayType;
// result = "[core::integer::u32; 1]"

Methods​

getFixedArraySize()​

static getFixedArraySize(type): number

Defined in: src/utils/cairoDataTypes/fixedArray.ts:237

Retrieves the array size from the given type string representing a Cairo fixed array.

Parameters​

type​

string

The Cairo fixed array type.

Returns​

number

The array size.

Example​

const result = CairoFixedArray.getFixedArraySize("[core::integer::u32; 8]");
// result = 8

getFixedArrayType()​

static getFixedArrayType(type): string

Defined in: src/utils/cairoDataTypes/fixedArray.ts:268

Retrieve the Cairo content type from a Cairo fixed array type.

Parameters​

type​

string

The type string.

Returns​

string

The fixed-array type.

Example​

const result = CairoFixedArray.getFixedArrayType("[core::integer::u32; 8]");
// result = "core::integer::u32"

compile()​

static compile(input): Object

Defined in: src/utils/cairoDataTypes/fixedArray.ts:300

Create an object from a Cairo fixed array. Be sure to have an array length conform to the ABI. To be used with CallData.compile().

Parameters​

input​

any[]

JS array representing a Cairo fixed array.

Returns​

Object

a specific struct representing a fixed Array.

Example​

const result = CairoFixedArray.compile([10,20,30]);
// result = { '0': 10, '1': 20, '2': 30 }

validate()​

static validate(input, type): void

Defined in: src/utils/cairoDataTypes/fixedArray.ts:341

Throw unless this input can be read as a fixed array of this type.

The item count is checked here, unlike a dynamic array's : a fixed array declares its length in its type, so a list of the wrong size is the caller's mistake.

Parameters​

input​

unknown

the items to check

type​

string

the abi type they are meant for

Returns​

void

Throws​

when the type is not a fixed array, when the input is neither an array nor an object, or when the item count does not match

Example​

CairoFixedArray.validate([1, 2, 3], '[core::integer::u8; 3]'); // passes
CairoFixedArray.validate([1, 2], '[core::integer::u8; 3]');
// throws Error("The ABI type [core::integer::u8; 3] is expecting 3 items. 2 items provided.")

is()​

static is(input, type): boolean

Defined in: src/utils/cairoDataTypes/fixedArray.ts:373

Can this input be read as a fixed array of this type?

The non-throwing form of CairoFixedArray.validate.

Parameters​

input​

unknown

the items to test

type​

string

the abi type they are meant for

Returns​

boolean

true when the shape and the count both fit

Example​

const result = CairoFixedArray.is([1, 2, 3], '[core::integer::u8; 3]');
// result = true
const result2 = CairoFixedArray.is([1, 2], '[core::integer::u8; 3]');
// result2 = false (one item short)

isAbiType()​

static isAbiType(type): boolean

Defined in: src/utils/cairoDataTypes/fixedArray.ts:394

Is this abi type a fixed array?

Parameters​

type​

string

the abi type to test

Returns​

boolean

true for [itemType; length], nested or not

Example​

const result = CairoFixedArray.isAbiType('[core::integer::u32; 8]');
// result = true
const result2 = CairoFixedArray.isAbiType('core::array::Array::<core::integer::u8>');
// result2 = false (a dynamic array carries its length on the wire)

isTypeFixedArray()​

static isTypeFixedArray(type): boolean

Defined in: src/utils/cairoDataTypes/fixedArray.ts:412

Checks if the given Cairo type is a fixed-array type. structure: [string; number]

The name every other Cairo type class uses for this is isAbiType; this one is kept because it is what the library has exposed all along.

Parameters​

type​

string

The type to check.

Returns​

boolean

true if the type is a fixed array type, false otherwise.

Example​

const result = CairoFixedArray.isTypeFixedArray("[core::integer::u32; 8]");
// result = true

getFixedArraySize()​

getFixedArraySize(): number

Defined in: src/utils/cairoDataTypes/fixedArray.ts:254

Retrieves the Cairo fixed array size from the CairoFixedArray instance.

Returns​

number

The fixed array size.

Example​

const fArray = new CairoFixedArray([10,20,30], "[core::integer::u32; 3]", cairoTypeStrategy);
const result = fArray.getFixedArraySize();
// result = 3

getFixedArrayType()​

getFixedArrayType(): string

Defined in: src/utils/cairoDataTypes/fixedArray.ts:284

Retrieve the Cairo content type of the Cairo fixed array.

Returns​

string

The fixed-array content type.

Example​

const fArray = new CairoFixedArray([10,20,30], "[core::integer::u32; 3]", cairoTypeStrategy);
const result = fArray.getFixedArrayType();
// result = "core::integer::u32"

compile()​

compile(): Object

Defined in: src/utils/cairoDataTypes/fixedArray.ts:321

Generate an object from the Cairo fixed array instance. To be used with CallData.compile().

The values are the built items rather than the raw ones, which changes nothing for the calldata : CallData.compile serializes anything that knows its own wire format.

Returns​

Object

a specific struct representing a fixed array.

Example​

const fArray = new CairoFixedArray([10,20,30], "[core::integer::u32; 3]", cairoTypeStrategy);
const result = CallData.compile(fArray.compile());
// result = ["10", "20", "30"]

toApiRequest()​

toApiRequest(): string[]

Defined in: src/utils/cairoDataTypes/fixedArray.ts:429

Serialize to the felts a contract call carries.

The items follow one another with nothing in front : the length is in the type, not on the wire — which is the whole difference with a dynamic array.

Returns​

string[]

the items' felts, in order, flagged as compiled

Example​

const fArray = new CairoFixedArray([10, 20, 30], '[core::integer::u32; 3]', cairoTypeStrategy);
const result = fArray.toApiRequest();
// result = ["10", "20", "30"]

decompose()​

decompose(strategyDecompose): any[]

Defined in: src/utils/cairoDataTypes/fixedArray.ts:445

Read the array back as the plain values a caller reads.

Parameters​

strategyDecompose​

AllowArray<CairoTypeStrategy>

how to read each item back

Returns​

any[]

the items' values, in order

Throws​

when no strategy can read an item back

Example​

const fArray = new CairoFixedArray([10, 20, 30], '[core::integer::u32; 3]', cairoTypeStrategy);
const result = fArray.decompose(cairoTypeStrategy);
// result = [10n, 20n, 30n]