2019-09-03 15:00:10 +00:00
|
|
|
package storagecontract
|
2018-08-20 11:22:46 +00:00
|
|
|
|
|
|
|
import (
|
2020-03-25 14:40:58 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/iterator"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/storage"
|
2018-08-20 11:22:46 +00:00
|
|
|
)
|
|
|
|
|
2020-08-10 10:42:02 +00:00
|
|
|
// ctx holds storage context for contract methods
|
|
|
|
var ctx storage.Context
|
2020-07-03 11:53:29 +00:00
|
|
|
|
2020-08-10 10:42:02 +00:00
|
|
|
// init inits storage context before any other contract method is called
|
|
|
|
func init() {
|
|
|
|
ctx = storage.GetContext()
|
2020-07-03 11:53:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2020-07-03 11:53:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
2020-07-03 11:53:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete deletes the value at passed key.
|
2020-08-10 10:42:02 +00:00
|
|
|
func Delete(key []byte) bool {
|
2020-07-03 11:53:29 +00:00
|
|
|
storage.Delete(ctx, key)
|
|
|
|
return true
|
|
|
|
}
|
2018-08-20 11:22:46 +00:00
|
|
|
|
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 {
|
2021-01-12 10:39:31 +00:00
|
|
|
iter := storage.Find(ctx, value, storage.None)
|
2020-08-10 10:42:02 +00:00
|
|
|
result := []string{}
|
|
|
|
for iterator.Next(iter) {
|
2021-01-12 09:30:21 +00:00
|
|
|
val := iterator.Value(iter).([]string)
|
|
|
|
result = append(result, val[0]+":"+val[1])
|
2018-08-20 11:22:46 +00:00
|
|
|
}
|
2020-08-10 10:42:02 +00:00
|
|
|
return result
|
2018-08-20 11:22:46 +00:00
|
|
|
}
|