storage

get_context() StorageContext

Gets current storage context.

>>> get_context()       # StorageContext cannot be read outside the blockchain
_InteropInterface
Returns:

the current storage context

Return type:

StorageContext

get(key: bytes, context: StorageContext = None) bytes

Gets a value from the persistent store based on the given key.

>>> put(b'unit', 'test')
... get(b'unit')
b'test'
>>> get(b'fake_key')
b''
Parameters:
  • key (bytes) – value identifier in the store

  • context (StorageContext) – storage context to be used

Returns:

the value corresponding to given key for current storage context

Return type:

bytes

get_int(key: bytes, context: StorageContext = None) int

Gets a value as integer from the persistent store based on the given key. It’s equivalent to boa3.builtin.type.helper.to_int(get(key, context))

>>> put_int(b'unit', 5)
... get_int(b'unit')
5
>>> get_int(b'fake_key')
0
Parameters:
  • key (bytes) – value identifier in the store

  • context (StorageContext) – storage context to be used

Returns:

the value corresponding to given key for current storage context

Return type:

int

get_bool(key: bytes, context: StorageContext = None) bool

Gets a value as boolean from the persistent store based on the given key. It’s equivalent to boa3.builtin.type.helper.to_bool(get(key, context))

>>> put_bool(b'unit', True)
... get_bool(b'unit')
True
>>> get_bool(b'fake_key')
False
Parameters:
  • key (bytes) – value identifier in the store

  • context (StorageContext) – storage context to be used

Returns:

the value corresponding to given key for current storage context

Return type:

bool

get_str(key: bytes, context: StorageContext = None) str

Gets a value as string from the persistent store based on the given key. It’s equivalent to boa3.builtin.type.helper.to_str(get(key, context))

>>> put_str(b'unit', 'test')
... get_str(b'unit')
'test'
>>> get_str(b'fake_key')
''
Parameters:
  • key (bytes) – value identifier in the store

  • context (StorageContext) – storage context to be used

Returns:

the value corresponding to given key for current storage context

Return type:

str

get_uint160(key: bytes, context: StorageContext = None) UInt160

Gets a value as UInt160 from the persistent store based on the given key. It’s equivalent UInt160(get(key, context))

>>> put_uint160(b'unit', UInt160(b'0123456789ABCDEFGHIJ'))
... get_uint160(b'unit')
UInt160(0x4a49484746454443424139383736353433323130)
>>> get_uint160(b'fake_key')
UInt160(0x0000000000000000000000000000000000000000)
Parameters:
  • key (bytes) – value identifier in the store

  • context (StorageContext) – storage context to be used

Returns:

the value corresponding to given key for current storage context

Return type:

UInt160

get_uint256(key: bytes, context: StorageContext = None) UInt256

Gets a value as UInt256 from the persistent store based on the given key. It’s equivalent UInt256(get(key, context))

>>> put_uint256(b'unit', UInt256(b'0123456789ABCDEFGHIJKLMNOPQRSTUV'))
... get_uint256(b'unit')
UInt256(0x565554535251504f4e4d4c4b4a49484746454443424139383736353433323130)
>>> get_uint160(b'fake_key')
UInt256(0x0000000000000000000000000000000000000000000000000000000000000000)
Parameters:
  • key (bytes) – value identifier in the store

  • context (StorageContext) – storage context to be used

Returns:

the value corresponding to given key for current storage context

Return type:

UInt256

get_ecpoint(key: bytes, context: StorageContext = None) ECPoint

Gets a value as ECPoint from the persistent store based on the given key. It’s equivalent ECPoint(get(key, context))

>>> put_ecpoint(b'unit', ECPoint(b'0123456789ABCDEFGHIJKLMNOPQRSTUVW'))
... get_ecpoint(b'unit')
ECPoint(0x57565554535251504f4e4d4c4b4a49484746454443424139383736353433323130)
>>> get_ecpoint(b'fake_key')
ECPoint(0x000000000000000000000000000000000000000000000000000000000000000000)
Parameters:
  • key (bytes) – value identifier in the store

  • context (StorageContext) – storage context to be used

Returns:

the value corresponding to given key for current storage context

Return type:

ECPoint

get_read_only_context() StorageContext

Gets current read only storage context.

>>> get_context()       # StorageContext cannot be read outside the blockchain
_InteropInterface
Returns:

the current read only storage context

Return type:

StorageContext

put(key: bytes, value: bytes, context: StorageContext = None)

Inserts a given bytes value in the key-value format into the persistent storage.

>>> put(b'unit', b'test')
None
Parameters:
  • key (bytes) – the identifier in the store for the new value

  • value (bytes) – value to be stored

  • context (StorageContext) – storage context to be used

put_int(key: bytes, value: int, context: StorageContext = None)

Inserts a given integer value in the key-value format into the persistent storage. It’s equivalent to put(key, boa3.builtin.type.helper.to_int(value), context)

>>> put_int(b'unit', 5)
None
Parameters:
  • key (bytes) – the identifier in the store for the new value

  • value (int) – value to be stored

  • context (StorageContext) – storage context to be used

put_bool(key: bytes, value: bool, context: StorageContext = None)

Inserts a given boolean value in the key-value format into the persistent storage. It’s equivalent to put(key, boa3.builtin.type.helper.to_bool(value), context)

