Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: parsing in staking params #64

Merged
merged 1 commit into from
Nov 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions internal/clients/bbnclient/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type StakingParams struct {
UnbondingFeeSat int64 `bson:"unbonding_fee_sat"`
MinCommissionRate string `bson:"min_commission_rate"`
DelegationCreationBaseGasFee uint64 `bson:"delegation_creation_base_gas_fee"`
AllowListExpirationHeight uint64 `bson:"allow_list_expiration_height"`
}

type CheckpointParams struct {
Expand All @@ -46,6 +47,7 @@ func FromBbnStakingParams(params stakingtypes.Params) *StakingParams {
UnbondingFeeSat: params.UnbondingFeeSat,
MinCommissionRate: params.MinCommissionRate.String(),
DelegationCreationBaseGasFee: params.DelegationCreationBaseGasFee,
AllowListExpirationHeight: params.AllowListExpirationHeight,
jrwbabylonlab marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
22 changes: 18 additions & 4 deletions internal/db/model/params.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
package model

type GlobalParamsDocument struct {
Type string `bson:"type"`
Version uint32 `bson:"version"`
Params interface{} `bson:"params"`
import "github.com/babylonlabs-io/babylon-staking-indexer/internal/clients/bbnclient"

// Base document for common fields
type BaseParamsDocument struct {
Type string `bson:"type"`
Version uint32 `bson:"version"`
}

// Specific document for staking params
type StakingParamsDocument struct {
BaseParamsDocument `bson:",inline"`
Params *bbnclient.StakingParams `bson:"params"`
}

// Specific document for checkpoint params
type CheckpointParamsDocument struct {
BaseParamsDocument `bson:",inline"`
Params *bbnclient.CheckpointParams `bson:"params"`
}
55 changes: 24 additions & 31 deletions internal/db/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,54 +23,47 @@ const (
func (db *Database) SaveStakingParams(
ctx context.Context, version uint32, params *bbnclient.StakingParams,
) error {
collection := db.client.Database(db.dbName).
Collection(model.GlobalParamsCollection)

filter := bson.M{
"type": STAKING_PARAMS_TYPE,
"version": version,
}
collection := db.client.Database(db.dbName).Collection(model.GlobalParamsCollection)

update := bson.M{
"$setOnInsert": &model.GlobalParamsDocument{
doc := &model.StakingParamsDocument{
BaseParamsDocument: model.BaseParamsDocument{
Type: STAKING_PARAMS_TYPE,
Version: version,
Params: params,
},
Params: params,
}

_, err := collection.UpdateOne(ctx, filter, update, options.Update().SetUpsert(true))
if err != nil {
return fmt.Errorf("failed to save staking params: %w", err)
filter := bson.M{
"type": STAKING_PARAMS_TYPE,
"version": version,
}
return nil
update := bson.M{"$setOnInsert": doc}

_, err := collection.UpdateOne(ctx, filter, update, options.Update().SetUpsert(true))
return err
}

func (db *Database) SaveCheckpointParams(
ctx context.Context, params *bbnclient.CheckpointParams,
) error {
collection := db.client.Database(db.dbName).
Collection(model.GlobalParamsCollection)

filter := bson.M{
"type": CHECKPOINT_PARAMS_TYPE,
"version": CHECKPOINT_PARAMS_VERSION,
}
collection := db.client.Database(db.dbName).Collection(model.GlobalParamsCollection)

update := bson.M{
"$setOnInsert": &model.GlobalParamsDocument{
doc := &model.CheckpointParamsDocument{
BaseParamsDocument: model.BaseParamsDocument{
Type: CHECKPOINT_PARAMS_TYPE,
Version: CHECKPOINT_PARAMS_VERSION,
Params: params,
Version: CHECKPOINT_PARAMS_VERSION, // hardcoded as 0
},
Params: params,
}

_, err := collection.UpdateOne(ctx, filter, update, options.Update().SetUpsert(true))
if err != nil {
return fmt.Errorf("failed to save checkpoint params: %w", err)
filter := bson.M{
"type": CHECKPOINT_PARAMS_TYPE,
"version": CHECKPOINT_PARAMS_VERSION, // hardcoded as 0
}
update := bson.M{"$setOnInsert": doc}

return nil
_, err := collection.UpdateOne(ctx, filter, update, options.Update().SetUpsert(true))
return err
}

func (db *Database) GetStakingParams(ctx context.Context, version uint32) (*bbnclient.StakingParams, error) {
Expand All @@ -82,11 +75,11 @@ func (db *Database) GetStakingParams(ctx context.Context, version uint32) (*bbnc
"version": version,
}

var params model.GlobalParamsDocument
var params model.StakingParamsDocument
err := collection.FindOne(ctx, filter).Decode(&params)
if err != nil {
return nil, fmt.Errorf("failed to get staking params: %w", err)
}

return params.Params.(*bbnclient.StakingParams), nil
return params.Params, nil
}
Loading