2020-10-02 11:16:25 +00:00
|
|
|
package deploy
|
|
|
|
|
|
|
|
import (
|
2022-10-05 06:44:10 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/cli/smartcontract/testdata/deploy/sub"
|
2020-12-13 15:26:35 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop"
|
2020-10-02 11:16:25 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/contract"
|
2021-01-12 12:52:08 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/iterator"
|
2020-12-13 15:26:35 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/runtime"
|
2020-10-02 11:16:25 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/storage"
|
|
|
|
)
|
|
|
|
|
|
|
|
var key = "key"
|
|
|
|
|
2020-12-13 15:26:35 +00:00
|
|
|
const mgmtKey = "mgmt"
|
|
|
|
|
2023-04-03 10:34:24 +00:00
|
|
|
func _deploy(data any, isUpdate bool) {
|
2020-12-13 15:26:35 +00:00
|
|
|
var value string
|
|
|
|
|
2020-10-02 11:16:25 +00:00
|
|
|
ctx := storage.GetContext()
|
|
|
|
if isUpdate {
|
|
|
|
value = "on update"
|
2020-12-13 15:26:35 +00:00
|
|
|
} else {
|
|
|
|
value = "on create"
|
|
|
|
sh := runtime.GetCallingScriptHash()
|
|
|
|
storage.Put(ctx, mgmtKey, sh)
|
2021-04-16 15:04:19 +00:00
|
|
|
|
|
|
|
if data != nil {
|
2023-04-03 10:34:24 +00:00
|
|
|
arr := data.([]any)
|
2021-04-16 15:04:19 +00:00
|
|
|
for i := 0; i < len(arr)-1; i += 2 {
|
|
|
|
storage.Put(ctx, arr[i], arr[i+1])
|
|
|
|
}
|
|
|
|
}
|
2020-10-02 11:16:25 +00:00
|
|
|
}
|
2020-12-13 15:26:35 +00:00
|
|
|
|
2020-10-02 11:16:25 +00:00
|
|
|
storage.Put(ctx, key, value)
|
|
|
|
}
|
|
|
|
|
2020-12-03 11:57:45 +00:00
|
|
|
// Fail just fails.
|
|
|
|
func Fail() {
|
|
|
|
panic("as expected")
|
|
|
|
}
|
|
|
|
|
2021-11-22 09:46:42 +00:00
|
|
|
// CheckSenderWitness checks sender's witness.
|
|
|
|
func CheckSenderWitness() {
|
|
|
|
tx := runtime.GetScriptContainer()
|
|
|
|
if !runtime.CheckWitness(tx.Sender) {
|
|
|
|
panic("not witnessed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Update updates the contract with a new one.
|
2020-10-02 11:16:25 +00:00
|
|
|
func Update(script, manifest []byte) {
|
2020-12-13 15:26:35 +00:00
|
|
|
ctx := storage.GetReadOnlyContext()
|
|
|
|
mgmt := storage.Get(ctx, mgmtKey).(interop.Hash160)
|
2020-12-29 10:44:07 +00:00
|
|
|
contract.Call(mgmt, "update", contract.All, script, manifest)
|
2020-10-02 11:16:25 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// GetValue returns the stored value.
|
2020-10-02 11:16:25 +00:00
|
|
|
func GetValue() string {
|
|
|
|
ctx := storage.GetReadOnlyContext()
|
|
|
|
val1 := storage.Get(ctx, key)
|
|
|
|
val2 := storage.Get(ctx, sub.Key)
|
|
|
|
return val1.(string) + "|" + val2.(string)
|
|
|
|
}
|
2021-01-12 12:52:08 +00:00
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// GetValueWithKey returns the stored value with the specified key.
|
2021-04-16 15:04:19 +00:00
|
|
|
func GetValueWithKey(key string) string {
|
|
|
|
ctx := storage.GetReadOnlyContext()
|
|
|
|
return storage.Get(ctx, key).(string)
|
|
|
|
}
|
|
|
|
|
2021-01-12 12:52:08 +00:00
|
|
|
// TestFind finds items with the specified prefix.
|
2023-04-03 10:34:24 +00:00
|
|
|
func TestFind(f storage.FindFlags) []any {
|
2021-01-12 12:52:08 +00:00
|
|
|
ctx := storage.GetContext()
|
|
|
|
storage.Put(ctx, "findkey1", "value1")
|
|
|
|
storage.Put(ctx, "findkey2", "value2")
|
|
|
|
|
2023-04-03 10:34:24 +00:00
|
|
|
var result []any
|
2021-01-12 12:52:08 +00:00
|
|
|
iter := storage.Find(ctx, "findkey", f)
|
|
|
|
for iterator.Next(iter) {
|
|
|
|
result = append(result, iterator.Value(iter))
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|