v2 Contract SDK
  • Orbs Contract SDK
  • Getting Started
    • About smart contracts
    • Becoming a Go developer
    • Installing Gamma - local blockchain
    • Deploying your first contract
    • The Orbs Starter Kit
    • Downloading the Contract SDK
  • Orbs Contracts
    • Smart contracts
    • Layout of a contract file
    • Data types (Exported Functions)
    • State
    • Address
    • Events
    • Error handling
    • Calling other contracts
    • Calling Ethereum contract
    • API Reference
    • Limitations of Orbs Contracts
    • Creating a new contract
  • Gamma in Depth
    • Starting and stopping the server
    • Test keys and accounts
    • Deploying smart contracts
    • Sending transactions and queries
    • Checking sent transaction status
    • Reading Logs from Contracts
    • Working with multiple environments
    • Upgrading to latest versions
    • Gamma server under the hood
    • Deploying Gamma in the Cloud
      • Amazon Web Services
      • Google Cloud Platform
      • Azure
Powered by GitBook
On this page
  • Orbs smart contracts
  • Storage and State
  • Exported functions
  • Transactions and Queries
  1. Orbs Contracts

Smart contracts

Smart contracts are the cornerstone of decentralized systems. When developers are building a decentralized app, they are implementing one or more smart contracts, and ultimately deploying them to run over a blockchain infrastructure like Orbs.

Orbs smart contracts

The Orbs smart contracts are the logic of your apps: the high level workflow is creating the contracts (code) using the Orbs Contract SDK, testing it and experimenting with the Gamma framework, and later deploying your contract to the Orbs blockchain.

Currently, the contracts can be written in the Golang language. In the future the Orbs contract SDK will be available in other languages as well.

Similar to other Smart Contract frameworks, an Orbs contract is comprised of the written code (functions) and data (state) that resides inside of the blockchain.

Here is an example of a straightforward contract that implements a counter. It has two main functions, get and add, and it stores a single state value called counter.

counter.go
package main

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

var PUBLIC = sdk.Export(add, get)
var SYSTEM = sdk.Export(_init)

var COUNTER_KEY = []byte("count")

func _init() {
    state.WriteUint64(COUNTER_KEY, 0)
}

func add(amount uint64) {
    count := state.ReadUint64(COUNTER_KEY)
    count += amount
    state.WriteUint64(COUNTER_KEY, count)
}

func get() uint64 {
    return state.ReadUint64(COUNTER_KEY)
}

Storage and State

The line state.WriteUint64([]byte("count"), 0) will use the state library to write to the state. The state library is the only way to interact with the contract state and thus non-volatile storage. In the get() function, the line return state.ReadUint64([]byte("count")) will read the count value (and return it), that count value is the same one that was written in the _init() function or the add() function.

Exported functions

In Golang, functions beginning with the lowercase letters are considered as 'private' or not exported, and those whose name starts with an uppercase are 'public' and exported.

In Orbs, all functions should be written as 'private', meaning beginning with a lowercase letter, and functions that should be exported will be declared explicitly using the sdk.Export() function. In the example above, we can see that the functions add and get are declared as exported.

Transactions and Queries

There are two ways of interacting with the Orbs blockchain: sending a transaction or running a query.

Sending a transaction means that the logic executed by the contract inside the virtual machine is executed under consensus, and thus the logic is allowed to change the state.

Running a query is a more lightweight operation which does not happen under consensus. However, if the logic of the querry is executed and the state is being written to, the result of the execution will be an error, as the operation was not done under a transaction.

PreviousDownloading the Contract SDKNextLayout of a contract file

Last updated 5 years ago

The state is described in more detail at the documentation page.

Contract state