Skip to main content
Version: 11.x

Class: CairoNonZero

Defined in: src/utils/cairoDataTypes/nonZero.ts:50

A Cairo core::zeroable::NonZero::<T> : a value of type T that is guaranteed not to be zero.

It adds nothing to the wire — a NonZero<u8> is the felt a u8 is. Its whole job is the check it performs while being built, which is why a contract can then divide by it without a guard.

Only the types Cairo allows are accepted : the unsigned integers up to u256, and felt252.

Example​

const nonZero = new CairoNonZero(2, 'core::zeroable::NonZero::<core::integer::u8>', cairoTypeStrategy);
nonZero.toApiRequest(); // ["2"] no wrapper felt
nonZero.decompose(cairoTypeStrategy); // 2n

Constructors​

Constructor​

new CairoNonZero(content, type, parsingStrategy): CairoNonZero

Defined in: src/utils/cairoDataTypes/nonZero.ts:112

Build a non-zero value, from what a caller passed or from the felts of a response.

A value given raw is built as the wrapped type and then checked against zero. A value already built is taken as it stands, and a response is read as the wrapped type would be — in neither case is zero rejected, since what a node answers is not the caller's mistake to catch.

Parameters​

content​

unknown

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

type​

string

the abi type, core::zeroable::NonZero::<T>

parsingStrategy​

AllowArray<CairoTypeStrategy>

how to build the value

Returns​

CairoNonZero

Throws​

when the type is not a NonZero, when the wrapped type is one Cairo does not allow there, or when a value given raw is zero

Example​

const type = 'core::zeroable::NonZero::<core::integer::u8>';
new CairoNonZero(2, type, cairoTypeStrategy).toApiRequest(); // ["2"]
new CairoNonZero(['0x9'].values(), type, cairoTypeStrategy).toApiRequest(); // ["9"]

Properties​

dynamicSelector​

static dynamicSelector: "CairoNonZero"

Defined in: src/utils/cairoDataTypes/nonZero.ts:59

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

Example​

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

dynamicSelector​

readonly dynamicSelector: "CairoNonZero" = CairoNonZero.dynamicSelector

Defined in: src/utils/cairoDataTypes/nonZero.ts:70

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

Example​

const type = 'core::zeroable::NonZero::<core::integer::u8>';
const result = new CairoNonZero(2, type, cairoTypeStrategy).dynamicSelector;
// result = "CairoNonZero"

content​

readonly content: CairoType

Defined in: src/utils/cairoDataTypes/nonZero.ts:81

The value, built as the type the NonZero wraps.

Example​

const type = 'core::zeroable::NonZero::<core::integer::u8>';
const result = new CairoNonZero(2, type, cairoTypeStrategy).content.toApiRequest();
// result = ["2"]

contentType​

readonly contentType: string

Defined in: src/utils/cairoDataTypes/nonZero.ts:92

The abi type this value was built for, brackets included.

Example​

const type = 'core::zeroable::NonZero::<core::integer::u8>';
const result = new CairoNonZero(2, type, cairoTypeStrategy).contentType;
// result = "core::zeroable::NonZero::<core::integer::u8>"

Methods​

getNonZeroType()​

static getNonZeroType(type): string

Defined in: src/utils/cairoDataTypes/nonZero.ts:183

The type a NonZero wraps.

Parameters​

type​

string

the abi type to read

Returns​

string

the type between the angle brackets

Example​

const result = CairoNonZero.getNonZeroType('core::zeroable::NonZero::<core::integer::u8>');
// result = "core::integer::u8"

validate()​

static validate(input, type): void

Defined in: src/utils/cairoDataTypes/nonZero.ts:202

Throw unless this is a NonZero of a type Cairo allows there.

The value is not read here : whether it is zero is checked once it has been built, since that is what turns '0x0' and 0 into the same thing.

Parameters​

input​

unknown

the value, which this does not read

type​

string

the abi type it is meant for

Returns​

void

Throws​

when the type is not a NonZero, or wraps a type Cairo does not allow

Example​

CairoNonZero.validate(1, 'core::zeroable::NonZero::<core::integer::u8>'); // passes
CairoNonZero.validate(1, 'core::zeroable::NonZero::<core::integer::u512>');
// throws Error("Validate: core::integer::u512 type is not authorized for NonZero type.")

is()​

static is(input, type): boolean

Defined in: src/utils/cairoDataTypes/nonZero.ts:261

Can this be read as a NonZero of this type?

The non-throwing form of CairoNonZero.validate, so it says nothing about whether the value is zero.

Parameters​

input​

unknown

the value to test

type​

string

the abi type it is meant for

Returns​

boolean

true when the type is a NonZero of an allowed type

Example​

const result = CairoNonZero.is(1, 'core::zeroable::NonZero::<core::integer::u8>');
// result = true
const result2 = CairoNonZero.is(1, 'core::integer::u8');
// result2 = false

isAbiType()​

static isAbiType(type): boolean

Defined in: src/utils/cairoDataTypes/nonZero.ts:282

Is this abi type a NonZero?

Parameters​

type​

string

the abi type to test

Returns​

boolean

true for core::zeroable::NonZero::<T>

Example​

const result = CairoNonZero.isAbiType('core::zeroable::NonZero::<core::integer::u8>');
// result = true
const result2 = CairoNonZero.isAbiType('core::integer::u8');
// result2 = false

validateValue()​

validateValue(cairoInstance): void

Defined in: src/utils/cairoDataTypes/nonZero.ts:234

Throw unless this built value is one a NonZero may hold, and is not zero.

Only a value given raw goes through here : one already built is taken as it stands, and a response is read as it comes.

Parameters​

cairoInstance​

CairoType

the value that was just built

Returns​

void

Throws​

when the value is zero, or of a class a NonZero may not hold

Example​

// called from the constructor
const type = 'core::zeroable::NonZero::<core::integer::u8>';
new CairoNonZero(0, type, cairoTypeStrategy);
// throws Error("ValidateValue: value 0 is not authorized in NonZero type.")

toApiRequest()​

toApiRequest(): string[]

Defined in: src/utils/cairoDataTypes/nonZero.ts:299

Serialize to the felts a contract call carries.

Exactly the felts of the wrapped value : a NonZero is a promise about a value, not a shape around it.

Returns​

string[]

the wrapped value's felts, flagged as compiled

Example​

const type = 'core::zeroable::NonZero::<core::integer::u8>';
const result = new CairoNonZero(2, type, cairoTypeStrategy).toApiRequest();
// result = ["2"]

decompose()​

decompose(strategyDecompose): any

Defined in: src/utils/cairoDataTypes/nonZero.ts:315

Read the wrapped value back as the plain value a caller reads.

Parameters​

strategyDecompose​

AllowArray<CairoTypeStrategy>

how to read the value back

Returns​

any

the value the NonZero wraps

Throws​

when no strategy can read it back

Example​

const type = 'core::zeroable::NonZero::<core::integer::u8>';
const result = new CairoNonZero(2, type, cairoTypeStrategy).decompose(cairoTypeStrategy);
// result = 2n