Class: CairoBool
Defined in: src/utils/cairoDataTypes/bool.ts:30
A Cairo core::bool : true or false, carried in one felt252 as 1 or 0.
A boolean is the natural input, but the two numbers a bool occupies on the wire are accepted
too — 1, 0n, '1', '0x0' — because that is how a bool comes back from a node, as a felt
rather than as a JS value. Any other number is refused : a bool is not a felt narrowed to a
range, it is exactly two values.
On the request side the library is stricter still : CallData.compile runs validateFields
first, which requires a real boolean. So a 1 reaching a core::bool argument is refused there
before this class ever sees it.
Example
// the same value, reached three ways
new CairoBool(true).toBoolean(); // true
new CairoBool(1).toBoolean(); // true
new CairoBool('0x1').toBoolean(); // true
Constructors
Constructor
new CairoBool(
data):CairoBool
Defined in: src/utils/cairoDataTypes/bool.ts:61
Build from a boolean, or from the numbers 0 and 1.
Parameters
data
unknown
the value to carry : a boolean, 0 or 1
Returns
CairoBool
Throws
when the value is text, is not a felt252 input, or is a number other than 0 or 1
Example
const result = new CairoBool(false).toApiRequest();
// result = ["0"]
Properties
abiSelector
staticabiSelector:string='core::bool'
Defined in: src/utils/cairoDataTypes/bool.ts:49
The abi type this class serializes.
Example
const result = CairoBool.abiSelector;
// result = "core::bool"
data
data:
boolean
Defined in: src/utils/cairoDataTypes/bool.ts:39
The value, always as a boolean.
Example
const result = new CairoBool('0x1').data;
// result = true
Methods
__processData()
static__processData(data):boolean
Defined in: src/utils/cairoDataTypes/bool.ts:79
Turn an accepted input into its boolean.
Nothing here refuses a value : validate is what decides, and only a boolean, a 0 or a 1
reaches this point.
Parameters
data
unknown
the value to convert
Returns
boolean
the boolean the input spells
Example
const result = CairoBool.__processData('0x1');
// result = true
validate()
staticvalidate(data):void
Defined in: src/utils/cairoDataTypes/bool.ts:144
Throw unless the value can be carried by a bool.
Text is refused first, then the value is read as a felt252 — which is what refuses a null, an object or an unsupported type — and finally checked to be one of the only two numbers a bool can hold.
Parameters
data
unknown
the value to check
Returns
void
Throws
when the value is text, is not a felt252 input, or is a number other than 0 or 1
Example
CairoBool.validate(true); // passes
CairoBool.validate(2);
// throws Error("Only values 0 or 1 are possible in a core::bool, received 2")
is()
staticis(data):boolean
Defined in: src/utils/cairoDataTypes/bool.ts:169
Can this value be carried by a bool?
The non-throwing form of CairoBool.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 is a boolean, a 0 or a 1
Example
const result = CairoBool.is(1);
// result = true
const result2 = CairoBool.is(2);
// result2 = false (a bool is exactly two values)
isAbiType()
staticisAbiType(abiType):boolean
Defined in: src/utils/cairoDataTypes/bool.ts:190
Is this abi type the one this class serializes?
Parameters
abiType
string
the abi type to test
Returns
boolean
true for core::bool
Example
const result = CairoBool.isAbiType('core::bool');
// result = true
const result2 = CairoBool.isAbiType('core::felt252');
// result2 = false
factoryFromApiResponse()
staticfactoryFromApiResponse(responseIterator):CairoBool
Defined in: src/utils/cairoDataTypes/bool.ts:208
Read one bool 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 bool
Returns
CairoBool
the bool that was read
Example
const response = ['0x1'];
const result = CairoBool.factoryFromApiResponse(response.values()).toBoolean();
// result = true
toApiRequest()
toApiRequest():
string[]
Defined in: src/utils/cairoDataTypes/bool.ts:97
Serialize to the single felt a contract call carries.
Returns
string[]
one decimal-string felt, "1" or "0", flagged as compiled
Example
const result = new CairoBool(true).toApiRequest();
// result = ["1"]
const result2 = new CairoBool(false).toApiRequest();
// result2 = ["0"]
toBoolean()
toBoolean():
boolean
Defined in: src/utils/cairoDataTypes/bool.ts:110
The value as a boolean.
Returns
boolean
the boolean this bool holds
Example
const result = new CairoBool(1).toBoolean();
// result = true
toHexString()
toHexString():
string
Defined in: src/utils/cairoDataTypes/bool.ts:125
The value in hexadecimal, as the felt a bool occupies.
Returns
string
"0x1" for true, "0x0" for false
Example
const result = new CairoBool(true).toHexString();
// result = "0x1"
const result2 = new CairoBool(false).toHexString();
// result2 = "0x0"