-
Notifications
You must be signed in to change notification settings - Fork 33
/
types.go
256 lines (210 loc) · 4.8 KB
/
types.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package data
import (
"fmt"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/graph-gophers/graphql-go"
)
const StatusParseErr = StatusErr("Invalid status")
type StatusErr string
func (s StatusErr) Error() string {
return string(s)
}
// Define the Go struct for the Status enum type.
type Status string
func ParseStatus(s string) (Status, error) {
switch s {
case string(StatusFailed):
return StatusFailed, nil
case string(StatusPending):
return StatusPending, nil
case string(StatusSuccess):
return StatusSuccess, nil
default:
return Status(""), StatusParseErr
}
}
func MustParseStatus(s string) Status {
st, err := ParseStatus(s)
if err != nil {
panic(err)
}
return st
}
const (
StatusFailed Status = "FAILED"
StatusPending Status = "PENDING"
StatusSuccess Status = "SUCCESS"
)
type Chainer interface {
Chain(id string) (Chain, bool)
}
// Define the Go struct for the XMsg type.
type XMsg struct {
Chainer
ID graphql.ID
Block XBlock
To common.Address
Data hexutil.Bytes
DestChainID hexutil.Big
GasLimit hexutil.Big
DisplayID string
Offset hexutil.Big
Receipt *XReceipt
Sender common.Address
SourceChainID hexutil.Big
Status Status
TxHash common.Hash
}
func (m XMsg) ToURL() string {
c, ok := m.Chain(m.DestChainID.String())
if !ok {
return ""
}
return c.AddrUrl(m.To)
}
func (m XMsg) SenderURL() string {
c, ok := m.Chain(m.SourceChainID.String())
if !ok {
return ""
}
return c.AddrUrl(m.Sender)
}
func (m XMsg) TxURL() string {
c, ok := m.Chain(m.SourceChainID.String())
if !ok {
return ""
}
return c.TxUrl(m.TxHash)
}
func (m XMsg) SourceChain() (Chain, error) {
c, ok := m.Chain(m.SourceChainID.String())
if !ok {
return Chain{}, fmt.Errorf("chain not found: %s", m.SourceChainID.String())
}
return c, nil
}
func (m XMsg) DestChain() (Chain, error) {
c, ok := m.Chain(m.DestChainID.String())
if !ok {
return Chain{}, fmt.Errorf("chain not found: %s", m.DestChainID.String())
}
return c, nil
}
// Define the Go struct for the XBlock type.
type XBlock struct {
Chainer
ID graphql.ID
ChainID hexutil.Big
Height hexutil.Big
Hash common.Hash
Messages []XMsg
Timestamp graphql.Time
}
func (b XBlock) Chain() (Chain, error) {
c, ok := b.Chainer.Chain(b.ChainID.String())
if !ok {
return Chain{}, fmt.Errorf("chain not found: %s", b.ChainID.String())
}
return c, nil
}
func (b XBlock) URL() (string, error) {
c, ok := b.Chainer.Chain(b.ChainID.String())
if !ok {
return "", fmt.Errorf("chain not found: %s", b.ChainID.String())
}
return c.BlockURL(b.Height.ToInt().Uint64()), nil
}
// Define the Go struct for the XReceipt type.
type XReceipt struct {
Chainer
ID graphql.ID
GasUsed hexutil.Big
Success bool
Relayer common.Address
SourceChainID hexutil.Big
DestChainID hexutil.Big
Offset hexutil.Big
TxHash common.Hash
Timestamp graphql.Time
RevertReason *string
}
func (r *XReceipt) TxURL() string {
c, ok := r.Chain(r.SourceChainID.String())
if !ok {
return ""
}
return c.TxUrl(r.TxHash)
}
func (r *XReceipt) SourceChain() (Chain, error) {
c, ok := r.Chain(r.SourceChainID.String())
if !ok {
return Chain{}, fmt.Errorf("chain not found: %s", r.SourceChainID.String())
}
return c, nil
}
func (r *XReceipt) DestChain() (Chain, error) {
c, ok := r.Chain(r.DestChainID.String())
if !ok {
return Chain{}, fmt.Errorf("chain not found: %s", r.DestChainID.String())
}
return c, nil
}
// Define the Go struct for the Chain type.
type Chain struct {
AddrURLFmt string
BlockURLFmt string
TxURLFmt string
ID graphql.ID
ChainID hexutil.Big
DisplayID Long
Name string
LogoURL string
}
func (c *Chain) AddrUrl(addr common.Address) string {
if c.AddrURLFmt == "" {
return ""
}
return fmt.Sprintf(c.AddrURLFmt, addr)
}
func (c *Chain) BlockURL(height uint64) string {
if c.BlockURLFmt == "" {
return ""
}
return fmt.Sprintf(c.BlockURLFmt, height)
}
func (c *Chain) TxUrl(tx common.Hash) string {
if c.TxURLFmt == "" {
return ""
}
return fmt.Sprintf(c.TxURLFmt, tx.String())
}
// Define the Go struct for the XMsgConnection type.
type XMsgConnection struct {
TotalCount Long
Edges []XMsgEdge
PageInfo PageInfo
}
// Define the Go struct for the XMsgEdge type.
type XMsgEdge struct {
Cursor graphql.ID
Node XMsg
}
// Define the Go struct for the PageInfo type.
type PageInfo struct {
HasNextPage bool
HasPrevPage bool
TotalPages Long
CurrentPage Long
}
// Define the Go struct for the StatsResult type.
type StatsResult struct {
TotalMsgs Long
TopStreams []StreamStats
}
// Define the Go struct for the StreamStats type.
type StreamStats struct {
SourceChain Chain
DestChain Chain
MsgCount Long
}