Search Apps Documentation Source Content File Folder Download Copy Actions Download

operator.gno

1.23 Kb ยท 73 lines
 1package admin
 2
 3import (
 4	"chain"
 5	"chain/runtime"
 6)
 7
 8const (
 9	SetOperatorEvent   = "SetOperator"
10	UnsetOperatorEvent = "UnsetOperator"
11)
12
13var (
14	operatorAddrs map[address]bool = make(map[address]bool)
15)
16
17func init() {
18	caller := runtime.OriginCaller()
19	operatorAddrs[caller] = true
20}
21
22func SetOperator(cur realm, addr address) {
23	caller := runtime.OriginCaller()
24
25	if caller != admin {
26		panic("admin access required to set operator")
27	}
28
29	if !addr.IsValid() {
30		panic("invalid address")
31	}
32
33	operatorAddrs[addr] = true
34
35	chain.Emit(
36		SetOperatorEvent,
37		"admin", caller.String(),
38		"address", addr.String(),
39	)
40}
41
42func GetOperators() []address {
43	result := []address{}
44	for addr, active := range operatorAddrs {
45		if active {
46			result = append(result, addr)
47		}
48	}
49	return result
50}
51
52func UnsetOperator(cur realm, addr address) {
53	caller := runtime.OriginCaller()
54	if caller != admin {
55		panic("admin access required to unset operator")
56	}
57	if !addr.IsValid() {
58		panic("invalid address")
59	}
60	if !operatorAddrs[addr] {
61		panic("address is not an operator")
62	}
63	delete(operatorAddrs, addr)
64	chain.Emit(
65		UnsetOperatorEvent,
66		"admin", caller.String(),
67		"address", addr.String(),
68	)
69}
70
71func IsOperator(addr address) bool {
72	return operatorAddrs[addr]
73}