Skip to main content
Version: 11.x

Class: CairoUint32

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

A Cairo core::integer::u32 : a whole number in [0, 4294967295], carried in one felt252.

The value is kept as a bigint, so the shape of the input does not survive — 1633837924, '1633837924' and '0x61626364' give the same u32. A string that reads as text rather than as a number is taken for its UTF-8 bytes, which is why 'abcd' is 1633837924 and not a rejected input.

An already built instance is not an accepted input : handed back to the constructor it is seen as an object and refused. Inside the library it is unwrapCairoScalar that reduces an instance to its number before an abi slot receives it.

Example​

// the same value, reached four ways
new CairoUint32(1633837924).toBigInt(); // 1633837924n
new CairoUint32('1633837924').toBigInt(); // 1633837924n
new CairoUint32('0x61626364').toBigInt(); // 1633837924n
new CairoUint32('abcd').toBigInt(); // 1633837924n the UTF-8 bytes of the text

Constructors​

Constructor​

new CairoUint32(data): CairoUint32

Defined in: src/utils/cairoDataTypes/uint32.ts:65

Build from a number, a string or a boolean, refusing anything out of the u32 range.

A string is read as a number when it spells one — decimal or hexadecimal — and as UTF-8 text otherwise. Text therefore only fits here up to 4 ASCII characters : one more already makes a number past 4294967295.

Parameters​

data​

unknown

the value to carry, within [0, 4294967295]

Returns​

CairoUint32

Throws​

when the value is null, undefined, an object, a decimal number, '0x', or out of range

Example​

const result = new CairoUint32('abcd').toApiRequest();
// result = ["1633837924"]

Properties​

abiSelector​

static abiSelector: string = 'core::integer::u32'

Defined in: src/utils/cairoDataTypes/uint32.ts:49

The abi type this class serializes.

Example​

const result = CairoUint32.abiSelector;
// result = "core::integer::u32"

data​

data: bigint

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

The value, always as a bigint.

Example​

const result = new CairoUint32('0x61626364').data;
// result = 1633837924n

Methods​

__processData()​

static __processData(data): bigint

Defined in: src/utils/cairoDataTypes/uint32.ts:85

Turn an accepted input into its number, before the range is checked.

Nothing here refuses a value : validate is what reads this number and decides. So an input far outside the u32 range comes back untouched rather than raising.

Parameters​

data​

unknown

the value to convert

Returns​

bigint

the number the input spells, of whatever size

Example​

const result = CairoUint32.__processData('abcd');
// result = 1633837924n
const result2 = CairoUint32.__processData('abcde');
// result2 = 418262508645n (past the u32 range, and returned all the same)

validate()​

static validate(data): void

Defined in: src/utils/cairoDataTypes/uint32.ts:165

Throw unless the value can be carried by a u32.

Five things are refused, each with its own message : a null or undefined value, an object or an array, a number with a decimal part, '0x', and a value outside [0, 4294967295]. A text string reaches that last check as the number its bytes spell, so 'abcde' is refused for being out of range.

Parameters​

data​

unknown

the value to check

Returns​

void

Throws​

when the value is null, undefined, an object, a decimal number, '0x', or out of range

Example​

CairoUint32.validate(70000); // passes
CairoUint32.validate(4294967296);
// throws Error("Value is out of u32 range [0, 4294967295]")

is()​

static is(data): boolean

Defined in: src/utils/cairoDataTypes/uint32.ts:196

Can this value be carried by a u32?

The non-throwing form of CairoUint32.validate, so it answers false for every input that one refuses, whatever the reason.

Parameters​

data​

unknown

the value to test

Returns​

boolean

true when the value fits in a u32

Example​

const result = CairoUint32.is('abcd');
// result = true (1633837924, the UTF-8 bytes of the text)
const result2 = CairoUint32.is('abcde');
// result2 = false (418262508645, past the u32 range)

isAbiType()​

static isAbiType(abiType): boolean

Defined in: src/utils/cairoDataTypes/uint32.ts:217

Is this abi type the one this class serializes?

Parameters​

abiType​

string

the abi type to test

Returns​

boolean

true for core::integer::u32

Example​

const result = CairoUint32.isAbiType('core::integer::u32');
// result = true
const result2 = CairoUint32.isAbiType('core::felt252');
// result2 = false

factoryFromApiResponse()​

static factoryFromApiResponse(responseIterator): CairoUint32

Defined in: src/utils/cairoDataTypes/uint32.ts:235

Read one u32 off a contract response, advancing the iterator past it.

The felts a node returns are hex strings, and one is consumed per call, so successive calls read successive return values.

Parameters​

responseIterator​

Iterator<string>

the response felts, positioned on this u32

Returns​

CairoUint32

the u32 that was read

Example​

const response = ['0x11170'];
const result = CairoUint32.factoryFromApiResponse(response.values()).toBigInt();
// result = 70000n

toApiRequest()​

toApiRequest(): string[]

Defined in: src/utils/cairoDataTypes/uint32.ts:102

Serialize to the single felt a contract call carries.

Returns​

string[]

one decimal-string felt, flagged as compiled

Example​

const result = new CairoUint32(70000).toApiRequest();
// result = ["70000"]

toBigInt()​

toBigInt(): bigint

Defined in: src/utils/cairoDataTypes/uint32.ts:115

The value as a number.

Returns​

bigint

the number this u32 holds

Example​

const result = new CairoUint32(4294967295).toBigInt();
// result = 4294967295n

decodeUtf8()​

decodeUtf8(): string

Defined in: src/utils/cairoDataTypes/uint32.ts:131

Read the value back as the UTF-8 text its bytes spell.

Only a value that was text to begin with comes back as readable text. Any other number is decoded all the same, and what it gives is whatever its bytes happen to mean.

Returns​

string

the bytes decoded as UTF-8

Example​

const result = new CairoUint32('abcd').decodeUtf8();
// result = "abcd"

toHexString()​

toHexString(): string

Defined in: src/utils/cairoDataTypes/uint32.ts:146

The value in hexadecimal, without padding.

Returns​

string

the number as a 0x-prefixed hex string

Example​

const result = new CairoUint32(70000).toHexString();
// result = "0x11170"
const result2 = new CairoUint32(0).toHexString();
// result2 = "0x0" (one digit, not "0x00")