Skip to content

Commit

Permalink
chore(repo): update ECR20 labels in prometheus (#17854)
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaodino authored Jul 27, 2024
1 parent 1fd907c commit 26f4a2f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 14 deletions.
12 changes: 8 additions & 4 deletions packages/balance-monitor/balance-monitor/balance_monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,14 @@ func (b *BalanceMonitor) Start() error {
b.checkEthBalance(context.Background(), b.l2EthClient, l2EthBalanceGauge, "L2", address)

// Check ERC-20 token balances
var balance float64 = 0
for _, tokenAddress := range b.erc20Addresses {
b.checkErc20Balance(context.Background(), b.l1EthClient, l1Erc20BalanceGauge, "L1", tokenAddress, address)
b.checkErc20Balance(context.Background(), b.l2EthClient, l2Erc20BalanceGauge, "L2", tokenAddress, address)
balance = balance + b.checkErc20Balance(context.Background(), b.l1EthClient, "L1", tokenAddress, address)
balance = balance + b.checkErc20Balance(context.Background(), b.l2EthClient, "L2", tokenAddress, address)

}
l1Erc20BalanceGauge.WithLabelValues(address.Hex()).Set(balance)
slog.Info("ERC-20 Balance", "address", address.Hex(), "balance", balance)
// Add a 1 second sleep between address checks
time.Sleep(time.Second)
}
Expand All @@ -119,7 +123,7 @@ func (b *BalanceMonitor) checkEthBalance(ctx context.Context, client ethClient,
slog.Info(fmt.Sprintf("%s ETH Balance", clientLabel), "address", address.Hex(), "balance", balanceFloat)
}

func (b *BalanceMonitor) checkErc20Balance(ctx context.Context, client ethClient, gauge *prometheus.GaugeVec, clientLabel string, tokenAddress, holderAddress common.Address) {
func (b *BalanceMonitor) checkErc20Balance(ctx context.Context, client ethClient, clientLabel string, tokenAddress, holderAddress common.Address) float64 {
// Check the cache for the token decimals
tokenDecimals, ok := b.erc20DecimalsCache[tokenAddress]
if !ok {
Expand Down Expand Up @@ -152,8 +156,8 @@ func (b *BalanceMonitor) checkErc20Balance(ctx context.Context, client ethClient
}

balance := tokenBalanceFloat + tokenBondBalanceFloat
gauge.WithLabelValues(tokenAddress.Hex(), holderAddress.Hex()).Set(balance)
slog.Info(fmt.Sprintf("%s ERC-20 Balance", clientLabel), "tokenAddress", tokenAddress.Hex(), "address", holderAddress.Hex(), "balance", balance)
return balance
}

const erc20BalanceOfABI = `[{"constant":true,"inputs":[{"name":"_owner","type":"address"}],"name":"balanceOf","outputs":[{"name":"balance","type":"uint256"}],"type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint8"}],"type":"function"}]`
Expand Down
12 changes: 2 additions & 10 deletions packages/balance-monitor/balance-monitor/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ var (
l1Erc20BalanceGauge = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "l1_erc20_balance",
Help: "ERC-20 token balance of addresses on L1",
Help: "ERC-20 token balance of addresses",
},
[]string{"token_address", "address"},
[]string{"address"},
)
l2EthBalanceGauge = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Expand All @@ -26,18 +26,10 @@ var (
},
[]string{"address"},
)
l2Erc20BalanceGauge = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "l2_erc20_balance",
Help: "ERC-20 token balance of addresses on L2",
},
[]string{"token_address", "address"},
)
)

func init() {
prometheus.MustRegister(l1EthBalanceGauge)
prometheus.MustRegister(l2EthBalanceGauge)
prometheus.MustRegister(l1Erc20BalanceGauge)
prometheus.MustRegister(l2Erc20BalanceGauge)
}

0 comments on commit 26f4a2f

Please sign in to comment.