-
Notifications
You must be signed in to change notification settings - Fork 33
/
provider_helpers.go
217 lines (183 loc) · 6.06 KB
/
provider_helpers.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
package data
import (
"context"
"fmt"
"math/big"
"strconv"
"time"
"github.com/omni-network/omni/explorer/db/ent"
"github.com/omni-network/omni/explorer/graphql/uintconv"
"github.com/omni-network/omni/lib/errors"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/graph-gophers/graphql-go"
"github.com/graph-gophers/graphql-go/relay"
)
// EntBlockToGraphQLBlock converts an ent.Block to a XBlock.
func EntBlockToGraphQLBlock(_ context.Context, block *ent.Block) (*XBlock, error) {
sourceChainIDBig, err := uintconv.ToBig(block.ChainID)
if err != nil {
return nil, errors.Wrap(err, "decoding source chain id")
}
blockHeight, err := uintconv.ToBig(block.Height)
if err != nil {
return nil, errors.Wrap(err, "decoding block height")
}
res := XBlock{
ID: relay.MarshalID("xblock", block.ID),
ChainID: hexutil.Big(sourceChainIDBig),
Height: hexutil.Big(blockHeight),
Hash: common.Hash(block.Hash),
Timestamp: graphql.Time{Time: block.Timestamp},
}
// Decode messages
for _, msg := range block.Edges.Msgs {
msg, err := EntMsgToGraphQLXMsg(msg)
if err != nil {
return nil, errors.Wrap(err, "decoding msg for block")
}
res.Messages = append(res.Messages, *msg)
}
return &res, nil
}
// EntMsgToGraphQLXMsg converts an ent.Msg to a XMsg.
func EntMsgToGraphQLXMsg(msg *ent.Msg) (*XMsg, error) {
sourceChainIDBig, err := uintconv.ToBig(msg.SourceChainID)
if err != nil {
return nil, errors.Wrap(err, "decoding source chain id")
}
destChainIDBig, err := uintconv.ToBig(msg.DestChainID)
if err != nil {
return nil, errors.Wrap(err, "decoding source chain id")
}
destGasLimit, err := uintconv.ToBig(msg.GasLimit)
if err != nil {
return nil, errors.Wrap(err, "decoding dest gas limit")
}
offset, err := uintconv.ToBig(msg.Offset)
if err != nil {
return nil, errors.Wrap(err, "decoding stream offset")
}
xmsg := &XMsg{
ID: relay.MarshalID("xmsg", msg.ID),
Sender: common.Address(msg.Sender),
SourceChainID: hexutil.Big(sourceChainIDBig),
To: common.Address(msg.To),
GasLimit: hexutil.Big(destGasLimit),
DestChainID: hexutil.Big(destChainIDBig),
Offset: hexutil.Big(offset),
TxHash: common.Hash(msg.TxHash),
Data: msg.Data,
Status: Status(msg.Status),
}
return xmsg, nil
}
// EntReceiptToGraphQLXReceipt converts an ent.Receipt to a XReceipt.
func EntReceiptToGraphQLXReceipt(ctx context.Context, receipt *ent.Receipt, block *ent.Block) (*XReceipt, error) {
gasUsed, err := uintconv.ToBig(receipt.GasUsed)
if err != nil {
return nil, errors.Wrap(err, "decoding gas used")
}
sourceChainIDBig, err := uintconv.ToBig(receipt.SourceChainID)
if err != nil {
return nil, errors.Wrap(err, "decoding source chain id")
}
destChainIDBig, err := uintconv.ToBig(receipt.DestChainID)
if err != nil {
return nil, errors.Wrap(err, "decoding source chain id")
}
streamOffset, err := uintconv.ToBig(receipt.Offset)
if err != nil {
return nil, errors.Wrap(err, "decoding stream offset")
}
// blockHeight, err := uintconv.ToBig(block.Height)
// if err != nil {
// return nil, errors.Wrap(err, "decoding block height")
// }
res := &XReceipt{
ID: relay.MarshalID("Receipt", receipt.ID),
Success: receipt.Success,
GasUsed: hexutil.Big(gasUsed),
Relayer: common.Address(receipt.RelayerAddress),
SourceChainID: hexutil.Big(sourceChainIDBig),
DestChainID: hexutil.Big(destChainIDBig),
Offset: hexutil.Big(streamOffset),
TxHash: common.Hash(receipt.TxHash),
Timestamp: graphql.Time{Time: receipt.CreatedAt},
}
return res, nil
}
func EntChainToGraphQLChain(chain *ent.Chain) Chain {
chainID, err := uintconv.ToBig(chain.ChainID)
if err != nil {
panic(errors.Wrap(err, "decoding chain id"))
}
return Chain{
Name: chain.Name,
ChainID: hexutil.Big(chainID),
DisplayID: Long(chain.ChainID),
}
}
// EntMsgToGraphQLXMsg converts an ent.Msg to a XMsg.
func EntMsgToGraphQLXMsgWithEdges(ctx context.Context, msg *ent.Msg) (*XMsg, error) {
block, err := msg.QueryBlock().Only(ctx)
if err != nil {
return nil, errors.Wrap(err, "querying block for message")
}
sourceChainIDBig, err := uintconv.ToBig(msg.SourceChainID)
if err != nil {
return nil, errors.Wrap(err, "decoding source chain id")
}
destChainIDBig, err := uintconv.ToBig(msg.DestChainID)
if err != nil {
return nil, errors.Wrap(err, "decoding source chain id")
}
destGasLimit, err := uintconv.ToBig(msg.GasLimit)
if err != nil {
return nil, errors.Wrap(err, "decoding dest gas limit")
}
streamOffset, err := uintconv.ToBig(msg.Offset)
if err != nil {
return nil, errors.Wrap(err, "decoding stream offset")
}
receipts, err := msg.QueryReceipts().All(ctx)
if err != nil {
return nil, errors.Wrap(err, "querying receipts for message")
}
var xreceipt *XReceipt
var rts time.Time
for _, r := range receipts {
rec, err := EntReceiptToGraphQLXReceipt(ctx, r, block)
if err != nil {
return nil, errors.Wrap(err, "decoding receipt for message")
}
// only use the latest receipt if more than one is available
if r.CreatedAt.After(rts) {
rts = r.CreatedAt
xreceipt = rec
}
}
b := msg.QueryBlock().OnlyX(ctx)
xblock := XBlock{
ChainID: hexutil.Big(sourceChainIDBig),
Height: hexutil.Big(*big.NewInt(int64(b.Height))),
Hash: common.Hash(b.Hash),
Timestamp: graphql.Time{Time: block.Timestamp},
}
xmsg := &XMsg{
ID: graphql.ID(strconv.Itoa(msg.ID)),
Sender: common.Address(msg.Sender),
SourceChainID: hexutil.Big(sourceChainIDBig),
To: common.Address(msg.To),
GasLimit: hexutil.Big(destGasLimit),
DestChainID: hexutil.Big(destChainIDBig),
Offset: hexutil.Big(streamOffset),
TxHash: common.Hash(msg.TxHash),
Data: msg.Data,
Receipt: xreceipt,
Block: xblock,
Status: MustParseStatus(msg.Status),
}
xmsg.DisplayID = fmt.Sprintf("%d-%d-%d", xmsg.SourceChainID.ToInt().Int64(), xmsg.DestChainID.ToInt().Int64(), xmsg.Offset.ToInt().Int64())
return xmsg, nil
}