-
Notifications
You must be signed in to change notification settings - Fork 23
/
bridge.go
335 lines (272 loc) · 10.5 KB
/
bridge.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package exporters
import (
"context"
"errors"
"fmt"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/lmittmann/w3/module/eth"
"github.com/lmittmann/w3/w3types"
"github.com/synapsecns/sanguine/contrib/promexporter/internal/decoders"
"github.com/synapsecns/sanguine/core"
"github.com/synapsecns/sanguine/core/metrics"
ethergoClient "github.com/synapsecns/sanguine/ethergo/client"
"github.com/synapsecns/sanguine/services/explorer/contracts/bridge"
"github.com/synapsecns/sanguine/services/explorer/contracts/bridgeconfig"
"github.com/synapsecns/sanguine/services/explorer/contracts/swap"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/trace"
"golang.org/x/sync/errgroup"
"math/big"
"time"
)
func (e *exporter) getBridgeConfig(ctx context.Context) (*bridgeconfig.BridgeConfigRef, error) {
client, err := e.omnirpcClient.GetConfirmationsClient(ctx, e.cfg.BridgeConfig.ChainID, 1)
if err != nil {
return nil, fmt.Errorf("could not get confirmations client: %w", err)
}
// note this will not update
configContract, err := bridgeconfig.NewBridgeConfigRef(common.HexToAddress(e.cfg.BridgeConfig.Address), client)
if err != nil {
return nil, fmt.Errorf("could not get bridge config contract: %w", err)
}
return configContract, nil
}
// Will be a lot faster w/: https://github.com/open-telemetry/opentelemetry-go/issues/3034
// nolint: cyclop
func (e *exporter) vpriceStats(ctx context.Context, chainID int, tokenID string) error {
meter := e.metrics.Meter(meterName)
vpriceMetric, err := meter.Float64ObservableGauge("vpriceMetric")
if err != nil {
return fmt.Errorf("could not create gauge: %w", err)
}
attributes := attribute.NewSet(attribute.Int(metrics.ChainID, chainID), attribute.String("tokenID", tokenID))
client, err := e.omnirpcClient.GetConfirmationsClient(ctx, chainID, 1)
if err != nil {
return fmt.Errorf("could not get confirmations client: %w", err)
}
bridgeConfig, err := e.getBridgeConfig(ctx)
if err != nil {
return err
}
token, err := bridgeConfig.GetToken(&bind.CallOpts{Context: ctx}, tokenID, big.NewInt(int64(chainID)))
if err != nil {
return fmt.Errorf("could not get token: %w", err)
}
poolConfig, err := bridgeConfig.GetPoolConfig(&bind.CallOpts{Context: ctx}, common.HexToAddress(token.TokenAddress), big.NewInt(int64(chainID)))
if err != nil {
return errPoolNotExist
}
// pool doesn't exist, no metrics to record!
if poolConfig.PoolAddress == common.BigToAddress(big.NewInt(0)) {
return errPoolNotExist
}
tokenContract, err := bridge.NewERC20(common.HexToAddress(token.TokenAddress), client)
if err != nil {
return fmt.Errorf("could not get tokenID contract: %w", err)
}
decimals, err := tokenContract.Decimals(&bind.CallOpts{Context: ctx})
if err != nil {
return fmt.Errorf("could not get decimals: %w", err)
}
iswap, err := swap.NewISwap(poolConfig.PoolAddress, client)
if err != nil {
return fmt.Errorf("could not get iswap contract: %w", err)
}
if _, err := meter.RegisterCallback(func(parentCtx context.Context, o metric.Observer) (err error) {
ctx, span := e.metrics.Tracer().Start(parentCtx, "vprice_stats", trace.WithAttributes(
attribute.Int(metrics.ChainID, chainID), attribute.String("tokenID", tokenID),
))
defer func() {
metrics.EndSpanWithErr(span, err)
}()
ctx, cancel := context.WithTimeout(ctx, time.Minute)
defer cancel()
realvPrice, err := iswap.GetVirtualPrice(&bind.CallOpts{Context: ctx})
if err != nil {
return fmt.Errorf("could not get virtual price: %w", err)
}
// Use floatVPrice as required
o.ObserveFloat64(vpriceMetric, core.BigToDecimals(realvPrice, decimals), metric.WithAttributeSet(attributes))
return nil
}, vpriceMetric); err != nil {
return fmt.Errorf("registering callback on instruments: %w", err)
}
return nil
}
var errPoolNotExist = errors.New("pool does not exist")
// nolint: cyclop
func (e *exporter) getTokenBalances(ctx context.Context) error {
allTokens, err := e.getAllTokens(ctx)
if err != nil {
return fmt.Errorf("could not get all tokens: %w", err)
}
for chainID, bridgeContract := range e.cfg.BridgeChecks {
chainID := chainID
bridgeContract := bridgeContract // capture func literals
meter := e.metrics.Meter(meterName)
bridgeBalanceMetric, err := meter.Float64ObservableGauge("bridgeBalanceMetric")
if err != nil {
return fmt.Errorf("could not create gauge: %w", err)
}
feeBalanceMetric, err := meter.Float64ObservableCounter("feeBalance")
if err != nil {
return fmt.Errorf("could not create counter: %w", err)
}
totalSupplyMetric, err := meter.Float64ObservableGauge("totalSupply")
if err != nil {
return fmt.Errorf("could not create gauge: %w", err)
}
gasBalanceMetric, err := meter.Float64ObservableGauge("gasBalance")
if err != nil {
return fmt.Errorf("could not create gauge: %w", err)
}
if _, err := meter.RegisterCallback(func(parentCtx context.Context, o metric.Observer) (err error) {
ctx, span := e.metrics.Tracer().Start(parentCtx, "tokenbalances", trace.WithAttributes(
attribute.Int(metrics.ChainID, chainID),
))
defer func() {
metrics.EndSpanWithErr(span, err)
}()
client, err := e.omnirpcClient.GetConfirmationsClient(ctx, chainID, 1)
if err != nil {
return fmt.Errorf("could not get confirmations client: %w", err)
}
var realGasBalance big.Int
calls := []w3types.Caller{
eth.Balance(common.HexToAddress(bridgeContract), nil).Returns(&realGasBalance),
}
type tokenData struct {
metadata TokenConfig
contractBalance *big.Int
totalSuppply *big.Int
feeBalance *big.Int
}
allTokenData := make([]tokenData, len(allTokens.GetForChainID(chainID)))
for i, tokenConfig := range allTokens.GetForChainID(chainID) {
// initialize empty struct
allTokenData[i] = tokenData{
metadata: tokenConfig,
contractBalance: new(big.Int),
totalSuppply: new(big.Int),
feeBalance: new(big.Int),
}
calls = append(calls,
eth.CallFunc(decoders.FuncBalanceOf(), tokenConfig.TokenAddress, common.HexToAddress(bridgeContract)).Returns(allTokenData[i].contractBalance),
eth.CallFunc(decoders.FuncTotalSupply(), tokenConfig.TokenAddress).Returns(allTokenData[i].totalSuppply),
eth.CallFunc(decoders.FuncFeeBalance(), common.HexToAddress(bridgeContract), tokenConfig.TokenAddress).Returns(allTokenData[i].feeBalance),
)
}
err = e.batchCalls(ctx, client, calls)
if err != nil {
return fmt.Errorf("could not get token balances: %w", err)
}
// eth is always 18 decimals
o.ObserveFloat64(gasBalanceMetric, core.BigToDecimals(&realGasBalance, 18), metric.WithAttributes(attribute.Int(metrics.ChainID, chainID)))
for _, td := range allTokenData {
tokenAttributes := attribute.NewSet(attribute.String("tokenID", td.metadata.TokenID), attribute.Int(metrics.ChainID, td.metadata.ChainID))
o.ObserveFloat64(bridgeBalanceMetric, core.BigToDecimals(td.contractBalance, td.metadata.TokenDecimals), metric.WithAttributeSet(tokenAttributes))
o.ObserveFloat64(feeBalanceMetric, core.BigToDecimals(td.feeBalance, td.metadata.TokenDecimals), metric.WithAttributeSet(tokenAttributes))
o.ObserveFloat64(totalSupplyMetric, core.BigToDecimals(td.totalSuppply, td.metadata.TokenDecimals), metric.WithAttributeSet(tokenAttributes))
}
return nil
}, bridgeBalanceMetric, feeBalanceMetric, totalSupplyMetric, gasBalanceMetric); err != nil {
return fmt.Errorf("could not register")
}
}
return nil
}
func (e *exporter) getAllTokens(parentCtx context.Context) (allTokens Tokens, err error) {
allTokens = []TokenConfig{}
ctx, span := e.metrics.Tracer().Start(parentCtx, "get_all_tokens")
defer func() {
metrics.EndSpanWithErr(span, err)
}()
bridgeConfig, err := e.getBridgeConfig(ctx)
if err != nil {
return nil, fmt.Errorf("could not get bridge config: %w", err)
}
// TODO: multicall is preferable here, but I ain't got time for that
tokenIDs, err := bridgeConfig.GetAllTokenIDs(&bind.CallOpts{Context: ctx})
if err != nil {
return nil, fmt.Errorf("could not get all token ids: %w", err)
}
bridgeConfigClient, err := e.omnirpcClient.GetConfirmationsClient(ctx, e.cfg.BridgeConfig.ChainID, 1)
if err != nil {
return nil, fmt.Errorf("could not get confirmations client: %w", err)
}
bridgeTokens := make([]*bridgeconfig.BridgeConfigV3Token, len(tokenIDs)*len(e.cfg.BridgeChecks))
tokenIDS := make([]string, len(tokenIDs)*len(e.cfg.BridgeChecks))
var calls []w3types.Caller
i := 0
for _, tokenID := range tokenIDs {
for chainID := range e.cfg.BridgeChecks {
token := &bridgeconfig.BridgeConfigV3Token{}
calls = append(calls, eth.CallFunc(decoders.TokenConfigGetToken(), bridgeConfig.Address(), tokenID, big.NewInt(int64(chainID))).Returns(token))
bridgeTokens[i] = token
tokenIDS[i] = tokenID
i++
}
}
// TODO: once go 1.21 is introduced do min(cfg.BatchCallLimit, 2)
err = e.batchCalls(ctx, bridgeConfigClient, calls)
if err != nil {
return nil, fmt.Errorf("could not get token balances: %w", err)
}
for i, token := range bridgeTokens {
tokenID := tokenIDS[i]
if token.TokenAddress == "" {
continue
}
allTokens = append(allTokens, TokenConfig{
TokenID: tokenID,
ChainID: int(token.ChainId.Int64()),
TokenAddress: common.HexToAddress(token.TokenAddress),
TokenDecimals: token.TokenDecimals,
HasUnderlying: token.HasUnderlying,
IsUnderlying: token.IsUnderlying,
})
}
return allTokens, nil
}
func (e *exporter) batchCalls(ctx context.Context, evmClient ethergoClient.EVM, calls []w3types.Caller) (err error) {
tasks := core.ChunkSlice(calls, e.cfg.BatchCallLimit)
g, ctx := errgroup.WithContext(ctx)
for _, task := range tasks {
task := task // capture func literal
g.Go(func() error {
err = evmClient.BatchWithContext(ctx, task...)
if err != nil {
return fmt.Errorf("could not batch calls: %w", err)
}
return nil
})
}
err = g.Wait()
if err != nil {
return fmt.Errorf("could not get token balances: %w", err)
}
return nil
}
// Tokens is a list of token configs.
type Tokens []TokenConfig
// GetForChainID returns all tokens for a given chainID.
func (t Tokens) GetForChainID(chainID int) Tokens {
var chainTokens []TokenConfig
for _, token := range t {
if token.ChainID == chainID {
chainTokens = append(chainTokens, token)
}
}
return chainTokens
}
// TokenConfig is a cleaned up token config.
type TokenConfig struct {
TokenID string
ChainID int
TokenAddress common.Address
TokenDecimals uint8
HasUnderlying bool
IsUnderlying bool
}