Skip to content

Commit b2aa317

Browse files
committed
feat: add names to wallets
1 parent 87397b0 commit b2aa317

File tree

5 files changed

+26
-13
lines changed

5 files changed

+26
-13
lines changed

cmd/serve.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"fmt"
45
"slices"
56
"strings"
67
"time"
@@ -78,7 +79,18 @@ func CommandServe(cfg *config.Config) *cli.Command {
7879
Flags: flags,
7980

8081
Before: func(_ *cli.Context) error {
81-
cfg.Eth.WalletAddresses = walletAddresses.Value()
82+
_walletAddresses := make(map[string]string, len(walletAddresses.Value()))
83+
for _, wa := range walletAddresses.Value() {
84+
parts := strings.Split(wa, "=")
85+
if len(parts) != 2 {
86+
return fmt.Errorf("invalid wallet address (mush be like `name=0xNNNN`): %s", wa)
87+
}
88+
name := strings.TrimSpace(parts[0])
89+
addr := strings.TrimSpace(parts[1])
90+
_walletAddresses[name] = addr
91+
}
92+
93+
cfg.Eth.WalletAddresses = _walletAddresses
8294
return cfg.Validate()
8395
},
8496

config/eth.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ import (
1010
)
1111

1212
type Eth struct {
13-
BlockTime time.Duration `yaml:"block_time"`
14-
BuilderAddress string `yaml:"builder_address"`
15-
ReorgWindow int `yaml:"reorg_window"`
16-
RPC string `yaml:"rpc"`
17-
WalletAddresses []string `yaml:"wallet_addresses"`
13+
BlockTime time.Duration `yaml:"block_time"`
14+
BuilderAddress string `yaml:"builder_address"`
15+
ReorgWindow int `yaml:"reorg_window"`
16+
RPC string `yaml:"rpc"`
17+
WalletAddresses map[string]string `yaml:"wallet_addresses"`
1818
}
1919

2020
const (

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ go run github.com/flashbots/chain-monitor/cmd serve \
77
--eth-block-time 1s \
88
--eth-rpc http://127.0.0.1:8645 \
99
--eth-builder-address 0xdD11751cdD3f6EFf01B1f6151B640685bfa5dB4a \
10-
--eth-monitor-wallets 0xdD11751cdD3f6EFf01B1f6151B640685bfa5dB4a
10+
--eth-monitor-wallets builder=0xdD11751cdD3f6EFf01B1f6151B640685bfa5dB4a
1111
```
1212

1313
```shell

server/eth.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ func (s *Server) hasBuilderTx(ctx context.Context, block *ethtypes.Block) bool {
162162
func (s *Server) observeWallets(ctx context.Context, o otelapi.Observer) error {
163163
errs := make([]error, 0)
164164

165-
for _, addr := range s.wallets {
165+
for name, addr := range s.wallets {
166166
_balance, err := s.eth.BalanceAt(ctx, addr, nil)
167167
if err != nil {
168168
errs = append(errs, err)
@@ -172,7 +172,8 @@ func (s *Server) observeWallets(ctx context.Context, o otelapi.Observer) error {
172172
balance, _ := _balance.Float64()
173173

174174
o.ObserveFloat64(metrics.WalletBalance, balance, otelapi.WithAttributes(
175-
attribute.KeyValue{Key: "address", Value: attribute.StringValue(addr.String())},
175+
attribute.KeyValue{Key: "wallet_address", Value: attribute.StringValue(addr.String())},
176+
attribute.KeyValue{Key: "wallet_name", Value: attribute.StringValue(name)},
176177
))
177178
}
178179

server/server.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ type Server struct {
4141
blocksLanded int64
4242

4343
blocks *types.RingBuffer[bool]
44-
wallets []ethcommon.Address
44+
wallets map[string]ethcommon.Address
4545
}
4646

4747
func New(cfg *config.Config) (*Server, error) {
4848
var (
4949
builderAddr ethcommon.Address
50-
wallets = make([]ethcommon.Address, 0, len(cfg.Eth.WalletAddresses))
50+
wallets = make(map[string]ethcommon.Address, len(cfg.Eth.WalletAddresses))
5151
)
5252

5353
{ // builder address
@@ -61,7 +61,7 @@ func New(cfg *config.Config) (*Server, error) {
6161
copy(builderAddr[:], addr)
6262
}
6363

64-
for _, wa := range cfg.Eth.WalletAddresses {
64+
for name, wa := range cfg.Eth.WalletAddresses {
6565
_addr, err := ethcommon.ParseHexOrString(wa)
6666
if err != nil {
6767
return nil, err
@@ -71,7 +71,7 @@ func New(cfg *config.Config) (*Server, error) {
7171
}
7272
var addr ethcommon.Address
7373
copy(addr[:], _addr)
74-
wallets = append(wallets, addr)
74+
wallets[name] = addr
7575
}
7676

7777
eth, err := ethclient.Dial(cfg.Eth.RPC)

0 commit comments

Comments
 (0)