-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathedit.go
57 lines (49 loc) · 1.17 KB
/
edit.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package oracle
import (
"github.com/fox-one/pando/core"
"github.com/fox-one/pando/pkg/maker"
"github.com/fox-one/pando/pkg/number"
"github.com/fox-one/pkg/logger"
"github.com/spf13/cast"
)
func HandleEdit(oracles core.OracleStore) maker.HandlerFunc {
return func(r *maker.Request) error {
ctx := r.Context()
if err := require(r.Gov(), "not-authorized"); err != nil {
return err
}
list, err := List(r, oracles)
if err != nil {
return err
}
for {
var key, value string
if err := require(r.Scan(&key, &value) == nil, "bad-data"); err != nil {
break
}
for _, oracle := range list {
switch key {
case "hop":
if ts := cast.ToInt64(value); ts > 0 {
oracle.Hop = ts
}
case "next":
if next := number.Decimal(value).Truncate(12); next.IsPositive() {
oracle.Next = next
}
case "threshold":
if threshold := cast.ToInt64(value); threshold >= 0 {
oracle.Threshold = threshold
}
}
}
}
for _, oracle := range list {
if err := oracles.Update(ctx, oracle, r.Version); err != nil {
logger.FromContext(ctx).WithError(err).Errorln("oracles.Update")
return err
}
}
return nil
}
}