Skip to content

Commit

Permalink
chore: refactor denoms in rewards and self delegated fetcher
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno committed Jun 23, 2024
1 parent 6ebcb13 commit be14b4b
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 29 deletions.
4 changes: 2 additions & 2 deletions pkg/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ func NewApp(configPath string, filesystem fs.FS, version string) *App {
generatorsPkg.NewDelegationsGenerator(),
generatorsPkg.NewUnbondsGenerator(),
generatorsPkg.NewSigningInfoGenerator(),
generatorsPkg.NewRewardsGenerator(),
generatorsPkg.NewRewardsGenerator(appConfig.Chains),
generatorsPkg.NewBalanceGenerator(appConfig.Chains),
generatorsPkg.NewSelfDelegationGenerator(),
generatorsPkg.NewSelfDelegationGenerator(appConfig.Chains),

Check warning on line 114 in pkg/app.go

View check run for this annotation

Codecov / codecov/patch

pkg/app.go#L112-L114

Added lines #L112 - L114 were not covered by tests
generatorsPkg.NewValidatorsInfoGenerator(),
generatorsPkg.NewSingleValidatorInfoGenerator(appConfig.Chains, logger),
generatorsPkg.NewValidatorRankGenerator(appConfig.Chains, logger),
Expand Down
31 changes: 22 additions & 9 deletions pkg/generators/rewards.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package generators

import (
"main/pkg/config"
"main/pkg/constants"
fetchersPkg "main/pkg/fetchers"
statePkg "main/pkg/state"
Expand All @@ -9,10 +10,11 @@ import (
)

type RewardsGenerator struct {
Chains []*config.Chain
}

func NewRewardsGenerator() *RewardsGenerator {
return &RewardsGenerator{}
func NewRewardsGenerator(chains []*config.Chain) *RewardsGenerator {
return &RewardsGenerator{Chains: chains}
}

func (g *RewardsGenerator) Generate(state *statePkg.State) []prometheus.Collector {
Expand All @@ -31,14 +33,25 @@ func (g *RewardsGenerator) Generate(state *statePkg.State) []prometheus.Collecto

data, _ := dataRaw.(fetchersPkg.RewardsData)

for chain, rewards := range data.Rewards {
for validator, validatorReward := range rewards {
for _, balance := range validatorReward {
for _, chain := range g.Chains {
chainRewards, ok := data.Rewards[chain.Name]
if !ok {
continue
}

for _, validator := range chain.Validators {
validatorRewards, ok := chainRewards[validator.Address]
if !ok {
continue
}

for _, balance := range validatorRewards {
amountConverted := chain.Denoms.Convert(&balance)
selfDelegationRewardsTokens.With(prometheus.Labels{
"chain": chain,
"address": validator,
"denom": balance.Denom,
}).Set(balance.Amount)
"chain": chain.Name,
"address": validator.Address,
"denom": amountConverted.Denom,
}).Set(amountConverted.Amount)
}
}
}
Expand Down
30 changes: 25 additions & 5 deletions pkg/generators/rewards_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package generators

import (
"main/pkg/config"
"main/pkg/constants"
"main/pkg/fetchers"
statePkg "main/pkg/state"
Expand All @@ -17,7 +18,7 @@ func TestRewardsGeneratorNoState(t *testing.T) {
t.Parallel()

state := statePkg.NewState()
generator := NewRewardsGenerator()
generator := NewRewardsGenerator([]*config.Chain{})
results := generator.Generate(state)
assert.Empty(t, results)
}
Expand All @@ -30,21 +31,40 @@ func TestRewardsGeneratorNotEmptyState(t *testing.T) {
Rewards: map[string]map[string][]types.Amount{
"chain": {
"validator": []types.Amount{
{Amount: 1, Denom: "denom"},
{Amount: 100000, Denom: "uatom"},
{Amount: 200000, Denom: "ustake"},
},
},
},
})

generator := NewRewardsGenerator()
chains := []*config.Chain{
{
Name: "chain",
Validators: []config.Validator{{Address: "validator"}, {Address: "validator2"}},
Denoms: config.DenomInfos{
{Denom: "uatom", DisplayDenom: "atom", DenomCoefficient: 1000000},
},
},
{
Name: "chain2",
Validators: []config.Validator{{Address: "validator"}},
},
}
generator := NewRewardsGenerator(chains)
results := generator.Generate(state)
assert.NotEmpty(t, results)

gauge, ok := results[0].(*prometheus.GaugeVec)
assert.True(t, ok)
assert.InEpsilon(t, float64(1), testutil.ToFloat64(gauge.With(prometheus.Labels{
assert.InEpsilon(t, 0.1, testutil.ToFloat64(gauge.With(prometheus.Labels{
"chain": "chain",
"address": "validator",
"denom": "atom",
})), 0.01)
assert.InEpsilon(t, float64(200000), testutil.ToFloat64(gauge.With(prometheus.Labels{
"chain": "chain",
"address": "validator",
"denom": "denom",
"denom": "ustake",
})), 0.01)
}
29 changes: 21 additions & 8 deletions pkg/generators/self_delegation.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package generators

import (
"main/pkg/config"
"main/pkg/constants"
fetchersPkg "main/pkg/fetchers"
statePkg "main/pkg/state"
Expand All @@ -9,10 +10,11 @@ import (
)

type SelfDelegationGenerator struct {
Chains []*config.Chain
}

func NewSelfDelegationGenerator() *SelfDelegationGenerator {
return &SelfDelegationGenerator{}
func NewSelfDelegationGenerator(chains []*config.Chain) *SelfDelegationGenerator {
return &SelfDelegationGenerator{Chains: chains}
}

func (g *SelfDelegationGenerator) Generate(state *statePkg.State) []prometheus.Collector {
Expand All @@ -31,13 +33,24 @@ func (g *SelfDelegationGenerator) Generate(state *statePkg.State) []prometheus.C

data, _ := dataRaw.(fetchersPkg.SelfDelegationData)

for chain, delegations := range data.Delegations {
for validator, delegation := range delegations {
for _, chain := range g.Chains {
chainDelegations, ok := data.Delegations[chain.Name]
if !ok {
continue
}

for _, validator := range chain.Validators {
validatorSelfDelegation, ok := chainDelegations[validator.Address]
if !ok {
continue
}

amountConverted := chain.Denoms.Convert(validatorSelfDelegation)
selfDelegatedTokensGauge.With(prometheus.Labels{
"chain": chain,
"address": validator,
"denom": delegation.Denom,
}).Set(delegation.Amount)
"chain": chain.Name,
"address": validator.Address,
"denom": amountConverted.Denom,
}).Set(amountConverted.Amount)
}
}

Expand Down
34 changes: 29 additions & 5 deletions pkg/generators/self_delegations_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package generators

import (
"main/pkg/config"
"main/pkg/constants"
"main/pkg/fetchers"
statePkg "main/pkg/state"
Expand All @@ -17,7 +18,7 @@ func TestSelfDelegationGeneratorNoState(t *testing.T) {
t.Parallel()

state := statePkg.NewState()
generator := NewSelfDelegationGenerator()
generator := NewSelfDelegationGenerator([]*config.Chain{})
results := generator.Generate(state)
assert.Empty(t, results)
}
Expand All @@ -29,20 +30,43 @@ func TestSelfDelegationGeneratorNotEmptyState(t *testing.T) {
state.Set(constants.FetcherNameSelfDelegation, fetchers.SelfDelegationData{
Delegations: map[string]map[string]*types.Amount{
"chain": {
"validator": {Amount: 1, Denom: "denom"},
"validator": {Amount: 100000, Denom: "uatom"},
"validator2": {Amount: 200000, Denom: "ustake"},
},
},
})

generator := NewSelfDelegationGenerator()
chains := []*config.Chain{
{
Name: "chain",
Validators: []config.Validator{
{Address: "validator"},
{Address: "validator2"},
{Address: "validator3"},
},
Denoms: config.DenomInfos{
{Denom: "uatom", DisplayDenom: "atom", DenomCoefficient: 1000000},
},
},
{
Name: "chain2",
Validators: []config.Validator{{Address: "validator"}},
},
}
generator := NewSelfDelegationGenerator(chains)
results := generator.Generate(state)
assert.NotEmpty(t, results)

gauge, ok := results[0].(*prometheus.GaugeVec)
assert.True(t, ok)
assert.InEpsilon(t, float64(1), testutil.ToFloat64(gauge.With(prometheus.Labels{
assert.InEpsilon(t, 0.1, testutil.ToFloat64(gauge.With(prometheus.Labels{
"chain": "chain",
"address": "validator",
"denom": "denom",
"denom": "atom",
})), 0.01)
assert.InEpsilon(t, float64(200000), testutil.ToFloat64(gauge.With(prometheus.Labels{
"chain": "chain",
"address": "validator2",
"denom": "ustake",
})), 0.01)
}

0 comments on commit be14b4b

Please sign in to comment.