neo-go/examples/storage/storage.go

43 lines
986 B
Go
Raw Normal View History

package storagecontract
import (
"github.com/nspcc-dev/neo-go/pkg/interop/iterator"
"github.com/nspcc-dev/neo-go/pkg/interop/storage"
)
2020-08-10 10:42:02 +00:00
// ctx holds storage context for contract methods
var ctx storage.Context
2020-08-10 10:42:02 +00:00
// init inits storage context before any other contract method is called
func init() {
ctx = storage.GetContext()
}
// Put puts value at key.
2020-08-10 10:42:02 +00:00
func Put(key, value []byte) []byte {
storage.Put(ctx, key, value)
return key
}
// Get returns the value at passed key.
2020-08-10 10:42:02 +00:00
func Get(key []byte) interface{} {
return storage.Get(ctx, key)
}
// Delete deletes the value at passed key.
2020-08-10 10:42:02 +00:00
func Delete(key []byte) bool {
storage.Delete(ctx, key)
return true
}
2020-08-10 10:42:02 +00:00
// Find returns an array of key-value pairs with key that matched the passed value
func Find(value []byte) []string {
iter := storage.Find(ctx, value, storage.None)
2020-08-10 10:42:02 +00:00
result := []string{}
for iterator.Next(iter) {
val := iterator.Value(iter).([]string)
result = append(result, val[0]+":"+val[1])
}
2020-08-10 10:42:02 +00:00
return result
}