Class: CairoInt64
Defined in: src/utils/cairoDataTypes/int64.ts:39
A Cairo core::integer::i64 : a whole number in [-9223372036854775808, 9223372036854775807], 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 i64. 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.
A minus sign in front of a number is read as a sign, not as text : '-5' is -5, as -5 and
-5n are. Only the signed integers read a string this way — for every other type, '-5' is
text, encoded or refused as such.
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 CairoInt64(7017280452245743464).toBigInt(); // 7017280452245743464n
new CairoInt64('7017280452245743464').toBigInt(); // 7017280452245743464n
new CairoInt64('0x6162636465666768').toBigInt(); // 7017280452245743464n
new CairoInt64('abcdefgh').toBigInt(); // 7017280452245743464n the UTF-8 bytes of the text
// a negative value, reached three ways
new CairoInt64(-5).toBigInt(); // -5n
new CairoInt64('-5').toBigInt(); // -5n
new CairoInt64('-0x5').toBigInt(); // -5n a sign, not the text "-0x5"
Constructors
Constructor
new CairoInt64(
data):CairoInt64
Defined in: src/utils/cairoDataTypes/int64.ts:76
Build from a number, a string or a boolean, refusing anything out of the i64 range.
A string is read as a number when it spells one — decimal or hexadecimal, with or without a leading minus sign — and as UTF-8 text otherwise. Text therefore only fits here up to 8 ASCII characters : one more already makes a number past 9223372036854775807.
Parameters
data
unknown
the value to carry, within [-9223372036854775808, 9223372036854775807]
Returns
CairoInt64
Throws
when the value is null, undefined, an object, a decimal number, '0x', or out of range
Example
const result = new CairoInt64('abcdefgh').toApiRequest();
// result = ["7017280452245743464"]
const result2 = new CairoInt64('-5').toApiRequest();
// result2 = ["3618502788666131213697322783095070105623107215331596699973092056135872020476"]
Properties
abiSelector
staticabiSelector:string='core::integer::i64'
Defined in: src/utils/cairoDataTypes/int64.ts:58
The abi type this class serializes.
Example
const result = CairoInt64.abiSelector;
// result = "core::integer::i64"
data
data:
bigint
Defined in: src/utils/cairoDataTypes/int64.ts:48
The value, always as a bigint.
Example
const result = new CairoInt64('0x6162636465666768').data;
// result = 7017280452245743464n
Methods
__processData()
static__processData(data):bigint
Defined in: src/utils/cairoDataTypes/int64.ts:101
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 i64 range comes back untouched rather than raising.
A minus sign in front of a number makes it negative rather than text : '-5' and '-0x5' are
both -5. This holds for the signed integers only — for any other type, such a string is text.
Parameters
data
unknown
the value to convert
Returns
bigint
the number the input spells, of whatever size
Example
const result = CairoInt64.__processData('abcdefgh');
// result = 7017280452245743464n
const result2 = CairoInt64.__processData('abcdefghi');
// result2 = 1796423795774910326889n (past the i64 range, and returned all the same)
const result3 = CairoInt64.__processData('-0x5');
// result3 = -5n (a negative number, not the text "-0x5")
validate()
staticvalidate(data):void
Defined in: src/utils/cairoDataTypes/int64.ts:204
Throw unless the value can be carried by an i64.
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 [-9223372036854775808, 9223372036854775807]. 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
CairoInt64.validate(5000000000); // passes
CairoInt64.validate(9223372036854775808);
// throws Error("Value is out of i64 range [-9223372036854775808, 9223372036854775807]")
is()
staticis(data):boolean
Defined in: src/utils/cairoDataTypes/int64.ts:235
Can this value be carried by an i64?
The non-throwing form of CairoInt64.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 an i64
Example
const result = CairoInt64.is('abcdefgh');
// result = true (7017280452245743464, the UTF-8 bytes of the text)
const result2 = CairoInt64.is('abcdefghi');
// result2 = false (1796423795774910326889, past the i64 range)
isAbiType()
staticisAbiType(abiType):boolean
Defined in: src/utils/cairoDataTypes/int64.ts:256
Is this abi type the one this class serializes?
Parameters
abiType
string
the abi type to test
Returns
boolean
true for core::integer::i64
Example
const result = CairoInt64.isAbiType('core::integer::i64');
// result = true
const result2 = CairoInt64.isAbiType('core::felt252');
// result2 = false
factoryFromApiResponse()
staticfactoryFromApiResponse(responseIterator):CairoInt64
Defined in: src/utils/cairoDataTypes/int64.ts:275
Read one i64 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. A felt past half the prime is a negative number written as its field element, and is brought back below zero here.
Parameters
responseIterator
Iterator<string>
the response felts, positioned on this i64
Returns
CairoInt64
the i64 that was read
Example
const response = ['0x12a05f200'];
const result = CairoInt64.factoryFromApiResponse(response.values()).toBigInt();
// result = 5000000000n
toApiRequest()
toApiRequest():
string[]
Defined in: src/utils/cairoDataTypes/int64.ts:128
Serialize to the single felt a contract call carries.
A negative value goes out as its field element, PRIME + value, which is what Cairo reads
back as the negative number.
Returns
string[]
one decimal-string felt, flagged as compiled
Example
const result = new CairoInt64(5000000000).toApiRequest();
// result = ["5000000000"]
const result2 = new CairoInt64(-5000000000).toApiRequest();
// result2 = ["3618502788666131213697322783095070105623107215331596699973092056130872020481"]
toBigInt()
toBigInt():
bigint
Defined in: src/utils/cairoDataTypes/int64.ts:141
The value as a number.
Returns
bigint
the number this i64 holds, negative included
Example
const result = new CairoInt64(-5000000000).toBigInt();
// result = -5000000000n
decodeUtf8()
decodeUtf8():
string
Defined in: src/utils/cairoDataTypes/int64.ts:159
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 — a negative value is first wrapped to its two's complement, which rarely spells valid UTF-8 and so comes back as replacement characters.
Returns
string
the bytes decoded as UTF-8
Example
const result = new CairoInt64('abcdefgh').decodeUtf8();
// result = "abcdefgh"
toHexString()
toHexString():
string
Defined in: src/utils/cairoDataTypes/int64.ts:179
The value in hexadecimal, without padding.
A negative value has no hexadecimal form of its own here : it is written as its field element,
PRIME + value, the positive number Cairo actually carries.
Returns
string
the number as a 0x-prefixed hex string
Example
const result = new CairoInt64(5000000000).toHexString();
// result = "0x12a05f200"
const result2 = new CairoInt64(-5000000000).toHexString();
// result2 = "0x800000000000010fffffffffffffffffffffffffffffffffffffffed5fa0e01"