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: Optional[StorageContext] = None) bytes

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

>>> put(b'unit', 'test')
... get(b'unit')
'test'
>>> get(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:

bytes

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: Union[int, bytes, str], context: Optional[StorageContext] = None)

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

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

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

  • context (StorageContext) – storage context to be used

delete(key: bytes, context: Optional[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: Optional[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)

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: Union[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