Skip to content

Commit

Permalink
feat: add token-exponent option (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
MattKetmo authored Oct 9, 2023
1 parent 0b27176 commit bf28411
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
5 changes: 5 additions & 0 deletions pkg/app/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ var Flags = []cli.Flag{
Name: "no-staking",
Usage: "disable calls to staking module (useful for consumer chains)",
},
&cli.UintFlag{
Name: "token-exponent",
Usage: "token exponent (ie. 6 for uatom)",
Value: 6,
},
&cli.StringSliceFlag{
Name: "validator",
Usage: "validator address(es) to track (use :my-label to add a custom label in metrics & ouput)",
Expand Down
5 changes: 4 additions & 1 deletion pkg/app/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func RunFunc(cCtx *cli.Context) error {
nodes = cCtx.StringSlice("node")
noGov = cCtx.Bool("no-gov")
noStaking = cCtx.Bool("no-staking")
tokenExpon = cCtx.Uint("token-exponent")
validators = cCtx.StringSlice("validator")
xGov = cCtx.String("x-gov")
)
Expand Down Expand Up @@ -95,7 +96,9 @@ func RunFunc(cCtx *cli.Context) error {
// Pool watchers
//
if !noStaking {
validatorsWatcher := watcher.NewValidatorsWatcher(trackedValidators, metrics, pool)
validatorsWatcher := watcher.NewValidatorsWatcher(trackedValidators, metrics, pool, watcher.ValidatorsWatcherOptions{
TokenExponent: tokenExpon,
})
errg.Go(func() error {
return validatorsWatcher.Start(ctx)
})
Expand Down
17 changes: 14 additions & 3 deletions pkg/watcher/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,19 @@ type ValidatorsWatcher struct {
metrics *metrics.Metrics
validators []TrackedValidator
pool *rpc.Pool
opts ValidatorsWatcherOptions
}

func NewValidatorsWatcher(validators []TrackedValidator, metrics *metrics.Metrics, pool *rpc.Pool) *ValidatorsWatcher {
type ValidatorsWatcherOptions struct {
TokenExponent uint
}

func NewValidatorsWatcher(validators []TrackedValidator, metrics *metrics.Metrics, pool *rpc.Pool, opts ValidatorsWatcherOptions) *ValidatorsWatcher {
return &ValidatorsWatcher{
metrics: metrics,
validators: validators,
pool: pool,
opts: opts,
}
}

Expand Down Expand Up @@ -71,9 +77,14 @@ func (w *ValidatorsWatcher) handleValidators(chainID string, validators []stakin
// Sort validators by tokens & status (bonded, unbonded, jailed)
sort.Sort(RankedValidators(validators))

tokenExponent := w.opts.TokenExponent
if tokenExponent == 0 {
tokenExponent = 1
}

seatPrice := decimal.Zero
for _, val := range validators {
tokens := decimal.NewFromBigInt(val.Tokens.BigInt(), -6)
tokens := decimal.NewFromBigInt(val.Tokens.BigInt(), -int32(tokenExponent))
if val.Status == staking.Bonded && (seatPrice.IsZero() || seatPrice.GreaterThan(tokens)) {
seatPrice = tokens
}
Expand All @@ -92,7 +103,7 @@ func (w *ValidatorsWatcher) handleValidators(chainID string, validators []stakin
rank = i + 1
isBonded = val.Status == staking.Bonded
isJailed = val.Jailed
tokens = decimal.NewFromBigInt(val.Tokens.BigInt(), -6)
tokens = decimal.NewFromBigInt(val.Tokens.BigInt(), -int32(tokenExponent))
)

w.metrics.Rank.WithLabelValues(chainID, address, name).Set(float64(rank))
Expand Down
3 changes: 3 additions & 0 deletions pkg/watcher/validators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ func TestValidatorsWatcher(t *testing.T) {
},
metrics.New("cosmos_validator_watcher"),
nil,
ValidatorsWatcherOptions{
TokenExponent: 6,
},
)

t.Run("Handle Validators", func(t *testing.T) {
Expand Down

0 comments on commit bf28411

Please sign in to comment.