Skip to content

Commit

Permalink
feat(graphql): implements bsaci validator query
Browse files Browse the repository at this point in the history
  • Loading branch information
amimart committed Nov 25, 2022
1 parent 9c4a63f commit 88731cf
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 6 deletions.
42 changes: 40 additions & 2 deletions app/nemeton/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@ import (

"okp4/nemeton-leaderboard/app/util"

"github.com/cosmos/cosmos-sdk/types"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
)

const collectionName = "phases"
const (
phasesCollectionName = "phases"
validatorsCollectionName = "validators"
)

type Store struct {
db *mongo.Database
Expand All @@ -34,7 +39,7 @@ func NewStore(ctx context.Context, mongoURI, dbName string) (*Store, error) {
}

func (s *Store) init(ctx context.Context) error {
phases := s.db.Collection(collectionName)
phases := s.db.Collection(phasesCollectionName)
count, err := phases.CountDocuments(ctx, bson.M{})
if err != nil {
return err
Expand Down Expand Up @@ -108,3 +113,36 @@ func (s *Store) GetPhases(criteriaFn func(p Phase) bool) []*Phase {
}
return filtered
}

func (s *Store) GetValidatorByID(ctx context.Context, id primitive.ObjectID) (*Validator, error) {
return s.GetValidatorBy(ctx, bson.M{"_id": id})
}

func (s *Store) GetValidatorByValoper(ctx context.Context, addr types.ValAddress) (*Validator, error) {
return s.GetValidatorBy(ctx, bson.M{"valoper": addr.String()})
}

func (s *Store) GetValidatorByDelegator(ctx context.Context, addr types.AccAddress) (*Validator, error) {
return s.GetValidatorBy(ctx, bson.M{"delegator": addr.String()})
}

func (s *Store) GetValidatorByDiscord(ctx context.Context, discord string) (*Validator, error) {
return s.GetValidatorBy(ctx, bson.M{"discord": discord})
}

func (s *Store) GetValidatorByTwitter(ctx context.Context, twitter string) (*Validator, error) {
return s.GetValidatorBy(ctx, bson.M{"twitter": twitter})
}

func (s *Store) GetValidatorBy(ctx context.Context, filter bson.M) (*Validator, error) {
res := s.db.Collection(validatorsCollectionName).FindOne(ctx, filter)
if err := res.Err(); err != nil {
if err == mongo.ErrNoDocuments {
return nil, nil
}
return nil, err
}

var val Validator
return &val, res.Decode(&val)
}
9 changes: 7 additions & 2 deletions app/nemeton/validator.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package nemeton

import "github.com/cosmos/cosmos-sdk/types"
import (
"net/url"

"github.com/cosmos/cosmos-sdk/types"
)

type Validator struct {
Moniker string `bson:"moniker"`
Identity *int64 `bson:"identity,omitempty"`
Identity *string `bson:"identity,omitempty"`
Valoper types.ValAddress `bson:"valoper"`
Delegator types.AccAddress `bson:"delegator"`
Twitter *string `bson:"twitter,omitempty"`
Website *url.URL `bson:"website,omitempty"`
Discord string `bson:"discord"`
Country string `bson:"country"`
Status string `bson:"status"`
Expand Down
78 changes: 78 additions & 0 deletions graphql/generated/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions graphql/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,11 @@ type Validator {
"""
twitter: String

"""
The validator website.
"""
website: URI

"""
The validator discord account.
"""
Expand Down
20 changes: 19 additions & 1 deletion graphql/schema.resolvers.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,25 @@ func (r *queryResolver) ValidatorCount(ctx context.Context) (int, error) {

// Validator is the resolver for the validator field.
func (r *queryResolver) Validator(ctx context.Context, cursor *primitive.ObjectID, rank *int, valoper types.ValAddress, delegator types.AccAddress, discord *string, twitter *string) (*nemeton.Validator, error) {
panic(fmt.Errorf("not implemented: Validator - validator"))
if cursor != nil {
return r.store.GetValidatorByID(ctx, *cursor)
}
if rank != nil {
panic(fmt.Errorf("not implemented: Validator - validator"))
}
if !valoper.Empty() {
return r.store.GetValidatorByValoper(ctx, valoper)
}
if !delegator.Empty() {
return r.store.GetValidatorByDelegator(ctx, delegator)
}
if discord != nil {
return r.store.GetValidatorByDiscord(ctx, *discord)
}
if twitter != nil {
return r.store.GetValidatorByTwitter(ctx, *twitter)
}
return nil, fmt.Errorf("one option must be passed")
}

// Rank is the resolver for the rank field.
Expand Down
17 changes: 16 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
package main

import "okp4/nemeton-leaderboard/cmd"
import (
"okp4/nemeton-leaderboard/cmd"

"github.com/cosmos/cosmos-sdk/types"
)

const (
accprefix = "okp4"
valprefix = "okp4valoper"
consprefix = "okp4valcons"
)

func main() {
conf := types.GetConfig()
conf.SetBech32PrefixForAccount(accprefix, accprefix)
conf.SetBech32PrefixForValidator(valprefix, valprefix)
conf.SetBech32PrefixForConsensusNode(consprefix, consprefix)

cmd.Execute()
}

0 comments on commit 88731cf

Please sign in to comment.