Skip to main content
Version: 11.x

Class: CairoTypeOption

Defined in: src/utils/cairoDataTypes/cairoTypeOption.ts:30

A Cairo core::option::Option::<T> : either a value, or nothing.

On the wire it is an enum with two branches — 0 then the value for Some, 1 alone for None. Which branch is meant cannot be read off the value, since Some(0) and None would look alike, so building one from raw data asks for the variant.

This is the internal shape. What a caller writes and reads back is CairoOption, and handing one of those here says the variant by itself.

Example​

const type = 'core::option::Option::<core::integer::u8>';
new CairoTypeOption(7, type, cairoTypeStrategy, CairoOptionVariant.Some).toApiRequest();
// ["0", "7"]
new CairoTypeOption(undefined, type, cairoTypeStrategy, CairoOptionVariant.None).toApiRequest();
// ["1"]

Constructors​

Constructor​

new CairoTypeOption(content, optionCairoType, parsingStrategy, variant?, subType?): CairoTypeOption

Defined in: src/utils/cairoDataTypes/cairoTypeOption.ts:126

Build an option, from a value a caller passed or from the felts of a response.

A CairoOption says its own branch, so variant is not given with one — nor with the response iterator, which reads the branch off the wire, nor with a CairoTypeOption, which is copied as it is. Anywhere else it is required.

For the same reason, a CairoOption that holds a CairoTypeOption is refused: once it is unwrapped, the variant comes with the CairoTypeOption. Accepted, it would lose one level: Some(Some(1)) would be sent as Some(None). Write a nested value with CairoOption only, or build one CairoTypeOption for the whole type.

subType is what makes an option of options work. Handed a CairoOption, this constructor unwraps it and calls itself with what was inside, which for a nested option is another CairoOption; the flag stops that second call from unwrapping again and losing a level, and sends it to the strategy instead, where the inner type is what gets built.

Parameters​

content​

unknown

the value, a CairoOption, an instance already built, or the response iterator

optionCairoType​

string

the abi type, core::option::Option::<T>

parsingStrategy​

AllowArray<CairoTypeStrategy>

how to build the value

variant?​

number

which branch, when the content does not say. Must be omitted when content is the response iterator or a CairoTypeOption.

subType?​

boolean = false

true when called from the unwrapping of a nested CairoOption, which is the only caller that should set it

Returns​

CairoTypeOption

Throws​

when the type is not an option, or when the variant is missing, absurd, contradicts the content, or is given with a CairoTypeOption

Example​

const type = 'core::option::Option::<core::integer::u8>';
new CairoTypeOption(7, type, cairoTypeStrategy, CairoOptionVariant.Some).toApiRequest();
// ["0", "7"]
new CairoTypeOption(new CairoOption(CairoOptionVariant.Some, 7), type, cairoTypeStrategy).toApiRequest();
// ["0", "7"] the CairoOption says the branch
new CairoTypeOption(['0x0', '0x20'].values(), type, cairoTypeStrategy).toApiRequest();
// ["0", "32"] read off a response

Properties​

dynamicSelector​

static dynamicSelector: "CairoTypeOption"

Defined in: src/utils/cairoDataTypes/cairoTypeOption.ts:39

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

Example​

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

dynamicSelector​

readonly dynamicSelector: "CairoTypeOption" = CairoTypeOption.dynamicSelector

Defined in: src/utils/cairoDataTypes/cairoTypeOption.ts:51

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

Example​

const type = 'core::option::Option::<core::integer::u8>';
const result = new CairoTypeOption(7, type, cairoTypeStrategy, CairoOptionVariant.Some)
.dynamicSelector;
// result = "CairoTypeOption"

content​

readonly content: CairoType | undefined

Defined in: src/utils/cairoDataTypes/cairoTypeOption.ts:63

The value this option carries, or nothing at all when it is None.

Example​

const type = 'core::option::Option::<core::integer::u8>';
const result = new CairoTypeOption(undefined, type, cairoTypeStrategy, CairoOptionVariant.None)
.content;
// result = undefined

optionCairoType​

readonly optionCairoType: string

Defined in: src/utils/cairoDataTypes/cairoTypeOption.ts:75

The abi type this option was built for.

Example​

