2020-10-10 17:17:04 +00:00
|
|
|
package timer
|
|
|
|
|
|
|
|
import (
|
2020-12-13 15:26:35 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop"
|
2020-10-10 17:17:04 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/contract"
|
2021-03-04 10:26:16 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/native/std"
|
2020-10-10 17:17:04 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/runtime"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/storage"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
const defaultTicks = 3
|
2020-12-13 15:26:35 +00:00
|
|
|
const mgmtKey = "mgmt"
|
2020-10-10 17:17:04 +00:00
|
|
|
|
|
|
|
var (
|
|
|
|
// ctx holds storage context for contract methods
|
|
|
|
ctx storage.Context
|
|
|
|
// Check if the invoker of the contract is the specified owner
|
2021-05-11 13:32:09 +00:00
|
|
|
owner = util.FromAddress("NbrUYaZgyhSkNoRo9ugRyEMdUZxrhkNaWB")
|
2020-10-10 17:17:04 +00:00
|
|
|
// ticksKey is a storage key for ticks counter
|
|
|
|
ticksKey = []byte("ticks")
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
ctx = storage.GetContext()
|
|
|
|
}
|
|
|
|
|
2023-04-03 10:34:24 +00:00
|
|
|
func _deploy(_ any, isUpdate bool) {
|
2020-10-10 17:17:04 +00:00
|
|
|
if isUpdate {
|
|
|
|
ticksLeft := storage.Get(ctx, ticksKey).(int) + 1
|
|
|
|
storage.Put(ctx, ticksKey, ticksLeft)
|
|
|
|
runtime.Log("One more tick is added.")
|
|
|
|
return
|
|
|
|
}
|
2020-12-13 15:26:35 +00:00
|
|
|
sh := runtime.GetCallingScriptHash()
|
|
|
|
storage.Put(ctx, mgmtKey, sh)
|
2020-10-10 17:17:04 +00:00
|
|
|
storage.Put(ctx, ticksKey, defaultTicks)
|
2021-03-04 10:26:16 +00:00
|
|
|
i := std.Itoa(defaultTicks, 10)
|
2020-11-10 12:21:57 +00:00
|
|
|
runtime.Log("Timer set to " + i + " ticks.")
|
2020-10-10 17:17:04 +00:00
|
|
|
}
|
|
|
|
|
2021-09-20 18:56:06 +00:00
|
|
|
// Update updates the contract.
|
|
|
|
func Update(script []byte, manifest []byte) bool {
|
2020-10-10 17:17:04 +00:00
|
|
|
if !runtime.CheckWitness(owner) {
|
|
|
|
runtime.Log("Only owner is allowed to update the contract.")
|
|
|
|
return false
|
|
|
|
}
|
2020-12-13 15:26:35 +00:00
|
|
|
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-10 17:17:04 +00:00
|
|
|
runtime.Log("Contract updated.")
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tick decrement ticks count and checks whether the timer is fired.
|
|
|
|
func Tick() bool {
|
|
|
|
runtime.Log("Tick-tock.")
|
|
|
|
ticksLeft := storage.Get(ctx, ticksKey)
|
|
|
|
ticksLeft = ticksLeft.(int) - 1
|
|
|
|
if ticksLeft == 0 {
|
|
|
|
runtime.Log("Fired!")
|
2020-12-29 10:44:07 +00:00
|
|
|
return contract.Call(runtime.GetExecutingScriptHash(), "selfDestroy", contract.All).(bool)
|
2020-10-10 17:17:04 +00:00
|
|
|
}
|
|
|
|
storage.Put(ctx, ticksKey, ticksLeft)
|
2021-03-04 10:26:16 +00:00
|
|
|
i := std.Itoa(ticksLeft.(int), 10)
|
2020-11-10 12:21:57 +00:00
|
|
|
runtime.Log(i + " ticks left.")
|
2020-10-10 17:17:04 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// SelfDestroy destroys the contract.
|
|
|
|
func SelfDestroy() bool {
|
|
|
|
if !(runtime.CheckWitness(owner) || runtime.CheckWitness(runtime.GetExecutingScriptHash())) {
|
|
|
|
runtime.Log("Only owner or the contract itself are allowed to destroy the contract.")
|
|
|
|
return false
|
|
|
|
}
|
2020-12-13 15:26:35 +00:00
|
|
|
mgmt := storage.Get(ctx, mgmtKey).(interop.Hash160)
|
2020-12-29 10:44:07 +00:00
|
|
|
contract.Call(mgmt, "destroy", contract.All)
|
2020-10-10 17:17:04 +00:00
|
|
|
runtime.Log("Destroyed.")
|
|
|
|
return true
|
|
|
|
}
|