State

Part of the smart contract infrastructure that Orbs support requires not only code, which are the written functions in the contract, but also the state which is the data that the contract stores.

This state (data) is isolated per contract, meaning each contract can only access its own data and the state is never shared between contracts - if the contract owner wants to share its state, the way to do this is using an exported method.

Orbs contracts will always behave with accordance to statically typed languages, specifically with Go, you may refer to the Data types page for details on supported types that can be used and stored to or read from the state

The state in Orbs can be referred to a map or dictionary, the api will always be in the form of:

state.write(key, value)
value = state.read(key)

In order to use the state in your contract, you must add the following import line to your contract code:

import (
    "github.com/orbs-network/orbs-contract-sdk/go/sdk/v1/state"
)

Scope

The state is 'storing' data in a contract scope, meaning that the data stored is available only for the contract it was stored at, and can be read only by that contract

Writing to state

The following write actions are available to use:

state.WriteBytes(key, value)
state.WriteString(key, value)
state.WriteUint32(key, value)
state.WriteUint64(key, value)
state.WriteBool(key, value)
state.WriteUint256(key, value)
state.WriteBytes20(key, value)
state.WriteBytes32(key, value)

Each function will accept value with the given type and store it to the state at the given key

The key will always be binary as well, an array of bytes

Reading from the state

The following read actions are available to use:

state.ReadBytes(key)
state.ReadString(key)
state.ReadUint32(key)
state.ReadUint64(key)
state.ReadBool(key)
state.ReadUint256(key)
state.ReadBytes20(key)
state.ReadBytes32(key)

Each function will accept the key of the value you need to read, and return the typed value from the contract state.

Reading a value that was never set will return the zero value of its type. Reading with a function of a different type than what was written is highly discouraged, and may fail (especially for fixed size ones like bool, [20]byte and [32]byte).

Removing data

It is also possible to remove data from the state using the Clear function:

state.Clear(key)

Clear does not return any value.

Last updated