Class: CairoStorageAddress
Defined in: src/utils/cairoDataTypes/storageAddress.ts:29
A Cairo core::starknet::storage_access::StorageAddress : the address of a value in the storage
of a contract.
On the wire it is a field element like any other, so what this class adds over
CairoFelt252 is the bound of the Cairo StorageAddress type : [0, 2^251), the same range
as the Cairo type of a class hash, and narrower than the field.
A number, a bigint, a decimal string and a hexadecimal string are all read as the same number. Text is not an accepted input : a storage address spelled as words is a mistake, not a value.
Example
// the same address, reached three ways
new CairoStorageAddress('0x1234').toBigInt(); // 4660n
new CairoStorageAddress(4660).toBigInt(); // 4660n
new CairoStorageAddress('4660').toBigInt(); // 4660n
Constructors
Constructor
new CairoStorageAddress(
data):CairoStorageAddress
Defined in: src/utils/cairoDataTypes/storageAddress.ts:61
Build from a number or a numeric string, refusing text and anything outside the StorageAddress range.
Parameters
data
unknown
the address to carry, within [0, 2^251 - 1]
Returns
CairoStorageAddress
Throws
when the value is text, a decimal number, not a felt252 input, or out of the StorageAddress range
Example
const result = new CairoStorageAddress('0x1234').toApiRequest();
// result = ["4660"]
Properties
abiSelector
staticabiSelector:"core::starknet::storage_access::StorageAddress"=Literal.StorageAddress
Defined in: src/utils/cairoDataTypes/storageAddress.ts:48
The abi type this class serializes.
Example
const result = CairoStorageAddress.abiSelector;
// result = "core::starknet::storage_access::StorageAddress"
data
data:
bigint
Defined in: src/utils/cairoDataTypes/storageAddress.ts:38
The address, always as a bigint.
Example
const result = new CairoStorageAddress('0x1234').data;
// result = 4660n
Methods
validate()
staticvalidate(data):void
Defined in: src/utils/cairoDataTypes/storageAddress.ts:129
Throw unless the value can be carried by a StorageAddress.
Text is refused first, then a number with a decimal part. A number or a bigint is read as it
is, so that a negative one reaches the range check. Anything else is read as a felt252 — which
is what refuses a null, an object or an unsupported type. The value is finally checked against
the range of the Cairo StorageAddress type, [0, 2^251), which is narrower than the field.
Parameters
data
unknown
the value to check
Returns
void
Throws
when the value is text, a decimal number, not a felt252 input, or out of the StorageAddress range
Example
CairoStorageAddress.validate('0x1234'); // passes
CairoStorageAddress.validate('abc');
// throws Error("Invalid input: a StorageAddress cannot be built from text")
CairoStorageAddress.validate(2n ** 251n);
// throws Error("Value is out of StorageAddress range [0, 3618502788666131106986593281521497120414687020801267626233049500247285301247]")
CairoStorageAddress.validate(-1);
// throws Error("Value is out of StorageAddress range [0, 3618502788666131106986593281521497120414687020801267626233049500247285301247]")
is()
staticis(data):boolean
Defined in: src/utils/cairoDataTypes/storageAddress.ts:161
Can this value be carried by a StorageAddress?
The non-throwing form of CairoStorageAddress.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 StorageAddress
Example
const result = CairoStorageAddress.is('0x1234');
// result = true
const result2 = CairoStorageAddress.is('abc');
// result2 = false (text, not a number)
isAbiType()
staticisAbiType(abiType):boolean
Defined in: src/utils/cairoDataTypes/storageAddress.ts:184
Is this abi type the one this class serializes?
Parameters
abiType
string
the abi type to test
Returns
boolean
true for core::starknet::storage_access::StorageAddress
Example
const result = CairoStorageAddress.isAbiType(
'core::starknet::storage_access::StorageAddress'
);
// result = true
const result2 = CairoStorageAddress.isAbiType('core::felt252');
// result2 = false
factoryFromApiResponse()
staticfactoryFromApiResponse(responseIterator):CairoStorageAddress
Defined in: src/utils/cairoDataTypes/storageAddress.ts:199
Read one StorageAddress off a contract response, advancing the iterator past it.
Parameters
responseIterator
Iterator<string>
the response felts, positioned on this address
Returns
CairoStorageAddress
the address that was read
Example
const response = ['0x1234'];
const result = CairoStorageAddress.factoryFromApiResponse(response.values()).toBigInt();
// result = 4660n
toApiRequest()
toApiRequest():
string[]
Defined in: src/utils/cairoDataTypes/storageAddress.ts:75
Serialize to the single felt a contract call carries.
Returns
string[]
one decimal-string felt, flagged as compiled
Example
const result = new CairoStorageAddress('0x1234').toApiRequest();
// result = ["4660"]
toBigInt()
toBigInt():
bigint
Defined in: src/utils/cairoDataTypes/storageAddress.ts:88
The address as a number.
Returns
bigint
the number this address holds
Example
const result = new CairoStorageAddress('0x1234').toBigInt();
// result = 4660n
toHexString()
toHexString():
string
Defined in: src/utils/cairoDataTypes/storageAddress.ts:105
The address in hexadecimal, without padding.
Leading zeros are dropped, as they are everywhere else in the library.
Returns
string
the address as a 0x-prefixed hex string
Example
const result = new CairoStorageAddress(4660).toHexString();
// result = "0x1234"
const result2 = new CairoStorageAddress('0x0034').toHexString();
// result2 = "0x34" (four digits in, two out)