This repository has been archived by the owner on Sep 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
/
binance-smart-chain.go
175 lines (149 loc) · 4.88 KB
/
binance-smart-chain.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
package blockchain
import (
"encoding/json"
"math/big"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/prometheus/client_golang/prometheus"
"github.com/smartcontractkit/chainlink/core/logger"
"github.com/smartcontractkit/chainlink/core/store/models"
"github.com/smartcontractkit/external-initiator/store"
"github.com/smartcontractkit/external-initiator/subscriber"
)
const BSC = "binance-smart-chain"
// The bscManager implements the subscriber.JsonManager interface and allows
// for interacting with ETH nodes over RPC or WS.
type bscManager struct {
ethManager
}
// createBscManager creates a new instance of bscManager with the provided
// connection type and store.Subscription config.
func createBscManager(p subscriber.Type, config store.Subscription) bscManager {
return bscManager{
ethManager{
fq: createEvmFilterQuery(config.Job, config.BinanceSmartChain.Addresses),
p: p,
endpointName: config.EndpointName,
jobid: config.Job,
},
}
}
// GetTriggerJson generates a JSON payload to the ETH node
// using the config in bscManager.
//
// If bscManager is using WebSocket:
// Creates a new "eth_subscribe" subscription.
//
// If bscManager is using RPC:
// Sends a "eth_getLogs" request.
func (e bscManager) GetTriggerJson() []byte {
return e.ethManager.GetTriggerJson()
}
// GetTestJson generates a JSON payload to test
// the connection to the ETH node.
//
// If bscManager is using WebSocket:
// Returns nil.
//
// If bscManager is using RPC:
// Sends a request to get the latest block number.
func (e bscManager) GetTestJson() []byte {
return e.ethManager.GetTestJson()
}
// ParseTestResponse parses the response from the
// ETH node after sending GetTestJson(), and returns
// the error from parsing, if any.
//
// If bscManager is using WebSocket:
// Returns nil.
//
// If bscManager is using RPC:
// Attempts to parse the block number in the response.
// If successful, stores the block number in bscManager.
func (e bscManager) ParseTestResponse(data []byte) error {
return e.ethManager.ParseTestResponse(data)
}
// ParseResponse parses the response from the
// ETH node, and returns a slice of subscriber.Events
// and if the parsing was successful.
//
// If bscManager is using RPC:
// If there are new events, update bscManager with
// the latest block number it sees.
func (e bscManager) ParseResponse(data []byte) ([]subscriber.Event, bool) {
promLastSourcePing.With(prometheus.Labels{"endpoint": e.endpointName, "jobid": e.jobid}).SetToCurrentTime()
logger.Debugw("Parsing Binance Smart Chain response", "ExpectsMock", ExpectsMock)
var msg JsonrpcMessage
if err := json.Unmarshal(data, &msg); err != nil {
logger.Error("failed parsing JSON-RPC message:", msg)
return nil, false
}
var events []subscriber.Event
switch e.p {
case subscriber.WS:
var res ethSubscribeResponse
if err := json.Unmarshal(msg.Params, &res); err != nil {
logger.Error("unmarshal:", err)
return nil, false
}
var evt models.Log
if err := json.Unmarshal(res.Result, &evt); err != nil {
logger.Error("unmarshal:", err)
return nil, false
}
if evt.Removed {
return nil, false
}
request, err := logEventToOracleRequest(evt)
if err != nil {
logger.Error("failed to get oracle request:", err)
return nil, false
}
event, err := json.Marshal(request)
if err != nil {
logger.Error("marshal:", err)
return nil, false
}
events = append(events, event)
case subscriber.RPC:
var rawEvents []models.Log
if err := json.Unmarshal(msg.Result, &rawEvents); err != nil {
logger.Error("unmarshal:", err)
return nil, false
}
for _, evt := range rawEvents {
if evt.Removed {
continue
}
request, err := logEventToOracleRequest(evt)
if err != nil {
logger.Error("failed to get oracle request:", err)
return nil, false
}
event, err := json.Marshal(request)
if err != nil {
logger.Error("failed marshaling request:", err)
continue
}
events = append(events, event)
// Check if we can update the "fromBlock" in the query,
// so we only get new events from blocks we haven't queried yet
// Increment the block number by 1, since we want events from *after* this block number
curBlkn := &big.Int{}
curBlkn = curBlkn.Add(big.NewInt(int64(evt.BlockNumber)), big.NewInt(1))
fromBlkn, err := hexutil.DecodeBig(e.fq.FromBlock)
if err != nil && !(e.fq.FromBlock == "latest" || e.fq.FromBlock == "") {
logger.Error("Failed to get block number from event:", err)
continue
}
// If our query "fromBlock" is "latest", or our current "fromBlock" is in the past compared to
// the last event we received, we want to update the query
if e.fq.FromBlock == "latest" || e.fq.FromBlock == "" || curBlkn.Cmp(fromBlkn) > 0 {
e.fq.FromBlock = hexutil.EncodeBig(curBlkn)
}
}
default:
logger.Errorw(ErrSubscriberType.Error(), "type", e.p)
return nil, false
}
return events, true
}