>>> put_bool(b'unit', True)
None
Parameters:
  • key (bytes) – the identifier in the store for the new value

  • value (bool) – value to be stored

  • context (StorageContext) – storage context to be used

put_str(key: bytes, value: str, context: StorageContext = None)

Inserts a given str value in the key-value format into the persistent storage. It’s equivalent to put(key, boa3.builtin.type.helper.to_str(value), context)

>>> put_str(b'unit', 'test')
None
Parameters:
  • key (bytes) – the identifier in the store for the new value

  • value (str) – value to be stored

  • context (StorageContext) – storage context to be used

put_uint160(key: bytes, value: UInt160, context: StorageContext = None)

Inserts a given UInt160 value in the key-value format into the persistent storage. It’s equivalent to put(key, value, context) since UInt160 is a subclass of bytes

>>> put_uint160(b'unit', UInt160(b'0123456789ABCDEFGHIJ'))
None
Parameters:
  • key (bytes) – the identifier in the store for the new value

  • value (UInt160) – value to be stored

  • context (StorageContext) – storage context to be used

put_uint256(key: bytes, value: UInt256, context: StorageContext = None)

Inserts a given UInt256 value in the key-value format into the persistent storage. It’s equivalent to put(key, value, context) since UInt256 is a subclass of bytes

>>> put_uint256(b'unit', UInt256(b'0123456789ABCDEFGHIJKLMNOPQRSTUV'))
None
Parameters:
  • key (bytes) – the identifier in the store for the new value

  • value (UInt256) – value to be stored

  • context (StorageContext) – storage context to be used

put_ecpoint(key: bytes, value: ECPoint, context: StorageContext = None)

Inserts a given ECPoint value in the key-value format into the persistent storage. It’s equivalent to put(key, value, context) since ECPoint is a subclass of bytes

>>> put_ecpoint(b'unit', ECPoint(b'0123456789ABCDEFGHIJKLMNOPQRSTUVW'))
None
Parameters:
  • key (bytes) – the identifier in the store for the new value

  • value (ECPoint) – value to be stored

  • context (StorageContext) – storage context to be used

delete(key: bytes, context: StorageContext = None)

Removes a given key from the persistent storage if exists.

>>> put(b'unit', 'test')
... delete()
... get(b'unit')
''
Parameters:
  • key (bytes) – the identifier in the store for the new value

  • context (StorageContext) – storage context to be used

find(prefix: bytes, context: StorageContext = None, options: FindOptions = FindOptions.NONE) Iterator

Searches in the storage for keys that start with the given prefix.

>>> put(b'a1', 'one')
... put(b'a2', 'two')
... put(b'a3', 'three')
... put(b'b4', 'four')
... findIterator = find(b'a')
... findResults = []
... while findIterator.next():
...     findResults.append(findIterator.value)
... findResults
['one', 'two', 'three']
Parameters:
  • prefix (bytes) – prefix to find the storage keys

  • context (StorageContext) – storage context to be used

  • options (FindOptions) – the options of the search

Returns:

an iterator with the search results

Return type:

Iterator

Subpackages

class FindOptions(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)

Bases: IntEnum

Represents the options you can use when trying to find a set of values inside the storage.

Check out Neo’s Documentation to learn more about the FindOption class.

NONE

No option is set. The results will be an iterator of (key, value).

KEYS_ONLY

Indicates that only keys need to be returned. The results will be an iterator of keys.

REMOVE_PREFIX

Indicates that the prefix byte of keys should be removed before return.

VALUES_ONLY

Indicates that only values need to be returned. The results will be an iterator of values.

DESERIALIZE_VALUES

Indicates that values should be deserialized before return.

PICK_FIELD_0

Indicates that only the field 0 of the deserialized values need to be returned. This flag must be set together with DESERIALIZE_VALUES, e.g., DESERIALIZE_VALUES | PICK_FIELD_0

PICK_FIELD_1

Indicates that only the field 1 of the deserialized values need to be returned. This flag must be set together with DESERIALIZE_VALUES, e.g., DESERIALIZE_VALUES | PICK_FIELD_1

BACKWARDS

Indicates that results should be returned in backwards (descending) order.

class StorageContext

Bases: object

The storage context used to read and write data in smart contracts.

Check out Neo’s Documentation to learn more about the StorageContext class.

create_map(prefix: bytes) StorageMap

Creates a storage map with the given prefix.

Parameters:

prefix (bytes) – the identifier of the storage map

Returns:

a map with the key-values in the storage that match with the given prefix

Return type:

StorageMap

as_read_only() StorageContext

Converts the specified storage context to a new readonly storage context.

Returns:

current StorageContext as ReadOnly

Return type:

StorageContext

class StorageMap(context, prefix: bytes)

Bases: object

The key-value storage for the specific prefix in the given storage context.

Check out Neo’s Documentation to learn more about StorageMap.

get(key: bytes) bytes

Gets a value from the map based on the given key.

Parameters:

key (bytes) – value identifier in the store

Returns:

the value corresponding to given key for current storage context

Return type:

bytes

put(key: bytes, value: int | bytes | str)

Inserts a given value in the key-value format into the map.

Parameters:
  • key (bytes) – the identifier in the store for the new value

  • value (int or str or bytes) – value to be stored

delete(key: bytes)

Removes a given key from the map if exists.

Parameters:

key (bytes) – the identifier in the store for the new value