Class: CairoUint64
Defined in: src/utils/cairoDataTypes/uint64.ts:30
A Cairo core::integer::u64 : a whole number in [0, 18446744073709551615], carried in one felt252.
The value is kept as a bigint, so the shape of the input does not survive — 7017280452245743464,
'7017280452245743464' and '0x6162636465666768' give the same u64. A string that reads as text rather than as a
number is taken for its UTF-8 bytes, which is why 'abcdefgh' is 7017280452245743464 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 CairoUint64(7017280452245743464).toBigInt(); // 7017280452245743464n
new CairoUint64('7017280452245743464').toBigInt(); // 7017280452245743464n
new CairoUint64('0x6162636465666768').toBigInt(); // 7017280452245743464n
new CairoUint64('abcdefgh').toBigInt(); // 7017280452245743464n the UTF-8 bytes of the text
Constructors
Constructor
new CairoUint64(
data):CairoUint64
Defined in: src/utils/cairoDataTypes/uint64.ts:65
Build from a number, a string or a boolean, refusing anything out of the u64 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 8 ASCII characters : one more already makes a number past 18446744073709551615.
Parameters
data
unknown
the value to carry, within [0, 18446744073709551615]
Returns
CairoUint64
Throws
when the value is null, undefined, an object, a decimal number, '0x', or out of range
Example
const result = new CairoUint64('abcdefgh').toApiRequest();
// result = ["7017280452245743464"]
Properties
abiSelector
staticabiSelector:string='core::integer::u64'
Defined in: src/utils/cairoDataTypes/uint64.ts:49
The abi type this class serializes.
Example
const result = CairoUint64.abiSelector;
// result = "core::integer::u64"
data
data:
bigint
Defined in: src/utils/cairoDataTypes/uint64.ts:39
The value, always as a bigint.
Example
const result = new CairoUint64('0x6162636465666768').data;
// result = 7017280452245743464n
Methods
__processData()
static__processData(data):bigint
Defined in: src/utils/cairoDataTypes/uint64.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 u64 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 = CairoUint64.__processData('abcdefgh');
// result = 7017280452245743464n
const result2 = CairoUint64.__processData('abcdefghi');
// result2 = 1796423795774910326889n (past the u64 range, and returned all the same)
validate()
staticvalidate(data):void
Defined in: src/utils/cairoDataTypes/uint64.ts:165
Throw unless the value can be carried by a u64.
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, 18446744073709551615]. A text string reaches
that last check as the number its bytes spell, so 'abcdefghi' 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
CairoUint64.validate(5000000000); // passes
CairoUint64.validate(18446744073709551616);
// throws Error("Value is out of u64 range [0, 18446744073709551615]")
is()
staticis(data):boolean
Defined in: src/utils/cairoDataTypes/uint64.ts:196
Can this value be carried by a u64?
The non-throwing form of CairoUint64.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 u64
Example
const result = CairoUint64.is('abcdefgh');
// result = true (7017280452245743464, the UTF-8 bytes of the text)
const result2 = CairoUint64.is('abcdefghi');
// result2 = false (1796423795774910326889, past the u64 range)
isAbiType()
staticisAbiType(abiType):boolean
Defined in: src/utils/cairoDataTypes/uint64.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::u64
Example
const result = CairoUint64.isAbiType('core::integer::u64');
// result = true
const result2 = CairoUint64.isAbiType('core::felt252');
// result2 = false
factoryFromApiResponse()
staticfactoryFromApiResponse(responseIterator):CairoUint64
Defined in: src/utils/cairoDataTypes/uint64.ts:235
Read one u64 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 u64
Returns
CairoUint64
the u64 that was read
Example
const response = ['0x12a05f200'];
const result = CairoUint64.factoryFromApiResponse(response.values()).toBigInt();
// result = 5000000000n
toApiRequest()
toApiRequest():
string[]
Defined in: src/utils/cairoDataTypes/uint64.ts:102
Serialize to the single felt a contract call carries.
Returns
string[]
one decimal-string felt, flagged as compiled
Example
const result = new CairoUint64(5000000000).toApiRequest();
// result = ["5000000000"]
toBigInt()
toBigInt():
bigint
Defined in: src/utils/cairoDataTypes/uint64.ts:115
The value as a number.
Returns
bigint
the number this u64 holds
Example
const result = new CairoUint64(18446744073709551615).toBigInt();
// result = 18446744073709551615n
decodeUtf8()
decodeUtf8():
string
Defined in: src/utils/cairoDataTypes/uint64.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 CairoUint64('abcdefgh').decodeUtf8();
// result = "abcdefgh"
toHexString()
toHexString():
string
Defined in: src/utils/cairoDataTypes/uint64.ts:146
The value in hexadecimal, without padding.
Returns
string
the number as a 0x-prefixed hex string
Example
const result = new CairoUint64(5000000000).toHexString();
// result = "0x12a05f200"
const result2 = new CairoUint64(0).toHexString();
// result2 = "0x0" (one digit, not "0x00")