web3-course/l3/storage/counter.go
Stanislav Bogatyrev 230921d7ee Add Storage Counter SC
Signed-off-by: Stanislav Bogatyrev <s.bogatyrev@yadro.com>
2023-11-08 13:35:42 +03:00

31 lines
844 B
Go

package counter
import (
"github.com/nspcc-dev/neo-go/pkg/interop/runtime"
"github.com/nspcc-dev/neo-go/pkg/interop/storage"
)
const itemKey = "Counter"
func _deploy(_ interface{}, isUpdate bool) {
if !isUpdate {
ctx := storage.GetContext()
runtime.Notify("info", []byte("Counter not set in Storage. Setting to 0"))
itemValue := 0
storage.Put(ctx, itemKey, itemValue)
runtime.Notify("info", []byte("Counter in Storage is now initialised"))
}
}
func Main() interface{} {
ctx := storage.GetContext()
itemValue := storage.Get(ctx, itemKey)
runtime.Notify("info", []byte("Value read from Storage"))
runtime.Notify("info", []byte("Incrementing Counter by 1"))
itemValue = itemValue.(int) + 1
storage.Put(ctx, itemKey, itemValue)
runtime.Notify("info", []byte("New counter value written into Storage"))
return itemValue
}