-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathquerier.go
84 lines (75 loc) · 2.41 KB
/
querier.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package executor
import (
"strconv"
"github.com/gofiber/fiber/v2"
"github.com/initia-labs/opinit-bots/types"
"github.com/pkg/errors"
"go.uber.org/zap"
)
func (ex *Executor) RegisterQuerier(ctx types.Context) {
ex.server.RegisterQuerier("/withdrawal/:sequence", func(c *fiber.Ctx) error {
sequenceStr := c.Params("sequence")
if sequenceStr == "" {
return errors.New("sequence is required")
}
sequence, err := strconv.ParseUint(sequenceStr, 10, 64)
if err != nil {
return errors.Wrap(err, "failed to parse sequence")
}
res, err := ex.child.QueryWithdrawal(sequence)
if err != nil {
ctx.Logger().Error("failed to query withdrawal", zap.Error(err))
return errors.New("failed to query withdrawal")
}
return c.JSON(res)
})
ex.server.RegisterQuerier("/withdrawals/:address", func(c *fiber.Ctx) error {
address := c.Params("address")
if address == "" {
return errors.New("address is required")
}
offset := c.QueryInt("offset", 0)
uoffset, err := types.SafeInt64ToUint64(int64(offset))
if err != nil {
return errors.Wrap(err, "failed to convert offset")
}
limit := c.QueryInt("limit", 10)
if limit > 100 {
limit = 100
}
ulimit, err := types.SafeInt64ToUint64(int64(limit))
if err != nil {
return errors.Wrap(err, "failed to convert limit")
}
descOrder := true
orderStr := c.Query("order", "desc")
if orderStr == "asc" {
descOrder = false
}
res, err := ex.child.QueryWithdrawals(address, uoffset, ulimit, descOrder)
if err != nil {
ctx.Logger().Error("failed to query withdrawals", zap.Error(err))
return errors.New("failed to query withdrawals")
}
return c.JSON(res)
})
ex.server.RegisterQuerier("/status", func(c *fiber.Ctx) error {
status, err := ex.GetStatus()
if err != nil {
ctx.Logger().Error("failed to query status", zap.Error(err))
return errors.New("failed to query status")
}
return c.JSON(status)
})
ex.server.RegisterQuerier("/syncing", func(c *fiber.Ctx) error {
status, err := ex.GetStatus()
if err != nil {
ctx.Logger().Error("failed to query status", zap.Error(err))
return errors.New("failed to query status")
}
hostSync := status.Host.Node.Syncing != nil && *status.Host.Node.Syncing
childSync := status.Child.Node.Syncing != nil && *status.Child.Node.Syncing
batchSync := status.BatchSubmitter.Node.Syncing != nil && *status.BatchSubmitter.Node.Syncing
return c.JSON(hostSync || childSync || batchSync)
})
}