const type = 'core::option::Option::<core::integer::u8>';
const result = new CairoTypeOption(7, type, cairoTypeStrategy, CairoOptionVariant.Some)
.optionCairoType;
// result = "core::option::Option::<core::integer::u8>"

isVariantSome​

readonly isVariantSome: boolean

Defined in: src/utils/cairoDataTypes/cairoTypeOption.ts:87

Which of the two branches this option is on.

Example​

const type = 'core::option::Option::<core::integer::u8>';
const result = new CairoTypeOption(7, type, cairoTypeStrategy, CairoOptionVariant.Some)
.isVariantSome;
// result = true

Methods​

getVariantSomeType()​

static getVariantSomeType(type): string

Defined in: src/utils/cairoDataTypes/cairoTypeOption.ts:277

The type a Some carries, read out of the option type.

Parameters​

type​

string

the abi type to read

Returns​

string

the type between the angle brackets

Throws​

when there is no type between brackets

Example​

const result = CairoTypeOption.getVariantSomeType('core::option::Option::<core::integer::u8>');
// result = "core::integer::u8"

validate()​

static validate(_input, type, variant): void

Defined in: src/utils/cairoDataTypes/cairoTypeOption.ts:302

Throw unless this is an option type and this variant is one of its two branches.

The value is not read here : what it is worth is the business of the type the option carries, and is checked when that value is built.

Parameters​

_input​

unknown

the value, which this does not read

type​

string

the abi type it is meant for

variant​

VariantType | undefined

the branch, when one was given

Returns​

void

Throws​

when the type is not an option, or the variant is neither 0 nor 1

Example​

const type = 'core::option::Option::<core::integer::u16>';
CairoTypeOption.validate(200, type, CairoOptionVariant.Some); // passes
CairoTypeOption.validate(200, type, 3);
// throws Error("In Cairo option, only 0 or 1 variants are authorized.")

is()​

static is(input, type, variant): boolean

Defined in: src/utils/cairoDataTypes/cairoTypeOption.ts:333

Can this be read as an option of this type, on this branch?

The non-throwing form of CairoTypeOption.validate.

Parameters​

input​

unknown

the value to test

type​

string

the abi type it is meant for

variant​

VariantType

the branch

Returns​

boolean

true when the type and the variant both fit

Example​

const type = 'core::option::Option::<core::integer::u16>';
const result = CairoTypeOption.is(200, type, CairoOptionVariant.Some);
// result = true
const result2 = CairoTypeOption.is(200, type, 3);
// result2 = false

isAbiType()​

static isAbiType(type): boolean

Defined in: src/utils/cairoDataTypes/cairoTypeOption.ts:354

Is this abi type an option?

Parameters​

type​

string

the abi type to test

Returns​

boolean

true for core::option::Option::<T>

Example​

const result = CairoTypeOption.isAbiType('core::option::Option::<core::integer::u16>');
// result = true
const result2 = CairoTypeOption.isAbiType('core::integer::u16');
// result2 = false

toApiRequest()​

toApiRequest(): string[]

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

Serialize to the felts a contract call carries.

The branch comes first, and the value only follows it on Some — a None is one felt, and that is the whole of it.

Returns​

string[]

the branch then the value, flagged as compiled

Example​

const type = 'core::option::Option::<core::integer::u8>';
new CairoTypeOption(8, type, cairoTypeStrategy, CairoOptionVariant.Some).toApiRequest();
// ["0", "8"]
new CairoTypeOption(undefined, type, cairoTypeStrategy, CairoOptionVariant.None).toApiRequest();
// ["1"]

decompose()​

decompose(strategyDecompose): CairoOption<any>

Defined in: src/utils/cairoDataTypes/cairoTypeOption.ts:425

Read the option back as the CairoOption a caller reads.

Parameters​

strategyDecompose​

AllowArray<CairoTypeStrategy>

how to read the value back

Returns​

CairoOption<any>

the option, on the branch it was built with

Throws​

when no strategy can read the value back

Example​

const type = 'core::option::Option::<core::integer::u8>';
const option = new CairoTypeOption(3, type, cairoTypeStrategy, CairoOptionVariant.Some);
const result = option.decompose(cairoTypeStrategy).unwrap();
// result = 3n