Deploying your first contract
This tutorial will take you through the entire process of writing a contract and deploying it on a local instance of ORBS in just a few minutes.

1. Write a simple contract

Let's write a simple example that implements a counter. Create a file named counter.go and type in the following content
counter.go
1
package main
2
3
import (
4
"github.com/orbs-network/orbs-contract-sdk/go/sdk/v1"
5
"github.com/orbs-network/orbs-contract-sdk/go/sdk/v1/state"
6
)
7
8
var PUBLIC = sdk.Export(add, get)
9
var SYSTEM = sdk.Export(_init)
10
11
var COUNTER_KEY = []byte("count")
12
13
func _init() {
14
state.WriteUint64(COUNTER_KEY, 0)
15
}
16
17
func add(amount uint64) {
18
count := state.ReadUint64(COUNTER_KEY)
19
count += amount
20
state.WriteUint64(COUNTER_KEY, count)
21
}
22
23
func get() uint64 {
24
return state.ReadUint64(COUNTER_KEY)
25
}
Copied!
Don't worry if you don't fully understand the code at this point.

2. Start Gamma server

We'll test our contract on Gamma server. Start it from terminal
1
gamma-cli start-local
Copied!
You can keep an eye on Prism (http://localhost:3000/) to see new blocks and transactions apear as you continue with the deployment in the next steps.

3. Deploy the contract

To deploy the counter contract, run in terminal
1
gamma-cli deploy counter.go -name MyCounter
Copied!
If the deploy is successful, you'll see a response similar to this
1
{
2
"RequestStatus": "COMPLETED",
3
"TxId": "7Y4urVmKvunYsxh7kKhUoQ72XjSJcdkBxxzBcauC9icC9gzMy8mPDcg",
4
"ExecutionResult": "SUCCESS",
5
"OutputArguments": [],
6
"TransactionStatus": "COMMITTED",
7
"BlockHeight": "1869",
8
"BlockTimestamp": "2018-12-05T13:05:51.347Z"
9
}
Copied!

4. Send a transaction to increment the counter

Write the transaction details in a JSON file named add-25.json
add-25.json
1
{
2
"ContractName": "MyCounter",
3
"MethodName": "add",
4
"Arguments": [
5
{
6
"Type": "uint64",
7
"Value": "25"
8
}
9
]
10
}
Copied!
More Information about sending transactions and the Gamma-cli json files structures can be found at the gamma in depth chapter of this documentation
To increment the counter by 75, let's send this transaction 3 times from terminal
1
gamma-cli send-tx add-25.json -signer user1
2
gamma-cli send-tx add-25.json -signer user1
3
gamma-cli send-tx add-25.json -signer user1
Copied!
Note that the transaction will be signed by user1, an example account found in orbs-test-keys.json

5. Read the counter value

Write the query details in a JSON file named get.json
get.json
1
{
2
"ContractName": "MyCounter",
3
"MethodName": "get",
4
"Arguments": []
5
}
Copied!
This query will read the counter value from the contract's state. Send it from terminal
1
gamma-cli run-query get.json
Copied!
Note that transactions that change state require consensus by several nodes. Reading state with queries is a simpler action that doesn't require consensus.

6. Stop Gamma server

Since we're done testing, the server is no longer needed. Let's stop it from terminal
1
gamma-cli stop-local
Copied!
Last modified 2yr ago