Skip to content

Commit

Permalink
[goreleaser] add balance gauge
Browse files Browse the repository at this point in the history
  • Loading branch information
trajan0x committed Jun 28, 2024
1 parent 9475162 commit fb82cfc
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
30 changes: 30 additions & 0 deletions ethergo/submitter/chain_queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package submitter
import (
"context"
"fmt"
"github.com/ethereum/go-ethereum/params"
"math/big"
"sort"
"sync"
Expand Down Expand Up @@ -84,7 +85,9 @@ func (t *txSubmitterImpl) chainPendingQueue(parentCtx context.Context, chainID *
if err != nil {
return fmt.Errorf("could not get gas balance: %w", err)
}
t.currentGasBalances.Set(uint32(chainID.Int64()), core.CopyBigInt(gasBalance))
span.SetAttributes(attribute.String("gas_balance", gasBalance.String()))

for i := range txes {
tx := txes[i]

Expand Down Expand Up @@ -173,6 +176,33 @@ func (t *txSubmitterImpl) recordNonces(_ context.Context, observer metric.Observ
return nil
}

func (t *txSubmitterImpl) recordBalance(_ context.Context, observer metric.Observer) (err error) {

Check failure on line 179 in ethergo/submitter/chain_queue.go

View workflow job for this annotation

GitHub Actions / Lint (ethergo)

`(*txSubmitterImpl).recordBalance` - result `err` is always `nil` (unparam)
if t.metrics == nil || t.gasBalanceGauge == nil {
return nil
}

t.currentGasBalances.Range(func(chainID uint32, gasPrice *big.Int) bool {
opts := metric.WithAttributes(
attribute.Int(metrics.ChainID, int(chainID)),
attribute.String("wallet", t.signer.Address().Hex()),
)

observer.ObserveFloat64(t.gasBalanceGauge, toFloat(gasPrice), opts)
return true
})

return nil
}

func toFloat(wei *big.Int) float64 {

Check failure on line 197 in ethergo/submitter/chain_queue.go

View workflow job for this annotation

GitHub Actions / Lint (ethergo)

func `toFloat` is unused (unused)
// Convert wei to float64
weiFloat := new(big.Float).SetInt(wei)
weiAsFloat64, _ := weiFloat.Float64()

// Perform the division to convert wei to ether
return weiAsFloat64 / params.Ether
}

// storeAndSubmit stores the txes in the database and submits them to the chain.
func (c *chainQueue) storeAndSubmit(ctx context.Context, calls []w3types.Caller, span trace.Span) {
var wg sync.WaitGroup
Expand Down
10 changes: 9 additions & 1 deletion ethergo/submitter/submitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,14 @@ type txSubmitterImpl struct {
numPendingGauge metric.Int64ObservableGauge
// nonceGauge is the gauge for the current nonce.
nonceGauge metric.Int64ObservableGauge
// gasBalanceGauge is the gauge for the gas balance.
gasBalanceGauge metric.Float64ObservableGauge
// numPendingTxes is used for metrics.
numPendingTxes *hashmap.Map[uint32, int]
// currentNonces is used for metrics.
currentNonces *hashmap.Map[uint32, uint64]
// currentGasBalance is used for metrics.
currentGasBalances *hashmap.Map[uint32, *big.Int]
}

// ClientFetcher is the interface for fetching a chain client.
Expand All @@ -97,7 +101,6 @@ type ClientFetcher interface {

// NewTransactionSubmitter creates a new transaction submitter.
func NewTransactionSubmitter(metrics metrics.Handler, signer signer.Signer, fetcher ClientFetcher, db db.Service, config config.IConfig) TransactionSubmitter {

return &txSubmitterImpl{
db: db,
config: config,
Expand Down Expand Up @@ -176,6 +179,11 @@ func (t *txSubmitterImpl) setupMetrics() (err error) {
return fmt.Errorf("could not create nonce gauge: %w", err)
}

t.gasBalanceGauge, err = t.meter.Float64ObservableGauge("gas_balance")
if err != nil {
return fmt.Errorf("could not create nonce gauge: %w", err)
}

_, err = t.meter.RegisterCallback(t.recordNonces, t.nonceGauge)
if err != nil {
return fmt.Errorf("could not register callback: %w", err)
Expand Down

0 comments on commit fb82cfc

Please sign in to comment.